summaryrefslogtreecommitdiff
path: root/lld/ELF/Target.cpp
AgeCommit message (Collapse)Author
2025-10-13[ELF] Refactor RelocScan::scan to be target-specific (#163138)Fangrui Song
- Extract RelocScan to RelocScan.h. The file includes Target.h, and cannot be merged with Relocations.h - Add MIPS and PPC64 specific relocation scanners, removing runtime checks for other targets. This refactoring prepares the codebase for better target-specific optimizations and easier addition of target-specific behavior.
2025-09-22ELF: Split relocateAlloc to relocateAlloc and relocateEh. NFCFangrui Song
relocateAlloc can be called with either InputSection (including SyntheticSection like GotSection) or EhInputSection. Introduce relocateEh so that we can remove some boilerplate and replace relocateAlloc's parameter type with `InputSection`. Pull Request: https://github.com/llvm/llvm-project/pull/160031
2025-07-30[ELF] Add a dummySym member to CtxJessica Clarke
This ensures subsequent calls to elf::postScanRelocations with a new Ctx will correctly use an instance with the right internalFile (with the old one presumably deleted, even). It also avoids having to create a new instance in elf::getErrorPlace, and will allow more uses of such a dummy symbol in future commits. Reviewers: MaskRay Reviewed By: MaskRay Pull Request: https://github.com/llvm/llvm-project/pull/150796
2025-05-25[lld] Remove unused includes (NFC) (#141421)Kazu Hirata
2024-11-29[ELF] Change getSrcMsg to use ELFSyncStream. NFCFangrui Song
2024-11-24[ELF] Remove unneeded Twine in ELFSyncStreamFangrui Song
2024-11-16[ELF] Replace internalLinkerError(getErrorLoc(ctx, buf) + ...) with ↵Fangrui Song
InternalErr(ctx, buf) and simplify `+ toStr(ctx, x)` to `<< x`. The trailing '\n' << llvm::getBugReportMsg() is not very useful and therefore removed.
2024-11-16[ELF] Replace contex-less toString(x) with toStr(ctx, x)Fangrui Song
so that we can remove the global `ctx` from toString implementations. Rename lld::toString (to lld::elf::toStr) to simplify name lookup (we have many llvm::toString and another lld::toString(const llvm::opt::Arg &)).
2024-11-06[ELF] Replace fatal(...) with Fatal or ErrFangrui Song
2024-11-06[ELF] Add context-aware diagnostic functions (#112319)Fangrui Song
The current diagnostic functions log/warn/error/fatal lack a context argument and call the global `lld::errorHandler()`, which prevents multiple lld instances in one process. This patch introduces context-aware replacements: * log => Log(ctx) * warn => Warn(ctx) * errorOrWarn => Err(ctx) * error => ErrAlways(ctx) * fatal => Fatal(ctx) Example: `errorOrWarn(toString(f) + "xxx")` => `Err(ctx) << f << "xxx"`. (`toString(f)` is shortened to `f` as a bonus and may access `ctx` without accessing the global variable (see `Target.cpp`)). `ctx.e = &context->e;` can be replaced with a non-global Errorhandler when `ctx` becomes a local variable. (For the ELF port, the long term goal is to eliminate `error`. Most can be straightforwardly converted to `Err(ctx)`.)
2024-10-07[ELF] Change Ctx::target to unique_ptr (#111260)Fangrui Song
also rename `TargetInfo *getXXXTargetInfo` to `void setXXXTargetInfo` and change it to set `ctx.target`. This ensures that when `ctx` becomes a local variable, two lld invocations will not reuse the function-local static variable. --- Reland after commit c35214c131c0bc7f54dc18ceb75c75cba89f58ee ([ELF] Initialize TargetInfo members).
2024-10-07Revert "[ELF] Change Ctx::target to unique_ptr (#111260)" (#111449)Paul Kirth
This patch seems to be breaking the windows build bots. https://lab.llvm.org/buildbot/#/builders/63/builds/1953 We also see this in Fuchsia's Linux CI: https://fxbug.dev/372010530 This reverts commit 4ec06b17435e32ece5e1aa2bc8a6d26dbf0bb312.
2024-10-06[ELF] Change Ctx::target to unique_ptr (#111260)Fangrui Song
also rename `TargetInfo *getXXXTargetInfo` to `void setXXXTargetInfo` and change it to set `ctx.target`. This ensures that when `ctx` becomes a local variable, two lld invocations will not reuse the function-local static variable.
2024-10-06[ELF] Pass Ctx & to some free functionsFangrui Song
2024-10-06[ELF] getRelocTargetVA: pass Ctx and Relocation. NFCFangrui Song
2024-09-28[ELF] Pass Ctx & to TargetInfo. NFCFangrui Song
2024-09-28[ELF] Pass Ctx & to Target.cppFangrui Song
2024-09-21[ELF] Replace config-> with ctx.arg.Fangrui Song
2024-08-21[ELF] Move target to Ctx. NFCFangrui Song
Ctx was introduced in March 2022 as a more suitable place for such singletons. Follow-up to driver (2022-10) and script (2024-08).
2024-08-03[ELF] Move Out into Ctx. NFCFangrui Song
Ctx was introduced in March 2022 as a more suitable place for such singletons. ctx's hidden visibility optimizes generated instructions. bufferStart and tlsPhdr, which are not OutputSection, can now be moved outside of `Out`.
2024-05-03[lld] Error on unsupported split stack (#88063)Patrick O'Neill
Targets with no `-fstack-split` support now emit `ld.lld: error: target doesn't support split stacks` instead of `UNREACHABLE executed` with a backtrace asking the user to report a bug. Resolves #88061
2024-02-13[lld] Add target support for SystemZ (s390x) (#75643)Ulrich Weigand
This patch adds full support for linking SystemZ (ELF s390x) object files. Support should be generally complete: - All relocation types are supported. - Full shared library support (DYNAMIC, GOT, PLT, ifunc). - Relaxation of TLS and GOT relocations where appropriate. - Platform-specific test cases. In addition to new platform code and the obvious changes, there were a few additional changes to common code: - Add three new RelExpr members (R_GOTPLT_OFF, R_GOTPLT_PC, and R_PLT_GOTREL) needed to support certain s390x relocations. I chose not to use a platform-specific name since nothing in the definition of these relocs is actually platform-specific; it is well possible that other platforms will need the same. - A couple of tweaks to TLS relocation handling, as the particular semantics of the s390x versions differ slightly. See comments in the code. This was tested by building and testing >1500 Fedora packages, with only a handful of failures; as these also have issues when building with LLD on other architectures, they seem unrelated. Co-authored-by: Tulio Magno Quites Machado Filho <tuliom@redhat.com>
2024-02-12[ELF] Properly reject unsupported e_machineFangrui Song
Fix an incorrect llvm_unreachable.
2024-01-22[ELF] Add internal InputFile (#78944)Fangrui Song
Based on https://reviews.llvm.org/D45375 . Introduce a new InputFile kind `InternalKind`, use it for * `ctx.internalFile`: for linker-defined symbols and some synthesized `Undefined` * `createInternalFile`: for symbol assignments and --defsym I picked "internal" instead of "synthetic" to avoid confusion with SyntheticSection. Currently a symbol's file is one of: nullptr, ObjKind, SharedKind, BitcodeKind, BinaryKind. Now it's non-null (I plan to add an `assert(file)` to Symbol::Symbol and change `toString(const InputFile *)` separately). Debugging and error reporting gets improved. The immediate user-facing difference is more descriptive "File" column in the --cref output. This patch may unlock further simplification. Currently each symbol assignment gets its own `createInternalFile(cmd->location)`. Two symbol assignments in a linker script do not share the same file. Making the file the same would be nice, but would require non trivial code.
2023-10-03[ELF] Handle relocations in synthetic .eh_frame with a non-zero offset ↵simpal01
within the output section (#65966) When the .eh_frame section is placed at a non-zero offset within its output section, the relocation value within .eh_frame are computed incorrectly. We had similar issue in .ARM.exidx section and it has been fixed already in https://reviews.llvm.org/D148033. While applying the relocation using S+A-P, the value of P (the location of the relocation) is getting wrong. P is: P = SecAddr + rel.offset, But SecAddr points to the starting address of the outputsection rather than the starting address of the eh frame section within that output section. This issue affects all targets which generates .eh_frame section. Hence fixing in all the corresponding targets it affecting.
2023-07-25[lld][ELF] Support LoongArchWANG Xuerui
This adds support for the LoongArch ELF psABI v2.00 [1] relocation model to LLD. The deprecated stack-machine-based psABI v1 relocs are not supported. The code is tested by successfully bootstrapping a Gentoo/LoongArch stage3, complete with common GNU userland tools and both the LLVM and GNU toolchains (GNU toolchain is present only for building glibc, LLVM+Clang+LLD are used for the rest). Large programs like QEMU are tested to work as well. [1]: https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html Reviewed By: MaskRay, SixWeining Differential Revision: https://reviews.llvm.org/D138135
2022-11-21[ELF] Add InputSectionBase::{addRelocs,relocs} and GotSection::addConstant ↵Fangrui Song
to add/access relocations to prepare for changing `relocations` from a SmallVector to a pointer. Also change the `isec` parameter in `addAddendOnlyRelocIfNonPreemptible` to `GotSection &`.
2022-11-20[ELF] Change rawData to content() and data() to contentMaybeDecompress()Fangrui Song
Clarify data() which may trigger decompression and make it feasible to refactor the member variable rawData.
2022-10-17[ELF] Make relocateAlloc target specific. NFCFangrui Song
The target-specific code (AArch64, PPC64) does not fit into the generic code and adds virtual function overhead. Move relocateAlloc into ELF/Arch/ instead. This removes many virtual functions (relaxTls*). In addition, this helps get rid of getRelocTargetVA dispatch and many RelExpr members in the future.
2022-10-16[ELF] Move inputSections/ehInputSections into Ctx. NFCFangrui Song
2022-07-29[ELF] Combine EhInputSection removal and MergeInputSection removal. NFCFangrui Song
2022-01-29[ELF] De-template getErrorPlace. NFCFangrui Song
2022-01-18[ELF] De-template InputSectionBase::getLocation. NFCFangrui Song
2021-10-28[ELF] Change common diagnostics to report both object file location and ↵Fangrui Song
source file location Many diagnostics use `getErrorPlace` or `getErrorLocation` to report a location. In the presence of line table debug information, `getErrorPlace` uses a source file location and ignores the object file location. However, the object file location is sometimes more useful. This patch changes "undefined symbol" and "out of range" diagnostics to report both object/source file locations. Other diagnostics can use similar format if needed. The key idea is to let `InputSectionBase::getLocation` report the object file location and use `getSrcMsg` for source file/line information. `getSrcMsg` doesn't leverage `STT_FILE` information yet, but I think the temporary lack of the functionality is ok. For the ARM "branch and link relocation" diagnostic, I arbitrarily place the source file location at the end of the line. The diagnostic is not very common so its formatting doesn't need to be pretty. Differential Revision: https://reviews.llvm.org/D112518
2021-10-25[ELF] Update comments/diagnostics for some long options to use the canonical ↵Fangrui Song
two-dash form Rewrite some comments as appropriate.
2021-07-09[ELF] Check the Elf_Rel addends for dynamic relocationsAlex Richardson
There used to be many cases where addends for Elf_Rel were not emitted in the final object file (mostly when building for MIPS64 since the input .o files use RELA but the output uses REL). These cases have been fixed since, but this patch adds a check to ensure that the written values are correct. It is based on a previous patch that I added to the CHERI fork of LLD since we were using MIPS64 as a baseline. The work has now almost entirely shifted to RISC-V and Arm Morello (which use Elf_Rela), but I thought it would be useful to upstream our local changes anyway. This patch adds a (hidden) command line flag --check-dynamic-relocations that can be used to enable these checks. It is also on by default in assertions builds for targets that handle all dynamic relocations kinds that LLD can emit in Target::getImplicitAddend(). Currently this is enabled for ARM, MIPS, and I386. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D101450
2020-11-25[ELF] Rename adjustRelaxExpr to adjustTlsExpr and delete the unused `data` ↵Fangrui Song
parameter. NFC Reviewed By: psmith Differential Revision: https://reviews.llvm.org/D91995
2020-11-25[ELF] Add TargetInfo::adjustGotPcExpr for `R_GOT_PC` relaxations. NFCFangrui Song
With this change, `TargetInfo::adjustRelaxExpr` is only related to TLS relaxations and a subsequent clean-up can delete the `data` parameter. Differential Revision: https://reviews.llvm.org/D92079
2020-05-15[ELF] Use namespace qualifiers (lld:: or elf::) instead of `namespace lld { ↵Fangrui Song
namespace elf {` Similar to D74882. This reverts much code from commit bd8cfe65f5fee4ad573adc2172359c9552e8cdc0 (D68323) and fixes some problems before D68323. Sorry for the churn but D68323 was a mistake. Namespace qualifiers avoid bugs where the definition does not match the declaration from the header. See https://llvm.org/docs/CodingStandards.html#use-namespace-qualifiers-to-implement-previously-declared-functions (D74515) Differential Revision: https://reviews.llvm.org/D79982
2020-01-28Make llvm::StringRef to std::string conversions explicit.Benjamin Kramer
This is how it should've been and brings it more in line with std::string_view. There should be no functional change here. This is mostly mechanical from a custom clang-tidy check, with a lot of manual fixups. It uncovers a lot of minor inefficiencies. This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2020-01-23[ELF] Pass `Relocation` to relaxGot and relaxTls{GdToIe,GdToLe,LdToLe,IeToLe}Fangrui Song
These functions call relocateOne(). This patch is a prerequisite for making relocateOne() aware of `Symbol` (D73254). Reviewed By: grimar, nickdesaulniers Differential Revision: https://reviews.llvm.org/D73250
2020-01-15[ELF] Avoid false-positive assert in getErrPlace()Alex Richardson
This assertion was added as part of D70659 but did not account for .bss input sections. I noticed that this assert was incorrectly triggering while building FreeBSD for MIPS64. Fixed by relaxing the assert to also account for SHT_NOBITS input sections and adjust the test mips-jalr-non-function.s to link a file with a .bss section first. Reviewed By: MaskRay, grimar Differential Revision: https://reviews.llvm.org/D72567
2020-01-10[ELF] Make TargetInfo::writeIgotPlt a no-opFangrui Song
RELA targets don't read initial .got.plt entries. REL targets (ARM, x86-32) write the address of the IFUNC resolver to the entry (`write32le(buf, s.getVA())`). The default writeIgotPlt() is not meaningful. Make it a no-op. AArch64 and x86-64 will have 0 as initial .got.plt entries associated with IFUNC. Reviewed By: peter.smith Differential Revision: https://reviews.llvm.org/D72474
2020-01-09Re-apply "[ELF] Allow getErrPlace() to work before Out::bufferStart is set"Alex Richardson
This time with a fix for the UBSAN failure. Differential Revision: https://reviews.llvm.org/D70659
2019-12-13Revert "[ELF] Allow getErrPlace() to work before Out::bufferStart is set"Vlad Tsyrklevich
This reverts commit 2bbd32f5e8f0f62d895966e2623d9bdb9778b50b, it was causing UBSan failures like the following: lld/ELF/Target.cpp:103:41: runtime error: applying non-zero offset 24 to null pointer
2019-12-13[ELF] Allow getErrPlace() to work before Out::bufferStart is setAlex Richardson
Summary: So far it seems like the only test affected by this change is the one I recently added for R_MIPS_JALR relocations since the other test cases that use this function early (unknown-relocation-*) do not have a valid input section for the relocation offset. Reviewers: ruiu, grimar, MaskRay, espindola Reviewed By: ruiu, MaskRay Subscribers: emaste, sdardis, jrtc27, atanasyan, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D70659
2019-12-02[ELF][AArch64] Support R_AARCH64_{CALL26,JUMP26} range extension thunks with ↵Fangrui Song
addends Fixes AArch64 part of PR40438 The current range extension thunk framework does not handle a relocation relative to a STT_SECTION symbol with a non-zero addend, which may be used by jumps/calls to local functions on some RELA targets (AArch64, powerpc ELFv1, powerpc64 ELFv2, etc). See PR40438 and the following code for examples: // clang -target $target a.cc // .text.cold may be placed in a separate output section. // The distance between bar in .text.cold and foo in .text may be larger than 128MiB. static void foo() {} __attribute__((section(".text.cold"))) static int bar() { foo(); return 0; } __attribute__((used)) static int dummy = bar(); This patch makes such thunks with addends work for AArch64. The target independent part can be reused by PPC in the future. On REL targets (ARM, MIPS), jumps/calls are not represented as STT_SECTION + non-zero addend (see MCELFObjectTargetWriter::needsRelocateWithSymbol), so they don't need this feature, but we need to make sure this patch does not affect them. Reviewed By: peter.smith Differential Revision: https://reviews.llvm.org/D70637
2019-10-10Make nullptr check more robustRui Ueyama
The only condition that isecLoc becomes null is Out::bufferStart == nullptr, isec->getParent()->offset == 0, and isec->outSecOff == 0. We can check the first condition only once. llvm-svn: 374332
2019-10-10[lld] getErrPlace(): don't perform arithmetics on maybe-null pointerRoman Lebedev
isecLoc there can be null, but at the same time isec->getSize() may be non-null. It is UB to offset a nullptr.The most straight-forward fix here appears to perform casts+normal integral arithmetics. FAIL: lld :: ELF/invalid/invalid-relocation-aarch64.test (1158 of 2217) ******************** TEST 'lld :: ELF/invalid/invalid-relocation-aarch64.test' FAILED ******************** Script: -- : 'RUN: at line 2'; /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/yaml2obj /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/test/ELF/invalid/invalid-relocation-aarch64.test -o /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-aarch64.test.tmp.o : 'RUN: at line 3'; not /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/ld.lld /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-aarch64.test.tmp.o -o /dev/null 2>&1 | /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/FileCheck /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/test/ELF/invalid/invalid-relocation-aarch64.test -- Exit Code: 1 Command Output (stderr): -- /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/test/ELF/invalid/invalid-relocation-aarch64.test:4:10: error: CHECK: expected string not found in input # CHECK: error: unknown relocation (1024) against symbol foo ^ <stdin>:1:1: note: scanning from here /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/ELF/Target.cpp:100:41: runtime error: applying non-zero offset 24 to null pointer ^ <stdin>:1:118: note: possible intended match here /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/ELF/Target.cpp:100:41: runtime error: applying non-zero offset 24 to null pointer ^ -- ******************** Testing: 0.. 10.. 20.. 30.. 40.. 50. FAIL: lld :: ELF/invalid/invalid-relocation-x64.test (1270 of 2217) ******************** TEST 'lld :: ELF/invalid/invalid-relocation-x64.test' FAILED ******************** Script: -- : 'RUN: at line 2'; /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/yaml2obj /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/test/ELF/invalid/invalid-relocation-x64.test -o /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-x64.test.tmp1.o : 'RUN: at line 3'; echo ".global foo; foo:" > /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-x64.test.tmp2.s : 'RUN: at line 4'; /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/llvm-mc /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-x64.test.tmp2.s -o /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-x64.test.tmp2.o -filetype=obj -triple x86_64-pc-linux : 'RUN: at line 5'; not /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/ld.lld /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-x64.test.tmp1.o /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/tools/lld/test/ELF/invalid/Output/invalid-relocation-x64.test.tmp2.o -o /dev/null 2>&1 | /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/FileCheck /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/test/ELF/invalid/invalid-relocation-x64.test -- Exit Code: 1 Command Output (stderr): -- /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/test/ELF/invalid/invalid-relocation-x64.test:6:10: error: CHECK: expected string not found in input # CHECK: error: unknown relocation (152) against symbol foo ^ <stdin>:1:1: note: scanning from here /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/ELF/Target.cpp:100:41: runtime error: applying non-zero offset 24 to null pointer ^ <stdin>:1:118: note: possible intended match here /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/lld/ELF/Target.cpp:100:41: runtime error: applying non-zero offset 24 to null pointer ^ -- ******************** Testing: 0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. Testing Time: 20.73s ******************** Failing Tests (2): lld :: ELF/invalid/invalid-relocation-aarch64.test lld :: ELF/invalid/invalid-relocation-x64.test llvm-svn: 374329
2019-10-07[ELF] Wrap things in `namespace lld { namespace elf {`, NFCFangrui Song
This makes it clear `ELF/**/*.cpp` files define things in the `lld::elf` namespace and simplifies `elf::foo` to `foo`. Reviewed By: atanasyan, grimar, ruiu Differential Revision: https://reviews.llvm.org/D68323 llvm-svn: 373885