hppa64 root.u.def assertions

Alan Modra amodra@gmail.com
Wed Jan 21 08:01:11 GMT 2026


The aim here is to add assertions when root.u.def is accessed that
the field is valid, ie. we have a bfd_link_hash_defined or
bfd_link_hash_defweak symbol.  On doing so I found the assertion
triggering on one of the ld undefweak tests, fixed with the
elf64_hppa_finish_dynamic_symbol change.

The patch also refactors code using text_hash_entry and
data_hash_entry.

OK?

	* elf64-hppa.c (elf64_hppa_finish_dynamic_symbol): Use zero
	value for both undefined and undefweak symbols, and when
	non-pic too.  Assert when accessing root.u.def that the symbol
	is the correct type.
	(elf64_hppa_finalize_opd, elf64_hppa_finalize_dlt),
	(elf64_hppa_finalize_dynreloc): Similarly assert here.
	(elf64_hppa_finalize_dlt): Move duplicate code involving
	text_hash_entry and data_hash_entry out of conditionals.
	(elf64_hppa_finalize_dynreloc, elf_hppa_dlt_dynrel_reloc),
	(elf_hppa_final_link_relocate): Likewise.

diff --git a/bfd/elf64-hppa.c b/bfd/elf64-hppa.c
index c35dcf02976..66a01346a9a 100644
--- a/bfd/elf64-hppa.c
+++ b/bfd/elf64-hppa.c
@@ -2125,10 +2125,16 @@ elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
 	 if we are creating a shared library and the symbol is
 	 still undefined, we create a dynamic relocation to fill
 	 in the correct value.  */
-      if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
+      if (eh->root.type == bfd_link_hash_undefined
+	  || eh->root.type == bfd_link_hash_undefweak)
 	value = 0;
       else
-	value = (eh->root.u.def.value + eh->root.u.def.section->vma);
+	{
+	  BFD_ASSERT (eh->root.type == bfd_link_hash_defined
+		      || eh->root.type == bfd_link_hash_defweak);
+
+	  value = eh->root.u.def.value + eh->root.u.def.section->vma;
+	}
 
       /* Fill in the entry in the procedure linkage table.
 
@@ -2260,6 +2266,9 @@ elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
     {
       bfd_vma value;
 
+      BFD_ASSERT (eh->root.type == bfd_link_hash_defined
+		  || eh->root.type == bfd_link_hash_defweak);
+
       value = (eh->root.u.def.value
 	       + eh->root.u.def.section->output_section->vma
 	       + eh->root.u.def.section->output_offset);
@@ -2390,27 +2399,27 @@ elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
       Elf_Internal_Rela rel;
       bfd_byte *loc;
       long dynindx;
+      struct elf_link_hash_entry *baseh;
       asection *sec;
       bfd_vma value, value2;
 
       if (eh->dynindx == -1)
 	{
+	  BFD_ASSERT (eh->root.type == bfd_link_hash_defined
+		      || eh->root.type == bfd_link_hash_defweak);
+
 	  value = (eh->root.u.def.value
 		   + eh->root.u.def.section->output_section->vma
 		   + eh->root.u.def.section->output_offset);
 
 	  if (eh->root.u.def.section->flags & SEC_READONLY)
-	    {
-	      sec = hppa_info->text_hash_entry->root.u.def.section;
-	      value2 = sec->output_offset + sec->output_section->vma;
-	      dynindx = hppa_info->text_hash_entry->dynindx;
-	    }
+	    baseh = hppa_info->text_hash_entry;
 	  else
-	    {
-	      sec = hppa_info->data_hash_entry->root.u.def.section;
-	      value2 = sec->output_offset + sec->output_section->vma;
-	      dynindx = hppa_info->data_hash_entry->dynindx;
-	    }
+	    baseh = hppa_info->data_hash_entry;
+
+	  sec = baseh->root.u.def.section;
+	  value2 = sec->output_offset + sec->output_section->vma;
+	  dynindx = baseh->dynindx;
 	  rel.r_addend = value - value2;
 	}
       else
@@ -2499,6 +2508,7 @@ elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
 
 	  if (eh->dynindx == -1)
 	    {
+	      struct elf_link_hash_entry *baseh;
 	      asection *sec, *sopd;
 	      bfd_vma value, value2;
 
@@ -2518,22 +2528,22 @@ elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
 		{
 		  if (discarded_section (eh->root.u.def.section))
 		    continue;
+
+		  BFD_ASSERT (eh->root.type == bfd_link_hash_defined
+			      || eh->root.type == bfd_link_hash_defweak);
+
 		  value = (eh->root.u.def.value
 			   + eh->root.u.def.section->output_section->vma
 			   + eh->root.u.def.section->output_offset);
 
 		  if (eh->root.u.def.section->flags & SEC_READONLY)
-		    {
-		      sec = hppa_info->text_hash_entry->root.u.def.section;
-		      value2 = sec->output_offset + sec->output_section->vma;
-		      dynindx = hppa_info->text_hash_entry->dynindx;
-		    }
+		    baseh = hppa_info->text_hash_entry;
 		  else
-		    {
-		      sec = hppa_info->data_hash_entry->root.u.def.section;
-		      value2 = sec->output_offset + sec->output_section->vma;
-		      dynindx = hppa_info->data_hash_entry->dynindx;
-		    }
+		    baseh = hppa_info->data_hash_entry;
+
+		  sec = baseh->root.u.def.section;
+		  value2 = sec->output_offset + sec->output_section->vma;
+		  dynindx = baseh->dynindx;
 		}
 	      rel.r_addend = value - value2;
 	    }
@@ -3300,6 +3310,7 @@ elf_hppa_dlt_dynrel_reloc (Elf_Internal_Rela *rel,
   Elf_Internal_Rela rela;
   bfd_byte *loc;
   long dynindx;
+  struct elf_link_hash_entry *baseh;
   asection *sec;
   asection *sdlt, *sdltrel;
   bfd_signed_vma addend = rel->r_addend;
@@ -3313,15 +3324,12 @@ elf_hppa_dlt_dynrel_reloc (Elf_Internal_Rela *rel,
   rela.r_offset = dlt_offset + sdlt->output_offset + sdlt->output_section->vma;
 
   if (sym_sec->flags & SEC_READONLY)
-    {
-      sec = hppa_info->text_hash_entry->root.u.def.section;
-      dynindx = hppa_info->text_hash_entry->dynindx;
-    }
+    baseh = hppa_info->text_hash_entry;
   else
-    {
-      sec = hppa_info->data_hash_entry->root.u.def.section;
-      dynindx = hppa_info->data_hash_entry->dynindx;
-    }
+    baseh = hppa_info->data_hash_entry;
+
+  sec = baseh->root.u.def.section;
+  dynindx = baseh->dynindx;
 
   /* Adjust addend using the difference of the symbol's
      location and the section symbol's address.  */
@@ -4032,6 +4040,7 @@ elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
 						     r_symndx) != -1)
 	    {
 	      bfd_vma out_off;
+	      struct elf_link_hash_entry *baseh;
 
 	      out_off = _bfd_elf_section_offset (output_bfd, info,
 						 input_section,
@@ -4046,15 +4055,12 @@ elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
 
 	      /* Select base segment.  */
 	      if (sym_sec->flags & SEC_READONLY)
-		{
-		  sec = hppa_info->text_hash_entry->root.u.def.section;
-		  dynindx = hppa_info->text_hash_entry->dynindx;
-		}
+		baseh = hppa_info->text_hash_entry;
 	      else
-		{
-		  sec = hppa_info->data_hash_entry->root.u.def.section;
-		  dynindx = hppa_info->data_hash_entry->dynindx;
-		}
+		baseh = hppa_info->data_hash_entry;
+
+	      sec = baseh->root.u.def.section;
+	      dynindx = baseh->dynindx;
 
 	      /* Adjust addend using the difference of the symbol's
 		 location and the section symbol's address.  */

-- 
Alan Modra


More information about the Binutils mailing list