13ae24
diff -rup binutils-2.26.orig/bfd/bfd.c binutils-2.26/bfd/bfd.c
13ae24
--- binutils-2.26.orig/bfd/bfd.c	2016-02-19 09:35:36.231000625 +0000
13ae24
+++ binutils-2.26/bfd/bfd.c	2016-02-19 09:35:54.901106498 +0000
13ae24
@@ -57,6 +57,14 @@ CODE_FRAGMENT
13ae24
 .    bfd_byte data[1];
13ae24
 .  };
13ae24
 .
13ae24
+.enum bfd_lto_object_type
13ae24
+.  {
13ae24
+.    lto_non_object,
13ae24
+.    lto_non_ir_object,
13ae24
+.    lto_ir_object,
13ae24
+.    lto_mixed_object
13ae24
+.  };
13ae24
+.
13ae24
 .struct bfd
13ae24
 .{
13ae24
 .  {* The filename the application opened the BFD with.  *}
13ae24
@@ -227,6 +235,9 @@ CODE_FRAGMENT
13ae24
 .  {* Set if this is a plugin output file.  *}
13ae24
 .  unsigned int lto_output : 1;
13ae24
 .
13ae24
+.  {* LTO object type.  *}
13ae24
+.  ENUM_BITFIELD (bfd_lto_object_type) lto_type : 2;
13ae24
+.
13ae24
 .  {* Set to dummy BFD created when claimed by a compiler plug-in
13ae24
 .     library.  *}
13ae24
 .  bfd *plugin_dummy_bfd;
13ae24
@@ -252,6 +263,9 @@ CODE_FRAGMENT
13ae24
 .  {* The last section on the section list.  *}
13ae24
 .  struct bfd_section *section_last;
13ae24
 .
13ae24
+.  {* The object-only section on the section list.  *}
13ae24
+.  struct bfd_section *object_only_section;
13ae24
+.
13ae24
 .  {* The number of sections.  *}
13ae24
 .  unsigned int section_count;
13ae24
 .
13ae24
@@ -2270,3 +2284,36 @@ bfd_convert_section_contents (bfd *ibfd,
13ae24
   *ptr_size = size;
13ae24
   return TRUE;
13ae24
 }
13ae24
+
13ae24
+/*
13ae24
+FUNCTION
13ae24
+	bfd_group_signature
13ae24
+
13ae24
+SYNOPSIS
13ae24
+	asymbol *bfd_group_signature (asection *group, asymbol **isympp);
13ae24
+
13ae24
+DESCRIPTION
13ae24
+	Return a pointer to the symbol used as a signature for GROUP.
13ae24
+*/
13ae24
+
13ae24
+asymbol *
13ae24
+bfd_group_signature (asection *group, asymbol **isympp)
13ae24
+{
13ae24
+  bfd *abfd = group->owner;
13ae24
+  Elf_Internal_Shdr *ghdr;
13ae24
+
13ae24
+  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13ae24
+    return NULL;
13ae24
+
13ae24
+  ghdr = &elf_section_data (group)->this_hdr;
13ae24
+  if (ghdr->sh_link < elf_numsections (abfd))
13ae24
+    {
13ae24
+      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13ae24
+      Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
13ae24
+
13ae24
+      if (symhdr->sh_type == SHT_SYMTAB
13ae24
+	  && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
13ae24
+	return isympp[ghdr->sh_info - 1];
13ae24
+    }
13ae24
+  return NULL;
13ae24
+}
13ae24
diff -rup binutils-2.26.orig/bfd/bfd-in2.h binutils-2.26/bfd/bfd-in2.h
13ae24
--- binutils-2.26.orig/bfd/bfd-in2.h	2016-02-19 09:35:36.280000903 +0000
13ae24
+++ binutils-2.26/bfd/bfd-in2.h	2016-02-19 09:35:54.900106492 +0000
13ae24
@@ -1654,6 +1657,9 @@ extern asection _bfd_std_section[4];
13ae24
 #define BFD_COM_SECTION_NAME "*COM*"
13ae24
 #define BFD_IND_SECTION_NAME "*IND*"
13ae24
 
13ae24
+/* GNU object-only section name.  */
13ae24
+#define GNU_OBJECT_ONLY_SECTION_NAME ".gnu_object_only"
13ae24
+
13ae24
 /* Pointer to the common section.  */
13ae24
 #define bfd_com_section_ptr (&_bfd_std_section[0])
13ae24
 /* Pointer to the undefined section.  */
13ae24
@@ -6511,6 +6517,14 @@ struct bfd_build_id
13ae24
     bfd_byte data[1];
13ae24
   };
13ae24
 
13ae24
+enum bfd_lto_object_type
13ae24
+  {
13ae24
+    lto_non_object,
13ae24
+    lto_non_ir_object,
13ae24
+    lto_ir_object,
13ae24
+    lto_mixed_object
13ae24
+  };
13ae24
+
13ae24
 struct bfd
13ae24
 {
13ae24
   /* The filename the application opened the BFD with.  */
13ae24
@@ -6681,6 +6695,9 @@ struct bfd
13ae24
   /* Set if this is a plugin output file.  */
13ae24
   unsigned int lto_output : 1;
13ae24
 
13ae24
+  /* LTO object type.  */
13ae24
+  ENUM_BITFIELD (bfd_lto_object_type) lto_type : 2;
13ae24
+
13ae24
   /* Set to dummy BFD created when claimed by a compiler plug-in
13ae24
      library.  */
13ae24
   bfd *plugin_dummy_bfd;
13ae24
@@ -6706,6 +6723,9 @@ struct bfd
13ae24
   /* The last section on the section list.  */
13ae24
   struct bfd_section *section_last;
13ae24
 
13ae24
+  /* The object-only section on the section list.  */
13ae24
+  struct bfd_section *object_only_section;
13ae24
+
13ae24
   /* The number of sections.  */
13ae24
   unsigned int section_count;
13ae24
 
13ae24
@@ -7026,6 +7046,8 @@ bfd_boolean bfd_convert_section_contents
13ae24
    (bfd *ibfd, asection *isec, bfd *obfd,
13ae24
     bfd_byte **ptr, bfd_size_type *ptr_size);
13ae24
 
13ae24
+asymbol *bfd_group_signature (asection *group, asymbol **isympp);
13ae24
+
13ae24
 /* Extracted from archive.c.  */
13ae24
 symindex bfd_get_next_mapent
13ae24
    (bfd *abfd, symindex previous, carsym **sym);
13ae24
diff -rup binutils-2.26.orig/bfd/elf.c binutils-2.26/bfd/elf.c
13ae24
--- binutils-2.26.orig/bfd/elf.c	2016-02-19 09:35:36.280000903 +0000
13ae24
+++ binutils-2.26/bfd/elf.c	2016-02-19 09:35:54.904106515 +0000
13ae24
@@ -2387,6 +2387,7 @@ static const struct bfd_elf_special_sect
13ae24
   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
13ae24
   { STRING_COMMA_LEN (".gnu.lto_"),	  -1, SHT_PROGBITS,    SHF_EXCLUDE },
13ae24
   { STRING_COMMA_LEN (".got"),		   0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
13ae24
+  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
13ae24
   { STRING_COMMA_LEN (".gnu.version"),	   0, SHT_GNU_versym,  0 },
13ae24
   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
13ae24
   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
13ae24
diff -rup binutils-2.26.orig/bfd/format.c binutils-2.26/bfd/format.c
13ae24
--- binutils-2.26.orig/bfd/format.c	2016-02-19 09:35:36.265000818 +0000
13ae24
+++ binutils-2.26/bfd/format.c	2016-02-19 09:36:21.420256880 +0000
13ae24
@@ -46,6 +46,9 @@ SUBSECTION
13ae24
 #include "sysdep.h"
13ae24
 #include "bfd.h"
13ae24
 #include "libbfd.h"
13ae24
+#if BFD_SUPPORTS_PLUGINS
13ae24
+#include "plugin.h"
13ae24
+#endif
13ae24
 
13ae24
 /* IMPORT from targets.c.  */
13ae24
 extern const size_t _bfd_target_vector_entries;
13ae24
@@ -179,6 +182,33 @@ bfd_preserve_finish (bfd *abfd ATTRIBUTE
13ae24
   preserve->marker = NULL;
13ae24
 }
13ae24
 
13ae24
+/* Set lto_type in ABFD.  */
13ae24
+
13ae24
+static void
13ae24
+bfd_set_lto_type (bfd *abfd)
13ae24
+{
13ae24
+  if (abfd->format == bfd_object
13ae24
+      && abfd->lto_type == lto_non_object
13ae24
+      && (abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13ae24
+    {
13ae24
+      asection *sec;
13ae24
+      enum bfd_lto_object_type type = lto_non_ir_object;
13ae24
+      for (sec = abfd->sections; sec != NULL; sec = sec->next)
13ae24
+	{
13ae24
+	  if (strcmp (sec->name, GNU_OBJECT_ONLY_SECTION_NAME) == 0)
13ae24
+	    {
13ae24
+	      type = lto_mixed_object;
13ae24
+	      abfd->object_only_section = sec;
13ae24
+	      break;
13ae24
+	    }
13ae24
+	  else if (type != lto_ir_object
13ae24
+		   && strncmp (sec->name, ".gnu.lto_", 9) == 0)
13ae24
+	    type = lto_ir_object;
13ae24
+	}
13ae24
+      abfd->lto_type = type;
13ae24
+    }
13ae24
+}
13ae24
+
13ae24
 /*
13ae24
 FUNCTION
13ae24
 	bfd_check_format_matches
13ae24
@@ -221,7 +251,10 @@ bfd_check_format_matches (bfd *abfd, bfd
13ae24
     }
13ae24
 
13ae24
   if (abfd->format != bfd_unknown)
13ae24
-    return abfd->format == format;
13ae24
+    {
13ae24
+      bfd_set_lto_type (abfd);
13ae24
+      return abfd->format == format;
13ae24
+    }
13ae24
 
13ae24
   if (matching != NULL || *bfd_associated_vector != NULL)
13ae24
     {
13ae24
@@ -285,6 +318,13 @@ bfd_check_format_matches (bfd *abfd, bfd
13ae24
 	  || (*target)->match_priority > best_match)
13ae24
 	continue;
13ae24
 
13ae24
+#if BFD_SUPPORTS_PLUGINS
13ae24
+      /* If the plugin target is explicitly specified when a BFD file
13ae24
+	 is opened, don't check it twice.  */
13ae24
+      if (bfd_plugin_specified_p () && bfd_plugin_target_p (*target))
13ae24
+	continue;
13ae24
+#endif
13ae24
+
13ae24
       /* If we already tried a match, the bfd is modified and may
13ae24
 	 have sections attached, which will confuse the next
13ae24
 	 _bfd_check_format call.  */
13ae24
@@ -449,6 +489,8 @@ bfd_check_format_matches (bfd *abfd, bfd
13ae24
       if (matching_vector)
13ae24
 	free (matching_vector);
13ae24
 
13ae24
+      bfd_set_lto_type (abfd);
13ae24
+
13ae24
       /* File position has moved, BTW.  */
13ae24
       return TRUE;
13ae24
     }
13ae24
diff -rup binutils-2.26.orig/bfd/plugin.c binutils-2.26/bfd/plugin.c
13ae24
--- binutils-2.26.orig/bfd/plugin.c	2016-02-19 09:35:36.271000852 +0000
13ae24
+++ binutils-2.26/bfd/plugin.c	2016-02-19 09:35:54.905106520 +0000
13ae24
@@ -130,6 +130,146 @@ register_claim_file (ld_plugin_claim_fil
13ae24
   return LDPS_OK;
13ae24
 }
13ae24
 
13ae24
+static asection bfd_plugin_fake_text_section
13ae24
+  = BFD_FAKE_SECTION (bfd_plugin_fake_text_section, 0, 0, ".text", 0);
13ae24
+static asection bfd_plugin_fake_common_section
13ae24
+  = BFD_FAKE_SECTION (bfd_plugin_fake_common_section, SEC_IS_COMMON, 0,
13ae24
+		      NULL, 0);
13ae24
+
13ae24
+/* Get symbols from object only section.  */
13ae24
+
13ae24
+static void
13ae24
+bfd_plugin_get_symbols_in_object_only (bfd *abfd)
13ae24
+{
13ae24
+  struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
13ae24
+  const char *object_only_file;
13ae24
+  bfd *nbfd;
13ae24
+  long storage;
13ae24
+  long object_only_nsyms, added_nsyms, i;
13ae24
+  asymbol **object_only_syms, **added_syms;
13ae24
+
13ae24
+  plugin_data->object_only_syms = NULL;
13ae24
+  plugin_data->object_only_nsyms = 0;
13ae24
+
13ae24
+  if (abfd->sections == NULL && abfd->my_archive == NULL)
13ae24
+    {
13ae24
+      nbfd = bfd_openr (abfd->filename, NULL);
13ae24
+      if (nbfd == NULL)
13ae24
+	{
13ae24
+	  (*_bfd_error_handler)
13ae24
+	    (_("%s: failed to open to extract object only section: %s"),
13ae24
+	     abfd->filename, bfd_errmsg (bfd_get_error ()));
13ae24
+	  goto error_return;
13ae24
+	}
13ae24
+      else if (!bfd_check_format (nbfd, bfd_object))
13ae24
+	{
13ae24
+	  /* There is no object only section if it isn't a bfd_object
13ae24
+	     file.  */
13ae24
+error_return:
13ae24
+	  bfd_close (nbfd);
13ae24
+	  return;
13ae24
+	}
13ae24
+    }
13ae24
+  else
13ae24
+    {
13ae24
+      if (!bfd_check_format (abfd, bfd_object))
13ae24
+	{
13ae24
+	  (*_bfd_error_handler)
13ae24
+	    (_("%B: invalid file to extract object only section: %s"),
13ae24
+	     abfd, bfd_errmsg (bfd_get_error ()));
13ae24
+	  return;
13ae24
+	}
13ae24
+      nbfd = abfd;
13ae24
+    }
13ae24
+
13ae24
+  if (nbfd->lto_type == lto_mixed_object
13ae24
+      && (nbfd->flags & HAS_SYMS) != 0)
13ae24
+    {
13ae24
+      object_only_file = bfd_extract_object_only_section (nbfd);
13ae24
+      if (object_only_file == NULL)
13ae24
+	(*_bfd_error_handler)
13ae24
+	  (_("%B: failed to extract object only section: %s"),
13ae24
+	   abfd, bfd_errmsg (bfd_get_error ()));
13ae24
+    }
13ae24
+  else
13ae24
+    object_only_file = NULL;
13ae24
+
13ae24
+  /* Close the new bfd we just opened.  */
13ae24
+  if (nbfd != abfd)
13ae24
+    bfd_close (nbfd);
13ae24
+
13ae24
+  /* Return if there is no object only section or there is no
13ae24
+     symbol in object only section.  */
13ae24
+  if (!object_only_file)
13ae24
+    return;
13ae24
+
13ae24
+  /* Open the file containing object only section.  */
13ae24
+  nbfd = bfd_openr (object_only_file, NULL);
13ae24
+  if (!bfd_check_format (nbfd, bfd_object))
13ae24
+    {
13ae24
+      (*_bfd_error_handler)
13ae24
+	(_("%B: failed to open object only section: %s"),
13ae24
+	 abfd, bfd_errmsg (bfd_get_error ()));
13ae24
+      goto quit;
13ae24
+    }
13ae24
+
13ae24
+  storage = bfd_get_symtab_upper_bound (nbfd);
13ae24
+  if (storage <= 0)
13ae24
+    {
13ae24
+      if (storage < 0)
13ae24
+	(*_bfd_error_handler)
13ae24
+	  (_("%B: failed to get symbol table in object only section: %s"),
13ae24
+	   abfd, bfd_errmsg (bfd_get_error ()));
13ae24
+
13ae24
+      goto quit;
13ae24
+    }
13ae24
+
13ae24
+  object_only_syms = (asymbol **) bfd_malloc (storage);
13ae24
+  object_only_nsyms = bfd_canonicalize_symtab (nbfd, object_only_syms);
13ae24
+
13ae24
+  /* FIXME: We waste some spaces if not all symbols are copied.  */
13ae24
+  added_syms = (asymbol **) bfd_alloc (abfd, storage);
13ae24
+  added_nsyms = 0;
13ae24
+
13ae24
+  /* Copy only global symbols from object only section.  */
13ae24
+  for (i = 0; i < object_only_nsyms; i++)
13ae24
+    {
13ae24
+      asection *sec = object_only_syms[i]->section;
13ae24
+      flagword flags = object_only_syms[i]->flags;
13ae24
+      asymbol *s;
13ae24
+
13ae24
+      if (bfd_is_com_section (sec))
13ae24
+	sec = &bfd_plugin_fake_common_section;
13ae24
+      else if (bfd_is_und_section (sec))
13ae24
+	;
13ae24
+      else if ((flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0)
13ae24
+	sec = &bfd_plugin_fake_text_section;
13ae24
+      else
13ae24
+	continue;
13ae24
+
13ae24
+      s = bfd_alloc (abfd, sizeof (asymbol));
13ae24
+      BFD_ASSERT (s);
13ae24
+      added_syms[added_nsyms++] = s;
13ae24
+
13ae24
+      s->section = sec;
13ae24
+      s->the_bfd = abfd;
13ae24
+      s->name = xstrdup (object_only_syms[i]->name);
13ae24
+      s->value = 0;
13ae24
+      s->flags = flags;
13ae24
+      s->udata.p = NULL;
13ae24
+    }
13ae24
+
13ae24
+  plugin_data->object_only_syms = added_syms;
13ae24
+  plugin_data->object_only_nsyms = added_nsyms;
13ae24
+
13ae24
+  free (object_only_syms);
13ae24
+
13ae24
+quit:
13ae24
+  /* Close and remove the object only section file.  */
13ae24
+  bfd_close (nbfd);
13ae24
+  unlink (object_only_file);
13ae24
+}
13ae24
+
13ae24
 static enum ld_plugin_status
13ae24
 add_symbols (void * handle,
13ae24
 	     int nsyms,
13ae24
@@ -142,10 +282,13 @@ add_symbols (void * handle,
13ae24
   plugin_data->nsyms = nsyms;
13ae24
   plugin_data->syms = syms;
13ae24
 
13ae24
-  if (nsyms != 0)
13ae24
+  abfd->tdata.plugin_data = plugin_data;
13ae24
+
13ae24
+  bfd_plugin_get_symbols_in_object_only (abfd);
13ae24
+
13ae24
+  if ((nsyms + plugin_data->object_only_nsyms) != 0)
13ae24
     abfd->flags |= HAS_SYMS;
13ae24
 
13ae24
-  abfd->tdata.plugin_data = plugin_data;
13ae24
   return LDPS_OK;
13ae24
 }
13ae24
 
13ae24
@@ -440,7 +583,8 @@ static long
13ae24
 bfd_plugin_get_symtab_upper_bound (bfd *abfd)
13ae24
 {
13ae24
   struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
13ae24
-  long nsyms = plugin_data->nsyms;
13ae24
+  /* Add symbols from object only section.  */
13ae24
+  long nsyms = plugin_data->nsyms + plugin_data->object_only_nsyms;
13ae24
 
13ae24
   BFD_ASSERT (nsyms >= 0);
13ae24
 
13ae24
@@ -474,12 +618,7 @@ bfd_plugin_canonicalize_symtab (bfd *abf
13ae24
   struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
13ae24
   long nsyms = plugin_data->nsyms;
13ae24
   const struct ld_plugin_symbol *syms = plugin_data->syms;
13ae24
-  static asection fake_section;
13ae24
-  static asection fake_common_section;
13ae24
-  int i;
13ae24
-
13ae24
-  fake_section.name = ".text";
13ae24
-  fake_common_section.flags = SEC_IS_COMMON;
13ae24
+  int i, j;
13ae24
 
13ae24
   for (i = 0; i < nsyms; i++)
13ae24
     {
13ae24
@@ -492,10 +631,11 @@ bfd_plugin_canonicalize_symtab (bfd *abf
13ae24
       s->name = syms[i].name;
13ae24
       s->value = 0;
13ae24
       s->flags = convert_flags (&syms[i]);
13ae24
+      s->udata.p = NULL;
13ae24
       switch (syms[i].def)
13ae24
 	{
13ae24
 	case LDPK_COMMON:
13ae24
-	  s->section = &fake_common_section;
13ae24
+	  s->section = &bfd_plugin_fake_common_section;
13ae24
 	  break;
13ae24
 	case LDPK_UNDEF:
13ae24
 	case LDPK_WEAKUNDEF:
13ae24
@@ -503,15 +643,18 @@ bfd_plugin_canonicalize_symtab (bfd *abf
13ae24
 	  break;
13ae24
 	case LDPK_DEF:
13ae24
 	case LDPK_WEAKDEF:
13ae24
-	  s->section = &fake_section;
13ae24
+	  s->section = &bfd_plugin_fake_text_section;
13ae24
 	  break;
13ae24
 	default:
13ae24
 	  BFD_ASSERT (0);
13ae24
 	}
13ae24
-
13ae24
-      s->udata.p = (void *) &syms[i];
13ae24
     }
13ae24
 
13ae24
+  /* Copy symbols from object only section.  */
13ae24
+  nsyms += plugin_data->object_only_nsyms;
13ae24
+  for (j = 0; j < plugin_data->object_only_nsyms; j++, i++)
13ae24
+    alocation[i] = plugin_data->object_only_syms[j];
13ae24
+
13ae24
   return nsyms;
13ae24
 }
13ae24
 
13ae24
diff -rup binutils-2.26.orig/bfd/plugin.h binutils-2.26/bfd/plugin.h
13ae24
--- binutils-2.26.orig/bfd/plugin.h	2016-02-19 09:35:36.271000852 +0000
13ae24
+++ binutils-2.26/bfd/plugin.h	2016-02-19 09:35:54.906106526 +0000
13ae24
@@ -33,6 +33,8 @@ typedef struct plugin_data_struct
13ae24
 {
13ae24
   int nsyms;
13ae24
   const struct ld_plugin_symbol *syms;
13ae24
+  int object_only_nsyms;
13ae24
+  asymbol **object_only_syms;
13ae24
 }
13ae24
 plugin_data_struct;
13ae24
 
13ae24
diff -rup binutils-2.26.orig/bfd/section.c binutils-2.26/bfd/section.c
13ae24
--- binutils-2.26.orig/bfd/section.c	2016-02-19 09:35:36.276000881 +0000
13ae24
+++ binutils-2.26/bfd/section.c	2016-02-19 09:35:54.906106526 +0000
13ae24
@@ -588,6 +588,9 @@ CODE_FRAGMENT
13ae24
 .#define BFD_COM_SECTION_NAME "*COM*"
13ae24
 .#define BFD_IND_SECTION_NAME "*IND*"
13ae24
 .
13ae24
+.{* GNU object-only section name.  *}
13ae24
+.#define GNU_OBJECT_ONLY_SECTION_NAME ".gnu_object_only"
13ae24
+.
13ae24
 .{* Pointer to the common section.  *}
13ae24
 .#define bfd_com_section_ptr (&_bfd_std_section[0])
13ae24
 .{* Pointer to the undefined section.  *}
13ae24
diff -rup binutils-2.26.orig/binutils/readelf.c binutils-2.26/binutils/readelf.c
13ae24
--- binutils-2.26.orig/binutils/readelf.c	2016-02-19 09:35:36.295000988 +0000
13ae24
+++ binutils-2.26/binutils/readelf.c	2016-02-19 09:35:54.911106554 +0000
13ae24
@@ -3900,6 +3900,7 @@ get_section_type_name (unsigned int sh_t
13ae24
     case 0x7ffffffd:		return "AUXILIARY";
13ae24
     case 0x7fffffff:		return "FILTER";
13ae24
     case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
13ae24
+    case SHT_GNU_OBJECT_ONLY:	return "GNU_OBJECT_ONLY";
13ae24
 
13ae24
     default:
13ae24
       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
13ae24
diff -rup binutils-2.26.orig/gas/testsuite/gas/elf/section9.d binutils-2.26/gas/testsuite/gas/elf/section9.d
13ae24
--- binutils-2.26.orig/gas/testsuite/gas/elf/section9.d	2016-02-19 09:35:36.397001567 +0000
13ae24
+++ binutils-2.26/gas/testsuite/gas/elf/section9.d	2016-02-19 09:35:54.912106560 +0000
13ae24
@@ -4,4 +4,5 @@
13ae24
 #...
13ae24
 [ 	]*\[.*\][ 	]+\.gnu\.lto_main[ 	]+PROGBITS.*[ 	]+E[   ]+.*
13ae24
 [ 	]*\[.*\][ 	]+\.gnu\.lto_\.pureconst[ 	]+PROGBITS.*[ 	]+E[   ]+.*
13ae24
+[ 	]*\[.*\][ 	]+\.gnu_object_only[ 	]+GNU_OBJECT_ONLY.*[ 	]+E[   ]+.*
13ae24
 #pass
13ae24
diff -rup binutils-2.26.orig/gas/testsuite/gas/elf/section9.s binutils-2.26/gas/testsuite/gas/elf/section9.s
13ae24
--- binutils-2.26.orig/gas/testsuite/gas/elf/section9.s	2016-02-19 09:35:36.397001567 +0000
13ae24
+++ binutils-2.26/gas/testsuite/gas/elf/section9.s	2016-02-19 09:35:54.912106560 +0000
13ae24
@@ -2,3 +2,5 @@
13ae24
 	.byte 0,0,0,0
13ae24
 	.section .gnu.lto_.pureconst,"",%progbits
13ae24
 	.byte 0,0,0,0
13ae24
+	.section .gnu_object_only
13ae24
+	.byte 0,0,0,0
13ae24
diff -rup binutils-2.26.orig/include/bfdlink.h binutils-2.26/include/bfdlink.h
13ae24
--- binutils-2.26.orig/include/bfdlink.h	2016-02-19 09:35:36.672003126 +0000
13ae24
+++ binutils-2.26/include/bfdlink.h	2016-02-19 09:35:54.913106566 +0000
13ae24
@@ -400,6 +400,12 @@ struct bfd_link_info
13ae24
   /* TRUE if ok to have multiple definition.  */
13ae24
   unsigned int allow_multiple_definition: 1;
13ae24
 
13ae24
+  /* TRUE if .gnu_object_only section should be created.  */
13ae24
+  unsigned int emit_gnu_object_only: 1;
13ae24
+
13ae24
+  /* TRUE if .gnu_object_only section is being created.  */
13ae24
+  unsigned int emitting_gnu_object_only: 1;
13ae24
+
13ae24
   /* TRUE if ok to have version with no definition.  */
13ae24
   unsigned int allow_undefined_version: 1;
13ae24
 
13ae24
diff -rup binutils-2.26.orig/include/elf/common.h binutils-2.26/include/elf/common.h
13ae24
--- binutils-2.26.orig/include/elf/common.h	2016-02-19 09:35:36.675003143 +0000
13ae24
+++ binutils-2.26/include/elf/common.h	2016-02-19 09:35:54.913106566 +0000
13ae24
@@ -478,6 +478,7 @@
13ae24
 #define SHT_GNU_ATTRIBUTES 0x6ffffff5	/* Object attributes */
13ae24
 #define SHT_GNU_HASH	0x6ffffff6	/* GNU style symbol hash table */
13ae24
 #define SHT_GNU_LIBLIST	0x6ffffff7	/* List of prelink dependencies */
13ae24
+#define SHT_GNU_OBJECT_ONLY 0x6ffffff8	/* Object only */
13ae24
 
13ae24
 /* The next three section types are defined by Solaris, and are named
13ae24
    SHT_SUNW*.  We use them in GNU code, so we also define SHT_GNU*
13ae24
diff -rup binutils-2.26.orig/ld/emultempl/aarch64elf.em binutils-2.26/ld/emultempl/aarch64elf.em
13ae24
--- binutils-2.26.orig/ld/emultempl/aarch64elf.em	2016-02-19 09:35:36.698003274 +0000
13ae24
+++ binutils-2.26/ld/emultempl/aarch64elf.em	2016-02-19 09:35:54.914106571 +0000
13ae24
@@ -271,7 +271,7 @@ gld${EMULATION_NAME}_after_allocation (v
13ae24
 }
13ae24
 
13ae24
 static void
13ae24
-gld${EMULATION_NAME}_finish (void)
13ae24
+aarch64_finish (void)
13ae24
 {
13ae24
   if (!bfd_link_relocatable (&link_info))
13ae24
     {
13ae24
@@ -283,7 +283,7 @@ gld${EMULATION_NAME}_finish (void)
13ae24
 	}
13ae24
     }
13ae24
 
13ae24
-  finish_default ();
13ae24
+  gld${EMULATION_NAME}_finish ();
13ae24
 }
13ae24
 
13ae24
 /* This is a convenient point to tell BFD about target specific flags.
13ae24
@@ -435,4 +435,4 @@ LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=
13ae24
 LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME}"_before_parse
13ae24
 
13ae24
 # Call the extra arm-elf function
13ae24
-LDEMUL_FINISH=gld${EMULATION_NAME}_finish
13ae24
+LDEMUL_FINISH=aarch64_finish
13ae24
diff -rup binutils-2.26.orig/ld/emultempl/alphaelf.em binutils-2.26/ld/emultempl/alphaelf.em
13ae24
--- binutils-2.26.orig/ld/emultempl/alphaelf.em	2016-02-19 09:35:36.698003274 +0000
13ae24
+++ binutils-2.26/ld/emultempl/alphaelf.em	2016-02-19 09:35:54.914106571 +0000
13ae24
@@ -104,7 +104,7 @@ alpha_finish (void)
13ae24
   if (limit_32bit)
13ae24
     elf_elfheader (link_info.output_bfd)->e_flags |= EF_ALPHA_32BIT;
13ae24
 
13ae24
-  finish_default ();
13ae24
+  gld${EMULATION_NAME}_finish ();
13ae24
 }
13ae24
 EOF
13ae24
 
13ae24
diff -rup binutils-2.26.orig/ld/emultempl/avrelf.em binutils-2.26/ld/emultempl/avrelf.em
13ae24
--- binutils-2.26.orig/ld/emultempl/avrelf.em	2016-02-19 09:35:36.698003274 +0000
13ae24
+++ binutils-2.26/ld/emultempl/avrelf.em	2016-02-19 09:35:54.915106577 +0000
13ae24
@@ -209,7 +209,7 @@ avr_finish (void)
13ae24
 	elf_elfheader (abfd)->e_flags &= ~EF_AVR_LINKRELAX_PREPARED;
13ae24
     }
13ae24
 
13ae24
-  finish_default ();
13ae24
+  gld${EMULATION_NAME}_finish ();
13ae24
 }
13ae24
 EOF
13ae24
 
13ae24
diff -rup binutils-2.26.orig/ld/emultempl/elf32.em binutils-2.26/ld/emultempl/elf32.em
13ae24
--- binutils-2.26.orig/ld/emultempl/elf32.em	2016-02-19 09:35:36.700003285 +0000
13ae24
+++ binutils-2.26/ld/emultempl/elf32.em	2016-02-19 09:35:54.915106577 +0000
13ae24
@@ -66,6 +66,7 @@ static void gld${EMULATION_NAME}_before_
13ae24
 static void gld${EMULATION_NAME}_after_allocation (void);
13ae24
 static lang_output_section_statement_type *gld${EMULATION_NAME}_place_orphan
13ae24
   (asection *, const char *, int);
13ae24
+static void gld${EMULATION_NAME}_finish (void);
13ae24
 EOF
13ae24
 
13ae24
 if [ "x${USE_LIBPATH}" = xyes ] ; then
13ae24
@@ -1795,6 +1796,8 @@ output_rel_find (asection *sec, int isdy
13ae24
 					  in->owner, in);
13ae24
 }
13ae24
 
13ae24
+static int orphan_init_done = 0;
13ae24
+
13ae24
 /* Place an orphan section.  We use this to put random SHF_ALLOC
13ae24
    sections in the right segment.  */
13ae24
 
13ae24
@@ -1803,7 +1806,7 @@ gld${EMULATION_NAME}_place_orphan (asect
13ae24
 				   const char *secname,
13ae24
 				   int constraint)
13ae24
 {
13ae24
-  static struct orphan_save hold[] =
13ae24
+  static struct orphan_save orig_hold[] =
13ae24
     {
13ae24
       { ".text",
13ae24
 	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
13ae24
@@ -1833,6 +1836,7 @@ gld${EMULATION_NAME}_place_orphan (asect
13ae24
 	SEC_HAS_CONTENTS,
13ae24
 	0, 0, 0, 0 },
13ae24
     };
13ae24
+  static struct orphan_save hold[ARRAY_SIZE (orig_hold)];
13ae24
   enum orphan_save_index
13ae24
     {
13ae24
       orphan_text = 0,
13ae24
@@ -1845,7 +1849,6 @@ gld${EMULATION_NAME}_place_orphan (asect
13ae24
       orphan_sdata,
13ae24
       orphan_nonalloc
13ae24
     };
13ae24
-  static int orphan_init_done = 0;
13ae24
   struct orphan_save *place;
13ae24
   lang_output_section_statement_type *after;
13ae24
   lang_output_section_statement_type *os;
13ae24
@@ -1924,15 +1927,22 @@ gld${EMULATION_NAME}_place_orphan (asect
13ae24
 
13ae24
   if (!orphan_init_done)
13ae24
     {
13ae24
-      struct orphan_save *ho;
13ae24
+      struct orphan_save *ho, *horig;
13ae24
 
13ae24
       for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
13ae24
+      for (ho = hold, horig = orig_hold;
13ae24
+	   ho < hold + ARRAY_SIZE (hold);
13ae24
+	   ++ho, ++horig)
13ae24
+	{
13ae24
+	  *ho = *horig;
13ae24
+	  if (ho->name != NULL)
13ae24
 	if (ho->name != NULL)
13ae24
 	  {
13ae24
 	    ho->os = lang_output_section_find (ho->name);
13ae24
 	    if (ho->os != NULL && ho->os->flags == 0)
13ae24
 	      ho->os->flags = ho->flags;
13ae24
 	  }
13ae24
+	}
13ae24
       orphan_init_done = 1;
13ae24
     }
13ae24
 
13ae24
@@ -2017,6 +2027,27 @@ gld${EMULATION_NAME}_place_orphan (asect
13ae24
 EOF
13ae24
 fi
13ae24
 
13ae24
+fragment <
13ae24
+
13ae24
+/* Final emulation specific call.  */
13ae24
+
13ae24
+static void
13ae24
+gld${EMULATION_NAME}_finish (void)
13ae24
+{
13ae24
+EOF
13ae24
+if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
13ae24
+fragment <
13ae24
+  /* Support the object-only output.  */
13ae24
+  if (link_info.emit_gnu_object_only)
13ae24
+    orphan_init_done = 0;
13ae24
+
13ae24
+EOF
13ae24
+fi
13ae24
+fragment <
13ae24
+  finish_default ();
13ae24
+}
13ae24
+EOF
13ae24
+
13ae24
 if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then
13ae24
 fragment <
13ae24
 
13ae24
@@ -2503,7 +2534,7 @@ struct ld_emulation_xfer_struct ld_${EMU
13ae24
   ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
13ae24
   "${EMULATION_NAME}",
13ae24
   "${OUTPUT_FORMAT}",
13ae24
-  ${LDEMUL_FINISH-finish_default},
13ae24
+  ${LDEMUL_FINISH-gld${EMULATION_NAME}_finish},
13ae24
   ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
13ae24
   ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
13ae24
   ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
13ae24
diff -rup binutils-2.26.orig/ld/emultempl/ppc32elf.em binutils-2.26/ld/emultempl/ppc32elf.em
13ae24
--- binutils-2.26.orig/ld/emultempl/ppc32elf.em	2016-02-19 09:35:36.699003279 +0000
13ae24
+++ binutils-2.26/ld/emultempl/ppc32elf.em	2016-02-19 09:35:54.916106583 +0000
13ae24
@@ -215,7 +215,7 @@ ppc_finish (void)
13ae24
 {
13ae24
   if (params.ppc476_workaround)
13ae24
     lang_for_each_statement (no_zero_padding);
13ae24
-  finish_default ();
13ae24
+  gld${EMULATION_NAME}_finish ();
13ae24
 }
13ae24
 
13ae24
 EOF
13ae24
diff -rup binutils-2.26.orig/ld/emultempl/spuelf.em binutils-2.26/ld/emultempl/spuelf.em
13ae24
--- binutils-2.26.orig/ld/emultempl/spuelf.em	2016-02-19 09:35:36.699003279 +0000
13ae24
+++ binutils-2.26/ld/emultempl/spuelf.em	2016-02-19 09:35:54.916106583 +0000
13ae24
@@ -416,7 +416,7 @@ spu_elf_relink (void)
13ae24
 /* Final emulation specific call.  */
13ae24
 
13ae24
 static void
13ae24
-gld${EMULATION_NAME}_finish (void)
13ae24
+spu_finish (void)
13ae24
 {
13ae24
   if (is_spu_target ())
13ae24
     {
13ae24
@@ -432,7 +432,7 @@ gld${EMULATION_NAME}_finish (void)
13ae24
 	einfo (_("%P: --auto-overlay ignored with zero local store range\n"));
13ae24
     }
13ae24
 
13ae24
-  finish_default ();
13ae24
+  gld${EMULATION_NAME}_finish ();
13ae24
 }
13ae24
 
13ae24
 static char *
13ae24
@@ -832,5 +832,5 @@ PARSE_AND_LIST_ARGS_CASES='
13ae24
 
13ae24
 LDEMUL_AFTER_OPEN=spu_after_open
13ae24
 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
13ae24
-LDEMUL_FINISH=gld${EMULATION_NAME}_finish
13ae24
+LDEMUL_FINISH=spu_finish
13ae24
 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target
13ae24
diff -rup binutils-2.26.orig/ld/ldexp.c binutils-2.26/ld/ldexp.c
13ae24
--- binutils-2.26.orig/ld/ldexp.c	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldexp.c	2016-02-19 09:35:54.917106588 +0000
13ae24
@@ -1616,14 +1616,15 @@ align_n (bfd_vma value, bfd_vma align)
13ae24
 }
13ae24
 
13ae24
 void
13ae24
-ldexp_init (void)
13ae24
+ldexp_init (bfd_boolean object_only)
13ae24
 {
13ae24
   /* The value "13" is ad-hoc, somewhat related to the expected number of
13ae24
      assignments in a linker script.  */
13ae24
-  if (!bfd_hash_table_init_n (&definedness_table,
13ae24
-			      definedness_newfunc,
13ae24
-			      sizeof (struct definedness_hash_entry),
13ae24
-			      13))
13ae24
+  if (!object_only
13ae24
+      && !bfd_hash_table_init_n (&definedness_table,
13ae24
+				 definedness_newfunc,
13ae24
+				 sizeof (struct definedness_hash_entry),
13ae24
+				 13))
13ae24
     einfo (_("%P%F: can not create hash table: %E\n"));
13ae24
 }
13ae24
 
13ae24
@@ -1658,7 +1659,8 @@ ldexp_finalize_syms (void)
13ae24
 }
13ae24
 
13ae24
 void
13ae24
-ldexp_finish (void)
13ae24
+ldexp_finish (bfd_boolean object_only)
13ae24
 {
13ae24
-  bfd_hash_table_free (&definedness_table);
13ae24
+  if (!object_only)
13ae24
+    bfd_hash_table_free (&definedness_table);
13ae24
 }
13ae24
diff -rup binutils-2.26.orig/ld/ldexp.h binutils-2.26/ld/ldexp.h
13ae24
--- binutils-2.26.orig/ld/ldexp.h	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldexp.h	2016-02-19 09:35:54.917106588 +0000
13ae24
@@ -229,8 +229,8 @@ fill_type *exp_get_fill
13ae24
   (etree_type *, fill_type *, char *);
13ae24
 bfd_vma exp_get_abs_int
13ae24
   (etree_type *, int, char *);
13ae24
-void ldexp_init (void);
13ae24
+void ldexp_init (bfd_boolean);
13ae24
 void ldexp_finalize_syms (void);
13ae24
-void ldexp_finish (void);
13ae24
+void ldexp_finish (bfd_boolean);
13ae24
 
13ae24
 #endif
13ae24
diff -rup binutils-2.26.orig/ld/ldfile.c binutils-2.26/ld/ldfile.c
13ae24
--- binutils-2.26.orig/ld/ldfile.c	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldfile.c	2016-02-19 09:35:54.918106594 +0000
13ae24
@@ -313,7 +313,9 @@ success:
13ae24
       && !no_more_claiming
13ae24
       && bfd_check_format (entry->the_bfd, bfd_object))
13ae24
     plugin_maybe_claim (entry);
13ae24
+  else
13ae24
 #endif /* ENABLE_PLUGINS */
13ae24
+    cmdline_check_object_only_section (entry->the_bfd, FALSE);
13ae24
 
13ae24
   /* It opened OK, the format checked out, and the plugins have had
13ae24
      their chance to claim it, so this is success.  */
13ae24
diff -rup binutils-2.26.orig/ld/ldlang.c binutils-2.26/ld/ldlang.c
13ae24
--- binutils-2.26.orig/ld/ldlang.c	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldlang.c	2016-02-19 09:35:54.921106611 +0000
13ae24
@@ -36,6 +36,7 @@
13ae24
 #include "ldctor.h"
13ae24
 #include "ldfile.h"
13ae24
 #include "ldemul.h"
13ae24
+#include "ldwrite.h"
13ae24
 #include "fnmatch.h"
13ae24
 #include "demangle.h"
13ae24
 #include "hashtab.h"
13ae24
@@ -45,6 +46,9 @@
13ae24
 #include "plugin.h"
13ae24
 #endif /* ENABLE_PLUGINS */
13ae24
 
13ae24
+/* FIXME: Put it here to avoid NAME conflict from ldgram.h.  */
13ae24
+#include "elf-bfd.h"
13ae24
+
13ae24
 #ifndef offsetof
13ae24
 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
13ae24
 #endif
13ae24
@@ -86,6 +93,13 @@ static void lang_do_version_exports_sect
13ae24
 static void lang_finalize_version_expr_head
13ae24
   (struct bfd_elf_version_expr_head *);
13ae24
 static void lang_do_memory_regions (void);
13ae24
+static void cmdline_lists_init (void);
13ae24
+static void cmdline_get_object_only_input_files (void);
13ae24
+static void print_cmdline_list (cmdline_union_type *);
13ae24
+static bfd_boolean cmdline_on_object_only_archive_list_p (bfd *);
13ae24
+static cmdline_list_type cmdline_object_only_file_list;
13ae24
+static cmdline_list_type cmdline_object_only_archive_list;
13ae24
+static cmdline_list_type cmdline_temp_object_only_list;
13ae24
 
13ae24
 /* Exported variables.  */
13ae24
 const char *output_target;
13ae24
@@ -1219,14 +1230,17 @@ output_section_statement_table_free (voi
13ae24
 /* Build enough state so that the parser can build its tree.  */
13ae24
 
13ae24
 void
13ae24
-lang_init (void)
13ae24
+lang_init (bfd_boolean object_only)
13ae24
 {
13ae24
-  obstack_begin (&stat_obstack, 1000);
13ae24
+  if (!object_only)
13ae24
+    obstack_begin (&stat_obstack, 1000);
13ae24
 
13ae24
   stat_ptr = &statement_list;
13ae24
 
13ae24
   output_section_statement_table_init ();
13ae24
 
13ae24
+  cmdline_lists_init ();
13ae24
+
13ae24
   lang_list_init (stat_ptr);
13ae24
 
13ae24
   lang_list_init (&input_file_chain);
13ae24
@@ -2843,6 +2857,12 @@ load_symbols (lang_input_statement_type
13ae24
 		  loaded = FALSE;
13ae24
 		}
13ae24
 
13ae24
+	      if (link_info.emitting_gnu_object_only)
13ae24
+		{
13ae24
+		  if (!cmdline_on_object_only_archive_list_p (member))
13ae24
+		    continue;
13ae24
+		}
13ae24
+
13ae24
 	      subsbfd = member;
13ae24
 	      if (!(*link_info.callbacks
13ae24
 		    ->add_archive_element) (&link_info, member,
13ae24
@@ -6780,10 +6800,41 @@ lang_process (void)
13ae24
 	  open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
13ae24
 	}
13ae24
     }
13ae24
+  else
13ae24
 #endif /* ENABLE_PLUGINS */
13ae24
+    if (bfd_link_relocatable (&link_info))
13ae24
+    {
13ae24
+      /* Check if .gnu_object_only section should be created.  */
13ae24
+      bfd *p;
13ae24
+      int object_type;
13ae24
+
13ae24
+      object_type = 0;
13ae24
+      for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link.next)
13ae24
+	{
13ae24
+	  object_type |= 1 << p->lto_type;
13ae24
+	  if ((object_type & (1 << lto_mixed_object)) != 0
13ae24
+	      || ((object_type
13ae24
+		   & (1 << lto_non_ir_object
13ae24
+		      | 1 << lto_ir_object))
13ae24
+		  == (1 << lto_non_ir_object | 1 << lto_ir_object)))
13ae24
+	    {
13ae24
+	      link_info.emit_gnu_object_only = TRUE;
13ae24
+	      break;
13ae24
+	    }
13ae24
+	}
13ae24
+
13ae24
+      if (verbose
13ae24
+	  && (cmdline_object_only_file_list.head
13ae24
+	      || cmdline_object_only_archive_list.head))
13ae24
+	{
13ae24
+	  info_msg (_("Object-only input files:\n "));
13ae24
+	  print_cmdline_list (cmdline_object_only_file_list.head);
13ae24
+	  print_cmdline_list (cmdline_object_only_archive_list.head);
13ae24
+	}
13ae24
+    }
13ae24
 
13ae24
   /* Make sure that nobody has tried to add a symbol to this list
13ae24
      before now.  */
13ae24
   ASSERT (link_info.gc_sym_list == NULL);
13ae24
 
13ae24
   link_info.gc_sym_list = &entry_symbol;
13ae24
@@ -8275,3 +8326,968 @@ lang_print_memory_usage (void)
13ae24
       printf ("    %6.2f%%\n", percent);
13ae24
     }
13ae24
 }
13ae24
+
13ae24
+static void
13ae24
+cmdline_lists_init (void)
13ae24
+{
13ae24
+  cmdline_object_only_file_list.tail
13ae24
+    = &cmdline_object_only_file_list.head;
13ae24
+  cmdline_object_only_archive_list.tail
13ae24
+    = &cmdline_object_only_archive_list.head;
13ae24
+  cmdline_temp_object_only_list.tail
13ae24
+    = &cmdline_temp_object_only_list.head;
13ae24
+}
13ae24
+
13ae24
+/* Allocate an item with TYPE and DATA.  */
13ae24
+
13ae24
+static cmdline_union_type *
13ae24
+cmdline_list_new (cmdline_enum_type type, void *data)
13ae24
+{
13ae24
+  cmdline_union_type *new_opt;
13ae24
+
13ae24
+  new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt));
13ae24
+  new_opt->header.type = type;
13ae24
+  switch (type)
13ae24
+    {
13ae24
+    default:
13ae24
+      break;
13ae24
+    case cmdline_is_file_enum:
13ae24
+      new_opt->file.filename = (const char *) data;
13ae24
+      break;
13ae24
+    case cmdline_is_bfd_enum:
13ae24
+      new_opt->abfd.abfd = (bfd *) data;
13ae24
+      break;
13ae24
+    }
13ae24
+  return new_opt;
13ae24
+}
13ae24
+
13ae24
+/* Append an item with TYPE and DATA to LIST.  */
13ae24
+
13ae24
+static void
13ae24
+cmdline_list_append (cmdline_list_type *list, cmdline_enum_type type,
13ae24
+		     void *data)
13ae24
+{
13ae24
+  cmdline_union_type *new_opt = cmdline_list_new (type, data);
13ae24
+  new_opt->header.next = NULL;
13ae24
+  *list->tail = new_opt;
13ae24
+  list->tail = &new_opt->header.next;
13ae24
+}
13ae24
+
13ae24
+static void
13ae24
+print_cmdline_list (cmdline_union_type *c)
13ae24
+{
13ae24
+  for (; c != NULL; c = c->header.next)
13ae24
+    switch (c->header.type)
13ae24
+      {
13ae24
+      default:
13ae24
+	abort ();
13ae24
+      case cmdline_is_file_enum:
13ae24
+	info_msg (" %s", c->file.filename);
13ae24
+	break;
13ae24
+      case cmdline_is_bfd_enum:
13ae24
+	info_msg (" [%B]", c->abfd.abfd);
13ae24
+	break;
13ae24
+      }
13ae24
+
13ae24
+  info_msg ("\n");
13ae24
+}
13ae24
+
13ae24
+/* Return TRUE if ABFD is on cmdline_object_only_archive_list.  */
13ae24
+
13ae24
+static bfd_boolean
13ae24
+cmdline_on_object_only_archive_list_p (bfd *abfd)
13ae24
+{
13ae24
+  cmdline_union_type *c, *next;
13ae24
+  bfd *archive, *obfd, *oarchive;
13ae24
+  ufile_ptr origin = abfd->origin;
13ae24
+
13ae24
+  archive = abfd->my_archive;
13ae24
+  for (c = cmdline_object_only_archive_list.head; c != NULL; c = next)
13ae24
+    {
13ae24
+      if (c->header.type != cmdline_is_bfd_enum)
13ae24
+	abort ();
13ae24
+
13ae24
+      next = c->header.next;
13ae24
+      obfd = c->abfd.abfd;
13ae24
+      oarchive = obfd->my_archive;
13ae24
+
13ae24
+      /* The list is grouped by archive file name and sorted by member
13ae24
+	 origin.  */
13ae24
+      if (strcmp (archive->filename, oarchive->filename) != 0)
13ae24
+	continue;
13ae24
+
13ae24
+      if (origin == obfd->origin)
13ae24
+	return TRUE;
13ae24
+      else if (origin < obfd->origin)
13ae24
+	return FALSE;
13ae24
+    }
13ae24
+
13ae24
+  return FALSE;
13ae24
+}
13ae24
+
13ae24
+/* Append an item with TYPE and DATA to cmdline_object_only_file_list
13ae24
+   or cmdline_object_only_archive_list if needed.  */
13ae24
+
13ae24
+static void
13ae24
+cmdline_object_only_list_append (cmdline_enum_type type, void *data)
13ae24
+{
13ae24
+  cmdline_union_type *c;
13ae24
+  cmdline_union_type *new_opt, *next, **prev;
13ae24
+  bfd *abfd, *archive;
13ae24
+  bfd *obfd, *oarchive;
13ae24
+  bfd *nbfd, *narchive;
13ae24
+  ufile_ptr origin, norigin;
13ae24
+
13ae24
+  /* Put it on cmdline_object_only_file_list if it isn't an archive
13ae24
+     member.  */
13ae24
+  switch (type)
13ae24
+    {
13ae24
+    default:
13ae24
+      abort ();
13ae24
+    case cmdline_is_bfd_enum:
13ae24
+      abfd = (bfd *) data;
13ae24
+      archive = abfd->my_archive;
13ae24
+      if (archive)
13ae24
+	break;
13ae24
+    case cmdline_is_file_enum:
13ae24
+      cmdline_list_append (&cmdline_object_only_file_list, type, data);
13ae24
+      return;
13ae24
+    }
13ae24
+
13ae24
+  /* Put archive member on cmdline_object_only_archive_list and sort
13ae24
+     the list by archive name and archive member origin.  */
13ae24
+  new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt));
13ae24
+  new_opt->header.type = cmdline_is_bfd_enum;
13ae24
+  new_opt->header.next = NULL;
13ae24
+  new_opt->abfd.abfd = (bfd *) data;
13ae24
+
13ae24
+  c = cmdline_object_only_archive_list.head;
13ae24
+  if (c == NULL)
13ae24
+    {
13ae24
+      cmdline_object_only_archive_list.head = new_opt;
13ae24
+      cmdline_object_only_archive_list.tail = &new_opt->header.next;
13ae24
+      return;
13ae24
+    }
13ae24
+
13ae24
+  prev = NULL;
13ae24
+  origin = abfd->origin;
13ae24
+  for (; c != NULL; c = next)
13ae24
+    {
13ae24
+      if (c->header.type != cmdline_is_bfd_enum)
13ae24
+	abort ();
13ae24
+
13ae24
+      next = c->header.next;
13ae24
+
13ae24
+      obfd = c->abfd.abfd;
13ae24
+      oarchive = abfd->my_archive;
13ae24
+
13ae24
+      if (strcmp (archive->filename, oarchive->filename) == 0)
13ae24
+	{
13ae24
+	  bfd_boolean after;
13ae24
+
13ae24
+	  if (origin < obfd->origin)
13ae24
+	    {
13ae24
+	      /* Insert it before the current.  */
13ae24
+	      new_opt->header.next = c;
13ae24
+	      if (prev)
13ae24
+		*prev = new_opt;
13ae24
+	      else
13ae24
+		cmdline_object_only_archive_list.head = new_opt;
13ae24
+	      return;
13ae24
+	    }
13ae24
+
13ae24
+	  after = TRUE;
13ae24
+
13ae24
+	  /* Check origin.  */
13ae24
+	  while (next)
13ae24
+	    {
13ae24
+	      if (next->header.type != cmdline_is_bfd_enum)
13ae24
+		abort ();
13ae24
+
13ae24
+	      nbfd = next->abfd.abfd;
13ae24
+	      norigin = nbfd->origin;
13ae24
+	      if (origin > norigin)
13ae24
+		{
13ae24
+		  /* Insert it after NEXT.  */
13ae24
+		  break;
13ae24
+		}
13ae24
+
13ae24
+	      narchive = nbfd->my_archive;
13ae24
+	      if (strcmp (archive->filename, narchive->filename) != 0)
13ae24
+		{
13ae24
+		  /* Insert it befor NEXT.  */
13ae24
+		  after = FALSE;
13ae24
+		  break;
13ae24
+		}
13ae24
+
13ae24
+	      c = next;
13ae24
+	      next = next->header.next;
13ae24
+	    }
13ae24
+
13ae24
+	  if (after && next)
13ae24
+	    {
13ae24
+	      c = next;
13ae24
+	      next = next->header.next;
13ae24
+	    }
13ae24
+
13ae24
+	  if (*cmdline_object_only_archive_list.tail == c->header.next)
13ae24
+	    cmdline_object_only_archive_list.tail
13ae24
+	      = &new_opt->header.next;
13ae24
+
13ae24
+	  prev = &c->header.next;
13ae24
+	  new_opt->header.next = next;
13ae24
+	  *prev = new_opt;
13ae24
+	  return;
13ae24
+	}
13ae24
+
13ae24
+      prev = &c->header.next;
13ae24
+    }
13ae24
+
13ae24
+  *cmdline_object_only_archive_list.tail = new_opt;
13ae24
+  cmdline_object_only_archive_list.tail = &new_opt->header.next;
13ae24
+}
13ae24
+
13ae24
+/* Get object-only input files.  */
13ae24
+
13ae24
+static void
13ae24
+cmdline_get_object_only_input_files (void)
13ae24
+{
13ae24
+  cmdline_union_type *c, *next;
13ae24
+  bfd *abfd, *archive;
13ae24
+  bfd *nbfd, *narchive;
13ae24
+
13ae24
+  /* Add files first.  */
13ae24
+  for (c = cmdline_object_only_file_list.head;
13ae24
+       c != NULL; c = c->header.next)
13ae24
+    switch (c->header.type)
13ae24
+      {
13ae24
+      default:
13ae24
+	abort ();
13ae24
+      case cmdline_is_file_enum:
13ae24
+	lang_add_input_file (c->file.filename,
13ae24
+			     lang_input_file_is_file_enum, NULL);
13ae24
+	break;
13ae24
+      case cmdline_is_bfd_enum:
13ae24
+	abfd = c->abfd.abfd;
13ae24
+	if (abfd->my_archive)
13ae24
+	  abort ();
13ae24
+	lang_add_input_file (abfd->filename,
13ae24
+			     lang_input_file_is_file_enum, NULL);
13ae24
+	break;
13ae24
+      }
13ae24
+
13ae24
+  /* Add archive members next.  */
13ae24
+  for (c = cmdline_object_only_archive_list.head; c != NULL; c = next)
13ae24
+    {
13ae24
+      if (c->header.type != cmdline_is_bfd_enum)
13ae24
+	abort ();
13ae24
+
13ae24
+      next = c->header.next;
13ae24
+
13ae24
+      abfd = c->abfd.abfd;
13ae24
+      archive = abfd->my_archive;
13ae24
+
13ae24
+      /* Add the first archive of the archive member group.  */
13ae24
+      lang_add_input_file (archive->filename,
13ae24
+			   lang_input_file_is_file_enum, NULL);
13ae24
+
13ae24
+      /* Skip the rest members in the archive member group.  */
13ae24
+      do
13ae24
+	{
13ae24
+	  if (!next)
13ae24
+	    break;
13ae24
+
13ae24
+	  if (next->header.type != cmdline_is_bfd_enum)
13ae24
+	    abort ();
13ae24
+
13ae24
+	  next = next->header.next;
13ae24
+	  if (!next)
13ae24
+	    break;
13ae24
+	  nbfd = next->abfd.abfd;
13ae24
+	  narchive = nbfd->my_archive;
13ae24
+	}
13ae24
+      while (strcmp (archive->filename, narchive->filename) == 0);
13ae24
+    }
13ae24
+}
13ae24
+
13ae24
+struct cmdline_arg
13ae24
+{
13ae24
+  bfd *obfd;
13ae24
+  asymbol **isympp;
13ae24
+  int status;
13ae24
+};
13ae24
+
13ae24
+/* Create a section in OBFD with the same
13ae24
+   name and attributes as ISECTION in IBFD.  */
13ae24
+
13ae24
+static void
13ae24
+setup_section (bfd *ibfd, sec_ptr isection, void *p)
13ae24
+{
13ae24
+  struct cmdline_arg *arg = (struct cmdline_arg *) p;
13ae24
+  bfd *obfd = arg->obfd;
13ae24
+  asymbol **isympp = arg->isympp;
13ae24
+  const char *name = isection->name;
13ae24
+  sec_ptr osection;
13ae24
+  const char *err;
13ae24
+
13ae24
+  /* Skip the object-only section.  */
13ae24
+  if (ibfd->object_only_section == isection)
13ae24
+    return;
13ae24
+
13ae24
+  /* If we have already failed earlier on, do not keep on generating
13ae24
+     complaints now.  */
13ae24
+  if (arg->status)
13ae24
+    return;
13ae24
+
13ae24
+  osection = bfd_make_section_anyway_with_flags (obfd, name,
13ae24
+						 isection->flags);
13ae24
+
13ae24
+  if (osection == NULL)
13ae24
+    {
13ae24
+      err = _("failed to create output section");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  osection->size = isection->size;
13ae24
+  osection->vma = isection->vma;
13ae24
+  osection->lma = isection->lma;
13ae24
+  osection->alignment_power = isection->alignment_power;
13ae24
+
13ae24
+  /* Copy merge entity size.  */
13ae24
+  osection->entsize = isection->entsize;
13ae24
+
13ae24
+  /* This used to be mangle_section; we do here to avoid using
13ae24
+     bfd_get_section_by_name since some formats allow multiple
13ae24
+     sections with the same name.  */
13ae24
+  isection->output_section = osection;
13ae24
+  isection->output_offset = 0;
13ae24
+
13ae24
+  if ((isection->flags & SEC_GROUP) != 0)
13ae24
+    {
13ae24
+      asymbol *gsym = bfd_group_signature (isection, isympp);
13ae24
+
13ae24
+      if (gsym != NULL)
13ae24
+	{
13ae24
+	  gsym->flags |= BSF_KEEP;
13ae24
+	  if (ibfd->xvec->flavour == bfd_target_elf_flavour)
13ae24
+	    elf_group_id (isection) = gsym;
13ae24
+	}
13ae24
+    }
13ae24
+
13ae24
+  /* Allow the BFD backend to copy any private data it understands
13ae24
+     from the input section to the output section.  */
13ae24
+  if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
13ae24
+    {
13ae24
+      err = _("failed to copy private data");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  /* All went well.  */
13ae24
+  return;
13ae24
+
13ae24
+loser:
13ae24
+  arg->status = 1;
13ae24
+  einfo (_("%P%F: setup_section: %s: %s\n"), err, name);
13ae24
+}
13ae24
+
13ae24
+/* Copy the data of input section ISECTION of IBFD
13ae24
+   to an output section with the same name in OBFD.
13ae24
+   If stripping then don't copy any relocation info.  */
13ae24
+
13ae24
+static void
13ae24
+copy_section (bfd *ibfd, sec_ptr isection, void *p)
13ae24
+{
13ae24
+  struct cmdline_arg *arg = (struct cmdline_arg *) p;
13ae24
+  bfd *obfd = arg->obfd;
13ae24
+  asymbol **isympp = arg->isympp;
13ae24
+  arelent **relpp;
13ae24
+  long relcount;
13ae24
+  sec_ptr osection;
13ae24
+  bfd_size_type size;
13ae24
+  long relsize;
13ae24
+  flagword flags;
13ae24
+  const char *err;
13ae24
+
13ae24
+  /* Skip the object-only section.  */
13ae24
+  if (ibfd->object_only_section == isection)
13ae24
+    return;
13ae24
+
13ae24
+  /* If we have already failed earlier on, do not keep on generating
13ae24
+     complaints now.  */
13ae24
+  if (arg->status)
13ae24
+    return;
13ae24
+
13ae24
+  flags = bfd_get_section_flags (ibfd, isection);
13ae24
+  if ((flags & SEC_GROUP) != 0)
13ae24
+    return;
13ae24
+
13ae24
+  osection = isection->output_section;
13ae24
+  size = bfd_get_section_size (isection);
13ae24
+
13ae24
+  if (size == 0 || osection == 0)
13ae24
+    return;
13ae24
+
13ae24
+  relsize = bfd_get_reloc_upper_bound (ibfd, isection);
13ae24
+
13ae24
+  if (relsize < 0)
13ae24
+    {
13ae24
+      /* Do not complain if the target does not support relocations.  */
13ae24
+      if (relsize == -1
13ae24
+	  && bfd_get_error () == bfd_error_invalid_operation)
13ae24
+	relsize = 0;
13ae24
+      else
13ae24
+	{
13ae24
+	  err = bfd_errmsg (bfd_get_error ());
13ae24
+	  goto loser;
13ae24
+	}
13ae24
+    }
13ae24
+
13ae24
+  if (relsize == 0)
13ae24
+    bfd_set_reloc (obfd, osection, NULL, 0);
13ae24
+  else
13ae24
+    {
13ae24
+      relpp = (arelent **) xmalloc (relsize);
13ae24
+      relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
13ae24
+      if (relcount < 0)
13ae24
+	{
13ae24
+	  err = _("relocation count is negative");
13ae24
+	  goto loser;
13ae24
+	}
13ae24
+
13ae24
+      bfd_set_reloc (obfd, osection,
13ae24
+		     relcount == 0 ? NULL : relpp, relcount);
13ae24
+      if (relcount == 0)
13ae24
+	free (relpp);
13ae24
+    }
13ae24
+
13ae24
+  if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
13ae24
+    {
13ae24
+      bfd_byte *memhunk = NULL;
13ae24
+
13ae24
+      if (!bfd_get_full_section_contents (ibfd, isection, &memhunk))
13ae24
+	{
13ae24
+	  err = bfd_errmsg (bfd_get_error ());
13ae24
+	  goto loser;
13ae24
+	}
13ae24
+
13ae24
+      if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
13ae24
+	{
13ae24
+	  err = bfd_errmsg (bfd_get_error ());
13ae24
+	  goto loser;
13ae24
+	}
13ae24
+      free (memhunk);
13ae24
+    }
13ae24
+
13ae24
+  /* All went well.  */
13ae24
+  return;
13ae24
+
13ae24
+loser:
13ae24
+  einfo (_("%P%F: copy_section: %s: %s\n"), err, isection->name);
13ae24
+}
13ae24
+/* Open the temporary bfd created in the same directory as PATH.  */
13ae24
+
13ae24
+static bfd *
13ae24
+cmdline_fopen_temp (const char *path, const char *target,
13ae24
+		    const char *mode)
13ae24
+{
13ae24
+#define template "ldXXXXXX"
13ae24
+  const char *slash = strrchr (path, '/');
13ae24
+  char *tmpname;
13ae24
+  size_t len;
13ae24
+  int fd;
13ae24
+
13ae24
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
13ae24
+  {
13ae24
+    /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
13ae24
+    char *bslash = strrchr (path, '\\');
13ae24
+
13ae24
+    if (slash == NULL || (bslash != NULL && bslash > slash))
13ae24
+      slash = bslash;
13ae24
+    if (slash == NULL && path[0] != '\0' && path[1] == ':')
13ae24
+      slash = path + 1;
13ae24
+  }
13ae24
+#endif
13ae24
+
13ae24
+  if (slash != (char *) NULL)
13ae24
+    {
13ae24
+      len = slash - path;
13ae24
+      tmpname = (char *) xmalloc (len + sizeof (template) + 2);
13ae24
+      memcpy (tmpname, path, len);
13ae24
+
13ae24
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
13ae24
+      /* If tmpname is "X:", appending a slash will make it a root
13ae24
+	 directory on drive X, which is NOT the same as the current
13ae24
+	 directory on drive X.  */
13ae24
+      if (len == 2 && tmpname[1] == ':')
13ae24
+	tmpname[len++] = '.';
13ae24
+#endif
13ae24
+      tmpname[len++] = '/';
13ae24
+    }
13ae24
+  else
13ae24
+    {
13ae24
+      tmpname = (char *) xmalloc (sizeof (template));
13ae24
+      len = 0;
13ae24
+    }
13ae24
+
13ae24
+  memcpy (tmpname + len, template, sizeof (template));
13ae24
+#undef template
13ae24
+
13ae24
+#ifdef HAVE_MKSTEMP
13ae24
+  fd = mkstemp (tmpname);
13ae24
+#else
13ae24
+  tmpname = mktemp (tmpname);
13ae24
+  if (tmpname == NULL)
13ae24
+    return NULL;
13ae24
+  fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
13ae24
+#endif
13ae24
+  if (fd == -1)
13ae24
+    return NULL;
13ae24
+  return bfd_fopen (tmpname, target, mode, fd);
13ae24
+}
13ae24
+
13ae24
+/* Add the object-only section.  */
13ae24
+
13ae24
+static void
13ae24
+cmdline_add_object_only_section (bfd_byte *contents, size_t size)
13ae24
+{
13ae24
+  bfd_vma start;
13ae24
+  flagword flags;
13ae24
+  enum bfd_architecture iarch;
13ae24
+  unsigned int imach;
13ae24
+  long symcount;
13ae24
+  long symsize;
13ae24
+  asymbol **isympp = NULL;
13ae24
+  asymbol **osympp = NULL;
13ae24
+  bfd *obfd = NULL, *ibfd;
13ae24
+  const char *err;
13ae24
+  struct arg
13ae24
+    {
13ae24
+      bfd *obfd;
13ae24
+      asymbol **isympp;
13ae24
+      int status;
13ae24
+    } arg;
13ae24
+  char **matching;
13ae24
+  char *ofilename = NULL;
13ae24
+  asection *sec;
13ae24
+
13ae24
+  ibfd = bfd_openr (output_filename, output_target);
13ae24
+  if (!ibfd)
13ae24
+    {
13ae24
+      err = bfd_errmsg (bfd_get_error ());
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  if (!bfd_check_format_matches (ibfd, bfd_object, &matching))
13ae24
+    {
13ae24
+      err = bfd_errmsg (bfd_get_error ());
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  obfd = cmdline_fopen_temp (output_filename, output_target, "w");
13ae24
+  if (!obfd)
13ae24
+    {
13ae24
+      err = bfd_errmsg (bfd_get_error ());
13ae24
+      goto loser;
13ae24
+    }
13ae24
+  /* To be used after bfd_close ().  */
13ae24
+  ofilename = xstrdup (bfd_get_filename (obfd));
13ae24
+
13ae24
+  if (!bfd_set_format (obfd, bfd_object))
13ae24
+    {
13ae24
+      err = bfd_errmsg (bfd_get_error ());
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  /* Copy the start address, flags and architecture of input file to
13ae24
+     output file.  */
13ae24
+  flags = bfd_get_file_flags (ibfd);
13ae24
+  start = bfd_get_start_address (ibfd);
13ae24
+  iarch = bfd_get_arch (ibfd);
13ae24
+  imach = bfd_get_mach (ibfd);
13ae24
+  if (!bfd_set_start_address (obfd, start)
13ae24
+      || !bfd_set_file_flags (obfd, flags)
13ae24
+      || !bfd_set_arch_mach (obfd, iarch, imach))
13ae24
+    {
13ae24
+      err = bfd_errmsg (bfd_get_error ());
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  symsize = bfd_get_symtab_upper_bound (ibfd);
13ae24
+  if (symsize < 0)
13ae24
+    {
13ae24
+      err = bfd_errmsg (bfd_get_error ());
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  isympp = (asymbol **) xmalloc (symsize);
13ae24
+  symcount = bfd_canonicalize_symtab (ibfd, isympp);
13ae24
+  if (symcount < 0)
13ae24
+    {
13ae24
+      err = bfd_errmsg (bfd_get_error ());
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  arg.obfd = obfd;
13ae24
+  arg.isympp = isympp;
13ae24
+  arg.status = 0;
13ae24
+
13ae24
+  /* BFD mandates that all output sections be created and sizes set before
13ae24
+     any output is done.  Thus, we traverse all sections multiple times.  */
13ae24
+  bfd_map_over_sections (ibfd, setup_section, &arg;;
13ae24
+
13ae24
+  if (arg.status)
13ae24
+    {
13ae24
+      err = _("error setting up sections");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  /* Allow the BFD backend to copy any private data it understands
13ae24
+     from the input section to the output section.  */
13ae24
+  if (! bfd_copy_private_header_data (ibfd, obfd))
13ae24
+    {
13ae24
+      err = _("error copying private header data");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  /* Create the object-only section.  */
13ae24
+  sec = bfd_make_section_with_flags (obfd,
13ae24
+				     GNU_OBJECT_ONLY_SECTION_NAME,
13ae24
+				     (SEC_HAS_CONTENTS
13ae24
+				      | SEC_READONLY
13ae24
+				      | SEC_DATA
13ae24
+				      | SEC_LINKER_CREATED));
13ae24
+  if (sec == NULL)
13ae24
+    {
13ae24
+      err = _("can't create object-only section");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  if (! bfd_set_section_size (obfd, sec, size))
13ae24
+    {
13ae24
+      err = _("can't set object-only section size");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  if (ibfd->object_only_section)
13ae24
+    {
13ae24
+      /* Filter out the object-only section symbol.  */
13ae24
+      long src_count = 0, dst_count = 0;
13ae24
+      asymbol **from, **to;
13ae24
+
13ae24
+      osympp = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
13ae24
+      from = isympp;
13ae24
+      to = osympp;
13ae24
+      for (; src_count < symcount; src_count++)
13ae24
+	{
13ae24
+	  asymbol *sym = from[src_count];
13ae24
+	  if (bfd_get_section (sym) != ibfd->object_only_section)
13ae24
+	    to[dst_count++] = sym;
13ae24
+	}
13ae24
+      to[dst_count] = NULL;
13ae24
+      symcount = dst_count;
13ae24
+      bfd_set_symtab (obfd, osympp, symcount);
13ae24
+    }
13ae24
+  else
13ae24
+    bfd_set_symtab (obfd, isympp, symcount);
13ae24
+
13ae24
+  /* This has to happen after the symbol table has been set.  */
13ae24
+  bfd_map_over_sections (ibfd, copy_section, &arg;;
13ae24
+
13ae24
+  if (arg.status)
13ae24
+    {
13ae24
+      err = _("error copying sections");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  /* Copy the object-only section to the output.  */
13ae24
+  if (! bfd_set_section_contents (obfd, sec, contents, 0, size))
13ae24
+    {
13ae24
+      err = _("error adding object-only section");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  /* Allow the BFD backend to copy any private data it understands
13ae24
+     from the input BFD to the output BFD.  This is done last to
13ae24
+     permit the routine to look at the filtered symbol table, which is
13ae24
+     important for the ECOFF code at least.  */
13ae24
+  if (! bfd_copy_private_bfd_data (ibfd, obfd))
13ae24
+    {
13ae24
+      err = _("error copying private BFD data");
13ae24
+      goto loser;
13ae24
+    }
13ae24
+
13ae24
+  if (!bfd_close (obfd))
13ae24
+    {
13ae24
+      unlink (ofilename);
13ae24
+      einfo (_("%P%F: failed to finish output with object-only section\n"));
13ae24
+    }
13ae24
+
13ae24
+  /* Must be freed after bfd_close ().  */
13ae24
+  free (isympp);
13ae24
+  if (osympp)
13ae24
+    free (osympp);
13ae24
+
13ae24
+  if (rename (ofilename, output_filename))
13ae24
+    {
13ae24
+      unlink (ofilename);
13ae24
+      einfo (_("%P%F: failed to rename output with object-only section\n"));
13ae24
+    }
13ae24
+
13ae24
+  free (ofilename);
13ae24
+  return;
13ae24
+
13ae24
+loser:
13ae24
+  if (isympp)
13ae24
+    free (isympp);
13ae24
+  if (osympp)
13ae24
+    free (osympp);
13ae24
+  if (obfd)
13ae24
+    bfd_close (obfd);
13ae24
+  if (ofilename)
13ae24
+    {
13ae24
+      unlink (ofilename);
13ae24
+      free (ofilename);
13ae24
+    }
13ae24
+  einfo (_("%P%F: failed to add object-only section: %s\n"), err);
13ae24
+}
13ae24
+
13ae24
+/* Emit the final output with object-only section.  */
13ae24
+
13ae24
+void
13ae24
+cmdline_emit_object_only_section (void)
13ae24
+{
13ae24
+  const char *saved_output_filename = output_filename;
13ae24
+  int fd;
13ae24
+  size_t size, off;
13ae24
+  bfd_byte *contents;
13ae24
+  struct stat st;
13ae24
+
13ae24
+  /* Get a temporary object-only file.  */
13ae24
+  output_filename = make_temp_file (".obj-only.o");
13ae24
+
13ae24
+  had_output_filename = FALSE;
13ae24
+  link_info.input_bfds = NULL;
13ae24
+  link_info.input_bfds_tail = &link_info.input_bfds;
13ae24
+
13ae24
+  lang_init (TRUE);
13ae24
+  ldexp_init (TRUE);
13ae24
+
13ae24
+  ld_parse_linker_script ();
13ae24
+
13ae24
+  /* Set up the object-only output. */
13ae24
+  lang_final ();
13ae24
+
13ae24
+  /* Open the object-only file for output.  */
13ae24
+  lang_for_each_statement (ldlang_open_output);
13ae24
+
13ae24
+  ldemul_create_output_section_statements ();
13ae24
+
13ae24
+  if (!bfd_section_already_linked_table_init ())
13ae24
+    einfo (_("%P%F: Failed to create hash table\n"));
13ae24
+
13ae24
+  /* Call cmdline_on_object_only_archive_list_p to check which member
13ae24
+     should be loaded.  */
13ae24
+  input_flags.whole_archive = TRUE;
13ae24
+
13ae24
+  /* Set it to avoid adding more to cmdline lists.  */
13ae24
+  link_info.emitting_gnu_object_only = TRUE;
13ae24
+
13ae24
+  /* Get object-only input files.  */
13ae24
+  cmdline_get_object_only_input_files ();
13ae24
+
13ae24
+  /* Open object-only input files.  */
13ae24
+  open_input_bfds (statement_list.head, FALSE);
13ae24
+
13ae24
+  ldemul_after_open ();
13ae24
+
13ae24
+  bfd_section_already_linked_table_free ();
13ae24
+
13ae24
+  /* Make sure that we're not mixing architectures.  We call this
13ae24
+     after all the input files have been opened, but before we do any
13ae24
+     other processing, so that any operations merge_private_bfd_data
13ae24
+     does on the output file will be known during the rest of the
13ae24
+     link.  */
13ae24
+  lang_check ();
13ae24
+
13ae24
+  /* Size up the common data.  */
13ae24
+  lang_common ();
13ae24
+
13ae24
+  /* Update wild statements.  */
13ae24
+  update_wild_statements (statement_list.head);
13ae24
+
13ae24
+  /* Run through the contours of the script and attach input sections
13ae24
+     to the correct output sections.  */
13ae24
+  map_input_to_output_sections (statement_list.head, NULL, NULL);
13ae24
+
13ae24
+  /* Find any sections not attached explicitly and handle them.  */
13ae24
+  lang_place_orphans ();
13ae24
+
13ae24
+  /* Do anything special before sizing sections.  This is where ELF
13ae24
+     and other back-ends size dynamic sections.  */
13ae24
+  ldemul_before_allocation ();
13ae24
+
13ae24
+  /* Size up the sections.  */
13ae24
+  lang_size_sections (NULL, ! RELAXATION_ENABLED);
13ae24
+
13ae24
+  /* See if anything special should be done now we know how big
13ae24
+     everything is.  This is where relaxation is done.  */
13ae24
+  ldemul_after_allocation ();
13ae24
+
13ae24
+  ldemul_finish ();
13ae24
+
13ae24
+  /* Make sure that the section addresses make sense.  */
13ae24
+  if (command_line.check_section_addresses)
13ae24
+    lang_check_section_addresses ();
13ae24
+
13ae24
+  lang_end ();
13ae24
+
13ae24
+  ldwrite ();
13ae24
+
13ae24
+  ldexp_finish (TRUE);
13ae24
+  lang_finish ();
13ae24
+
13ae24
+  if (! bfd_close (link_info.output_bfd))
13ae24
+    einfo (_("%P%F:%s: final close failed on object-only output: %E\n"),
13ae24
+	   output_filename);
13ae24
+
13ae24
+  /* Read in the object-only file.  */
13ae24
+  fd = open (output_filename, O_RDONLY | O_BINARY);
13ae24
+  if (fd < 0)
13ae24
+    {
13ae24
+      bfd_set_error (bfd_error_system_call);
13ae24
+      einfo (_("%P%F:%s: cannot open object-only output: %E"),
13ae24
+	     output_filename);
13ae24
+    }
13ae24
+
13ae24
+  /* Get the object-only file size.  */
13ae24
+  if (fstat (fd, &st) != 0)
13ae24
+    {
13ae24
+      bfd_set_error (bfd_error_system_call);
13ae24
+      einfo (_("%P%F:%s: cannot stat object-only output: %E"),
13ae24
+	     output_filename);
13ae24
+    }
13ae24
+
13ae24
+  size = st.st_size;
13ae24
+  off = 0;
13ae24
+  contents = (bfd_byte *) xmalloc (size);
13ae24
+  while (off != size)
13ae24
+    {
13ae24
+      ssize_t got;
13ae24
+
13ae24
+      got = read (fd, contents + off, size - off);
13ae24
+      if (got < 0)
13ae24
+	{
13ae24
+	  bfd_set_error (bfd_error_system_call);
13ae24
+	  einfo (_("%P%F:%s: read failed on object-only output: %E"),
13ae24
+		 output_filename);
13ae24
+	}
13ae24
+
13ae24
+      off += got;
13ae24
+    }
13ae24
+
13ae24
+  close (fd);
13ae24
+
13ae24
+  /* Remove the temporary object-only file.  */
13ae24
+  unlink (output_filename);
13ae24
+
13ae24
+  output_filename = saved_output_filename;
13ae24
+
13ae24
+  cmdline_add_object_only_section (contents, size);
13ae24
+
13ae24
+  free (contents);
13ae24
+}
13ae24
+
13ae24
+/* Extract the object-only section.  */
13ae24
+
13ae24
+static const char *
13ae24
+cmdline_extract_object_only_section (bfd *abfd)
13ae24
+{
13ae24
+  const char *name = bfd_extract_object_only_section (abfd);
13ae24
+
13ae24
+  if (name == NULL)
13ae24
+    einfo (_("%P%F: cannot extract object-only section from %B: %E"),
13ae24
+	   abfd);
13ae24
+
13ae24
+  /* It should be removed after it is done.  */
13ae24
+  cmdline_list_append (&cmdline_temp_object_only_list,
13ae24
+		       cmdline_is_file_enum, (void *) name);
13ae24
+
13ae24
+  return name;
13ae24
+}
13ae24
+
13ae24
+/* Check and handle the object-only section.   */
13ae24
+
13ae24
+void
13ae24
+cmdline_check_object_only_section (bfd *abfd, bfd_boolean lto)
13ae24
+{
13ae24
+  const char *filename;
13ae24
+
13ae24
+  if (link_info.emitting_gnu_object_only
13ae24
+      || abfd->format != bfd_object)
13ae24
+    return;
13ae24
+
13ae24
+  if (lto)
13ae24
+    {
13ae24
+      /* For LTO link, we only need to extract object-only section
13ae24
+	 from the mixed object, add it to input, and put it on LTO
13ae24
+	 claimed output.  */
13ae24
+      switch (abfd->lto_type)
13ae24
+	{
13ae24
+	default:
13ae24
+	  abort ();
13ae24
+	case lto_mixed_object:
13ae24
+	  filename = cmdline_extract_object_only_section (abfd);
13ae24
+	  lang_add_input_file (filename,
13ae24
+			       lang_input_file_is_file_enum, NULL);
13ae24
+	  break;
13ae24
+	case lto_non_ir_object:
13ae24
+	case lto_ir_object:
13ae24
+	  break;
13ae24
+	}
13ae24
+    }
13ae24
+  else if (bfd_link_relocatable (&link_info))
13ae24
+    {
13ae24
+      /* For non-LTO relocatable link, we need to append non-IR object
13ae24
+	 file and the object file in object-only section to the object
13ae24
+	 only list.  */
13ae24
+      switch (abfd->lto_type)
13ae24
+	{
13ae24
+	default:
13ae24
+	  abort ();
13ae24
+	case lto_mixed_object:
13ae24
+	  filename = cmdline_extract_object_only_section (abfd);
13ae24
+	  cmdline_object_only_list_append (cmdline_is_file_enum,
13ae24
+					  (void *) filename);
13ae24
+	  break;
13ae24
+	case lto_non_ir_object:
13ae24
+	  cmdline_object_only_list_append (cmdline_is_bfd_enum, abfd);
13ae24
+	  break;
13ae24
+	case lto_ir_object:
13ae24
+	  break;
13ae24
+	}
13ae24
+    }
13ae24
+}
13ae24
+
13ae24
+/* Remove temporary object-only files.  */
13ae24
+
13ae24
+void
13ae24
+cmdline_remove_object_only_files (void)
13ae24
+{
13ae24
+  cmdline_union_type *c;
13ae24
+
13ae24
+#ifdef ENABLE_PLUGINS
13ae24
+  if (plugin_save_temps)
13ae24
+    return;
13ae24
+#endif
13ae24
+
13ae24
+  c = cmdline_temp_object_only_list.head;
13ae24
+  for (; c != NULL; c = c->header.next)
13ae24
+    switch (c->header.type)
13ae24
+      {
13ae24
+      default:
13ae24
+	abort ();
13ae24
+      case cmdline_is_file_enum:
13ae24
+	unlink (c->file.filename);
13ae24
+	break;
13ae24
+      }
13ae24
+}
13ae24
diff -rup binutils-2.26.orig/ld/ldlang.h binutils-2.26/ld/ldlang.h
13ae24
--- binutils-2.26.orig/ld/ldlang.h	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldlang.h	2016-02-19 09:35:54.921106611 +0000
13ae24
@@ -517,7 +517,7 @@ extern struct asneeded_minfo **asneeded_
13ae24
 extern void (*output_bfd_hash_table_free_fn) (struct bfd_link_hash_table *);
13ae24
 
13ae24
 extern void lang_init
13ae24
-  (void);
13ae24
+  (bfd_boolean);
13ae24
 extern void lang_finish
13ae24
   (void);
13ae24
 extern lang_memory_region_type * lang_memory_region_lookup
13ae24
@@ -693,7 +693,48 @@ lang_ld_feature (char *);
13ae24
 extern void
13ae24
 lang_print_memory_usage (void);
13ae24
 
13ae24
 extern void
13ae24
 lang_add_gc_name (const char *);
13ae24
 
13ae24
+typedef enum
13ae24
+{
13ae24
+  cmdline_is_file_enum,
13ae24
+  cmdline_is_bfd_enum
13ae24
+} cmdline_enum_type;
13ae24
+
13ae24
+typedef struct cmdline_header_struct
13ae24
+{
13ae24
+  union cmdline_union *next;
13ae24
+  cmdline_enum_type type;
13ae24
+} cmdline_header_type;
13ae24
+
13ae24
+typedef struct cmdline_file_struct
13ae24
+{
13ae24
+  cmdline_header_type header;
13ae24
+  const char *filename;
13ae24
+} cmdline_file_type;
13ae24
+
13ae24
+typedef struct cmdline_bfd_struct
13ae24
+{
13ae24
+  cmdline_header_type header;
13ae24
+  bfd *abfd;
13ae24
+} cmdline_bfd_type;
13ae24
+
13ae24
+typedef union cmdline_union
13ae24
+{
13ae24
+  cmdline_header_type header;
13ae24
+  cmdline_file_type file;
13ae24
+  cmdline_bfd_type abfd;
13ae24
+} cmdline_union_type;
13ae24
+
13ae24
+typedef struct cmdline_list
13ae24
+{
13ae24
+  cmdline_union_type *head;
13ae24
+  cmdline_union_type **tail;
13ae24
+} cmdline_list_type;
13ae24
+
13ae24
+extern void cmdline_emit_object_only_section (void);
13ae24
+extern void cmdline_check_object_only_section (bfd *, bfd_boolean);
13ae24
+extern void cmdline_remove_object_only_files (void);
13ae24
+
13ae24
 #endif
13ae24
diff -rup binutils-2.26.orig/ld/ldlex.h binutils-2.26/ld/ldlex.h
13ae24
--- binutils-2.26.orig/ld/ldlex.h	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldlex.h	2016-02-19 09:35:54.921106611 +0000
13ae24
@@ -134,6 +134,7 @@ enum option_values
13ae24
 #ifdef ENABLE_PLUGINS
13ae24
   OPTION_PLUGIN,
13ae24
   OPTION_PLUGIN_OPT,
13ae24
+  OPTION_PLUGIN_SAVE_TEMPS,
13ae24
 #endif /* ENABLE_PLUGINS */
13ae24
   OPTION_DEFAULT_SCRIPT,
13ae24
   OPTION_PRINT_OUTPUT_FORMAT,
13ae24
diff -rup binutils-2.26.orig/ld/ldmain.c binutils-2.26/ld/ldmain.c
13ae24
--- binutils-2.26.orig/ld/ldmain.c	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldmain.c	2016-02-19 09:35:54.922106617 +0000
13ae24
@@ -219,6 +219,9 @@ main (int argc, char **argv)
13ae24
 
13ae24
   xatexit (ld_cleanup);
13ae24
 
13ae24
+  /* Remove temporary object-only files.  */
13ae24
+  xatexit (cmdline_remove_object_only_files);
13ae24
+
13ae24
   /* Set up the sysroot directory.  */
13ae24
   ld_sysroot = get_sysroot (argc, argv);
13ae24
   if (*ld_sysroot)
13ae24
@@ -291,8 +294,8 @@ main (int argc, char **argv)
13ae24
   default_target = ldemul_choose_target (argc, argv);
13ae24
   config.maxpagesize = bfd_emul_get_maxpagesize (default_target);
13ae24
   config.commonpagesize = bfd_emul_get_commonpagesize (default_target);
13ae24
-  lang_init ();
13ae24
-  ldexp_init ();
13ae24
+  lang_init (FALSE);
13ae24
+  ldexp_init (FALSE);
13ae24
   ldemul_before_parse ();
13ae24
   lang_has_input_file = FALSE;
13ae24
   parse_args (argc, argv);
13ae24
@@ -307,34 +310,7 @@ main (int argc, char **argv)
13ae24
 
13ae24
   ldemul_set_symbols ();
13ae24
 
13ae24
-  /* If we have not already opened and parsed a linker script,
13ae24
-     try the default script from command line first.  */
13ae24
-  if (saved_script_handle == NULL
13ae24
-      && command_line.default_script != NULL)
13ae24
-    {
13ae24
-      ldfile_open_command_file (command_line.default_script);
13ae24
-      parser_input = input_script;
13ae24
-      yyparse ();
13ae24
-    }
13ae24
-
13ae24
-  /* If we have not already opened and parsed a linker script
13ae24
-     read the emulation's appropriate default script.  */
13ae24
-  if (saved_script_handle == NULL)
13ae24
-    {
13ae24
-      int isfile;
13ae24
-      char *s = ldemul_get_script (&isfile);
13ae24
-
13ae24
-      if (isfile)
13ae24
-	ldfile_open_default_command_file (s);
13ae24
-      else
13ae24
-	{
13ae24
-	  lex_string = s;
13ae24
-	  lex_redirect (s, _("built in linker script"), 1);
13ae24
-	}
13ae24
-      parser_input = input_script;
13ae24
-      yyparse ();
13ae24
-      lex_string = NULL;
13ae24
-    }
13ae24
+  ld_parse_linker_script ();
13ae24
 
13ae24
   if (verbose)
13ae24
     {
13ae24
@@ -445,7 +421,7 @@ main (int argc, char **argv)
13ae24
     fprintf (stderr, "lookup = %p val %lx\n", h, h ? h->u.def.value : 1);
13ae24
   }
13ae24
 #endif
13ae24
-  ldexp_finish ();
13ae24
+  ldexp_finish (FALSE);
13ae24
   lang_finish ();
13ae24
 
13ae24
   /* Even if we're producing relocatable output, some non-fatal errors should
13ae24
@@ -465,6 +441,8 @@ main (int argc, char **argv)
13ae24
       if (!bfd_close (link_info.output_bfd))
13ae24
 	einfo (_("%F%B: final close failed: %E\n"), link_info.output_bfd);
13ae24
 
13ae24
+      link_info.output_bfd = NULL;
13ae24
+
13ae24
       /* If the --force-exe-suffix is enabled, and we're making an
13ae24
 	 executable file and it doesn't end in .exe, copy it to one
13ae24
 	 which does.  */
13ae24
@@ -512,6 +490,9 @@ main (int argc, char **argv)
13ae24
 	}
13ae24
     }
13ae24
 
13ae24
+  if (link_info.emit_gnu_object_only)
13ae24
+    cmdline_emit_object_only_section ();
13ae24
+
13ae24
   END_PROGRESS (program_name);
13ae24
 
13ae24
   if (config.stats)
13ae24
@@ -805,7 +786,9 @@ add_archive_element (struct bfd_link_inf
13ae24
 	  *subsbfd = input->the_bfd;
13ae24
 	}
13ae24
     }
13ae24
+  else
13ae24
 #endif /* ENABLE_PLUGINS */
13ae24
+    cmdline_check_object_only_section (input->the_bfd, FALSE);
13ae24
 
13ae24
   ldlang_add_file (input);
13ae24
 
13ae24
@@ -1484,3 +1467,38 @@ notice (struct bfd_link_info *info,
13ae24
 
13ae24
   return TRUE;
13ae24
 }
13ae24
+
13ae24
+/* Parse the linker script.   */
13ae24
+
13ae24
+void
13ae24
+ld_parse_linker_script ()
13ae24
+{
13ae24
+  /* If we have not already opened and parsed a linker script,
13ae24
+     try the default script from command line first.  */
13ae24
+  if (saved_script_handle == NULL
13ae24
+      && command_line.default_script != NULL)
13ae24
+    {
13ae24
+      ldfile_open_command_file (command_line.default_script);
13ae24
+      parser_input = input_script;
13ae24
+      yyparse ();
13ae24
+    }
13ae24
+
13ae24
+  /* If we have not already opened and parsed a linker script
13ae24
+     read the emulation's appropriate default script.  */
13ae24
+  if (saved_script_handle == NULL)
13ae24
+    {
13ae24
+      int isfile;
13ae24
+      char *s = ldemul_get_script (&isfile);
13ae24
+
13ae24
+      if (isfile)
13ae24
+	ldfile_open_default_command_file (s);
13ae24
+      else
13ae24
+	{
13ae24
+	  lex_string = s;
13ae24
+	  lex_redirect (s, _("built in linker script"), 1);
13ae24
+	}
13ae24
+      parser_input = input_script;
13ae24
+      yyparse ();
13ae24
+      lex_string = NULL;
13ae24
+    }
13ae24
+}
13ae24
diff -rup binutils-2.26.orig/ld/ldmain.h binutils-2.26/ld/ldmain.h
13ae24
--- binutils-2.26.orig/ld/ldmain.h	2016-02-19 09:35:36.701003291 +0000
13ae24
+++ binutils-2.26/ld/ldmain.h	2016-02-19 09:35:54.922106617 +0000
13ae24
@@ -59,4 +59,6 @@ extern void add_wrap (const char *);
13ae24
 extern void add_ignoresym (struct bfd_link_info *, const char *);
13ae24
 extern void add_keepsyms_file (const char *);
13ae24
 
13ae24
+extern void ld_parse_linker_script (void);
13ae24
+
13ae24
 #endif
13ae24
diff -rup binutils-2.26.orig/ld/lexsup.c binutils-2.26/ld/lexsup.c
13ae24
--- binutils-2.26.orig/ld/lexsup.c	2016-02-19 09:35:36.700003285 +0000
13ae24
+++ binutils-2.26/ld/lexsup.c	2016-02-19 09:35:54.923106623 +0000
13ae24
@@ -169,6 +169,9 @@ static const struct ld_option ld_options
13ae24
     '\0', N_("PLUGIN"), N_("Load named plugin"), ONE_DASH },
13ae24
   { {"plugin-opt", required_argument, NULL, OPTION_PLUGIN_OPT},
13ae24
     '\0', N_("ARG"), N_("Send arg to last-loaded plugin"), ONE_DASH },
13ae24
+  { {"plugin-save-temps", no_argument, NULL, OPTION_PLUGIN_SAVE_TEMPS},
13ae24
+    '\0', NULL, N_("Store plugin intermediate files permanently"),
13ae24
+    ONE_DASH },
13ae24
   { {"flto", optional_argument, NULL, OPTION_IGNORE},
13ae24
     '\0', NULL, N_("Ignored for GCC LTO option compatibility"),
13ae24
     ONE_DASH },
13ae24
@@ -1020,6 +1023,9 @@ parse_args (unsigned argc, char **argv)
13ae24
 	  if (plugin_opt_plugin_arg (optarg))
13ae24
 	    einfo (_("%P%F: bad -plugin-opt option\n"));
13ae24
 	  break;
13ae24
+	case OPTION_PLUGIN_SAVE_TEMPS:
13ae24
+	  plugin_save_temps = TRUE;
13ae24
+	  break;
13ae24
 #endif /* ENABLE_PLUGINS */
13ae24
 	case 'q':
13ae24
 	  link_info.emitrelocations = TRUE;
13ae24
diff -rup binutils-2.26.orig/ld/plugin.c binutils-2.26/ld/plugin.c
13ae24
--- binutils-2.26.orig/ld/plugin.c	2016-02-19 09:35:36.702003296 +0000
13ae24
+++ binutils-2.26/ld/plugin.c	2016-02-19 09:35:54.923106623 +0000
13ae24
@@ -57,6 +57,9 @@ extern int errno;
13ae24
 /* Report plugin symbols.  */
13ae24
 bfd_boolean report_plugin_symbols;
13ae24
 
13ae24
+/* Store plugin intermediate files permanently.  */
13ae24
+bfd_boolean plugin_save_temps;
13ae24
+
13ae24
 /* The suffix to append to the name of the real (claimed) object file
13ae24
    when generating a dummy BFD to hold the IR symbols sent from the
13ae24
    plugin.  For cosmetic use only; appears in maps, crefs etc.  */
13ae24
@@ -1182,6 +1185,9 @@ plugin_maybe_claim (lang_input_statement
13ae24
     {
13ae24
       bfd *abfd = entry->the_bfd->plugin_dummy_bfd;
13ae24
 
13ae24
+      /* Check object only section.  */
13ae24
+      cmdline_check_object_only_section (entry->the_bfd, TRUE);
13ae24
+
13ae24
       /* Discard the real file's BFD and substitute the dummy one.  */
13ae24
 
13ae24
       /* We can't call bfd_close on archives.  BFD archive handling
13ae24
@@ -1227,14 +1233,17 @@ plugin_call_cleanup (void)
13ae24
     {
13ae24
       if (curplug->cleanup_handler && !curplug->cleanup_done)
13ae24
 	{
13ae24
-	  enum ld_plugin_status rv;
13ae24
-	  curplug->cleanup_done = TRUE;
13ae24
-	  called_plugin = curplug;
13ae24
-	  rv = (*curplug->cleanup_handler) ();
13ae24
-	  called_plugin = NULL;
13ae24
-	  if (rv != LDPS_OK)
13ae24
-	    info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
13ae24
-		      curplug->name, rv);
13ae24
+	  if (!plugin_save_temps)
13ae24
+	    {
13ae24
+	      enum ld_plugin_status rv;
13ae24
+	      curplug->cleanup_done = TRUE;
13ae24
+	      called_plugin = curplug;
13ae24
+	      rv = (*curplug->cleanup_handler) ();
13ae24
+	      called_plugin = NULL;
13ae24
+	      if (rv != LDPS_OK)
13ae24
+		info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"),
13ae24
+			  curplug->name, rv);
13ae24
+	    }
13ae24
 	  dlclose (curplug->dlhandle);
13ae24
 	}
13ae24
       curplug = curplug->next;
13ae24
diff -rup binutils-2.26.orig/ld/plugin.h binutils-2.26/ld/plugin.h
13ae24
--- binutils-2.26.orig/ld/plugin.h	2016-02-19 09:35:36.702003296 +0000
13ae24
+++ binutils-2.26/ld/plugin.h	2016-02-19 09:35:54.924106628 +0000
13ae24
@@ -24,6 +24,9 @@
13ae24
 /* Report plugin symbols.  */
13ae24
 extern bfd_boolean report_plugin_symbols;
13ae24
 
13ae24
+/* Store plugin intermediate files permanently.  */
13ae24
+extern bfd_boolean plugin_save_temps;
13ae24
+
13ae24
 /* Set at all symbols read time, to avoid recursively offering the plugin
13ae24
    its own newly-added input files and libs to claim.  */
13ae24
 extern bfd_boolean no_more_claiming;
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/armbpabi.sc binutils-2.26/ld/scripttempl/armbpabi.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/armbpabi.sc	2016-02-19 09:35:36.704003308 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/armbpabi.sc	2016-02-19 09:35:54.924106628 +0000
13ae24
@@ -36,7 +36,7 @@ INTERP=".interp       0 : { *(.interp) }
13ae24
 PLT=".plt          ${RELOCATING-0} : { *(.plt) }"
13ae24
 RODATA=".rodata       ${RELOCATING-0} : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
13ae24
 DATARELRO=".data.rel.ro : { *(.data.rel.ro.local) *(.data.rel.ro .data.rel.ro.*) }"
13ae24
-DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink)  *(.gnu.lto_*) }"
13ae24
+DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.gnu_object_only) }"
13ae24
 if test -z "${NO_SMALL_DATA}"; then
13ae24
   SBSS=".sbss         ${RELOCATING-0} :
13ae24
   {
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/elf32sh-symbian.sc binutils-2.26/ld/scripttempl/elf32sh-symbian.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/elf32sh-symbian.sc	2016-02-19 09:35:36.705003313 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/elf32sh-symbian.sc	2016-02-19 09:35:54.925106634 +0000
13ae24
@@ -88,7 +88,7 @@ fi
13ae24
     PLT=".plt            : { *(.plt) } :dynamic :dyn"
13ae24
 DYNAMIC=".dynamic        : { *(.dynamic) } :dynamic :dyn"
13ae24
  RODATA=".rodata    ALIGN(4) : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
13ae24
-DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.directive)  *(.gnu.lto_*) }"
13ae24
+DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.directive)  *(.gnu.lto_*) *(.gnu_object_only) }"
13ae24
 test -z "$GOT" && GOT=".got          ${RELOCATING-0} : { *(.got.plt) *(.got) } :dynamic :dyn"
13ae24
 INIT_ARRAY=".init_array   ${RELOCATING-0} :
13ae24
   {
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/elf64hppa.sc binutils-2.26/ld/scripttempl/elf64hppa.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/elf64hppa.sc	2016-02-19 09:35:36.705003313 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/elf64hppa.sc	2016-02-19 09:35:54.925106634 +0000
13ae24
@@ -132,7 +132,7 @@ fi
13ae24
 DYNAMIC=".dynamic      ${RELOCATING-0} : { *(.dynamic) }"
13ae24
 RODATA=".rodata       ${RELOCATING-0} : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
13ae24
 DATARELRO=".data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }"
13ae24
-DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink)  *(.gnu.lto_*) }"
13ae24
+DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.gnu_object_only) }"
13ae24
 if test -z "${NO_SMALL_DATA}"; then
13ae24
   SBSS=".sbss         ${RELOCATING-0} :
13ae24
   {
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/elf.sc binutils-2.26/ld/scripttempl/elf.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/elf.sc	2016-02-19 09:35:36.705003313 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/elf.sc	2016-02-19 09:35:54.924106628 +0000
13ae24
@@ -170,7 +170,7 @@ RELA_IPLT=".rela.iplt    ${RELOCATING-0}
13ae24
 DYNAMIC=".dynamic      ${RELOCATING-0} : { *(.dynamic) }"
13ae24
 RODATA=".${RODATA_NAME}       ${RELOCATING-0} : { *(.${RODATA_NAME}${RELOCATING+ .${RODATA_NAME}.* .gnu.linkonce.r.*}) }"
13ae24
 DATARELRO=".data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }"
13ae24
-DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }"
13ae24
+DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.gnu_object_only) }"
13ae24
 if test -z "${NO_SMALL_DATA}"; then
13ae24
   SBSS=".${SBSS_NAME}         ${RELOCATING-0} :
13ae24
   {
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/elfxtensa.sc binutils-2.26/ld/scripttempl/elfxtensa.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/elfxtensa.sc	2016-02-19 09:35:36.705003313 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/elfxtensa.sc	2016-02-19 09:35:54.925106634 +0000
13ae24
@@ -145,7 +145,7 @@ fi
13ae24
 DYNAMIC=".dynamic      ${RELOCATING-0} : { *(.dynamic) }"
13ae24
 RODATA=".rodata       ${RELOCATING-0} : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
13ae24
 DATARELRO=".data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }"
13ae24
-DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink)  *(.gnu.lto_*) }"
13ae24
+DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.gnu_object_only) }"
13ae24
 INIT_LIT=".init.literal 0 : { *(.init.literal)	}"
13ae24
 INIT=".init         0 : { *(.init)		}"
13ae24
 FINI_LIT=".fini.literal 0 : { *(.fini.literal)	}"
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/mep.sc binutils-2.26/ld/scripttempl/mep.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/mep.sc	2016-02-19 09:35:36.706003319 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/mep.sc	2016-02-19 09:35:54.926106640 +0000
13ae24
@@ -119,7 +119,7 @@ fi
13ae24
 DYNAMIC=".dynamic      ${RELOCATING-0} : { *(.dynamic) }"
13ae24
 RODATA=".rodata       ${RELOCATING-0} : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
13ae24
 DATARELRO=".data.rel.ro : { *(.data.rel.ro.local) *(.data.rel.ro .data.rel.ro.*) }"
13ae24
-DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink)  *(.gnu.lto_*) }"
13ae24
+DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) *(.gnu_object_only) }"
13ae24
 if test -z "${NO_SMALL_DATA}"; then
13ae24
   SBSS=".sbss         ${RELOCATING-0} :
13ae24
   {
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/pep.sc binutils-2.26/ld/scripttempl/pep.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/pep.sc	2016-02-19 09:35:36.706003319 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/pep.sc	2016-02-19 09:35:54.926106640 +0000
13ae24
@@ -178,6 +178,7 @@ SECTIONS
13ae24
     *(.drectve)
13ae24
     ${RELOCATING+ *(.note.GNU-stack)}
13ae24
     ${RELOCATING+ *(.gnu.lto_*)}
13ae24
+    ${RELOCATING+ *(.gnu_object_only)}
13ae24
   }
13ae24
 
13ae24
   .idata ${RELOCATING+BLOCK(__section_alignment__)} :
13ae24
diff -rup binutils-2.26.orig/ld/scripttempl/pe.sc binutils-2.26/ld/scripttempl/pe.sc
13ae24
--- binutils-2.26.orig/ld/scripttempl/pe.sc	2016-02-19 09:35:36.706003319 +0000
13ae24
+++ binutils-2.26/ld/scripttempl/pe.sc	2016-02-19 09:35:54.926106640 +0000
13ae24
@@ -165,6 +165,7 @@ SECTIONS
13ae24
     *(.drectve)
13ae24
     ${RELOCATING+ *(.note.GNU-stack)}
13ae24
     ${RELOCATING+ *(.gnu.lto_*)}
13ae24
+    ${RELOCATING+ *(.gnu_object_only)}
13ae24
   }
13ae24
 
13ae24
   .idata ${RELOCATING+BLOCK(__section_alignment__)} :
13ae24
--- binutils.orig/bfd/bfd-in2.h	2017-03-02 11:14:48.572979573 +0000
13ae24
+++ binutils-2.28/bfd/bfd-in2.h	2017-03-02 11:21:39.828458555 +0000
13ae24
@@ -1118,6 +1118,9 @@ bfd_boolean bfd_fill_in_gnu_debuglink_se
13ae24
 
13ae24
 char *bfd_follow_build_id_debuglink (bfd *abfd, const char *dir);
13ae24
 
13ae24
+const char *bfd_extract_object_only_section
13ae24
+   (bfd *abfd);
13ae24
+
13ae24
 /* Extracted from libbfd.c.  */
13ae24
 
13ae24
 /* Byte swapping macros for user section data.  */
13ae24
--- binutils.orig/bfd/opncls.c	2017-03-02 11:14:48.586979385 +0000
13ae24
+++ binutils-2.28/bfd/opncls.c	2017-03-02 11:23:23.546065945 +0000
13ae24
@@ -1990,3 +1990,69 @@ bfd_follow_build_id_debuglink (bfd *abfd
13ae24
 				   get_build_id_name,
13ae24
 				   check_build_id_file, &build_id);
13ae24
 }
13ae24
+
13ae24
+/*
13ae24
+FUNCTION
13ae24
+	bfd_extract_object_only_section
13ae24
+
13ae24
+SYNOPSIS
13ae24
+	const char *bfd_extract_object_only_section
13ae24
+	  (bfd *abfd);
13ae24
+
13ae24
+DESCRIPTION
13ae24
+
13ae24
+	Takes a @var{ABFD} and extract the .gnu_object_only section into
13ae24
+	a temporary file.
13ae24
+
13ae24
+RETURNS
13ae24
+	The name of the temporary file is returned if all is ok.
13ae24
+	Otherwise <<NULL>> is returned and bfd_error is set.
13ae24
+*/
13ae24
+
13ae24
+const char *
13ae24
+bfd_extract_object_only_section (bfd *abfd)
13ae24
+{
13ae24
+  asection *sec = abfd->object_only_section;
13ae24
+  const char *name;
13ae24
+  FILE *file;
13ae24
+  bfd_byte *memhunk = NULL;
13ae24
+  size_t off, size;
13ae24
+  bfd_error_type err;
13ae24
+
13ae24
+  /* Get a temporary object-only file.  */
13ae24
+  name = make_temp_file (".obj-only.o");
13ae24
+
13ae24
+  /* Open the object-only file.  */
13ae24
+  file = _bfd_real_fopen (name, FOPEN_WB);
13ae24
+  if (!bfd_get_full_section_contents (abfd, sec, &memhunk))
13ae24
+    {
13ae24
+      err = bfd_get_error ();
13ae24
+
13ae24
+loser:
13ae24
+      free (memhunk);
13ae24
+      fclose (file);
13ae24
+      unlink (name);
13ae24
+      bfd_set_error (err);
13ae24
+      return NULL;
13ae24
+    }
13ae24
+
13ae24
+  off = 0;
13ae24
+  size = sec->size;
13ae24
+  while (off != size)
13ae24
+    {
13ae24
+      size_t written, nwrite = size - off;
13ae24
+
13ae24
+      written = fwrite (memhunk + off, 1, nwrite, file);
13ae24
+      if (written < nwrite && ferror (file))
13ae24
+	{
13ae24
+	  err = bfd_error_system_call;
13ae24
+	  goto loser;
13ae24
+	}
13ae24
+
13ae24
+      off += written;
13ae24
+    }
13ae24
+
13ae24
+  free (memhunk);
13ae24
+  fclose (file);
13ae24
+  return name;
13ae24
+}
13ae24
--- binutils.orig/bfd/plugin.c	2017-03-02 11:37:31.572677260 +0000
13ae24
+++ binutils-2.28/bfd/plugin.c	2017-03-02 11:40:35.246209573 +0000
13ae24
@@ -132,10 +132,10 @@ register_claim_file (ld_plugin_claim_fil
13ae24
 }
13ae24
 
13ae24
 static asection bfd_plugin_fake_text_section
13ae24
-  = BFD_FAKE_SECTION (bfd_plugin_fake_text_section, 0, 0, ".text", 0);
13ae24
+= BFD_FAKE_SECTION (bfd_plugin_fake_text_section, 0, ".text", 0, 0);
13ae24
 static asection bfd_plugin_fake_common_section
13ae24
-  = BFD_FAKE_SECTION (bfd_plugin_fake_common_section, SEC_IS_COMMON, 0,
13ae24
-		      NULL, 0);
13ae24
+  = BFD_FAKE_SECTION (bfd_plugin_fake_common_section, 0,
13ae24
+		      NULL, 0, SEC_IS_COMMON);
13ae24
 
13ae24
 /* Get symbols from object only section.  */
13ae24
 
13ae24
--- binutils.orig/ld/emultempl/armelf.em	2017-03-02 11:58:14.632981042 +0000
13ae24
+++ binutils-2.28/ld/emultempl/armelf.em	2017-03-02 12:00:55.663819055 +0000
13ae24
@@ -414,7 +414,7 @@ gld${EMULATION_NAME}_after_allocation (v
13ae24
 }
13ae24
 
13ae24
 static void
13ae24
-gld${EMULATION_NAME}_finish (void)
13ae24
+arm_finish (void)
13ae24
 {
13ae24
   struct bfd_link_hash_entry * h;
13ae24
 
13ae24
@@ -441,7 +441,7 @@ gld${EMULATION_NAME}_finish (void)
13ae24
 	}
13ae24
     }
13ae24
 
13ae24
-  finish_default ();
13ae24
+  gld${EMULATION_NAME}_finish ();
13ae24
 
13ae24
   if (params.thumb_entry_symbol)
13ae24
     {
13ae24
@@ -790,4 +790,4 @@ LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME
13ae24
 LDEMUL_SET_SYMBOLS=gld"${EMULATION_NAME}"_set_symbols
13ae24
 
13ae24
 # Call the extra arm-elf function
13ae24
-LDEMUL_FINISH=gld${EMULATION_NAME}_finish
13ae24
+LDEMUL_FINISH=arm_finish
13ae24
--- binutils.orig/ld/emultempl/ppc64elf.em	2017-03-02 11:58:14.632981042 +0000
13ae24
+++ binutils-2.28/ld/emultempl/ppc64elf.em	2017-03-02 12:01:40.961210895 +0000
13ae24
@@ -561,7 +561,7 @@ gld${EMULATION_NAME}_after_allocation (v
13ae24
 /* Final emulation specific call.  */
13ae24
 
13ae24
 static void
13ae24
-gld${EMULATION_NAME}_finish (void)
13ae24
+ppc_finish (void)
13ae24
 {
13ae24
   char *msg = NULL;
13ae24
   char *line, *endline;
13ae24
@@ -592,7 +592,7 @@ gld${EMULATION_NAME}_finish (void)
13ae24
   if (msg != NULL)
13ae24
     free (msg);
13ae24
 
13ae24
-  finish_default ();
13ae24
+  gld${EMULATION_NAME}_finish ();
13ae24
 }
13ae24
 
13ae24
 
13ae24
@@ -926,4 +926,4 @@ LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=
13ae24
 LDEMUL_AFTER_OPEN=ppc_after_open
13ae24
 LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
13ae24
 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
13ae24
-LDEMUL_FINISH=gld${EMULATION_NAME}_finish
13ae24
+LDEMUL_FINISH=ppc_finish
13ae24
--- binutils.orig/bfd/format.c	2018-01-22 16:04:36.855155517 +0000
13ae24
+++ binutils-2.30.0/bfd/format.c	2018-01-22 16:11:36.546253154 +0000
13ae24
@@ -47,6 +47,7 @@ SUBSECTION
13ae24
 #include "bfd.h"
13ae24
 #include "libbfd.h"
13ae24
 #if BFD_SUPPORTS_PLUGINS
13ae24
+#include "plugin-api.h"
13ae24
 #include "plugin.h"
13ae24
 #endif
13ae24
 
13ae24
--- binutils.orig/ld/ldlang.c	2018-01-22 16:04:37.271150658 +0000
13ae24
+++ binutils-2.30.0/ld/ldlang.c	2018-01-22 16:14:10.653453040 +0000
13ae24
@@ -8954,6 +8954,7 @@ cmdline_object_only_list_append (cmdline
13ae24
       archive = abfd->my_archive;
13ae24
       if (archive)
13ae24
 	break;
13ae24
+      /* Fall through.  */
13ae24
     case cmdline_is_file_enum:
13ae24
       cmdline_list_append (&cmdline_object_only_file_list, type, data);
13ae24
       return;