[llvm-branch-commits] [lld] 71c7675 - [ELF] Move layout-changing transforms before address assignment (#232189)
Tobias Hieta via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Oct 18 05:08:52 PDT 2026
Author: Fangrui Song
Date: 2026-10-18T14:08:32+02:00
New Revision: 71c76755a8ffda1190f58d7973d11ad5448c2ecc
URL: https://github.com/llvm/llvm-project/commit/71c76755a8ffda1190f58d7973d11ad5448c2ecc
DIFF: https://github.com/llvm/llvm-project/commit/71c76755a8ffda1190f58d7973d11ad5448c2ecc.diff
LOG: [ELF] Move layout-changing transforms before address assignment (#232189)
finalizeAddressDependentContent assigns addresses, then
--randomize-section-padding and CFI jump table relaxation
(--branch-to-branch, implied by -O2) move input sections, so the first
fixed-point iteration computes address-dependent content from stale
addresses. Since #179089, EhFrameHeader::updateAllocSize caches the
.eh_frame_hdr search table; if no iteration changes anything, the loop
exits and the table keys no longer match the FDEs' initial_location.
Neither transform reads addresses, so run them before the first
assignAddresses. SHF_LINK_ORDER sections are now sorted by the relaxed
layout as well.
LLM-aided. Partially fix #226166
(cherry picked from commit 115554f90f920fc000234c2c247b8318d4e7d2d8)
Added:
lld/test/ELF/x86-64-relax-jump-tables-eh-frame-hdr.s
Modified:
lld/ELF/Writer.cpp
lld/test/ELF/randomize-section-padding.test
Removed:
################################################################################
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 635bb47defb83f..e88ef20eea65bc 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1484,6 +1484,13 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
llvm::TimeTraceScope timeScope("Finalize address dependent content");
AArch64Err843419Patcher a64p(ctx);
ARMErr657417Patcher a32p(ctx);
+
+ // Transforms that move input sections without reading addresses go here.
+ // After the first assignAddresses, only the loop below may change layout.
+ if (ctx.arg.randomizeSectionPadding)
+ randomizeSectionPadding(ctx);
+ if (ctx.arg.branchToBranch)
+ ctx.target->relaxCFIJumpTables();
ctx.script->assignAddresses();
// .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
@@ -1497,12 +1504,6 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
};
finalizeOrderDependentContent();
- if (ctx.arg.randomizeSectionPadding)
- randomizeSectionPadding(ctx);
-
- if (ctx.arg.branchToBranch)
- ctx.target->relaxCFIJumpTables();
-
// Iterate until a fixed point is reached, skipping relocatable links since
// the final addresses are unavailable.
uint32_t pass = 0, assignPasses = 0;
diff --git a/lld/test/ELF/randomize-section-padding.test b/lld/test/ELF/randomize-section-padding.test
index af8e4f14981cd1..3c29400590cdf4 100644
--- a/lld/test/ELF/randomize-section-padding.test
+++ b/lld/test/ELF/randomize-section-padding.test
@@ -71,6 +71,16 @@
# PAD46-LDS: Hex dump of section '.data':
# PAD46-LDS: 0x00002530 00000000 00000000 00000000 09000807 ................
+## .eh_frame_hdr uses the address after padding.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/eh.s -o %t/eh.o
+# RUN: ld.lld --randomize-section-padding=6 -z separate-code --eh-frame-hdr %t/eh.o -o %t/eh
+# RUN: llvm-readelf -s --unwind %t/eh | FileCheck --check-prefix=EH %s
+
+# EH: entry 0 {
+# EH-NEXT: initial_location: 0x[[#%x,F:]]
+# EH: [[#%.16x,F]] {{.*}} f{{$}}
+
#--- a.s
.section .rodata.a,"a", at progbits
@@ -123,3 +133,9 @@ SECTIONS {
.data : { *(.data.c) *(.data.b) *(.data.a) }
.bss : { *(.bss.c) *(.bss.b) *(.bss.a) }
}
+
+#--- eh.s
+f:
+ .cfi_startproc
+ ret
+ .cfi_endproc
diff --git a/lld/test/ELF/x86-64-relax-jump-tables-eh-frame-hdr.s b/lld/test/ELF/x86-64-relax-jump-tables-eh-frame-hdr.s
new file mode 100644
index 00000000000000..157046ce4a3566
--- /dev/null
+++ b/lld/test/ELF/x86-64-relax-jump-tables-eh-frame-hdr.s
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+## CFI jump table relaxation moves f.cfi. .eh_frame_hdr uses the new address.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+# RUN: ld.lld --branch-to-branch --eh-frame-hdr %t.o -o %t
+# RUN: llvm-readelf -s --unwind %t | FileCheck %s
+
+# CHECK: entry 0 {
+# CHECK-NEXT: initial_location: 0x[[#%x,F:]]
+# CHECK: [[#%.16x,F]] {{.*}} f.cfi{{$}}
+
+.section .text.jt,"ax", at llvm_cfi_jump_table,8
+.globl _start
+_start:
+ jmp f.cfi
+ .balign 8, 0xcc
+
+.section .text.f,"ax", at progbits
+f.cfi:
+ .cfi_startproc
+ ret
+ .cfi_endproc
More information about the llvm-branch-commits
mailing list