[binutils-gdb] Delay writing of stub bfd sections

Alan Modra via Binutils-cvs <[email protected]>
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ffd2a7258c46d74d960502b3b3f77aaa44b8118b

commit ffd2a7258c46d74d960502b3b3f77aaa44b8118b
Author: Alan Modra <[email protected]>
Date:   Fri Aug 21 09:32:57 2026 +0930

    Delay writing of stub bfd sections
    
    AARCH64 continues an ARM tradition of updating stubs late.  See
    coff_arm_link_output_has_begun.  Commit e189bfd9b492 broke AARCH64,
    because adding dynamic sections to the stub bfd resulted in that
    entire bfd being written out fairly early.  Prior to e189bfd9b492
    dynamic sections were usually added to crt1.o, the first object being
    linked.  If crt1.o happened to need an erratum_843419 stub I think
    we'd see the same sort of breakage exposed by e189bfd9b492, with later
    object files' erratum_843419 stubs not being updated (resulting in
    an all-zero insn in the stub).  You would likely hit the same problem
    if user linker scripts divided up code sections for some reason.
    
    There likely is no reason to clear sub->output_has_begun here, but
    I'll leave removing that to a followup patch.
    
            * elflink.c (_bfd_elf_final_link): Write any linker created
            bfd last.

Diff:
---
 bfd/elflink.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index d1edf1bddf7..df9c698d874 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -13193,7 +13193,8 @@ _bfd_elf_final_link (bfd *obfd, struct bfd_link_info *info)
 	    {
 	      if (! sub->output_has_begun)
 		{
-		  if (! elf_link_input_bfd (&flinfo, sub))
+		  if ((sub->flags & BFD_LINKER_CREATED) == 0
+		      && !elf_link_input_bfd (&flinfo, sub))
 		    goto error_return;
 		  sub->output_has_begun = true;
 		}
@@ -13244,6 +13245,17 @@ _bfd_elf_final_link (bfd *obfd, struct bfd_link_info *info)
 	    }
 	}
     }
+  /* Writing of linker created BFDs is left until last, because the
+     aarch64 backend wants to copy insns from a relocated section to
+     a stub section.  See erratum_843419 code.  */
+  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
+    if (sub->output_has_begun && (sub->flags & BFD_LINKER_CREATED) != 0)
+      {
+	sub->output_has_begun = false;
+	if (!elf_link_input_bfd (&flinfo, sub))
+	  goto error_return;
+	sub->output_has_begun = true;
+      }
 
   /* Free symbol buffer if needed.  */
   if (!info->reduce_memory_overheads)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.