Commit Graph

15678 Commits

Author SHA1 Message Date
Keith Smiley 1d1aa2d013
[lld-macho] Mark aliased symbols as noDeadStrip
This matches ld64 behavior

Differential Revision: https://reviews.llvm.org/D135082
2022-10-04 14:53:53 -07:00
Keith Smiley 0634a8440d
[lld-macho][test] Add -lSystem to all lld invocations
This is a requirement from ld64, so this mirrors that expectation. Most
of these changes were just offsets from adding the library, or adding
dyld_stub_binder which is always added when linking ld64. This also adds
`%no-lsystem-lld` for the few cases where we're explicitly testing
things without that.

Fixes: https://github.com/llvm/llvm-project/issues/54184

Differential Revision: https://reviews.llvm.org/D135193
2022-10-04 14:10:43 -07:00
Nico Weber 062c660dbf [lld/mac] With -demangle, strip leading _ from non-mangled names
For

    void f();
    int main() { f(); }

`lld -demangle` now produces

    ld64.lld: error: undefined symbol: f
    >>> referenced by path/to/main.o:(symbol main+0x8)

instead of

    ld64.lld: error: undefined symbol: _f
    >>> referenced by path/to/main.o:(symbol _main+0x8)

previously. (Without `-demangle`, it still prints `_f` and `_main`.)

This does *not* match ld64's behavior, but it does match e.g. lld/COFF's
behaviour.

This is arguably easier to understand: clang prepends symbol names with `_`
on macOS, so it seems friendly if the linker removes it again in its
diagnostics. It also makes the `extern "C"` insertion diagnostics we added
recently look more self-consistent.

Differential Revision: https://reviews.llvm.org/D135189
2022-10-04 16:47:07 -04:00
Daniel Bertalan 0d30e92f59
[lld-macho] Add support for emitting chained fixups
This commit adds support for chained fixups, which were introduced in
Apple's late 2020 OS releases. This format replaces the dyld opcodes
used for supplying rebase and binding information, and encodes most of
that data directly in the memory location that will have the fixup
applied.

This reduces binary size and is a requirement for page-in linking, which
will be available starting with macOS 13.

A high-level overview of the format and my implementation can be found
in SyntheticSections.h.

This feature is currently gated behind the `-fixup_chains` flag, and
will be enabled by default for supported targets in a later commit.

Like in ld64, lazy binding is disabled when chained fixups are in use,
and the `-init_offsets` transformation is performed by default.

Differential Revision: https://reviews.llvm.org/D132560
2022-10-04 11:48:45 +02:00
Sam Clegg 0a9756fc15 [lld][WebAssemlby] Improve support for -L / -l and add testing
- Add support -Bdynamic/-Bstatic and their aliases
- Add support for `--library` and `--library-path` long form args
- Add test based on test/ELF/libsearch.s
- In `-Bdynamic` mode search for `.so` files in preference to `.a`.
- Unlike ELF continue to default to static mode until `-pie` or
  `-shared` are used.

Differential Revision: https://reviews.llvm.org/D135087
2022-10-03 16:53:30 -07:00
Nico Weber 8c45e80298 [lld/mac] Port typo correction for undefined symbols from ELF port
Ports:
- core feature: https://reviews.llvm.org/D67039
- case mismatch: https://reviews.llvm.org/D70506
- extern "C" suggestions: https://reviews.llvm.org/D69592,
  https://reviews.llvm.org/D69650

Does not port https://reviews.llvm.org/D71735 since I believe that that doesn't
apply to lld/Mach-O.

Differential Revision: https://reviews.llvm.org/D135038
2022-10-03 18:53:55 -04:00
oberrich 7f9c264b3c [lld/COFF] Ignore `/ASSEMBLYDEBUG[:NONE]` and `/EMITPOGOPHASEINFO`
Adds aforementioned link switches in lld-link and ignores them.

Differential revision: https://reviews.llvm.org/D135033
2022-10-03 20:38:36 +02:00
Nico Weber 248237364b [lld/mac] Extract a reportUndefinedSymbol function
Makes things look more similar to the ELF port, and removes some
slightly deep nesting.

No behavior change.

Differential Revision: https://reviews.llvm.org/D135032
2022-10-03 09:17:57 -04:00
Alvin Wong 0ce4f57052 [LLD][COFF] Reduce chance of symbol name collision with delay-load
Delay-loaded imports creats a load thunk with a symbol name. Before this
change, the name uses a `__imp_load_` prefix. On the other hand, normal
import uses the `__imp_` prefix for the import address pointer. If an
import symbol named `load_func` is imported normally and another named
`func` is imported using delay-load, this can cause a symbol name
collision.

This patch changes delay-load imports to use `__imp___load_` prefix.
Because it is less likely for normal imports to have a name starting in
`__load_` this should reduce the chance of a name collision.

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D134464
2022-10-03 10:58:45 +03:00
Alvin Wong e2e132c5d9 [LLD][COFF] Set OrdinalBase to 1 for export table
Before this, LLD sets OrdinalBase to 0, which deviates from usual
practices. This technically would allow LLD to export a symbol using
ordinal 0, however LLD never use export ordinal 0, which results in
binaries with export tables always having an empty export at ordinal 0.

This change makes LLD set OrdinalBase to 1 and not create the empty
export with ordinal 0, which makes its behaviour more in line with both
the MSVC linker and the GNU linker.

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D134140
2022-10-03 10:58:44 +03:00
Fangrui Song 9f9bab19e3 [ELF] Replace some config->ekind with file->ekind. NFC 2022-10-02 21:27:41 -07:00
Fangrui Song d9dbf9e30a [ELF] Move init from ELFFileBase constructor to a separate function. NFC 2022-10-02 21:10:28 -07:00
Fangrui Song 8bcf22e318 [ELF] Remove redundant getELFKind call. NFC 2022-10-02 20:16:13 -07:00
Fangrui Song c171250e38 [ELF] Simplify addFile. NFC 2022-10-02 19:49:17 -07:00
Fangrui Song 961439cd7e [ELF] Add LLVM_LIBRARY_VISIBILITY to some global variables. NFC 2022-10-02 13:23:52 -07:00
Fangrui Song 1837333dac [ELF] --check-sections: allow address 0xffffffff for ELFCLASS32
Fix https://github.com/llvm/llvm-project/issues/58101
2022-10-01 15:37:07 -07:00
Fangrui Song dd6aea9582 [ELF] Rename LinkerScript::ctx to state. NFC
To avoid name conflict with `elf::ctx`.
2022-10-01 15:27:39 -07:00
Fangrui Song f596d82385 [ELF] Move driver into ctx and remove indirection. NFC
This removes one global variable and removes GOT and unique_ptr indirection.
2022-10-01 15:12:50 -07:00
Fangrui Song 9c626d4a0d [ELF] Remove symtab indirection. NFC
Add LLVM_LIBRARY_VISIBILITY to remove unneeded GOT and unique_ptr indirection.
2022-10-01 14:46:49 -07:00
Fangrui Song 34fa860048 [ELF] Remove ctx indirection. NFC
Add LLVM_LIBRARY_VISIBILITY to remove unneeded GOT and unique_ptr
indirection. We can move other global variables into ctx without
indirection concern. In the long term we may consider passing Ctx
as a parameter to various functions and eliminate global state as
much as possible and then remove `Ctx::reset`.
2022-10-01 12:06:33 -07:00
Fangrui Song a623a4c8b4 [ELF] Remove elf::config indirection. NFC
`config` has 1000+ uses so we try to avoid changing `config->foo`. Define a
wrapper with LLVM_LIBRARY_VISIBILITY to remove unneeded GOT and unique_ptr
indirection.

My x86-64 lld executable is 11+KiB smaller.
2022-10-01 11:39:45 -07:00
Vincent Lee 58edaef3fe [lld-macho] Do not error out on dead stripped duplicate symbols
Builds that error out on duplicate symbols can still succeed if the symbols
will be dead stripped. Currently, this is the current behavior in ld64.
https://github.com/apple-oss-distributions/ld64/blob/main/src/ld/Resolver.cpp#L2018.
In order to provide an easier to path for adoption, introduce a new flag that will
retain compatibility with ld64's behavior (similar to `--deduplicate-literals`). This is
turned off by default since we do not encourage this behavior in the linker.

Reviewed By: #lld-macho, thakis, int3

Differential Revision: https://reviews.llvm.org/D134794
2022-09-30 15:09:27 -07:00
Alexander Kornienko d5e4a5a12f Cleanup: avoid referring to std::vector<T> members when T is incomplete.
This is not legal according to the C++ standard, and causes build errors in
particular in C++20 mode. Fix it by defining the vector's type before using the
vector.

Patch by poompatai@google.com.
2022-09-30 13:05:26 +02:00
Fangrui Song ab11ed5249 [ELF] Reset verdefIndex for Defined preempting SharedSymbol
to avoid spurious "attempt to reassign symbol '...'" warning after
7a58dd1046
2022-09-29 21:26:53 -07:00
Arthur Eubanks 1079f1c6ea [lld][COFF][LTO] Don't disable verifier in assert builds
We should catch more issues this way.

This previously the behavior and was dropped in cde5e5b600.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D134839
2022-09-29 08:58:38 -07:00
Fangrui Song e3ecc6a912 [ELF] Make symAux[0] a sentinel
And default auxIdx to 0.
2022-09-29 00:50:19 -07:00
Fangrui Song 9e6840ccba [ELF] Remove resolve => resolve{Defined,Common,Shared,Lazy,Undefined} indirection. NFC 2022-09-28 20:01:41 -07:00
Fangrui Song 7787427605 [ELF] Avoid redundant assignment to Symbol fields. NFC 2022-09-28 17:56:16 -07:00
Fangrui Song a98cbf01fa [ELF] Remove unused Symbol::getSymbolSize. NFC 2022-09-28 14:32:26 -07:00
Fangrui Song 7a58dd1046 [ELF] Refactor Symbol initialization and overwriting
Symbol::replace intends to overwrite a few fields (mostly Elf{32,64}_Sym
fields), but the implementation copies all fields then restores some old fields.
This is error-prone and wasteful. Add Symbol::overwrite to copy just the
needed fields and add other overwrite member functions to copy the extra
fields.
2022-09-28 13:11:31 -07:00
Fangrui Song df6803d94b [ELF] Symbols: remove isPlaceholder() test for Defined/CommonSymbol. NFC 2022-09-28 10:39:31 -07:00
Daniel Bertalan f546165754
[lld-macho] Don't create entries in isecPriorities during sorting (NFC)
If a value for a given key is not present, `DenseMap::operator[]`
default-constructs one, which is wasteful when we don't do anything with
it afterwards. Fix it by calling `lookup()` instead which only returns
the default value, but does not modify the map.

This speeds up linking a fair bit when only a small portion of all
sections are specified in the order file, like in the case of Chromium
Framework:

      N           Min           Max        Median           Avg        Stddev
  x  25      3.727684     3.8808699      3.753552     3.7702461     0.0397282
  +  25     3.6469049     3.7523289     3.6764321     3.6841622   0.025525047
  Difference at 95.0% confidence
  	-0.0860839 +/- 0.0189924
  	-2.28324% +/- 0.503745%
  	(Student's t, pooled s = 0.0333906)

Differential Revision: https://reviews.llvm.org/D134811
2022-09-28 16:50:18 +02:00
Fangrui Song 62e7c5b4e2 Revert "[ELF] --pack-dyn-relocs=android: scan relocation serially after D133003"
This reverts commit bce6416775.

The workaround is unneeded after 7dac9f4e48.
2022-09-28 07:06:49 +00:00
Fangrui Song 7dac9f4e48 [ELF] Make --pack-dyn-relocs=android compatible with parallel relocation scanning 2022-09-28 06:58:58 +00:00
Bernhard Urban-Forster 2769ceb0e7 [lld-macho] Fix -force_symbols_{,not_}weak_list arg type
This is still undocumented and unsupported, but if someone passed it before you
would end up with a missing file error since this takes an argument that
wouldn't be handled.

Differential Revision: https://reviews.llvm.org/D133854
2022-09-27 10:36:40 -04:00
Fangrui Song 78084d9e77 [ELF] Rewrite R_RISCV_ALIGN nops when r.addend%4 != 0
For RVC, GNU assembler and LLVM integrated assembler add c.nop followed by a
sequence of 4-byte nops. Even if remove % 4 == 0, we have to split one 4-byte
nop and therefore need to write the code sequence, otherwise we create an
incorrect c.unimp.
2022-09-26 14:20:27 -07:00
Pengxuan Zheng 961c45fd9f [LLD][COFF] Generate map file even if no /out flag is present
Currently LLD does not generate a map file if no /out flag (e.g., /out:a.exe) is
present. This is because LLD derives the map file's name from the default output
file name is no output file name is specified explicitly on the command
line. However, in this case, the default output file name has not been set by
LLD yet when LLD tries to set the name of the map file. This patch fixes this
corner case by moving the logic handling map file flags to a place after the
default output file name is set.

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D134559
2022-09-26 09:05:23 -07:00
Alvin Wong 37bd099daf [LLD][COFF] Fix absolute & synthetic symbols in COFF symbol table
Absolute symbol should contain its absolute value, but LLD had been
writing its RVA instead. Write its VA instead.

DefinedSynthetic were being skipped before with the reasoning "Relative
symbols are unrepresentable in a COFF symbol table", which is only true
if the RVA points to outside of a section. LLD does create synthetic
symbols which points to actual data chunks (typical for symbols embedded
into the load config directory). Write these symbols to the COFF symbol
table too.

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D134462
2022-09-26 11:05:41 +03:00
Daniel Bertalan d2f3d7bad2
[lld-macho] Force higher alignment for __thread_vars
`__thread_vars` contains pointers to `__tlv_bootstrap`, which are fixed
up by dyld; however the section's alignment is not specified. This means
that the relocations might end up on odd addresses, which is not
representable by the soon to be added chained fixups.

This is arguably a bug in MC, but this behavior has been there since TLV
support was originally added.

This patch forces the `__thread_vars` sections to be aligned to the
target's pointer size. This is done by ld64 as well.

Differential Revision: https://reviews.llvm.org/D134594
2022-09-25 08:02:07 +02:00
Fangrui Song 1a50213ce7 [ELF] --compress-debug-sections=zstd: ignore error if zstd was not built with ZSTD_MULTITHREAD 2022-09-22 13:16:50 -07:00
Alex Brachet 38b20a02fe [ELF] Fix std::min error on MacOs 2022-09-22 19:03:13 +00:00
Dmitri Gribenko eda9fdc493 Fix -Wunused-local-typedef warning in some build configurations 2022-09-22 17:10:17 +02:00
Martin Storsjö 525a400c7c [LLD] [ELF] Fix building with LLVM_LINK_LLVM_DYLIB since zstd was taken into use
This fixes a regression since fa74144c64dff6b145b0b3fa9397f913ddaa87bf;
even if we're linking to the dylib (which handles all the dependencies
in LLVMSupport), we're now also directly referencing zstd from
lld/ELF, and thus need to explicitly express our dependency on it.
2022-09-22 15:14:24 +03:00
Fangrui Song bce6416775 [ELF] --pack-dyn-relocs=android: scan relocation serially after D133003
https://reviews.llvm.org/D133003#3806508 can reproduce a non-determinism with
--threads=4. Making the config serial fixes non-determinism (by running the link
many times and compare output).
2022-09-21 11:43:13 -07:00
Fangrui Song fa74144c64 [ELF] Parallelize --compress-debug-sections=zstd
See D117853: compressing debug sections is a bottleneck and therefore it
has a large value parallizing the step.

zstd provides multi-threading API and the output is deterministic even with
different numbers of threads (see https://github.com/facebook/zstd/issues/2238).
Therefore we can leverage it instead of using the pigz-style sharding approach.

Also, switch to the default compression level 3. The current level 5
is significantly slower without providing justifying size benefit.

```
  'dash b.sh 1' ran
    1.05 ± 0.01 times faster than 'dash b.sh 3'
    1.18 ± 0.01 times faster than 'dash b.sh 4'
    1.29 ± 0.02 times faster than 'dash b.sh 5'

level=1 size: 358946945
level=3 size: 309002145
level=4 size: 307693204
level=5 size: 297828315
```

Reviewed By: andrewng, peter.smith

Differential Revision: https://reviews.llvm.org/D133679
2022-09-21 11:13:03 -07:00
Fangrui Song 06010fd1be [ELF] Make -V an alias for -v
In GNU ld,

* --version skips linker input processing.
* -v and -V keep processing if there is any input file. -V has more
  information we don't support.

We currently make -V an alias for --version which skips input processing.
On many `*-freebsd` and `powerpc-*` targets, `gcc -v` passes `-V` to ld
and expects to process input. Make -V an alias for -v to provide
compatibility.

Fix https://github.com/llvm/llvm-project/issues/57859
2022-09-20 17:12:44 -07:00
Fangrui Song bd6e018472 [ELF][test] Improve -v and --version tests 2022-09-20 17:05:36 -07:00
Pengxuan Zheng 7b31756393 [LLD][COFF] Support /MAPINFO flag
This patch adds support for link.exe's /MAPINFO flag to lld-link.exe.

Here is a description of the flag from Microsoft
(https://learn.microsoft.com/en-us/cpp/build/reference/mapinfo-include-information-in-mapfile?view=msvc-170):

 The /MAPINFO option tells the linker to include the specified information in a
 mapfile, which is created if you specify the /MAP option. EXPORTS tells the
 linker to include exported functions.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D134247
2022-09-20 14:39:07 -07:00
Fangrui Song 87792bdc38 [ELF] Name ctx->objectFiles[0] in the EF_RISCV_FLOAT_ABI mismatch error
Reviewed By: compnerd

Differential Revision: https://reviews.llvm.org/D134198
2022-09-20 11:14:04 -07:00
Vy Nguyen 016c2f5e32 [lld-macho] Support -dyld_env
This arg is undocumented but from looking at the code + experiment, it's used to add additional DYLD_ENVIRONMENT load commands to the output.

Differential Revision: https://reviews.llvm.org/D134058
2022-09-20 10:16:45 -04:00