Blame SOURCES/binutils-objcopy-set-section-flags-shared.patch

2e4410
diff -rup binutils.orig/binutils/doc/binutils.texi binutils-2.30/binutils/doc/binutils.texi
2e4410
--- binutils.orig/binutils/doc/binutils.texi	2020-04-07 16:11:52.164358203 +0100
2e4410
+++ binutils-2.30/binutils/doc/binutils.texi	2020-04-07 16:14:37.759171936 +0100
2e4410
@@ -1586,7 +1586,9 @@ recognized names are @samp{alloc}, @samp
2e4410
 for a section which does not have contents, but it is not meaningful
2e4410
 to clear the @samp{contents} flag of a section which does have
2e4410
 contents--just remove the section instead.  Not all flags are
2e4410
-meaningful for all object file formats.
2e4410
+meaningful for all object file formats.  In particular the
2e4410
+@samp{share} flag is only meaningful for COFF format files and not for
2e4410
+ELF format files.
2e4410
 
2e4410
 @item --add-section @var{sectionname}=@var{filename}
2e4410
 Add a new section named @var{sectionname} while copying the file.  The
2e4410
@@ -1637,7 +1639,8 @@ Rename a section from @var{oldname} to @
2e4410
 changing the section's flags to @var{flags} in the process.  This has
2e4410
 the advantage over using a linker script to perform the rename in that
2e4410
 the output stays as an object file and does not become a linked
2e4410
-executable.
2e4410
+executable.  This option accepts the same set of flags as the
2e4410
+@option{--sect-section-flags} option.
2e4410
 
2e4410
 This option is particularly helpful when the input format is binary,
2e4410
 since this will always create a section called .data.  If for example,
2e4410
diff -rup binutils.orig/binutils/objcopy.c binutils-2.30/binutils/objcopy.c
2e4410
--- binutils.orig/binutils/objcopy.c	2020-04-07 16:11:52.177358110 +0100
2e4410
+++ binutils-2.30/binutils/objcopy.c	2020-04-07 16:16:15.736470047 +0100
2e4410
@@ -2514,6 +2514,23 @@ merge_gnu_build_notes (bfd *          ab
2e4410
   return size;
2e4410
 }
2e4410
 
2e4410
+static flagword
2e4410
+check_new_section_flags (flagword flags, bfd * abfd, const char * secname)
2e4410
+{
2e4410
+  /* Only set the SEC_COFF_SHARED flag on COFF files.
2e4410
+     The same bit value is used by ELF targets to indicate
2e4410
+     compressed sections, and setting that flag here breaks
2e4410
+     things.  */
2e4410
+  if ((flags & SEC_COFF_SHARED)
2e4410
+      && bfd_get_flavour (abfd) != bfd_target_coff_flavour)
2e4410
+    {
2e4410
+      non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"),
2e4410
+		 bfd_get_filename (abfd), secname);
2e4410
+      flags &= ~ SEC_COFF_SHARED;
2e4410
+    }
2e4410
+  return flags;
2e4410
+}
2e4410
+
2e4410
 /* Copy object file IBFD onto OBFD.
2e4410
    Returns TRUE upon success, FALSE otherwise.  */
2e4410
 
2e4410
@@ -2755,7 +2772,10 @@ copy_object (bfd *ibfd, bfd *obfd, const
2e4410
 	  pset = find_section_list (padd->name, FALSE,
2e4410
 				    SECTION_CONTEXT_SET_FLAGS);
2e4410
 	  if (pset != NULL)
2e4410
-	    flags = pset->flags | SEC_HAS_CONTENTS;
2e4410
+	    {	      
2e4410
+	      flags = pset->flags | SEC_HAS_CONTENTS;
2e4410
+	      flags = check_new_section_flags (flags, obfd, padd->name);
2e4410
+	    }
2e4410
 	  else
2e4410
 	    flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
2e4410
 
2e4410
@@ -3867,6 +3887,7 @@ setup_section (bfd *ibfd, sec_ptr isecti
2e4410
   flagword flags;
2e4410
   const char *err;
2e4410
   const char * name;
2e4410
+  const char * new_name;
2e4410
   char *prefix = NULL;
2e4410
   bfd_boolean make_nobits;
2e4410
 
2e4410
@@ -3876,7 +3897,12 @@ setup_section (bfd *ibfd, sec_ptr isecti
2e4410
   /* Get the, possibly new, name of the output section.  */
2e4410
   name = bfd_section_name (ibfd, isection);
2e4410
   flags = bfd_get_section_flags (ibfd, isection);
2e4410
-  name = find_section_rename (name, &flags);
2e4410
+  new_name = find_section_rename (name, &flags);
2e4410
+  if (new_name != name)
2e4410
+    {
2e4410
+      name = new_name;
2e4410
+      flags = check_new_section_flags (flags, obfd, name);
2e4410
+    }
2e4410
 
2e4410
   /* Prefix sections.  */
2e4410
   if ((prefix_alloc_sections_string)
2e4410
@@ -3900,7 +3926,10 @@ setup_section (bfd *ibfd, sec_ptr isecti
2e4410
   p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
2e4410
 			 SECTION_CONTEXT_SET_FLAGS);
2e4410
   if (p != NULL)
2e4410
-    flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2e4410
+    {
2e4410
+      flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2e4410
+      flags = check_new_section_flags (flags, obfd, bfd_section_name (ibfd, isection));
2e4410
+    }
2e4410
   else if (strip_symbols == STRIP_NONDEBUG
2e4410
 	   && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
2e4410
 	   && !is_nondebug_keep_contents_section (ibfd, isection))