[4/4] elf: Return error on unsorted symbol table if not allowed

Message ID CAMe9rOoaMiDm7xdNaNP3wENdQRfiU0_t55vEU6mH1hKjh3fu-A@mail.gmail.com
State New
Headers
Series [1/4] elf: Disallow the empty symbol name |

Commit Message

H.J. Lu Sept. 23, 2025, 2:24 a.m. UTC
  Normally ELF symbol table should be sorted, i.e., local symbols precede
global symbols.  Irix 6 is an exception and its elf_bad_symtab is set
to true.  Issue an error if elf_bad_symtab is false and symbol table is
unsorted.

PR ld/33450
* elflink.c (set_symbol_value): Change return type to bool and
return false on error.  Issue an error on unsorted symbol table
if not allowed.
(elf_link_input_bfd): Return false if set_symbol_value reurns
false.
  

Comments

Alan Modra Sept. 23, 2025, 12:02 p.m. UTC | #1
On Tue, Sep 23, 2025 at 10:24:53AM +0800, H.J. Lu wrote:
> Normally ELF symbol table should be sorted, i.e., local symbols precede
> global symbols.  Irix 6 is an exception and its elf_bad_symtab is set
> to true.  Issue an error if elf_bad_symtab is false and symbol table is
> unsorted.
> 
> PR ld/33450
> * elflink.c (set_symbol_value): Change return type to bool and
> return false on error.  Issue an error on unsorted symbol table
> if not allowed.
> (elf_link_input_bfd): Return false if set_symbol_value reurns
> false.

OK.
  

Patch

From 18f366556c2328ba62f70a03a227c4b25aad2835 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Tue, 23 Sep 2025 08:52:26 +0800
Subject: [PATCH 4/4] elf: Return error on unsorted symbol table if not allowed

Normally ELF symbol table should be sorted, i.e., local symbols precede
global symbols.  Irix 6 is an exception and its elf_bad_symtab is set
to true.  Issue an error if elf_bad_symtab is false and symbol table is
unsorted.

	PR ld/33450
	* elflink.c (set_symbol_value): Change return type to bool and
	return false on error.  Issue an error on unsorted symbol table
	if not allowed.
	(elf_link_input_bfd): Return false if set_symbol_value reurns
	false.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elflink.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index f366e8f569e..ec869624cf8 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -9155,7 +9155,7 @@  struct elf_outext_info
    <binary-operator> := as in C
    <unary-operator> := as in C, plus "0-" for unambiguous negation.  */
 
-static void
+static bool
 set_symbol_value (bfd *bfd_with_globals,
 		  Elf_Internal_Sym *isymbuf,
 		  size_t locsymcount,
@@ -9176,9 +9176,15 @@  set_symbol_value (bfd *bfd_with_globals,
 	     "absolute" section and give it a value.  */
 	  sym->st_shndx = SHN_ABS;
 	  sym->st_value = val;
-	  return;
+	  return true;
+	}
+      if (!elf_bad_symtab (bfd_with_globals))
+	{
+	  _bfd_error_handler (_("%pB: corrupt symbol table"),
+			      bfd_with_globals);
+	  bfd_set_error (bfd_error_bad_value);
+	  return false;
 	}
-      BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
       extsymoff = 0;
     }
 
@@ -9188,11 +9194,12 @@  set_symbol_value (bfd *bfd_with_globals,
   if (h == NULL)
     {
       /* FIXMEL What should we do ?  */
-      return;
+      return false;
     }
   h->root.type = bfd_link_hash_defined;
   h->root.u.def.value = val;
   h->root.u.def.section = bfd_abs_section_ptr;
+  return true;
 }
 
 static bool
@@ -11890,8 +11897,10 @@  elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
 		    return false;
 
 		  /* Symbol evaluated OK.  Update to absolute value.  */
-		  set_symbol_value (input_bfd, isymbuf, locsymcount,
-				    r_symndx, val);
+		  if (!set_symbol_value (input_bfd, isymbuf, locsymcount, r_symndx,
+					 val))
+		    return false;
+
 		  continue;
 		}
 
-- 
2.51.0