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

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