7508 Commits

Author SHA1 Message Date
Fangrui Song
17473182f5 [ELF] Pass Ctx & to MapFile 2024-09-29 15:39:52 -07:00
Fangrui Song
5c33424778 [ELF] Pass Ctx & to MarkLive 2024-09-29 15:32:16 -07:00
Fangrui Song
04e69ad727 [ELF] Pass Ctx & to Thunk 2024-09-29 15:20:01 -07:00
Fangrui Song
cf30e8e153 [ELF] Pass Ctx & to Thunk 2024-09-29 14:59:57 -07:00
Fangrui Song
bab5d5b6b0 [ELF] Pass Ctx & to ICF and SymbolTable 2024-09-29 14:45:00 -07:00
Shengchen Kan
31dd29cfb3
[X86,lld] Handle relocation R_X86_64_REX2_GOTPCRELX (#109783)
For

	mov        name@GOTPCREL(%rip), %reg
	test       %reg, name@GOTPCREL(%rip)
	binop      name@GOTPCREL(%rip), %reg

where binop is one of adc, add, and, cmp, or, sbb, sub, xor
instructions, we added

 R_X86_64_REX2_GOTPCRELX = 43

in #106681.

Linker can treat R_X86_64_REX2_GOTPCRELX as R_X86_64_GOTPCREL or convert
the above instructions to

	lea	name(%rip), %reg
	mov	$name, %reg
	test	$name, %reg
	binop	$name, %reg

if the first byte of the instruction at the relocation `offset - 4` is
`0xd5` (namely, encoded w/ REX2 prefix) when possible.

Binutils patch:
3d5a60de52
Binutils mailthread:
https://sourceware.org/pipermail/binutils/2023-December/131462.html
ABI discussion: https://groups.google.com/g/x86-64-abi/c/KbzaNHRB6QU
Blog: https://kanrobert.github.io/rfc/All-about-APX-relocation
2024-09-29 12:52:36 +08:00
Fangrui Song
c3e4998c0b [ELF] Pass Ctx & to TargetInfo. NFC 2024-09-28 21:48:26 -07:00
Fangrui Song
b0fc36dfa4 [ELF] Remove unneeded getTargetInfo. NFC 2024-09-28 20:29:37 -07:00
Fangrui Song
a52251675f [ELF] Pass Ctx & to Target.cpp 2024-09-28 19:23:56 -07:00
Fangrui Song
29783f70db [ELF] Pass Ctx & to Relocations 2024-09-28 19:17:18 -07:00
Fangrui Song
a94060ca0c [ELF] Pass Ctx & to Relocations 2024-09-28 00:26:23 -07:00
Fangrui Song
a7550e1521 [ELF] Pass Ctx & to Driver 2024-09-25 22:04:45 -07:00
Rahman Lavaee
7b7747dc1d
Reapply "Deprecate the -fbasic-block-sections=labels option." (#110039)
This reapplies commit 1911a50fae8a441b445eb835b98950710d28fc88 with a
minor fix in lld/ELF/LTO.cpp which sets Options.BBAddrMap when
`--lto-basic-block-sections=labels` is passed.
2024-09-25 22:03:10 -07:00
Fangrui Song
72307ba615 [ELF] Pass Ctx & to Driver 2024-09-25 10:22:13 -07:00
Fangrui Song
0206181ada [ELF] Pass Ctx & to Driver and Writer 2024-09-23 21:28:14 -07:00
Fangrui Song
df0864e761 [ELF] Move elf::symtab into Ctx
Remove the global variable `symtab` and add a member variable
(`std::unique_ptr<SymbolTable>`) to `Ctx` instead.

This is one step toward eliminating global states.

Pull Request: https://github.com/llvm/llvm-project/pull/109612
2024-09-23 10:33:43 -07:00
Kazu Hirata
9ed46fbe9f
[lld] Use StringRef idioms (NFC) (#109584) 2024-09-22 20:45:25 -07:00
Fangrui Song
e1a1f18022 [ELF] Make Config config part of Ctx ctx 2024-09-22 18:18:27 -07:00
Fangrui Song
b8248dacad [ELF] Replace remnant config-> with ctx.arg. 2024-09-22 18:03:33 -07:00
Fangrui Song
777329d32f [ELF] Replace config-> with ctx.arg. in Driver.cpp and Target.h 2024-09-22 17:48:48 -07:00
Fangrui Song
67f0277cc3 [ELF] Replace config-> with ctx.arg. in Writer 2024-09-21 23:14:09 -07:00
Fangrui Song
a7e14e2487 [ELF] Replace config-> with ctx.arg. 2024-09-21 22:54:37 -07:00
Fangrui Song
6f482010ae [ELF] Replace config-> with ctx.arg. 2024-09-21 22:46:13 -07:00
Fangrui Song
ff8d55f8d5 [ELF] Replace config-> with ctx.arg. in Relocations and Thunks 2024-09-21 19:56:07 -07:00
Fangrui Song
c64277d1b4 [ELF] Speculatively fix older MSVC after &ctx change
https://lab.llvm.org/buildbot/#/builders/107/builds/3155
```
error C2039: 'ctx': is not a member of '`anonymous-namespace'::Writer<ELFT>::fixSectionAlignments::<lambda_38c4af40b02773e966aeff2e6bd45bf2>'
```
2024-09-21 14:57:37 -07:00
Fangrui Song
eba30b3370 [ELF] Replace config-> with ctx.arg. in [IS]*.cpp 2024-09-21 12:47:47 -07:00
Fangrui Song
1dd9a565ea [ELF] Replace config-> with ctx.arg. in Arch/ 2024-09-21 12:03:18 -07:00
Fangrui Song
49ec508596 [ELF] Pass Ctx & to Writer. NFC 2024-09-21 12:00:21 -07:00
Fangrui Song
33204002f6 [ELF] ScriptParser: make Ctx & a member variable. NFC
Lambda captures need adjusting.
2024-09-21 11:51:02 -07:00
Fangrui Song
cf57a670bb [ELF] ScriptParser: pass Ctx to ScriptParser and ScriptLexer. NFC 2024-09-21 11:06:06 -07:00
Fangrui Song
9b7a22ebb5 [ELF] Replace config-> with ctx.arg. in LinkerScript 2024-09-21 10:28:23 -07:00
Fangrui Song
bb0a6f252f [ELF] Pass Ctx to LinkerScript. NFC 2024-09-21 10:22:11 -07:00
Fangrui Song
6b56a27349 [ELF] Replace config-> with ctx.arg. in LTO.cpp 2024-09-21 10:11:37 -07:00
Fangrui Song
b84d773fd0 [Parallel] Revert sequential task changes
https://reviews.llvm.org/D148728 introduced `bool Sequential` to unify
`execute` and the old `spawn` without argument. However, sequential
tasks might be executed by any worker thread (non-deterministic),
leading to non-determinism output for ld.lld -z nocombreloc (see
https://reviews.llvm.org/D133003).

In addition, the extra member variables have overhead.
This sequential task has only been used for lld parallel relocation
scanning.

This patch restores the behavior before https://reviews.llvm.org/D148728 .

Fix #105958

Pull Request: https://github.com/llvm/llvm-project/pull/109084
2024-09-20 21:15:42 -07:00
Daniil Fukalov
65bc259a97
[NFC] Add explicit #include llvm-config.h where its macros are used, last part. (#107615)
(this is the part related to bolt, lld and mlir)

Without these explicit includes, removing other headers, who implicitly
include llvm-config.h, may have non-trivial side effects. For example,
`clangd` may report even `llvm-config.h` as "no used" in case it defines
a macro, that is explicitly used with #ifdef. It is actually amplified
with different build configs which use different set of macros.
2024-09-20 19:59:39 +02:00
Fangrui Song
e82f0838ae [ELF] --icf: don't fold a section without relocation and a section with relocations for SHT_CREL
Similar to commit 686cff17cc310884e48ae963bf7507f96950cc90 for SHT_REL (#57693).
CREL hasn't been tested with ICF before.

And avoid a pitfall that eqClass[0] might interfere with ICF.
2024-09-18 23:06:12 -07:00
Fangrui Song
da0b9db26f [ELF] Replace config-> with ctx.arg. NFC 2024-09-17 18:30:10 -07:00
Fangrui Song
d9045420ae [ELF] Add Config &Ctx::arg. NFC
And migrate LinkerDriver member functions to use `ctx.arg.x` instead of
`config->x`.
2024-09-16 16:44:53 -07:00
Fangrui Song
bffb26f153 [ELF] Add LinkerDriver::ctx. NFC 2024-09-16 16:25:28 -07:00
Fangrui Song
cf70a1ee81 [ELF] .llvm.sympart: support CREL
When both CREL and the experimental lld partitions feature are enabled,
the relocation section may look like .crel.llvm_sympart.f1, and
`rels.relas` is empty. While here, support relocation sections with zero
entry.
2024-09-16 13:12:45 -07:00
Fangrui Song
e88b7ff016 [ELF] Move InStruct into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

llvm/Support/thread.h includes <thread>, which transitively includes
sstream in libc++ and uses ios_base::in, so we cannot use `#define in ctx.sec`.

`symtab, config, ctx` are now the only variables using
LLVM_LIBRARY_VISIBILITY.
2024-09-15 22:15:02 -07:00
Vitaly Buka
a248ec3178 Revert "[ELF] Move InStruct into Ctx. NFC"
The define breaks `std::in`.

https://lab.llvm.org/buildbot/#/builders/169/builds/3253

This reverts commit 2531b46264cd066d51f2571d134a63998d13710f.
2024-09-15 18:22:42 -07:00
Fangrui Song
2531b46264 [ELF] Move InStruct into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

`#define in ctx.sec` is used for now to avoid migrating `in.xxx`.
2024-09-15 16:59:28 -07:00
Fangrui Song
1cd07526b4 [ELF] Rename unique_saver to uniqueSaver. NFC
and remove an unneeded FIXME.
2024-09-15 16:20:58 -07:00
Fangrui Song
40e8e4ddcb [ELF] Move partitions into ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
2024-09-15 14:52:56 -07:00
JOE1994
4b27b5800f [lld] Nits on uses of raw_string_ostream (NFC)
* Don't call raw_string_ostream::flush(), which is essentially a no-op.
* Strip calls to raw_string_ostream::str(), to avoid excess layer of indirection.
2024-09-15 04:23:11 -04:00
Brian Cain
d1ba432533
[lld] select a default eflags for hexagon (#108431)
Empty archives are apparently routine in linux kernel builds, so instead
of asserting, we should handle this case with a sane default value.
2024-09-13 17:10:03 -05:00
Simon Tatham
daf208598b
[lld][AArch64] Fix getImplicitAddend in big-endian mode. (#107845)
In AArch64, the endianness of instruction encodings is always little,
whereas the endianness of data swaps between LE and BE modes. So
getImplicitAddend must use the right one of read32() and read32le(), for
data and code respectively. It was using read32() throughout, causing
instructions to be read as big-endian in BE mode, getting the wrong
addend.

Fixed, and updated the existing test to check both endiannesses. The
expected results for data must be byte-swapped, but the ones for code
need no adjustment.
2024-09-10 12:38:32 +01:00
Mingming Liu
09b231cb38
Re-apply "[NFCI][LTO][lld] Optimize away symbol copies within LTO global resolution in ELF" (#107792)
Fix the use-after-free bug and re-apply
https://github.com/llvm/llvm-project/pull/106193
* Without the fix, the string referenced by `objSym.Name` could be
destroyed even if string saver keeps a copy of the referenced string.
This caused use-after-free.
* The fix ([latest
commit](9776ed44cf))
updates `objSym.Name` to reference (via `StringRef`) the string saver's
copy.

Test:
1. For `lld/test/ELF/lto/asmundef.ll`, its test failure is reproducible
with `-DLLVM_USE_SANITIZER=Address` and gone with the fix.
3. Run all tests by following
https://github.com/google/sanitizers/wiki/SanitizerBotReproduceBuild#try-local-changes.
* Without the fix, `ELF/lto/asmundef.ll` aborted the multi-stage test at
`@@@BUILD_STEP stage2/asan_ubsan check@@@`, defined
[here](https://github.com/llvm/llvm-zorg/blob/main/zorg/buildbot/builders/sanitizers/buildbot_fast.sh#L30)
* With the fix, the [multi-stage
test](https://github.com/llvm/llvm-zorg/blob/main/zorg/buildbot/builders/sanitizers/buildbot_fast.sh)
pass stage2 {asan, ubsan, masan}. This is also the test used by
https://lab.llvm.org/buildbot/#/builders/169


**Original commit message**

`StringMap<T>` creates a [copy of the
string](d4c519e7b2/llvm/include/llvm/ADT/StringMapEntry.h (L55-L58))
for entry insertions and intentionally keep copies [since the
implementation optimizes string memory
usage](d4c519e7b2/llvm/include/llvm/ADT/StringMap.h (L124)).
On the other hand, linker keeps copies of symbol names [1] in
`lld:🧝:parseFiles` [2] before invoking `compileBitcodeFiles` [3].

This change proposes to optimize away string copies inside
[LTO::GlobalResolutions](24e791b416/llvm/include/llvm/LTO/LTO.h (L409)),
which will make LTO indexing more memory efficient for ELF. There are
similar opportunities for other (COFF, wasm, MachO) formats.

The optimization takes place for lld (ELF) only. For the rest of use
cases (gold plugin, `llvm-lto2`, etc), LTO owns a string saver to keep
copies and use global resolution key for de-duplication.

Together with @kazutakahirata's work to make `ComputeCrossModuleImport`
more memory efficient, we see a ~20% peak memory usage reduction in a
binary where peak memory usage needs to go down. Thanks to the
optimization in
329ba523cc,
the max (as opposed to the sum) of `ComputeCrossModuleImport` or
`GlobalResolution` shows up in peak memory usage.
* Regarding correctness, the set of
[resolved](80c47ad3ae/llvm/lib/LTO/LTO.cpp (L739))
[per-module
symbols](80c47ad3ae/llvm/include/llvm/LTO/LTO.h (L188-L191))
is a subset of
[llvm::lto::InputFile::Symbols](80c47ad3ae/llvm/include/llvm/LTO/LTO.h (L120)).
And bitcode symbol parsing saves symbol name when iterating
`obj->symbols` in `BitcodeFile::parse` already. This change updates
`BitcodeFile::parseLazy` to keep copies of per-module undefined symbols.
* Presumably the undefined symbols in a LTO unit (copied in this patch
in linker unique saver) is a small set compared with the set of symbols
in global-resolution (copied before this patch), making this a
worthwhile trade-off. Benchmarking this change alone shows measurable
memory savings across various benchmarks.

[1] ELF
1cea5c2138/lld/ELF/InputFiles.cpp (L1748)
[2]
ef7b18a53c/lld/ELF/Driver.cpp (L2863)
[3]
ef7b18a53c/lld/ELF/Driver.cpp (L2995)
2024-09-09 11:16:58 -07:00
Mingming Liu
1cc4c87198
Revert "[NFCI][LTO][lld] Optimize away symbol copies within LTO global resolution in ELF" (#107788)
Reverts llvm/llvm-project#106193 while investigating bot failures
https://lab.llvm.org/buildbot/#/builders/169/builds/2989/steps/9/logs/stdio
2024-09-08 16:45:59 -07:00