summaryrefslogtreecommitdiff
path: root/lld/ELF/LinkerScript.cpp
AgeCommit message (Collapse)Author
2025-09-20[ELF] -r/--emit-relocs: Fix crash when processing .rela.text before .text ↵mykouHW
(#156354) fixes #156417 When the relocation section is placed before the relocated section and the relocated section is not defined in the linker script, an error will occur during the linking process. **Issue Cause:** In a.ro, `.rela.text` precedes its relocated `InputSection` `.text`. `addOrphanSections` doesn't handle this scenario. When it processes `.rela.text`, in the called `getOutputSectionName`, `rel->getOutputSection()` is nullptr (input `.text` doesn't yet have a parent output section), leading to an assertion failure. **Solution:** For --emit-relocs and -r, ensure the output section for `.text.foo` is created before the output section for `.rela.text.foo`. --------- Co-authored-by: Fangrui Song <i@maskray.me>
2025-08-22[ELF][LoongArch] -r: Synthesize R_LARCH_ALIGN at input section start (#153935)Zhaoxin Yang
Similay to https://github.com/llvm/llvm-project/commit/94655dc8aec2f4e4b287e4c6ac829263f93e9740 The difference is that in LoongArch, the ALIGN is synthesized when the alignment is >4, (instead of >=4), and the number of bytes inserted is `sec->addralign - 4`.
2025-08-12[ELF] -r: Synthesize R_RISCV_ALIGN at input section start" (#151639)Fangrui Song
Clear `synthesizedAligns` to prevent stray relocations to an unrelated text section. Enhance the test to check llvm-readelf -r output. --- Without linker relaxation enabled for a particular relocatable file or section (e.g., using .option norelax), the assembler will not generate R_RISCV_ALIGN relocations for alignment directives. This becomes problematic in a two-stage linking process: ``` ld -r a.o b.o -o ab.o // b.o is norelax. Its alignment information is lost in ab.o. ld ab.o -o ab ``` When ab.o is linked into an executable, the preceding relaxed section (a.o's content) might shrink. Since there's no R_RISCV_ALIGN relocation in b.o for the linker to act upon, the `.word 0x3a393837` data in b.o may end up unaligned in the final executable. To address the issue, this patch inserts NOP bytes and synthesizes an R_RISCV_ALIGN relocation at the beginning of a text section when the alignment >= 4. For simplicity, when RVC is disabled, we synthesize an ALIGN relocation (addend: 2) for a 4-byte aligned section, allowing the linker to trim the excess 2 bytes. See also https://sourceware.org/bugzilla/show_bug.cgi?id=33236
2025-08-12Revert "[ELF] -r: Synthesize R_RISCV_ALIGN at input section start" (#151639)Fangrui Song
This reverts commit 6f53f1c8d2bdd13e30da7d1b85ed6a3ae4c4a856. synthesiedAligns is not cleared, leading to stray relocations for unrelated sections. Revert for now.
2025-08-08[ELF] -r: Synthesize R_RISCV_ALIGN at input section startFangrui Song
Without linker relaxation enabled for a particular relocatable file or section (e.g., using .option norelax), the assembler will not generate R_RISCV_ALIGN relocations for alignment directives. This becomes problematic in a two-stage linking process: ``` ld -r a.o b.o -o ab.o // b.o is norelax. Its alignment information is lost in ab.o. ld ab.o -o ab ``` When ab.o is linked into an executable, the preceding relaxed section (a.o's content) might shrink. Since there's no R_RISCV_ALIGN relocation in b.o for the linker to act upon, the `.word 0x3a393837` data in b.o may end up unaligned in the final executable. To address the issue, this patch inserts NOP bytes and synthesizes an R_RISCV_ALIGN relocation at the beginning of a text section when the alignment >= 4. For simplicity, when RVC is disabled, we synthesize an ALIGN relocation (addend: 2) for a 4-byte aligned section, allowing the linker to trim the excess 2 bytes. See also https://sourceware.org/bugzilla/show_bug.cgi?id=33236 Pull Request: https://github.com/llvm/llvm-project/pull/151639
2025-05-25[lld] Remove unused includes (NFC) (#141421)Kazu Hirata
2025-03-31[LLD][ELF] Allow memory region in OVERLAY (#133540)Daniel Thornburgh
This allows the contents of OVERLAYs to be attributed to memory regions. This is the only clean way to overlap VMAs in linker scripts that choose to primarily use memory regions to lay out addresses. This also simplifies OVERLAY expansion to better match GNU LD. Expressions for the first section's LMA and VMA are not generated if the user did not provide them. This allows the LMA/VMA offset to be preserved across multiple overlays in the same region, as with regular sections. Closes #129816
2025-03-22[lld] Use *Set::insert_range (NFC) (#132590)Kazu Hirata
DenseSet, SmallPtrSet, SmallSet, SetVector, and StringSet recently gained C++23-style insert_range. This patch replaces: Dest.insert(Src.begin(), Src.end()); with: Dest.insert_range(Src);
2025-03-11Reland "[LLD][ELF] Don't spill to same memory region" (#130851)Daniel Thornburgh
Relands #129795 Remove skipped potential spills from their parent input section descriptions to prevent undefined behavior.
2025-03-11Revert "[LLD][ELF] Don't spill to same memory region" (#130815)Daniel Thornburgh
Reverts llvm/llvm-project#129795 Breaks buildbots
2025-03-11[LLD][ELF] Don't spill to same memory region (#129795)Daniel Thornburgh
This prevents useless spills to the same memory region from causing spilling to take too many passes to converge. Handling this at spilling time allows us to relax the generation of spill sections; specifically, multiple spills can now be generated per output section. This should be fairly benign for performance, and it would eventually allow linker scripts to express things like holes or minimum addresses for parts of output sections. The linker could then spill within an output section whenever address constraints are violated.
2025-01-10[lld][ELF] Handle archive special casing in Input Sections (#119293)amosher-nvidia
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>
2024-12-16[ELF] Remove unneeded sec->file checkFangrui Song
2024-11-24[ELF] Remove unneeded Twine in ELFSyncStreamFangrui Song
2024-11-23[ELF] Make OutputDesc unique_ptrFangrui Song
Store them in LinkerScript::descPool. This removes a SpecificAlloc instantiation, makes lld smaller, and drops the small memory waste due to the separate BumpPtrAllocator.
2024-11-19[ELF] Avoid make<PhdrEntry>Fangrui Song
Store them in Partition::phdrs instead.
2024-11-17[ELF] Make elfHeader/programHeaders unique_ptrFangrui Song
This removes some SpecificAlloc instantiations, makes lld smaller, and drops the small memory waste due to the separate BumpPtrAllocator.
2024-11-16[ELF] Replace functions bAlloc/saver/uniqueSaver with member accessFangrui Song
2024-11-16[ELF] Pass ctx to bAlloc/saver/uniqueSaverFangrui Song
2024-11-14[ELF] Migrate away from global ctxFangrui Song
2024-11-07[ELF] Replace log with Log(ctx)Fangrui Song
2024-11-06[ELF] Replace errorOrWarn(...) with ErrFangrui Song
2024-11-06[ELF] Replace warn(...) with WarnFangrui Song
2024-11-06[ELF] Replace error(...) with ErrAlways or ErrFangrui Song
Most are migrated to ErrAlways mechanically. In the future we should change most to Err.
2024-10-20[ELF] Pass Ctx & to Defined & CommonSymbolFangrui Song
2024-10-15[ELF] Fix PROVIDE_HIDDEN -shared regression with bitcode file referencesFangrui Song
The inaccurate #111945 condition fixes a PROVIDE regression (#111478) but introduces another regression: in a DSO link, if a symbol referenced only by bitcode files is defined as PROVIDE_HIDDEN, lld would not set the visibility correctly, leading to an assertion failure in DynamicReloc::getSymIndex (https://reviews.llvm.org/D123985). This is because `(sym->isUsedInRegularObj || sym->exportDynamic)` is initially false (bitcode undef does not set `isUsedInRegularObj`) then true (in `addSymbol`, after LTO compilation). Fix this by making the condition accurate: use a map to track defined symbols. Reviewers: smithp35 Reviewed By: smithp35 Pull Request: https://github.com/llvm/llvm-project/pull/112386
2024-10-12[ELF] Pass Ctx &Fangrui Song
2024-10-11[ELF] Pass Ctx & to OutputSectionFangrui Song
2024-10-11[ELF] Pass Ctx &Fangrui Song
2024-10-11[ELF] Make shouldAddProvideSym return values consistent when demoted to ↵Fangrui Song
Undefined Case: `PROVIDE(f1 = bar);` when both `f1` and `bar` are in separate sections that would be discarded by GC. Due to `demoteDefined`, `shouldAddProvideSym(f1)` may initially return false (when Defined) and then return true (been demoted to Undefined). ``` addScriptReferencedSymbolsToSymTable shouldAddProvideSym(f1): false // the RHS (bar) is not added to `referencedSymbols` and may be GCed declareSymbols shouldAddProvideSym(f1): false markLive demoteSymbolsAndComputeIsPreemptible // demoted f1 to Undefined processSymbolAssignments addSymbol shouldAddProvideSym(f1): true ``` The inconsistency can cause `cmd->expression()` in `addSymbol` to be evaluated, leading to `symbol not found: bar` errors (since `bar` in the RHS is not in `referencedSymbols` and is GCed) (#111478). Fix this by adding a `sym->isUsedInRegularObj` condition, making `shouldAddProvideSym(f1)` values consistent. In addition, we need a `sym->exportDynamic` condition to keep provide-shared.s working. Fixes: ebb326a51fec37b5a47e5702e8ea157cd4f835cd Pull Request: https://github.com/llvm/llvm-project/pull/111945
2024-10-10[ELF] Revert Ctx & parameters from SyntheticSectionFangrui Song
Since Ctx &ctx is a member variable, 1f391a75af8685e6bba89421443d72ac6a186599 7a5b9ef54eb96abd8415fd893576c42e51fd95db e2f0ec3a3a8a2981be8a1aac2004cfb9064c61e8 can be reverted.
2024-10-10[ELF] Pass Ctx &Fangrui Song
2024-10-06[ELF] Pass Ctx & to some free functionsFangrui Song
2024-10-06[ELF] Pass Ctx & to InputFileFangrui Song
2024-10-06[ELF] Pass Ctx & to SyntheticSectionsFangrui Song
2024-10-03[ELF] Pass Ctx & to OutputSectionsFangrui Song
2024-09-29[ELF] Pass Ctx & to WriterFangrui Song
2024-09-23[ELF] Move elf::symtab into CtxFangrui Song
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-22[ELF] Replace remnant config-> with ctx.arg.Fangrui Song
2024-09-21[ELF] Replace config-> with ctx.arg. in LinkerScriptFangrui Song
2024-09-21[ELF] Pass Ctx to LinkerScript. NFCFangrui Song
2024-09-15[ELF] Move InStruct into Ctx. NFCFangrui Song
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[ELF] Move partitions into ctx. NFCFangrui Song
Ctx was introduced in March 2022 as a more suitable place for such singletons.
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-21[ELF] Move script into Ctx. NFCFangrui Song
Ctx was introduced in March 2022 as a more suitable place for such singletons. We now use default-initialization for `LinkerScript` and should pay attention to non-class types (e.g. `dot` is initialized by commit 503907dc505db1e439e7061113bf84dd105f2e35).
2024-08-21[ELF] Remove unneeded script->. NFCFangrui Song
2024-08-19[lld][ELF] Combine uniqued small data sections (#104485)Sam Elliott
RISC-V GCC with `-fdata-sections` will emit `.sbss.<name>`, `.srodata.<name>`, and `.sdata.<name>` sections for small data items of different kinds. Clang/LLVM already emits `.srodata.*` sections, and we intend to emit the other two section name patterns in #87040. This change ensures that any input sections starting `.sbss` are combined into one output section called `.sbss`, and the same respectively for `.srodata` and `.sdata`. This also allows the existing RISC-V specific code for determining an output order for `.sbss` and `.sdata` sections to apply to placing the sections.
2024-08-05[LLD] Add CLASS syntax to SECTIONS (#95323)Daniel Thornburgh
This allows the input section matching algorithm to be separated from output section descriptions. This allows a group of sections to be assigned to multiple output sections, providing an explicit version of --enable-non-contiguous-regions's spilling that doesn't require altering global linker script matching behavior with a flag. It also makes the linker script language more expressive even if spilling is not intended, since input section matching can be done in a different order than sections are placed in an output section. The implementation reuses the backend mechanism provided by --enable-non-contiguous-regions, so it has roughly similar semantics and limitations. In particular, sections cannot be spilled into or out of INSERT, OVERWRITE_SECTIONS, or /DISCARD/. The former two aren't intrinsic, so it may be possible to relax those restrictions later.
2024-08-03[ELF] Move outputSections into Ctx. NFCFangrui Song
Ctx was introduced in March 2022 as a more suitable place for such singletons.
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`.