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

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