summaryrefslogtreecommitdiff
path: root/lld/ELF/InputSection.cpp
AgeCommit message (Collapse)Author
2024-10-08[LLD][RISCV] Error on PCREL_LO referencing other Section (#107558)Sam Elliott
The RISC-V psABI states that "The `R_RISCV_PCREL_LO12_I` or `R_RISCV_PCREL_LO12_S` relocations contain a label pointing to an instruction in the same section with an `R_RISCV_PCREL_HI20` relocation entry that points to the target symbol." Without this patch, GNU ld errors, but LLD does not -- I think because LLD is doing the right thing, certainly in the testcase provided. Nonetheless, I think an error is good here to bring LLD in line with what GNU ld is doing in showing that the object the user provided is not following the psABI as written. Fixes #107304
2024-10-06[ELF] Pass Ctx & to some free functionsFangrui Song
2024-10-06[ELF] Pass Ctx & to SymbolsFangrui Song
2024-10-06[ELF] getRelocTargetVA: pass Ctx and Relocation. NFCFangrui Song
2024-10-06[ELF] Pass Ctx & to Arch/Fangrui Song
2024-10-05[ELF] Pass Ctx &Fangrui Song
2024-10-03[ELF] Pass Ctx & to SyntheticSection::getSizeFangrui 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 [IS]*.cppFangrui Song
2024-09-18[ELF] --icf: don't fold a section without relocation and a section with ↵Fangrui Song
relocations for SHT_CREL Similar to commit 686cff17cc310884e48ae963bf7507f96950cc90 for SHT_REL (#57693). CREL hasn't been tested with ICF before. And avoid a pitfall that eqClass[0] might interfere with ICF.
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-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 ppc64noTocRelax to Ctx. NFCFangrui Song
Ctx was introduced in March 2022 as a more suitable place for such singletons.
2024-08-12[ELF] Change "no PT_TLS" error to use errorOrWarnFangrui Song
so that --noinhibit-exec downgrades the error to a warning, which helps debugging when `PHDRS` is specified without `PT_TLS`. Also update the message to make it accurate: STT_TLS may exist in the absence of PT_TLS. In addition, invoking `exitLld(1)` (through `fatal`) is problematic (#66974): When a thread is `exitLld(1)`, triggering `llvm_shutdown`, another thread may be at `relocateAlloc`, accessing `sec.relocs()` which got destroyed(tampered?), leading to incorrect `llvm_unreachable("invalid expression")`.
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 ElfSym 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. This change fixes a pitfall: certain ElfSym members (e.g. globalOffsetTable, tlsModuleBase) were not zeroed and might be stale when lld::elf::link was invoked the second time.
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-08-01[ELF] Support relocatable files using CREL with explicit addendsFangrui Song
... using the temporary section type code 0x40000020 (`clang -c -Wa,--crel,--allow-experimental-crel`). LLVM will change the code and break compatibility (Clang and lld of different versions are not guaranteed to cooperate, unlike other features). CREL with implicit addends are not supported. --- Introduce `RelsOrRelas::crels` to iterate over SHT_CREL sections and update users to check `crels`. (The decoding performance is critical and error checking is difficult. Follow `skipLeb` and `R_*LEB128` handling, do not use `llvm::decodeULEB128`, whichs compiles to a lot of code.) A few users (e.g. .eh_frame, LLDDwarfObj, s390x) require random access. Pass `/*supportsCrel=*/false` to `relsOrRelas` to allocate a buffer and convert CREL to RELA (`relas` instead of `crels` will be used). Since allocating a buffer increases, the conversion is only performed when absolutely necessary. --- Non-alloc SHT_CREL sections may be created in -r and --emit-relocs links. SHT_CREL and SHT_RELA components need reencoding since r_offset/r_symidx/r_type/r_addend may change. (r_type may change because relocations referencing a symbol in a discarded section are converted to `R_*_NONE`). * SHT_CREL components: decode with `RelsOrRelas` and re-encode (`OutputSection::finalizeNonAllocCrel`) * SHT_RELA components: convert to CREL (`relToCrel`). An output section can only have one relocation section. * SHT_REL components: print an error for now. SHT_REL to SHT_CREL conversion for -r/--emit-relocs is complex and unsupported yet. Link: https://discourse.llvm.org/t/rfc-crel-a-compact-relocation-format-for-elf/77600 Pull Request: https://github.com/llvm/llvm-project/pull/98115
2024-07-27[ELF] Use invokeOnRelocs. NFCFangrui Song
2024-07-27[ELF] Add Relocs and invokeOnRelocs. NFCFangrui Song
Relocs is to simplify CREL support (#98115) while invokeOnRelocs simplifies some relsOrRelas call sites that will use the CREL iterator.
2024-07-11[ELF,RISCV] Fix TLSDESC=>IE when there is no TLS sectionFangrui Song
See the comment in handleTlsRelocation. For TLSDESC=>IE (the TLS symbol is defined in another DSO), R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} referencing a non-preemptible label uses the `R_RELAX_TLS_GD_TO_LE` code path. If there is no TLS section, `getTlsTpOffset` will be called with null `Out::tlsPhdr`, leading to a null pointer dereference. Since the return value is used by `RISCV::relocateAlloc` and ignored there, just return 0. LoongArch TLSDESC doesn't use STT_NOTYPE labels. The `if (..) return 0;` is a no-op for LoongArch. This patch is a follow-up to #79239 and fixes some comments. Pull Request: https://github.com/llvm/llvm-project/pull/98569
2024-06-25[ELF] Rename IsRela to HasAddendFangrui Song
`IsRela` is used by lld to differentiate REL and RELA static relocations. The proposed CREL patch will reuse `IsRela` for CREL (#91280). Rename `IsRela` to be more appropriate. Pull Request: https://github.com/llvm/llvm-project/pull/96592
2024-06-14[lld][ELF] Add basic TLSDESC support for LoongArchwanglei
LoongArch does not yet implement transition from TLSDESC to LE/IE, so TLSDESC dynamic relocation needs to be generated for each desc, which is ultimately handled by the dynamic linker. The test cases reference RISC-V: #79239 Reviewed By: MaskRay, SixWeining Pull Request: https://github.com/llvm/llvm-project/pull/94451
2024-06-07[ELF] Implement --force-group-allocationFangrui Song
GNU ld's relocatable linking behaviors: * Sections with the `SHF_GROUP` flag are handled like sections matched by the `--unique=pattern` option. They are processed like orphan sections and ignored by input section descriptions. * Section groups' (usually named `.group`) content is updated as the section indexes are updated. Section groups can be discarded with `/DISCARD/ : { *(.group) }`. `-r --force-group-allocation` discards section groups and allows sections with the `SHF_GROUP` flag to be matched like normal sections. If two section group members are placed into the same output section, their relocation sections (if present) are combined as well. This behavior can be useful when -r output is used as a pseudo shared object (e.g., FreeBSD's amd64 kernel modules, CHERIoT compartments). This patch implements --force-group-allocation: * Input SHT_GROUP sections are discarded. * Input sections do not get the SHF_GROUP flag, so `addInputSec` will combine relocation sections if their relocated section group members are combined. The default behavior is: * Input SHT_GROUP sections are retained. * Input SHF_GROUP sections can be matched (unlike GNU ld) * Input SHF_GROUP sections keep the SHF_GROUP flag, so `addInputSec` will create different OutputDesc copies. GNU ld provides the `FORCE_GROUP_ALLOCATION` command, which is not implemented. Pull Request: https://github.com/llvm/llvm-project/pull/94704
2024-05-17Revert "[LoongArch] Use R_LARCH_ALIGN with section symbol (#84741)"Fangrui Song
This reverts commit 01f79899ba349a0200586c8d05f5e22cca2ced31. This unusual special case has been discussed on the binutils mailing list. The approach will be revisited: https://sourceware.org/pipermail/binutils/2024-May/134092.html Pull Request: https://github.com/llvm/llvm-project/pull/92584
2024-05-13Reland: [LLD] Implement --enable-non-contiguous-regions (#90007)Daniel Thornburgh
When enabled, input sections that would otherwise overflow a memory region are instead spilled to the next matching output section. This feature parallels the one in GNU LD, but there are some differences from its documented behavior: - /DISCARD/ only matches previously-unmatched sections (i.e., the flag does not affect it). - If a section fails to fit at any of its matches, the link fails instead of discarding the section. - The flag --enable-non-contiguous-regions-warnings is not implemented, as it exists to warn about such occurrences. The implementation places stubs at possible spill locations, and replaces them with the original input section when effecting spills. Spilling decisions occur after address assignment. Sections are spilled in reverse order of assignment, with each spill naively decreasing the size of the affected memory regions. This continues until the memory regions are brought back under size. Spilling anything causes another pass of address assignment, and this continues to fixed point. Spilling after rather than during assignment allows the algorithm to consider the size effects of unspillable input sections that appear later in the assignment. Otherwise, such sections (e.g. thunks) may force an overflow, even if spilling something earlier could have avoided it. A few notable feature interactions occur: - Stubs affect alignment, ONLY_IF_RO, etc, broadly as if a copy of the input section were actually placed there. - SHF_MERGE synthetic sections use the spill list of their first contained input section (the one that gives the section its name). - ICF occurs oblivious to spill sections; spill lists for merged-away sections become inert and are removed after assignment. - SHF_LINK_ORDER and .ARM.exidx are ordered according to the final section ordering, after all spilling has completed. - INSERT BEFORE/AFTER and OVERWRITE_SECTIONS are explicitly disallowed.
2024-05-13Revert "[LLD] Implement --enable-non-contiguous-regions" (#92005)Daniel Thornburgh
Reverts llvm/llvm-project#90007 Broke in merging I think.
2024-05-13[LLD] Implement --enable-non-contiguous-regions (#90007)Daniel Thornburgh
When enabled, input sections that would otherwise overflow a memory region are instead spilled to the next matching output section. This feature parallels the one in GNU LD, but there are some differences from its documented behavior: - /DISCARD/ only matches previously-unmatched sections (i.e., the flag does not affect it). - If a section fails to fit at any of its matches, the link fails instead of discarding the section. - The flag --enable-non-contiguous-regions-warnings is not implemented, as it exists to warn about such occurrences. The implementation places stubs at possible spill locations, and replaces them with the original input section when effecting spills. Spilling decisions occur after address assignment. Sections are spilled in reverse order of assignment, with each spill naively decreasing the size of the affected memory regions. This continues until the memory regions are brought back under size. Spilling anything causes another pass of address assignment, and this continues to fixed point. Spilling after rather than during assignment allows the algorithm to consider the size effects of unspillable input sections that appear later in the assignment. Otherwise, such sections (e.g. thunks) may force an overflow, even if spilling something earlier could have avoided it. A few notable feature interactions occur: - Stubs affect alignment, ONLY_IF_RO, etc, broadly as if a copy of the input section were actually placed there. - SHF_MERGE synthetic sections use the spill list of their first contained input section (the one that gives the section its name). - ICF occurs oblivious to spill sections; spill lists for merged-away sections become inert and are removed after assignment. - SHF_LINK_ORDER and .ARM.exidx are ordered according to the final section ordering, after all spilling has completed. - INSERT BEFORE/AFTER and OVERWRITE_SECTIONS are explicitly disallowed.
2024-05-12Use StringRef::operator== instead of StringRef::equals (NFC) (#91864)Kazu Hirata
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator==/!= outnumber StringRef::equals by a factor of 276 under llvm-project/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
2024-04-16[LoongArch] Use R_LARCH_ALIGN with section symbol (#84741)Jinyang He
In LoongArch psABI v2.30, the R_LARCH_ALIGN requires symbol index to support the third parameter of alignment directive. Create symbol for each section is redundant because they have section symbol which can also be used as symbol index. So use section symbol directly for R_LARCH_ALIGN.
2024-04-10[ELF] relocateNonAlloc & ICF: replace random access iterators of relocations ↵Fangrui Song
with input iterators. NFC Also replace one `this->file` with a local variable as some function calls are opaque to the compiler, causing unneeded reload.
2024-04-04[lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (#72714)Daniil Kovalev
This patch adds lld support for: - Dynamic R_AARCH64_AUTH_* relocations (without including RELR compressed AUTH relocations) as described here: https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#auth-variant-dynamic-relocations - .note.AARCH64-PAUTH-ABI-tag section as defined here https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#elf-marking Depends on #72713 and #85231 --------- Co-authored-by: Peter Collingbourne <peter@pcc.me.uk> Co-authored-by: Fangrui Song <i@maskray.me>
2024-03-28[Object,ELFType] Rename TargetEndianness to Endianness (#86604)Fangrui Song
`TargetEndianness` is long and unwieldy. "Target" in the name is confusing. Rename it to "Endianness". I cannot find noticeable out-of-tree users of `TargetEndianness`, but keep `TargetEndianness` to make this patch safer. `TargetEndianness` will be removed by a subsequent change.
2024-03-20[ELF] Add isStaticRelSecType to simplify SHT_REL/SHT_RELA testing. NFCFangrui Song
and make it easier to introduce a new relocation format. https://discourse.llvm.org/t/rfc-relleb-a-compact-relocation-format-for-elf/77600 Pull Request: https://github.com/llvm/llvm-project/pull/85893
2024-03-19[ELF] Remove unneeded null check in getRelocatedSection. NFCFangrui Song
2024-03-18[ELF] Change getSymbolIndex to use const reference. NFCFangrui Song
2024-03-10[ELF] Move getSymbol/getRelocTargetSym from ObjFile<ELFT> to InputFile. NFCFangrui Song
This removes lots of unneeded `template getFile<ELFT>()`.
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-06[lld][ELF] Support relax R_LARCH_ALIGN (#78692)Jinyang He
Refer to commit 6611d58f5bbc ("Relax R_RISCV_ALIGN"), we can relax R_LARCH_ALIGN by same way. Reuse `SymbolAnchor`, `RISCVRelaxAux` and `initSymbolAnchors` to simplify codes. As `riscvFinalizeRelax` is an arch-specific function, put it override on `TargetInfo::finalizeRelax`, so that LoongArch can override it, too. The flow of relax R_LARCH_ALIGN is almost consistent with RISCV. The difference is that LoongArch only has 4-bytes NOP and all executable insn is 4-bytes aligned. So LoongArch not need rewrite NOP sequence. Alignment maxBytesEmit parameter is supported in psABI v2.30.
2024-01-24[ELF] Don't resolve relocations referencing SHN_ABS to tombstone in ↵Fangrui Song
non-SHF_ALLOC sections (#79238) A SHN_ABS symbol has never been considered for InputSection::relocateNonAlloc. Before #74686, the code did made it work in the absence of `-z dead-reloc-in-nonalloc=`. There is now a report about such SHN_ABS uses (https://github.com/llvm/llvm-project/pull/74686#issuecomment-1904101711) and I think it makes sense for non-SHF_ALLOC to support SHN_ABS, like SHF_ALLOC sections do. ``` // clang -g __attribute__((weak)) int symbol; int *foo() { return &symbol; } 0x00000023: DW_TAG_variable [2] (0x0000000c) ... DW_AT_location [DW_FORM_exprloc] (DW_OP_addrx 0x0) ``` .debug_addr references `symbol`, which can be redefined by a symbol assignment or --defsym to become a SHN_ABS symbol. The problem is that `!sym.getOutputSection()` cannot discern SHN_ABS from a symbol whose section has been discarded. Since commit 1981b1b6b92f7579a30c9ed32dbdf3bc749c1b40, a symbol relative to a discarded section is changed to `Undefined`, so the `SHN_ABS` check become trivial. We currently apply tombstone for a relocation referencing `SharedSymbol`. This patch does not change the behavior.
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.
2024-01-10[lld][LoongArch] Handle extreme code model relocs according to psABI v2.30 ↵Lu Weining
(#73387) psABI v2.30 requires the extreme code model instructions sequence (pcalau12i+addi.d+lu32i.d+lu52i.d) to be adjacent. See https://github.com/llvm/llvm-project/pull/71907 and https://github.com/loongson-community/discussions/issues/17 for details.
2024-01-08[ELF] Support R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128 in SHF_ALLOC sections ↵Fangrui Song
(#77261) Complement #72610 (non-SHF_ALLOC sections). GCC-generated .gcc_exception_table has the SHF_ALLOC flag and may contain R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128 relocations.
2023-12-21[ELF] Change .debug_names tombstone value to UINT32_MAX/UINT64_MAX (#74686)Fangrui Song
`clang -g -gpubnames -fdebug-types-section` now emits .debug_names section with references to local type unit entries defined in COMDAT .debug_info sections. ``` .section .debug_info,"G",@progbits,5657452045627120676,comdat .Ltu_begin0: ... .section .debug_names,"",@progbits ... // DWARF32 .long .Ltu_begin0 # Type unit 0 // DWARF64 // .long .Ltu_begin0 # Type unit 0 ``` When `.Ltu_begin0` is relative to a non-prevailing .debug_info section, the relocation resolves to 0, which is a valid offset within the .debug_info section. ``` cat > a.cc <<e struct A { int x; }; inline A foo() { return {1}; } int main() { foo(); } e cat > b.cc <<e struct A { int x; }; inline A foo() { return {1}; } void use() { foo(); } e clang++ -g -gpubnames -fdebug-types-section -fuse-ld=lld a.cc b.cc -o old ``` ``` % llvm-dwarfdump old ... Local Type Unit offsets [ LocalTU[0]: 0x00000000 ] ... Local Type Unit offsets [ LocalTU[0]: 0x00000000 // indistinguishable from a valid offset within .debug_info ] ``` https://dwarfstd.org/issues/231013.1.html proposes that we use a tombstone value instead to inform consumers. This patch implements the idea. The second LocalTU entry will now use 0xffffffff. https://reviews.llvm.org/D84825 has a TODO that we should switch the tombstone value for most `.debug_*` sections to UINT64_MAX. We have postponed the change for more than three years for consumers to migrate. At some point we shall make the change, so that .debug_names is no long different from other debug section that is not .debug_loc/.debug_ranges. Co-authored-by: Alexander Yermolovich <ayermolo@meta.com>
2023-12-07[ELF] relocateNonAlloc: clean up workaround codeFangrui Song
relocateNonAlloc is costly for .debug_* section relocating. We don't want to burn CPU cycles on other targets' workarounds. Remove a temporary workaround for Linux objtool after a proper fix https://git.kernel.org/linus/b8ec60e1186cdcfce41e7db4c827cb107e459002 Move the R_386_GOTPC workaround for GCC<8 beside the R_PC workaround.
2023-12-07[ELF] relocateNonAlloc: move likely expr == R_ABS before unlikely R_SIZE. NFCFangrui Song
2023-12-06[ELF] IWYU <optional> NFCFangrui Song
2023-11-21[ELF] Support R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128 in non-SHF_ALLOC ↵Fangrui Song
sections (#72610) For a label difference like `.uleb128 A-B`, MC generates a pair of R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128 if A-B cannot be folded as a constant. GNU assembler generates a pair of relocations in more cases (when A or B is in a code section with linker relaxation). `.uleb128 A-B` is primarily used by DWARF v5 .debug_loclists/.debug_rnglists (DW_LLE_offset_pair/DW_RLE_offset_pair entry kinds) implemented in Clang and GCC. `.uleb128 A-B` can be used in SHF_ALLOC sections as well (e.g. `.gcc_except_table`). This patch does not handle SHF_ALLOC. `-z dead-reloc-in-nonalloc=` can be used to change the relocated value, if the R_RISCV_SET_ULEB128 symbol is in a discarded section. We don't check the R_RISCV_SUB_ULEB128 symbol since for the expected cases A and B should be defined in the same input section.
2023-11-16[ELF] Make some InputSection/InputFile member functions const. NFCFangrui Song