binutils/
[deliverable/binutils-gdb.git] / binutils / objcopy.c
index 212b9bf482ac13551432c83b741a6b0772835b79..a3bd95e21003d05c867779cb69a1e08a7ec29cf3 100644 (file)
@@ -1,6 +1,6 @@
 /* objcopy.c -- copy object file from input to output, optionally massaging it.
    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004
+   2001, 2002, 2003, 2004, 2005
    Free Software Foundation, Inc.
 
    This file is part of GNU Binutils.
@@ -28,6 +28,7 @@
 #include "budbg.h"
 #include "filenames.h"
 #include "fnmatch.h"
+#include "elf-bfd.h"
 #include <sys/stat.h>
 
 /* A list of symbols to explicitly strip out, or to keep.  A linked
@@ -191,8 +192,10 @@ static bfd_boolean wildcard = FALSE;
 /* List of symbols to strip, keep, localize, keep-global, weaken,
    or redefine.  */
 static struct symlist *strip_specific_list = NULL;
+static struct symlist *strip_unneeded_list = NULL;
 static struct symlist *keep_specific_list = NULL;
 static struct symlist *localize_specific_list = NULL;
+static struct symlist *globalize_specific_list = NULL;
 static struct symlist *keepglobal_specific_list = NULL;
 static struct symlist *weaken_specific_list = NULL;
 static struct redefine_node *redefine_sym_list = NULL;
@@ -230,8 +233,12 @@ enum command_line_switch
     OPTION_SREC_LEN,
     OPTION_SREC_FORCES3,
     OPTION_STRIP_SYMBOLS,
+    OPTION_STRIP_UNNEEDED_SYMBOL,
+    OPTION_STRIP_UNNEEDED_SYMBOLS,
     OPTION_KEEP_SYMBOLS,
     OPTION_LOCALIZE_SYMBOLS,
+    OPTION_GLOBALIZE_SYMBOL,
+    OPTION_GLOBALIZE_SYMBOLS,
     OPTION_KEEPGLOBAL_SYMBOLS,
     OPTION_WEAKEN_SYMBOLS,
     OPTION_RENAME_SECTION,
@@ -302,6 +309,8 @@ static struct option copy_options[] =
   {"discard-locals", no_argument, 0, 'X'},
   {"format", required_argument, 0, 'F'}, /* Obsolete */
   {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
+  {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
+  {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
   {"help", no_argument, 0, 'h'},
   {"impure", no_argument, 0, OPTION_IMPURE},
   {"info", no_argument, 0, OPTION_FORMATS_INFO},
@@ -339,6 +348,8 @@ static struct option copy_options[] =
   {"strip-all", no_argument, 0, 'S'},
   {"strip-debug", no_argument, 0, 'g'},
   {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
+  {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
+  {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
   {"strip-symbol", required_argument, 0, 'N'},
   {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
   {"target", required_argument, 0, 'F'},
@@ -376,6 +387,7 @@ extern unsigned long          bfd_external_machine;
 
 /* Forward declarations.  */
 static void setup_section (bfd *, asection *, void *);
+static void setup_bfd_headers (bfd *, bfd *);
 static void copy_section (bfd *, asection *, void *);
 static void get_sections (bfd *, asection *, void *);
 static int compare_section_lma (const void *, const void *);
@@ -403,13 +415,17 @@ copy_usage (FILE *stream, int exit_status)
   -g --strip-debug                 Remove all debugging symbols & sections\n\
      --strip-unneeded              Remove all symbols not needed by relocations\n\
   -N --strip-symbol <name>         Do not copy symbol <name>\n\
+     --strip-unneeded-symbol <name>\n\
+                                   Do not copy symbol <name> unless needed by\n\
+                                     relocations\n\
      --only-keep-debug             Strip everything but the debug information\n\
-  -K --keep-symbol <name>          Only copy symbol <name>\n\
+  -K --keep-symbol <name>          Do not strip symbol <name>\n\
   -L --localize-symbol <name>      Force symbol <name> to be marked as a local\n\
+     --globalize-symbol <name>     Force symbol <name> to be marked as a global\n\
   -G --keep-global-symbol <name>   Localize all symbols except <name>\n\
   -W --weaken-symbol <name>        Force symbol <name> to be marked as a weak\n\
      --weaken                      Force all global symbols to be marked as weak\n\
-  -w --wildcard                    Permit wildcard in symbol comparasion\n\
+  -w --wildcard                    Permit wildcard in symbol comparison\n\
   -x --discard-all                 Remove all non-global symbols\n\
   -X --discard-locals              Remove any compiler-generated symbols\n\
   -i --interleave <number>         Only copy one out of every <number> bytes\n\
@@ -441,8 +457,12 @@ copy_usage (FILE *stream, int exit_status)
      --srec-len <number>           Restrict the length of generated Srecords\n\
      --srec-forceS3                Restrict the type of generated Srecords to S3\n\
      --strip-symbols <file>        -N for all symbols listed in <file>\n\
+     --strip-unneeded-symbols <file>\n\
+                                   --strip-unneeded-symbol for all symbols listed\n\
+                                     in <file>\n\
      --keep-symbols <file>         -K for all symbols listed in <file>\n\
      --localize-symbols <file>     -L for all symbols listed in <file>\n\
+     --globalize-symbols <file>    --globalize-symbol for all in <file>\n\
      --keep-global-symbols <file>  -G for all symbols listed in <file>\n\
      --weaken-symbols <file>       -W for all symbols listed in <file>\n\
      --alt-machine-code <index>    Use alternate machine code for output\n\
@@ -483,8 +503,8 @@ strip_usage (FILE *stream, int exit_status)
      --strip-unneeded              Remove all symbols not needed by relocations\n\
      --only-keep-debug             Strip everything but the debug information\n\
   -N --strip-symbol=<name>         Do not copy symbol <name>\n\
-  -K --keep-symbol=<name>          Only copy symbol <name>\n\
-  -w --wildcard                    Permit wildcard in symbol comparasion\n\
+  -K --keep-symbol=<name>          Do not strip symbol <name>\n\
+  -w --wildcard                    Permit wildcard in symbol comparison\n\
   -x --discard-all                 Remove all non-global symbols\n\
   -X --discard-locals              Remove any compiler-generated symbols\n\
   -v --verbose                     List all object files modified\n\
@@ -534,7 +554,7 @@ parse_flags (const char *s)
       PARSE_FLAG ("code", SEC_CODE);
       PARSE_FLAG ("data", SEC_DATA);
       PARSE_FLAG ("rom", SEC_ROM);
-      PARSE_FLAG ("share", SEC_SHARED);
+      PARSE_FLAG ("share", SEC_COFF_SHARED);
       PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
 #undef PARSE_FLAG
       else
@@ -698,8 +718,8 @@ add_specific_symbols (const char *filename, struct symlist **list)
            ;
 
          if (! IS_LINE_TERMINATOR (* extra))
-           non_fatal (_("Ignoring rubbish found on line %d of %s"),
-                      line_count, filename);
+           non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
+                      filename, line_count);
        }
 
       * name_end = '\0';
@@ -776,7 +796,7 @@ is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
        return FALSE;
     }
 
-  return strip_symbols == STRIP_NONDEBUG ? TRUE : FALSE;
+  return FALSE;
 }
 
 /* Choose which symbol entries to copy; put the result in OSYMS.
@@ -889,7 +909,7 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
        keep = (strip_symbols != STRIP_DEBUG
                && strip_symbols != STRIP_UNNEEDED
                && ! convert_debugging);
-      else if (bfd_get_section (sym)->comdat)
+      else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
        /* COMDAT sections store special information in local
           symbols, so we cannot risk stripping any of them.  */
        keep = 1;
@@ -901,28 +921,44 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
 
       if (keep && is_specified_symbol (name, strip_specific_list))
        keep = 0;
+      if (keep
+         && !(flags & BSF_KEEP)
+         && is_specified_symbol (name, strip_unneeded_list))
+       keep = 0;
       if (!keep && is_specified_symbol (name, keep_specific_list))
        keep = 1;
       if (keep && is_strip_section (abfd, bfd_get_section (sym)))
        keep = 0;
 
-      if (keep && (flags & BSF_GLOBAL) != 0
-         && (weaken || is_specified_symbol (name, weaken_specific_list)))
-       {
-         sym->flags &=~ BSF_GLOBAL;
-         sym->flags |= BSF_WEAK;
-       }
-      if (keep && !undefined && (flags & (BSF_GLOBAL | BSF_WEAK))
-         && (is_specified_symbol (name, localize_specific_list)
-             || (keepglobal_specific_list != NULL
-                 && ! is_specified_symbol (name, keepglobal_specific_list))))
+      if (keep)
        {
-         sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
-         sym->flags |= BSF_LOCAL;
-       }
+         if ((flags & BSF_GLOBAL) != 0
+             && (weaken || is_specified_symbol (name, weaken_specific_list)))
+           {
+             sym->flags &= ~ BSF_GLOBAL;
+             sym->flags |= BSF_WEAK;
+           }
 
-      if (keep)
-       to[dst_count++] = sym;
+         if (!undefined
+             && (flags & (BSF_GLOBAL | BSF_WEAK))
+             && (is_specified_symbol (name, localize_specific_list)
+                 || (keepglobal_specific_list != NULL
+                     && ! is_specified_symbol (name, keepglobal_specific_list))))
+           {
+             sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
+             sym->flags |= BSF_LOCAL;
+           }
+
+         if (!undefined
+             && (flags & BSF_LOCAL) 
+             && is_specified_symbol (name, globalize_specific_list))
+           {
+             sym->flags &= ~ BSF_LOCAL;
+             sym->flags |= BSF_GLOBAL;
+           }
+
+         to[dst_count++] = sym;
+       }
     }
 
   to[dst_count] = NULL;
@@ -1066,10 +1102,10 @@ add_redefine_syms_file (const char *filename)
          continue;
        }
       else
-       fatal (_("%s: garbage at end of line %d"), filename, lineno);
+       fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
  comment:
       if (len != 0 && (outsym_off == 0 || outsym_off == len))
-       fatal (_("%s: missing new symbol name at line %d"), filename, lineno);
+       fatal (_("%s:%d: missing new symbol name"), filename, lineno);
       buf[len++] = '\0';
 
       /* Eat the rest of the line and finish it.  */
@@ -1079,13 +1115,13 @@ add_redefine_syms_file (const char *filename)
     }
 
   if (len != 0)
-    fatal (_("%s: premature end of file at line %d"), filename, lineno);
+    fatal (_("%s:%d: premature end of file"), filename, lineno);
 
   free (buf);
 }
 
 /* Copy object file IBFD onto OBFD.
-   Trues TRUE upon success, FALSE otherwise.  */
+   Returns TRUE upon success, FALSE otherwise.  */
 
 static bfd_boolean
 copy_object (bfd *ibfd, bfd *obfd)
@@ -1177,6 +1213,8 @@ copy_object (bfd *ibfd, bfd *obfd)
      any output is done.  Thus, we traverse all sections multiple times.  */
   bfd_map_over_sections (ibfd, setup_section, obfd);
 
+  setup_bfd_headers (ibfd, obfd);
+
   if (add_sections != NULL)
     {
       struct section_add *padd;
@@ -1370,6 +1408,7 @@ copy_object (bfd *ibfd, bfd *obfd)
       || strip_specific_list != NULL
       || keep_specific_list != NULL
       || localize_specific_list != NULL
+      || globalize_specific_list != NULL
       || keepglobal_specific_list != NULL
       || weaken_specific_list != NULL
       || prefix_symbols_string
@@ -1588,6 +1627,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target)
       l = xmalloc (sizeof (struct name_list));
       l->name = output_name;
       l->next = list;
+      l->obfd = NULL;
       list = l;
 
       if (output_bfd == NULL)
@@ -1662,6 +1702,7 @@ copy_file (const char *input_filename, const char *output_filename,
 
   if (get_file_size (input_filename) < 1)
     {
+      non_fatal (_("error: the input file '%s' is empty"), input_filename);
       status = 1;
       return;
     }
@@ -1690,7 +1731,6 @@ copy_file (const char *input_filename, const char *output_filename,
   else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
     {
       bfd *obfd;
-      bfd_boolean delete;
     do_copy:
 
       /* bfd_get_target does not return the correct value until
@@ -1702,7 +1742,8 @@ copy_file (const char *input_filename, const char *output_filename,
       if (obfd == NULL)
        RETURN_NONFATAL (output_filename);
 
-      delete = ! copy_object (ibfd, obfd);
+      if (! copy_object (ibfd, obfd))
+       status = 1;
 
       if (!bfd_close (obfd))
        RETURN_NONFATAL (output_filename);
@@ -1710,11 +1751,6 @@ copy_file (const char *input_filename, const char *output_filename,
       if (!bfd_close (ibfd))
        RETURN_NONFATAL (input_filename);
 
-      if (delete)
-       {
-         unlink (output_filename);
-         status = 1;
-       }
     }
   else
     {
@@ -1807,6 +1843,32 @@ find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
   return old_name;
 }
 
+/* Once each of the sections is copied, we may still need to do some
+   finalization work for private section headers.  Do that here.  */
+
+static void
+setup_bfd_headers (bfd *ibfd, bfd *obfd)
+{
+  const char *err;
+
+  /* Allow the BFD backend to copy any private data it understands
+     from the input section to the output section.  */
+  if (! bfd_copy_private_header_data (ibfd, obfd))
+    {
+      err = _("private header data");
+      goto loser;
+    }
+
+  /* All went well.  */
+  return;
+
+loser:
+  non_fatal (_("%s: error in %s: %s"),
+            bfd_get_filename (ibfd),
+            err, bfd_errmsg (bfd_get_error ()));
+  status = 1;
+}
+
 /* Create a section in OBFD with the same
    name and attributes as ISECTION in IBFD.  */
 
@@ -1909,6 +1971,13 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
 
   if (p != NULL && p->set_flags)
     flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
+  else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
+    {
+      flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
+      if (obfd->xvec->flavour == bfd_target_elf_flavour)
+       elf_section_type (osection) = SHT_NOBITS;
+    }
+
   if (!bfd_set_section_flags (obfd, osection, flags))
     {
       err = _("flags");
@@ -1977,7 +2046,7 @@ copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
     return;
 
   osection = isection->output_section;
-  size = bfd_get_section_size_before_reloc (isection);
+  size = bfd_get_section_size (isection);
 
   if (size == 0 || osection == 0)
     return;
@@ -2029,11 +2098,10 @@ copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
        }
 
       bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
+      if (relcount == 0)
+       free (relpp);
     }
 
-  isection->_cooked_size = isection->_raw_size;
-  isection->reloc_done = TRUE;
-
   if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
       && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
     {
@@ -2045,9 +2113,9 @@ copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
       if (copy_byte >= 0)
        {
          /* Keep only every `copy_byte'th byte in MEMHUNK.  */
-         char *from = memhunk + copy_byte;
+         char *from = (char *) memhunk + copy_byte;
          char *to = memhunk;
-         char *end = memhunk + size;
+         char *end = (char *) memhunk + size;
 
          for (; from < end; from += interleave)
            *to++ = *from;
@@ -2125,9 +2193,9 @@ compare_section_lma (const void *arg1, const void *arg2)
     return -1;
 
   /* Sort sections with the same LMA by size.  */
-  if ((*sec1)->_raw_size > (*sec2)->_raw_size)
+  if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
     return 1;
-  else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
+  else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
     return -1;
 
   return 0;
@@ -2387,7 +2455,7 @@ strip_main (int argc, char *argv[])
          status = hold_status;
        }
       else
-       unlink (tmpname);
+       unlink_if_ordinary (tmpname);
       if (output_file == NULL)
        free (tmpname);
     }
@@ -2489,10 +2557,18 @@ copy_main (int argc, char *argv[])
          add_specific_symbol (optarg, &strip_specific_list);
          break;
 
+       case OPTION_STRIP_UNNEEDED_SYMBOL:
+         add_specific_symbol (optarg, &strip_unneeded_list);
+         break;
+
        case 'L':
          add_specific_symbol (optarg, &localize_specific_list);
          break;
 
+       case OPTION_GLOBALIZE_SYMBOL:
+         add_specific_symbol (optarg, &globalize_specific_list);
+         break;
+
        case 'G':
          add_specific_symbol (optarg, &keepglobal_specific_list);
          break;
@@ -2822,6 +2898,10 @@ copy_main (int argc, char *argv[])
          add_specific_symbols (optarg, &strip_specific_list);
          break;
 
+       case OPTION_STRIP_UNNEEDED_SYMBOLS:
+         add_specific_symbols (optarg, &strip_unneeded_list);
+         break;
+
        case OPTION_KEEP_SYMBOLS:
          add_specific_symbols (optarg, &keep_specific_list);
          break;
@@ -2830,6 +2910,10 @@ copy_main (int argc, char *argv[])
          add_specific_symbols (optarg, &localize_specific_list);
          break;
 
+       case OPTION_GLOBALIZE_SYMBOLS:
+         add_specific_symbols (optarg, &globalize_specific_list);
+         break;
+
        case OPTION_KEEPGLOBAL_SYMBOLS:
          add_specific_symbols (optarg, &keepglobal_specific_list);
          break;
@@ -2965,6 +3049,8 @@ copy_main (int argc, char *argv[])
 
       if (status == 0 && preserve_dates)
        set_times (output_filename, &statbuf);
+      else if (status != 0)
+       unlink_if_ordinary (output_filename);
     }
 
   if (change_warn)
This page took 0.031415 seconds and 4 git commands to generate.