Commit Graph

14405 Commits

Author SHA1 Message Date
George Koehler
e68f640dee [ELF][PPC32] Make R_PPC32_PLTREL retain .got
PLT usage needs the first 12 bytes of the .got section. We need to keep .got and
DT_GOT_PPC even if .got/_GLOBAL_OFFSET_TABLE_ are not referenced (large PIC code
may only reference .got2), which is the case in OpenBSD's ld.so, leading
to a misleading error, "unsupported insecure BSS PLT object".

Fix this by adding R_PPC32_PLTREL to the list of hasGotOffRel.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D114982

(cherry picked from commit 885fb9a257)
2021-12-16 18:06:50 -08:00
Fangrui Song
724ed207b7 [ELF] Hint -z nostart-stop-gc for __start_ undefined references
Make users aware what to do with ld.lld 13.0.0 / GNU ld<2015-10 --gc-sections
behavior.

Differential Revision: https://reviews.llvm.org/D114830

(cherry picked from commit 353fe72ca3)
2021-12-14 16:49:13 -08:00
Igor Kudrin
52bfd2c1cc [ELF] Do not report undefined weak references in shared libraries
This fixes an issue introduced in D101996.

A weak reference in a shared library could be incorrectly reported if
there is another library that has a strong reference to the same symbol.

Differential Revision: https://reviews.llvm.org/D115041

(cherry picked from commit ce25eb12dd)
2021-12-13 15:47:32 -08:00
Tom Stellard
8a93745a71 Remove "In Progress" text from release notes 2021-10-18 17:12:51 -07:00
Simon Atanasyan
6aa054242d [LLD] Add required ppc target to the test cases. NFC
(cherry picked from commit 454f69bcc1)
2021-09-17 20:51:23 -07:00
Simon Atanasyan
4728892cd3 [LLD] Support compressed input sections on big-endian targets
This patch enables compressed input sections on big-endian targets by
checking the target endianness and selecting an appropriate `Chdr`
structure.

Fixes PR51369

Differential Revision: https://reviews.llvm.org/D107635

(cherry picked from commit c6ebc651b6)
2021-09-10 15:36:33 -07:00
Fangrui Song
08dd644d07 ReleaseNotes: add lld/ELF notes
For the release/13.x branch.

Differential Revision: https://reviews.llvm.org/D107782
2021-08-17 09:44:19 -07:00
Martin Storsjö
87d56ad441 [LLD] [MinGW] Add more options for disabling flags in the executable
In e72403f96d, we added the flag
"--no-dynamicbase" for disabling the dynamicbase flag which we set
by default. At the time, ld.bfd didn't have any corresponding
option (as ld.bfd defaulted to not setting the flag). Almost at
the same time, corresponding options were added to ld.bfd for
disabling it (while it was being enabled by default), with a
different name, "--disable-dynamicbase".

Thus add the "--disable-dynamicbase" option. Make this default
one advertised in the help listing, but keep the "--no-dynamicbase"
form as an alias. Also improve checking for the last option set
if there are multiple ones on the same command line.

Also add corresponding disable options for a lot of other flags
that we set by default, also added in ld.bfd in the same commit:
https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=514b4e191d5f46de8e142fe216e677a35fa9c4bb

Differential Revision: https://reviews.llvm.org/D107930

(cherry picked from commit f8340c8c5d)
2021-08-16 20:39:35 -07:00
Jez Ng
1bbe8ef815 [lld-macho] Fill out release notes for 13.x
I probably missed out some things, given how much work was done in the
last few months...

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D107922
2021-08-16 13:19:00 -04:00
Martin Storsjö
dcdb12496c Add release notes for things relating to MinGW in the release 2021-08-16 12:26:49 +03:00
Pan, Tao
4d9937065a Fix gcc build error after D105519
Same as 3bec7ed59e

Reviewed By: sbc100

Differential Revision: https://reviews.llvm.org/D107422

(cherry picked from commit c70fa6da9a)
2021-08-11 21:38:23 -07:00
Martin Storsjö
8f24d17869 [LLD] [MinGW] Support both "--opt value" and "--opt=value" for more options
This does the same fix as D107237 but for a couple more options,
converting all remaining cases of such options to accept both
forms, for consistency. This fixes building e.g. openldap, which
uses --image-base=<value>.

Differential Revision: https://reviews.llvm.org/D107253

(cherry picked from commit b7fb5b54a9)
2021-08-09 23:24:23 -07:00
Fangrui Song
63081c9bc1 [ELF] Apply version script patterns to non-default version symbols
Currently version script patterns are ignored for .symver produced
non-default version (single @) symbols. This makes such symbols
not localizable by `local:`, e.g.

```
.symver foo3_v1,foo3@v1
.globl foo_v1
foo3_v1:

ld.lld --version-script=a.ver -shared a.o
```

This patch adds the support:

* Move `config->versionDefinitions[VER_NDX_LOCAL].patterns` to `config->versionDefinitions[versionId].localPatterns`
* Rename `config->versionDefinitions[versionId].patterns` to `config->versionDefinitions[versionId].nonLocalPatterns`
* Allow `findAllByVersion` to find non-default version symbols when `includeNonDefault` is true. (Note: `symtab` keys do not have `@@`)
* Make each pattern check both the unversioned `pat.name` and the versioned `${pat.name}@${v.name}`
* `localPatterns` can localize `${pat.name}@${v.name}`. `nonLocalPatterns` can prevent localization by assigning `verdefIndex` (before `parseSymbolVersion`).

---

If a user notices new `undefined symbol` errors with a version script containing
`local: *;`, the issue is likely due to a missing `global:` pattern.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D107234

(cherry picked from commit 00809c8889)
2021-08-05 10:42:02 -07:00
Fangrui Song
6b97268c56 Revert "[ELF] Apply version script patterns to non-default version symbols"
This reverts commit 7ed22a6fa9.

buf is not cleared so the commit misses some cases.

(cherry picked from commit a533eb7423)
2021-08-05 10:41:56 -07:00
Fangrui Song
6b6aef5dba [ELF] Support copy relocation on non-default version symbols
Copy relocation on a non-default version symbol is unsupported and can crash at
runtime. Fortunately there is a one-line fix which works for most cases:
ensure `getSymbolsAt` unconditionally returns `ss`.

If two non-default version symbols are defined at the same place and both
are copy relocated, our implementation will copy relocated them into different
addresses. The pointer inequality is very unlikely an issue. In GNU ld, copy
relocating version aliases seems to create more pointer inequality problems than
us.

(
In glibc, sys_errlist@GLIBC_2.2.5 sys_errlist@GLIBC_2.3 sys_errlist@GLIBC_2.4
are defined at the same place, but it is unlikely they are all copy relocated in
one executable. Even if so, the variables are read-only and pointer inequality
should not be a problem.
)

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D107535

(cherry picked from commit 72d070b4db)
2021-08-05 10:33:31 -07:00
Fangrui Song
4783a6cdf0 [ELF] Combine foo@v1 and foo with the same versionId if both are defined
Due to an assembler design flaw (IMO), `.symver foo,foo@v1` produces two symbols `foo` and `foo@v1` if `foo` is defined.

* `v1 {};` produces both `foo` and `foo@v1`, but GNU ld only produces `foo@v1`
* `v1 { foo; };` produces both `foo@@v1` and `foo@v1`, but GNU ld only produces `foo@v1`
* `v2 { foo; };` produces both `foo@@v2` and `foo@v1`, matching GNU ld. (Tested by symver.s)

This patch implements the GNU ld behavior by reusing the symbol redirection mechanism
in D92259. The new test symver-non-default.s checks the first two cases.

Without the patch, the second case will produce `foo@v1` and `foo@@v1` which
looks weird and makes foo unnecessarily default versioned.

Note: `.symver foo,foo@v1,remove` exists but the unfortunate `foo` will not go
away anytime soon.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D107235
2021-08-04 09:18:10 -07:00
Fangrui Song
17edcb3a6e [ELF] Apply version script patterns to non-default version symbols
Currently version script patterns are ignored for .symver produced
non-default version (single @) symbols. This makes such symbols
not localizable by `local:`, e.g.

```
.symver foo3_v1,foo3@v1
.globl foo_v1
foo3_v1:

ld.lld --version-script=a.ver -shared a.o
# In a.out, foo3@v1 is incorrectly exported.
```

This patch adds the support:

* Move `config->versionDefinitions[VER_NDX_LOCAL].patterns` to `config->versionDefinitions[versionId].localPatterns`
* Rename `config->versionDefinitions[versionId].patterns` to `config->versionDefinitions[versionId].nonLocalPatterns`
* Allow `findAllByVersion` to find non-default version symbols when `includeNonDefault` is true. (Note: `symtab` keys do not have `@@`)
* Make each pattern check both the unversioned `pat.name` and the versioned `${pat.name}@${v.name}`
* `localPatterns` can localize `${pat.name}@${v.name}`. `nonLocalPatterns` can prevent localization by assigning `verdefIndex` (before `parseSymbolVersion`).

---

If a user notices new `undefined symbol` errors with a version script containing
`local: *;`, the issue is likely due to a missing `global:` pattern.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D107234
2021-08-04 09:18:02 -07:00
Fangrui Song
7e69b1728c [ELF][test] Improve .symver & --version-script tests
And delete redundant tests.
2021-08-04 09:17:51 -07:00
Fangrui Song
73ea8254d2 [ELF] Make dot in .tbss correct
GNU ld doesn't support multiple SHF_TLS SHT_NOBITS output sections (it restores
the address after an SHF_TLS SHT_NOBITS section, so consecutive SHF_TLS
SHT_NOBITS sections will have conflicting address ranges).

That said, `threadBssOffset` implements limited support for consecutive SHF_TLS
SHT_NOBITS sections. (SHF_TLS SHT_PROGBITS following a SHF_TLS SHT_NOBITS can still be
incorrect.)

`.` in an output section description of an SHF_TLS SHT_NOBITS section is
incorrect. (https://lists.llvm.org/pipermail/llvm-dev/2021-July/151974.html)

This patch saves the end address of the previous tbss section in
`ctx->tbssAddr`, changes `dot` in the beginning of `assignOffset` so
that `.` evaluation will be correct.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D107208

(cherry picked from commit 9bd29a73d1)
2021-08-04 09:06:59 -07:00
Mateusz Mikuła
a6c39006e5 [LLD][MinGW] Accept joined format for --stack
Postgresql uses `--stack=` in its Makefile.

Downstream issue: https://github.com/msys2/MINGW-packages/pull/9167

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D107237

(cherry picked from commit 05b025edf4)
2021-08-02 21:26:03 +03:00
Fangrui Song
ad5a6b15ff [ELF] Add -Bsymbolic-non-weak-functions
This option is a subset of -Bsymbolic-functions. It applies to STB_GLOBAL
STT_FUNC definitions.

The address of a vague linkage function (STB_WEAK STT_FUNC, e.g. an inline
function, a template instantiation) seen by a -Bsymbolic-functions linked
shared object may be different from the address seen from outside the shared
object. Such cases are uncommon. (ELF/Mach-O programs may use
`-fvisibility-inlines-hidden` to break such pointer equality.  On Windows,
correct dllexport and dllimport are needed to make pointer equality work.
Windows link.exe enables /OPT:ICF by default so different inline functions may
have the same address.)

```
// a.cc -> a.o -> a.so (-Bsymbolic-functions)
inline void f() {}
void *g() { return (void *)&f; }

// b.cc -> b.o -> exe
// The address is different!
inline void f() {}
```

-Bsymbolic-non-weak-functions is a safer (C++ conforming) subset of
-Bsymbolic-functions, which can make such programs work.

Implementations usually emit a vague linkage definition in a COMDAT group.  We
could detect the group (with more code) but I feel that we should just check
STB_WEAK for simplicity. A weak definition will thus serve as an escape hatch
for rare cases when users want interposition on definitions.

GNU ld feature request: https://sourceware.org/bugzilla/show_bug.cgi?id=27871

Longer write-up: https://maskray.me/blog/2021-05-16-elf-interposition-and-bsymbolic

If Linux distributions migrate to protected non-vague-linkage external linkage
functions by default, the linker option can still be handy because it allows
rapid experiment without recompilation. Protected function addresses currently
have deep issues in GNU ld.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D102570

(cherry picked from commit b06426da76)
2021-07-29 19:29:45 -07:00
Fangrui Song
323b9bf862 [lld] Replace LLVM_ATTRIBUTE_NORETURN with [[noreturn]]
[[noreturn]] can be used since 2016 when the minimum compiler requirement was bumped to GCC 4.8/MSVC 2015.
2021-07-27 18:51:17 -07:00
Fangrui Song
b00c8ab1b9 Revert "[ELF] --gc-sections: allow GC on reserved sections in a group"
clang may place dynamic initializations for explicitly specialized class
template static data members in comdat.
Such in-comdat SHT_INIT_ARRAY was an abuse but we have to work around it for a while.
2021-07-27 16:34:32 -07:00
Amilendra Kodithuwakku
b9cf1769de [lld][ELF] remove empty SyntheticSections from inputSections
Change removeUnusedSyntheticSections() to actually remove empty
SyntheticSections in inputSections.

In addition to doing what removeUnusedSyntheticSections() was meant
to do, this will also make the shuffle-sections tests, which shuffles
inputSections, less sensitive to empty Synthetic Sections that
will not appear in the final image.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D106427

Change-Id: I589eaf596472161a4395fb658aea0fad73318088
2021-07-27 23:29:02 +01:00
Nico Weber
dd57915b1e [lld/mac] Fix sub-library.s on Windows after 8e8701abca
The endswith() check for the framework name fails when joining
with the native path separator. Always use the posix separator as fix.
2021-07-27 15:25:52 -04:00
Nico Weber
e26356a00e [lld/mac] Fix application-extension.s failure after 8e8701abca
The test accidentally tested something else that makes lld fail
with a different (correct-looking) error that wasn't the one the
test tries to test for. (The test case before this change makes
ld64 hang in an infinite loop.)
2021-07-27 14:39:43 -04:00
Nico Weber
8e8701abca [lld/mac] When loading reexports, look for basename in -F / -L first
Matches ld64 (cf Options::findIndirectDylib()), and fixes PR51218.

Differential Revision: https://reviews.llvm.org/D106842
2021-07-27 14:28:52 -04:00
Derek Schuff
cf54424a46 [lld][WebAssembly] Do not remove name section with --strip-debug
Leave the name section in the output when using the --strip-debug
flag. This treats it more like ELF symbol tables, as the name
section has similar uses at runtime (e.g. wasm engines understand
it and it can be used for symbolization at runtime).

Fixes https://github.com/emscripten-core/emscripten/issues/14623

Differential Revision: https://reviews.llvm.org/D106728
2021-07-26 11:06:52 -07:00
Fangrui Song
c0da287c30 [yaml2obj][MachO] Rename PayloadString to Content
The new name is conciser and matches yaml2obj ELF & DWARF.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D106759
2021-07-26 09:04:51 -07:00
Fangrui Song
e7a7ad134f [ELF] Support quoted symbols in symbol assignments
glibc/elf/tst-absolute-zero-lib.lds uses `"absolute" = 0;`
2021-07-25 16:26:37 -07:00
Nico Weber
75e7d1320c [lld/mac] Make comment style uniform in start-end.s test 2021-07-25 18:37:49 -04:00
Nico Weber
80caa1eb4a [lld/mac] Add support for segment$start$ and segment$end$ symbols
These symbols are somewhat interesting in that they create non-existing
segments, which as far as I know is the only way to create segments
that don't contain any sections.

Final part of part of PR50760. Like D106629, but for segments instead
of sections. I'm not aware of anything that needs this in practice.

Differential Revision: https://reviews.llvm.org/D106767
2021-07-25 18:25:13 -04:00
Nico Weber
afdeb432f0 [lld/mac] Move output segment rename logic into OutputSegment
Fixes the output segment name if both -rename_section and
-rename_segment are used and the post-section-rename segment
name is the same as the pre-segment-rename segment name to
match ld64's behavior.

The motivation is that segment$start$ can create section-less segments,
and this makes a corner case in the interaction between segment$start and
-rename_segment in the upcoming segment$start patch.

Differential Revision: https://reviews.llvm.org/D106766
2021-07-25 18:20:09 -04:00
Nico Weber
6bf7d2d9c9 [lld/mac] Reland: Add tests for the interaction between -rename_section and -rename_segment
No behavior change.

Differential Revision: https://reviews.llvm.org/D106765
2021-07-25 18:16:33 -04:00
Nico Weber
14bb6e4d70 Revert "[lld/mac] Add tests for the interaction between -rename_section and -rename_segment"
This reverts commit a6eb34624d.
The test fails, I screwed something up.
2021-07-25 18:11:36 -04:00
Nico Weber
a6eb34624d [lld/mac] Add tests for the interaction between -rename_section and -rename_segment
No behavior change.

Differential Revision: https://reviews.llvm.org/D106765
2021-07-25 18:03:25 -04:00
Ayke van Laethem
13ca0c87ed
[lld][WebAssembly] Align __heap_base
__heap_base was not aligned. In practice, it will often be aligned
simply because it follows the stack, but when the stack is placed at the
beginning (with the --stack-first option), the __heap_base might be
unaligned. It could even be byte-aligned.

At least wasi-libc appears to expect that __heap_base is aligned:
659ff41456/dlmalloc/src/malloc.c (L5224)

While WebAssembly itself does not appear to require any alignment for
memory accesses, it is sometimes required when sharing a pointer
externally. For example, WASI might expect alignment up to 8:
https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#-timestamp-u64

This issue got introduced with the addition of the --stack-first flag:
https://reviews.llvm.org/D46141
I suspect the lack of alignment wasn't intentional here.

Differential Revision: https://reviews.llvm.org/D106499
2021-07-24 14:03:26 +02:00
Nico Weber
92c085e7c4 [lld/mac] Fix comment typo in new start-end.s test 2021-07-23 18:14:38 -04:00
Nico Weber
04f5eb407c [lld/mac] Fix start-stop.s test with expensive checks enabled
See e.g. https://lab.llvm.org/buildbot/#/builders/16/builds/14317
Not 100% sure why this fails yet, but this fixes it. Let's get
the bots green again first :)

Differential Revision: https://reviews.llvm.org/D106711
2021-07-23 17:01:16 -04:00
Nico Weber
04e8d0b62d [lld/mac] Implement support for section$start and section$ end symbols
With this, libclang_rt.profile_osx.a can be linked, that is coverage
and PGO-instrumented builds should now work with lld.

section$start and section$end symbols can create non-existing sections.
They're also undefined symbols that are only magic if there isn't a
regular symbol with their name, which means the need to be handled
in treatUndefined() instead of just looping over all existing
sections and adding start and end symbols like the ELF port does.

To represent the actual symbols, this uses absolute symbols that
get their value updated once an output section is layed out.

segment$start and segment$end are still missing for now, but they produce a
nicer error message after this patch.

Main part of PR50760.

Differential Revision: https://reviews.llvm.org/D106629
2021-07-23 16:01:09 -04:00
Jez Ng
d9a639901f [lld-macho][nfc] Add test for resolution of bitcode symbols
We lacked a test for bitcode symbol precedence. We assumed that
they followed the same rules as their regular symbol counterparts, but
never had a test to verify that we were matching ld64's behavior. It
turns out that we were largely correct, though we deviate from ld64 when
there are bitcode and non-bitcode symbols of the same name. The test
added in this diff both verifies our behavior and documents the
differences.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D106596
2021-07-23 11:49:00 -04:00
Jez Ng
cafed6f292 [lld-macho][nfc] Fix test to reflect that symbol attributes don't matter within an archive
We had a comment that claimed that defined symbols had priority
over common symbols if they occurred in the same archive. In fact, they
appear to have equal precedence. Our implementation already does this,
so I'm just updating the test comment. Also added a few other test
comments along the way for readability.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D106595
2021-07-23 11:49:00 -04:00
Jez Ng
3313b84481 [lld-macho] ICF: Do more work in equalsConstant, less in equalsVariable
In particular, relocations to absolute symbols or literal sections can
be handled in equalsConstant(), since their output addresses will not
change across each iteration of ICF. Offsets and addends can also be
dealt with entirely in equalsConstant(), making the code somewhat easier
to reason about. Only ConcatInputSections need to be handled in
equalsVariable().

LLD-ELF's implementation takes a similar approach.

Although this should make ICF do less work, in practice it seems like
there is no stat sig difference in time taken when linking
chromium_framework.

This refactor is motivated by an upcoming diff which improves ICF's handling of
addends.

Reviewed By: #lld-macho, gkm

Differential Revision: https://reviews.llvm.org/D106212
2021-07-23 11:49:00 -04:00
Jez Ng
8eac5dcb36 [lld-macho] Reorganize + extend ICF test
I found icf.s a bit hard to work with as it was not possible to
extend any of the functions `_a` ... `_k` to test new relocation /
referent types without modifying every single one of them. Additionally,
their one-letter names were not descriptive (though the comments
helped).

I've renamed all the functions to reflect the feature they are testing,
and shrunk them so that they contain just enough to test that one
feature.

I've also added tests for non-zero addends (via the
`_abs1a_ref_with_addend` and `_defined_ref_with_addend_1` functions).

Reviewed By: #lld-macho, gkm

Differential Revision: https://reviews.llvm.org/D106211
2021-07-23 11:49:00 -04:00
Nico Weber
9482aa98e5 [lld/mac] Let OutputSegment store its start address
segment$start$/segment$end$ symbols allow creating segments without
sections, so getting the segment address off the first section
won't work there. Storing the address on the segment is arguably a
bit simpler too.

No behavior change, part of PR50760.

Differential Revision: https://reviews.llvm.org/D106665
2021-07-23 11:43:25 -04:00
Nico Weber
2c508cf583 [lld/mac] Don't crash on absolute symbols in order files
Absolute symbols have a nullptr isec. buildInputSectionPriorities()
would defer isec, causing crashes. Ordering absolute symbols doesn't
make sense, so just ignore them. This seems to match ld64.

Differential Revision: https://reviews.llvm.org/D106628
2021-07-23 11:33:23 -04:00
Nico Weber
687181caba [lld/mac] Add missing REQUIRES line to new test 2021-07-23 10:40:22 -04:00
Leonard Grey
5acc6d4572 [lld-macho] Disambiguate bitcode files with the same name by archive name/offset in archive
Ported from COFF/ELF; test is adapted from
test/COFF/thinlto-archivecollision.ll

LTO expects every bitcode file to have a unique name. If given multiple bitcode
files with the same name, it errors with "Expected at most one ThinLTO module
per bitcode file".

This change incorporates the archive name, to disambiguate members with the
same name in different archives and the offset in archive to disambiguate
members with the same name in the same archive.

Differential Revision: https://reviews.llvm.org/D106179
2021-07-22 22:50:25 -04:00
Nico Weber
393116faad [lld/mac] Remove "else" after return
No behavior change
2021-07-22 21:31:52 -04:00
Fangrui Song
120b18767c [ELF] --gc-sections: allow GC on reserved sections in a group
This generalizes D70146 (SHT_NOTE) to more reserved sections and makes our rules
more consistent. Now SHF_GROUP is more similar to SHF_LINK_ORDER.

For SHT_INIT_ARRAY/SHT_FINI_ARRAY, the rule will be closer to PE/COFF link.exe.

Previously sanitizers use llvm.global_ctors to make module_ctor a GC
root, which is considered an abuse.
https://groups.google.com/g/generic-abi/c/TpleUEkNoQI

We can squeak through on compatibility issues because compilers otherwise don't
use SHF_GROUP special sections.
2021-07-22 17:09:23 -07:00