[binutils-gdb] RISC-V: Add no-op relax pass 0 to update section VMAs before shortening [PR34979]
Kito Cheng
kito@sourceware.org
Thu Nov 19 09:04:13 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=20a3bfdbee7e4615e96d779744e25e272beb1f4c
commit 20a3bfdbee7e4615e96d779744e25e272beb1f4c
Author: Zhuo Zhi <xeonacid@archlinuxcn.org>
Date: Wed Nov 11 14:32:09 2026 +0100
RISC-V: Add no-op relax pass 0 to update section VMAs before shortening [PR34979]
When ldelf_map_segments invokes lang_relax_sections (true) after either
bfd_elf_discard_info edits .eh_frame or bfd_elf_map_sections_to_segments
increases elf_program_header_size (growing SIZEOF_HEADERS),
lang_size_sections_1 walks the linker script top-down and updates each
output section's VMA immediately before invoking bfd_relax_section on
its input sections.
As a result, when relaxing an earlier output section (such as .text)
that calls a symbol in a later output section, the earlier output
section already has its new VMA (shifted forward by the SIZEOF_HEADERS
increase), whereas the later output section still holds its stale VMA
from the previous iteration.
Although _bfd_riscv_relax_call and _bfd_riscv_relax_jal already reserve
max_alignment slack for alignment-induced section movement, max_alignment
can be smaller than the SIZEOF_HEADERS growth (for example, max_alignment
is 8 bytes while 2 * sizeof(Elf64_Phdr) is 112 bytes). The temporary
stale-VMA discrepancy causes _bfd_riscv_relax_call to underestimate the
forward PC-relative offset and irreversibly shorten auipc+jalr into jal,
which then overflows R_RISCV_JAL once the target output section's VMA is
updated:
relocation truncated to fit: R_RISCV_JAL against symbol `...'
Per Alan Modra's suggestion, prepend a no-op "layout" pass at pass 0 in
bfd_elfNN_riscv_init_relax_passes so that all output section VMAs are
recalculated consistently before the "shorten" pass runs, without adding
an extra sizing pass to other targets.
bfd/
PR ld/34979
* elfnn-riscv.c (struct riscv_relax_pass): Document NULL select.
(riscv_relax_pass_layout): New no-op relax pass.
(bfd_elfNN_riscv_init_relax_passes): Register
riscv_relax_pass_layout as pass 0.
(_bfd_riscv_relax_section): Return early when pass->select is
NULL.
ld/
PR ld/34979
* testsuite/ld-riscv-elf/pr34979.s: New test source.
* testsuite/ld-riscv-elf/pr34979-rv32.d: New test.
* testsuite/ld-riscv-elf/pr34979-rv64.d: New test.
* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run pr34979-rv32 and
pr34979-rv64.
Signed-off-by: Zhuo Zhi <xeonacid@archlinuxcn.org>
Diff:
---
bfd/elfnn-riscv.c | 11 ++++++++++-
ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 2 ++
ld/testsuite/ld-riscv-elf/pr34979-rv32.d | 18 ++++++++++++++++++
ld/testsuite/ld-riscv-elf/pr34979-rv64.d | 18 ++++++++++++++++++
ld/testsuite/ld-riscv-elf/pr34979.s | 10 ++++++++++
5 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 17c714ab2c8..78b62e6f339 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -5760,7 +5760,9 @@ typedef struct riscv_relax_pass
/* Decide how to relax RELOCS[*I]. Set *FUNC to the relax function, or
leave it NULL to skip the reloc. May step *I over a paired
- R_RISCV_RELAX. Return false on error. */
+ R_RISCV_RELAX. Return false on error. If SELECT itself is NULL,
+ nothing is relaxed in this pass so that section addresses are
+ recalculated first. */
bool (*select) (bfd *, asection *, struct bfd_link_info *,
Elf_Internal_Rela *, unsigned int *, relax_func_t *);
@@ -5855,6 +5857,11 @@ riscv_relax_select_align (bfd *abfd ATTRIBUTE_UNUSED,
return true;
}
+/* No-op pass to recalculate output section VMAs (e.g. after .eh_frame
+ edits or SIZEOF_HEADERS changes) before shortening instructions. */
+static const riscv_relax_pass riscv_relax_pass_layout =
+ { "layout", false, NULL, NULL, NULL };
+
static const riscv_relax_pass riscv_relax_pass_shorten =
{ "shorten", false, riscv_relax_select_shorten, NULL, NULL };
@@ -5894,6 +5901,7 @@ bfd_elfNN_riscv_init_relax_passes (struct bfd_link_info *info)
htab->num_relax_passes = 0;
htab->cur_relax_pass = -1;
+ riscv_add_relax_pass (htab, &riscv_relax_pass_layout);
riscv_add_relax_pass (htab, &riscv_relax_pass_shorten);
riscv_add_relax_pass (htab, &riscv_relax_pass_align);
@@ -5932,6 +5940,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec,
pass = htab->relax_passes[info->relax_pass];
if (bfd_link_relocatable (info)
+ || pass->select == NULL
|| sec->sec_flg0
|| sec->reloc_count == 0
|| (sec->flags & SEC_RELOC) == 0
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index ad12f8d63b1..cba0923c650 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -286,6 +286,8 @@ if [istarget "riscv*-*-*"] {
run_dump_test "pie-bind-locally-rv32"
run_dump_test "pie-bind-locally-rv64"
+ run_dump_test "pr34979-rv32"
+ run_dump_test "pr34979-rv64"
run_dump_test "discard-exe"
run_dump_test "discard-pie"
diff --git a/ld/testsuite/ld-riscv-elf/pr34979-rv32.d b/ld/testsuite/ld-riscv-elf/pr34979-rv32.d
new file mode 100644
index 00000000000..3d8b9d7106b
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pr34979-rv32.d
@@ -0,0 +1,18 @@
+#source: pr34979.s
+#as: -march=rv32i -mabi=ilp32 -mrelax
+#ld: -m[riscv_choose_ilp32_emul] -pie --relax
+#objdump: -d -Mno-aliases
+
+.*:[ ]+file format .*
+
+Disassembly of section \.text:
+
+0+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+auipc[ ]+ra,0x100
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+jalr[ ]+ra,4\(ra\) # [0-9a-f]+ <target>
+ \.\.\.
+
+Disassembly of section __lcxx_override:
+
+0+[0-9a-f]+ <target>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+jalr[ ]+zero,0\(ra\)
diff --git a/ld/testsuite/ld-riscv-elf/pr34979-rv64.d b/ld/testsuite/ld-riscv-elf/pr34979-rv64.d
new file mode 100644
index 00000000000..bedc784ac76
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pr34979-rv64.d
@@ -0,0 +1,18 @@
+#source: pr34979.s
+#as: -march=rv64i -mabi=lp64 -mrelax
+#ld: -m[riscv_choose_lp64_emul] -pie --relax
+#objdump: -d -Mno-aliases
+
+.*:[ ]+file format .*
+
+Disassembly of section \.text:
+
+0+[0-9a-f]+ <_start>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+auipc[ ]+ra,0x100
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+jalr[ ]+ra,4\(ra\) # [0-9a-f]+ <target>
+ \.\.\.
+
+Disassembly of section __lcxx_override:
+
+0+[0-9a-f]+ <target>:
+[ ]+[0-9a-f]+:[ ]+[0-9a-f]+[ ]+jalr[ ]+zero,0\(ra\)
diff --git a/ld/testsuite/ld-riscv-elf/pr34979.s b/ld/testsuite/ld-riscv-elf/pr34979.s
new file mode 100644
index 00000000000..1f4bffa1013
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pr34979.s
@@ -0,0 +1,10 @@
+ .text
+ .globl _start
+_start:
+ call target
+ .space 0xffffc
+
+ .section __lcxx_override,"ax",@progbits
+ .globl target
+target:
+ ret
More information about the Binutils-cvs
mailing list