public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Set sh_link for reloc sections created as normal sections
@ 2021-12-09 22:06 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2021-12-09 22:06 UTC (permalink / raw)
  To: binutils

binutils-all/strip-13 and binutils-all/strip-14 tests create
SHT_REL/SHT_RELA sections by hand.  These don't have sh_link set to
the .symtab section as they should, leading to readelf warnings if you
happen to be looking at the object files.

	* elf.c (assign_section_numbers): Formatting.  Set sh_link for
	reloc sections created as normal sections in relocatable
	objects.

diff --git a/bfd/elf.c b/bfd/elf.c
index 5ffe36900e6..98f47da0797 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -3779,9 +3779,9 @@ assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
     }
 
   need_symtab = (bfd_get_symcount (abfd) > 0
-		|| (link_info == NULL
-		    && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
-			== HAS_RELOC)));
+		 || (link_info == NULL
+		     && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
+			 == HAS_RELOC)));
   if (need_symtab)
     {
       elf_onesymtab (abfd) = section_number++;
@@ -3935,11 +3935,17 @@ assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
 	     section.  sh_link is the section index of the symbol
 	     table.  sh_info is the section index of the section to
 	     which the relocation entries apply.  We assume that an
-	     allocated reloc section uses the dynamic symbol table.
-	     FIXME: How can we be sure?  */
-	  s = bfd_get_section_by_name (abfd, ".dynsym");
-	  if (s != NULL)
-	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
+	     allocated reloc section uses the dynamic symbol table
+	     if there is one.  Otherwise we guess the normal symbol
+	     table.  FIXME: How can we be sure?  */
+	  if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
+	    {
+	      s = bfd_get_section_by_name (abfd, ".dynsym");
+	      if (s != NULL)
+		d->this_hdr.sh_link = elf_section_data (s)->this_idx;
+	    }
+	  if (d->this_hdr.sh_link == 0)
+	    d->this_hdr.sh_link = elf_onesymtab (abfd);
 
 	  s = elf_get_reloc_section (sec);
 	  if (s != NULL)
@@ -3994,8 +4000,8 @@ assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
 	  /* sh_link is the section header index of the prelink library
 	     list used for the dynamic entries, or the symbol table, or
 	     the version strings.  */
-	  s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
-					     ? ".dynstr" : ".gnu.libstr");
+	  s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
+					      ? ".dynstr" : ".gnu.libstr"));
 	  if (s != NULL)
 	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
 	  break;

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-12-09 22:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-09 22:06 Set sh_link for reloc sections created as normal sections Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).