Commit Graph

6978 Commits

Author SHA1 Message Date
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
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
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
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
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
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 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
Mircea Trofin c625c17b88 [lld][thinlto] Include -mllvm options in the thinlto cache key
They may modify thinlto optimization.

This patch only extends support for `-mllvm`. There is another way to
pass llvm flags, `-plugin-opt`, but its processing is different and will
be provided in a subsequent patch.

Differential Revision: https://reviews.llvm.org/D134013
2022-09-19 12:04:17 -07:00
Fangrui Song 367997d0d6 [Support] Rename llvm::compression::{zlib,zstd}::uncompress to more appropriate decompress
This improves consistency with other places (e.g. llvm::compression::decompress,
llvm::object::Decompressor::decompress, llvm-objcopy).
Note: when zstd::uncompress was added, we noticed that the API `ZSTD_decompress`
is fine while the zlib API `uncompress` is a misnomer.
2022-09-17 12:35:17 -07:00
Nico Weber cd7ffa2e52 lld: Include name of output file in "failed to write output" diag
Differential Revision: https://reviews.llvm.org/D133110
2022-09-14 14:57:47 -04:00
Martin Storsjö e280940bfb [Support] Access threadIndex via a wrapper function
On Unix platforms, this wrapper function is inline, so it should
expand to the same direct access to the thread local variable. On
Windows, it's a non-inline function within Parallel.cpp, allowing
making the thread_local variable static.

Windows Native TLS doesn't support direct access to thread local
variables in a different DLL, and GCC/binutils on Windows occasionally
has problems with non-static thread local variables too.

This fixes mingw dylib builds with native TLS after
e6aebff674.

At the same time, move the whole thread local variable within
    #if LLVM_ENABLE_THREADS
to fix builds without threading support.

Differential Revision: https://reviews.llvm.org/D133759
2022-09-14 09:19:27 +03:00
Wei Wang 5529a7524a [ELF][Distributed ThinLTO] Do not generate empty index when bitcode object is linked
When the same bitcode object file is given multiple times from the Command-line
as lazy object file, empty index is generated which overwrites the one from thinlink.
This could cause undefined symbols during final link.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D133740
2022-09-13 14:34:41 -07:00
Fangrui Song 12607f57da [ELF] Cache compute_thread_count. NFC 2022-09-12 19:09:08 -07:00
Fangrui Song e6aebff674 [ELF] Parallelize relocation scanning
* Change `Symbol::flags` to a `std::atomic<uint16_t>`
* Add `llvm::parallel::threadIndex` as a thread-local non-negative integer
* Add `relocsVec` to part.relaDyn and part.relrDyn so that relative relocations can be added without a mutex
* Arbitrarily change -z nocombreloc to move relative relocations to the end. Disable parallelism for deterministic output.

MIPS and PPC64 use global states for relocation scanning. Keep serial scanning.

Speed-up with mimalloc and --threads=8 on an Intel Skylake machine:

* clang (Release): 1.27x as fast
* clang (Debug): 1.06x as fast
* chrome (default): 1.05x as fast
* scylladb (default): 1.04x as fast

Speed-up with glibc malloc and --threads=16 on a ThunderX2 (AArch64):

* clang (Release): 1.31x as fast
* scylladb (default): 1.06x as fast

Reviewed By: andrewng

Differential Revision: https://reviews.llvm.org/D133003
2022-09-12 12:56:35 -07:00
Fangrui Song bd16ffb389 [ELF] Merge Symbol::needs* into uint16_t flags. NFC
Split off from D133003 ([ELF] Parallelize relocation scanning) to make its diff smaller.
2022-09-09 14:37:18 -07:00
Fangrui Song 449f2ca146 [ELF] Add --compress-debug-sections=zstd
`clang -gz=zstd a.o` passes this option to the linker. This option compresses output
debug sections with zstd and sets ch_type to ELFCOMPRESS_ZSTD. As of today, very
few DWARF consumers recognize ELFCOMPRESS_ZSTD.

Use the llvm::zstd::compress API with level llvm::zstd::DefaultCompression (5),
which we may tune after we have more experience with zstd output.
zstd has built-in parallel compression support (so we don't need to do D117853
for zlib), which is not leveraged yet.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D133548
2022-09-09 10:30:18 -07:00
Fangrui Song 5e0464e38b [ELF] Support ELFCOMPRESS_ZSTD input
so that lld accepts relocatable object files produced by `clang -c -g -gz=zstd`.

We don't want to increase the size of InputSection, so do redundant but cheap
ch_type checks instead.

Differential Revision: https://reviews.llvm.org/D129406
2022-09-09 10:25:37 -07:00
Fangrui Song c682c26942 [ELF] Rename InputSectionBase::uncompress to decompress. NFC
The canonical verb is "decompress" (also used in llvm-objcopy). "uncompressed"
describes the state.
2022-09-09 10:18:46 -07:00
Joseph Huber c1d19a8489 [ELF] Provide the GNU hash function in libObject
GNU uses a different hashing function compared to the sys-V standard
function already provided in libObject. This is already used internally
in LLD for generating synthetic sections. This patch simply extracts
this definition and makes it availible to other users of `libObject`.
This is done in preparation for supporting symbol name lookups via the
GNU hash table.

Reviewed By: MaskRay, jhenderson

Differential Revision: https://reviews.llvm.org/D132696
2022-09-05 11:04:57 -05:00
Fangrui Song bc89663b69 [ELF] MarkLive: remove dead code from D24750. NFC 2022-09-05 00:01:09 -07:00
Fangrui Song 50b7eb91f0 [ELF] Reuse one RelocationScanner to scan all sections. NFC 2022-09-04 23:12:27 -07:00
Fangrui Song 94ca041905 [ELF] Move scanRelocations into Relocations.cpp. NFC 2022-09-04 21:31:18 -07:00
Fangrui Song c8d9d0000b [ELF] Relocations: set hasDirectReloc only if not ifunc. NFC 2022-09-04 21:30:19 -07:00
Fangrui Song 3ac9428024 [ELF] Change a DSO warning to errorOrWarn 2022-09-04 17:43:39 -07:00
Fangrui Song 82ed93ea05 [ELF] Use stOther to track visibility
This simplifies SymbolTableSection<ELFT>::writeTo. Add dsoProtected to be used
in canDefineSymbolInExecutable and get the side benefit that the protected DSO
preemption diagnostic is clearer.
2022-09-04 17:27:35 -07:00
Fangrui Song 86c35a54db [ELF] Simplify visibility computation. NFC 2022-09-04 16:38:27 -07:00
Fangrui Song 498151d944 [ELF] SharedFile::parse: make versym handling more precise
VER_NDX_LOCAL/VER_NDX_GLOBAL cannot be hidden, so we can compare them with
versyms[i] instead of versyms[i] & ~VERSYM_HIDDEN. In the presence of an error,
we can suppress addSymbol.
2022-09-04 16:21:19 -07:00
Fangrui Song 634a722dd9 [ELF] Clarify a workaround for mips GNU ld<2.31 2022-09-04 15:38:52 -07:00