Add a flag to asection, linker_has_input, and use it to reliably
[deliverable/binutils-gdb.git] / binutils / objcopy.c
index e8b710fa3bb82b4ce6f4e6f57f49026ae9cfb4d4..de053b11287f3bd8231adadd77a40c30a478d07d 100644 (file)
@@ -1,5 +1,6 @@
 /* objcopy.c -- copy object file from input to output, optionally massaging it.
-   Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
+   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+   2001
    Free Software Foundation, Inc.
 
    This file is part of GNU Binutils.
@@ -25,6 +26,7 @@
 #include "getopt.h"
 #include "libiberty.h"
 #include "budbg.h"
+#include "filenames.h"
 #include <sys/stat.h>
 
 /* A list of symbols to explicitly strip out, or to keep.  A linked
@@ -214,6 +216,8 @@ static boolean weaken = false;
 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
+#define OPTION_SREC_LEN (OPTION_REDEFINE_SYM + 1)
+#define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
 
 /* Options to handle if running as "strip".  */
 
@@ -249,6 +253,7 @@ static struct option copy_options[] =
   {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
   {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
   {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
+  {"binary-architecture", required_argument, 0, 'B'},
   {"byte", required_argument, 0, 'b'},
   {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
   {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
@@ -289,6 +294,8 @@ static struct option copy_options[] =
   {"weaken", no_argument, 0, OPTION_WEAKEN},
   {"weaken-symbol", required_argument, 0, 'W'},
   {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
+  {"srec-len", required_argument, 0, OPTION_SREC_LEN},
+  {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
   {0, no_argument, 0, 0}
 };
 
@@ -300,6 +307,17 @@ extern char *program_name;
    -1 means if we should use argv[0] to decide. */
 extern int is_strip;
 
+/* The maximum length of an S record.  This variable is declared in srec.c
+   and can be modified by the --srec-len parameter.  */
+extern unsigned int Chunk;
+
+/* Restrict the generation of Srecords to type S3 only.
+   This variable is declare in bfd/srec.c and can be toggled
+   on by the --srec-forceS3 command line switch.  */
+extern boolean S3Forced;
+
+/* Defined in bfd/binary.c.  Used to set architecture of input binary files.  */
+extern enum bfd_architecture bfd_external_binary_architecture;
 
 static void
 copy_usage (stream, exit_status)
@@ -311,6 +329,7 @@ copy_usage (stream, exit_status)
   fprintf (stream, _("\
   -I --input-target <bfdname>      Assume input file is in format <bfdname>\n\
   -O --output-target <bfdname>     Create an output file in format <bfdname>\n\
+  -B --binary-architecture <arch>  Set arch of output file, when input is binary\n\
   -F --target <bfdname>            Set both input and output format to <bfdname>\n\
      --debugging                   Convert debugging information, if possible\n\
   -p --preserve-dates              Copy modified/access timestamps to the output\n\
@@ -349,6 +368,8 @@ copy_usage (stream, exit_status)
      --change-leading-char         Force output format's leading character style\n\
      --remove-leading-char         Remove leading character from global symbols\n\
      --redefine-sym <old>=<new>    Redefine symbol name <old> to <new>\n\
+     --srec-len <number>           Restrict the length of generated Srecords\n\
+     --srec-forceS3                Restrict the type of generated Srecords to S3\n\
   -v --verbose                     List all object files modified\n\
   -V --version                     Display this program's version number\n\
   -h --help                        Display this output\n\
@@ -556,6 +577,8 @@ filter_symbols (abfd, obfd, osyms, isyms, symcount)
 {
   register asymbol **from = isyms, **to = osyms;
   long src_count = 0, dst_count = 0;
+  int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
+                   == HAS_RELOC;
 
   for (; src_count < symcount; src_count++)
     {
@@ -610,6 +633,9 @@ filter_symbols (abfd, obfd, osyms, isyms, symcount)
                   && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
                       & BSF_KEEP) != 0))
        keep = 1;
+      else if (relocatable                     /* Relocatable file. */
+              && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
+       keep = 1;
       else if ((flags & BSF_GLOBAL) != 0       /* Global symbol.  */
               || (flags & BSF_WEAK) != 0
               || bfd_is_und_section (bfd_get_section (sym))
@@ -745,6 +771,13 @@ copy_object (ibfd, obfd)
   long symsize;
   PTR dhandle;
 
+  if (ibfd->xvec->byteorder != obfd->xvec->byteorder
+      && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
+      && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
+    {
+      fatal (_("Unable to change endianness of input file(s)"));
+      return;
+    }
 
   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
     RETURN_NONFATAL (bfd_get_filename (obfd));
@@ -1245,7 +1278,7 @@ setup_section (ibfd, isection, obfdarg)
   bfd_vma vma;
   bfd_vma lma;
   flagword flags;
-  char *err;
+  const char *err;
 
   if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
       && (strip_symbols == STRIP_DEBUG
@@ -1268,7 +1301,7 @@ setup_section (ibfd, isection, obfdarg)
 
   if (osection == NULL)
     {
-      err = "making";
+      err = _("making");
       goto loser;
     }
 
@@ -1277,7 +1310,7 @@ setup_section (ibfd, isection, obfdarg)
     size = (size + interleave - 1) / interleave;
   if (! bfd_set_section_size (obfd, osection, size))
     {
-      err = "size";
+      err = _("size");
       goto loser;
     }
 
@@ -1291,7 +1324,7 @@ setup_section (ibfd, isection, obfdarg)
 
   if (! bfd_set_section_vma (obfd, osection, vma))
     {
-      err = "vma";
+      err = _("vma");
       goto loser;
     }
 
@@ -1317,7 +1350,7 @@ setup_section (ibfd, isection, obfdarg)
                                 bfd_section_alignment (ibfd, isection))
       == false)
     {
-      err = "alignment";
+      err = _("alignment");
       goto loser;
     }
 
@@ -1326,7 +1359,7 @@ setup_section (ibfd, isection, obfdarg)
     flags = p->flags | (flags & SEC_HAS_CONTENTS);
   if (!bfd_set_section_flags (obfd, osection, flags))
     {
-      err = "flags";
+      err = _("flags");
       goto loser;
     }
 
@@ -1340,7 +1373,7 @@ setup_section (ibfd, isection, obfdarg)
      from the input section to the output section.  */
   if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
     {
-      err = "private data";
+      err = _("private data");
       goto loser;
     }
 
@@ -1662,7 +1695,7 @@ strip_main (argc, argv)
   struct section_list *p;
   char *output_file = NULL;
 
-  while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv",
+  while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXVv",
                           strip_options, (int *) 0)) != EOF)
     {
       switch (c)
@@ -1686,6 +1719,7 @@ strip_main (argc, argv)
          break;
        case 'S':
        case 'g':
+       case 'd':       /* Historic BSD alias for -g.  Used by early NetBSD.  */
          strip_symbols = STRIP_DEBUG;
          break;
        case OPTION_STRIP_UNNEEDED:
@@ -1785,6 +1819,7 @@ copy_main (argc, argv)
      int argc;
      char *argv[];
 {
+  char * binary_architecture = NULL;
   char *input_filename = NULL, *output_filename = NULL;
   char *input_target = NULL, *output_target = NULL;
   boolean show_version = false;
@@ -1793,7 +1828,7 @@ copy_main (argc, argv)
   struct section_list *p;
   struct stat statbuf;
 
-  while ((c = getopt_long (argc, argv, "b:i:I:j:K:N:s:O:d:F:L:R:SpgxXVvW:",
+  while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:R:SpgxXVvW:",
                           copy_options, (int *) 0)) != EOF)
     {
       switch (c)
@@ -1804,6 +1839,10 @@ copy_main (argc, argv)
            fatal (_("byte number must be non-negative"));
          break;
 
+        case 'B':
+          binary_architecture = optarg;
+          break;
+
        case 'i':
          interleave = atoi (optarg);
          if (interleave < 1)
@@ -2122,6 +2161,14 @@ copy_main (argc, argv)
          set_start_set = true;
          break;
 
+        case OPTION_SREC_LEN:
+          Chunk = parse_vma (optarg, "--srec-len");
+          break;
+
+        case OPTION_SREC_FORCES3:
+         S3Forced = true;
+          break;
+
        case 0:
          break;                /* we've been given a long option */
 
@@ -2153,12 +2200,30 @@ copy_main (argc, argv)
   if (output_target == (char *) NULL)
     output_target = input_target;
 
-  if (preserve_dates)
+  if (binary_architecture != (char *) NULL)
     {
-      if (stat (input_filename, &statbuf) < 0)
-       fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
+      if (input_target && strcmp (input_target, "binary") == 0)
+        {
+          const bfd_arch_info_type * temp_arch_info;
+
+         temp_arch_info = bfd_scan_arch (binary_architecture);
+
+          if (temp_arch_info != NULL)
+            bfd_external_binary_architecture = temp_arch_info->arch;
+          else
+            fatal (_("architecture %s unknown"), binary_architecture);
+        }
+      else
+       {
+         non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
+         non_fatal (_(" Argument %s ignored"), binary_architecture);
+       }
     }
 
+  if (preserve_dates)
+    if (stat (input_filename, & statbuf) < 0)
+      fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
+
   /* If there is no destination file then create a temp and rename
      the result into the input.  */
 
@@ -2248,7 +2313,15 @@ main (argc, argv)
   if (is_strip < 0)
     {
       int i = strlen (program_name);
-      is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+      /* Drop the .exe suffix, if any.  */
+      if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
+       {
+         i -= 4;
+         program_name[i] = '\0';
+       }
+#endif
+      is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
     }
 
   if (is_strip)
This page took 0.027398 seconds and 4 git commands to generate.