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

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