_bfd_elf_get_link_hash_entry tidy

Alan Modra amodra@gmail.com
Tue Oct 21 23:46:29 GMT 2025


Replace the "Elf_Internal_Shdr *symtab_hdr" parameter with
"unsigned int ext_sym_start", making it a duplicate of the existing
get_link_hash_entry function.

Also remove unnecessary checks from get_ext_sym_hash_from_cookie and
find_merged_cie.  The sym_hashes and symbol index checks in
get_ext_sym_hash_from_cookie are duplicates of those done in
_bfd_elf_get_link_hash_entry, and there is no need to check for a
global symbol before calling _bfd_elf_get_link_hash_entry.  When
bad_symtab, local symbols will have a NULL sym_hashes entry.  Removing
these unnecessary checks gets rid of some cookie->locsyms references.

	PR 33530
	* elf-bfd.h (_bfd_elf_get_link_hash_entry): Update declaration.
	* elflink.c (_bfd_elf_get_link_hash_entry): Rename from
	get_link_hash_entry, adjusting all calls and deleting original
	function.
	(get_ext_sym_hash_from_cookie): Make "symndx" unsigned int.
	Remove unnecessary check on sym_hashes, symbol index and
	symbol binding.
	* elf-eh-frame.c (find_merged_cie): Remove similar unnecessary
	checks.
	* elf64-x86-64.c (elf_x86_64_scan_relocs): Adjust.
	* elfxx-x86.c (_bfd_x86_elf_check_relocs): Adjust.
	(_bfd_x86_elf_link_relax_section): Adjust.

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 159a2401305..114a22f0fe0 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -3177,7 +3177,7 @@ extern void _bfd_elf_link_munmap_section_contents
   (asection *);
 
 extern struct elf_link_hash_entry * _bfd_elf_get_link_hash_entry
-  (struct elf_link_hash_entry **, unsigned int, Elf_Internal_Shdr *);
+  (struct elf_link_hash_entry **, unsigned int, unsigned int);
 
 /* Large common section.  */
 extern asection _bfd_elf_large_com_section;
diff --git a/bfd/elf-eh-frame.c b/bfd/elf-eh-frame.c
index 62cba84f458..2ef52e081e2 100644
--- a/bfd/elf-eh-frame.c
+++ b/bfd/elf-eh-frame.c
@@ -1238,6 +1238,7 @@ find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec,
 
   if (cie->per_encoding != DW_EH_PE_omit)
     {
+      struct elf_link_hash_entry *h;
       bool per_binds_local;
 
       /* Work out the address of personality routine, or at least
@@ -1254,14 +1255,13 @@ find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec,
       else
 #endif
 	r_symndx = ELF32_R_SYM (rel->r_info);
-      if (r_symndx >= cookie->locsymcount
-	  || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
-	{
-	  struct elf_link_hash_entry *h;
 
-	  r_symndx -= cookie->extsymoff;
-	  h = cookie->sym_hashes[r_symndx];
+      h = NULL;
+      if (r_symndx >= cookie->extsymoff)
+	h = elf_sym_hashes (cookie->abfd)[r_symndx - cookie->extsymoff];
 
+      if (h != NULL)
+	{
 	  while (h->root.type == bfd_link_hash_indirect
 		 || h->root.type == bfd_link_hash_warning)
 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 59b43149897..f2a172662a5 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -2615,7 +2615,8 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
       else
 	{
 	  isym = NULL;
-	  h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx, symtab_hdr);
+	  h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
+					    symtab_hdr->sh_info);
 	}
 
       /* Check invalid x32 relocations.  */
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 27a48431ecc..fe62e1c0505 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -93,10 +93,10 @@ _bfd_elf_link_keep_memory (struct bfd_link_info *info)
   return true;
 }
 
-static struct elf_link_hash_entry *
-get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
-		     unsigned int                   symndx,
-		     unsigned int                   ext_sym_start)
+struct elf_link_hash_entry *
+_bfd_elf_get_link_hash_entry (struct elf_link_hash_entry **sym_hashes,
+			      unsigned int symndx,
+			      unsigned int ext_sym_start)
 {
   if (sym_hashes == NULL
       /* Guard against corrupt input.  See PR 32636 for an example.  */
@@ -105,7 +105,7 @@ get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
 
   struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start];
 
-  /* The hash might be empty.  See PR 32641 for an example of this.  */
+  /* The hash might be empty when bad_symtab.  Also see PR32641.  */
   if (h == NULL)
     return NULL;
 
@@ -116,28 +116,15 @@ get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
   return h;
 }
 
-struct elf_link_hash_entry *
-_bfd_elf_get_link_hash_entry (struct elf_link_hash_entry **  sym_hashes,
-			      unsigned int                   symndx,
-			      Elf_Internal_Shdr *            symtab_hdr)
-{
-  if (symtab_hdr == NULL)
-    return NULL;
-
-  return get_link_hash_entry (sym_hashes, symndx, symtab_hdr->sh_info);
-}
-
 static struct elf_link_hash_entry *
-get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie, unsigned long r_symndx)
+get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie,
+			      unsigned int symndx)
 {
-  if (cookie == NULL || cookie->sym_hashes == NULL)
+  if (cookie == NULL)
     return NULL;
   
-  if (r_symndx >= cookie->locsymcount
-      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
-    return get_link_hash_entry (cookie->sym_hashes, r_symndx, cookie->extsymoff);
-
-  return NULL;
+  return _bfd_elf_get_link_hash_entry (cookie->sym_hashes, symndx,
+				       cookie->extsymoff);
 }
 
 asection *
@@ -9178,7 +9165,8 @@ set_symbol_value (bfd *bfd_with_globals,
 
   /* It is a global symbol: set its link type
      to "defined" and give it a value.  */
-  h = get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx, extsymoff);
+  h = _bfd_elf_get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx,
+				    extsymoff);
   if (h == NULL)
     {
       /* FIXMEL What should we do ?  */
@@ -11664,7 +11652,8 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
 	    {
 	      struct elf_link_hash_entry *h;
 
-	      h = get_link_hash_entry (sym_hashes, symndx, extsymoff);
+	      h = _bfd_elf_get_link_hash_entry (sym_hashes, symndx,
+						extsymoff);
 	      if (h == NULL)
 		{
 		  _bfd_error_handler
@@ -11809,7 +11798,8 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
 		  || (elf_bad_symtab (input_bfd)
 		      && flinfo->sections[r_symndx] == NULL))
 		{
-		  h = get_link_hash_entry (sym_hashes, r_symndx, extsymoff);
+		  h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
+						    extsymoff);
 
 		  /* Badly formatted input files can contain relocs that
 		     reference non-existant symbols.  Check here so that
@@ -12050,8 +12040,8 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
 			 reloc to point to the global hash table entry
 			 for this symbol.  The symbol index is then
 			 set at the end of bfd_elf_final_link.  */
-		      rh = get_link_hash_entry (elf_sym_hashes (input_bfd),
-						r_symndx, extsymoff);
+		      rh = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
+							 extsymoff);
 		      if (rh == NULL)
 			{
 			  /* FIXME: Generate an error ?  */
diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index b224a546901..cffec0f5aab 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -966,7 +966,8 @@ _bfd_x86_elf_check_relocs (bfd *abfd,
 	  goto error_return;
 	}
 
-      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx, symtab_hdr);
+      h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
+					symtab_hdr->sh_info);
 
       if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64, r_type)
 	  && NEED_DYNAMIC_RELOCATION_P (is_x86_64, info, true, h, sec,
@@ -1194,7 +1195,8 @@ _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
       else
 	{
 	  /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below.  */
-	  h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx, symtab_hdr);
+	  h = _bfd_elf_get_link_hash_entry (sym_hashes, r_symndx,
+					    symtab_hdr->sh_info);
 	  if (h == NULL)
 	    {
 	      /* FIXMEL: Issue an error message ?  */

-- 
Alan Modra


More information about the Binutils mailing list