From b9846b82c5add805e468c00cf1dd951038889478 Mon Sep 17 00:00:00 2001
From: Nick Clifton <nickc@redhat.com>
Date: Fri, 8 Dec 2017 08:40:01 +0000
Subject: [PATCH] Stop strip from crashing when deleteing relocs in a file with
 annobin notes. Resolves: #1520805

---
 binutils-strip-delete-relocs.patch | 73 ++++++++++++++++++++++++++++++
 binutils.spec                      | 10 +++-
 2 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 binutils-strip-delete-relocs.patch

diff --git a/binutils-strip-delete-relocs.patch b/binutils-strip-delete-relocs.patch
new file mode 100644
index 0000000..798d192
--- /dev/null
+++ b/binutils-strip-delete-relocs.patch
@@ -0,0 +1,73 @@
+diff -rup binutils.orig/bfd/elf64-sparc.c binutils-2.29.1/bfd/elf64-sparc.c
+--- binutils.orig/bfd/elf64-sparc.c	2017-12-07 17:23:06.764174312 +0000
++++ binutils-2.29.1/bfd/elf64-sparc.c	2017-12-07 17:23:20.540009839 +0000
+@@ -287,6 +287,7 @@ elf64_sparc_set_reloc (bfd *abfd ATTRIBU
+                        unsigned int count)
+ {
+   asect->orelocation = location;
++  asect->reloc_count = count / 2;
+   canon_reloc_count (asect) = count;
+ }
+ 
+Only in binutils-2.29.1/bfd: elf64-sparc.c.orig
+diff -rup binutils.orig/bfd/elfcode.h binutils-2.29.1/bfd/elfcode.h
+--- binutils.orig/bfd/elfcode.h	2017-12-07 17:23:06.722174814 +0000
++++ binutils-2.29.1/bfd/elfcode.h	2017-12-07 17:23:20.541009827 +0000
+@@ -958,6 +958,12 @@ elf_write_relocs (bfd *abfd, asection *s
+ 	  return;
+ 	}
+ 
++      if (ptr->howto == NULL)
++	{
++	  *failedp = TRUE;
++	  return;
++	}
++
+       src_rela.r_offset = ptr->address + addr_offset;
+       src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
+       src_rela.r_addend = ptr->addend;
+diff -rup binutils.orig/binutils/objcopy.c binutils-2.29.1/binutils/objcopy.c
+--- binutils.orig/binutils/objcopy.c	2017-12-07 17:23:06.769174253 +0000
++++ binutils-2.29.1/binutils/objcopy.c	2017-12-07 17:23:20.542009815 +0000
+@@ -3785,14 +3785,24 @@ copy_relocations_in_section (bfd *ibfd,
+     }
+   else
+     {
+-      relpp = (arelent **) xmalloc (relsize);
+-      relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
+-      if (relcount < 0)
++      if (isection->orelocation != NULL)
+ 	{
+-	  status = 1;
+-	  bfd_nonfatal_message (NULL, ibfd, isection,
+-				_("relocation count is negative"));
+-	  return;
++	  /* Some other function has already set up the output relocs
++	     for us, so scan those instead of the default relocs.  */
++	  relcount = isection->reloc_count;
++	  relpp = isection->orelocation;
++	}
++      else
++	{
++	  relpp = (arelent **) xmalloc (relsize);
++	  relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
++	  if (relcount < 0)
++	    {
++	      status = 1;
++	      bfd_nonfatal_message (NULL, ibfd, isection,
++				    _("relocation count is negative"));
++	      return;
++	    }
+ 	}
+ 
+       if (strip_symbols == STRIP_ALL)
+@@ -3815,7 +3825,8 @@ copy_relocations_in_section (bfd *ibfd,
+ 		  temp_relpp [temp_relcount++] = relpp [i];
+ 	    }
+ 	  relcount = temp_relcount;
+-	  free (relpp);
++	  if (isection->orelocation == NULL)
++	    free (relpp);
+ 	  relpp = temp_relpp;
+ 	}
+ 
diff --git a/binutils.spec b/binutils.spec
index af11c22..a4bccf4 100644
--- a/binutils.spec
+++ b/binutils.spec
@@ -54,7 +54,7 @@
 Summary: A GNU collection of binary utilities
 Name: %{?cross}binutils%{?_with_debug:-debug}
 Version: 2.29.1
-Release: 6%{?dist}
+Release: 7%{?dist}
 License: GPLv3+
 Group: Development/Tools
 URL: http://sources.redhat.com/binutils
@@ -137,6 +137,11 @@ Patch09: binutils-2.29.1-readelf-use-dynamic.patch
 # FIXME:    Replace with a configure time option.
 Patch10: binutils-2.29-revert-PLT-elision.patch
 
+# Purpose:  Fixes a bug in strip/objcopy which could cause it to crash when
+#           deleting relocs in a file which also contains mergeable notes.
+# Lifetime: Fixed in 2.30.
+Patch11: binutils-strip-delete-relocs.patch
+
 #----------------------------------------------------------------------------
 
 Provides: bundled(libiberty)
@@ -668,6 +673,9 @@ exit 0
 
 #----------------------------------------------------------------------------
 %changelog
+* Thu Dec 07 2017 Nick Clifton  <nickc@redhat.com> 2.29.1-7
+- Stop strip from crashing when deleteing relocs in a file with annobin notes.  (#1520805)
+
 * Wed Dec 06 2017 Nick Clifton  <nickc@redhat.com> 2.29.1-6
 - Have readelf return an exit failure status when attempting to process an empty file. (#1522732)