[binutils-gdb] elflink.c: accept dynamic and plugin for dynobj

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=f3ba1f30f14731242a0dfa28d4cd48a7dd75ef39

commit f3ba1f30f14731242a0dfa28d4cd48a7dd75ef39
Author: Alan Modra <[email protected]>
Date:   Thu Aug 6 11:17:05 2026 +0930

    elflink.c: accept dynamic and plugin for dynobj
    
    This patch makes _bfd_elf_link_dynobj accept input objects that are
    dynamic or plugins for use as dynobj.  Contrary to the comment about
    clashes with "a dynamic object with its own dynamic sections", there
    are in fact no sections to clash with as elf_link_add_object_symbols
    removes all sections from dynamic objects.  I also don't see why
    BFD_PLUGIN objects cannot hold dynamic sections.  In fact, for
    x86_64-linux there are 40 testcases in the testsuite that choose
    either a plugin or a dynamic library via the line I flagged with
    "??? Accept abfd anyway..".
    
    Besides changing the set of acceptable objects for dynobj, this patch
    also drops the abfd arg which was usually the bfd chosen for dynobj.
    Mostly the abfd arg had been checked that it had the correct xvec,
    but then there was vxworks.em which blindly assumed to first bfd was
    good.  Since the only reason bfd_elf_link_create_dynamic_sections has
    as abfd arg is to pass it to _bfd_elf_link_dynobj, drop that arg too.
    
    bfd/
            * elf-bfd.h (bfd_elf_link_create_dynamic_sections): Update prototype.
            * elflink.c (_bfd_elf_link_dynobj): Allow plugin and dynamic
            objects as dynobj.  Warn if no suitable object is found.
            (bfd_elf_link_create_dynamic_sections): Remove now unused
            "abfd" parameter.  Update all calles.
            * elf32-arc.c (elf_arc_check_relocs): Update call to
            bfd_elf_link_create_dynamic_sections.
            * elf32-tic6x.c (elf32_tic6x_check_relocs): Likewise.
            * elf64-hppa.c (elf64_hppa_check_relocs): Likewise.
    ld/
            * emultempl/vxworks.em (vxworks_after_open): Update call to
            bfd_elf_link_create_dynamic_sections.

Diff:
---
 bfd/elf-bfd.h           |  2 +-
 bfd/elf32-arc.c         |  2 +-
 bfd/elf32-tic6x.c       |  2 +-
 bfd/elf64-hppa.c        |  2 +-
 bfd/elflink.c           | 58 ++++++++++++++++++++++---------------------------
 ld/emultempl/vxworks.em |  3 +--
 6 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 88d35925987..f7886a57aa8 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2732,7 +2732,7 @@ extern struct bfd_section *bfd_elf_tls_setup
   (struct bfd_link_info *);
 
 extern bool bfd_elf_link_create_dynamic_sections
-  (bfd *, struct bfd_link_info *);
+  (struct bfd_link_info *);
 extern bool _bfd_elf_omit_section_dynsym_default
   (struct bfd_link_info *, asection *) ATTRIBUTE_HIDDEN;
 extern bool _bfd_elf_omit_section_dynsym_all
diff --git a/bfd/elf32-arc.c b/bfd/elf32-arc.c
index 52037e06508..19011c416af 100644
--- a/bfd/elf32-arc.c
+++ b/bfd/elf32-arc.c
@@ -2074,7 +2074,7 @@ elf_arc_check_relocs (bfd *			 abfd,
 		  {
 		    if (info->dynamic
 			&& ! htab->dynamic_sections_created
-			&& ! bfd_elf_link_create_dynamic_sections (abfd, info))
+			&& ! bfd_elf_link_create_dynamic_sections (info))
 		      return false;
 		    sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
 								  2, abfd,
diff --git a/bfd/elf32-tic6x.c b/bfd/elf32-tic6x.c
index a74e8235f32..e8223312b3d 100644
--- a/bfd/elf32-tic6x.c
+++ b/bfd/elf32-tic6x.c
@@ -2693,7 +2693,7 @@ elf32_tic6x_check_relocs (bfd *abfd, struct bfd_link_info *info,
   if ((bfd_link_pic (info) || elf32_tic6x_using_dsbt (abfd))
       && ! htab->elf.dynamic_sections_created)
     {
-      if (! bfd_elf_link_create_dynamic_sections (abfd, info))
+      if (!bfd_elf_link_create_dynamic_sections (info))
 	return false;
     }
 
diff --git a/bfd/elf64-hppa.c b/bfd/elf64-hppa.c
index 7ce9a3bdf2e..230ed4997ec 100644
--- a/bfd/elf64-hppa.c
+++ b/bfd/elf64-hppa.c
@@ -505,7 +505,7 @@ elf64_hppa_check_relocs (bfd *abfd,
      the special sections required for dynamic linking.  */
   if (! elf_hash_table (info)->dynamic_sections_created)
     {
-      if (! bfd_elf_link_create_dynamic_sections (abfd, info))
+      if (! bfd_elf_link_create_dynamic_sections (info))
 	return false;
     }
 
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 5a3ee89142b..ecbb4d3b42e 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -303,34 +303,29 @@ compatible_format (struct bfd_link_info *info, bfd *ibfd)
 /* Find a suitable object for attaching dynamic sections.  */
 
 static bfd *
-_bfd_elf_link_dynobj (bfd *abfd, struct bfd_link_info *info)
+_bfd_elf_link_dynobj (struct bfd_link_info *info)
 {
-  struct elf_link_hash_table *hash_table;
+  struct elf_link_hash_table *htab = elf_hash_table (info);
 
-  hash_table = elf_hash_table (info);
-  if (hash_table->dynobj == NULL)
-    {
-      /* We may not set dynobj, an input file holding linker created
-	 dynamic sections to abfd, which may be a dynamic object with
-	 its own dynamic sections.  We need to find a normal input file
-	 to hold linker created sections if possible.  */
-      if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
-	{
-	  bfd *ibfd;
-	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
-	    if ((ibfd->flags
-		 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
-		&& compatible_format (info, ibfd))
-	      {
-		abfd = ibfd;
-		break;
-	      }
-	}
-      /* ??? Accept abfd anyway, if the loop doesn't find anything?  */
-      hash_table->dynobj = abfd;
+  if (htab->dynobj == NULL)
+    {
+      /* We need to find an input file of the same format as the
+	 output to hold linker created sections.  For now, exclude
+	 any stub bfd.  */
+      bfd *ibfd = info->input_bfds;
+      if ((ibfd->flags
+	   & (BFD_PLUGIN | BFD_LINKER_CREATED)) == BFD_LINKER_CREATED)
+	ibfd = ibfd->link.next;
+      for (; ibfd; ibfd = ibfd->link.next)
+	if (compatible_format (info, ibfd))
+	  break;
+      if (ibfd == NULL)
+	_bfd_error_handler (_("no %s input object found"),
+			    bfd_get_target (info->output_bfd));
+      htab->dynobj = ibfd;
     }
 
-  return hash_table->dynobj;
+  return htab->dynobj;
 }
 
 /* Return the strtab for dynamic symbol names.  Create it if
@@ -347,14 +342,13 @@ _bfd_elf_link_dynstr (struct bfd_link_info *info)
 }
 
 /* Create some sections which will be filled in with dynamic linking
-   information.  ABFD is an input file which requires dynamic sections
-   to be created.  The dynamic sections take up virtual memory space
+   information.  The dynamic sections take up virtual memory space
    when the final executable is run, so we need to create them before
    addresses are assigned to the output sections.  We work out the
    actual contents and size of these sections later.  */
 
 bool
-bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
+bfd_elf_link_create_dynamic_sections (struct bfd_link_info *info)
 {
   flagword flags;
   asection *s;
@@ -371,7 +365,7 @@ bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   if (!_bfd_elf_link_dynstr (info))
     return false;
 
-  dynobj = _bfd_elf_link_dynobj (abfd, info);
+  dynobj = _bfd_elf_link_dynobj (info);
   if (dynobj == NULL)
     return false;
   obed = get_elf_backend_data (dynobj);
@@ -3969,7 +3963,7 @@ bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
   if (strindex == (size_t) -1)
     return -1;
 
-  if (!bfd_elf_link_create_dynamic_sections (abfd, info))
+  if (!bfd_elf_link_create_dynamic_sections (info))
     return -1;
 
   if (_bfd_elf_strtab_refcount (dynstr, strindex) != 1)
@@ -4601,7 +4595,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
 	  && info->output_bfd->xvec == abfd->xvec
 	  && !htab->dynamic_sections_created)
 	{
-	  if (!bfd_elf_link_create_dynamic_sections (abfd, info))
+	  if (!bfd_elf_link_create_dynamic_sections (info))
 	    goto error_return;
 	}
     }
@@ -4822,7 +4816,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
       /* Create dynamic sections for backends that require that be done
 	 before setup_gnu_properties.  */
       if (add_needed
-	  && !bfd_elf_link_create_dynamic_sections (abfd, info))
+	  && !bfd_elf_link_create_dynamic_sections (info))
 	return false;
 
       /* Save the DT_AUDIT entry for the linker emulation code. */
@@ -5760,7 +5754,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
 
 		  /* Create dynamic sections for backends that require
 		     that be done before setup_gnu_properties.  */
-		  if (!bfd_elf_link_create_dynamic_sections (abfd, info))
+		  if (!bfd_elf_link_create_dynamic_sections (info))
 		    goto error_free_vers;
 		  add_needed = true;
 		}
diff --git a/ld/emultempl/vxworks.em b/ld/emultempl/vxworks.em
index 80d32d31cb7..8c6c1330054 100644
--- a/ld/emultempl/vxworks.em
+++ b/ld/emultempl/vxworks.em
@@ -42,8 +42,7 @@ vxworks_after_open (void)
   if (force_dynamic
       && link_info.input_bfds
       && bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
-      && !bfd_elf_link_create_dynamic_sections (link_info.input_bfds,
-						&link_info))
+      && !bfd_elf_link_create_dynamic_sections (&link_info))
     einfo (_("%X%P: cannot create dynamic sections %E\n"));
 
   if (!force_dynamic
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.