Blame SOURCES/binutils-clearing-discarded-relocs.patch

d7faec
diff -rup binutils.orig/bfd/cofflink.c binutils-2.30/bfd/cofflink.c
d7faec
--- binutils.orig/bfd/cofflink.c	2021-06-15 15:38:31.578170486 +0100
d7faec
+++ binutils-2.30/bfd/cofflink.c	2021-06-15 15:59:12.394611963 +0100
d7faec
@@ -3084,8 +3084,8 @@ _bfd_coff_generic_relocate_section (bfd
d7faec
 	 then zero this reloc field.  */
d7faec
       if (sec != NULL && discarded_section (sec))
d7faec
 	{
d7faec
-	  _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
-			       contents + (rel->r_vaddr - input_section->vma));
d7faec
+	  (void) _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
+				      contents, (rel->r_vaddr - input_section->vma));
d7faec
 	  continue;
d7faec
 	}
d7faec
 
d7faec
diff -rup binutils.orig/bfd/dwarf2.c binutils-2.30/bfd/dwarf2.c
d7faec
--- binutils.orig/bfd/dwarf2.c	2021-06-15 15:38:31.597170370 +0100
d7faec
+++ binutils-2.30/bfd/dwarf2.c	2021-06-15 15:42:19.979779516 +0100
d7faec
@@ -2865,7 +2865,9 @@ find_abstract_instance_name (struct comp
d7faec
       info_ptr = unit->stash->info_ptr_memory;
d7faec
       info_ptr_end = unit->stash->info_ptr_end;
d7faec
       total = info_ptr_end - info_ptr;
d7faec
-      if (!die_ref || die_ref >= total)
d7faec
+      if (!die_ref)
d7faec
+	return TRUE;
d7faec
+      if (die_ref >= total)
d7faec
 	{
d7faec
 	  _bfd_error_handler
d7faec
 	    (_("Dwarf Error: Invalid abstract instance DIE ref."));
d7faec
diff -rup binutils.orig/bfd/elf-bfd.h binutils-2.30/bfd/elf-bfd.h
d7faec
--- binutils.orig/bfd/elf-bfd.h	2021-06-15 15:38:31.595170382 +0100
d7faec
+++ binutils-2.30/bfd/elf-bfd.h	2021-06-15 15:58:55.365715715 +0100
d7faec
@@ -2829,8 +2829,8 @@ extern asection _bfd_elf_large_com_secti
d7faec
 					howto, index, contents)		\
d7faec
   {									\
d7faec
     int i_;								\
d7faec
-    _bfd_clear_contents (howto, input_bfd, input_section,		\
d7faec
-			 contents + rel[index].r_offset);		\
d7faec
+    (void) _bfd_clear_contents (howto, input_bfd, input_section,	\
d7faec
+				contents, rel[index].r_offset);		\
d7faec
 									\
d7faec
     if (bfd_link_relocatable (info)					\
d7faec
 	&& (input_section->flags & SEC_DEBUGGING))			\
d7faec
diff -rup binutils.orig/bfd/elf32-arc.c binutils-2.30/bfd/elf32-arc.c
d7faec
--- binutils.orig/bfd/elf32-arc.c	2021-06-15 15:38:31.579170480 +0100
d7faec
+++ binutils-2.30/bfd/elf32-arc.c	2021-06-15 15:58:28.869877138 +0100
d7faec
@@ -1532,8 +1532,8 @@ elf_arc_relocate_section (bfd *			  outp
d7faec
       /* Clean relocs for symbols in discarded sections.  */
d7faec
       if (sec != NULL && discarded_section (sec))
d7faec
 	{
d7faec
-	  _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
-			       contents + rel->r_offset);
d7faec
+	  (void) _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
+				      contents, rel->r_offset);
d7faec
 	  rel->r_offset = rel->r_offset;
d7faec
 	  rel->r_info = 0;
d7faec
 	  rel->r_addend = 0;
d7faec
diff -rup binutils.orig/bfd/elf32-i386.c binutils-2.30/bfd/elf32-i386.c
d7faec
--- binutils.orig/bfd/elf32-i386.c	2021-06-15 15:38:31.578170486 +0100
d7faec
+++ binutils-2.30/bfd/elf32-i386.c	2021-06-15 15:58:12.694975692 +0100
d7faec
@@ -2165,8 +2165,8 @@ elf_i386_relocate_section (bfd *output_b
d7faec
 
d7faec
       if (sec != NULL && discarded_section (sec))
d7faec
 	{
d7faec
-	  _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
-			       contents + rel->r_offset);
d7faec
+	  (void) _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
+				      contents, rel->r_offset);
d7faec
 	  wrel->r_offset = rel->r_offset;
d7faec
 	  wrel->r_info = 0;
d7faec
 	  wrel->r_addend = 0;
d7faec
diff -rup binutils.orig/bfd/elf32-metag.c binutils-2.30/bfd/elf32-metag.c
d7faec
--- binutils.orig/bfd/elf32-metag.c	2021-06-15 15:38:31.593170394 +0100
d7faec
+++ binutils-2.30/bfd/elf32-metag.c	2021-06-15 15:57:48.039125909 +0100
d7faec
@@ -1392,8 +1392,8 @@ metag_final_link_relocate (reloc_howto_t
d7faec
 #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
d7faec
 					      rel, relend, howto, contents) \
d7faec
   {									\
d7faec
-    _bfd_clear_contents (howto, input_bfd, input_section,		\
d7faec
-			 contents + rel->r_offset);			\
d7faec
+    (void) _bfd_clear_contents (howto, input_bfd, input_section,		\
d7faec
+				contents, rel->r_offset);		\
d7faec
 									\
d7faec
     if (bfd_link_relocatable (info)					\
d7faec
 	&& (input_section->flags & SEC_DEBUGGING))			\
d7faec
diff -rup binutils.orig/bfd/elf32-nds32.c binutils-2.30/bfd/elf32-nds32.c
d7faec
--- binutils.orig/bfd/elf32-nds32.c	2021-06-15 15:38:31.589170419 +0100
d7faec
+++ binutils-2.30/bfd/elf32-nds32.c	2021-06-15 15:56:30.184600239 +0100
d7faec
@@ -12771,18 +12771,17 @@ nds32_elf_get_relocated_section_contents
d7faec
 	  symbol = *(*parent)->sym_ptr_ptr;
d7faec
 	  if (symbol->section && discarded_section (symbol->section))
d7faec
 	    {
d7faec
-	      bfd_byte *p;
d7faec
+	      bfd_vma off;
d7faec
 	      static reloc_howto_type none_howto
d7faec
 		= HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
d7faec
 			 "unused", FALSE, 0, 0, FALSE);
d7faec
 
d7faec
-	      p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
d7faec
-	      _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
d7faec
-				   p);
d7faec
-	      (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
d7faec
+	      off = (*parent)->address * bfd_octets_per_byte (input_bfd);
d7faec
+	      r = _bfd_clear_contents ((*parent)->howto, input_bfd,
d7faec
+				   input_section, data, off);
d7faec
+ 	      (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
d7faec
 	      (*parent)->addend = 0;
d7faec
 	      (*parent)->howto = &none_howto;
d7faec
-	      r = bfd_reloc_ok;
d7faec
 	    }
d7faec
 	  else
d7faec
 	    r = bfd_perform_relocation (input_bfd, *parent, data,
d7faec
diff -rup binutils.orig/bfd/elf32-ppc.c binutils-2.30/bfd/elf32-ppc.c
d7faec
--- binutils.orig/bfd/elf32-ppc.c	2021-06-15 15:38:31.597170370 +0100
d7faec
+++ binutils-2.30/bfd/elf32-ppc.c	2021-06-15 15:56:39.367544293 +0100
d7faec
@@ -7778,8 +7778,8 @@ ppc_elf_relocate_section (bfd *output_bf
d7faec
 	  if (r_type < R_PPC_max)
d7faec
 	    howto = ppc_elf_howto_table[r_type];
d7faec
 
d7faec
-	  _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
-			       contents + rel->r_offset);
d7faec
+	  (void) _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
+				      contents, rel->r_offset);
d7faec
 	  wrel->r_offset = rel->r_offset;
d7faec
 	  wrel->r_info = 0;
d7faec
 	  wrel->r_addend = 0;
d7faec
diff -rup binutils.orig/bfd/elf32-visium.c binutils-2.30/bfd/elf32-visium.c
d7faec
--- binutils.orig/bfd/elf32-visium.c	2021-06-15 15:38:31.580170473 +0100
d7faec
+++ binutils-2.30/bfd/elf32-visium.c	2021-06-15 15:57:29.271240254 +0100
d7faec
@@ -616,8 +616,8 @@ visium_elf_relocate_section (bfd *output
d7faec
 	  /* For relocs against symbols from removed linkonce sections,
d7faec
 	     or sections discarded by a linker script, we just want the
d7faec
 	     section contents zeroed.  Avoid any special processing.  */
d7faec
-	  _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
-			       contents + rel->r_offset);
d7faec
+	  (void) _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
+				      contents, rel->r_offset);
d7faec
 
d7faec
 	  rel->r_info = 0;
d7faec
 	  rel->r_addend = 0;
d7faec
diff -rup binutils.orig/bfd/elf64-ppc.c binutils-2.30/bfd/elf64-ppc.c
d7faec
--- binutils.orig/bfd/elf64-ppc.c	2021-06-15 15:38:31.578170486 +0100
d7faec
+++ binutils-2.30/bfd/elf64-ppc.c	2021-06-15 15:57:01.529409265 +0100
d7faec
@@ -13526,9 +13526,9 @@ ppc64_elf_relocate_section (bfd *output_
d7faec
 
d7faec
       if (sec != NULL && discarded_section (sec))
d7faec
 	{
d7faec
-	  _bfd_clear_contents (ppc64_elf_howto_table[r_type],
d7faec
-			       input_bfd, input_section,
d7faec
-			       contents + rel->r_offset);
d7faec
+	  (void) _bfd_clear_contents (ppc64_elf_howto_table[r_type],
d7faec
+				      input_bfd, input_section,
d7faec
+				      contents, rel->r_offset);
d7faec
 	  wrel->r_offset = rel->r_offset;
d7faec
 	  wrel->r_info = 0;
d7faec
 	  wrel->r_addend = 0;
d7faec
diff -rup binutils.orig/bfd/elf64-x86-64.c binutils-2.30/bfd/elf64-x86-64.c
d7faec
--- binutils.orig/bfd/elf64-x86-64.c	2021-06-15 15:38:31.585170443 +0100
d7faec
+++ binutils-2.30/bfd/elf64-x86-64.c	2021-06-15 15:57:09.831358693 +0100
d7faec
@@ -2457,8 +2457,8 @@ elf_x86_64_relocate_section (bfd *output
d7faec
 
d7faec
       if (sec != NULL && discarded_section (sec))
d7faec
 	{
d7faec
-	  _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
-			       contents + rel->r_offset);
d7faec
+	  (void) _bfd_clear_contents (howto, input_bfd, input_section,
d7faec
+				      contents, rel->r_offset);
d7faec
 	  wrel->r_offset = rel->r_offset;
d7faec
 	  wrel->r_info = 0;
d7faec
 	  wrel->r_addend = 0;
d7faec
diff -rup binutils.orig/bfd/libbfd-in.h binutils-2.30/bfd/libbfd-in.h
d7faec
--- binutils.orig/bfd/libbfd-in.h	2021-06-15 15:38:31.593170394 +0100
d7faec
+++ binutils-2.30/bfd/libbfd-in.h	2021-06-15 15:54:10.856449129 +0100
d7faec
@@ -674,8 +674,9 @@ extern bfd_reloc_status_type _bfd_reloca
d7faec
   (reloc_howto_type *, bfd *, bfd_vma, bfd_byte *);
d7faec
 
d7faec
 /* Clear a given location using a given howto.  */
d7faec
-extern void _bfd_clear_contents (reloc_howto_type *howto, bfd *input_bfd,
d7faec
-				 asection *input_section, bfd_byte *location);
d7faec
+extern bfd_reloc_status_type _bfd_clear_contents
d7faec
+  (reloc_howto_type *howto, bfd *input_bfd,
d7faec
+   asection *input_section, bfd_byte *, bfd_vma);
d7faec
 
d7faec
 /* Link stabs in sections in the first pass.  */
d7faec
 
d7faec
diff -rup binutils.orig/bfd/libbfd.h binutils-2.30/bfd/libbfd.h
d7faec
--- binutils.orig/bfd/libbfd.h	2021-06-15 15:38:31.581170467 +0100
d7faec
+++ binutils-2.30/bfd/libbfd.h	2021-06-15 15:53:55.863540475 +0100
d7faec
@@ -679,8 +679,9 @@ extern bfd_reloc_status_type _bfd_reloca
d7faec
   (reloc_howto_type *, bfd *, bfd_vma, bfd_byte *);
d7faec
 
d7faec
 /* Clear a given location using a given howto.  */
d7faec
-extern void _bfd_clear_contents (reloc_howto_type *howto, bfd *input_bfd,
d7faec
-				 asection *input_section, bfd_byte *location);
d7faec
+extern bfd_reloc_status_type _bfd_clear_contents
d7faec
+  (reloc_howto_type *howto, bfd *input_bfd,
d7faec
+   asection *input_section, bfd_byte *, bfd_vma);
d7faec
 
d7faec
 /* Link stabs in sections in the first pass.  */
d7faec
 
d7faec
diff -rup binutils.orig/bfd/reloc.c binutils-2.30/bfd/reloc.c
d7faec
--- binutils.orig/bfd/reloc.c	2021-06-15 15:38:31.593170394 +0100
d7faec
+++ binutils-2.30/bfd/reloc.c	2021-06-15 15:51:59.449249747 +0100
d7faec
@@ -1604,23 +1604,29 @@ _bfd_relocate_contents (reloc_howto_type
d7faec
    relocations against discarded symbols, to make ignorable debug or unwind
d7faec
    information more obvious.  */
d7faec
 
d7faec
-void
d7faec
+bfd_reloc_status_type
d7faec
 _bfd_clear_contents (reloc_howto_type *howto,
d7faec
 		     bfd *input_bfd,
d7faec
 		     asection *input_section,
d7faec
-		     bfd_byte *location)
d7faec
+		     bfd_byte *buf,
d7faec
+		     bfd_vma off)
d7faec
 {
d7faec
   int size;
d7faec
   bfd_vma x = 0;
d7faec
+  bfd_byte *location;
d7faec
+
d7faec
+  if (!bfd_reloc_offset_in_range (howto, input_bfd, input_section, off))
d7faec
+    return bfd_reloc_outofrange;
d7faec
 
d7faec
   /* Get the value we are going to relocate.  */
d7faec
-  size = bfd_get_reloc_size (howto);
d7faec
+  location = buf + off;
d7faec
+  size = bfd_get_reloc_size (howto); 
d7faec
   switch (size)
d7faec
     {
d7faec
     default:
d7faec
-      abort ();
d7faec
+      return bfd_reloc_notsupported;
d7faec
     case 0:
d7faec
-      return;
d7faec
+      return bfd_reloc_ok;
d7faec
     case 1:
d7faec
       x = bfd_get_8 (input_bfd, location);
d7faec
       break;
d7faec
@@ -1634,7 +1640,7 @@ _bfd_clear_contents (reloc_howto_type *h
d7faec
 #ifdef BFD64
d7faec
       x = bfd_get_64 (input_bfd, location);
d7faec
 #else
d7faec
-      abort ();
d7faec
+      return bfd_reloc_notsupported;
d7faec
 #endif
d7faec
       break;
d7faec
     }
d7faec
@@ -1654,7 +1660,7 @@ _bfd_clear_contents (reloc_howto_type *h
d7faec
     {
d7faec
     default:
d7faec
     case 0:
d7faec
-      abort ();
d7faec
+      return bfd_reloc_notsupported;
d7faec
     case 1:
d7faec
       bfd_put_8 (input_bfd, x, location);
d7faec
       break;
d7faec
@@ -1668,10 +1674,12 @@ _bfd_clear_contents (reloc_howto_type *h
d7faec
 #ifdef BFD64
d7faec
       bfd_put_64 (input_bfd, x, location);
d7faec
 #else
d7faec
-      abort ();
d7faec
+      return bfd_reloc_notsupported;
d7faec
 #endif
d7faec
       break;
d7faec
     }
d7faec
+
d7faec
+  return bfd_reloc_ok;
d7faec
 }
d7faec
 
d7faec
 /*
d7faec
@@ -8209,20 +8217,30 @@ bfd_generic_get_relocated_section_conten
d7faec
 	      goto error_return;
d7faec
 	    }
d7faec
 
d7faec
-	  if (symbol->section && discarded_section (symbol->section))
d7faec
+	  /* Zap reloc field when the symbol is from a discarded
d7faec
+	     section, ignoring any addend.  Do the same when called
d7faec
+	     from bfd_simple_get_relocated_section_contents for
d7faec
+	     undefined symbols in debug sections.  This is to keep
d7faec
+	     debug info reasonably sane, in particular so that
d7faec
+	     DW_FORM_ref_addr to another file's .debug_info isn't
d7faec
+	     confused with an offset into the current file's
d7faec
+	     .debug_info.  */
d7faec
+	  if ((symbol->section != NULL && discarded_section (symbol->section))
d7faec
+	      || (symbol->section == bfd_und_section_ptr
d7faec
+		  && (input_section->flags & SEC_DEBUGGING) != 0
d7faec
+		  && link_info->input_bfds == link_info->output_bfd))
d7faec
 	    {
d7faec
-	      bfd_byte *p;
d7faec
+	      bfd_vma off;
d7faec
 	      static reloc_howto_type none_howto
d7faec
 		= HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
d7faec
 			 "unused", FALSE, 0, 0, FALSE);
d7faec
 
d7faec
-	      p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
d7faec
-	      _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
d7faec
-				   p);
d7faec
+	      off = (*parent)->address * bfd_octets_per_byte (input_bfd);
d7faec
+	      r = _bfd_clear_contents ((*parent)->howto, input_bfd,
d7faec
+				       input_section, data, off);
d7faec
 	      (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
d7faec
 	      (*parent)->addend = 0;
d7faec
 	      (*parent)->howto = &none_howto;
d7faec
-	      r = bfd_reloc_ok;
d7faec
 	    }
d7faec
 	  else
d7faec
 	    r = bfd_perform_relocation (input_bfd,