Skip to contentSkip to conversation

OpenRISC BFD fixups for Glibc

PATCH New Stafford Horne 2 participants 12 messages May 14, 2020 – May 20, 2020

lore
Share & tools
lore
#0 lore raw

cover OpenRISC BFD fixups for Glibc

Hello,

I am currently working on the glibc port for openrisc and during testing the glibc testsuite we uncovered several issues with the OpenRISC bfd implementation relating to TLS relocations. This series fixes them all.

-Stafford

Stafford Horne (8):
or1k: Fix static linking when with .rela.got relocations
or1k: Fix dynamic TLS symbol flag
or1k: Add TLS mask to handle multiple model access
or1k: Fix issue with multiple PCREL relocations
or1k: TLS offset to use tcb size and section alignment
or1k: refactor: Rename p to sec_relocs
or1k: refactor: Rename s to sgot and splt
or1k: Add dynamic flag to tpoff

 bfd/elf32-or1k.c | 368 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 236 insertions(+), 132 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/8 or1k: Fix static linking when with .rela.got relocations

When testing glibc static cases were failing due to the assert of srelgot failing. Using the reference via htab->root.srelgot works fine as it is initialized. Also, this contains a few other fixups made while figuring out the issue.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* elf32-or1k.c (or1k_elf_finish_dynamic_symbol): Rename srela to relgot.
(or1k_elf_relocate_section): Access srelgot via htab->root.srelgot. Add assertions for srelgot->contents. Introduce local variable for srelgot to not reuse global sreloc.

---
 bfd/elf32-or1k.c | 43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)
bfd/elf32-or1k.c +23 -20
index 41e61ac7e2..221a14c5b1 100644
@@ -1226,7 +1226,6 @@ or1k_elf_relocate_section (bfd *output_bfd,
   Elf_Internal_Rela *rel;
   Elf_Internal_Rela *relend;
   struct elf_or1k_link_hash_table *htab = or1k_elf_hash_table (info);
-  bfd *dynobj;
   asection *sreloc;
   bfd_vma *local_got_offsets;
   asection *sgot, *splt;
@@ -1236,7 +1235,6 @@ or1k_elf_relocate_section (bfd *output_bfd,
   if (htab == NULL)
     return FALSE;
 
-  dynobj = htab->root.dynobj;
   local_got_offsets = elf_local_got_offsets (input_bfd);
 
   sreloc = elf_section_data (input_section)->sreloc;
@@ -1254,7 +1252,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
       got_sym_value = (hgot->root.u.def.value
 		       + hgot->root.u.def.section->output_section->vma
 		       + hgot->root.u.def.section->output_offset);
-    got_base = sgot->output_section->vma + sgot->output_offset;
+      got_base = sgot->output_section->vma + sgot->output_offset;
     }
 
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
@@ -1416,16 +1414,16 @@ or1k_elf_relocate_section (bfd *output_bfd,
 
 		      /* We need to generate a R_OR1K_RELATIVE reloc
 			 for the dynamic linker.  */
-			srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
+		      srelgot = htab->root.srelgot;
 		      BFD_ASSERT (srelgot != NULL);
 
 		      outrel.r_offset = got_base + off;
 		      outrel.r_info = ELF32_R_INFO (0, R_OR1K_RELATIVE);
 		      outrel.r_addend = relocation;
 		      loc = srelgot->contents;
-			loc += (srelgot->reloc_count
-				* sizeof (Elf32_External_Rela));
-		      bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
+		      loc += (srelgot->reloc_count
+			      * sizeof (Elf32_External_Rela));
+		      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
 		      ++srelgot->reloc_count;
 		    }
 		  local_got_offsets[r_symndx] |= 1;
@@ -1591,10 +1589,11 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	  {
 	    bfd_vma gotoff;
 	    Elf_Internal_Rela rela;
+	    asection *srelgot;
 	    bfd_byte *loc;
 	    int dynamic;
 
-	    sreloc = bfd_get_section_by_name (dynobj, ".rela.got");
+	    srelgot = htab->root.srelgot;
 
 	    /* Mark as TLS related GOT entry by setting
 	       bit 2 as well as bit 1.  */
@@ -1619,7 +1618,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    BFD_ASSERT (elf_hash_table (info)->hgot == NULL
 			|| elf_hash_table (info)->hgot->root.u.def.value == 0);
 
-	    /* Dynamic entries will require relocations. if we do not need
+	    /* Dynamic entries will require relocations.  If we do not need
 	       them we will just use the default R_OR1K_NONE and
 	       not set anything.  */
 	    dynamic = bfd_link_pic (info)
@@ -1639,6 +1638,8 @@ or1k_elf_relocate_section (bfd *output_bfd,
 		/* Add DTPMOD and DTPOFF GOT and rela entries.  */
 		for (i = 0; i < 2; ++i)
 		  {
+		    BFD_ASSERT (srelgot->contents != NULL);
+
 		    rela.r_offset = got_base + gotoff + i*4;
 		    if (h != NULL && h->dynindx != -1)
 		      {
@@ -1653,9 +1654,9 @@ or1k_elf_relocate_section (bfd *output_bfd,
 			rela.r_addend = tpoff (info, relocation);
 		      }
 
-		    loc = sreloc->contents;
-		    loc += sreloc->reloc_count++ *
-		      sizeof (Elf32_External_Rela);
+		    loc = srelgot->contents;
+		    loc += (srelgot->reloc_count++
+			    * sizeof (Elf32_External_Rela));
 
 		    bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
 		    bfd_put_32 (output_bfd, 0, sgot->contents + gotoff + i*4);
@@ -1674,6 +1675,8 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    /* Shared IE.  */
 	    else if (dynamic)
 	      {
+		BFD_ASSERT (srelgot->contents != NULL);
+
 		/* Add TPOFF GOT and rela entries.  */
 		rela.r_offset = got_base + gotoff;
 		if (h != NULL && h->dynindx != -1)
@@ -1687,8 +1690,8 @@ or1k_elf_relocate_section (bfd *output_bfd,
 		    rela.r_addend = tpoff (info, relocation);
 		  }
 
-		loc = sreloc->contents;
-		loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
+		loc = srelgot->contents;
+		loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
 
 		bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
 		bfd_put_32 (output_bfd, 0, sgot->contents + gotoff);
@@ -2402,14 +2405,14 @@ or1k_elf_finish_dynamic_symbol (bfd *output_bfd,
       && (h->got.offset & 2) == 0) /* Homemade TLS check.  */
     {
       asection *sgot;
-      asection *srela;
+      asection *srelgot;
       Elf_Internal_Rela rela;
 
       /* This symbol has an entry in the global offset table.  Set it
 	 up.  */
       sgot = htab->root.sgot;
-      srela = htab->root.srelgot;
-      BFD_ASSERT (sgot != NULL && srela != NULL);
+      srelgot = htab->root.srelgot;
+      BFD_ASSERT (sgot != NULL && srelgot != NULL);
 
       rela.r_offset = (sgot->output_section->vma
 		       + sgot->output_offset
@@ -2435,10 +2438,10 @@ or1k_elf_finish_dynamic_symbol (bfd *output_bfd,
 	  rela.r_addend = 0;
 	}
 
-      loc = srela->contents;
-      loc += srela->reloc_count * sizeof (Elf32_External_Rela);
+      loc = srelgot->contents;
+      loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
-      ++srela->reloc_count;
+      ++srelgot->reloc_count;
     }
 
   if (h->needs_copy)
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/8 or1k: Fix dynamic TLS symbol flag

In reply to Stafford Horne

The dynamic flag used for TLS relocations was not properly being set for some cases causing link failure. The fix here was mostly copied from other BFD implementations.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* bfd/elf32-or1k.c (or1k_elf_relocate_section): Fixup dynamic symbol detection.

---
 bfd/elf32-or1k.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
bfd/elf32-or1k.c +15 -4
index 221a14c5b1..99a36c7f04 100644
@@ -1592,6 +1592,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    asection *srelgot;
 	    bfd_byte *loc;
 	    int dynamic;
+	    int indx = 0;
 
 	    srelgot = htab->root.srelgot;
 
@@ -1618,13 +1619,23 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    BFD_ASSERT (elf_hash_table (info)->hgot == NULL
 			|| elf_hash_table (info)->hgot->root.u.def.value == 0);
 
+	    if (h != NULL)
+	      {
+		bfd_boolean dyn = htab->root.dynamic_sections_created;
+		bfd_boolean pic = bfd_link_pic (info);
+
+		if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
+		    && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
+		  indx = h->dynindx;
+	      }
+
 	    /* Dynamic entries will require relocations.  If we do not need
 	       them we will just use the default R_OR1K_NONE and
 	       not set anything.  */
-	    dynamic = bfd_link_pic (info)
-	      || (sec && (sec->flags & SEC_ALLOC) != 0
-		  && h != NULL
-		  && (h->root.type == bfd_link_hash_defweak || !h->def_regular));
+	    dynamic = (bfd_link_pic (info) || indx != 0)
+		       && (h == NULL
+			   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+			   || h->root.type != bfd_link_hash_undefweak);
 
 	    /* Shared GD.  */
 	    if (dynamic
Signature · 1 line
2.26.2
#3 lore raw

PATCH 3/8 or1k: Add TLS mask to handle multiple model access

In reply to Stafford Horne

In cases where a variable is accessed both via GD and IE the GOT was not getting setup correctly and causing failures. This was noticed running the glibc testsuite.

This patch uses tls_type as a bitmask to allow for handling multiple access types to TLS symbols.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* bfd/elf32-or1k.c (or1k_set_got_and_rela_sizes): New function. (or1k_initial_exec_offset): New function.
(TLS_GD, TLS_IE, TLS_LD, TLS_LE): Redefine macros as masks. (or1k_elf_relocate_section): Allow for TLS to handle multiple model access.
(or1k_elf_check_relocs): Use OR to set TLS access. (allocate_dynrelocs): Use or1k_set_got_and_rela_sizes to set sizes.
(or1k_elf_size_dynamic_sections): Use or1k_set_got_and_rela_sizes to set sizes.

---
 bfd/elf32-or1k.c | 149 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 103 insertions(+), 46 deletions(-)
bfd/elf32-or1k.c +103 -46
index 99a36c7f04..acfdb5bef2 100644
@@ -873,12 +873,16 @@ static const struct or1k_reloc_map or1k_reloc_map[] =
   { BFD_RELOC_OR1K_PLTA26,	R_OR1K_PLTA26 },
 };
 
+/* tls_type is a mask used to track how each symbol is accessed,
+   it may be accessed via multiple types of TLS access methods.
+   We track this for sizing (allocating got + relocation section space) and
+   for how to process relocations.  */
 #define TLS_UNKNOWN    0
 #define TLS_NONE       1
 #define TLS_GD	       2
-#define TLS_LD	       3
-#define TLS_IE	       4
-#define TLS_LE	       5
+#define TLS_LD	       4
+#define TLS_IE	       8
+#define TLS_LE	      16
 
 /* ELF linker hash entry.  */
 struct elf_or1k_link_hash_entry
@@ -1058,6 +1062,24 @@ tpoff (struct bfd_link_info *info, bfd_vma address)
   return (address - elf_hash_table (info)->tls_sec->vma);
 }
 
+/* If we have both IE and GD accesses to a symbol the IE relocations should be
+   offset by 8 bytes because the got contains both GD and IE entries.  */
+static bfd_vma
+or1k_initial_exec_offset (reloc_howto_type *howto, unsigned char tls_type_mask)
+{
+   switch (howto->type)
+     {
+     case R_OR1K_TLS_IE_HI16:
+     case R_OR1K_TLS_IE_LO16:
+     case R_OR1K_TLS_IE_PG21:
+     case R_OR1K_TLS_IE_LO13:
+     case R_OR1K_TLS_IE_AHI16:
+       return (tls_type_mask & TLS_GD) != 0 ? 8 : 0;
+     default:
+       return 0;
+     }
+}
+
 /* Like _bfd_final_link_relocate, but handles non-contiguous fields.  */
 
 static bfd_reloc_status_type
@@ -1593,19 +1615,26 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    bfd_byte *loc;
 	    int dynamic;
 	    int indx = 0;
+	    unsigned char tls_type;
 
 	    srelgot = htab->root.srelgot;
 
 	    /* Mark as TLS related GOT entry by setting
-	       bit 2 as well as bit 1.  */
+	       bit 2 to indcate TLS and bit 1 to indicate GOT.  */
 	    if (h != NULL)
 	      {
 		gotoff = h->got.offset;
+		tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
 		h->got.offset |= 3;
 	      }
 	    else
 	      {
+		unsigned char *local_tls_type;
+
 		gotoff = local_got_offsets[r_symndx];
+		local_tls_type = (unsigned char *) elf_or1k_local_tls_type (input_bfd);
+		tls_type = local_tls_type == NULL ? TLS_NONE
+						  : local_tls_type[r_symndx];
 		local_got_offsets[r_symndx] |= 3;
 	      }
 
@@ -1638,11 +1667,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 			   || h->root.type != bfd_link_hash_undefweak);
 
 	    /* Shared GD.  */
-	    if (dynamic
-		&& (howto->type == R_OR1K_TLS_GD_HI16
-		    || howto->type == R_OR1K_TLS_GD_LO16
-		    || howto->type == R_OR1K_TLS_GD_PG21
-		    || howto->type == R_OR1K_TLS_GD_LO13))
+	    if (dynamic && ((tls_type & TLS_GD) != 0))
 	      {
 		int i;
 
@@ -1674,17 +1699,17 @@ or1k_elf_relocate_section (bfd *output_bfd,
 		  }
 	      }
 	    /* Static GD.  */
-	    else if (howto->type == R_OR1K_TLS_GD_HI16
-		     || howto->type == R_OR1K_TLS_GD_LO16
-		     || howto->type == R_OR1K_TLS_GD_PG21
-		     || howto->type == R_OR1K_TLS_GD_LO13)
+	    else if ((tls_type & TLS_GD) != 0)
 	      {
 		bfd_put_32 (output_bfd, 1, sgot->contents + gotoff);
 		bfd_put_32 (output_bfd, tpoff (info, relocation),
 		    sgot->contents + gotoff + 4);
 	      }
+
+	    gotoff += or1k_initial_exec_offset (howto, tls_type);
+
 	    /* Shared IE.  */
-	    else if (dynamic)
+	    if (dynamic && ((tls_type & TLS_IE) != 0))
 	      {
 		BFD_ASSERT (srelgot->contents != NULL);
 
@@ -1708,11 +1733,9 @@ or1k_elf_relocate_section (bfd *output_bfd,
 		bfd_put_32 (output_bfd, 0, sgot->contents + gotoff);
 	      }
 	    /* Static IE.  */
-	    else
-	      {
-		bfd_put_32 (output_bfd, tpoff (info, relocation),
-			    sgot->contents + gotoff);
-	      }
+	    else if ((tls_type & TLS_IE) != 0)
+	      bfd_put_32 (output_bfd, tpoff (info, relocation),
+			  sgot->contents + gotoff);
 
 	    /* The PG21 and LO13 relocs are pc-relative, while the
 	       rest are GOT relative.  */
@@ -1909,7 +1932,7 @@ or1k_elf_check_relocs (bfd *abfd,
 
       /* Record TLS type.  */
       if (h != NULL)
-	  ((struct elf_or1k_link_hash_entry *) h)->tls_type = tls_type;
+	  ((struct elf_or1k_link_hash_entry *) h)->tls_type |= tls_type;
       else
 	{
 	  unsigned char *local_tls_type;
@@ -1926,7 +1949,7 @@ or1k_elf_check_relocs (bfd *abfd,
 		return FALSE;
 	      elf_or1k_local_tls_type (abfd) = local_tls_type;
 	    }
-	  local_tls_type[r_symndx] = tls_type;
+	  local_tls_type[r_symndx] |= tls_type;
 	}
 
       switch (r_type)
@@ -2648,6 +2671,56 @@ or1k_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   return _bfd_elf_adjust_dynamic_copy (info, h, s);
 }
 
+/* Caclulate an update the sizes required for a symbol in the GOT and
+   RELA relocation section based on the TLS_TYPE and whether or not the symbol
+   is DYNAMIC.
+
+   Symbols with TLS_GD access require 8 bytes in the GOT and, if dynamic,
+   require two relocation entries.  Symbols with TLS_IE access require 4 bytes
+   in the GOT and, if dynamic, require one relocation entry.  Symbols may have
+   both TLS_GD and TLS_IE access to be accounted for.
+
+   Other symbols require 4 bytes in the GOT table and, if dynamic, require one
+   relocation entry.  */
+
+static void
+or1k_set_got_and_rela_sizes (const unsigned char tls_type,
+			     const bfd_boolean dynamic,
+			     bfd_vma *got_size,
+			     bfd_vma *rela_size)
+{
+  bfd_boolean is_tls_entry = FALSE;
+
+  /* TLS GD requires two GOT entries and two relocs.  */
+  if ((tls_type & TLS_GD) != 0)
+    {
+      *got_size += 8;
+      is_tls_entry = TRUE;
+    }
+
+  if ((tls_type & TLS_IE) != 0)
+    {
+      *got_size += 4;
+      is_tls_entry = TRUE;
+    }
+
+  if (is_tls_entry == FALSE)
+    *got_size += 4;
+
+  if (dynamic)
+    {
+      if ((tls_type & TLS_GD) != 0)
+	*rela_size += 2 * sizeof (Elf32_External_Rela);
+
+      if ((tls_type & TLS_IE) != 0)
+	*rela_size += sizeof (Elf32_External_Rela);
+
+      if (is_tls_entry == FALSE)
+	*rela_size += sizeof (Elf32_External_Rela);
+    }
+}
+
+
 /* Allocate space in .plt, .got and associated reloc sections for
    dynamic relocs.  */
 
@@ -2747,19 +2820,10 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 
       tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
 
-      /* TLS GD requires two GOT and two relocs.  */
-      if (tls_type == TLS_GD)
-	s->size += 8;
-      else
-	s->size += 4;
       dyn = htab->root.dynamic_sections_created;
-      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
-	{
-	  if (tls_type == TLS_GD)
-	    htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
-	  else
-	    htab->root.srelgot->size += sizeof (Elf32_External_Rela);
-	}
+      dyn = WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h);
+      or1k_set_got_and_rela_sizes (tls_type, dyn,
+				   &s->size, &htab->root.srelgot->size);
     }
   else
     h->got.offset = (bfd_vma) -1;
@@ -2964,20 +3028,13 @@ or1k_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 	{
 	  if (*local_got > 0)
 	    {
-	      *local_got = s->size;
+	      unsigned char tls_type = (local_tls_type == NULL)
+					? TLS_UNKNOWN
+					: *local_tls_type;
 
-	      /* TLS GD requires two GOT and two relocs.  */
-	      if (local_tls_type != NULL && *local_tls_type == TLS_GD)
-		s->size += 8;
-	      else
-		s->size += 4;
-	      if (bfd_link_pic (info))
-		{
-		  if (local_tls_type != NULL && *local_tls_type == TLS_GD)
-		    srel->size += 2 * sizeof (Elf32_External_Rela);
-		  else
-		    srel->size += sizeof (Elf32_External_Rela);
-		}
+	      *local_got = s->size;
+	      or1k_set_got_and_rela_sizes (tls_type, bfd_link_pic (info),
+					   &s->size, &srel->size);
 	    }
 	  else
 
Signature · 1 line
2.26.2
#4 lore raw

PATCH 4/8 or1k: Fix issue with multiple PCREL relocations

In reply to Stafford Horne

When a symbol has multiple PCREL accesses the first access relocation will be properly calculated but subsequent ones were not performing the PCREL calculation.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* bfd/elf32-or1k.c (or1k_elf_relocate_section): Fixup PCREL relocation calculation.

---
 bfd/elf32-or1k.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
bfd/elf32-or1k.c +11 -2
index acfdb5bef2..eee418c558 100644
@@ -1639,9 +1639,18 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	      }
 
 	    /* Only process the relocation once.  */
-	    if (gotoff & 1)
+	    if ((gotoff & 1) != 0)
 	      {
-		relocation = sgot->output_offset + (gotoff  & ~3);
+		gotoff += or1k_initial_exec_offset (howto, tls_type);
+
+		/* The PG21 and LO13 relocs are pc-relative, while the
+		   rest are GOT relative.  */
+		relocation = got_base + (gotoff & ~3);
+		if (!(r_type == R_OR1K_TLS_GD_PG21
+		    || r_type == R_OR1K_TLS_GD_LO13
+		    || r_type == R_OR1K_TLS_IE_PG21
+		    || r_type == R_OR1K_TLS_IE_LO13))
+		  relocation -= got_sym_value;
 		break;
 	      }
 
Signature · 1 line
2.26.2
#5 lore raw

PATCH 5/8 or1k: TLS offset to use tcb size and section alignment

In reply to Stafford Horne

The offset was wrong and causing issues when the TLS section alignment is larger than TCB_SIZE.

In or1k we have:

                 /- TP
                V

[ Pthread | TCB 0x0 0x0 0x0 | TDATA , TBSS ]

Here TDATA is not at TP, but TDATA is offset by the alignment. TP points to just after the TCB. With this patch I adjust for that TCB padding due to alignment which was not being properly accounted for before.

This allows glibc nptl and elf tests to pass.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* elf32-or1k.c (TCB_SIZE): New macro.
(tpoff): Use TCB_SIZE and alignment to calculate offset.

---
 bfd/elf32-or1k.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
bfd/elf32-or1k.c +13 -2
index eee418c558..ca1b61fe1c 100644
@@ -884,6 +884,9 @@ static const struct or1k_reloc_map or1k_reloc_map[] =
 #define TLS_IE	       8
 #define TLS_LE	      16
 
+/* The size of the TLS thread control block, used to offset LE access.  */
+#define TCB_SIZE      16
+
 /* ELF linker hash entry.  */
 struct elf_or1k_link_hash_entry
 {
@@ -1052,14 +1055,22 @@ or1k_info_to_howto_rela (bfd * abfd,
 static bfd_vma
 tpoff (struct bfd_link_info *info, bfd_vma address)
 {
+  struct elf_link_hash_table *htab = elf_hash_table (info);
+  bfd_vma base;
+
   /* If tls_sec is NULL, we should have signalled an error already.  */
-  if (elf_hash_table (info)->tls_sec == NULL)
+  if (htab->tls_sec == NULL)
     return 0;
 
+  /* On or1k, the tp points to just after the tcb, if we have an alignment
+     greater than the tcb size we need to offset by the alignment difference.  */
+  base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power)
+	 - TCB_SIZE;
+
   /* The thread pointer on or1k stores the address after the TCB where
      the data is, just compute the difference. No need to compensate
      for the size of TCB.  */
-  return (address - elf_hash_table (info)->tls_sec->vma);
+  return address - htab->tls_sec->vma + base;
 }
 
 /* If we have both IE and GD accesses to a symbol the IE relocations should be
Signature · 1 line
2.26.2
#6 lore raw

PATCH 6/8 or1k: refactor: Rename p to sec_relocs

In reply to Stafford Horne

The symbol name p was not very meaningful and causing me confusion while debugging this code. Change to a more meaninful name.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* elf32-or1k.c (allocate_dynrelocs, readonly_dynrelocs, or1k_elf_check_relocs, or1k_elf_size_dynamic_sections): Rename p to sec_relocs.

---
 bfd/elf32-or1k.c | 86 ++++++++++++++++++++++++++----------------------
 1 file changed, 47 insertions(+), 39 deletions(-)
bfd/elf32-or1k.c +47 -39
index ca1b61fe1c..22c316d042 100644
@@ -2104,7 +2104,7 @@ or1k_elf_check_relocs (bfd *abfd,
 		    && (h->root.type == bfd_link_hash_defweak
 			|| !h->def_regular)))
 	      {
-		struct elf_dyn_relocs *p;
+		struct elf_dyn_relocs *sec_relocs;
 		struct elf_dyn_relocs **head;
 
 		/* When creating a shared object, we must copy these
@@ -2172,24 +2172,26 @@ or1k_elf_check_relocs (bfd *abfd,
 		    head = (struct elf_dyn_relocs **) vpp;
 		  }
 
-		p = *head;
-		if (p == NULL || p->sec != sec)
+		sec_relocs = *head;
+		/* Allocate this sections dynamic reolcations structure if this
+		   is a new section.  */
+		if (sec_relocs == NULL || sec_relocs->sec != sec)
 		  {
-		    size_t amt = sizeof *p;
-		    p = ((struct elf_dyn_relocs *)
-			 bfd_alloc (htab->root.dynobj, amt));
-		    if (p == NULL)
+		    size_t amt = sizeof *sec_relocs;
+		    sec_relocs = ((struct elf_dyn_relocs *)
+				  bfd_alloc (htab->root.dynobj, amt));
+		    if (sec_relocs == NULL)
 		      return FALSE;
-		    p->next = *head;
-		    *head = p;
-		    p->sec = sec;
-		    p->count = 0;
-		    p->pc_count = 0;
+		    sec_relocs->next = *head;
+		    *head = sec_relocs;
+		    sec_relocs->sec = sec;
+		    sec_relocs->count = 0;
+		    sec_relocs->pc_count = 0;
 		  }
 
-		p->count += 1;
+		sec_relocs->count += 1;
 		if (r_type == R_OR1K_INSN_REL_26)
-		  p->pc_count += 1;
+		  sec_relocs->pc_count += 1;
 	      }
 	  }
 	  break;
@@ -2549,15 +2551,17 @@ or1k_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
 static asection *
 readonly_dynrelocs (struct elf_link_hash_entry *h)
 {
-  struct elf_dyn_relocs *p;
+  struct elf_dyn_relocs *sec_relocs;
   struct elf_or1k_link_hash_entry *eh = (struct elf_or1k_link_hash_entry *) h;
 
-  for (p = eh->dyn_relocs; p != NULL; p = p->next)
+  for (sec_relocs = eh->dyn_relocs;
+       sec_relocs != NULL;
+       sec_relocs = sec_relocs->next)
     {
-      asection *s = p->sec->output_section;
+      asection *s = sec_relocs->sec->output_section;
 
       if (s != NULL && (s->flags & SEC_READONLY) != 0)
-	return p->sec;
+	return sec_relocs->sec;
     }
   return NULL;
 }
@@ -2750,7 +2754,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
   struct bfd_link_info *info;
   struct elf_or1k_link_hash_table *htab;
   struct elf_or1k_link_hash_entry *eh;
-  struct elf_dyn_relocs *p;
+  struct elf_dyn_relocs *sec_relocs;
 
   if (h->root.type == bfd_link_hash_indirect)
     return TRUE;
@@ -2863,14 +2867,14 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 	{
 	  struct elf_dyn_relocs **pp;
 
-	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
+	  for (pp = &eh->dyn_relocs; (sec_relocs = *pp) != NULL;)
 	    {
-	      p->count -= p->pc_count;
-	      p->pc_count = 0;
-	      if (p->count == 0)
-		*pp = p->next;
+	      sec_relocs->count -= sec_relocs->pc_count;
+	      sec_relocs->pc_count = 0;
+	      if (sec_relocs->count == 0)
+		*pp = sec_relocs->next;
 	      else
-		pp = &p->next;
+		pp = &sec_relocs->next;
 	    }
 	}
 
@@ -2926,10 +2930,12 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
     }
 
   /* Finally, allocate space.  */
-  for (p = eh->dyn_relocs; p != NULL; p = p->next)
+  for (sec_relocs = eh->dyn_relocs;
+       sec_relocs != NULL;
+       sec_relocs = sec_relocs->next)
     {
-      asection *sreloc = elf_section_data (p->sec)->sreloc;
-      sreloc->size += p->count * sizeof (Elf32_External_Rela);
+      asection *sreloc = elf_section_data (sec_relocs->sec)->sreloc;
+      sreloc->size += sec_relocs->count * sizeof (Elf32_External_Rela);
     }
 
   return TRUE;
@@ -3009,26 +3015,28 @@ or1k_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 
       for (s = ibfd->sections; s != NULL; s = s->next)
 	{
-	  struct elf_dyn_relocs *p;
+	  struct elf_dyn_relocs *sec_relocs;
 
-	  for (p = ((struct elf_dyn_relocs *)
-		    elf_section_data (s)->local_dynrel);
-	       p != NULL;
-	       p = p->next)
+	  for (sec_relocs = ((struct elf_dyn_relocs *)
+			     elf_section_data (s)->local_dynrel);
+	       sec_relocs != NULL;
+	       sec_relocs = sec_relocs->next)
 	    {
-	      if (! bfd_is_abs_section (p->sec)
-		  && bfd_is_abs_section (p->sec->output_section))
+	      if (! bfd_is_abs_section (sec_relocs->sec)
+		  && bfd_is_abs_section (sec_relocs->sec->output_section))
 		{
 		  /* Input section has been discarded, either because
 		     it is a copy of a linkonce section or due to
 		     linker script /DISCARD/, so we'll be discarding
 		     the relocs too.  */
 		}
-	      else if (p->count != 0)
+	      else if (sec_relocs->count != 0)
 		{
-		  srel = elf_section_data (p->sec)->sreloc;
-		  srel->size += p->count * sizeof (Elf32_External_Rela);
-		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
+		  srel = elf_section_data (sec_relocs->sec)->sreloc;
+		  srel->size += sec_relocs->count
+				* sizeof (Elf32_External_Rela);
+		  if ((sec_relocs->sec->output_section->flags & SEC_READONLY)
+		      != 0)
 		    info->flags |= DF_TEXTREL;
 		}
 	    }
Signature · 1 line
2.26.2
#7 lore raw

PATCH 7/8 or1k: refactor: Rename s to sgot and splt

In reply to Stafford Horne

The symbol name s for section is terse, replacing with sgot when it refers to the got and splt when it refers to the plt makes reading code a bit easier.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* elf32-or1k.c (allocate_dynrelocs): Rename s to splt or sgot based on usage.

---
 bfd/elf32-or1k.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
bfd/elf32-or1k.c +10 -10
index 22c316d042..7ac34737aa 100644
@@ -2780,14 +2780,14 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 
       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
 	{
-	  asection *s = htab->root.splt;
+	  asection *splt = htab->root.splt;
 
 	  /* If this is the first .plt entry, make room for the special
 	     first entry.  */
-	  if (s->size == 0)
-	    s->size = PLT_ENTRY_SIZE;
+	  if (splt->size == 0)
+	    splt->size = PLT_ENTRY_SIZE;
 
-	  h->plt.offset = s->size;
+	  h->plt.offset = splt->size;
 
 	  /* If this symbol is not defined in a regular file, and we are
 	     not generating a shared library, then set the symbol to this
@@ -2797,12 +2797,12 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 	  if (! bfd_link_pic (info)
 	      && !h->def_regular)
 	    {
-	      h->root.u.def.section = s;
+	      h->root.u.def.section = splt;
 	      h->root.u.def.value = h->plt.offset;
 	    }
 
 	  /* Make room for this entry.  */
-	  s->size += PLT_ENTRY_SIZE;
+	  splt->size += PLT_ENTRY_SIZE;
 
 	  /* We also need to make an entry in the .got.plt section, which
 	     will be placed in the .got section by the linker script.  */
@@ -2825,7 +2825,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 
   if (h->got.refcount > 0)
     {
-      asection *s;
+      asection *sgot;
       bfd_boolean dyn;
       unsigned char tls_type;
 
@@ -2838,16 +2838,16 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
 	    return FALSE;
 	}
 
-      s = htab->root.sgot;
+      sgot = htab->root.sgot;
 
-      h->got.offset = s->size;
+      h->got.offset = sgot->size;
 
       tls_type = ((struct elf_or1k_link_hash_entry *) h)->tls_type;
 
       dyn = htab->root.dynamic_sections_created;
       dyn = WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h);
       or1k_set_got_and_rela_sizes (tls_type, dyn,
-				   &s->size, &htab->root.srelgot->size);
+				   &sgot->size, &htab->root.srelgot->size);
     }
   else
     h->got.offset = (bfd_vma) -1;
Signature · 1 line
2.26.2
#8 lore raw

PATCH 8/8 or1k: Add dynamic flag to tpoff

In reply to Stafford Horne

This is needed to distinguish between initial executable GD, LD, IE and LE TLS access which require TCB offset and section alignment, but access to dynamic symbols in secondary TLS modules do not need this offset.

This was found when running the glibc testsuite.

bfd/ChangeLog:

yyyy-mm-dd Stafford Horne <[email protected]>

* elf32-or1k.c (tpoff): Add dynamic boolean argument. (or1k_elf_relocate_section): Pass dynamic flag to tpoff.

---
 bfd/elf32-or1k.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
bfd/elf32-or1k.c +22 -17
index 7ac34737aa..a7fe0a9c7c 100644
@@ -1050,10 +1050,9 @@ or1k_info_to_howto_rela (bfd * abfd,
   return TRUE;
 }
 
-
 /* Return the relocation value for @tpoff relocations..  */
 static bfd_vma
-tpoff (struct bfd_link_info *info, bfd_vma address)
+tpoff (struct bfd_link_info *info, bfd_vma address, bfd_boolean dynamic)
 {
   struct elf_link_hash_table *htab = elf_hash_table (info);
   bfd_vma base;
@@ -1062,15 +1061,20 @@ tpoff (struct bfd_link_info *info, bfd_vma address)
   if (htab->tls_sec == NULL)
     return 0;
 
-  /* On or1k, the tp points to just after the tcb, if we have an alignment
-     greater than the tcb size we need to offset by the alignment difference.  */
-  base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power)
-	 - TCB_SIZE;
-
-  /* The thread pointer on or1k stores the address after the TCB where
-     the data is, just compute the difference. No need to compensate
-     for the size of TCB.  */
-  return address - htab->tls_sec->vma + base;
+  if (dynamic)
+    return address - htab->tls_sec->vma;
+  else
+    {
+      /* On or1k, the tp points to just after the tcb, if we have an alignment
+	 greater than the tcb size we need to offset by the alignment difference.  */
+      base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power)
+	     - TCB_SIZE;
+
+      /* The thread pointer on or1k stores the address after the TCB where
+	 the data is, just compute the difference. No need to compensate
+	 for the size of TCB.  */
+      return address - htab->tls_sec->vma + base;
+    }
 }
 
 /* If we have both IE and GD accesses to a symbol the IE relocations should be
@@ -1624,7 +1628,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    Elf_Internal_Rela rela;
 	    asection *srelgot;
 	    bfd_byte *loc;
-	    int dynamic;
+	    bfd_boolean dynamic;
 	    int indx = 0;
 	    unsigned char tls_type;
 
@@ -1707,7 +1711,8 @@ or1k_elf_relocate_section (bfd *output_bfd,
 		      {
 			rela.r_info = ELF32_R_INFO (0,
 			    (i == 0 ? R_OR1K_TLS_DTPMOD : R_OR1K_TLS_DTPOFF));
-			rela.r_addend = tpoff (info, relocation);
+			rela.r_addend =
+			    (i == 0 ? 0 : tpoff (info, relocation, dynamic));
 		      }
 
 		    loc = srelgot->contents;
@@ -1722,7 +1727,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    else if ((tls_type & TLS_GD) != 0)
 	      {
 		bfd_put_32 (output_bfd, 1, sgot->contents + gotoff);
-		bfd_put_32 (output_bfd, tpoff (info, relocation),
+		bfd_put_32 (output_bfd, tpoff (info, relocation, dynamic),
 		    sgot->contents + gotoff + 4);
 	      }
 
@@ -1743,7 +1748,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 		else
 		  {
 		    rela.r_info = ELF32_R_INFO (0, R_OR1K_TLS_TPOFF);
-		    rela.r_addend = tpoff (info, relocation);
+		    rela.r_addend = tpoff (info, relocation, dynamic);
 		  }
 
 		loc = srelgot->contents;
@@ -1754,7 +1759,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	      }
 	    /* Static IE.  */
 	    else if ((tls_type & TLS_IE) != 0)
-	      bfd_put_32 (output_bfd, tpoff (info, relocation),
+	      bfd_put_32 (output_bfd, tpoff (info, relocation, dynamic),
 			  sgot->contents + gotoff);
 
 	    /* The PG21 and LO13 relocs are pc-relative, while the
@@ -1773,7 +1778,7 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	case R_OR1K_TLS_LE_AHI16:
 	case R_OR1K_TLS_LE_SLO16:
 	  /* Relocation is offset from TP.  */
-	  relocation = tpoff (info, relocation);
+	  relocation = tpoff (info, relocation, 0);
 	  break;
 
 	case R_OR1K_TLS_DTPMOD:
Signature · 1 line
2.26.2
#9 lore raw

cover OpenRISC BFD fixups for Glibc

In reply to Stafford Horne

Hi Stafford,

Quoted 9 lines

Stafford Horne (8):
or1k: Fix static linking when with .rela.got relocations
or1k: Fix dynamic TLS symbol flag
or1k: Add TLS mask to handle multiple model access
or1k: Fix issue with multiple PCREL relocations
or1k: TLS offset to use tcb size and section alignment
or1k: refactor: Rename p to sec_relocs
or1k: refactor: Rename s to sgot and splt
or1k: Add dynamic flag to tpoff

Patch series approved and applied. (Sorry for the delay...)

Cheers
Nick

#10 lore raw

cover OpenRISC BFD fixups for Glibc

Quoted 12 lines · Nick Clifton

Hi Stafford,

Quoted 9 lines

Stafford Horne (8):
or1k: Fix static linking when with .rela.got relocations
or1k: Fix dynamic TLS symbol flag
or1k: Add TLS mask to handle multiple model access
or1k: Fix issue with multiple PCREL relocations
or1k: TLS offset to use tcb size and section alignment
or1k: refactor: Rename p to sec_relocs
or1k: refactor: Rename s to sgot and splt
or1k: Add dynamic flag to tpoff

Quoted 1 line

Patch series approved and applied. (Sorry for the delay...)

Thank you,

That was not much delay at all. However, I was about to just apply the set of
patches myself. Would that have been a problem?

-Stafford

#11 lore raw

cover OpenRISC BFD fixups for Glibc

Hi Stafford,

Quoted 2 lines

That was not much delay at all. However, I was about to just apply the set of
patches myself. Would that have been a problem?

Not at all. But I had applied the patches locally, in order to run them through my test harness, so it was no effort for me to commit the patches as well.

Cheers
Nick