Ignore empty symtab sections

Message ID auJ2_Wox0zewHJmO@squeak.grove.modra.org
State New
Headers
Series Ignore empty symtab sections |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Alan Modra Oct. 28, 2026, 10:28 p.m. UTC
  Confusion results if we accept an empty .symtab as elf_symtab_hdr.  A
lot of BFD code for ELF targets supported by GNU ld requires that the
STN_UNDEF symbol be treated as a local sym when processing relocs,
which means it must be present and sh_info for .symtab be non-zero.

This patch stops BFD from attempting to handle relocations in an
object with an empty .symtab, as is already the case for relocations
where .symtab is missing entirely.  More reporting is done too.

The extra reporting showed failures on rx-elf when attempting to
assemble strip-15rela.s from the binutils testsuite.  The test would
need to be modified to create .relaP rather than .rela.text to work
on that target.  I opted instead to disable the test.

	PR 34835
	PR 13962
	PR 7023
bfd/
	* elf.c (bfd_section_from_shdr): Do not use an empty SHT_SYMTAB
	or SHT_DYNSYM section.  Validate sh_info of SHT_SYMTAB and
	SHT_DYNSYM sections.  Delete bogus SHT_SYMTAB comment.  Report
	an error on bad sh_link or sh_info of reloc sections.
binutils/
	* testsuite/binutils-all/strip-15.d: Disable for rx.
	* testsuite/binutils-all/strip-section-headers-1.d: Likewise.
	* testsuite/binutils-all/strip-13.d: Correct rx comment.
	* testsuite/binutils-all/strip-14.d: Likewise
  

Patch

diff --git a/bfd/elf.c b/bfd/elf.c
index 2cae97a6dd3..8e1b97388f4 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -2606,16 +2606,16 @@  bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
       if (hdr->sh_entsize != bed->s->sizeof_sym)
 	goto fail;
 
+      /* Ignore a zero sized symbol table.  */
+      if (hdr->sh_size < hdr->sh_entsize)
+	goto success;
+
+      /* sh_info must always point past the first symbol.  */
+      if (hdr->sh_info == 0)
+	goto fail;
+
       if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
-	{
-	  if (hdr->sh_size != 0)
-	    goto fail;
-	  /* Some assemblers erroneously set sh_info to one with a
-	     zero sh_size.  ld sees this as a global symbol count
-	     of (unsigned) -1.  Fix it here.  */
-	  hdr->sh_info = 0;
-	  goto success;
-	}
+	goto fail;
 
       /* PR 18854: A binary might contain more than one symbol table.
 	 Unusual, but possible.  Warn, but continue.  */
@@ -2678,8 +2678,6 @@  bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
 
 	if (i != shindex)
 	  ret = bfd_section_from_shdr (abfd, i);
-	/* else FIXME: we have failed to find the symbol table.
-	   Should we issue an error?  */
 	goto success;
       }
 
@@ -2690,17 +2688,16 @@  bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
       if (hdr->sh_entsize != bed->s->sizeof_sym)
 	goto fail;
 
-      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
-	{
-	  if (hdr->sh_size != 0)
-	    goto fail;
+      /* Ignore a zero sized symbol table.  */
+      if (hdr->sh_size < hdr->sh_entsize)
+	goto success;
 
-	  /* Some linkers erroneously set sh_info to one with a
-	     zero sh_size.  ld sees this as a global symbol count
-	     of (unsigned) -1.  Fix it here.  */
-	  hdr->sh_info = 0;
-	  goto success;
-	}
+      /* sh_info must always point past the first symbol.  */
+      if (hdr->sh_info == 0)
+	goto fail;
+
+      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
+	goto fail;
 
       /* PR 18854: A binary might contain more than one dynamic symbol table.
 	 Unusual, but possible.  Warn, but continue.  */
@@ -2805,13 +2802,15 @@  bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
     case SHT_REL:
     case SHT_RELA:
     case SHT_RELR:
-      /* *These* do a lot of work -- but build no sections!  */
+      /* *These* do a lot of work -- but usually build no sections!  */
       {
 	asection *target_sect;
 	Elf_Internal_Shdr *hdr2, **p_hdr;
 	unsigned int num_sec = elf_numsections (abfd);
 	struct bfd_elf_section_data *esdt;
 	bfd_size_type size;
+	const char *bad_field;
+	unsigned int bad_val;
 
 	if (hdr->sh_type == SHT_REL)
 	  size = bed->s->sizeof_rel;
@@ -2822,20 +2821,10 @@  bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
 	if (hdr->sh_entsize != size)
 	  goto fail;
 
-	/* Check for a bogus link to avoid crashing.  */
-	if (hdr->sh_link >= num_sec)
-	  {
-	    _bfd_error_handler
-	      /* xgettext:c-format */
-	      (_("%pB: invalid link %u for reloc section %s (index %u)"),
-	       abfd, hdr->sh_link, name, shindex);
-	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-	    goto success;
-	  }
-
 	/* Get the symbol table.  */
-	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
-	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
+	if (hdr->sh_link < num_sec
+	    && (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
+		|| elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
 	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
 	  goto fail;
 
@@ -2847,17 +2836,35 @@  bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
 	   also can't use it as a reloc section if it points to the
 	   null section, an invalid section, another reloc section, or
 	   its sh_link points to the null section.  */
+	bad_field = NULL;
+	bad_val = 0;
 	if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
 	     && (hdr->sh_flags & SHF_ALLOC) != 0)
 	    || (hdr->sh_flags & SHF_COMPRESSED) != 0
-	    || hdr->sh_type == SHT_RELR
-	    || hdr->sh_link == SHN_UNDEF
-	    || hdr->sh_link != elf_onesymtab (abfd)
-	    || hdr->sh_info == SHN_UNDEF
-	    || hdr->sh_info >= num_sec
-	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
-	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
+	    || hdr->sh_type == SHT_RELR)
+	  bad_field = "";
+	else if (hdr->sh_link == SHN_UNDEF
+		 || hdr->sh_link != elf_onesymtab (abfd))
+	  {
+	    bad_field = "sh_link";
+	    bad_val = hdr->sh_link;
+	  }
+	else if (hdr->sh_info == SHN_UNDEF
+		 || hdr->sh_info >= num_sec
+		 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
+		 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
+	  {
+	    bad_field = "sh_info";
+	    bad_val = hdr->sh_info;
+	  }
+
+	if (bad_field)
 	  {
+	    if (*bad_field)
+	      _bfd_error_handler
+		/* xgettext:c-format */
+		(_("%pB: not using relocs in %s (index %u) due to %s %u"),
+		 abfd, name, shindex, bad_field, bad_val);
 	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
 	    goto success;
 	  }
diff --git a/binutils/testsuite/binutils-all/strip-13.d b/binutils/testsuite/binutils-all/strip-13.d
index 48a3124493a..2c38c59a66a 100644
--- a/binutils/testsuite/binutils-all/strip-13.d
+++ b/binutils/testsuite/binutils-all/strip-13.d
@@ -2,6 +2,5 @@ 
 #strip: -g
 #error: \A[^\n]*: unsupported relocation type 0x[0-9a-f]+\Z
 #notarget: rx-*
-# The RX targets do not complain about unrecognised relocs, unless they
-#  are actually used
-#  (which is what should really happen with the other targets...)
+# The RX target text section is called "P" rather than ".text", so
+# creating .rela.text by hand is wrong.
diff --git a/binutils/testsuite/binutils-all/strip-14.d b/binutils/testsuite/binutils-all/strip-14.d
index 2c913c9d689..d14cce0d55a 100644
--- a/binutils/testsuite/binutils-all/strip-14.d
+++ b/binutils/testsuite/binutils-all/strip-14.d
@@ -2,6 +2,5 @@ 
 #strip: -g
 #error: \A[^\n]*: relocation 0 has invalid symbol index 1048560\Z
 #notarget: rx-*
-# The RX targets do not complain about bad relocs, unless they are
-#  actually used
-#  (which is what should really happen with the other targets...)
+# The RX target text section is called "P" rather than ".text", so
+# creating .rela.text by hand is wrong.
diff --git a/binutils/testsuite/binutils-all/strip-15.d b/binutils/testsuite/binutils-all/strip-15.d
index f649287cf59..79643baa342 100644
--- a/binutils/testsuite/binutils-all/strip-15.d
+++ b/binutils/testsuite/binutils-all/strip-15.d
@@ -1,6 +1,9 @@ 
 #PROG: strip
 #strip: -g
 #readelf: -r
+#notarget: rx-*
+# The RX target text section is called "P" rather than ".text", so
+# creating .rela.text by hand is wrong.
 
 Relocation section '\.rela?\.text' at offset .* contains 2 entries:
  *Offset * Info * Type * Sym\. *Value * Sym\. *Name(?: * \+ * Addend)?
diff --git a/binutils/testsuite/binutils-all/strip-section-headers-1.d b/binutils/testsuite/binutils-all/strip-section-headers-1.d
index dc249bba779..5f6ece5aea1 100644
--- a/binutils/testsuite/binutils-all/strip-section-headers-1.d
+++ b/binutils/testsuite/binutils-all/strip-section-headers-1.d
@@ -1,5 +1,8 @@ 
 #PROG: strip
 #strip: -g --strip-section-headers
 #readelf: -S
+#notarget: rx-*
+# The RX target text section is called "P" rather than ".text", so
+# creating .rela.text by hand is wrong.
 
 There are no sections in this file.