[lld] [ELF] Retain .rela.dyn for PPC64 PI long-branch thunks (PR #212078)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 25 18:30:39 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld-elf
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
PPC64PILongBranchThunk adds a relative relocation, which runs after
removeUnusedSyntheticSections. With no other dynamic relocation
.rela.dyn is incorrectly removed.
Extract the .rela.auth.dyn condition (#<!-- -->96496, #<!-- -->195649) to `mayGrowLate`
and add a .branch_lt check.
---
Full diff: https://github.com/llvm/llvm-project/pull/212078.diff
3 Files Affected:
- (modified) lld/ELF/Writer.cpp (+14-10)
- (modified) lld/test/ELF/ppc64-long-branch-rel14.s (+5-1)
- (modified) lld/test/ELF/ppc64-pcrel-call-to-pcrel.s (+10-10)
``````````diff
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 22946090b4891..bc0caf6beec32 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1747,6 +1747,17 @@ template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
is->trim();
}
+// Sections that finalizeAddressDependentContent may add to.
+static bool mayGrowLate(Ctx &ctx, SyntheticSection *sec) {
+ if (sec != ctx.in.relaDyn.get())
+ return false;
+ // Relocations may move here from .relr.auth.dyn.
+ if (ctx.in.relrAuthDyn && ctx.in.relrAuthDyn->isNeeded())
+ return true;
+ // PPC64PILongBranchThunk adds a relative relocation for its .branch_lt entry.
+ return ctx.in.ppc64LongBranchTarget && ctx.arg.isPic;
+}
+
// In order to allow users to manipulate linker-synthesized sections,
// we had to add synthetic sections to the input section list early,
// even before we make decisions whether they are needed. This allows
@@ -1758,7 +1769,8 @@ template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
//
// To deal with the above problem, this function is called after
// scanRelocations is called to remove synthetic sections that turn
-// out to be empty.
+// out to be empty. It runs before finalizeAddressDependentContent, which may
+// add to a section mayGrowLate reports.
static void removeUnusedSyntheticSections(Ctx &ctx) {
// All input synthetic sections that can be empty are placed after
// all regular ones. Reverse iterate to find the first synthetic section
@@ -1773,15 +1785,7 @@ static void removeUnusedSyntheticSections(Ctx &ctx) {
auto end =
std::remove_if(start, ctx.inputSections.end(), [&](InputSectionBase *s) {
auto *sec = cast<SyntheticSection>(s);
- if (sec->getParent() && sec->isNeeded())
- return false;
- // .relr.auth.dyn relocations may be moved to .rela.dyn in
- // finalizeAddressDependentContent, making .rela.dyn no longer empty.
- // Conservatively keep .rela.dyn. .relr.auth.dyn can be made empty, but
- // we would fail to remove it here.
- if (ctx.arg.emachine == EM_AARCH64 && ctx.arg.relrPackDynRelocs &&
- sec == ctx.in.relaDyn.get() && ctx.in.relrAuthDyn &&
- ctx.in.relrAuthDyn->isNeeded())
+ if ((sec->getParent() && sec->isNeeded()) || mayGrowLate(ctx, sec))
return false;
unused.insert(sec);
return true;
diff --git a/lld/test/ELF/ppc64-long-branch-rel14.s b/lld/test/ELF/ppc64-long-branch-rel14.s
index 722f3b6513bd9..5ac3f16a4eca1 100644
--- a/lld/test/ELF/ppc64-long-branch-rel14.s
+++ b/lld/test/ELF/ppc64-long-branch-rel14.s
@@ -11,11 +11,15 @@
# RUN: llvm-mc -filetype=obj -triple=powerpc64le %s -o %t.o
# RUN: ld.lld -pie -T %t.lds %t.o -o %t
-# RUN: llvm-readelf -r %t | FileCheck --check-prefix=SEC %s
+# RUN: llvm-readelf -r %t | FileCheck --check-prefix=SEC-PIE %s
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
# SEC: There are no relocations in this file.
+# SEC-PIE: Relocation section '.rela.dyn' {{.*}} contains 2 entries:
+# SEC-PIE: 000000000000a0b0 {{[0-9a-f]+}} R_PPC64_RELATIVE a004
+# SEC-PIE-NEXT: 000000000000a0b8 {{[0-9a-f]+}} R_PPC64_RELATIVE a010
+
# CHECK-LABEL: <_start>:
# CHECK-NEXT: 2000: bt 2, 0x2020
# CHECK-NEXT: bt+ 2, 0x2020
diff --git a/lld/test/ELF/ppc64-pcrel-call-to-pcrel.s b/lld/test/ELF/ppc64-pcrel-call-to-pcrel.s
index 6cbbbfb886e24..4662fe7fd1ff0 100644
--- a/lld/test/ELF/ppc64-pcrel-call-to-pcrel.s
+++ b/lld/test/ELF/ppc64-pcrel-call-to-pcrel.s
@@ -28,16 +28,16 @@
# RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=SYMBOL-GLOBAL
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
-# SYMBOL: 2: 0000000010010000 0 NOTYPE LOCAL DEFAULT 5 callee1_stother0_default
-# SYMBOL-NEXT: 3: 0000000010010004 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 5 caller1
-# SYMBOL-NEXT: 4: 0000000010020000 0 NOTYPE LOCAL DEFAULT 6 func_local
-# SYMBOL-NEXT: 5: 0000000010020004 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 6 callee2_stother1_default
-# SYMBOL-NEXT: 6: 000000001002000c 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 6 caller2
-# SYMBOL-NEXT: 7: 0000000010030000 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 7 caller3
-# SYMBOL-NEXT: 8: 0000000010040000 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 8 caller4
-# SYMBOL-NEXT: 9: 0000000010040008 0 NOTYPE LOCAL DEFAULT 9 func_extern
-# SYMBOL-NEXT: 10: 000000001004000c 0 NOTYPE LOCAL HIDDEN 9 callee3_stother0_hidden
-# SYMBOL-NEXT: 11: 0000000010040010 0 NOTYPE LOCAL HIDDEN [<other: 0x22>] 9 callee4_stother1_hidden
+# SYMBOL: 2: 0000000010010000 0 NOTYPE LOCAL DEFAULT 6 callee1_stother0_default
+# SYMBOL-NEXT: 3: 0000000010010004 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 6 caller1
+# SYMBOL-NEXT: 4: 0000000010020000 0 NOTYPE LOCAL DEFAULT 7 func_local
+# SYMBOL-NEXT: 5: 0000000010020004 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 7 callee2_stother1_default
+# SYMBOL-NEXT: 6: 000000001002000c 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 7 caller2
+# SYMBOL-NEXT: 7: 0000000010030000 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 8 caller3
+# SYMBOL-NEXT: 8: 0000000010040000 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 9 caller4
+# SYMBOL-NEXT: 9: 0000000010040008 0 NOTYPE LOCAL DEFAULT 10 func_extern
+# SYMBOL-NEXT: 10: 000000001004000c 0 NOTYPE LOCAL HIDDEN 10 callee3_stother0_hidden
+# SYMBOL-NEXT: 11: 0000000010040010 0 NOTYPE LOCAL HIDDEN [<other: 0x22>] 10 callee4_stother1_hidden
# SYMBOL-GLOBAL: 2: 0000000010010004 0 NOTYPE LOCAL DEFAULT [<other: 0x20>] 1 caller1
# SYMBOL-GLOBAL-NEXT: 3: 0000000010020000 0 NOTYPE LOCAL DEFAULT 2 func_local
``````````
</details>
https://github.com/llvm/llvm-project/pull/212078
More information about the llvm-commits
mailing list