cb347a4
diff -upr ../binutils-2.23.2.orig/bfd/bfd.c bfd/bfd.c
cb347a4
--- a/bfd/bfd.c	2013-05-14 16:39:24.681717759 +0100
cb347a4
+++ b/bfd/bfd.c	2013-05-14 16:40:06.988718932 +0100
cb347a4
@@ -43,6 +43,14 @@
cb347a4
 .    both_direction = 3
cb347a4
 .  };
cb347a4
 .
cb347a4
+.enum bfd_lto_object_type
cb347a4
+.  {
cb347a4
+.    lto_non_object,
cb347a4
+.    lto_non_ir_object,
cb347a4
+.    lto_ir_object,
cb347a4
+.    lto_mixed_object
cb347a4
+.  };
cb347a4
+.
cb347a4
 .struct bfd
cb347a4
 .{
cb347a4
 .  {* A unique identifier of the BFD  *}
cb347a4
@@ -190,6 +198,9 @@
cb347a4
 .  {* The last section on the section list.  *}
cb347a4
 .  struct bfd_section *section_last;
cb347a4
 .
cb347a4
+.  {* The object-only section on the section list.  *}
cb347a4
+.  struct bfd_section *object_only_section;
cb347a4
+.
cb347a4
 .  {* The number of sections.  *}
cb347a4
 .  unsigned int section_count;
cb347a4
 .
cb347a4
@@ -308,6 +319,9 @@
cb347a4
 .  {* Set if only required symbols should be added in the link hash table for
cb347a4
 .     this object.  Used by VMS linkers.  *}
cb347a4
 .  unsigned int selective_search : 1;
cb347a4
+.
cb347a4
+.  {* LTO object type.  *}
cb347a4
+.  unsigned int lto_type : 2;
cb347a4
 .};
cb347a4
 .
cb347a4
 */
cb347a4
@@ -2026,3 +2040,36 @@
cb347a4
 
cb347a4
   return res;
cb347a4
 }
cb347a4
+
cb347a4
+/*
cb347a4
+FUNCTION
cb347a4
+	bfd_group_signature
cb347a4
+
cb347a4
+SYNOPSIS
cb347a4
+	asymbol *bfd_group_signature (asection *group, asymbol **isympp);
cb347a4
+
cb347a4
+DESCRIPTION
cb347a4
+	Return a pointer to the symbol used as a signature for GROUP.
cb347a4
+*/
cb347a4
+
cb347a4
+asymbol *
cb347a4
+bfd_group_signature (asection *group, asymbol **isympp)
cb347a4
+{
cb347a4
+  bfd *abfd = group->owner;
cb347a4
+  Elf_Internal_Shdr *ghdr;
cb347a4
+
cb347a4
+  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
cb347a4
+    return NULL;
cb347a4
+
cb347a4
+  ghdr = &elf_section_data (group)->this_hdr;
cb347a4
+  if (ghdr->sh_link < elf_numsections (abfd))
cb347a4
+    {
cb347a4
+      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
cb347a4
+      Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
cb347a4
+
cb347a4
+      if (symhdr->sh_type == SHT_SYMTAB
cb347a4
+	  && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
cb347a4
+	return isympp[ghdr->sh_info - 1];
cb347a4
+    }
cb347a4
+  return NULL;
cb347a4
+}
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/bfd-in2.h bfd/bfd-in2.h
cb347a4
--- a/bfd/bfd-in2.h	2013-05-14 16:39:24.640717758 +0100
cb347a4
+++ b/bfd/bfd-in2.h	2013-05-14 16:42:03.217722154 +0100
cb347a4
@@ -1050,6 +1050,9 @@
cb347a4
 bfd_boolean bfd_fill_in_gnu_debuglink_section
cb347a4
    (bfd *abfd, struct bfd_section *sect, const char *filename);
cb347a4
 
cb347a4
+const char *bfd_extract_object_only_section
cb347a4
+   (bfd *abfd);
cb347a4
+
cb347a4
 /* Extracted from libbfd.c.  */
cb347a4
 
cb347a4
 /* Byte swapping macros for user section data.  */
cb347a4
@@ -1594,6 +1597,9 @@
cb347a4
   || ((SEC) == bfd_com_section_ptr)            \
cb347a4
   || ((SEC) == bfd_ind_section_ptr))
cb347a4
 
cb347a4
+/* GNU object-only section name.  */
cb347a4
+#define GNU_OBJECT_ONLY_SECTION_NAME ".gnu_object_only"
cb347a4
+
cb347a4
 /* Macros to handle insertion and deletion of a bfd's sections.  These
cb347a4
    only handle the list pointers, ie. do not adjust section_count,
cb347a4
    target_index etc.  */
cb347a4
@@ -5681,6 +5687,14 @@
cb347a4
     both_direction = 3
cb347a4
   };
cb347a4
 
cb347a4
+enum bfd_lto_object_type
cb347a4
+  {
cb347a4
+    lto_non_object,
cb347a4
+    lto_non_ir_object,
cb347a4
+    lto_ir_object,
cb347a4
+    lto_mixed_object
cb347a4
+  };
cb347a4
+
cb347a4
 struct bfd
cb347a4
 {
cb347a4
   /* A unique identifier of the BFD  */
cb347a4
@@ -5828,6 +5842,9 @@
cb347a4
   /* The last section on the section list.  */
cb347a4
   struct bfd_section *section_last;
cb347a4
 
cb347a4
+  /* The object-only section on the section list.  */
cb347a4
+  struct bfd_section *object_only_section;
cb347a4
+
cb347a4
   /* The number of sections.  */
cb347a4
   unsigned int section_count;
cb347a4
 
cb347a4
@@ -5946,6 +5963,9 @@
cb347a4
   /* Set if only required symbols should be added in the link hash table for
cb347a4
      this object.  Used by VMS linkers.  */
cb347a4
   unsigned int selective_search : 1;
cb347a4
+
cb347a4
+  /* LTO object type.  */
cb347a4
+  unsigned int lto_type : 2;
cb347a4
 };
cb347a4
 
cb347a4
 typedef enum bfd_error
cb347a4
@@ -6167,6 +6187,8 @@
cb347a4
 
cb347a4
 char *bfd_demangle (bfd *, const char *, int);
cb347a4
 
cb347a4
+asymbol *bfd_group_signature (asection *group, asymbol **isympp);
cb347a4
+
cb347a4
 /* Extracted from archive.c.  */
cb347a4
 symindex bfd_get_next_mapent
cb347a4
    (bfd *abfd, symindex previous, carsym **sym);
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/elf.c bfd/elf.c
cb347a4
--- a/bfd/elf.c	2013-05-14 16:39:25.317717777 +0100
cb347a4
+++ b/bfd/elf.c	2013-05-14 16:40:06.993718932 +0100
cb347a4
@@ -2092,6 +2092,7 @@
cb347a4
   { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
cb347a4
   { STRING_COMMA_LEN (".gnu.lto_"),       -1, SHT_PROGBITS,    SHF_EXCLUDE },
cb347a4
   { STRING_COMMA_LEN (".got"),             0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
cb347a4
+  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
cb347a4
   { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
cb347a4
   { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
cb347a4
   { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/elflink.c bfd/elflink.c
cb347a4
--- a/bfd/elflink.c	2013-05-14 16:39:25.280717776 +0100
cb347a4
+++ b/bfd/elflink.c	2013-05-14 16:40:06.997718932 +0100
cb347a4
@@ -5127,6 +5127,9 @@
cb347a4
 	     something wrong with the archive.  */
cb347a4
 	  if (element->archive_pass != 0)
cb347a4
 	    {
cb347a4
+	      /* Don't load the IR archive member twice.  */
cb347a4
+	      if (element->lto_type == lto_ir_object)
cb347a4
+		continue;
cb347a4
 	      bfd_set_error (bfd_error_bad_value);
cb347a4
 	      goto error_return;
cb347a4
 	    }
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/format.c bfd/format.c
cb347a4
--- a/bfd/format.c	2013-05-14 16:39:24.265717748 +0100
cb347a4
+++ b/bfd/format.c	2013-05-14 16:40:07.006718933 +0100
cb347a4
@@ -95,6 +95,33 @@
cb347a4
   return bfd_check_format_matches (abfd, format, NULL);
cb347a4
 }
cb347a4
 
cb347a4
+/* Set lto_type in ABFD.  */
cb347a4
+
cb347a4
+static void
cb347a4
+bfd_set_lto_type (bfd *abfd)
cb347a4
+{
cb347a4
+  if (abfd->format == bfd_object
cb347a4
+      && abfd->lto_type == lto_non_object
cb347a4
+      && (abfd->flags & (DYNAMIC | EXEC_P)) == 0)
cb347a4
+    {
cb347a4
+      asection *sec;
cb347a4
+      enum bfd_lto_object_type type = lto_non_ir_object;
cb347a4
+      for (sec = abfd->sections; sec != NULL; sec = sec->next)
cb347a4
+	{
cb347a4
+	  if (strcmp (sec->name, GNU_OBJECT_ONLY_SECTION_NAME) == 0)
cb347a4
+	    {
cb347a4
+	      type = lto_mixed_object;
cb347a4
+	      abfd->object_only_section = sec;
cb347a4
+	      break;
cb347a4
+	    }
cb347a4
+	  else if (type != lto_ir_object
cb347a4
+		   && strncmp (sec->name, ".gnu.lto_", 9) == 0)
cb347a4
+	    type = lto_ir_object;
cb347a4
+	}
cb347a4
+      abfd->lto_type = type;
cb347a4
+    }
cb347a4
+}
cb347a4
+
cb347a4
 struct bfd_preserve
cb347a4
 {
cb347a4
   void *marker;
cb347a4
@@ -136,7 +163,10 @@
cb347a4
     }
cb347a4
 
cb347a4
   if (abfd->format != bfd_unknown)
cb347a4
-    return abfd->format == format;
cb347a4
+    {
cb347a4
+      bfd_set_lto_type (abfd);
cb347a4
+      return abfd->format == format;
cb347a4
+    }
cb347a4
 
cb347a4
   if (matching != NULL || *bfd_associated_vector != NULL)
cb347a4
     {
cb347a4
@@ -322,6 +352,9 @@
cb347a4
 
cb347a4
       if (matching_vector)
cb347a4
 	free (matching_vector);
cb347a4
+
cb347a4
+      bfd_set_lto_type (abfd);
cb347a4
+
cb347a4
 
cb347a4
       /* File position has moved, BTW.  */
cb347a4
       return TRUE;
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/opncls.c bfd/opncls.c
cb347a4
--- a/bfd/opncls.c	2013-05-14 16:39:23.701717732 +0100
cb347a4
+++ b/bfd/opncls.c	2013-05-14 16:40:07.008718933 +0100
cb347a4
@@ -1569,3 +1569,69 @@
cb347a4
 
cb347a4
   return TRUE;
cb347a4
 }
cb347a4
+
cb347a4
+/*
cb347a4
+FUNCTION
cb347a4
+	bfd_extract_object_only_section
cb347a4
+
cb347a4
+SYNOPSIS
cb347a4
+	const char *bfd_extract_object_only_section
cb347a4
+	  (bfd *abfd);
cb347a4
+
cb347a4
+DESCRIPTION
cb347a4
+
cb347a4
+	Takes a @var{ABFD} and extract the .gnu_object_only section into
cb347a4
+	a temporary file.
cb347a4
+
cb347a4
+RETURNS
cb347a4
+	The name of the temporary file is returned if all is ok.
cb347a4
+	Otherwise <<NULL>> is returned and bfd_error is set.
cb347a4
+*/
cb347a4
+
cb347a4
+const char *
cb347a4
+bfd_extract_object_only_section (bfd *abfd)
cb347a4
+{
cb347a4
+  asection *sec = abfd->object_only_section;
cb347a4
+  const char *name;
cb347a4
+  FILE *file;
cb347a4
+  bfd_byte *memhunk = NULL;
cb347a4
+  size_t off, size;
cb347a4
+  bfd_error_type err;
cb347a4
+
cb347a4
+  /* Get a temporary object-only file.  */
cb347a4
+  name = make_temp_file (".obj-only.o");
cb347a4
+
cb347a4
+  /* Open the object-only file.  */
cb347a4
+  file = real_fopen (name, FOPEN_WB);
cb347a4
+  if (!bfd_get_full_section_contents (abfd, sec, &memhunk))
cb347a4
+    {
cb347a4
+      err = bfd_get_error ();
cb347a4
+
cb347a4
+loser:
cb347a4
+      free (memhunk);
cb347a4
+      fclose (file);
cb347a4
+      unlink (name);
cb347a4
+      bfd_set_error (err);
cb347a4
+      return NULL;
cb347a4
+    }
cb347a4
+
cb347a4
+  off = 0;
cb347a4
+  size = sec->size;
cb347a4
+  while (off != size)
cb347a4
+    {
cb347a4
+      size_t written, nwrite = size - off;
cb347a4
+
cb347a4
+      written = fwrite (memhunk + off, 1, nwrite, file);
cb347a4
+      if (written < nwrite && ferror (file))
cb347a4
+	{
cb347a4
+	  err = bfd_error_system_call;
cb347a4
+	  goto loser;
cb347a4
+	}
cb347a4
+
cb347a4
+      off += written;
cb347a4
+    }
cb347a4
+
cb347a4
+  free (memhunk);
cb347a4
+  fclose (file);
cb347a4
+  return name;
cb347a4
+}
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/plugin.c bfd/plugin.c
cb347a4
--- a/bfd/plugin.c	2013-05-14 16:39:25.137717772 +0100
cb347a4
+++ b/bfd/plugin.c	2013-05-14 16:40:07.010718933 +0100
cb347a4
@@ -130,6 +130,139 @@
cb347a4
   return LDPS_OK;
cb347a4
 }
cb347a4
 
cb347a4
+static asection bfd_plugin_fake_text_section
cb347a4
+  = BFD_FAKE_SECTION (bfd_plugin_fake_text_section, 0, 0, ".text", 0);
cb347a4
+static asection bfd_plugin_fake_common_section
cb347a4
+  = BFD_FAKE_SECTION (bfd_plugin_fake_common_section, SEC_IS_COMMON, 0,
cb347a4
+		      NULL, 0);
cb347a4
+
cb347a4
+/* Get symbols from object only section.  */
cb347a4
+
cb347a4
+static void
cb347a4
+bfd_plugin_get_symbols_in_object_only (bfd *abfd)
cb347a4
+{
cb347a4
+  struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
cb347a4
+  const char *object_only_file;
cb347a4
+  bfd *nbfd;
cb347a4
+  long storage;
cb347a4
+  long object_only_nsyms, added_nsyms, i;
cb347a4
+  asymbol **object_only_syms, **added_syms;
cb347a4
+
cb347a4
+  plugin_data->object_only_syms = NULL;
cb347a4
+  plugin_data->object_only_nsyms = 0;
cb347a4
+
cb347a4
+  if (abfd->sections == NULL && abfd->my_archive == NULL)
cb347a4
+    {
cb347a4
+      nbfd = bfd_openr (abfd->filename, NULL);
cb347a4
+      if (nbfd == NULL || !bfd_check_format (nbfd, bfd_object))
cb347a4
+	{
cb347a4
+	  (*_bfd_error_handler)
cb347a4
+	    (_("%s: failed to open to extract object only section: %s"),
cb347a4
+	     abfd->filename, bfd_errmsg (bfd_get_error ()));
cb347a4
+	  bfd_close (nbfd);
cb347a4
+	  return;
cb347a4
+	}
cb347a4
+    }
cb347a4
+  else
cb347a4
+    {
cb347a4
+      if (!bfd_check_format (abfd, bfd_object))
cb347a4
+	{
cb347a4
+	  (*_bfd_error_handler)
cb347a4
+	    (_("%B: invalid file to extract object only section: %s"),
cb347a4
+	     abfd, bfd_errmsg (bfd_get_error ()));
cb347a4
+	  return;
cb347a4
+	}
cb347a4
+      nbfd = abfd;
cb347a4
+    }
cb347a4
+
cb347a4
+  if (nbfd->lto_type == lto_mixed_object
cb347a4
+      && (nbfd->flags & HAS_SYMS) != 0)
cb347a4
+    {
cb347a4
+      object_only_file = bfd_extract_object_only_section (nbfd);
cb347a4
+      if (object_only_file == NULL)
cb347a4
+	(*_bfd_error_handler)
cb347a4
+	  (_("%B: failed to extract object only section: %s"),
cb347a4
+	   abfd, bfd_errmsg (bfd_get_error ()));
cb347a4
+    }
cb347a4
+  else
cb347a4
+    object_only_file = NULL;
cb347a4
+
cb347a4
+  /* Close the new bfd we just opened.  */
cb347a4
+  if (nbfd != abfd)
cb347a4
+    bfd_close (nbfd);
cb347a4
+
cb347a4
+  /* Return if there is no object only section or there is no
cb347a4
+     symbol in object only section.  */
cb347a4
+  if (!object_only_file)
cb347a4
+    return;
cb347a4
+
cb347a4
+  /* Open the file containing object only section.  */
cb347a4
+  nbfd = bfd_openr (object_only_file, NULL);
cb347a4
+  if (!bfd_check_format (nbfd, bfd_object))
cb347a4
+    {
cb347a4
+      (*_bfd_error_handler)
cb347a4
+	(_("%B: failed to open object only section: %s"),
cb347a4
+	 abfd, bfd_errmsg (bfd_get_error ()));
cb347a4
+      goto quit;
cb347a4
+    }
cb347a4
+
cb347a4
+  storage = bfd_get_symtab_upper_bound (nbfd);
cb347a4
+  if (storage <= 0)
cb347a4
+    {
cb347a4
+      if (storage < 0)
cb347a4
+	(*_bfd_error_handler)
cb347a4
+	  (_("%B: failed to get symbol table in object only section: %s"),
cb347a4
+	   abfd, bfd_errmsg (bfd_get_error ()));
cb347a4
+
cb347a4
+      goto quit;
cb347a4
+    }
cb347a4
+
cb347a4
+  object_only_syms = (asymbol **) bfd_malloc (storage);
cb347a4
+  object_only_nsyms = bfd_canonicalize_symtab (nbfd, object_only_syms);
cb347a4
+
cb347a4
+  /* FIXME: We waste some spaces if not all symbols are copied.  */
cb347a4
+  added_syms = (asymbol **) bfd_alloc (abfd, storage);
cb347a4
+  added_nsyms = 0;
cb347a4
+
cb347a4
+  /* Copy only global symbols from object only section.  */
cb347a4
+  for (i = 0; i < object_only_nsyms; i++)
cb347a4
+    {
cb347a4
+      asection *sec = object_only_syms[i]->section;
cb347a4
+      flagword flags = object_only_syms[i]->flags;
cb347a4
+      asymbol *s;
cb347a4
+
cb347a4
+      if (bfd_is_com_section (sec))
cb347a4
+	sec = &bfd_plugin_fake_common_section;
cb347a4
+      else if (bfd_is_und_section (sec))
cb347a4
+	;
cb347a4
+      else if ((flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0)
cb347a4
+	sec = &bfd_plugin_fake_text_section;
cb347a4
+      else
cb347a4
+	continue;
cb347a4
+
cb347a4
+      s = bfd_alloc (abfd, sizeof (asymbol)); 
cb347a4
+      BFD_ASSERT (s);
cb347a4
+      added_syms[added_nsyms++] = s;
cb347a4
+
cb347a4
+      s->section = sec;
cb347a4
+      s->the_bfd = abfd;
cb347a4
+      s->name = xstrdup (object_only_syms[i]->name);
cb347a4
+      s->value = 0;
cb347a4
+      s->flags = flags;
cb347a4
+      s->udata.p = NULL;
cb347a4
+    }
cb347a4
+
cb347a4
+  plugin_data->object_only_syms = added_syms;
cb347a4
+  plugin_data->object_only_nsyms = added_nsyms;
cb347a4
+
cb347a4
+  free (object_only_syms);
cb347a4
+
cb347a4
+quit:
cb347a4
+  /* Close and remove the object only section file.  */
cb347a4
+  bfd_close (nbfd);
cb347a4
+  unlink (object_only_file);
cb347a4
+}
cb347a4
+
cb347a4
 static enum ld_plugin_status
cb347a4
 add_symbols (void * handle,
cb347a4
 	     int nsyms,
cb347a4
@@ -142,7 +275,9 @@
cb347a4
   plugin_data->nsyms = nsyms;
cb347a4
   plugin_data->syms = syms;
cb347a4
 
cb347a4
-  if (nsyms != 0)
cb347a4
+  bfd_plugin_get_symbols_in_object_only (abfd);
cb347a4
+
cb347a4
+  if ((nsyms + plugin_data->object_only_nsyms) != 0)
cb347a4
     abfd->flags |= HAS_SYMS;
cb347a4
 
cb347a4
   abfd->tdata.plugin_data = plugin_data;
cb347a4
@@ -389,7 +524,8 @@
cb347a4
 bfd_plugin_get_symtab_upper_bound (bfd *abfd)
cb347a4
 {
cb347a4
   struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
cb347a4
-  long nsyms = plugin_data->nsyms;
cb347a4
+  /* Add symbols from object only section.  */
cb347a4
+  long nsyms = plugin_data->nsyms + plugin_data->object_only_nsyms;
cb347a4
 
cb347a4
   BFD_ASSERT (nsyms >= 0);
cb347a4
 
cb347a4
@@ -423,12 +559,7 @@
cb347a4
   struct plugin_data_struct *plugin_data = abfd->tdata.plugin_data;
cb347a4
   long nsyms = plugin_data->nsyms;
cb347a4
   const struct ld_plugin_symbol *syms = plugin_data->syms;
cb347a4
-  static asection fake_section;
cb347a4
-  static asection fake_common_section;
cb347a4
-  int i;
cb347a4
-
cb347a4
-  fake_section.name = ".text";
cb347a4
-  fake_common_section.flags = SEC_IS_COMMON;
cb347a4
+  int i, j;
cb347a4
 
cb347a4
   for (i = 0; i < nsyms; i++)
cb347a4
     {
cb347a4
@@ -441,10 +572,11 @@
cb347a4
       s->name = syms[i].name;
cb347a4
       s->value = 0;
cb347a4
       s->flags = convert_flags (&syms[i]);
cb347a4
+      s->udata.p = NULL;
cb347a4
       switch (syms[i].def)
cb347a4
 	{
cb347a4
 	case LDPK_COMMON:
cb347a4
-	  s->section = &fake_common_section;
cb347a4
+	  s->section = &bfd_plugin_fake_common_section;
cb347a4
 	  break;
cb347a4
 	case LDPK_UNDEF:
cb347a4
 	case LDPK_WEAKUNDEF:
cb347a4
@@ -452,15 +584,18 @@
cb347a4
 	  break;
cb347a4
 	case LDPK_DEF:
cb347a4
 	case LDPK_WEAKDEF:
cb347a4
-	  s->section = &fake_section;
cb347a4
+	  s->section = &bfd_plugin_fake_text_section;
cb347a4
 	  break;
cb347a4
 	default:
cb347a4
 	  BFD_ASSERT (0);
cb347a4
 	}
cb347a4
-
cb347a4
-      s->udata.p = (void *) &syms[i];
cb347a4
     }
cb347a4
 
cb347a4
+  /* Copy symbols from object only section.  */
cb347a4
+  nsyms += plugin_data->object_only_nsyms;
cb347a4
+  for (j = 0; j < plugin_data->object_only_nsyms; j++, i++)
cb347a4
+    alocation[i] = plugin_data->object_only_syms[j];
cb347a4
+
cb347a4
   return nsyms;
cb347a4
 }
cb347a4
 
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/plugin.h bfd/plugin.h
cb347a4
--- a/bfd/plugin.h	2013-05-14 16:39:25.481717782 +0100
cb347a4
+++ b/bfd/plugin.h	2013-05-14 16:40:07.010718933 +0100
cb347a4
@@ -30,6 +30,8 @@
cb347a4
 {
cb347a4
   int nsyms;
cb347a4
   const struct ld_plugin_symbol *syms;
cb347a4
+  int object_only_nsyms;
cb347a4
+  asymbol **object_only_syms;
cb347a4
 }
cb347a4
 plugin_data_struct;
cb347a4
 
d43b721
diff -cpr ../binutils-2.23.2.orig/bfd/section.c bfd/section.c
cb347a4
--- a/bfd/section.c	2013-05-14 16:39:26.180717801 +0100
cb347a4
+++ b/bfd/section.c	2013-05-14 16:43:06.082723897 +0100
cb347a4
@@ -571,6 +571,9 @@
cb347a4
 .  || ((SEC) == bfd_com_section_ptr)		\
cb347a4
 .  || ((SEC) == bfd_ind_section_ptr))
cb347a4
 .
cb347a4
+.{* GNU object-only section name.  *}
cb347a4
+.#define GNU_OBJECT_ONLY_SECTION_NAME ".gnu_object_only"
cb347a4
+.
cb347a4
 .{* Macros to handle insertion and deletion of a bfd's sections.  These
cb347a4
 .   only handle the list pointers, ie. do not adjust section_count,
cb347a4
 .   target_index etc.  *}
d43b721
diff -cpr ../binutils-2.23.2.orig/binutils/objcopy.c binutils/objcopy.c
cb347a4
--- a/binutils/objcopy.c	2013-05-14 16:39:36.214718079 +0100
cb347a4
+++ b/binutils/objcopy.c	2013-05-14 16:45:04.631727183 +0100
cb347a4
@@ -943,30 +943,6 @@
cb347a4
   return htab_find (htab, name) != NULL;
cb347a4
 }
cb347a4
 
cb347a4
-/* Return a pointer to the symbol used as a signature for GROUP.  */
cb347a4
-
cb347a4
-static asymbol *
cb347a4
-group_signature (asection *group)
cb347a4
-{
cb347a4
-  bfd *abfd = group->owner;
cb347a4
-  Elf_Internal_Shdr *ghdr;
cb347a4
-
cb347a4
-  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
cb347a4
-    return NULL;
cb347a4
-
cb347a4
-  ghdr = &elf_section_data (group)->this_hdr;
cb347a4
-  if (ghdr->sh_link < elf_numsections (abfd))
cb347a4
-    {
cb347a4
-      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
cb347a4
-      Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
cb347a4
-
cb347a4
-      if (symhdr->sh_type == SHT_SYMTAB
cb347a4
-	  && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
cb347a4
-	return isympp[ghdr->sh_info - 1];
cb347a4
-    }
cb347a4
-  return NULL;
cb347a4
-}
cb347a4
-
cb347a4
 /* Return TRUE if the section is a DWO section.  */
cb347a4
 
cb347a4
 static bfd_boolean
cb347a4
@@ -1034,7 +1010,7 @@
cb347a4
       /* PR binutils/3181
cb347a4
 	 If we are going to strip the group signature symbol, then
cb347a4
 	 strip the group section too.  */
cb347a4
-      gsym = group_signature (sec);
cb347a4
+      gsym = bfd_group_signature (sec, isympp);
cb347a4
       if (gsym != NULL)
cb347a4
 	gname = gsym->name;
cb347a4
       else
cb347a4
@@ -2633,7 +2609,7 @@
cb347a4
 
cb347a4
   if ((isection->flags & SEC_GROUP) != 0)
cb347a4
     {
cb347a4
-      asymbol *gsym = group_signature (isection);
cb347a4
+      asymbol *gsym = bfd_group_signature (isection, isympp);
cb347a4
 
cb347a4
       if (gsym != NULL)
cb347a4
 	{
d43b721
diff -cpr ../binutils-2.23.2.orig/binutils/readelf.c binutils/readelf.c
cb347a4
--- a/binutils/readelf.c	2013-05-14 16:39:35.668718064 +0100
cb347a4
+++ b/binutils/readelf.c	2013-05-14 16:40:07.022718933 +0100
cb347a4
@@ -3101,6 +3101,7 @@
cb347a4
     case 0x7ffffffd:		return "AUXILIARY";
cb347a4
     case 0x7fffffff:		return "FILTER";
cb347a4
     case SHT_GNU_LIBLIST:	return "GNU_LIBLIST";
cb347a4
+    case SHT_GNU_OBJECT_ONLY:	return "GNU_OBJECT_ONLY";
cb347a4
 
cb347a4
     default:
cb347a4
       if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
d43b721
diff -cpr ../binutils-2.23.2.orig/include/bfdlink.h include/bfdlink.h
cb347a4
--- a/include/bfdlink.h	2013-05-14 16:39:19.745717623 +0100
cb347a4
+++ b/include/bfdlink.h	2013-05-14 16:40:07.023718933 +0100
cb347a4
@@ -380,6 +380,12 @@
cb347a4
   /* TRUE if ok to have multiple definition.  */
cb347a4
   unsigned int allow_multiple_definition: 1;
cb347a4
 
cb347a4
+  /* TRUE if .gnu_object_only section should be created.  */
cb347a4
+  unsigned int emit_gnu_object_only: 1;
cb347a4
+
cb347a4
+  /* TRUE if .gnu_object_only section is being created.  */
cb347a4
+  unsigned int emitting_gnu_object_only: 1;
cb347a4
+
cb347a4
   /* TRUE if ok to have version with no definition.  */
cb347a4
   unsigned int allow_undefined_version: 1;
cb347a4
 
d43b721
diff -cpr ../binutils-2.23.2.orig/include/elf/common.h include/elf/common.h
cb347a4
--- a/include/elf/common.h	2013-05-14 16:39:19.628717619 +0100
cb347a4
+++ b/include/elf/common.h	2013-05-14 16:40:07.023718933 +0100
cb347a4
@@ -472,6 +472,7 @@
cb347a4
 #define SHT_GNU_ATTRIBUTES 0x6ffffff5	/* Object attributes */
cb347a4
 #define SHT_GNU_HASH	0x6ffffff6	/* GNU style symbol hash table */
cb347a4
 #define SHT_GNU_LIBLIST	0x6ffffff7	/* List of prelink dependencies */
cb347a4
+#define SHT_GNU_OBJECT_ONLY 0x6ffffff8	/* Object only */
cb347a4
 
cb347a4
 /* The next three section types are defined by Solaris, and are named
cb347a4
    SHT_SUNW*.  We use them in GNU code, so we also define SHT_GNU*
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/emultempl/alphaelf.em ld/emultempl/alphaelf.em
cb347a4
--- a/ld/emultempl/alphaelf.em	2013-05-14 16:39:37.461718114 +0100
cb347a4
+++ b/ld/emultempl/alphaelf.em	2013-05-14 16:40:07.030718933 +0100
cb347a4
@@ -100,7 +100,7 @@
cb347a4
   if (limit_32bit)
cb347a4
     elf_elfheader (link_info.output_bfd)->e_flags |= EF_ALPHA_32BIT;
cb347a4
 
cb347a4
-  finish_default ();
cb347a4
+  gld${EMULATION_NAME}_finish ();
cb347a4
 }
cb347a4
 EOF
cb347a4
 
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/emultempl/armelf.em ld/emultempl/armelf.em
cb347a4
--- a/ld/emultempl/armelf.em	2013-05-14 16:39:37.458718114 +0100
cb347a4
+++ b/ld/emultempl/armelf.em	2013-05-14 16:40:07.033718933 +0100
cb347a4
@@ -366,7 +366,7 @@
cb347a4
 }
cb347a4
 
cb347a4
 static void
cb347a4
-gld${EMULATION_NAME}_finish (void)
cb347a4
+arm_finish (void)
cb347a4
 {
cb347a4
   struct bfd_link_hash_entry * h;
cb347a4
 
cb347a4
@@ -389,7 +389,7 @@
cb347a4
 	}
cb347a4
     }
cb347a4
 
cb347a4
-  finish_default ();
cb347a4
+  gld${EMULATION_NAME}_finish ();
cb347a4
 
cb347a4
   if (thumb_entry_symbol)
cb347a4
     {
cb347a4
@@ -690,4 +690,4 @@
cb347a4
 LDEMUL_BEFORE_PARSE=gld"${EMULATION_NAME}"_before_parse
cb347a4
 
cb347a4
 # Call the extra arm-elf function
cb347a4
-LDEMUL_FINISH=gld${EMULATION_NAME}_finish
cb347a4
+LDEMUL_FINISH=arm_finish
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/emultempl/elf32.em ld/emultempl/elf32.em
cb347a4
--- a/ld/emultempl/elf32.em	2013-05-14 16:39:37.465718114 +0100
cb347a4
+++ b/ld/emultempl/elf32.em	2013-05-14 16:40:07.035718933 +0100
cb347a4
@@ -68,6 +68,7 @@
cb347a4
 static void gld${EMULATION_NAME}_after_allocation (void);
cb347a4
 static lang_output_section_statement_type *gld${EMULATION_NAME}_place_orphan
cb347a4
   (asection *, const char *, int);
cb347a4
+static void gld${EMULATION_NAME}_finish (void);
cb347a4
 EOF
cb347a4
 
cb347a4
 if [ "x${USE_LIBPATH}" = xyes ] ; then
cb347a4
@@ -1764,6 +1765,8 @@
cb347a4
   return last;
cb347a4
 }
cb347a4
 
cb347a4
+static int orphan_init_done = 0;
cb347a4
+
cb347a4
 /* Place an orphan section.  We use this to put random SHF_ALLOC
cb347a4
    sections in the right segment.  */
cb347a4
 
cb347a4
@@ -1772,7 +1775,7 @@
cb347a4
 				   const char *secname,
cb347a4
 				   int constraint)
cb347a4
 {
cb347a4
-  static struct orphan_save hold[] =
cb347a4
+  static struct orphan_save orig_hold[] =
cb347a4
     {
cb347a4
       { ".text",
cb347a4
 	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
cb347a4
@@ -1799,6 +1802,7 @@
cb347a4
 	SEC_HAS_CONTENTS,
cb347a4
 	0, 0, 0, 0 },
cb347a4
     };
cb347a4
+  static struct orphan_save hold[ARRAY_SIZE (orig_hold)];
cb347a4
   enum orphan_save_index
cb347a4
     {
cb347a4
       orphan_text = 0,
cb347a4
@@ -1810,7 +1814,6 @@
cb347a4
       orphan_sdata,
cb347a4
       orphan_nonalloc
cb347a4
     };
cb347a4
-  static int orphan_init_done = 0;
cb347a4
   struct orphan_save *place;
cb347a4
   lang_output_section_statement_type *after;
cb347a4
   lang_output_section_statement_type *os;
cb347a4
@@ -1887,15 +1890,22 @@
cb347a4
 
cb347a4
   if (!orphan_init_done)
cb347a4
     {
cb347a4
-      struct orphan_save *ho;
cb347a4
+      struct orphan_save *ho, *horig;
cb347a4
 
cb347a4
       for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
cb347a4
+      for (ho = hold, horig = orig_hold;
cb347a4
+	   ho < hold + ARRAY_SIZE (hold);
cb347a4
+	   ++ho, ++horig)
cb347a4
+	{
cb347a4
+	  *ho = *horig;
cb347a4
+	  if (ho->name != NULL)
cb347a4
 	if (ho->name != NULL)
cb347a4
 	  {
cb347a4
 	    ho->os = lang_output_section_find (ho->name);
cb347a4
 	    if (ho->os != NULL && ho->os->flags == 0)
cb347a4
 	      ho->os->flags = ho->flags;
cb347a4
 	  }
cb347a4
+	}
cb347a4
       orphan_init_done = 1;
cb347a4
     }
cb347a4
 
cb347a4
@@ -1965,6 +1975,27 @@
cb347a4
 EOF
cb347a4
 fi
cb347a4
 
cb347a4
+fragment <
cb347a4
+
cb347a4
+/* Final emulation specific call.  */
cb347a4
+
cb347a4
+static void
cb347a4
+gld${EMULATION_NAME}_finish (void)
cb347a4
+{
cb347a4
+EOF
cb347a4
+if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
cb347a4
+fragment <
cb347a4
+  /* Support the object-only output.  */
cb347a4
+  if (link_info.emit_gnu_object_only)
cb347a4
+    orphan_init_done = 0;
cb347a4
+
cb347a4
+EOF
cb347a4
+fi
cb347a4
+fragment <
cb347a4
+  finish_default ();
cb347a4
+}
cb347a4
+EOF
cb347a4
+
cb347a4
 if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then
cb347a4
 fragment <
cb347a4
 
cb347a4
@@ -2497,7 +2528,7 @@
cb347a4
   ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
cb347a4
   "${EMULATION_NAME}",
cb347a4
   "${OUTPUT_FORMAT}",
cb347a4
-  ${LDEMUL_FINISH-finish_default},
cb347a4
+  ${LDEMUL_FINISH-gld${EMULATION_NAME}_finish},
cb347a4
   ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
cb347a4
   ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
cb347a4
   ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/emultempl/ppc64elf.em ld/emultempl/ppc64elf.em
cb347a4
--- a/ld/emultempl/ppc64elf.em	2013-05-14 16:39:37.454718113 +0100
cb347a4
+++ b/ld/emultempl/ppc64elf.em	2013-05-14 16:40:07.044718934 +0100
cb347a4
@@ -533,7 +533,7 @@
cb347a4
 /* Final emulation specific call.  */
cb347a4
 
cb347a4
 static void
cb347a4
-gld${EMULATION_NAME}_finish (void)
cb347a4
+ppc_finish (void)
cb347a4
 {
cb347a4
   /* e_entry on PowerPC64 points to the function descriptor for
cb347a4
      _start.  If _start is missing, default to the first function
cb347a4
@@ -565,7 +565,7 @@
cb347a4
     }
cb347a4
 
cb347a4
   ppc64_elf_restore_symbols (&link_info);
cb347a4
-  finish_default ();
cb347a4
+  gld${EMULATION_NAME}_finish ();
cb347a4
 }
cb347a4
 
cb347a4
 
cb347a4
@@ -867,6 +867,6 @@
cb347a4
 #
cb347a4
 LDEMUL_BEFORE_ALLOCATION=ppc_before_allocation
cb347a4
 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
cb347a4
-LDEMUL_FINISH=gld${EMULATION_NAME}_finish
cb347a4
+LDEMUL_FINISH=ppc_finish
cb347a4
 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=ppc_create_output_section_statements
cb347a4
 LDEMUL_NEW_VERS_PATTERN=gld${EMULATION_NAME}_new_vers_pattern
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/emultempl/spuelf.em ld/emultempl/spuelf.em
cb347a4
--- a/ld/emultempl/spuelf.em	2013-05-14 16:39:37.453718113 +0100
cb347a4
+++ b/ld/emultempl/spuelf.em	2013-05-14 16:40:07.045718934 +0100
cb347a4
@@ -416,7 +416,7 @@
cb347a4
 /* Final emulation specific call.  */
cb347a4
 
cb347a4
 static void
cb347a4
-gld${EMULATION_NAME}_finish (void)
cb347a4
+spu_finish (void)
cb347a4
 {
cb347a4
   if (is_spu_target ())
cb347a4
     {
cb347a4
@@ -432,7 +432,7 @@
cb347a4
 	einfo ("%P: --auto-overlay ignored with zero local store range\n");
cb347a4
     }
cb347a4
 
cb347a4
-  finish_default ();
cb347a4
+  gld${EMULATION_NAME}_finish ();
cb347a4
 }
cb347a4
 
cb347a4
 static char *
cb347a4
@@ -832,5 +832,5 @@
cb347a4
 
cb347a4
 LDEMUL_AFTER_OPEN=spu_after_open
cb347a4
 LDEMUL_BEFORE_ALLOCATION=spu_before_allocation
cb347a4
-LDEMUL_FINISH=gld${EMULATION_NAME}_finish
cb347a4
+LDEMUL_FINISH=spu_finish
cb347a4
 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/ldfile.c ld/ldfile.c
cb347a4
--- a/ld/ldfile.c	2013-05-14 16:39:37.164718105 +0100
cb347a4
+++ b/ld/ldfile.c	2013-05-14 16:40:07.046718934 +0100
cb347a4
@@ -319,7 +319,9 @@
cb347a4
 	  plugin_maybe_claim (&file, entry);
cb347a4
 	}
cb347a4
     }
cb347a4
+  else
cb347a4
 #endif /* ENABLE_PLUGINS */
cb347a4
+    cmdline_check_object_only_section (entry->the_bfd, FALSE);
cb347a4
 
cb347a4
   /* It opened OK, the format checked out, and the plugins have had
cb347a4
      their chance to claim it, so this is success.  */
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/ldlang.c ld/ldlang.c
cb347a4
--- a/ld/ldlang.c	2013-05-14 16:39:39.704718176 +0100
cb347a4
+++ b/ld/ldlang.c	2013-05-14 17:03:54.030758491 +0100
cb347a4
@@ -38,6 +38,7 @@
cb347a4
 #include "ldctor.h"
cb347a4
 #include "ldfile.h"
cb347a4
 #include "ldemul.h"
cb347a4
+#include "ldwrite.h"
cb347a4
 #include "fnmatch.h"
cb347a4
 #include "demangle.h"
cb347a4
 #include "hashtab.h"
cb347a4
@@ -46,6 +47,9 @@
cb347a4
 #include "plugin.h"
cb347a4
 #endif /* ENABLE_PLUGINS */
cb347a4
 
cb347a4
+/* FIXME: Put it here to avoid NAME conflict from ldgram.h.  */
cb347a4
+#include "elf-bfd.h"
cb347a4
+
cb347a4
 #ifndef offsetof
cb347a4
 #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
cb347a4
 #endif
cb347a4
@@ -69,6 +73,9 @@
cb347a4
 static lang_statement_list_type *stat_save[10];
cb347a4
 static lang_statement_list_type **stat_save_ptr = &stat_save[0];
cb347a4
 static struct unique_sections *unique_section_list;
cb347a4
+static cmdline_list_type cmdline_object_only_file_list;
cb347a4
+static cmdline_list_type cmdline_object_only_archive_list;
cb347a4
+static cmdline_list_type cmdline_temp_object_only_list;
cb347a4
 
cb347a4
 /* Forward declarations.  */
cb347a4
 static void exp_init_os (etree_type *);
cb347a4
@@ -89,6 +96,10 @@
cb347a4
 static void lang_do_version_exports_section (void);
cb347a4
 static void lang_finalize_version_expr_head
cb347a4
   (struct bfd_elf_version_expr_head *);
cb347a4
+static void cmdline_lists_init (void);
cb347a4
+static void cmdline_get_object_only_input_files (void);
cb347a4
+static void print_cmdline_list (cmdline_union_type *);
cb347a4
+static bfd_boolean cmdline_on_object_only_archive_list_p (bfd *);
cb347a4
 
cb347a4
 /* Exported variables.  */
cb347a4
 const char *output_target;
cb347a4
@@ -1204,14 +1215,17 @@
cb347a4
 /* Build enough state so that the parser can build its tree.  */
cb347a4
 
cb347a4
 void
cb347a4
-lang_init (void)
cb347a4
+lang_init (bfd_boolean object_only)
cb347a4
 {
cb347a4
-  obstack_begin (&stat_obstack, 1000);
cb347a4
+  if (!object_only)
cb347a4
+    obstack_begin (&stat_obstack, 1000);
cb347a4
 
cb347a4
   stat_ptr = &statement_list;
cb347a4
 
cb347a4
   output_section_statement_table_init ();
cb347a4
 
cb347a4
+  cmdline_lists_init ();
cb347a4
+
cb347a4
   lang_list_init (stat_ptr);
cb347a4
 
cb347a4
   lang_list_init (&input_file_chain);
cb347a4
@@ -1230,10 +1244,11 @@
cb347a4
      simpler to re-use working machinery than using a linked list in terms
cb347a4
      of code-complexity here in ld, besides the initialization which just
cb347a4
      looks like other code here.  */
cb347a4
-  if (!bfd_hash_table_init_n (&lang_definedness_table,
cb347a4
-			      lang_definedness_newfunc,
cb347a4
-			      sizeof (struct lang_definedness_hash_entry),
cb347a4
-			      3))
cb347a4
+  if (!object_only
cb347a4
+      && !bfd_hash_table_init_n (&lang_definedness_table,
cb347a4
+				 lang_definedness_newfunc,
cb347a4
+				 sizeof (struct lang_definedness_hash_entry),
cb347a4
+				 3))
cb347a4
     einfo (_("%P%F: can not create hash table: %E\n"));
cb347a4
 }
cb347a4
 
cb347a4
@@ -2769,6 +2784,12 @@
cb347a4
 		  loaded = FALSE;
cb347a4
 		}
cb347a4
 
cb347a4
+	      if (link_info.emitting_gnu_object_only)
cb347a4
+		{
cb347a4
+		  if (!cmdline_on_object_only_archive_list_p (member))
cb347a4
+		    continue;
cb347a4
+		}
cb347a4
+
cb347a4
 	      subsbfd = member;
cb347a4
 	      if (!(*link_info.callbacks
cb347a4
 		    ->add_archive_element) (&link_info, member,
cb347a4
@@ -6659,7 +6680,37 @@
cb347a4
 	  open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
cb347a4
 	}
cb347a4
     }
cb347a4
+  else
cb347a4
 #endif /* ENABLE_PLUGINS */
cb347a4
+    if (link_info.relocatable)
cb347a4
+    {
cb347a4
+      /* Check if .gnu_object_only section should be created.  */
cb347a4
+      bfd *p;
cb347a4
+      int object_type;
cb347a4
+
cb347a4
+      object_type = 0;
cb347a4
+      for (p = link_info.input_bfds; p != (bfd *) NULL; p = p->link_next)
cb347a4
+	{
cb347a4
+	  object_type |= 1 << p->lto_type;
cb347a4
+	  if ((object_type & (1 << lto_mixed_object)) != 0
cb347a4
+	      || ((object_type
cb347a4
+		   & (1 << lto_non_ir_object
cb347a4
+		      | 1 << lto_ir_object))
cb347a4
+		  == (1 << lto_non_ir_object | 1 << lto_ir_object)))
cb347a4
+	    {
cb347a4
+	      link_info.emit_gnu_object_only = TRUE;
cb347a4
+	      break;
cb347a4
+	    }
cb347a4
+	}
cb347a4
+
cb347a4
+      if (trace_files
cb347a4
+	  && (cmdline_object_only_file_list.head
cb347a4
+	      || cmdline_object_only_archive_list.head))
cb347a4
+	{
cb347a4
+	  print_cmdline_list (cmdline_object_only_file_list.head);
cb347a4
+	  print_cmdline_list (cmdline_object_only_archive_list.head);
cb347a4
+	}
cb347a4
+    }
cb347a4
 
cb347a4
   link_info.gc_sym_list = &entry_symbol;
cb347a4
   if (entry_symbol.name == NULL)
cb347a4
@@ -8059,3 +8111,961 @@
cb347a4
       p = q;
cb347a4
     }
cb347a4
 }
cb347a4
+
cb347a4
+static void
cb347a4
+cmdline_lists_init (void)
cb347a4
+{
cb347a4
+  cmdline_object_only_file_list.tail
cb347a4
+    = &cmdline_object_only_file_list.head;
cb347a4
+  cmdline_object_only_archive_list.tail
cb347a4
+    = &cmdline_object_only_archive_list.head;
cb347a4
+  cmdline_temp_object_only_list.tail
cb347a4
+    = &cmdline_temp_object_only_list.head;
cb347a4
+}
cb347a4
+
cb347a4
+/* Allocate an item with TYPE and DATA.  */
cb347a4
+
cb347a4
+static cmdline_union_type *
cb347a4
+cmdline_list_new (cmdline_enum_type type, void *data)
cb347a4
+{
cb347a4
+  cmdline_union_type *new_opt;
cb347a4
+
cb347a4
+  new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt));
cb347a4
+  new_opt->header.type = type;
cb347a4
+  switch (type)
cb347a4
+    {
cb347a4
+    default:
cb347a4
+      break;
cb347a4
+    case cmdline_is_file_enum:
cb347a4
+      new_opt->file.filename = (const char *) data;
cb347a4
+      break;
cb347a4
+    case cmdline_is_bfd_enum:
cb347a4
+      new_opt->abfd.abfd = (bfd *) data;
cb347a4
+      break;
cb347a4
+    }
cb347a4
+  return new_opt;
cb347a4
+}
cb347a4
+
cb347a4
+/* Append an item with TYPE and DATA to LIST.  */
cb347a4
+
cb347a4
+static void
cb347a4
+cmdline_list_append (cmdline_list_type *list, cmdline_enum_type type,
cb347a4
+		     void *data)
cb347a4
+{
cb347a4
+  cmdline_union_type *new_opt = cmdline_list_new (type, data);
cb347a4
+  new_opt->header.next = NULL;
cb347a4
+  *list->tail = new_opt;
cb347a4
+  list->tail = &new_opt->header.next;
cb347a4
+}
cb347a4
+
cb347a4
+static void
cb347a4
+print_cmdline_list (cmdline_union_type *c)
cb347a4
+{
cb347a4
+  for (; c != NULL; c = c->header.next)
cb347a4
+    switch (c->header.type)
cb347a4
+      {
cb347a4
+      default:
cb347a4
+	abort ();
cb347a4
+      case cmdline_is_file_enum:
cb347a4
+	info_msg (" %s", c->file.filename);
cb347a4
+	break;
cb347a4
+      case cmdline_is_bfd_enum:
cb347a4
+	info_msg (" [%B]", c->abfd.abfd);
cb347a4
+	break;
cb347a4
+      }
cb347a4
+
cb347a4
+  info_msg ("\n");
cb347a4
+}
cb347a4
+
cb347a4
+/* Return TRUE if ABFD is on cmdline_object_only_archive_list.  */
cb347a4
+
cb347a4
+static bfd_boolean
cb347a4
+cmdline_on_object_only_archive_list_p (bfd *abfd)
cb347a4
+{
cb347a4
+  cmdline_union_type *c, *next;
cb347a4
+  bfd *archive, *obfd, *oarchive;
cb347a4
+  ufile_ptr origin = abfd->origin;
cb347a4
+
cb347a4
+  archive = bfd_my_archive (abfd);
cb347a4
+  for (c = cmdline_object_only_archive_list.head; c != NULL; c = next)
cb347a4
+    {
cb347a4
+      if (c->header.type != cmdline_is_bfd_enum)
cb347a4
+	abort ();
cb347a4
+
cb347a4
+      next = c->header.next;
cb347a4
+      obfd = c->abfd.abfd;
cb347a4
+      oarchive = bfd_my_archive (obfd);
cb347a4
+
cb347a4
+      /* The list is grouped by archive file name and sorted by member
cb347a4
+	 origin.  */
cb347a4
+      if (strcmp (archive->filename, oarchive->filename) != 0)
cb347a4
+	continue;
cb347a4
+
cb347a4
+      if (origin == obfd->origin)
cb347a4
+	return TRUE;
cb347a4
+      else if (origin < obfd->origin)
cb347a4
+	return FALSE;
cb347a4
+    }
cb347a4
+
cb347a4
+  return FALSE;
cb347a4
+}
cb347a4
+
cb347a4
+/* Append an item with TYPE and DATA to cmdline_object_only_file_list
cb347a4
+   or cmdline_object_only_archive_list if needed.  */
cb347a4
+
cb347a4
+static void
cb347a4
+cmdline_object_only_list_append (cmdline_enum_type type, void *data)
cb347a4
+{
cb347a4
+  cmdline_union_type *c;
cb347a4
+  cmdline_union_type *new_opt, *next, **prev;
cb347a4
+  bfd *abfd, *archive;
cb347a4
+  bfd *obfd, *oarchive;
cb347a4
+  bfd *nbfd, *narchive;
cb347a4
+  ufile_ptr origin, norigin;
cb347a4
+
cb347a4
+  /* Put it on cmdline_object_only_file_list if it isn't an archive
cb347a4
+     member.  */
cb347a4
+  switch (type)
cb347a4
+    {
cb347a4
+    default:
cb347a4
+      abort ();
cb347a4
+    case cmdline_is_bfd_enum:
cb347a4
+      abfd = (bfd *) data;
cb347a4
+      archive = bfd_my_archive (abfd);
cb347a4
+      if (archive)
cb347a4
+	break;
cb347a4
+    case cmdline_is_file_enum:
cb347a4
+      cmdline_list_append (&cmdline_object_only_file_list, type, data);
cb347a4
+      return;
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Put archive member on cmdline_object_only_archive_list and sort
cb347a4
+     the list by archive name and archive member origin.  */
cb347a4
+  new_opt = (cmdline_union_type *) stat_alloc (sizeof (*new_opt));
cb347a4
+  new_opt->header.type = cmdline_is_bfd_enum;
cb347a4
+  new_opt->header.next = NULL;
cb347a4
+  new_opt->abfd.abfd = (bfd *) data;
cb347a4
+
cb347a4
+  c = cmdline_object_only_archive_list.head;
cb347a4
+  if (c == NULL)
cb347a4
+    {
cb347a4
+      cmdline_object_only_archive_list.head = new_opt;
cb347a4
+      cmdline_object_only_archive_list.tail = &new_opt->header.next;
cb347a4
+      return;
cb347a4
+    }
cb347a4
+
cb347a4
+  prev = NULL;
cb347a4
+  origin = abfd->origin;
cb347a4
+  for (; c != NULL; c = next)
cb347a4
+    {
cb347a4
+      if (c->header.type != cmdline_is_bfd_enum)
cb347a4
+	abort ();
cb347a4
+
cb347a4
+      next = c->header.next;
cb347a4
+
cb347a4
+      obfd = c->abfd.abfd;
cb347a4
+      oarchive = bfd_my_archive (obfd);
cb347a4
+
cb347a4
+      if (strcmp (archive->filename, oarchive->filename) == 0)
cb347a4
+	{
cb347a4
+	  bfd_boolean after;
cb347a4
+
cb347a4
+	  if (origin < obfd->origin)
cb347a4
+	    {
cb347a4
+	      /* Insert it before the current.  */
cb347a4
+	      new_opt->header.next = c;
cb347a4
+	      if (prev)
cb347a4
+		*prev = new_opt;
cb347a4
+	      else
cb347a4
+		cmdline_object_only_archive_list.head = new_opt;
cb347a4
+	      return;
cb347a4
+	    }
cb347a4
+
cb347a4
+	  after = TRUE;
cb347a4
+
cb347a4
+	  /* Check origin.  */
cb347a4
+	  while (next)
cb347a4
+	    {
cb347a4
+	      if (next->header.type != cmdline_is_bfd_enum)
cb347a4
+		abort ();
cb347a4
+
cb347a4
+	      nbfd = next->abfd.abfd;
cb347a4
+	      norigin = nbfd->origin;
cb347a4
+	      if (origin > norigin)
cb347a4
+		{
cb347a4
+		  /* Insert it after NEXT.  */
cb347a4
+		  break;
cb347a4
+		}
cb347a4
+
cb347a4
+	      narchive = bfd_my_archive (nbfd);
cb347a4
+	      if (strcmp (archive->filename, narchive->filename) != 0)
cb347a4
+		{
cb347a4
+		  /* Insert it befor NEXT.  */
cb347a4
+		  after = FALSE;
cb347a4
+		  break;
cb347a4
+		}
cb347a4
+
cb347a4
+	      c = next;
cb347a4
+	      next = next->header.next;
cb347a4
+	    }
cb347a4
+
cb347a4
+	  if (after && next)
cb347a4
+	    {
cb347a4
+	      c = next;
cb347a4
+	      next = next->header.next;
cb347a4
+	    }
cb347a4
+
cb347a4
+	  if (*cmdline_object_only_archive_list.tail == c->header.next)
cb347a4
+	    cmdline_object_only_archive_list.tail
cb347a4
+	      = &new_opt->header.next;
cb347a4
+
cb347a4
+	  prev = &c->header.next;
cb347a4
+	  new_opt->header.next = next;
cb347a4
+	  *prev = new_opt;
cb347a4
+	  return;
cb347a4
+	}
cb347a4
+
cb347a4
+      prev = &c->header.next;
cb347a4
+    }
cb347a4
+
cb347a4
+  *cmdline_object_only_archive_list.tail = new_opt;
cb347a4
+  cmdline_object_only_archive_list.tail = &new_opt->header.next;
cb347a4
+}
cb347a4
+
cb347a4
+
cb347a4
+static void
cb347a4
+cmdline_get_object_only_input_files (void)
cb347a4
+{
cb347a4
+  cmdline_union_type *c, *next;
cb347a4
+  bfd *abfd, *archive;
cb347a4
+  bfd *nbfd, *narchive;
cb347a4
+
cb347a4
+  /* Add files first.  */
cb347a4
+  for (c = cmdline_object_only_file_list.head;
cb347a4
+       c != NULL; c = c->header.next)
cb347a4
+    switch (c->header.type)
cb347a4
+      {
cb347a4
+      default:
cb347a4
+	abort ();
cb347a4
+      case cmdline_is_file_enum:
cb347a4
+	lang_add_input_file (c->file.filename,
cb347a4
+			     lang_input_file_is_file_enum, NULL);
cb347a4
+	break;
cb347a4
+      case cmdline_is_bfd_enum:
cb347a4
+	abfd = c->abfd.abfd;
cb347a4
+	if (bfd_my_archive (abfd))
cb347a4
+	  abort ();
cb347a4
+	lang_add_input_file (abfd->filename,
cb347a4
+			     lang_input_file_is_file_enum, NULL);
cb347a4
+	break;
cb347a4
+      }
cb347a4
+
cb347a4
+  /* Add archive members next.  */
cb347a4
+  for (c = cmdline_object_only_archive_list.head; c != NULL; c = next)
cb347a4
+    {
cb347a4
+      if (c->header.type != cmdline_is_bfd_enum)
cb347a4
+	abort ();
cb347a4
+
cb347a4
+      next = c->header.next;
cb347a4
+
cb347a4
+      abfd = c->abfd.abfd;
cb347a4
+      archive = bfd_my_archive (abfd);
d43b721
+   
cb347a4
+      /* Add the first archive of the archive member group.  */
cb347a4
+      lang_add_input_file (archive->filename,
cb347a4
+			   lang_input_file_is_file_enum, NULL);
cb347a4
+
cb347a4
+      /* Skip the rest members in the archive member group.  */
cb347a4
+      do
cb347a4
+	{
cb347a4
+	  if (!next)
cb347a4
+	    break;
cb347a4
+
cb347a4
+	  if (next->header.type != cmdline_is_bfd_enum)
cb347a4
+	    abort ();
cb347a4
+
cb347a4
+	  next = next->header.next;
cb347a4
+	  if (!next)
cb347a4
+	    break;
cb347a4
+	  nbfd = next->abfd.abfd;
cb347a4
+	  narchive = bfd_my_archive (nbfd);
cb347a4
+	}
cb347a4
+      while (strcmp (archive->filename, narchive->filename) == 0);
cb347a4
+    }
cb347a4
+}
cb347a4
+
cb347a4
+struct cmdline_arg
cb347a4
+{
cb347a4
+  bfd *obfd;
cb347a4
+  asymbol **isympp;
cb347a4
+  int status;
cb347a4
+};
cb347a4
+
cb347a4
+/* Create a section in OBFD with the same
cb347a4
+   name and attributes as ISECTION in IBFD.  */
cb347a4
+
cb347a4
+static void
cb347a4
+setup_section (bfd *ibfd, sec_ptr isection, void *p)
cb347a4
+{
cb347a4
+  struct cmdline_arg *arg = (struct cmdline_arg *) p;
cb347a4
+  bfd *obfd = arg->obfd;
cb347a4
+  asymbol **isympp = arg->isympp;
cb347a4
+  const char *name = isection->name;
cb347a4
+  sec_ptr osection;
cb347a4
+  const char *err;
cb347a4
+
cb347a4
+  /* Skip the object-only section.  */
cb347a4
+  if (ibfd->object_only_section == isection)
cb347a4
+    return;
cb347a4
+
cb347a4
+  /* If we have already failed earlier on, do not keep on generating
cb347a4
+     complaints now.  */
cb347a4
+  if (arg->status)
cb347a4
+    return;
cb347a4
+
cb347a4
+  osection = bfd_make_section_anyway_with_flags (obfd, name,
cb347a4
+						 isection->flags);
cb347a4
+
cb347a4
+  if (osection == NULL)
cb347a4
+    {
cb347a4
+      err = _("failed to create output section");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  osection->size = isection->size;
cb347a4
+  osection->vma = isection->vma;
cb347a4
+  osection->lma = isection->lma;
cb347a4
+  osection->alignment_power = isection->alignment_power;
cb347a4
+
cb347a4
+  /* Copy merge entity size.  */
cb347a4
+  osection->entsize = isection->entsize;
cb347a4
+
cb347a4
+  /* This used to be mangle_section; we do here to avoid using
cb347a4
+     bfd_get_section_by_name since some formats allow multiple
cb347a4
+     sections with the same name.  */
cb347a4
+  isection->output_section = osection;
cb347a4
+  isection->output_offset = 0;
cb347a4
+
cb347a4
+  if ((isection->flags & SEC_GROUP) != 0)
cb347a4
+    {
cb347a4
+      asymbol *gsym = bfd_group_signature (isection, isympp);
cb347a4
+
cb347a4
+      if (gsym != NULL)
cb347a4
+	{
cb347a4
+	  gsym->flags |= BSF_KEEP;
cb347a4
+	  if (ibfd->xvec->flavour == bfd_target_elf_flavour)
cb347a4
+	    elf_group_id (isection) = gsym;
cb347a4
+	}
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Allow the BFD backend to copy any private data it understands
cb347a4
+     from the input section to the output section.  */
cb347a4
+  if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
cb347a4
+    {
cb347a4
+      err = _("failed to copy private data");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  /* All went well.  */
cb347a4
+  return;
cb347a4
+
cb347a4
+loser:
cb347a4
+  arg->status = 1;
cb347a4
+  einfo (_("%P%F: setup_section: %s: %s\n"), err, name);
cb347a4
+}
cb347a4
+
cb347a4
+/* Copy the data of input section ISECTION of IBFD
cb347a4
+   to an output section with the same name in OBFD.
cb347a4
+   If stripping then don't copy any relocation info.  */
cb347a4
+
cb347a4
+static void
cb347a4
+copy_section (bfd *ibfd, sec_ptr isection, void *p)
cb347a4
+{
cb347a4
+  struct cmdline_arg *arg = (struct cmdline_arg *) p;
cb347a4
+  bfd *obfd = arg->obfd;
cb347a4
+  asymbol **isympp = arg->isympp;
cb347a4
+  arelent **relpp;
cb347a4
+  long relcount;
cb347a4
+  sec_ptr osection;
cb347a4
+  bfd_size_type size;
cb347a4
+  long relsize;
cb347a4
+  flagword flags;
cb347a4
+  const char *err;
cb347a4
+
cb347a4
+  /* Skip the object-only section.  */
cb347a4
+  if (ibfd->object_only_section == isection)
cb347a4
+    return;
cb347a4
+
cb347a4
+  /* If we have already failed earlier on, do not keep on generating
cb347a4
+     complaints now.  */
cb347a4
+  if (arg->status)
cb347a4
+    return;
cb347a4
+
cb347a4
+  flags = bfd_get_section_flags (ibfd, isection);
cb347a4
+  if ((flags & SEC_GROUP) != 0)
cb347a4
+    return;
cb347a4
+
cb347a4
+  osection = isection->output_section;
cb347a4
+  size = bfd_get_section_size (isection);
cb347a4
+
cb347a4
+  if (size == 0 || osection == 0)
cb347a4
+    return;
cb347a4
+
cb347a4
+  relsize = bfd_get_reloc_upper_bound (ibfd, isection);
cb347a4
+
cb347a4
+  if (relsize < 0)
cb347a4
+    {
cb347a4
+      /* Do not complain if the target does not support relocations.  */
cb347a4
+      if (relsize == -1
cb347a4
+	  && bfd_get_error () == bfd_error_invalid_operation)
cb347a4
+	relsize = 0;
cb347a4
+      else
cb347a4
+	{
cb347a4
+	  err = bfd_errmsg (bfd_get_error ());
cb347a4
+	  goto loser;
cb347a4
+	}
cb347a4
+    }
cb347a4
+
cb347a4
+  if (relsize == 0)
cb347a4
+    bfd_set_reloc (obfd, osection, NULL, 0);
cb347a4
+  else
cb347a4
+    {
cb347a4
+      relpp = (arelent **) xmalloc (relsize);
cb347a4
+      relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
cb347a4
+      if (relcount < 0)
cb347a4
+	{
cb347a4
+	  err = _("relocation count is negative");
cb347a4
+	  goto loser;
cb347a4
+	}
cb347a4
+
cb347a4
+      bfd_set_reloc (obfd, osection,
cb347a4
+		     relcount == 0 ? NULL : relpp, relcount);
cb347a4
+      if (relcount == 0)
cb347a4
+	free (relpp);
cb347a4
+    }
cb347a4
+
cb347a4
+  if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
cb347a4
+    {
cb347a4
+      bfd_byte *memhunk = NULL;
cb347a4
+
cb347a4
+      if (!bfd_get_full_section_contents (ibfd, isection, &memhunk))
cb347a4
+	{
cb347a4
+	  err = bfd_errmsg (bfd_get_error ());
cb347a4
+	  goto loser;
cb347a4
+	}
cb347a4
+
cb347a4
+      if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
cb347a4
+	{
cb347a4
+	  err = bfd_errmsg (bfd_get_error ());
cb347a4
+	  goto loser;
cb347a4
+	}
cb347a4
+      free (memhunk);
cb347a4
+    }
cb347a4
+
cb347a4
+  /* All went well.  */
cb347a4
+  return;
cb347a4
+
cb347a4
+loser:
cb347a4
+  einfo (_("%P%F: copy_section: %s: %s\n"), err, isection->name);
cb347a4
+}
cb347a4
+/* Open the temporary bfd created in the same directory as PATH.  */
cb347a4
+
cb347a4
+static bfd *
cb347a4
+cmdline_fopen_temp (const char *path, const char *target,
cb347a4
+		    const char *mode)
cb347a4
+{
cb347a4
+#define template "ldXXXXXX"
cb347a4
+  const char *slash = strrchr (path, '/');
cb347a4
+  char *tmpname;
cb347a4
+  size_t len;
cb347a4
+  int fd;
cb347a4
+
cb347a4
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
cb347a4
+  {
cb347a4
+    /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
cb347a4
+    char *bslash = strrchr (path, '\\');
cb347a4
+
cb347a4
+    if (slash == NULL || (bslash != NULL && bslash > slash))
cb347a4
+      slash = bslash;
cb347a4
+    if (slash == NULL && path[0] != '\0' && path[1] == ':')
cb347a4
+      slash = path + 1;
cb347a4
+  }
cb347a4
+#endif
cb347a4
+
cb347a4
+  if (slash != (char *) NULL)
cb347a4
+    {
cb347a4
+      len = slash - path;
cb347a4
+      tmpname = (char *) xmalloc (len + sizeof (template) + 2);
cb347a4
+      memcpy (tmpname, path, len);
cb347a4
+
cb347a4
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
cb347a4
+      /* If tmpname is "X:", appending a slash will make it a root
cb347a4
+	 directory on drive X, which is NOT the same as the current
cb347a4
+	 directory on drive X.  */
cb347a4
+      if (len == 2 && tmpname[1] == ':')
cb347a4
+	tmpname[len++] = '.';
cb347a4
+#endif
cb347a4
+      tmpname[len++] = '/';
cb347a4
+    }
cb347a4
+  else
cb347a4
+    {
cb347a4
+      tmpname = (char *) xmalloc (sizeof (template));
cb347a4
+      len = 0;
cb347a4
+    }
cb347a4
+
cb347a4
+  memcpy (tmpname + len, template, sizeof (template));
cb347a4
+#undef template
cb347a4
+
cb347a4
+#ifdef HAVE_MKSTEMP
cb347a4
+  fd = mkstemp (tmpname);
cb347a4
+#else
cb347a4
+  tmpname = mktemp (tmpname);
cb347a4
+  if (tmpname == NULL)
cb347a4
+    return NULL;
cb347a4
+  fd = open (tmpname, O_RDWR | O_CREAT | O_EXCL, 0600);
cb347a4
+#endif
cb347a4
+  if (fd == -1)
cb347a4
+    return NULL;
cb347a4
+  return bfd_fopen (tmpname, target, mode, fd);
cb347a4
+}
cb347a4
+
cb347a4
+/* Add the object-only section.  */
cb347a4
+
cb347a4
+static void
cb347a4
+cmdline_add_object_only_section (bfd_byte *contents, size_t size)
cb347a4
+{
cb347a4
+  bfd_vma start;
cb347a4
+  flagword flags;
cb347a4
+  enum bfd_architecture iarch;
cb347a4
+  unsigned int imach;
cb347a4
+  long symcount;
cb347a4
+  long symsize;
cb347a4
+  asymbol **isympp = NULL;
cb347a4
+  asymbol **osympp = NULL;
cb347a4
+  bfd *obfd = NULL, *ibfd;
cb347a4
+  const char *err;
cb347a4
+  struct arg
cb347a4
+    {
cb347a4
+      bfd *obfd;
cb347a4
+      asymbol **isympp;
cb347a4
+      int status;
cb347a4
+    } arg;
cb347a4
+  char **matching;
cb347a4
+  const char *ofilename = NULL;
cb347a4
+  asection *sec;
cb347a4
+
cb347a4
+  ibfd = bfd_openr (output_filename, output_target);
cb347a4
+  if (!ibfd)
cb347a4
+    {
cb347a4
+      err = bfd_errmsg (bfd_get_error ());
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  if (!bfd_check_format_matches (ibfd, bfd_object, &matching))
cb347a4
+    {
cb347a4
+      err = bfd_errmsg (bfd_get_error ());
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  obfd = cmdline_fopen_temp (output_filename, output_target, "w");
cb347a4
+  if (!obfd)
cb347a4
+    {
cb347a4
+      err = bfd_errmsg (bfd_get_error ());
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+  ofilename = bfd_get_filename (obfd);
cb347a4
+
cb347a4
+  if (!bfd_set_format (obfd, bfd_object))
cb347a4
+    {
cb347a4
+      err = bfd_errmsg (bfd_get_error ());
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Copy the start address, flags and architecture of input file to
cb347a4
+     output file.  */
cb347a4
+  flags = bfd_get_file_flags (ibfd);
cb347a4
+  start = bfd_get_start_address (ibfd);
cb347a4
+  iarch = bfd_get_arch (ibfd);
cb347a4
+  imach = bfd_get_mach (ibfd);
cb347a4
+  if (!bfd_set_start_address (obfd, start)
cb347a4
+      || !bfd_set_file_flags (obfd, flags)
cb347a4
+      || !bfd_set_arch_mach (obfd, iarch, imach))
cb347a4
+    {
cb347a4
+      err = bfd_errmsg (bfd_get_error ());
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+	
cb347a4
+  symsize = bfd_get_symtab_upper_bound (ibfd);
cb347a4
+  if (symsize < 0)
cb347a4
+    {
cb347a4
+      err = bfd_errmsg (bfd_get_error ());
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  isympp = (asymbol **) xmalloc (symsize);
cb347a4
+  symcount = bfd_canonicalize_symtab (ibfd, isympp);
cb347a4
+  if (symcount < 0)
cb347a4
+    {
cb347a4
+      err = bfd_errmsg (bfd_get_error ());
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  arg.obfd = obfd;
cb347a4
+  arg.isympp = isympp;
cb347a4
+  arg.status = 0;
cb347a4
+
cb347a4
+  /* BFD mandates that all output sections be created and sizes set before
cb347a4
+     any output is done.  Thus, we traverse all sections multiple times.  */
cb347a4
+  bfd_map_over_sections (ibfd, setup_section, &arg;;
cb347a4
+
cb347a4
+  if (arg.status)
cb347a4
+    {
cb347a4
+      err = _("error setting up sections");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Allow the BFD backend to copy any private data it understands
cb347a4
+     from the input section to the output section.  */
cb347a4
+  if (! bfd_copy_private_header_data (ibfd, obfd))
cb347a4
+    {
cb347a4
+      err = _("error copying private header data");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Create the object-only section.  */
cb347a4
+  sec = bfd_make_section_with_flags (obfd,
cb347a4
+				     GNU_OBJECT_ONLY_SECTION_NAME,
cb347a4
+				     (SEC_HAS_CONTENTS
cb347a4
+				      | SEC_READONLY
cb347a4
+				      | SEC_DATA
cb347a4
+				      | SEC_LINKER_CREATED));
cb347a4
+  if (sec == NULL)
cb347a4
+    {
cb347a4
+      err = _("can't create object-only section");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  if (! bfd_set_section_size (obfd, sec, size))
cb347a4
+    {
cb347a4
+      err = _("can't set object-only section size");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  if (ibfd->object_only_section)
cb347a4
+    {
cb347a4
+      /* Filter out the object-only section symbol.  */
cb347a4
+      long src_count = 0, dst_count = 0;
cb347a4
+      asymbol **from, **to;
cb347a4
+
cb347a4
+      osympp = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
cb347a4
+      from = isympp;
cb347a4
+      to = osympp;
cb347a4
+      for (; src_count < symcount; src_count++)
cb347a4
+	{
cb347a4
+	  asymbol *sym = from[src_count];
cb347a4
+	  if (bfd_get_section (sym) != ibfd->object_only_section)
cb347a4
+	    to[dst_count++] = sym;
cb347a4
+	}
cb347a4
+      to[dst_count] = NULL;
cb347a4
+      symcount = dst_count;
cb347a4
+      bfd_set_symtab (obfd, osympp, symcount);
cb347a4
+    }
cb347a4
+  else
cb347a4
+    bfd_set_symtab (obfd, isympp, symcount);
cb347a4
+
cb347a4
+  /* This has to happen after the symbol table has been set.  */
cb347a4
+  bfd_map_over_sections (ibfd, copy_section, &arg;;
cb347a4
+
cb347a4
+  if (arg.status)
cb347a4
+    {
cb347a4
+      err = _("error copying sections");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Copy the object-only section to the output.  */
cb347a4
+  if (! bfd_set_section_contents (obfd, sec, contents, 0, size))
cb347a4
+    {
cb347a4
+      err = _("error adding object-only section");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Allow the BFD backend to copy any private data it understands
cb347a4
+     from the input BFD to the output BFD.  This is done last to
cb347a4
+     permit the routine to look at the filtered symbol table, which is
cb347a4
+     important for the ECOFF code at least.  */
cb347a4
+  if (! bfd_copy_private_bfd_data (ibfd, obfd))
cb347a4
+    {
cb347a4
+      err = _("error copying private BFD data");
cb347a4
+      goto loser;
cb347a4
+    }
cb347a4
+
cb347a4
+  if (!bfd_close (obfd))
cb347a4
+    {
cb347a4
+      unlink (ofilename);
cb347a4
+      einfo (_("%P%F: failed to finish output with object-only section\n"));
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Must be freed after bfd_close ().  */
cb347a4
+  free (isympp);
cb347a4
+  if (osympp)
cb347a4
+    free (osympp);
cb347a4
+
cb347a4
+  if (rename (ofilename, output_filename))
cb347a4
+    {
cb347a4
+      unlink (ofilename);
cb347a4
+      einfo (_("%P%F: failed to rename output with object-only section\n"));
cb347a4
+    }
cb347a4
+
cb347a4
+  return;
cb347a4
+
cb347a4
+loser:
cb347a4
+  if (isympp)
cb347a4
+    free (isympp);
cb347a4
+  if (osympp)
cb347a4
+    free (osympp);
cb347a4
+  if (obfd)
cb347a4
+    bfd_close (obfd);
cb347a4
+  if (ofilename)
cb347a4
+    unlink (ofilename);
cb347a4
+  einfo (_("%P%F: failed to add object-only section: %s\n"), err);
cb347a4
+}
cb347a4
+
cb347a4
+/* Emit the final output with object-only section.  */
cb347a4
+
cb347a4
+void
cb347a4
+cmdline_emit_object_only_section (void)
cb347a4
+{
cb347a4
+  const char *saved_output_filename = output_filename;
cb347a4
+  int fd;
cb347a4
+  size_t size, off;
cb347a4
+  bfd_byte *contents;
cb347a4
+  struct stat st;
cb347a4
+
cb347a4
+  /* Get a temporary object-only file.  */
cb347a4
+  output_filename = make_temp_file (".obj-only.o");
cb347a4
+
cb347a4
+  had_output_filename = FALSE;
cb347a4
+  link_info.input_bfds = NULL;
cb347a4
+  link_info.input_bfds_tail = &link_info.input_bfds;
cb347a4
+
cb347a4
+  lang_init (TRUE);
cb347a4
+
cb347a4
+  ld_parse_linker_script ();
cb347a4
+
cb347a4
+  /* Set up the object-only output. */
cb347a4
+  lang_final ();
cb347a4
+
cb347a4
+  /* Open the object-only file for output.  */
cb347a4
+  lang_for_each_statement (ldlang_open_output);
cb347a4
+
cb347a4
+  ldemul_create_output_section_statements ();
cb347a4
+
cb347a4
+  if (!bfd_section_already_linked_table_init ())
cb347a4
+    einfo (_("%P%F: Failed to create hash table\n"));
cb347a4
+
cb347a4
+  /* Call cmdline_on_object_only_archive_list_p to check which member
cb347a4
+     should be loaded.  */
cb347a4
+  input_flags.whole_archive = TRUE;
cb347a4
+
cb347a4
+  /* Set it to avoid adding more to cmdline lists.  */
cb347a4
+  link_info.emitting_gnu_object_only = TRUE;
cb347a4
+
cb347a4
+  cmdline_get_object_only_input_files ();
cb347a4
+
cb347a4
+  open_input_bfds (statement_list.head, FALSE);
cb347a4
+
cb347a4
+  ldemul_after_open ();
cb347a4
+
cb347a4
+  bfd_section_already_linked_table_free ();
cb347a4
+
cb347a4
+  /* Make sure that we're not mixing architectures.  We call this
cb347a4
+     after all the input files have been opened, but before we do any
cb347a4
+     other processing, so that any operations merge_private_bfd_data
cb347a4
+     does on the output file will be known during the rest of the
cb347a4
+     link.  */
cb347a4
+  lang_check ();
cb347a4
+
cb347a4
+  /* Size up the common data.  */
cb347a4
+  lang_common ();
cb347a4
+
cb347a4
+  /* Update wild statements.  */
cb347a4
+  update_wild_statements (statement_list.head);
cb347a4
+
cb347a4
+  /* Run through the contours of the script and attach input sections
cb347a4
+     to the correct output sections.  */
cb347a4
+  map_input_to_output_sections (statement_list.head, NULL, NULL);
cb347a4
+
cb347a4
+  /* Find any sections not attached explicitly and handle them.  */
cb347a4
+  lang_place_orphans ();
cb347a4
+
cb347a4
+  /* Do anything special before sizing sections.  This is where ELF
cb347a4
+     and other back-ends size dynamic sections.  */
cb347a4
+  ldemul_before_allocation ();
cb347a4
+
cb347a4
+  /* Size up the sections.  */
cb347a4
+  lang_size_sections (NULL, ! RELAXATION_ENABLED);
cb347a4
+
cb347a4
+  /* See if anything special should be done now we know how big
cb347a4
+     everything is.  This is where relaxation is done.  */
cb347a4
+  ldemul_after_allocation ();
cb347a4
+
cb347a4
+  ldemul_finish ();
cb347a4
+
cb347a4
+  /* Make sure that the section addresses make sense.  */
cb347a4
+  if (command_line.check_section_addresses)
cb347a4
+    lang_check_section_addresses ();
cb347a4
+
cb347a4
+  lang_end ();
cb347a4
+  
cb347a4
+  ldwrite ();
cb347a4
+
cb347a4
+  lang_finish ();
cb347a4
+
cb347a4
+  if (! bfd_close (link_info.output_bfd))
cb347a4
+    einfo (_("%P%F:%s: final close failed on object-only output: %E\n"),
cb347a4
+	   output_filename);
cb347a4
+
cb347a4
+  /* Read in the object-only file.  */
cb347a4
+  fd = open (output_filename, O_RDONLY | O_BINARY);
cb347a4
+  if (fd < 0)
cb347a4
+    {
cb347a4
+      bfd_set_error (bfd_error_system_call);
cb347a4
+      einfo (_("%P%F:%s: cannot open object-only output: %E"),
cb347a4
+	     output_filename);
cb347a4
+    }
cb347a4
+
cb347a4
+  /* Get the object-only file size.  */
cb347a4
+  if (fstat (fd, &st) != 0)
cb347a4
+    {
cb347a4
+      bfd_set_error (bfd_error_system_call);
cb347a4
+      einfo (_("%P%F:%s: cannot stat object-only output: %E"),
cb347a4
+	     output_filename);
cb347a4
+    }
cb347a4
+
cb347a4
+  size = st.st_size;
cb347a4
+  off = 0;
cb347a4
+  contents = (bfd_byte *) xmalloc (size);
cb347a4
+  while (off != size)
cb347a4
+    {
cb347a4
+      ssize_t got;
cb347a4
+
cb347a4
+      got = read (fd, contents + off, size - off);
cb347a4
+      if (got < 0)
cb347a4
+	{
cb347a4
+	  bfd_set_error (bfd_error_system_call);
cb347a4
+	  einfo (_("%P%F:%s: read failed on object-only output: %E"),
cb347a4
+		 output_filename);
cb347a4
+	}
cb347a4
+
cb347a4
+      off += got;
cb347a4
+    }
cb347a4
+
cb347a4
+  close (fd);
cb347a4
+
cb347a4
+  /* Remove the temporary object-only file.  */ 
cb347a4
+  unlink (output_filename);
cb347a4
+
cb347a4
+  output_filename = saved_output_filename;
cb347a4
+
cb347a4
+  cmdline_add_object_only_section (contents, size);
cb347a4
+
cb347a4
+  free (contents);
cb347a4
+}
cb347a4
+
cb347a4
+/* Extract the object-only section.  */
cb347a4
+
cb347a4
+static const char *
cb347a4
+cmdline_extract_object_only_section (bfd *abfd)
cb347a4
+{
cb347a4
+  const char *name = bfd_extract_object_only_section (abfd);
cb347a4
+
cb347a4
+  if (name == NULL)
cb347a4
+    einfo (_("%P%F: cannot extract object-only section from %B: %E"),
cb347a4
+	   abfd);
cb347a4
+
cb347a4
+  /* It should be removed after it is done.  */
cb347a4
+  cmdline_list_append (&cmdline_temp_object_only_list,
cb347a4
+		       cmdline_is_file_enum, (void *) name);
cb347a4
+
cb347a4
+  return name;
cb347a4
+}
cb347a4
+
cb347a4
+/* Check and handle the object-only section.   */
cb347a4
+
cb347a4
+void
cb347a4
+cmdline_check_object_only_section (bfd *abfd, bfd_boolean lto)
cb347a4
+{
cb347a4
+  const char *filename;
cb347a4
+
cb347a4
+  if (link_info.emitting_gnu_object_only
cb347a4
+      || abfd->format != bfd_object)
cb347a4
+    return;
cb347a4
+
cb347a4
+  if (lto)
cb347a4
+    {
cb347a4
+      /* For LTO link, we only need to extract object-only section
cb347a4
+	 from the mixed object, add it to input, and put it on LTO
cb347a4
+	 claimed output.  */
cb347a4
+      switch (abfd->lto_type)
cb347a4
+	{
cb347a4
+	default:
cb347a4
+	  abort ();
cb347a4
+	case lto_mixed_object:
cb347a4
+	  filename = cmdline_extract_object_only_section (abfd);
cb347a4
+	  lang_add_input_file (filename,
cb347a4
+			       lang_input_file_is_file_enum, NULL);
cb347a4
+	  break;
cb347a4
+	case lto_non_ir_object:
cb347a4
+	case lto_ir_object:
cb347a4
+	  break;
cb347a4
+	}
cb347a4
+    }
cb347a4
+  else if (link_info.relocatable)
cb347a4
+    {
cb347a4
+      /* For non-LTO relocatable link, we need to append non-IR object
cb347a4
+	 file and the object file in object-only section to the object
cb347a4
+	 only list.  */
cb347a4
+      switch (abfd->lto_type)
cb347a4
+	{
cb347a4
+	default:
cb347a4
+	  abort ();
cb347a4
+	case lto_mixed_object:
cb347a4
+	  filename = cmdline_extract_object_only_section (abfd);
cb347a4
+	  cmdline_object_only_list_append (cmdline_is_file_enum,
cb347a4
+					  (void *) filename);
cb347a4
+	  break;
cb347a4
+	case lto_non_ir_object:
cb347a4
+	  cmdline_object_only_list_append (cmdline_is_bfd_enum, abfd);
cb347a4
+	  break;
cb347a4
+	case lto_ir_object:
cb347a4
+	  break;
cb347a4
+	}
cb347a4
+    }
cb347a4
+}
cb347a4
+
cb347a4
+/* Remove temporary object-only files.  */
cb347a4
+
cb347a4
+void
cb347a4
+cmdline_remove_object_only_files (void)
cb347a4
+{
cb347a4
+  cmdline_union_type *c;
cb347a4
+
cb347a4
+#ifdef ENABLE_PLUGINS
cb347a4
+  if (plugin_save_temps)
cb347a4
+    return;
cb347a4
+#endif
cb347a4
+
cb347a4
+  c = cmdline_temp_object_only_list.head;
cb347a4
+  for (; c != NULL; c = c->header.next)
cb347a4
+    switch (c->header.type)
cb347a4
+      {
cb347a4
+      default:
cb347a4
+	abort ();
cb347a4
+      case cmdline_is_file_enum:
cb347a4
+	unlink (c->file.filename);
cb347a4
+	break;
cb347a4
+      }
cb347a4
+}
cb347a4
+
cb347a4
+
cb347a4
+
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/ldlang.h ld/ldlang.h
cb347a4
--- a/ld/ldlang.h	2013-05-14 16:39:36.861718097 +0100
cb347a4
+++ b/ld/ldlang.h	2013-05-14 16:40:07.053718934 +0100
cb347a4
@@ -488,7 +488,7 @@
cb347a4
 extern int lang_statement_iteration;
cb347a4
 
cb347a4
 extern void lang_init
cb347a4
-  (void);
cb347a4
+  (bfd_boolean);
cb347a4
 extern void lang_finish
cb347a4
   (void);
cb347a4
 extern lang_memory_region_type * lang_memory_region_lookup
cb347a4
@@ -664,4 +664,45 @@
cb347a4
 extern void
cb347a4
 lang_ld_feature (char *);
cb347a4
 
cb347a4
+typedef enum
cb347a4
+{
cb347a4
+  cmdline_is_file_enum,
cb347a4
+  cmdline_is_bfd_enum
cb347a4
+} cmdline_enum_type;
cb347a4
+
cb347a4
+typedef struct cmdline_header_struct
cb347a4
+{
cb347a4
+  union cmdline_union *next;
cb347a4
+  cmdline_enum_type type;
cb347a4
+} cmdline_header_type;
cb347a4
+
cb347a4
+typedef struct cmdline_file_struct
cb347a4
+{
cb347a4
+  cmdline_header_type header;
cb347a4
+  const char *filename;
cb347a4
+} cmdline_file_type;
cb347a4
+
cb347a4
+typedef struct cmdline_bfd_struct
cb347a4
+{
cb347a4
+  cmdline_header_type header;
cb347a4
+  bfd *abfd;
cb347a4
+} cmdline_bfd_type;
cb347a4
+
cb347a4
+typedef union cmdline_union
cb347a4
+{
cb347a4
+  cmdline_header_type header;
cb347a4
+  cmdline_file_type file;
cb347a4
+  cmdline_bfd_type abfd;
cb347a4
+} cmdline_union_type;
cb347a4
+
cb347a4
+typedef struct cmdline_list
cb347a4
+{
cb347a4
+  cmdline_union_type *head;
cb347a4
+  cmdline_union_type **tail;
cb347a4
+} cmdline_list_type;
cb347a4
+
cb347a4
+extern void cmdline_emit_object_only_section (void);
cb347a4
+extern void cmdline_check_object_only_section (bfd *, bfd_boolean);
cb347a4
+extern void cmdline_remove_object_only_files (void);
cb347a4
+
cb347a4
 #endif
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/ldlex.h ld/ldlex.h
cb347a4
--- a/ld/ldlex.h	2013-05-14 16:39:39.789718178 +0100
cb347a4
+++ b/ld/ldlex.h	2013-05-14 16:47:18.651730898 +0100
cb347a4
@@ -133,6 +133,7 @@
cb347a4
 #ifdef ENABLE_PLUGINS
cb347a4
   OPTION_PLUGIN,
cb347a4
   OPTION_PLUGIN_OPT,
cb347a4
+  OPTION_PLUGIN_SAVE_TEMPS,
cb347a4
 #endif /* ENABLE_PLUGINS */
cb347a4
   OPTION_DEFAULT_SCRIPT,
cb347a4
   OPTION_PRINT_OUTPUT_FORMAT,
d43b721
diff -cpr ../binutils-2.23.2.orig/ld/ldmain.c ld/ldmain.c
cb347a4
--- a/ld/ldmain.c	2013-05-14 16:39:36.862718097 +0100
cb347a4
+++ b/ld/ldmain.c	2013-05-14 16:40:07.055718934 +0100
cb347a4
@@ -219,6 +219,9 @@
cb347a4
 
cb347a4
   xatexit (ld_cleanup);
cb347a4
 
cb347a4
+  /* Remove temporary object-only files.  */
cb347a4
+  xatexit (cmdline_remove_object_only_files);
cb347a4
+
cb347a4
   /* Set up the sysroot directory.  */
cb347a4
   ld_sysroot = get_sysroot (argc, argv);
cb347a4
   if (*ld_sysroot)
cb347a4
@@ -295,7 +298,7 @@
cb347a4
   default_target = ldemul_choose_target (argc, argv);
cb347a4
   config.maxpagesize = bfd_emul_get_maxpagesize (default_target);
cb347a4
   config.commonpagesize = bfd_emul_get_commonpagesize (default_target);
cb347a4
-  lang_init ();
cb347a4
+  lang_init (FALSE);
cb347a4
   ldemul_before_parse ();
cb347a4
   lang_has_input_file = FALSE;
cb347a4
   parse_args (argc, argv);
cb347a4
@@ -310,34 +313,7 @@
cb347a4
 
cb347a4
   ldemul_set_symbols ();
cb347a4
 
cb347a4
-  /* If we have not already opened and parsed a linker script,
cb347a4
-     try the default script from command line first.  */
cb347a4
-  if (saved_script_handle == NULL
cb347a4
-      && command_line.default_script != NULL)
cb347a4
-    {
cb347a4
-      ldfile_open_command_file (command_line.default_script);
cb347a4
-      parser_input = input_script;
cb347a4
-      yyparse ();
cb347a4
-    }
cb347a4
-
cb347a4
-  /* If we have not already opened and parsed a linker script
cb347a4
-     read the emulation's appropriate default script.  */
cb347a4
-  if (saved_script_handle == NULL)
cb347a4
-    {
cb347a4
-      int isfile;
cb347a4
-      char *s = ldemul_get_script (&isfile);
cb347a4
-
cb347a4
-      if (isfile)
cb347a4
-	ldfile_open_default_command_file (s);
cb347a4
-      else
cb347a4
-	{
cb347a4
-	  lex_string = s;
cb347a4
-	  lex_redirect (s, _("built in linker script"), 1);
cb347a4
-	}
cb347a4
-      parser_input = input_script;
cb347a4
-      yyparse ();
cb347a4
-      lex_string = NULL;
cb347a4
-    }
cb347a4
+  ld_parse_linker_script ();
cb347a4
 
cb347a4
   if (verbose)
cb347a4
     {