Blame SOURCES/binutils-copy-multiple-relocs.patch

3bbc82
diff -rup binutils.orig/bfd/elf-bfd.h binutils-2.30/bfd/elf-bfd.h
3bbc82
--- binutils.orig/bfd/elf-bfd.h	2020-02-12 13:31:20.348605538 +0000
3bbc82
+++ binutils-2.30/bfd/elf-bfd.h	2020-02-12 13:31:33.831507582 +0000
3bbc82
@@ -1479,6 +1479,19 @@ struct elf_backend_data
3bbc82
   /* Opcode representing no unwind.  */
3bbc82
   int (*cant_unwind_opcode) (struct bfd_link_info *);
3bbc82
 
3bbc82
+  /* Called when a section has extra reloc sections.  */
3bbc82
+  bfd_boolean (*init_secondary_reloc_section) (bfd *, Elf_Internal_Shdr *, const char *, unsigned int);
3bbc82
+
3bbc82
+  /* Called when after loading the normal relocs for a section.  */
3bbc82
+  bfd_boolean (*slurp_secondary_relocs) (bfd *, asection *, asymbol **);
3bbc82
+
3bbc82
+  /* Called after writing the normal relocs for a section.  */
3bbc82
+  bfd_boolean (*write_secondary_relocs) (bfd *, asection *);
3bbc82
+
3bbc82
+  /* Called to return the value to set in the ST_SHNDX field of an ELF symbol
3bbc82
+     from an iternal symbol which does not map to any known section.  */
3bbc82
+  unsigned int (*symbol_section_index) (bfd *, elf_symbol_type *);
3bbc82
+  
3bbc82
   /* This is non-zero if static TLS segments require a special alignment.  */
3bbc82
   unsigned static_tls_alignment;
3bbc82
 
3bbc82
@@ -2696,6 +2709,19 @@ extern bfd_vma elf64_r_sym (bfd_vma);
3bbc82
 extern bfd_vma elf32_r_info (bfd_vma, bfd_vma);
3bbc82
 extern bfd_vma elf32_r_sym (bfd_vma);
3bbc82
 
3bbc82
+
3bbc82
+extern bfd_boolean _bfd_elf_init_secondary_reloc_section
3bbc82
+  (bfd *, Elf_Internal_Shdr *, const char *, unsigned int);
3bbc82
+extern bfd_boolean _bfd_elf_slurp_secondary_reloc_section
3bbc82
+  (bfd *, asection *, asymbol **);
3bbc82
+extern bfd_boolean _bfd_elf_copy_special_section_fields
3bbc82
+  (const bfd *, bfd *, const Elf_Internal_Shdr *, Elf_Internal_Shdr *);
3bbc82
+extern bfd_boolean _bfd_elf_write_secondary_reloc_section
3bbc82
+  (bfd *, asection *);
3bbc82
+extern unsigned int _bfd_elf_symbol_section_index
3bbc82
+  (bfd *, elf_symbol_type *);
3bbc82
+
3bbc82
+
3bbc82
 /* Large common section.  */
3bbc82
 extern asection _bfd_elf_large_com_section;
3bbc82
 
3bbc82
diff -rup binutils.orig/bfd/elf.c binutils-2.30/bfd/elf.c
3bbc82
--- binutils.orig/bfd/elf.c	2020-02-12 13:31:20.347605546 +0000
3bbc82
+++ binutils-2.30/bfd/elf.c	2020-02-12 13:33:19.635738944 +0000
3bbc82
@@ -1572,7 +1572,7 @@ _bfd_elf_copy_private_bfd_data (bfd *ibf
3bbc82
 	  /* Final attempt.  Call the backend copy function
3bbc82
 	     with a NULL input section.  */
3bbc82
 	  if (bed->elf_backend_copy_special_section_fields != NULL)
3bbc82
-	    bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
3bbc82
+	    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
3bbc82
 	}
3bbc82
     }
3bbc82
 
3bbc82
@@ -2416,11 +2416,14 @@ bfd_section_from_shdr (bfd *abfd, unsign
3bbc82
 	   sections.  */
3bbc82
 	if (*p_hdr != NULL)
3bbc82
 	  {
3bbc82
-	    _bfd_error_handler
3bbc82
-	      /* xgettext:c-format */
3bbc82
-	      (_("%B: warning: multiple relocation sections for section %A \
3bbc82
-found - ignoring all but the first"),
3bbc82
-	       abfd, target_sect);
3bbc82
+	    if (bed->init_secondary_reloc_section == NULL
3bbc82
+		|| ! bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
3bbc82
+	      {
3bbc82
+		_bfd_error_handler
3bbc82
+		  /* xgettext:c-format */
3bbc82
+		  (_("%pB: warning: secondary relocation section '%s' for section %pA found - ignoring"),
3bbc82
+		   abfd, name, target_sect);
3bbc82
+	      }
3bbc82
 	    goto success;
3bbc82
 	  }
3bbc82
 	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
3bbc82
@@ -7948,9 +7951,20 @@ error_return:
3bbc82
 		  if (elf_symtab_shndx_list (abfd))
3bbc82
 		    shndx = elf_symtab_shndx_list (abfd)->ndx;
3bbc82
 		  break;
3bbc82
-		default:
3bbc82
+		case SHN_COMMON:
3bbc82
+		case SHN_ABS:
3bbc82
 		  shndx = SHN_ABS;
3bbc82
 		  break;
3bbc82
+		default:
3bbc82
+		  if (bed->symbol_section_index)
3bbc82
+		    shndx = bed->symbol_section_index (abfd, type_ptr);
3bbc82
+		  else
3bbc82
+		    {
3bbc82
+		      _bfd_error_handler (_("%pB: Unable to handle section index %x in ELF symbol.  Using ABS instead. (%x)"),
3bbc82
+					  abfd, shndx, SHN_COMMON);
3bbc82
+		      shndx = SHN_ABS;
3bbc82
+		    }
3bbc82
+		  break;
3bbc82
 		}
3bbc82
 	    }
3bbc82
 	  else
3bbc82
@@ -11556,3 +11570,354 @@ _bfd_elf_maybe_function_sym (const asymb
3bbc82
     size = 1;
3bbc82
   return size;
3bbc82
 }
3bbc82
+
3bbc82
+/* Set to non-zero to enable some debug messages.  */
3bbc82
+#define DEBUG_SECONDARY_RELOCS	 0
3bbc82
+
3bbc82
+/* An internal-to-the-bfd-library only section type
3bbc82
+   used to indicate a cached secondary reloc section.  */
3bbc82
+#define SHT_SECONDARY_RELOC	 (SHT_LOOS + SHT_RELA)
3bbc82
+
3bbc82
+/* Create a BFD section to hold a secondary reloc section.  */
3bbc82
+
3bbc82
+bfd_boolean
3bbc82
+_bfd_elf_init_secondary_reloc_section (bfd * abfd,
3bbc82
+				       Elf_Internal_Shdr *hdr,
3bbc82
+				       const char * name,
3bbc82
+				       unsigned int shindex)
3bbc82
+{
3bbc82
+  /* We only support RELA secondary relocs.  */
3bbc82
+  if (hdr->sh_type != SHT_RELA)
3bbc82
+    return FALSE;
3bbc82
+
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+  fprintf (stderr, "secondary reloc section %s encountered\n", name);
3bbc82
+#endif
3bbc82
+  hdr->sh_type = SHT_SECONDARY_RELOC;
3bbc82
+  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3bbc82
+}
3bbc82
+
3bbc82
+/* Read in any secondary relocs associated with SEC.  */
3bbc82
+
3bbc82
+bfd_boolean
3bbc82
+_bfd_elf_slurp_secondary_reloc_section (bfd *      abfd,
3bbc82
+					asection * sec,
3bbc82
+					asymbol ** symbols)
3bbc82
+{
3bbc82
+  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
3bbc82
+  asection * relsec;
3bbc82
+  bfd_boolean result = TRUE;
3bbc82
+  bfd_vma (*r_sym) (bfd_vma);
3bbc82
+
3bbc82
+
3bbc82
+#ifdef BFD64
3bbc82
+  if (bfd_arch_bits_per_address (abfd) != 32)
3bbc82
+    r_sym = elf64_r_sym;
3bbc82
+  else
3bbc82
+#endif
3bbc82
+    r_sym = elf32_r_sym;
3bbc82
+  
3bbc82
+  /* Discover if there are any secondary reloc sections
3bbc82
+     associated with SEC.  */
3bbc82
+  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
3bbc82
+    {
3bbc82
+      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
3bbc82
+
3bbc82
+      if (hdr->sh_type == SHT_SECONDARY_RELOC
3bbc82
+	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
3bbc82
+	{
3bbc82
+	  bfd_byte * native_relocs;
3bbc82
+	  bfd_byte * native_reloc;
3bbc82
+	  arelent * internal_relocs;
3bbc82
+	  arelent * internal_reloc;
3bbc82
+	  unsigned int i;
3bbc82
+	  unsigned int entsize;
3bbc82
+	  unsigned int symcount;
3bbc82
+	  unsigned int reloc_count;
3bbc82
+
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+	  fprintf (stderr, "read secondary relocs for %s from %s\n", sec->name, relsec->name);
3bbc82
+#endif
3bbc82
+	  entsize = hdr->sh_entsize;
3bbc82
+
3bbc82
+	  native_relocs = bfd_malloc (hdr->sh_size);
3bbc82
+	  if (native_relocs == NULL)
3bbc82
+	    {
3bbc82
+	      result = FALSE;
3bbc82
+	      continue;
3bbc82
+	    }
3bbc82
+
3bbc82
+	  reloc_count = NUM_SHDR_ENTRIES (hdr);
3bbc82
+	  internal_relocs = (arelent *) bfd_alloc2 (abfd, reloc_count, sizeof (arelent));
3bbc82
+	  if (internal_relocs == NULL)
3bbc82
+	    {
3bbc82
+	      free (native_relocs);
3bbc82
+	      result = FALSE;
3bbc82
+	      continue;
3bbc82
+	    }
3bbc82
+
3bbc82
+	  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3bbc82
+	      || (bfd_bread (native_relocs, hdr->sh_size, abfd) != hdr->sh_size))
3bbc82
+	    {
3bbc82
+	      free (native_relocs);
3bbc82
+	      free (internal_relocs);
3bbc82
+	      result = FALSE;
3bbc82
+	      continue;
3bbc82
+	    }
3bbc82
+
3bbc82
+	  symcount = bfd_get_symcount (abfd);
3bbc82
+
3bbc82
+	  for (i = 0, internal_reloc = internal_relocs, native_reloc = native_relocs;
3bbc82
+	       i < reloc_count;
3bbc82
+	       i++, internal_reloc++, native_reloc += entsize)
3bbc82
+	    {
3bbc82
+	      ;
3bbc82
+	      Elf_Internal_Rela rela;
3bbc82
+
3bbc82
+	      ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
3bbc82
+
3bbc82
+	      /* The address of an ELF reloc is section relative for an object
3bbc82
+		 file, and absolute for an executable file or shared library.
3bbc82
+		 The address of a normal BFD reloc is always section relative,
3bbc82
+		 and the address of a dynamic reloc is absolute..  */
3bbc82
+	      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3bbc82
+		internal_reloc->address = rela.r_offset;
3bbc82
+	      else
3bbc82
+		internal_reloc->address = rela.r_offset - sec->vma;
3bbc82
+
3bbc82
+	      if (r_sym (rela.r_info) == STN_UNDEF)
3bbc82
+		{
3bbc82
+		  /* FIXME: This and the error case below mean that we
3bbc82
+		     have a symbol on relocs that is not elf_symbol_type.  */
3bbc82
+		  internal_reloc->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
3bbc82
+		}
3bbc82
+	      else if (r_sym (rela.r_info) > symcount)
3bbc82
+		{
3bbc82
+		  _bfd_error_handler
3bbc82
+		    /* xgettext:c-format */
3bbc82
+		    (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
3bbc82
+		     abfd, sec, i, (long) r_sym (rela.r_info));
3bbc82
+		  bfd_set_error (bfd_error_bad_value);
3bbc82
+		  internal_reloc->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
3bbc82
+		  result = FALSE;
3bbc82
+		}
3bbc82
+	      else
3bbc82
+		{
3bbc82
+		  asymbol **ps;
3bbc82
+
3bbc82
+		  ps = symbols + r_sym (rela.r_info) - 1;
3bbc82
+
3bbc82
+		  internal_reloc->sym_ptr_ptr = ps;
3bbc82
+		  /* Make sure that this symbol is not removed by strip.  */
3bbc82
+		  (*ps)->flags |= BSF_KEEP;
3bbc82
+		}
3bbc82
+
3bbc82
+	      internal_reloc->addend = rela.r_addend;
3bbc82
+
3bbc82
+	      ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
3bbc82
+	      if (internal_reloc->howto == NULL)
3bbc82
+		{
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+		  fprintf (stderr, "there is no howto associated with reloc %lx\n", rela.r_info);
3bbc82
+#endif
3bbc82
+		  result = FALSE;
3bbc82
+		}
3bbc82
+	    }
3bbc82
+
3bbc82
+	  free (native_relocs);
3bbc82
+	  /* Store the internal relocs.  */
3bbc82
+	  elf_section_data (relsec)->sec_info = internal_relocs;
3bbc82
+	}
3bbc82
+    }
3bbc82
+
3bbc82
+  return result;
3bbc82
+}
3bbc82
+
3bbc82
+/* Set the ELF section header fields of an output secondary reloc section.  */
3bbc82
+
3bbc82
+bfd_boolean
3bbc82
+_bfd_elf_copy_special_section_fields (const bfd *               ibfd ATTRIBUTE_UNUSED,
3bbc82
+				      bfd *                     obfd ATTRIBUTE_UNUSED,
3bbc82
+				      const Elf_Internal_Shdr * isection,
3bbc82
+				      Elf_Internal_Shdr *       osection)
3bbc82
+{
3bbc82
+  if (isection == NULL)
3bbc82
+    return FALSE;
3bbc82
+
2e4410
+  if (isection->sh_type != SHT_SECONDARY_RELOC)
2e4410
+    return TRUE;
2e4410
+
3bbc82
+  asection * isec = isection->bfd_section;
3bbc82
+  if (isec == NULL)
3bbc82
+    return FALSE;
3bbc82
+
3bbc82
+  asection * osec = osection->bfd_section;
3bbc82
+  if (osec == NULL)
3bbc82
+    return FALSE;
3bbc82
+
3bbc82
+  BFD_ASSERT (elf_section_data (osec)->sec_info == NULL);
3bbc82
+  elf_section_data (osec)->sec_info = elf_section_data (isec)->sec_info;
3bbc82
+  osection->sh_type = SHT_RELA;
3bbc82
+  osection->sh_link = elf_onesymtab (obfd);
3bbc82
+  if (osection->sh_link == 0)
3bbc82
+    {
3bbc82
+      /* There is no symbol table - we are hosed...  */
3bbc82
+      _bfd_error_handler
3bbc82
+	/* xgettext:c-format */
3bbc82
+	(_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
3bbc82
+	obfd, osec);
3bbc82
+      bfd_set_error (bfd_error_bad_value);
3bbc82
+      return FALSE;
3bbc82
+    }
3bbc82
+
3bbc82
+  /* Find the output section that corresponds to the isection's sh_info link.  */
3bbc82
+  BFD_ASSERT (isection->sh_info > 0 && isection->sh_info < elf_numsections (ibfd));
3bbc82
+  isection = elf_elfsections (ibfd)[isection->sh_info];
3bbc82
+
3bbc82
+  BFD_ASSERT (isection != NULL);
3bbc82
+  BFD_ASSERT (isection->bfd_section != NULL);
3bbc82
+  BFD_ASSERT (isection->bfd_section->output_section != NULL);
3bbc82
+  osection->sh_info = elf_section_data (isection->bfd_section->output_section)->this_idx;
3bbc82
+
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
3bbc82
+	   osec->name, osection->sh_link, osection->sh_info);
3bbc82
+#endif
3bbc82
+
3bbc82
+  return TRUE;
3bbc82
+}
3bbc82
+
3bbc82
+/* Write out a secondary reloc section.  */
3bbc82
+
3bbc82
+bfd_boolean
3bbc82
+_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
3bbc82
+{
3bbc82
+  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
3bbc82
+  bfd_vma addr_offset;
3bbc82
+  asection * relsec;
3bbc82
+  bfd_vma (*r_info) (bfd_vma, bfd_vma);
3bbc82
+
3bbc82
+#ifdef BFD64
3bbc82
+  if (bfd_arch_bits_per_address (abfd) != 32)
3bbc82
+    r_info = elf64_r_info;
3bbc82
+  else
3bbc82
+#endif
3bbc82
+    r_info = elf32_r_info;
3bbc82
+
3bbc82
+  if (sec == NULL)
3bbc82
+    return FALSE;
3bbc82
+
3bbc82
+  /* The address of an ELF reloc is section relative for an object
3bbc82
+     file, and absolute for an executable file or shared library.
3bbc82
+     The address of a BFD reloc is always section relative.  */
3bbc82
+  addr_offset = 0;
3bbc82
+  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3bbc82
+    addr_offset = sec->vma;
3bbc82
+
3bbc82
+  /* Discover if there are any secondary reloc sections
3bbc82
+     associated with SEC.  */
3bbc82
+  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
3bbc82
+    {
3bbc82
+      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
3bbc82
+      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
3bbc82
+
3bbc82
+      if (hdr->sh_type == SHT_RELA
3bbc82
+	  && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
3bbc82
+	{
3bbc82
+	  asymbol *    last_sym;
3bbc82
+	  int          last_sym_idx;
3bbc82
+	  unsigned int reloc_count;
3bbc82
+	  unsigned int idx;
3bbc82
+	  arelent *    src_irel;
3bbc82
+	  bfd_byte *   dst_rela;
3bbc82
+
3bbc82
+	  BFD_ASSERT (hdr->contents == NULL);
3bbc82
+
3bbc82
+	  reloc_count = hdr->sh_size / hdr->sh_entsize;
3bbc82
+	  BFD_ASSERT (reloc_count > 0);
3bbc82
+
3bbc82
+	  hdr->contents = bfd_alloc (abfd, hdr->sh_size);
3bbc82
+	  if (hdr->contents == NULL)
3bbc82
+	    continue;
3bbc82
+
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+	  fprintf (stderr, "write %u secondary relocs for %s from %s\n", reloc_count, sec->name, relsec->name);
3bbc82
+#endif
3bbc82
+	  last_sym = NULL;
3bbc82
+	  last_sym_idx = 0;
3bbc82
+	  dst_rela = hdr->contents;
3bbc82
+	  src_irel = (arelent *) esd->sec_info;
3bbc82
+	  BFD_ASSERT (src_irel != NULL);
3bbc82
+
3bbc82
+	  for (idx = 0; idx < reloc_count; idx++, dst_rela += hdr->sh_entsize)
3bbc82
+	    {
3bbc82
+	      Elf_Internal_Rela src_rela;
3bbc82
+	      arelent *ptr;
3bbc82
+	      asymbol *sym;
3bbc82
+	      int n;
3bbc82
+
3bbc82
+	      ptr = src_irel + idx;
3bbc82
+	      sym = *ptr->sym_ptr_ptr;
3bbc82
+
3bbc82
+	      if (sym == last_sym)
3bbc82
+		n = last_sym_idx;
3bbc82
+	      else
3bbc82
+		{
3bbc82
+		  last_sym = sym;
3bbc82
+		  n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
3bbc82
+		  if (n < 0)
3bbc82
+		    {
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+		      fprintf (stderr, "failed to find symbol %s whilst rewriting relocs\n",
3bbc82
+			       sym->name);
3bbc82
+#endif
3bbc82
+		      /* FIXME: Signal failure somehow.  */
3bbc82
+		      n = 0;
3bbc82
+		    }
3bbc82
+		  last_sym_idx = n;
3bbc82
+		}
3bbc82
+
3bbc82
+	      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
3bbc82
+		  && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
3bbc82
+		  && ! _bfd_elf_validate_reloc (abfd, ptr))
3bbc82
+		{
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+		  fprintf (stderr, "symbol %s is not in the output bfd\n",
3bbc82
+			   sym->name);
3bbc82
+#endif
3bbc82
+		  /* FIXME: Signal failure somehow.  */
3bbc82
+		  n = 0;
3bbc82
+		}
3bbc82
+
3bbc82
+	      if (ptr->howto == NULL)
3bbc82
+		{
3bbc82
+#if DEBUG_SECONDARY_RELOCS
3bbc82
+		  fprintf (stderr, "reloc for symbol %s does not have a howto associated with it\n",
3bbc82
+			   sym->name);
3bbc82
+#endif
3bbc82
+		  /* FIXME: Signal failure somehow.  */
3bbc82
+		  n = 0;
3bbc82
+		}
3bbc82
+
3bbc82
+	      src_rela.r_offset = ptr->address + addr_offset;
3bbc82
+	      src_rela.r_info = r_info (n, ptr->howto->type);
3bbc82
+	      src_rela.r_addend = ptr->addend;
3bbc82
+	      ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
3bbc82
+	    }
3bbc82
+	}
3bbc82
+    }
3bbc82
+
3bbc82
+  return TRUE;
3bbc82
+}
3bbc82
+
3bbc82
+/* Preserve any OS or PROCESSOR specific section indicies.  */
3bbc82
+
3bbc82
+unsigned int
3bbc82
+_bfd_elf_symbol_section_index (bfd * abfd ATTRIBUTE_UNUSED,
3bbc82
+			       elf_symbol_type * sym)
3bbc82
+{
3bbc82
+  unsigned int shndx = sym->internal_elf_sym.st_shndx;
3bbc82
+
3bbc82
+  /* Preserve special section indicies.  */
3bbc82
+  return shndx >= SHN_LORESERVE ? shndx : SHN_ABS;
3bbc82
+}
3bbc82
diff -rup binutils.orig/bfd/elfcode.h binutils-2.30/bfd/elfcode.h
3bbc82
--- binutils.orig/bfd/elfcode.h	2020-02-12 13:31:20.334605640 +0000
3bbc82
+++ binutils-2.30/bfd/elfcode.h	2020-02-12 13:31:33.833507567 +0000
3bbc82
@@ -855,6 +855,7 @@ elf_object_p (bfd *abfd)
3bbc82
 void
3bbc82
 elf_write_relocs (bfd *abfd, asection *sec, void *data)
3bbc82
 {
3bbc82
+  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
3bbc82
   bfd_boolean *failedp = (bfd_boolean *) data;
3bbc82
   Elf_Internal_Shdr *rela_hdr;
3bbc82
   bfd_vma addr_offset;
3bbc82
@@ -969,6 +970,13 @@ elf_write_relocs (bfd *abfd, asection *s
3bbc82
       src_rela.r_addend = ptr->addend;
3bbc82
       (*swap_out) (abfd, &src_rela, dst_rela);
3bbc82
     }
3bbc82
+
3bbc82
+  if (bed->write_secondary_relocs != NULL)
3bbc82
+    if (! bed->write_secondary_relocs (abfd, sec))
3bbc82
+      {
3bbc82
+	*failedp = TRUE;
3bbc82
+	return;
3bbc82
+      }
3bbc82
 }
3bbc82
 
3bbc82
 /* Write out the program headers.  */
3bbc82
@@ -1271,7 +1279,10 @@ elf_slurp_symbol_table (bfd *abfd, asymb
3bbc82
 		{
3bbc82
 		  /* This symbol is in a section for which we did not
3bbc82
 		     create a BFD section.  Just use bfd_abs_section,
3bbc82
-		     although it is wrong.  FIXME.  */
3bbc82
+		     although it is wrong.  FIXME.  Note - there is
3bbc82
+		     code in elf.c:swap_out_syms that calls
3bbc82
+		     symbol_section_index() in the elf backend for
3bbc82
+		     cases like this.  */
3bbc82
 		  sym->symbol.section = bfd_abs_section_ptr;
3bbc82
 		}
3bbc82
 	    }
3bbc82
@@ -1501,6 +1512,7 @@ elf_slurp_reloc_table (bfd *abfd,
3bbc82
 		       asymbol **symbols,
3bbc82
 		       bfd_boolean dynamic)
3bbc82
 {
3bbc82
+  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
3bbc82
   struct bfd_elf_section_data * const d = elf_section_data (asect);
3bbc82
   Elf_Internal_Shdr *rel_hdr;
3bbc82
   Elf_Internal_Shdr *rel_hdr2;
3bbc82
@@ -1564,6 +1576,10 @@ elf_slurp_reloc_table (bfd *abfd,
3bbc82
 					      symbols, dynamic))
3bbc82
     return FALSE;
3bbc82
 
3bbc82
+  if (bed->slurp_secondary_relocs != NULL
3bbc82
+      && ! bed->slurp_secondary_relocs (abfd, asect, symbols))
3bbc82
+    return FALSE;
3bbc82
+
3bbc82
   asect->relocation = relents;
3bbc82
   return TRUE;
3bbc82
 }
3bbc82
diff -rup binutils.orig/bfd/elflink.c binutils-2.30/bfd/elflink.c
3bbc82
--- binutils.orig/bfd/elflink.c	2020-02-12 13:31:20.338605611 +0000
3bbc82
+++ binutils-2.30/bfd/elflink.c	2020-02-12 13:31:33.834507560 +0000
3bbc82
@@ -11514,6 +11514,10 @@ elf_final_link_free (bfd *obfd, struct e
3bbc82
     }
3bbc82
 }
3bbc82
 
3bbc82
+#define is_reloc_section(ESDO)			\
3bbc82
+  (   (ESDO)->this_hdr.sh_type == SHT_REL	\
3bbc82
+   || (ESDO)->this_hdr.sh_type == SHT_RELA)
3bbc82
+
3bbc82
 /* Do the final step of an ELF link.  */
3bbc82
 
3bbc82
 bfd_boolean
3bbc82
@@ -11685,8 +11689,7 @@ bfd_elf_final_link (bfd *abfd, struct bf
3bbc82
 		      && elf_symtab_shndx_list (sec->owner) != NULL)
3bbc82
 		    max_sym_shndx_count = sym_count;
3bbc82
 
3bbc82
-		  if (esdo->this_hdr.sh_type == SHT_REL
3bbc82
-		      || esdo->this_hdr.sh_type == SHT_RELA)
3bbc82
+		  if (is_reloc_section (esdo))
3bbc82
 		    /* Some backends use reloc_count in relocation sections
3bbc82
 		       to count particular types of relocs.  Of course,
3bbc82
 		       reloc sections themselves can't have relocations.  */
3bbc82
@@ -12290,6 +12293,9 @@ bfd_elf_final_link (bfd *abfd, struct bf
3bbc82
       struct bfd_elf_section_data *esdo = elf_section_data (o);
3bbc82
       bfd_boolean sort;
3bbc82
 
3bbc82
+      if (esdo == NULL)
3bbc82
+	continue;
3bbc82
+
3bbc82
       if ((o->flags & SEC_RELOC) == 0)
3bbc82
 	continue;
3bbc82
 
3bbc82
diff -rup binutils.orig/bfd/elfxx-target.h binutils-2.30/bfd/elfxx-target.h
3bbc82
--- binutils.orig/bfd/elfxx-target.h	2020-02-12 13:31:20.338605611 +0000
3bbc82
+++ binutils-2.30/bfd/elfxx-target.h	2020-02-12 13:31:33.834507560 +0000
3bbc82
@@ -737,7 +737,7 @@
3bbc82
 #endif
3bbc82
 
3bbc82
 #ifndef elf_backend_copy_special_section_fields
3bbc82
-#define elf_backend_copy_special_section_fields NULL
3bbc82
+#define elf_backend_copy_special_section_fields _bfd_elf_copy_special_section_fields
3bbc82
 #endif
3bbc82
 
3bbc82
 #ifndef elf_backend_compact_eh_encoding
3bbc82
@@ -745,7 +745,23 @@
3bbc82
 #endif
3bbc82
 
3bbc82
 #ifndef elf_backend_cant_unwind_opcode
3bbc82
-#define elf_backend_cant_unwind_opcode 0
3bbc82
+#define elf_backend_cant_unwind_opcode NULL
3bbc82
+#endif
3bbc82
+
3bbc82
+#ifndef elf_backend_init_secondary_reloc_section
3bbc82
+#define elf_backend_init_secondary_reloc_section _bfd_elf_init_secondary_reloc_section
3bbc82
+#endif
3bbc82
+
3bbc82
+#ifndef elf_backend_slurp_secondary_reloc_section
3bbc82
+#define elf_backend_slurp_secondary_reloc_section _bfd_elf_slurp_secondary_reloc_section
3bbc82
+#endif
3bbc82
+
3bbc82
+#ifndef elf_backend_write_secondary_reloc_section
3bbc82
+#define elf_backend_write_secondary_reloc_section _bfd_elf_write_secondary_reloc_section
3bbc82
+#endif
3bbc82
+
3bbc82
+#ifndef elf_backend_symbol_section_index
3bbc82
+#define elf_backend_symbol_section_index _bfd_elf_symbol_section_index
3bbc82
 #endif
3bbc82
 
3bbc82
 #ifndef elf_match_priority
3bbc82
@@ -870,6 +886,10 @@ static struct elf_backend_data elfNN_bed
3bbc82
   elf_backend_setup_gnu_properties,
3bbc82
   elf_backend_compact_eh_encoding,
3bbc82
   elf_backend_cant_unwind_opcode,
3bbc82
+  elf_backend_init_secondary_reloc_section,
3bbc82
+  elf_backend_slurp_secondary_reloc_section,
3bbc82
+  elf_backend_write_secondary_reloc_section,
3bbc82
+  elf_backend_symbol_section_index,
3bbc82
   elf_backend_static_tls_alignment,
3bbc82
   elf_backend_stack_align,
3bbc82
   elf_backend_strtab_flags,
3bbc82
Only in binutils-2.30/bfd: elfxx-target.h.orig