17512 Commits

Author SHA1 Message Date
Jacek Caban
799e9883ea
[LLD][COFF] Silence GCC warning in Arm64XDynamicRelocEntry::getSize (NFC) (#122382)
Fixes 71bbafba31699bdabe289654d157ae961432e52a.
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 22b7b84860d39da71964c9b329937f2ee1d875ba
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 d496abbe2a037. 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
Jacek Caban
7168de5ca7 Revert "[LLD][COFF] Introduce hybrid symbol table for EC input files on ARM64X (#119294)"
This reverts commit a8206e7b37929f4754806667680ffba0206eef95 due to sanitizer failures.
2024-12-15 22:31:28 +01:00
Jacek Caban
a8206e7b37
[LLD][COFF] Introduce hybrid symbol table for EC input files on ARM64X (#119294)
On hybrid ARM64X targets, ARM64 and ARM64EC input files operate in
separate namespaces and cannot reference each other. This change
introduces separate `SymbolTable` instances and associates each
`InputFile` with the appropriate table to reflect this behavior.
2024-12-15 18:49:32 +01:00
Jacek Caban
d3c4857179
[LLD][COFF] Store machine type in SymbolTable (NFC) (#119298)
This change prepares for hybrid ARM64X support, which requires two
`SymbolTable` instances: one for native symbols and one for EC symbols.
In such cases, `config.machine` will remain ARM64X, while the
`SymbolTable` instances will store ARM64 and ARM64EC machine types.
2024-12-15 18:43:09 +01:00
Jacek Caban
0a9810d325
[LLD][COFF] Factor out LinkerDriver::setMachine (NFC) (#119297) 2024-12-15 18:41:26 +01:00
Jacek Caban
6b493baec1
[LLD][COFF] Store reference to SymbolTable instead of COFFLinkerContext in InputFile (NFC) (#119296)
This change prepares for the introduction of separate hybrid namespaces.
Hybrid images will require two `SymbolTable` instances, making it
necessary to associate `InputFile` objects with the relevant one.
2024-12-15 12:45:34 +01:00
Kazu Hirata
e04fde193b
[lld] Migrate away from PointerUnion::{is,get} (NFC) (#119993)
Note that PointerUnion::{is,get} have been soft deprecated in
PointerUnion.h:

  // FIXME: Replace the uses of is(), get() and dyn_cast() with
  //        isa<T>, cast<T> and the llvm::dyn_cast<T>

I'm not touching PointerUnion::dyn_cast for now because it's a bit
complicated; we could blindly migrate it to dyn_cast_if_present, but
we should probably use dyn_cast when the operand is known to be
non-null.
2024-12-14 20:07:08 -08:00