Commit Graph

17318 Commits

Author SHA1 Message Date
Kazu Hirata
16aa400a27 [ELF] Avoid repeated hash lookups (NFC) (#122628) 2025-01-12 11:07:07 -08:00
Kazu Hirata
fd87188c2b [wasm] Avoid repeated hash lookups (NFC) (#122626) 2025-01-12 11:06:56 -08:00
Teresa Johnson
799955eb17 [ThinLTO] Skip opt pipeline and summary wrapper pass on empty modules (#120143)
Follow up to PR118508, to avoid unnecessary compile time for an empty
combind regular LTO module if all modules end up being ThinLTO only.

This required minor changes to a few tests to ensure they weren't empty.
2025-01-10 19:33:20 -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
Jacek Caban
3b0dafff87 [LLD][COFF] Use EC load config for ARM64X relocations of load config directory (#121337)
This change ensures the load config in the hybrid image view is handled
correctly. It introduces a new Arm64XRelocVal class to abstract
relocation values, allowing them to be relative to a symbol. This class
will also be useful for managing ARM64X relocation offsets in the
future.
2025-01-10 21:50:07 +01:00
Fangrui Song
cc88a5e615 [lld-macho,NFC] Switch to increasing priorities
--order_file, call graph profile, and BalancedPartitioning currently
build the section order vector by decreasing priority (from SIZE_MAX to
0). However, it's conventional to use an increasing key (see
OutputSection::inputOrder).

Switch to increasing priorities, remove the global variable
highestAvailablePriority, and remove the highestAvailablePriority
parameter from BPSectionOrderer. Change size_t to int.

This improves consistenty with the ELF and COFF ports. The ELF port
utilizes negative priorities for --symbol-ordering-file and call graph
profile, and non-negative priorities for --shuffle-sections (no Mach-O
counterpart yet).

Pull Request: https://github.com/llvm/llvm-project/pull/121727
2025-01-10 09:32:03 -08:00
Jacek Caban
799e9883ea [LLD][COFF] Silence GCC warning in Arm64XDynamicRelocEntry::getSize (NFC) (#122382)
Fixes 71bbafba31.
2025-01-10 14:38:22 +01:00
alx32
156e605163 [lld-macho] Fix branch extension thunk estimation logic (#120529)
This patch improves the linker’s ability to estimate stub reachability
in the `TextOutputSection::estimateStubsInRangeVA` function. It does so
by including thunks that have already been placed ahead of the current
call site address when calculating the threshold for direct stub calls.

Before this fix, the estimation process overlooked existing forward
thunks. This could result in some thunks not being inserted where
needed. In rare situations, particularly with large and specially
arranged codebases, this might lead to branch instructions being out of
range, causing linking errors.

Although this patch successfully addresses the problem, it is not
feasible to create a test for this issue. The specific layout and order
of thunk creation required to reproduce the corner case are too complex,
making test creation impractical.

Example error messages the issue could generate:
```
ld64.lld: error: banana.o:(symbol OUTLINED_FUNCTION_24949_3875): relocation BRANCH26 is out of range: 134547892 is not in [-134217728, 134217727]; references objc_autoreleaseReturnValue
ld64.lld: error: main.o:(symbol _main+0xc): relocation BRANCH26 is out of range: 134544132 is not in [-134217728, 134217727]; references objc_release
```
2025-01-09 14:14:13 -08:00
Jacek Caban
84087226fa [LLD][COFF] Emit base relocation for native CHPE metadata pointer on ARM64X (#121500) 2025-01-09 21:48:16 +01:00
Jacek Caban
c57810a00a [LLD][COFF] Sort base relocations (#121699)
This change ensures that base relocations are sorted in the output,
aligning with MSVC linker behavior. While input files typically provide
sorted relocations, this update guarantees correct sorting even if the
input relocations are unordered.
2025-01-09 15:40:49 +01:00
alx32
162814a7ec [lld-macho] Include branch extension thunks in linker map (#120496)
This patch extends the MachO linker's map file generation to include
branch extension thunk symbols. Previously, thunks were omitted from the
map file, making it difficult to understand the final layout of the
binary, especially when debugging issues related to long branch thunks.
This change ensures thunks are included and correctly interleaved with
other symbols based on their address, providing an accurate
representation of the linked output.
2025-01-07 21:07:51 -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
Fangrui Song
c2f7745b4e [lld-macho] Remove redundant hasValidData. NFC
lld::macho::runBalancedPartitioning ensures that all sections satisfy
`hasValidData`.
2025-01-05 15:59:17 -08:00
Anutosh Bhat
ba93eccded [lld][MachO] Fix warning while building for wasm (#120889)
While building clang & lld against emscripten for wasm, I see the
following
```
 │ │ /home/runner/work/recipes/recipes/output/bld/rattler-build_llvm_1734801187/work/lld/MachO/SyntheticSections.cpp:2075:25: warning: comparison of integers of
 │ │  different signs: 'long' and 'const uint32_t' (aka 'const unsigned int') [-Wsign-compare]
 │ │  2075 |   assert(buf - bufStart == sectionSize &&
 │ │       |          ~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~
 │ │ $BUILD_PREFIX/opt/emsdk/upstream/emscripten/cache/sysroot/include/assert.h:8:28: note: expanded from macro 'assert'
 │ │     8 | #define assert(x) ((void)((x) || (__assert_fail(#x, __FILE__, __LINE__, __func__),0)))
 │ │       |                            ^
 ```
 
 Casting `sectionSize` should be enough I think
2025-01-05 17:49:50 +08:00
Haohai Wen
3092ebcd1e [LLD] Add CHECK-NEXT for cgprofile-obj.s tests (#121677) 2025-01-05 11:04:05 +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
Haohai Wen
ddba0365b3 [LLD] Track cg_profile from combination of obj and ordering file (#121324)
Add tests to track section reordering when both cg_profile section
and call-graph-ordering-file were given.
2025-01-05 10:08:29 +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
Nico Weber
6cd171dc33 [lld/COFF] Support thin archives in /reproduce: files (#121512)
This already worked without /wholearchive; now it works with it too.
(Only for thin archives containing relative file names, matching the ELF
and Mach-O ports.)
2025-01-03 08:20:06 -05:00
YAMAMOTO Takashi
9df375e5ea [lld][WebAssembly] Fix non-pie dynamic-linking executable (#108146)
The commit 22b7b84860
made the symbols provided by shared libraries "defined",
and thus effectively made it impossible to generate non-pie
dynamically linked executables using
--unresolved-symbols=import-dynamic.

This commit, based on https://github.com/llvm/llvm-project/pull/109249,
fixes it by checking sym->isShared() explictly.
(as a bonus, you don't need to rely on
--unresolved-symbols=import-dynamic
anymore.)

Fixes https://github.com/llvm/llvm-project/issues/107387
2025-01-02 18:53:21 -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
Fangrui Song
3792b36234 [lld][WebAssembly] Replace config-> with ctx.arg.
Change the global variable reference to a member access of another
variable `ctx`. In the future, we may pass through `ctx` to functions to
eliminate global variables.

Pull Request: https://github.com/llvm/llvm-project/pull/119835
2025-01-02 17:08:18 -08:00
Nico Weber
4b22ef7d6b [lld/COFF] Fix comment typo to cycle bots 2025-01-02 08:24:50 -05:00
Jacek Caban
1fa0302ba2 [LLD][COFF] Emit warnings for missing load config on EC targets (#121339)
ARM64EC and ARM64X images require a load configuration to be valid.
2025-01-02 12:06:58 +01:00
Jacek Caban
8435225374 [LLD][COFF] Move addFile implementation to LinkerDriver (NFC) (#121342)
The addFile implementation does not rely on the SymbolTable object. With
#119294, the symbol table for input files is determined during the
construction of the objects representing them. To clarify that
relationship, this change moves the implementation from the SymbolTable
class to the LinkerDriver class.
2025-01-01 19:42:49 +01:00
Ellis Hoag
40e734e041 [lld][MachO] Allow separate --irpgo-profile flag (#121354) 2024-12-31 09:07:00 -08:00
Jacek Caban
db7123fbbc [LLD][COFF] Use EC symbol table for CHPE metadata (#120328)
Copy CHPE metadata pointer from EC load config to native configuration.
2024-12-29 14:04:00 +01:00
Jacek Caban
7144325109 [LLD][COFF] Prepare both load configs on ARM64X (#120326) 2024-12-29 12:55:10 +01:00
Jacek Caban
ff29f38c02 [LLD][COFF] Store and validate load config in SymbolTable (#120324)
Improve diagnostics for invalid load configurations.
2024-12-29 11:43:45 +01:00
Carlo Cabrera
a0f0a69b62 [lld][MachO] Fix symbol insertion in transplantSymbolsAtOffset (#120737)
The existing comparison does not insert symbols in the intended place.

Closes #120559.

---------

Co-authored-by: Bjorn Pettersson <bjorn.a.pettersson@ericsson.com>
2024-12-22 21:50:15 +08:00
Brad Smith
1b9805c14d [ELF] Move PT_OPENBSD_NOBTCFI check to readConfigs() (#120678) 2024-12-22 06:11:50 -05:00
Jacek Caban
21a6dbd400 [LLD][COFF] Report undefined EC symbols on ARM64X (#120311) 2024-12-20 19:56:02 +01:00
Brad Smith
52574b5f40 [ELF] Add support for PT_OPENBSD_NOBTCFI (#120005) 2024-12-19 19:41:42 -05:00
Nico Weber
c2dd612797 [llvm-lib] Add /llvmlibindex:no to disable writing an index (#120596)
This can be used with /llvmlibthin to create thin archives without an
index, which is a prerequisite for porting
https://reviews.llvm.org/D117284 to lld-link.

Creating files like this is already possible with `llvm-ar rcS`, so this
doesn't add additional problems.
2024-12-19 12:28:09 -05:00
Nico Weber
f8bcd93224 [lld/COFF] Fix -start-lib / -end-lib after reviews.llvm.org/D116434 (#120452)
That change forgot to set `lazy` to false before calling `addFile()` in
`forceLazy()` which caused `addFile()` to parse the file we want to
force a load for to be added as a lazy object again instead of adding
the file to `ctx.objFileInstances`.

This is caught by a pretty simple test (included).
2024-12-19 11:30:54 -05:00
Nico Weber
b05071de89 [lld/ELF] Add tests for start-lib / end-lib with eager loads (#120294)
Contains tests for the scenarios fixed in lld/COFF in #120292. They pass
without code changes, but I didn't see existing tests for this.
2024-12-19 11:23:09 -05:00
Nico Weber
2b6713d3b8 [lld/coff] Fix assert on /start-lib foo.obj /end-lib during eager loads (#120292)
If foo.obj is eagerly loaded (due to a prior undef referencing one if
its symbols) and has more than one symbol, we used to assert:
SymbolTable::addLazyObject() for the first symbol would set `lazy` to
false and load all symbols from the file, but the outer
ObjFile::parseLazy() loop would continue to run and call addLazyObject()
for the second symbol, which would assert.

Instead, just stop adding lazy symbols if the file got loaded for real
while adding a symbol.

(The ELF port has a similar early exit in `ObjFile<ELFT>::parseLazy()`.)
2024-12-19 11:22:29 -05:00
Max
79e859e049 [lld] Move BPSectionOrderer from MachO to Common for reuse in ELF (#117514)
Add lld/Common/BPSectionOrdererBase from MachO for reuse in ELF
2024-12-18 09:24:25 -08: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
Nico Weber
cde996c31d [lld/COFF] Remove needless indirection
`symtab.ctx.symtab` is just `symtab`. Looks like #119296 added
this using a global find-and-replace.

This was the only instance of `symtab.ctx.symtab` in lld/.

No behavior change.
2024-12-17 16:27:16 -05:00
Jacek Caban
16ef239520 [LLD][COFF] Introduce hybrid symbol table for EC input files on ARM64X (#119294) 2024-12-17 21:19:01 +01:00
Nico Weber
c9a5a6d18b [lld/COFF] Remove unused InputFile::LazyObjectKind
Its use was removed in d496abbe2a. No behavior change.
2024-12-17 14:29:31 -05:00
Nico Weber
4c2a46f5fe [lld/COFF] Make test/COFF/start-lib.ll use split-file
The two input files were only used by this one test, so put them inline.

No behavior change.
2024-12-17 13:55:50 -05:00
Jacek Caban
9c8214ff31 [LLD][COFF] Create COFFObjectFile instance when constructing ObjFile (NFC) (#120144)
This change moves the creation of COFFObjectFile to the construction of
ObjFile, instead of delaying it until parsing.
2024-12-17 19:26:13 +01:00
Nico Weber
34a44b2088 [lld/COFF] Handle -start-lib / -end-lib better in /reproduce: output (#119752)
Previously, we'd collect all input files in Driver::filePaths, and then
write filePaths after all other flags in
createResponseFile(). This meant that `-start-lib foo.obj -end-lib`
would be written as `-start-lib -end-lib foo.obj`, changing semantics.

Instead, remove Driver::filePaths, and handle things that fed into it
directly:

* OPT_INPUT is now handled in the same way as other flags, so that we
now get `-start-lib foo.obj -end-lib` in response.txt as desired. Add a
test for -start-lib / -end-lib and /reproduce:.

* OPT_wholearchive_file needs explicit handling now -- but before, this
was buggy as well: We'd put the flag without a rewritten path in
response.txt, but also the rewritten input file without wholearchive
semantics via filePaths. So this commit makes --whole-archive work with
/reproduce: too, and adds test coverage.

* /defaultlib:foo is now written as /defaultlib:foo into response.txt,
instead of writing the resolved path previously. While response.txt
looks slightly differently, both should have the same semantics, and
this should be mostly a no-op. (It does require updating a test.)

* /defaultlib: from .drectve sections are no longer recorded in
response.txt. This seems like a progression -- in the non-repro case
they come from .obj files, so they should come (only) from there in the
repro case too. This adds test coverage for this case.

Makes createResponseFile() look more like the versions in the ELF and
MachO ports too.
2024-12-17 11:30:13 -05:00
Ivan G.
e6ced4da44 Typo fix in large_sections.rst (#120101)
Remove duplicate word.
2024-12-17 14:01:03 +00: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