[lld] 104e82b - [ELF, PPC64] Inline toAddr16Rel and TLS remapping into the main relocate switch
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 14 22:49:41 PST 2026
Author: Fangrui Song
Date: 2026-02-14T22:49:35-08:00
New Revision: 104e82b75defd23ade610a5a75cd95244638ec0b
URL: https://github.com/llvm/llvm-project/commit/104e82b75defd23ade610a5a75cd95244638ec0b
DIFF: https://github.com/llvm/llvm-project/commit/104e82b75defd23ade610a5a75cd95244638ec0b.diff
LOG: [ELF,PPC64] Inline toAddr16Rel and TLS remapping into the main relocate switch
Delete the toAddr16Rel helper and the GOT_TLS-to-GOT16 remapping switch,
inlining their val adjustments as fallthrough cases in the main switch.
This reduces relocate() from 4 switches on type to 2 (TLS relaxation
pre-switch with early returns, and the unified write switch).
Added:
Modified:
lld/ELF/Arch/PPC64.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index cb237fb1385eb..59dea59d16e68 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -1059,78 +1059,6 @@ void PPC64::writeIplt(uint8_t *buf, const Symbol &sym,
sym.getGotPltVA(ctx) - getPPC64TocBase(ctx));
}
-static std::pair<RelType, uint64_t> toAddr16Rel(RelType type, uint64_t val) {
- // Relocations relative to the toc-base need to be adjusted by the Toc offset.
- uint64_t tocBiasedVal = val - ppc64TocOffset;
- // Relocations relative to dtv[dtpmod] need to be adjusted by the DTP offset.
- uint64_t dtpBiasedVal = val - dynamicThreadPointerOffset;
-
- switch (type) {
- // TOC biased relocation.
- case R_PPC64_GOT16:
- case R_PPC64_GOT_TLSGD16:
- case R_PPC64_GOT_TLSLD16:
- case R_PPC64_TOC16:
- return {R_PPC64_ADDR16, tocBiasedVal};
- case R_PPC64_GOT16_DS:
- case R_PPC64_TOC16_DS:
- case R_PPC64_GOT_TPREL16_DS:
- case R_PPC64_GOT_DTPREL16_DS:
- return {R_PPC64_ADDR16_DS, tocBiasedVal};
- case R_PPC64_GOT16_HA:
- case R_PPC64_GOT_TLSGD16_HA:
- case R_PPC64_GOT_TLSLD16_HA:
- case R_PPC64_GOT_TPREL16_HA:
- case R_PPC64_GOT_DTPREL16_HA:
- case R_PPC64_TOC16_HA:
- return {R_PPC64_ADDR16_HA, tocBiasedVal};
- case R_PPC64_GOT16_HI:
- case R_PPC64_GOT_TLSGD16_HI:
- case R_PPC64_GOT_TLSLD16_HI:
- case R_PPC64_GOT_TPREL16_HI:
- case R_PPC64_GOT_DTPREL16_HI:
- case R_PPC64_TOC16_HI:
- return {R_PPC64_ADDR16_HI, tocBiasedVal};
- case R_PPC64_GOT16_LO:
- case R_PPC64_GOT_TLSGD16_LO:
- case R_PPC64_GOT_TLSLD16_LO:
- case R_PPC64_TOC16_LO:
- return {R_PPC64_ADDR16_LO, tocBiasedVal};
- case R_PPC64_GOT16_LO_DS:
- case R_PPC64_TOC16_LO_DS:
- case R_PPC64_GOT_TPREL16_LO_DS:
- case R_PPC64_GOT_DTPREL16_LO_DS:
- return {R_PPC64_ADDR16_LO_DS, tocBiasedVal};
-
- // Dynamic Thread pointer biased relocation types.
- case R_PPC64_DTPREL16:
- return {R_PPC64_ADDR16, dtpBiasedVal};
- case R_PPC64_DTPREL16_DS:
- return {R_PPC64_ADDR16_DS, dtpBiasedVal};
- case R_PPC64_DTPREL16_HA:
- return {R_PPC64_ADDR16_HA, dtpBiasedVal};
- case R_PPC64_DTPREL16_HI:
- return {R_PPC64_ADDR16_HI, dtpBiasedVal};
- case R_PPC64_DTPREL16_HIGHER:
- return {R_PPC64_ADDR16_HIGHER, dtpBiasedVal};
- case R_PPC64_DTPREL16_HIGHERA:
- return {R_PPC64_ADDR16_HIGHERA, dtpBiasedVal};
- case R_PPC64_DTPREL16_HIGHEST:
- return {R_PPC64_ADDR16_HIGHEST, dtpBiasedVal};
- case R_PPC64_DTPREL16_HIGHESTA:
- return {R_PPC64_ADDR16_HIGHESTA, dtpBiasedVal};
- case R_PPC64_DTPREL16_LO:
- return {R_PPC64_ADDR16_LO, dtpBiasedVal};
- case R_PPC64_DTPREL16_LO_DS:
- return {R_PPC64_ADDR16_LO_DS, dtpBiasedVal};
- case R_PPC64_DTPREL64:
- return {R_PPC64_ADDR64, dtpBiasedVal};
-
- default:
- return {type, val};
- }
-}
-
static bool isTocOptType(RelType type) {
switch (type) {
case R_PPC64_GOT16_HA:
@@ -1433,9 +1361,49 @@ void PPC64::scanSection(InputSectionBase &sec) {
void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
RelType type = rel.type;
bool shouldTocOptimize = isTocOptType(type);
- // For dynamic thread pointer relative, toc-relative, and got-indirect
- // relocations, proceed in terms of the corresponding ADDR16 relocation type.
- std::tie(type, val) = toAddr16Rel(type, val);
+
+ // Handle TLS optimization.
+ switch (type) {
+ case R_PPC64_GOT_TLSGD16:
+ case R_PPC64_GOT_TLSGD16_HA:
+ case R_PPC64_GOT_TLSGD16_HI:
+ case R_PPC64_GOT_TLSGD16_LO:
+ case R_PPC64_GOT_TLSGD_PCREL34:
+ case R_PPC64_TLSGD:
+ if (rel.expr == R_TPREL) {
+ relaxTlsGdToLe(loc, rel, val);
+ return;
+ }
+ if (oneof<R_GOT_OFF, R_GOT_PC>(rel.expr)) {
+ relaxTlsGdToIe(loc, rel, val);
+ return;
+ }
+ break;
+ case R_PPC64_GOT_TLSLD16:
+ case R_PPC64_GOT_TLSLD16_HA:
+ case R_PPC64_GOT_TLSLD16_HI:
+ case R_PPC64_GOT_TLSLD16_LO:
+ case R_PPC64_GOT_TLSLD_PCREL34:
+ case R_PPC64_TLSLD:
+ if (rel.expr == R_TPREL) {
+ relaxTlsLdToLe(loc, rel, val);
+ return;
+ }
+ break;
+ case R_PPC64_GOT_TPREL16_HA:
+ case R_PPC64_GOT_TPREL16_LO_DS:
+ case R_PPC64_GOT_TPREL16_DS:
+ case R_PPC64_GOT_TPREL16_HI:
+ case R_PPC64_GOT_TPREL_PCREL34:
+ case R_PPC64_TLS:
+ if (rel.expr == R_TPREL) {
+ relaxTlsIeToLe(loc, rel, val);
+ return;
+ }
+ break;
+ default:
+ break;
+ }
switch (type) {
case R_PPC64_ADDR14: {
@@ -1445,6 +1413,13 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
write16(ctx, loc + 2, (aalk & 3) | (val & 0xfffc));
break;
}
+ case R_PPC64_GOT16:
+ case R_PPC64_GOT_TLSGD16:
+ case R_PPC64_GOT_TLSLD16:
+ case R_PPC64_TOC16:
+ case R_PPC64_DTPREL16: // semantically subtracts DTP offset (== tocOffset)
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16:
checkIntUInt(ctx, loc, val, 16, rel);
write16(ctx, loc, val);
@@ -1453,6 +1428,13 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
checkIntUInt(ctx, loc, val, 32, rel);
write32(ctx, loc, val);
break;
+ case R_PPC64_GOT16_DS:
+ case R_PPC64_TOC16_DS:
+ case R_PPC64_GOT_DTPREL16_DS:
+ case R_PPC64_GOT_TPREL16_DS:
+ case R_PPC64_DTPREL16_DS:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_DS:
case R_PPC64_TPREL16_DS: {
checkInt(ctx, loc, val, 16, rel);
@@ -1462,6 +1444,15 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
checkAlignment(ctx, loc, lo(val), mask + 1, rel);
write16(ctx, loc, (read16(ctx, loc) & mask) | lo(val));
} break;
+ case R_PPC64_GOT16_HA:
+ case R_PPC64_GOT_TLSGD16_HA:
+ case R_PPC64_GOT_TLSLD16_HA:
+ case R_PPC64_GOT_TPREL16_HA:
+ case R_PPC64_GOT_DTPREL16_HA:
+ case R_PPC64_TOC16_HA:
+ case R_PPC64_DTPREL16_HA:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_HA:
case R_PPC64_REL16_HA:
case R_PPC64_TPREL16_HA:
@@ -1472,6 +1463,15 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
write16(ctx, loc, ha(val));
}
break;
+ case R_PPC64_GOT16_HI:
+ case R_PPC64_GOT_TLSGD16_HI:
+ case R_PPC64_GOT_TLSLD16_HI:
+ case R_PPC64_GOT_TPREL16_HI:
+ case R_PPC64_GOT_DTPREL16_HI:
+ case R_PPC64_TOC16_HI:
+ case R_PPC64_DTPREL16_HI:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_HI:
case R_PPC64_REL16_HI:
case R_PPC64_TPREL16_HI:
@@ -1481,22 +1481,41 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
case R_PPC64_ADDR16_HIGH:
write16(ctx, loc, hi(val));
break;
+ case R_PPC64_DTPREL16_HIGHER:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_HIGHER:
case R_PPC64_TPREL16_HIGHER:
write16(ctx, loc, higher(val));
break;
+ case R_PPC64_DTPREL16_HIGHERA:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_HIGHERA:
case R_PPC64_TPREL16_HIGHERA:
write16(ctx, loc, highera(val));
break;
+ case R_PPC64_DTPREL16_HIGHEST:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_HIGHEST:
case R_PPC64_TPREL16_HIGHEST:
write16(ctx, loc, highest(val));
break;
+ case R_PPC64_DTPREL16_HIGHESTA:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_HIGHESTA:
case R_PPC64_TPREL16_HIGHESTA:
write16(ctx, loc, highesta(val));
break;
+ case R_PPC64_GOT16_LO:
+ case R_PPC64_GOT_TLSGD16_LO:
+ case R_PPC64_GOT_TLSLD16_LO:
+ case R_PPC64_TOC16_LO:
+ case R_PPC64_DTPREL16_LO:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_LO:
case R_PPC64_REL16_LO:
case R_PPC64_TPREL16_LO:
@@ -1514,6 +1533,13 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
write16(ctx, loc, lo(val));
}
break;
+ case R_PPC64_GOT16_LO_DS:
+ case R_PPC64_GOT_TPREL16_LO_DS:
+ case R_PPC64_GOT_DTPREL16_LO_DS:
+ case R_PPC64_TOC16_LO_DS:
+ case R_PPC64_DTPREL16_LO_DS:
+ val -= ppc64TocOffset;
+ [[fallthrough]];
case R_PPC64_ADDR16_LO_DS:
case R_PPC64_TPREL16_LO_DS: {
// DQ-form instructions use bits 28-31 as part of the instruction encoding
@@ -1543,6 +1569,9 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
checkInt(ctx, loc, val, 32, rel);
write32(ctx, loc, val);
break;
+ case R_PPC64_DTPREL64:
+ val -= dynamicThreadPointerOffset;
+ [[fallthrough]];
case R_PPC64_ADDR64:
case R_PPC64_REL64:
case R_PPC64_TOC:
@@ -1563,13 +1592,7 @@ void PPC64::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
write32(ctx, loc, (read32(ctx, loc) & ~mask) | (val & mask));
break;
}
- case R_PPC64_DTPREL64:
- write64(ctx, loc, val - dynamicThreadPointerOffset);
- break;
case R_PPC64_DTPREL34:
- // The Dynamic Thread Vector actually points 0x8000 bytes past the start
- // of the TLS block. Therefore, in the case of R_PPC64_DTPREL34 we first
- // need to subtract that value then fallthrough to the general case.
val -= dynamicThreadPointerOffset;
[[fallthrough]];
case R_PPC64_PCREL34:
@@ -1737,18 +1760,17 @@ void PPC64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
if (rel.expr == R_RELAX_GOT_PC) {
lastPPCRelaxedRelocOff = rel.offset;
relaxGot(loc, rel, val);
- } else {
- relocate(loc, rel, val);
+ continue;
}
break;
case R_PPC64_PCREL_OPT:
// R_PPC64_PCREL_OPT must appear immediately after R_PPC64_GOT_PCREL34
// at the same offset. Only relax if the associated GOT_PCREL34 was
// relaxed.
- if (rel.expr == R_RELAX_GOT_PC && rel.offset == lastPPCRelaxedRelocOff)
+ if (rel.expr == R_RELAX_GOT_PC && rel.offset == lastPPCRelaxedRelocOff) {
relaxGot(loc, rel, val);
- else
- relocate(loc, rel, val);
+ continue;
+ }
break;
case R_PPC64_TOC16_HA:
case R_PPC64_TOC16_LO_DS:
@@ -1760,16 +1782,15 @@ void PPC64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
if (ctx.arg.tocOptimize &&
!ctx.ppc64noTocRelax.contains({rel.sym, rel.addend}) &&
tryRelaxPPC64TocIndirection(ctx, rel, loc))
- break;
- relocate(loc, rel, val);
+ continue;
break;
case R_PPC64_REL14:
case R_PPC64_REL24:
// If this is a call to __tls_get_addr, it may be part of a TLS
// sequence that has been relaxed and turned into a nop. In this
// case, we don't want to handle it as a call.
- if (read32(ctx, loc) == 0x60000000) // nop
- break;
+ if (read32(ctx, loc) == NOP)
+ continue;
// Patch a nop (0x60000000) to a ld.
if (rel.sym->needsTocRestore()) {
@@ -1778,57 +1799,19 @@ void PPC64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
// wrong in the common case where the function is not preempted at
// runtime. Just ignore.
if ((rel.offset + 8 > sec.content().size() ||
- read32(ctx, loc + 4) != 0x60000000) &&
+ read32(ctx, loc + 4) != NOP) &&
rel.sym->file != sec.file) {
// Use substr(6) to remove the "__plt_" prefix.
Err(ctx) << getErrorLoc(ctx, loc) << "call to "
<< toStr(ctx, *rel.sym).substr(6)
<< " lacks nop, can't restore toc";
- break;
+ continue;
}
write32(ctx, loc + 4, 0xe8410018); // ld %r2, 24(%r1)
}
- relocate(loc, rel, val);
- break;
- case R_PPC64_GOT_TLSGD16:
- case R_PPC64_GOT_TLSGD16_HA:
- case R_PPC64_GOT_TLSGD16_HI:
- case R_PPC64_GOT_TLSGD16_LO:
- case R_PPC64_GOT_TLSGD_PCREL34:
- case R_PPC64_TLSGD:
- if (rel.expr == R_TPREL)
- relaxTlsGdToLe(loc, rel, val);
- else if (oneof<R_GOT_OFF, R_GOT_PC>(rel.expr))
- relaxTlsGdToIe(loc, rel, val);
- else
- relocate(loc, rel, val);
- break;
- case R_PPC64_GOT_TLSLD16:
- case R_PPC64_GOT_TLSLD16_HA:
- case R_PPC64_GOT_TLSLD16_HI:
- case R_PPC64_GOT_TLSLD16_LO:
- case R_PPC64_GOT_TLSLD_PCREL34:
- case R_PPC64_TLSLD:
- if (rel.expr == R_TPREL)
- relaxTlsLdToLe(loc, rel, val);
- else
- relocate(loc, rel, val);
- break;
- case R_PPC64_GOT_TPREL16_HA:
- case R_PPC64_GOT_TPREL16_LO_DS:
- case R_PPC64_GOT_TPREL16_DS:
- case R_PPC64_GOT_TPREL16_HI:
- case R_PPC64_GOT_TPREL_PCREL34:
- case R_PPC64_TLS:
- if (rel.expr == R_TPREL)
- relaxTlsIeToLe(loc, rel, val);
- else
- relocate(loc, rel, val);
- break;
- default:
- relocate(loc, rel, val);
break;
}
+ relocate(loc, rel, val);
}
}
More information about the llvm-commits
mailing list