<div dir="auto">Could you add test case to the patch?</div><div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">Zhuo Zhi <<a href="mailto:xeonacid@archlinuxcn.org">xeonacid@archlinuxcn.org</a>>於 2026年11月8日週日,21:34寫道:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Before entering lang_relax_sections, elf_ldlang_before_allocation may<br>
set need_layout = true when either:<br>
  - bfd_elf_discard_info shrinks or removes .eh_frame entries, or<br>
  - bfd_elf_map_sections_to_segments updates elf_program_header_size<br>
    (causing SIZEOF_HEADERS to change).<br>
<br>
However, lang_relax_sections previously only checked need_layout after<br>
all relaxation passes finished, or inside the relaxation loop after<br>
individual passes.  When relaxation starts with need_layout == true,<br>
lang_size_sections_1 walks the linker script top-down and updates each<br>
output section's VMA right before invoking bfd_relax_section on its<br>
input sections.<br>
<br>
As a result, when relaxing an earlier output section (such as .text)<br>
that references a symbol in a later output section, the earlier output<br>
section already has its new VMA (reflecting the updated SIZEOF_HEADERS<br>
or .eh_frame size), whereas the later output section still holds its<br>
stale VMA from before elf_ldlang_before_allocation.<br>
<br>
On RISC-V, this causes _bfd_riscv_relax_call in pass 0 to underestimate<br>
the forward distance from .text to a later output section (e.g. when<br>
SIZEOF_HEADERS grows by 2 * sizeof(Elf64_Phdr) = 112 bytes after adding<br>
PT_TLS / PT_GNU_RELRO segments) and relax an auipc+jalr pair into jal.<br>
Once the later output section's VMA is updated, the actual distance<br>
exceeds the signed 21-bit range of R_RISCV_JAL, resulting in:<br>
<br>
  relocation truncated to fit: R_RISCV_JAL against symbol `...'<br>
<br>
Recalculate section addresses prior to the relaxation loop when<br>
need_layout is set so that all output sections start relaxation with<br>
consistent VMAs.<br>
<br>
        PR ld/34979<br>
        * ldlang.c (lang_relax_sections): If need_layout is true,<br>
        recalculate section addresses before running relaxation passes.<br>
<br>
Signed-off-by: Zhuo Zhi <<a href="mailto:xeonacid@archlinuxcn.org" target="_blank">xeonacid@archlinuxcn.org</a>><br>
---<br>
 ld/ldlang.c | 14 ++++++++++++++<br>
 1 file changed, 14 insertions(+)<br>
<br>
diff --git a/ld/ldlang.c b/ld/ldlang.c<br>
index b04ba113aa..757f04a79a 100644<br>
--- a/ld/ldlang.c<br>
+++ b/ld/ldlang.c<br>
@@ -8249,6 +8249,20 @@ lang_relax_sections (bool need_layout)<br>
   /* NB: Also enable relaxation to layout sections for DT_RELR.  */<br>
   if (RELAXATION_ENABLED || link_info.enable_dt_relr)<br>
     {<br>
+      /* If section layout has changed since sections were last sized<br>
+        (for example, when bfd_elf_discard_info edits .eh_frame or<br>
+        bfd_elf_map_sections_to_segments updates program_header_size),<br>
+        recalculate section addresses before relaxing.  Otherwise,<br>
+        lang_size_sections_1 updates output section VMAs top-down<br>
+        while invoking bfd_relax_section, causing earlier sections to<br>
+        see the new VMAs while later sections still have stale VMAs.  */<br>
+      if (need_layout)<br>
+       {<br>
+         lang_do_assignments (lang_assigning_phase_enum);<br>
+         lang_reset_memory_regions ();<br>
+         lang_size_sections (NULL, false);<br>
+       }<br>
+<br>
       /* We may need more than one relaxation pass.  */<br>
       int i = link_info.relax_pass;<br>
<br>
-- <br>
2.56.0<br>
<br>
</blockquote></div></div>