aboutsummaryrefslogtreecommitdiff
path: root/lld/ELF/Arch/PPC.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-09-22ELF: Split relocateAlloc to relocateAlloc and relocateEh. NFCFangrui Song1-5/+3
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-05-25[lld] Remove unused includes (NFC) (#141421)Kazu Hirata1-2/+0
2024-12-03[ELF] Rename target-specific RelExpr enumeratorsFangrui Song1-1/+1
RelExpr enumerators are named `R_*`, which can be confused with ELF relocation type names. Rename the target-specific ones to `RE_*` to avoid confusion. For consistency, the target-independent ones can be renamed as well, but that's not urgent. The relocation processing mechanism with RelExpr has non-trivial overhead compared with mold's approach, and we might make more code into Arch/*.cpp files and decrease the enumerators. Pull Request: https://github.com/llvm/llvm-project/pull/118424
2024-11-16[ELF] Make RelType a struct typeFangrui Song1-1/+1
otherwise operator<<(const ELFSyncStream &s, RelType type) applies to non-reloc-type uint32_t, which can be confusing.
2024-11-16[ELF] Replace internalLinkerError(getErrorLoc(ctx, buf) + ...) with ↵Fangrui Song1-3/+1
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 Song1-1/+2
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 error(...) with ErrAlways or ErrFangrui Song1-4/+4
Most are migrated to ErrAlways mechanically. In the future we should change most to Err.
2024-10-19[ELF] Pass Ctx & to Symbol::getVAFangrui Song1-1/+1
2024-10-13[ELF] Pass Ctx & to check*Fangrui Song1-6/+6
2024-10-13[ELF] Pass Ctx & to (read|write)(16|64)Fangrui Song1-5/+5
2024-10-13[ELF] Pass Ctx & to read32/write32Fangrui Song1-45/+49
2024-10-07[ELF] Change Ctx::target to unique_ptr (#111260)Fangrui Song1-4/+1
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 Kirth1-1/+4
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 Song1-4/+1
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 Song1-2/+2
2024-10-06[ELF] Pass Ctx & to SymbolsFangrui Song1-4/+4
2024-10-06[ELF] getRelocTargetVA: pass Ctx and Relocation. NFCFangrui Song1-4/+2
2024-10-06[ELF] Pass Ctx & to Arch/Fangrui Song1-9/+9
2024-09-29[ELF] Pass Ctx & to ThunkFangrui Song1-2/+2
2024-09-28[ELF] Pass Ctx & to TargetInfo. NFCFangrui Song1-4/+4
2024-09-21[ELF] Replace config-> with ctx.arg. in Arch/Fangrui Song1-4/+4
2024-09-15[ELF] Move InStruct into Ctx. NFCFangrui Song1-5/+7
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 mainPart to Ctx. NFCFangrui Song1-1/+1
Ctx was introduced in March 2022 as a more suitable place for such singletons.
2023-09-15[ELF] Implement getImplicitAddend and enable checkDynamicRelocsDefault for PPC32Fangrui Song1-0/+7
2023-08-31[PowerPC][lld] Account for additional X-Forms -> D-Form/DS-Forms load/stores ↵Amy Kwan1-4/+8
when relaxing initial-exec to local-exec D153645 added additional X-Form load/stores that can be generated for TLS accesses. However, these added instructions have not been accounted for in lld. As a result, lld does not know how to handle them and cannot relax initial-exec to local-exec when the initial-exec sequence contains these additional load/stores. This patch aims to resolve https://github.com/llvm/llvm-project/issues/64424. Differential Revision: https://reviews.llvm.org/D158197
2022-11-21[ELF] Add InputSectionBase::{addRelocs,relocs} and GotSection::addConstant ↵Fangrui Song1-1/+1
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-10-17[ELF] Make relocateAlloc target specific. NFCFangrui Song1-8/+37
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-02-25[ELF] Support some absolute/PC-relative relocation types for REL formatFangrui Song1-0/+3
ctfconvert seems to use REL-format `.rel.SUNW_dof` for 32-bit architectures. ``` Binary file usr/ports/lang/perl5.32/work/perl-5.32.1/dtrace_mini.o matches [alfredo.junior@dell-a ~/tmp/llvm-bug]$ readelf -r dtrace_mini.o Relocation section (.rel.SUNW_dof): r_offset r_info r_type st_value st_name 00000184 0000281a R_PPC_REL32 00000000 $dtrace1772974259.Perl_dtrace_probe_load ``` Support R_PPC_REL32 to fix `ld.lld: error: drti.c:(.SUNW_dof+0x4E4): internal linker error: cannot read addend for relocation R_PPC_REL32`. While here, add some common relocation types for AArch64, PPC, and PPC64. We perform minimum tests. Reviewed By: adalava, arichardson Differential Revision: https://reviews.llvm.org/D120535
2022-02-04[ELF] Support R_PPC_NONE/R_PPC64_NONE in getImplicitAddendFangrui Song1-0/+12
Similar to f457863ae345d2635026501f5383e0e625869639
2022-01-09[ELF] Move gotIndex/pltIndex/globalDynIndex to SymbolAuxFangrui Song1-1/+1
to decrease sizeof(SymbolUnion) by 8 on ELF64 platforms. Symbols needing such information are typically 1% or fewer (5134 out of 560520 when linking clang, 19898 out of 5550705 when linking chrome). Storing them elsewhere can decrease memory usage and symbol initialization time. There is a ~0.8% saving on max RSS when linking a large program. Future direction: * Move some of dynsymIndex/verdefIndex/versionId to SymbolAux * Support mixed TLSDESC and TLS GD without increasing sizeof(SymbolUnion) Reviewed By: peter.smith Differential Revision: https://reviews.llvm.org/D116281
2021-12-22[ELF] Change some non-null pointer parameters to references. NFCFangrui Song1-1/+1
2021-12-20[ELF] #undef PPC to support GCC powerpc32 buildFangrui Song1-0/+3
GCC's powerpc32 port predefines `PPC` as a macro in GNU C++ mode in some configurations (Linux, FreeBSD, and some others. See `builtin_define_std ("PPC"); ` in gcc/config/rs6000). ``` % powerpc-linux-gnu-g++ -E -dM -xc++ /dev/null -o - | grep -w PPC #define PPC 1 ``` Fixes https://bugs.gentoo.org/829599 Reviewed By: thesamesam Differential Revision: https://reviews.llvm.org/D116017
2021-09-25[ELF] Replace noneRel = R_*_NONE with static constexpr. NFCFangrui Song1-1/+0
All architectures define R_*_NONE to 0.
2021-09-25[ELF] Default gotBaseSymInGotPlt to false (NFC for most architectures)Fangrui Song1-1/+0
Most architectures use .got instead of .got.plt, so switching the default can minimize customization. This fixes an issue for SPARC V9 which uses .got . AVR, AMDGPU, and MSP430 don't seem to use _GLOBAL_OFFSET_TABLE_.
2021-01-17[ELF] Support R_PPC_ADDR24 (ba foo; bla foo)Fangrui Song1-0/+2
2020-12-18[ELF] Rename R_TLS to R_TPREL and R_NEG_TLS to R_TPREL_NEG. NFCFangrui Song1-1/+1
The scope of R_TLS (TP offset relocation types (TPREL/TPOFF) used for the local-exec TLS model) is actually narrower than its name may imply. R_TLS_NEG is only used by Solaris R_386_TLS_LE_32. Rename them so that they will be less confusing. Reviewed By: grimar, psmith, rprichard Differential Revision: https://reviews.llvm.org/D93467
2020-11-25[ELF] Rename adjustRelaxExpr to adjustTlsExpr and delete the unused `data` ↵Fangrui Song1-4/+2
parameter. NFC Reviewed By: psmith Differential Revision: https://reviews.llvm.org/D91995
2020-05-15[ELF] Use namespace qualifiers (lld:: or elf::) instead of `namespace lld { ↵Fangrui Song1-8/+4
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-02-28[ELF][PPC32] Fix canonical PLTs when the order does not match the PLT orderFangrui Song1-6/+5
Reviewed By: Bdragon28 Differential Revision: https://reviews.llvm.org/D75394
2020-01-25[ELF][PPC32] Support range extension thunks with addendsFangrui Song1-5/+5
* Generalize the code added in D70637 and D70937. We should eventually remove the EM_MIPS special case. * Handle R_PPC_LOCAL24PC the same way as R_PPC_REL24. Reviewed By: Bdragon28 Differential Revision: https://reviews.llvm.org/D73424
2020-01-25[ELF][PPC32] Support canonical PLTFangrui Song1-8/+19
-fno-pie produces a pair of non-GOT-non-PLT relocations R_PPC_ADDR16_{HA,LO} (R_ABS) referencing external functions. ``` lis 3, func@ha la 3, func@l(3) ``` In a -no-pie/-pie link, if func is not defined in the executable, a canonical PLT entry (st_value>0, st_shndx=0) will be needed. References to func in shared objects will be resolved to this address. -fno-pie -pie should fail with "can't create dynamic relocation ... against ...", so we just need to think about -no-pie. On x86, the PLT entry passes the JMP_SLOT offset to the rtld PLT resolver. On x86-64: the PLT entry passes the JUMP_SLOT index to the rtld PLT resolver. On ARM/AArch64: the PLT entry passes &.got.plt[n]. The PLT header passes &.got.plt[fixed-index]. The rtld PLT resolver can compute the JUMP_SLOT index from the two addresses. For these targets, the canonical PLT entry can just reuse the regular PLT entry (in PltSection). On PPC32: PltSection (.glink) consists of `b PLTresolve` instructions and `PLTresolve`. The rtld PLT resolver depends on r11 having been set up to the .plt (GotPltSection) entry. On PPC64 ELFv2: PltSection (.glink) consists of `__glink_PLTresolve` and `bl __glink_PLTresolve`. The rtld PLT resolver depends on r12 having been set up to the .plt (GotPltSection) entry. We cannot reuse a `b PLTresolve`/`bl __glink_PLTresolve` in PltSection as a canonical PLT entry. PPC64 ELFv2 avoids the problem by using TOC for any external reference, even in non-pic code, so the canonical PLT entry scenario should not happen in the first place. For PPC32, we have to create a PLT call stub as the canonical PLT entry. The code sequence sets up r11. Reviewed By: Bdragon28 Differential Revision: https://reviews.llvm.org/D73399
2020-01-25[ELF] Rename relocateOne() to relocate() and pass `Relocation` to itFangrui Song1-11/+12
Symbol information can be used to improve out-of-range/misalignment diagnostics. It also helps R_ARM_CALL/R_ARM_THM_CALL which has different behaviors with different symbol types. There are many (67) relocateOne() call sites used in thunks, {Arm,AArch64}errata, PLT, etc. Rename them to `relocateNoSym()` to be clearer that there is no symbol information. Reviewed By: grimar, peter.smith Differential Revision: https://reviews.llvm.org/D73254
2020-01-24[ELF][PowerPC] Support R_PPC_COPY and R_PPC64_COPYFangrui Song1-0/+1
Reviewed By: Bdragon28, jhenderson, grimar, sfertile Differential Revision: https://reviews.llvm.org/D73255
2020-01-23[ELF] Pass `Relocation` to relaxGot and relaxTls{GdToIe,GdToLe,LdToLe,IeToLe}Fangrui Song1-13/+21
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
2019-12-29[ELF][PPC32] Implement IPLT code sequence for non-preemptible IFUNCFangrui Song1-1/+11
Similar to D71509 (EM_PPC64), on EM_PPC, the IPLT code sequence should be similar to a PLT call stub. Unlike EM_PPC64, EM_PPC -msecure-plt has small/large PIC model differences. * -fpic/-fpie: R_PPC_PLTREL24 r_addend=0. The call stub loads an address relative to `_GLOBAL_OFFSET_TABLE_`. * -fPIC/-fPIE: R_PPC_PLTREL24 r_addend=0x8000. (A partial linked object file may have an addend larger than 0x8000.) The call stub loads an address relative to .got2+0x8000. Just assume large PIC model for now. This patch makes: // clang -fuse-ld=lld -msecure-plt -fno-pie -no-pie a.c // clang -fuse-ld=lld -msecure-plt -fPIE -pie a.c #include <stdio.h> static void impl(void) { puts("meow"); } void thefunc(void) __attribute__((ifunc("resolver"))); void *resolver(void) { return &impl; } int main(void) { thefunc(); void (*theptr)(void) = &thefunc; theptr(); } work on Linux glibc. -fpie will crash because the compiler and the linker do not agree on the value which r30 stores (_GLOBAL_OFFSET_TABLE_ vs .got2+0x8000). Differential Revision: https://reviews.llvm.org/D71621
2019-12-18[ELF] writePlt, writeIplt: replace parameters gotPltEntryAddr and index with ↵Fangrui Song1-2/+2
`const Symbol &`. NFC PPC::writeIplt (IPLT code sequence, D71621) needs to access `Symbol`. Reviewed By: grimar, ruiu Differential Revision: https://reviews.llvm.org/D71631
2019-12-17[ELF] Add IpltSectionFangrui Song1-0/+1
PltSection is used by both PLT and IPLT. The PLT section may have a header while the IPLT section does not. Split off IpltSection from PltSection to be clearer. Unlike other targets, PPC64 cannot use the same code sequence for PLT and IPLT. This helps make a future PPC64 patch (D71509) more isolated. On EM_386 and EM_X86_64, when PLT is empty while IPLT is not, currently we are inconsistent whether the PLT header is conceptually attached to in.plt or in.iplt . Consistently attach the header to in.plt can make the -z retpolineplt logic simpler. It also makes `jmp` point to an aesthetically better place for non-retpolineplt cases. Reviewed By: grimar, ruiu Differential Revision: https://reviews.llvm.org/D71519
2019-12-16[ELF] Delete relOff from TargetInfo::writePLTFangrui Song1-1/+1
This change only affects EM_386. relOff can be computed from `index` easily, so it is unnecessarily passed as a parameter. Both in.plt and in.iplt entries are written by writePLT. For in.iplt, the instruction `push reloc_offset` will change because `index` is now different. Fortunately, this does not matter because `push; jmp` is only used by PLT. IPLT does not need the code sequence. Reviewed By: grimar, ruiu Differential Revision: https://reviews.llvm.org/D71518
2019-12-02[ELF][AArch64] Support R_AARCH64_{CALL26,JUMP26} range extension thunks with ↵Fangrui Song1-2/+3
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-07[ELF] Wrap things in `namespace lld { namespace elf {`, NFCFangrui Song1-4/+8
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