7706 Commits

Author SHA1 Message Date
Fangrui Song
085f7fb560 [ELF] Remove redundant isExported computation
Commit 2a26292388fcab0c857c91b2d08074c33abd37e8 made `isExported`
accurate except a few linker-synthesized symbols in finalizeSections.
We can collect these linker-synthesized symbols into a vector
and avoid recomputation for other symbols.

This is reland of 1a4d6de1b532149b10522eae5dabce39e5f7c687 after
`isExported` has been made accurate by f10441ad003236ef3b9e5415a571d2be0c0ce5ce
2025-01-27 22:39:41 -08:00
Fangrui Song
f10441ad00 [ELF] Refine includeInDynsym condition
`includeInDynsym` has a special case for isUndefWeak and
--no-dynamic-linker, which can be removed if we simplify disallow
dynamic symbols for static-pie.

The partition feature reports errors only when a symbol `isExported`.
We need to link in a DSO to trigger the mips error.
2025-01-27 22:02:27 -08:00
Jorge Gorbe Moya
8b1edc0f0c Revert "[ELF] Remove redundant isExported computation"
This reverts commit 1a4d6de1b532149b10522eae5dabce39e5f7c687.
It causes problems with `-static-pie`. See repro at
1a4d6de1b5 (commitcomment-151827775)
2025-01-27 15:27:23 -08:00
Fangrui Song
b9efbed468 Revert "Move HIP fatbin sections farther away from .text"
This reverts commit 048f35037779763963c4b4478a0884e828ea9538.
This reverts commit f7bbc40b0736cc417f57cd039b098b504cf6a71f.

Related to #95949. A developer with no prior lld contribution and very
little AMD contribution sneaked in these application-specific section
order rules we discourage.
2025-01-26 21:14:49 -08:00
Fangrui Song
1a4d6de1b5 [ELF] Remove redundant isExported computation
Commit 2a26292388fcab0c857c91b2d08074c33abd37e8 made `isExported`
accurate except a few linker-synthesized symbols in finalizeSections.
We can collect these linker-synthesized symbols into a vector
and avoid recomputation for other symbols.
2025-01-26 20:43:03 -08:00
Fangrui Song
2a26292388 [ELF] Make isExported accurate early
LTO compilation might define symbols not in the symbol table (e.g.
__emutls_v.x in test/ELF/lto/wrap-unreferenced-before-codegen.test).
These symbols have a false `isExported` until
`demoteSymbolsAndComputeIsPreemptible`. This is usually benign as we do
not reference `isExported` that early.

Ensure that `isExported` is correct early. This helps remove a redundant
`isExported` computation in `demoteSymbolsAndComputeIsPreemptible`.
2025-01-26 20:32:43 -08:00
Fangrui Song
84af3ee512 [ELF] Replace Fatal with Err 2025-01-26 16:13:52 -08:00
Fangrui Song
c1f10ef0a5 [ELF] SHF_LINK_ORDER: replace Fatal with ErrAlways
In LLD_IN_TEST=2 mode, when a thread calls Fatal, there will be no
output even if the process exits with code 1. Change the Fatal to
ErrAlways (not-recoverable) as subsequent code assumes SHF_LINK_ORDER
sh_link is correct.
2025-01-25 18:13:42 -08:00
Fangrui Song
18335f4800 [ELF] Ignore --time-trace if disableOutput
To avoid prevent generating two JSON for LLD_IN_TEST=2 ld.lld
--time-trace.
2025-01-25 18:06:22 -08:00
Fangrui Song
f21c35d54f [ELF] Replace some Fatal with Err
In LLD_IN_TEST=2 mode, when a thread calls Fatal, there will be no
output even if the process exits with code 1. Change a few Fatal to
recoverable Err.
2025-01-25 17:29:28 -08:00
Fangrui Song
a9e92beb25 [ELF] openAuxiliaryFile: open /dev/null if disableOutput and filename is "-"
So that LLD_IN_TEST=2 ld.lld --print-archive-stats=- a.o (and -Map -)
only writes the output once.
2025-01-25 16:54:19 -08:00
Fangrui Song
6b87f01aaa [ELF] MergeInputSection: replace Fatal with Err
In LLD_IN_TEST=2 mode, when a thread calls Fatal, there will be no
output even if the process exits with code 1. Change a few Fatal to
recoverable Err.
2025-01-25 16:20:27 -08:00
Fangrui Song
7db789b570 [ELF] Replace a few Fatal with Err
In LLD_IN_TEST=2 mode, when a thread calls Fatal, there will be no
output even if the process exits with code 1. Change a few Fatal to
recoverable Err.
2025-01-25 16:00:51 -08:00
Fangrui Song
c7579bfba5 [ELF] -o -: suppress output if disableOutput
So that LLD_IN_TEST=2 ld.lld -o - a.o only writes the output once.
2025-01-25 15:50:29 -08:00
Fangrui Song
4f48048171 [ELF] SHF_MERGE: avoid Fatal
In LLD_IN_TEST=2 mode, when a thread calls Fatal, there will be no
output even if the process exits with code 1.
2025-01-25 15:28:17 -08:00
Fangrui Song
19a6ac18ef [ELF] EhFrame: replace failOn with errOn
These diagnostics are mostly reported by a thread during writeSections.
In LLD_IN_TEST=2 mode, when a thread calls Fatal, there will be no
output even if the process exits with code 1.
2025-01-25 15:18:13 -08:00
Fangrui Song
c025b96ef9 [ELF] Symbol::extract : remove unneeded file->lazy check 2025-01-24 09:34:48 -08:00
Fangrui Song
e00f1f8436
[ELF] Error for executable .note.GNU-stack unless -z execstack or -r
.note.GNU-stack with the SHF_EXECINSTR flag requires an executable
stack. This is exceedingly rare. We report an error to force
the user to explicitly request an executable stack.

Close #121234

Pull Request: https://github.com/llvm/llvm-project/pull/124068
2025-01-23 12:32:54 -05:00
Daniil Kovalev
9178708c3b
[PAC][lld][AArch64][ELF] Support signed TLSDESC (#113817)
Depends on #120010

Support `R_AARCH64_AUTH_TLSDESC_ADR_PAGE21`, `R_AARCH64_AUTH_TLSDESC_LD64_LO12`
and `R_AARCH64_AUTH_TLSDESC_LD64_LO12` static relocations and
`R_AARCH64_AUTH_TLSDESC` dynamic relocation. IE/LE optimization is not
currently supported for AUTH TLSDESC.
2025-01-22 12:18:05 +03:00
Parth Arora
8c2030b7d4
[LLD] [ELF] Add support for linker script unary plus operator (#121508)
This commit adds support for linker script unary plus ('+') operator. It
is helpful for improving compatibility between LLD and GNU LD.

Closes #118047
2025-01-21 20:05:07 -08:00
Kazu Hirata
16aa400a27
[ELF] Avoid repeated hash lookups (NFC) (#122628) 2025-01-12 11:07:07 -08:00
amosher-nvidia
1807860504
[lld][ELF] Handle archive special casing in Input Sections (#119293)
According to the binutils spec:
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html

You should be able to specify all files in an archive using this syntax
`archivename:` , however, lld currently will only accept `archivename:*`
to match all files within an archive.

This patch will, only when necessary, create a copy of the file
specification and add an implicit wildcard `*` to the end. It also
updates the filename-spec linkerscript test to check for this behavior.

---------

Co-authored-by: Peter Smith <peter.smith@arm.com>
2025-01-10 19:30:38 -08:00
Fangrui Song
5aef8ab6ec [lld,NFC] Fix stale comments related to config-> 2025-01-06 23:06:33 -08:00
Fangrui Song
bcc1e58448 [ELF] Allow --symbol-ordering-file and call graph profile to be used together
Port https://reviews.llvm.org/D117354 from the MachO port.

If both --symbol-ordering-file and call graph profile are present, the
--symbol-ordering-file takes precedence, but the call graph profile is
still used for symbols that don't appear in the order file.

In addition, call graph profile described sections are now ordered
before other sections.
2025-01-05 17:13:23 -08:00
Fangrui Song
8d2b070f07 [ELF] Internalize computeCacheDirectedSortOrder. NFC
and delete an incorremtn comment about ctx.arg.callGraphProfile
2025-01-05 16:19:09 -08:00
Haohai Wen
2d9d291da0
[LLD] Do not combine cg_profile from obj and ordering file (#121325)
cg_profile in object is from CGProfilePass and it is often inaccurate.
While call-graph-ordering-file is provided by user. It is weird to
aggregate them together especially when call-graph-ordering-file is
accurate enough.
2025-01-05 10:38:14 +08:00
Mingming Liu
ee9be864bc
[NFC] Fix a typo (#121545)
`InputSectionBase::relsOrRelas` make at most one array-ref non-empty. One-off counter (as debugging log) shows the number of empty member containers is 2 or 3 in a real build.

Fix the typo.
2025-01-03 09:38:04 -08:00
Fangrui Song
510a5c7fc2 [ELF] Fix .gnu.version crash when .dynsym is discarded
Fix #88650

In addition, delete the unneeded comment.
https://sourceware.org/gnu-gabi/program-loading-and-dynamic-linking.txt
2025-01-02 18:46:44 -08:00
Brad Smith
1b9805c14d
[ELF] Move PT_OPENBSD_NOBTCFI check to readConfigs() (#120678) 2024-12-22 06:11:50 -05:00
Brad Smith
52574b5f40
[ELF] Add support for PT_OPENBSD_NOBTCFI (#120005) 2024-12-19 19:41:42 -05:00
Daniil Kovalev
1ef5b987a4
[PAC][lld][AArch64][ELF] Support signed GOT with tiny code model (#113816)
Depends on #114525

Support `R_AARCH64_AUTH_GOT_ADR_PREL_LO21` and `R_AARCH64_AUTH_GOT_LD_PREL19`
GOT-generating relocations. A corresponding `RE_AARCH64_AUTH_GOT_PC` member
of `RelExpr` is added, which is an AUTH-specific variant of `R_GOT_PC`.
2024-12-18 09:41:54 +03:00
Daniil Kovalev
417d2d7ce6
[PAC][lld][AArch64][ELF] Support signed GOT (#113815)
Depends on #113811

Support `R_AARCH64_AUTH_ADR_GOT_PAGE`, `R_AARCH64_AUTH_GOT_LO12_NC` and
`R_AARCH64_AUTH_GOT_ADD_LO12_NC` GOT-generating relocations. For preemptible
symbols, dynamic relocation `R_AARCH64_AUTH_GLOB_DAT` is emitted. Otherwise,
we unconditionally emit `R_AARCH64_AUTH_RELATIVE` dynamic relocation since
pointers in signed GOT needs to be signed during dynamic link time.
2024-12-17 10:23:01 +03:00
Fangrui Song
e83afbe793 [ELF] Remove unneeded sec->file check 2024-12-16 22:17:18 -08:00
Peter Collingbourne
64da33a589
ELF: Introduce --randomize-section-padding option.
The --randomize-section-padding option randomly inserts padding between
input sections using the given seed. It is intended to be used in A/B
experiments to determine the average effect of a change on program
performance, while controlling for effects such as false sharing in
the cache which may introduce measurement bias. For more details,
see the RFC:

https://discourse.llvm.org/t/rfc-lld-feature-for-controlling-for-code-size-dependent-measurement-bias/83334

Reviewers: smithp35, MaskRay

Reviewed By: MaskRay, smithp35

Pull Request: https://github.com/llvm/llvm-project/pull/117653
2024-12-13 11:52:09 -08:00
Chandler Carruth
dd647e3e60
Rework the Option library to reduce dynamic relocations (#119198)
Apologies for the large change, I looked for ways to break this up and
all of the ones I saw added real complexity. This change focuses on the
option's prefixed names and the array of prefixes. These are present in
every option and the dominant source of dynamic relocations for PIE or
PIC users of LLVM and Clang tooling. In some cases, 100s or 1000s of
them for the Clang driver which has a huge number of options.

This PR addresses this by building a string table and a prefixes table
that can be referenced with indices rather than pointers that require
dynamic relocations. This removes almost 7k dynmaic relocations from the
`clang` binary, roughly 8% of the remaining dynmaic relocations outside
of vtables. For busy-boxing use cases where many different option tables
are linked into the same binary, the savings add up a bit more.

The string table is a straightforward mechanism, but the prefixes
required some subtlety. They are encoded in a Pascal-string fashion with
a size followed by a sequence of offsets. This works relatively well for
the small realistic prefixes arrays in use.

Lots of code has to change in order to land this though: both all the
option library code has to be updated to use the string table and
prefixes table, and all the users of the options library have to be
updated to correctly instantiate the objects.

Some follow-up patches in the works to provide an abstraction for this
style of code, and to start using the same technique for some of the
other strings here now that the infrastructure is in place.
2024-12-11 15:44:44 -08:00
Fangrui Song
53544fc15f
[ELF] Respect ltoCanOmit for symbols in non-prevailing COMDAT
A linkonce_odr definition can be omitted in LTO compilation if
`canBeOmittedFromSymbolTable()` is true in all bitcode files.

Currently, we don't respect the `canBeOmittedFromSymbolTable()` bit from
symbols in a non-prevailing COMDAT, which could lead to incorrect
omission of a definition when merging a prevailing linkonce_odr and a
non-prevailing weak_odr, e.g. an implicit template instantiation and an
explicit template instantiation.

To fix #111341, allow the non-prevailing COMDAT code path to clear the
`ltoCanOmit` bit, so that `VisibleToRegularObj` could be false in
LTO.cpp. We could resolve either an Undefined or a Defined. For
simplicity, just use a Defined like the prevailing case (similar to how
we resolve symbols in ObjectFile COMDAT reviews.llvm.org/D120626).

Pull Request: https://github.com/llvm/llvm-project/pull/119332
2024-12-11 08:55:05 -08:00
Daniel Thornburgh
4dac0dff08
[LLD][ELF] Fix SHF_MERGE misalignment when spilled (#119289)
Section merging can increase section alignment after potential spill
sections are created. Since this operation is never performed on spill
sections, they can keep their earlier, smaller, alignment, which
produces a misalignment if a spill occurs.

This change propagates alignment increases forward after merging.
2024-12-10 13:43:13 -08:00
Peter Smith
457e14b926
[LLD][ARM] Arm v6-m should not use short Thunks. (#118111)
Thumb short thunks use the B.w instruction. This instruction is not
present on Arm v6-m so we should prevent these targets from using
short-thunks. We want to permit Arm v8-m.base targets to continue using
short thunks as it does have the B.w instruction despite not
implementing all of Thumb 2.

Add a check to see if the Movt and Movw instructions are present before
enabling short thunks for Thumb. The v6-m architecture has
J1J2BranchEncoding, but it does not have Movt and Movw, whereas
v8-m.base has both.

The memory map and limited flash size of an Arm v6-m CPU makes a short
thunk very unlikely in practice, but it is worth getting it right just
in case.
2024-12-09 11:24:45 +00:00
Fangrui Song
3733ed6f1c [ELF] Introduce Symbol::isExported to cache includeInDynsym
isExported, intended to replace exportDynamic, is primarily set in two
locations, (a) after parseSymbolVersion and (b) during demoteSymbols.

In the future, we should try removing exportDynamic. Currently,
merging exportDynamic/isExported would cause
riscv-gp.s to fail:

* The first isExported computation considers the undefined symbol exported
* Defined as a linker-synthesized symbol
* isExported remains true, while it should be false
2024-12-08 22:40:14 -08:00
Fangrui Song
712264b83c [ELF] Merge parseSymbolVersion and computeIspreemptible
ICF needs isPreemptible, which can be combined with parseSymbolVersion.
2024-12-08 17:50:17 -08:00
Fangrui Song
18538e2134 Revert "[ELF] Optimize parseSymbolVersion"
This reverts commit 2b129dacdde667137b5012d52f1d96e0ab26c749.

parseSymbolVersion can be combined with computeIsPreemptible,
making hasVersionSyms unneeded.
2024-12-08 17:50:16 -08:00
Fangrui Song
fe0d0a9e62 [ELF] Merge numSymbols and numELFSyms 2024-12-08 15:59:03 -08:00
Fangrui Song
2b129dacdd [ELF] Optimize parseSymbolVersion
We can just scan objectFiles and sharedFiles that have versioned symbols
to skip scanning the global symtab. While we won't suggest __wrap_foo
for undefined __wrap_foo@v1 when --wrap=foo@v1 is specified
(internalFile isn't scanned), this edge case difference is acceptable.
2024-12-08 12:32:55 -08:00
Fangrui Song
c650880958 [ELF] Simplify handling of exportDynamic and canBeOmittedFromSymbolTable
When computing whether a defined symbol is exported, we set
`exportDynamic` in Defined and CommonSymbol's ctor and merge the bit in
symbol resolution. The complexity is for the LTO special case
canBeOmittedFromSymbolTable, which can be simplified by introducing a
new bit.

We might simplify the state by caching includeInDynsym in exportDynamic
in the future.
2024-12-08 09:33:48 -08:00
Fangrui Song
aedc81b9d8 [ELF] Remove unneeded sym->inDynamicList check
Follow-up to d31fb264821ceac90b55c87c3f777053ab30a9af
2024-12-08 09:17:38 -08:00
Fangrui Song
d31fb26482 [ELF] Simplify includeInDynsym for --dynamic-list 2024-12-07 22:57:25 -08:00
Fangrui Song
8669028c18 [ELF] Remove unneeded sym->file check
After #78944 and some follow-ups, sym->file, unless in the initial
Placeholder stage, is guaranteed to be non-null.
2024-12-07 20:46:02 -08:00
Fangrui Song
ae5fdaea43 [ELF] Simplify printLocation
sym.file is always non-null (since around #78944).
2024-12-07 20:31:50 -08:00
Sam Elliott
65ced158e9
[RISCV] Remove R_RISCV_RVC_LUI Relocation (#118714)
This was removed from the ABI in riscv-non-isa/riscv-elf-psabi-doc#398.
It is not emitted by LLVM, and seems to have been an internal
implementation detail in binutils.

This is a follow-up to 26ec5da744b8 which removed previous binutils
internal relocations when they were removed from the ABI.

The LLD implementation was not tested when it was added in
https://reviews.llvm.org/D39322
2024-12-05 10:10:27 +00:00
Feng Zou
636beb6a28
[X86][LLD] Handle R_X86_64_CODE_6_GOTTPOFF relocation type (#117675)
For

    add %reg1, name@GOTTPOFF(%rip), %reg2
    add name@GOTTPOFF(%rip), %reg1, %reg2
    {nf} add %reg1, name@GOTTPOFF(%rip), %reg2
    {nf} add name@GOTTPOFF(%rip), %reg1, %reg2
    {nf} add name@GOTTPOFF(%rip), %reg

add

    R_X86_64_CODE_6_GOTTPOFF = 50

in #117277.

Linker can treat R_X86_64_CODE_6_GOTTPOFF as R_X86_64_GOTTPOFF or
convert the instructions above to

    add $name@tpoff, %reg1, %reg2
    add $name@tpoff, %reg1, %reg2
    {nf} add $name@tpoff, %reg1, %reg2
    {nf} add $name@tpoff, %reg1, %reg2
    {nf} add $name@tpoff, %reg

if the first byte of the instruction at the relocation offset - 6 is
0x62 (namely, encoded w/EVEX prefix) when possible.

Binutils patch: bminor/binutils-gdb@5bc71c2
Binutils mailthread:
https://sourceware.org/pipermail/binutils/2024-February/132351.html
ABI discussion:
https://groups.google.com/g/x86-64-abi/c/FhEZjCtDLFw/m/VHDjN4orAgAJ
Blog: https://kanrobert.github.io/rfc/All-about-APX-relocation
2024-12-05 16:26:26 +08:00