[PATCH] ld: Recalculate section addresses before relaxing when need_layout is set [PR34979]

Zhuo Zhi xeonacid@archlinuxcn.org
Sun Nov 8 13:33:19 GMT 2026


Before entering lang_relax_sections, elf_ldlang_before_allocation may
set need_layout = true when either:
  - bfd_elf_discard_info shrinks or removes .eh_frame entries, or
  - bfd_elf_map_sections_to_segments updates elf_program_header_size
    (causing SIZEOF_HEADERS to change).

However, lang_relax_sections previously only checked need_layout after
all relaxation passes finished, or inside the relaxation loop after
individual passes.  When relaxation starts with need_layout == true,
lang_size_sections_1 walks the linker script top-down and updates each
output section's VMA right before invoking bfd_relax_section on its
input sections.

As a result, when relaxing an earlier output section (such as .text)
that references a symbol in a later output section, the earlier output
section already has its new VMA (reflecting the updated SIZEOF_HEADERS
or .eh_frame size), whereas the later output section still holds its
stale VMA from before elf_ldlang_before_allocation.

On RISC-V, this causes _bfd_riscv_relax_call in pass 0 to underestimate
the forward distance from .text to a later output section (e.g. when
SIZEOF_HEADERS grows by 2 * sizeof(Elf64_Phdr) = 112 bytes after adding
PT_TLS / PT_GNU_RELRO segments) and relax an auipc+jalr pair into jal.
Once the later output section's VMA is updated, the actual distance
exceeds the signed 21-bit range of R_RISCV_JAL, resulting in:

  relocation truncated to fit: R_RISCV_JAL against symbol `...'

Recalculate section addresses prior to the relaxation loop when
need_layout is set so that all output sections start relaxation with
consistent VMAs.

	PR ld/34979
	* ldlang.c (lang_relax_sections): If need_layout is true,
	recalculate section addresses before running relaxation passes.

Signed-off-by: Zhuo Zhi <xeonacid@archlinuxcn.org>
---
 ld/ldlang.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index b04ba113aa..757f04a79a 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -8249,6 +8249,20 @@ lang_relax_sections (bool need_layout)
   /* NB: Also enable relaxation to layout sections for DT_RELR.  */
   if (RELAXATION_ENABLED || link_info.enable_dt_relr)
     {
+      /* If section layout has changed since sections were last sized
+	 (for example, when bfd_elf_discard_info edits .eh_frame or
+	 bfd_elf_map_sections_to_segments updates program_header_size),
+	 recalculate section addresses before relaxing.  Otherwise,
+	 lang_size_sections_1 updates output section VMAs top-down
+	 while invoking bfd_relax_section, causing earlier sections to
+	 see the new VMAs while later sections still have stale VMAs.  */
+      if (need_layout)
+	{
+	  lang_do_assignments (lang_assigning_phase_enum);
+	  lang_reset_memory_regions ();
+	  lang_size_sections (NULL, false);
+	}
+
       /* We may need more than one relaxation pass.  */
       int i = link_info.relax_pass;
 
-- 
2.56.0



More information about the Binutils mailing list