[lld] [lld][ELF] Fix dynamic relocations in .eh_frame (PR #210967)
Kirill A. Korinsky via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 21 05:35:42 PDT 2026
https://github.com/catap created https://github.com/llvm/llvm-project/pull/210967
scanEhSection translates input offsets to merged .eh_frame offsets before creating dynamic relocations. DynamicReloc::getOffset translated them a second time through EhInputSection.
Use the synthetic parent for already merged offsets, and use the finalized relocation offset when verifying applied addends.
>From 7e202297a930e2210a7014c5d77dff1ebec5800b Mon Sep 17 00:00:00 2001
From: "Kirill A. Korinsky" <kirill at korins.ky>
Date: Thu, 16 Jul 2026 23:52:42 +0200
Subject: [PATCH] [lld][ELF] Fix dynamic relocations in .eh_frame
scanEhSection translates input offsets to merged .eh_frame offsets
before creating dynamic relocations. DynamicReloc::getOffset translated
them a second time through EhInputSection.
Use the synthetic parent for already merged offsets, and use the
finalized relocation offset when verifying applied addends.
---
lld/ELF/OutputSections.cpp | 4 ++--
lld/ELF/Relocations.cpp | 4 ++--
lld/ELF/SyntheticSections.cpp | 2 ++
lld/test/ELF/eh-frame-dyn-reloc.s | 36 +++++++++++++++++++++++++++++++
4 files changed, 42 insertions(+), 4 deletions(-)
create mode 100644 lld/test/ELF/eh-frame-dyn-reloc.s
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index eb1d8af5f375b..d3dcd6a5b77e9 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -924,8 +924,8 @@ void OutputSection::checkDynRelAddends(Ctx &ctx) {
(rel.inputSec == ctx.in.ppc64LongBranchTarget.get() ||
rel.inputSec == ctx.in.igotPlt.get()))
continue;
- const uint8_t *relocTarget = ctx.bufferStart + relOsec->offset +
- rel.inputSec->getOffset(rel.offsetInSec);
+ const uint8_t *relocTarget =
+ ctx.bufferStart + relOsec->offset + (rel.r_offset - relOsec->addr);
// For SHT_NOBITS the written addend is always zero.
int64_t writtenAddend =
relOsec->type == SHT_NOBITS
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 4702d941d28ca..6541205001ec3 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -983,8 +983,8 @@ void RelocScan::processAux(RelExpr expr, RelType type, uint64_t offset,
}
// Use a simple -z notext rule that treats all sections except .eh_frame as
- // writable. GNU ld does not produce dynamic relocations in .eh_frame (and our
- // SectionBase::getOffset would incorrectly adjust the offset).
+ // writable. GNU ld does not produce dynamic relocations in read-only
+ // .eh_frame.
//
// For MIPS, we don't implement GNU ld's DW_EH_PE_absptr to DW_EH_PE_pcrel
// conversion. We still emit a dynamic relocation.
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index fda7a835827ce..799a6afba3fae 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -1440,6 +1440,8 @@ template <class ELFT> void DynamicSection<ELFT>::writeTo(uint8_t *buf) {
}
uint64_t DynamicReloc::getOffset() const {
+ if (auto *eh = dyn_cast<EhInputSection>(inputSec))
+ return eh->getParent()->getVA(offsetInSec);
return inputSec->getVA(offsetInSec);
}
diff --git a/lld/test/ELF/eh-frame-dyn-reloc.s b/lld/test/ELF/eh-frame-dyn-reloc.s
new file mode 100644
index 0000000000000..1b284770dd4fe
--- /dev/null
+++ b/lld/test/ELF/eh-frame-dyn-reloc.s
@@ -0,0 +1,36 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/first.s -o %t/first-ro.o
+# RUN: llvm-objcopy --set-section-flags .eh_frame=alloc,data \
+# RUN: %t/first-ro.o %t/first.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/second.s -o %t/second-ro.o
+# RUN: llvm-objcopy --set-section-flags .eh_frame=alloc,data \
+# RUN: %t/second-ro.o %t/second.o
+# RUN: ld.lld -shared -z notext --apply-dynamic-relocs \
+# RUN: --check-dynamic-relocations \
+# RUN: %t/first.o %t/second.o -o %t/out.so
+# RUN: llvm-readelf -S -r %t/out.so | FileCheck %s
+
+# CHECK: .eh_frame PROGBITS [[#%x,EH:]]
+# CHECK: {{0*}}[[#%x,EH+0x12]] {{.*}} R_X86_64_64 {{.*}} external1 + 10
+# CHECK: {{0*}}[[#%x,EH+0x4a]] {{.*}} R_X86_64_64 {{.*}} external2 + 20
+
+#--- first.s
+.set personality1, external1+0x10
+.text
+.globl first
+first:
+ .cfi_startproc
+ .cfi_personality 0, personality1
+ ret
+ .cfi_endproc
+
+#--- second.s
+.set personality2, external2+0x20
+.text
+.globl second
+second:
+ .cfi_startproc
+ .cfi_personality 0, personality2
+ ret
+ .cfi_endproc
More information about the llvm-commits
mailing list