Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* objcopy.c -- copy object file from input to output, optionally massaging it.
  2.    Copyright 1991-2013 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Binutils.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  19.    02110-1301, USA.  */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "progress.h"
  23. #include "getopt.h"
  24. #include "libiberty.h"
  25. #include "bucomm.h"
  26. #include "budbg.h"
  27. #include "filenames.h"
  28. #include "fnmatch.h"
  29. #include "elf-bfd.h"
  30. #include "libbfd.h"
  31. #include "coff/internal.h"
  32. #include "libcoff.h"
  33.  
  34. /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
  35.    header in generic PE code.  */
  36. #include "coff/i386.h"
  37. #include "coff/pe.h"
  38.  
  39. static bfd_vma pe_file_alignment = (bfd_vma) -1;
  40. static bfd_vma pe_heap_commit = (bfd_vma) -1;
  41. static bfd_vma pe_heap_reserve = (bfd_vma) -1;
  42. static bfd_vma pe_image_base = (bfd_vma) -1;
  43. static bfd_vma pe_section_alignment = (bfd_vma) -1;
  44. static bfd_vma pe_stack_commit = (bfd_vma) -1;
  45. static bfd_vma pe_stack_reserve = (bfd_vma) -1;
  46. static short pe_subsystem = -1;
  47. static short pe_major_subsystem_version = -1;
  48. static short pe_minor_subsystem_version = -1;
  49.  
  50. struct is_specified_symbol_predicate_data
  51. {
  52.   const char    *name;
  53.   bfd_boolean   found;
  54. };
  55.  
  56. /* A list to support redefine_sym.  */
  57. struct redefine_node
  58. {
  59.   char *source;
  60.   char *target;
  61.   struct redefine_node *next;
  62. };
  63.  
  64. typedef struct section_rename
  65. {
  66.   const char *            old_name;
  67.   const char *            new_name;
  68.   flagword                flags;
  69.   struct section_rename * next;
  70. }
  71. section_rename;
  72.  
  73. /* List of sections to be renamed.  */
  74. static section_rename *section_rename_list;
  75.  
  76. static asymbol **isympp = NULL; /* Input symbols.  */
  77. static asymbol **osympp = NULL; /* Output symbols that survive stripping.  */
  78.  
  79. /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes.  */
  80. static int copy_byte = -1;
  81. static int interleave = 0; /* Initialised to 4 in copy_main().  */
  82. static int copy_width = 1;
  83.  
  84. static bfd_boolean verbose;             /* Print file and target names.  */
  85. static bfd_boolean preserve_dates;      /* Preserve input file timestamp.  */
  86. static int deterministic = -1;          /* Enable deterministic archives.  */
  87. static int status = 0;          /* Exit status.  */
  88.  
  89. enum strip_action
  90.   {
  91.     STRIP_UNDEF,
  92.     STRIP_NONE,                 /* Don't strip.  */
  93.     STRIP_DEBUG,                /* Strip all debugger symbols.  */
  94.     STRIP_UNNEEDED,             /* Strip unnecessary symbols.  */
  95.     STRIP_NONDEBUG,             /* Strip everything but debug info.  */
  96.     STRIP_DWO,                  /* Strip all DWO info.  */
  97.     STRIP_NONDWO,               /* Strip everything but DWO info.  */
  98.     STRIP_ALL                   /* Strip all symbols.  */
  99.   };
  100.  
  101. /* Which symbols to remove.  */
  102. static enum strip_action strip_symbols = STRIP_UNDEF;
  103.  
  104. enum locals_action
  105.   {
  106.     LOCALS_UNDEF,
  107.     LOCALS_START_L,             /* Discard locals starting with L.  */
  108.     LOCALS_ALL                  /* Discard all locals.  */
  109.   };
  110.  
  111. /* Which local symbols to remove.  Overrides STRIP_ALL.  */
  112. static enum locals_action discard_locals;
  113.  
  114. /* Structure used to hold lists of sections and actions to take.  */
  115. struct section_list
  116. {
  117.   struct section_list * next;      /* Next section to change.  */
  118.   const char *          pattern;   /* Section name pattern.  */
  119.   bfd_boolean           used;      /* Whether this entry was used.  */
  120.  
  121.   unsigned int          context;   /* What to do with matching sections.  */
  122.   /* Flag bits used in the context field.
  123.      COPY and REMOVE are mutually exlusive.  SET and ALTER are mutually exclusive.  */
  124. #define SECTION_CONTEXT_REMOVE    (1 << 0) /* Remove this section.  */
  125. #define SECTION_CONTEXT_COPY      (1 << 1) /* Copy this section, delete all non-copied section.  */
  126. #define SECTION_CONTEXT_SET_VMA   (1 << 2) /* Set the sections' VMA address.  */
  127. #define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address.  */
  128. #define SECTION_CONTEXT_SET_LMA   (1 << 4) /* Set the sections' LMA address.  */
  129. #define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address.  */
  130. #define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags.  */
  131.  
  132.   bfd_vma               vma_val;   /* Amount to change by or set to.  */
  133.   bfd_vma               lma_val;   /* Amount to change by or set to.  */
  134.   flagword              flags;     /* What to set the section flags to.  */
  135. };
  136.  
  137. static struct section_list *change_sections;
  138.  
  139. /* TRUE if some sections are to be removed.  */
  140. static bfd_boolean sections_removed;
  141.  
  142. /* TRUE if only some sections are to be copied.  */
  143. static bfd_boolean sections_copied;
  144.  
  145. /* Changes to the start address.  */
  146. static bfd_vma change_start = 0;
  147. static bfd_boolean set_start_set = FALSE;
  148. static bfd_vma set_start;
  149.  
  150. /* Changes to section addresses.  */
  151. static bfd_vma change_section_address = 0;
  152.  
  153. /* Filling gaps between sections.  */
  154. static bfd_boolean gap_fill_set = FALSE;
  155. static bfd_byte gap_fill = 0;
  156.  
  157. /* Pad to a given address.  */
  158. static bfd_boolean pad_to_set = FALSE;
  159. static bfd_vma pad_to;
  160.  
  161. /* Use alternative machine code?  */
  162. static unsigned long use_alt_mach_code = 0;
  163.  
  164. /* Output BFD flags user wants to set or clear */
  165. static flagword bfd_flags_to_set;
  166. static flagword bfd_flags_to_clear;
  167.  
  168. /* List of sections to add.  */
  169. struct section_add
  170. {
  171.   /* Next section to add.  */
  172.   struct section_add *next;
  173.   /* Name of section to add.  */
  174.   const char *name;
  175.   /* Name of file holding section contents.  */
  176.   const char *filename;
  177.   /* Size of file.  */
  178.   size_t size;
  179.   /* Contents of file.  */
  180.   bfd_byte *contents;
  181.   /* BFD section, after it has been added.  */
  182.   asection *section;
  183. };
  184.  
  185. /* List of sections to add to the output BFD.  */
  186. static struct section_add *add_sections;
  187.  
  188. /* If non-NULL the argument to --add-gnu-debuglink.
  189.    This should be the filename to store in the .gnu_debuglink section.  */
  190. static const char * gnu_debuglink_filename = NULL;
  191.  
  192. /* Whether to convert debugging information.  */
  193. static bfd_boolean convert_debugging = FALSE;
  194.  
  195. /* Whether to compress/decompress DWARF debug sections.  */
  196. static enum
  197. {
  198.   nothing,
  199.   compress,
  200.   decompress
  201. } do_debug_sections = nothing;
  202.  
  203. /* Whether to change the leading character in symbol names.  */
  204. static bfd_boolean change_leading_char = FALSE;
  205.  
  206. /* Whether to remove the leading character from global symbol names.  */
  207. static bfd_boolean remove_leading_char = FALSE;
  208.  
  209. /* Whether to permit wildcard in symbol comparison.  */
  210. static bfd_boolean wildcard = FALSE;
  211.  
  212. /* True if --localize-hidden is in effect.  */
  213. static bfd_boolean localize_hidden = FALSE;
  214.  
  215. /* List of symbols to strip, keep, localize, keep-global, weaken,
  216.    or redefine.  */
  217. static htab_t strip_specific_htab = NULL;
  218. static htab_t strip_unneeded_htab = NULL;
  219. static htab_t keep_specific_htab = NULL;
  220. static htab_t localize_specific_htab = NULL;
  221. static htab_t globalize_specific_htab = NULL;
  222. static htab_t keepglobal_specific_htab = NULL;
  223. static htab_t weaken_specific_htab = NULL;
  224. static struct redefine_node *redefine_sym_list = NULL;
  225.  
  226. /* If this is TRUE, we weaken global symbols (set BSF_WEAK).  */
  227. static bfd_boolean weaken = FALSE;
  228.  
  229. /* If this is TRUE, we retain BSF_FILE symbols.  */
  230. static bfd_boolean keep_file_symbols = FALSE;
  231.  
  232. /* Prefix symbols/sections.  */
  233. static char *prefix_symbols_string = 0;
  234. static char *prefix_sections_string = 0;
  235. static char *prefix_alloc_sections_string = 0;
  236.  
  237. /* True if --extract-symbol was passed on the command line.  */
  238. static bfd_boolean extract_symbol = FALSE;
  239.  
  240. /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
  241.    of <reverse_bytes> bytes within each output section.  */
  242. static int reverse_bytes = 0;
  243.  
  244. /* For Coff objects, we may want to allow or disallow long section names,
  245.    or preserve them where found in the inputs.  Debug info relies on them.  */
  246. enum long_section_name_handling
  247.   {
  248.     DISABLE,
  249.     ENABLE,
  250.     KEEP
  251.   };
  252.  
  253. /* The default long section handling mode is to preserve them.
  254.    This is also the only behaviour for 'strip'.  */
  255. static enum long_section_name_handling long_section_names = KEEP;
  256.  
  257. /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
  258. enum command_line_switch
  259.   {
  260.     OPTION_ADD_SECTION=150,
  261.     OPTION_CHANGE_ADDRESSES,
  262.     OPTION_CHANGE_LEADING_CHAR,
  263.     OPTION_CHANGE_START,
  264.     OPTION_CHANGE_SECTION_ADDRESS,
  265.     OPTION_CHANGE_SECTION_LMA,
  266.     OPTION_CHANGE_SECTION_VMA,
  267.     OPTION_CHANGE_WARNINGS,
  268.     OPTION_COMPRESS_DEBUG_SECTIONS,
  269.     OPTION_DEBUGGING,
  270.     OPTION_DECOMPRESS_DEBUG_SECTIONS,
  271.     OPTION_GAP_FILL,
  272.     OPTION_NO_CHANGE_WARNINGS,
  273.     OPTION_PAD_TO,
  274.     OPTION_REMOVE_LEADING_CHAR,
  275.     OPTION_SET_SECTION_FLAGS,
  276.     OPTION_SET_START,
  277.     OPTION_STRIP_UNNEEDED,
  278.     OPTION_WEAKEN,
  279.     OPTION_REDEFINE_SYM,
  280.     OPTION_REDEFINE_SYMS,
  281.     OPTION_SREC_LEN,
  282.     OPTION_SREC_FORCES3,
  283.     OPTION_STRIP_SYMBOLS,
  284.     OPTION_STRIP_UNNEEDED_SYMBOL,
  285.     OPTION_STRIP_UNNEEDED_SYMBOLS,
  286.     OPTION_KEEP_SYMBOLS,
  287.     OPTION_LOCALIZE_HIDDEN,
  288.     OPTION_LOCALIZE_SYMBOLS,
  289.     OPTION_LONG_SECTION_NAMES,
  290.     OPTION_GLOBALIZE_SYMBOL,
  291.     OPTION_GLOBALIZE_SYMBOLS,
  292.     OPTION_KEEPGLOBAL_SYMBOLS,
  293.     OPTION_WEAKEN_SYMBOLS,
  294.     OPTION_RENAME_SECTION,
  295.     OPTION_ALT_MACH_CODE,
  296.     OPTION_PREFIX_SYMBOLS,
  297.     OPTION_PREFIX_SECTIONS,
  298.     OPTION_PREFIX_ALLOC_SECTIONS,
  299.     OPTION_FORMATS_INFO,
  300.     OPTION_ADD_GNU_DEBUGLINK,
  301.     OPTION_ONLY_KEEP_DEBUG,
  302.     OPTION_KEEP_FILE_SYMBOLS,
  303.     OPTION_READONLY_TEXT,
  304.     OPTION_WRITABLE_TEXT,
  305.     OPTION_PURE,
  306.     OPTION_IMPURE,
  307.     OPTION_EXTRACT_SYMBOL,
  308.     OPTION_REVERSE_BYTES,
  309.     OPTION_FILE_ALIGNMENT,
  310.     OPTION_HEAP,
  311.     OPTION_IMAGE_BASE,
  312.     OPTION_SECTION_ALIGNMENT,
  313.     OPTION_STACK,
  314.     OPTION_INTERLEAVE_WIDTH,
  315.     OPTION_SUBSYSTEM,
  316.     OPTION_EXTRACT_DWO,
  317.     OPTION_STRIP_DWO
  318.   };
  319.  
  320. /* Options to handle if running as "strip".  */
  321.  
  322. static struct option strip_options[] =
  323. {
  324.   {"disable-deterministic-archives", no_argument, 0, 'U'},
  325.   {"discard-all", no_argument, 0, 'x'},
  326.   {"discard-locals", no_argument, 0, 'X'},
  327.   {"enable-deterministic-archives", no_argument, 0, 'D'},
  328.   {"format", required_argument, 0, 'F'}, /* Obsolete */
  329.   {"help", no_argument, 0, 'h'},
  330.   {"info", no_argument, 0, OPTION_FORMATS_INFO},
  331.   {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  332.   {"input-target", required_argument, 0, 'I'},
  333.   {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
  334.   {"keep-symbol", required_argument, 0, 'K'},
  335.   {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
  336.   {"output-format", required_argument, 0, 'O'}, /* Obsolete */
  337.   {"output-target", required_argument, 0, 'O'},
  338.   {"output-file", required_argument, 0, 'o'},
  339.   {"preserve-dates", no_argument, 0, 'p'},
  340.   {"remove-section", required_argument, 0, 'R'},
  341.   {"strip-all", no_argument, 0, 's'},
  342.   {"strip-debug", no_argument, 0, 'S'},
  343.   {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
  344.   {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
  345.   {"strip-symbol", required_argument, 0, 'N'},
  346.   {"target", required_argument, 0, 'F'},
  347.   {"verbose", no_argument, 0, 'v'},
  348.   {"version", no_argument, 0, 'V'},
  349.   {"wildcard", no_argument, 0, 'w'},
  350.   {0, no_argument, 0, 0}
  351. };
  352.  
  353. /* Options to handle if running as "objcopy".  */
  354.  
  355. static struct option copy_options[] =
  356. {
  357.   {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
  358.   {"add-section", required_argument, 0, OPTION_ADD_SECTION},
  359.   {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
  360.   {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
  361.   {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
  362.   {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
  363.   {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
  364.   {"binary-architecture", required_argument, 0, 'B'},
  365.   {"byte", required_argument, 0, 'b'},
  366.   {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
  367.   {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
  368.   {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
  369.   {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
  370.   {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
  371.   {"change-start", required_argument, 0, OPTION_CHANGE_START},
  372.   {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
  373.   {"compress-debug-sections", no_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
  374.   {"debugging", no_argument, 0, OPTION_DEBUGGING},
  375.   {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
  376.   {"disable-deterministic-archives", no_argument, 0, 'U'},
  377.   {"discard-all", no_argument, 0, 'x'},
  378.   {"discard-locals", no_argument, 0, 'X'},
  379.   {"enable-deterministic-archives", no_argument, 0, 'D'},
  380.   {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
  381.   {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
  382.   {"format", required_argument, 0, 'F'}, /* Obsolete */
  383.   {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
  384.   {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
  385.   {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
  386.   {"help", no_argument, 0, 'h'},
  387.   {"impure", no_argument, 0, OPTION_IMPURE},
  388.   {"info", no_argument, 0, OPTION_FORMATS_INFO},
  389.   {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  390.   {"input-target", required_argument, 0, 'I'},
  391.   {"interleave", optional_argument, 0, 'i'},
  392.   {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
  393.   {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
  394.   {"keep-global-symbol", required_argument, 0, 'G'},
  395.   {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
  396.   {"keep-symbol", required_argument, 0, 'K'},
  397.   {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
  398.   {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
  399.   {"localize-symbol", required_argument, 0, 'L'},
  400.   {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
  401.   {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
  402.   {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
  403.   {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
  404.   {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
  405.   {"only-section", required_argument, 0, 'j'},
  406.   {"output-format", required_argument, 0, 'O'}, /* Obsolete */
  407.   {"output-target", required_argument, 0, 'O'},
  408.   {"pad-to", required_argument, 0, OPTION_PAD_TO},
  409.   {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
  410.   {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
  411.   {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
  412.   {"preserve-dates", no_argument, 0, 'p'},
  413.   {"pure", no_argument, 0, OPTION_PURE},
  414.   {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
  415.   {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
  416.   {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
  417.   {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
  418.   {"remove-section", required_argument, 0, 'R'},
  419.   {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
  420.   {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
  421.   {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
  422.   {"set-start", required_argument, 0, OPTION_SET_START},
  423.   {"srec-len", required_argument, 0, OPTION_SREC_LEN},
  424.   {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
  425.   {"strip-all", no_argument, 0, 'S'},
  426.   {"strip-debug", no_argument, 0, 'g'},
  427.   {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
  428.   {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
  429.   {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
  430.   {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
  431.   {"strip-symbol", required_argument, 0, 'N'},
  432.   {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
  433.   {"target", required_argument, 0, 'F'},
  434.   {"verbose", no_argument, 0, 'v'},
  435.   {"version", no_argument, 0, 'V'},
  436.   {"weaken", no_argument, 0, OPTION_WEAKEN},
  437.   {"weaken-symbol", required_argument, 0, 'W'},
  438.   {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
  439.   {"wildcard", no_argument, 0, 'w'},
  440.   {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
  441.   {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
  442.   {"heap", required_argument, 0, OPTION_HEAP},
  443.   {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
  444.   {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
  445.   {"stack", required_argument, 0, OPTION_STACK},
  446.   {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
  447.   {0, no_argument, 0, 0}
  448. };
  449.  
  450. /* IMPORTS */
  451. extern char *program_name;
  452.  
  453. /* This flag distinguishes between strip and objcopy:
  454.    1 means this is 'strip'; 0 means this is 'objcopy'.
  455.    -1 means if we should use argv[0] to decide.  */
  456. extern int is_strip;
  457.  
  458. /* The maximum length of an S record.  This variable is declared in srec.c
  459.    and can be modified by the --srec-len parameter.  */
  460. extern unsigned int Chunk;
  461.  
  462. /* Restrict the generation of Srecords to type S3 only.
  463.    This variable is declare in bfd/srec.c and can be toggled
  464.    on by the --srec-forceS3 command line switch.  */
  465. extern bfd_boolean S3Forced;
  466.  
  467. /* Forward declarations.  */
  468. static void setup_section (bfd *, asection *, void *);
  469. static void setup_bfd_headers (bfd *, bfd *);
  470. static void copy_relocations_in_section (bfd *, asection *, void *);
  471. static void copy_section (bfd *, asection *, void *);
  472. static void get_sections (bfd *, asection *, void *);
  473. static int compare_section_lma (const void *, const void *);
  474. static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
  475. static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
  476. static const char *lookup_sym_redefinition (const char *);
  477. static void
  478. copy_usage (FILE *stream, int exit_status)
  479. {
  480.   fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
  481.   fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
  482.   fprintf (stream, _(" The options are:\n"));
  483.   fprintf (stream, _("\
  484.  -I --input-target <bfdname>      Assume input file is in format <bfdname>\n\
  485.  -O --output-target <bfdname>     Create an output file in format <bfdname>\n\
  486.  -B --binary-architecture <arch>  Set output arch, when input is arch-less\n\
  487.  -F --target <bfdname>            Set both input and output format to <bfdname>\n\
  488.     --debugging                   Convert debugging information, if possible\n\
  489.  -p --preserve-dates              Copy modified/access timestamps to the output\n"));
  490.   if (DEFAULT_AR_DETERMINISTIC)
  491.     fprintf (stream, _("\
  492.  -D --enable-deterministic-archives\n\
  493.                                   Produce deterministic output when stripping archives (default)\n\
  494.  -U --disable-deterministic-archives\n\
  495.                                   Disable -D behavior\n"));
  496.   else
  497.     fprintf (stream, _("\
  498.  -D --enable-deterministic-archives\n\
  499.                                   Produce deterministic output when stripping archives\n\
  500.  -U --disable-deterministic-archives\n\
  501.                                   Disable -D behavior (default)\n"));
  502.   fprintf (stream, _("\
  503.  -j --only-section <name>         Only copy section <name> into the output\n\
  504.     --add-gnu-debuglink=<file>    Add section .gnu_debuglink linking to <file>\n\
  505.  -R --remove-section <name>       Remove section <name> from the output\n\
  506.  -S --strip-all                   Remove all symbol and relocation information\n\
  507.  -g --strip-debug                 Remove all debugging symbols & sections\n\
  508.     --strip-dwo                   Remove all DWO sections\n\
  509.     --strip-unneeded              Remove all symbols not needed by relocations\n\
  510.  -N --strip-symbol <name>         Do not copy symbol <name>\n\
  511.     --strip-unneeded-symbol <name>\n\
  512.                                   Do not copy symbol <name> unless needed by\n\
  513.                                     relocations\n\
  514.     --only-keep-debug             Strip everything but the debug information\n\
  515.     --extract-dwo                 Copy only DWO sections\n\
  516.     --extract-symbol              Remove section contents but keep symbols\n\
  517.  -K --keep-symbol <name>          Do not strip symbol <name>\n\
  518.     --keep-file-symbols           Do not strip file symbol(s)\n\
  519.     --localize-hidden             Turn all ELF hidden symbols into locals\n\
  520.  -L --localize-symbol <name>      Force symbol <name> to be marked as a local\n\
  521.     --globalize-symbol <name>     Force symbol <name> to be marked as a global\n\
  522.  -G --keep-global-symbol <name>   Localize all symbols except <name>\n\
  523.  -W --weaken-symbol <name>        Force symbol <name> to be marked as a weak\n\
  524.     --weaken                      Force all global symbols to be marked as weak\n\
  525.  -w --wildcard                    Permit wildcard in symbol comparison\n\
  526.  -x --discard-all                 Remove all non-global symbols\n\
  527.  -X --discard-locals              Remove any compiler-generated symbols\n\
  528.  -i --interleave [<number>]       Only copy N out of every <number> bytes\n\
  529.     --interleave-width <number>   Set N for --interleave\n\
  530.  -b --byte <num>                  Select byte <num> in every interleaved block\n\
  531.     --gap-fill <val>              Fill gaps between sections with <val>\n\
  532.     --pad-to <addr>               Pad the last section up to address <addr>\n\
  533.     --set-start <addr>            Set the start address to <addr>\n\
  534.    {--change-start|--adjust-start} <incr>\n\
  535.                                   Add <incr> to the start address\n\
  536.    {--change-addresses|--adjust-vma} <incr>\n\
  537.                                   Add <incr> to LMA, VMA and start addresses\n\
  538.    {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
  539.                                   Change LMA and VMA of section <name> by <val>\n\
  540.     --change-section-lma <name>{=|+|-}<val>\n\
  541.                                   Change the LMA of section <name> by <val>\n\
  542.     --change-section-vma <name>{=|+|-}<val>\n\
  543.                                   Change the VMA of section <name> by <val>\n\
  544.    {--[no-]change-warnings|--[no-]adjust-warnings}\n\
  545.                                   Warn if a named section does not exist\n\
  546.     --set-section-flags <name>=<flags>\n\
  547.                                   Set section <name>'s properties to <flags>\n\
  548.     --add-section <name>=<file>   Add section <name> found in <file> to output\n\
  549.     --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
  550.     --long-section-names {enable|disable|keep}\n\
  551.                                   Handle long section names in Coff objects.\n\
  552.     --change-leading-char         Force output format's leading character style\n\
  553.     --remove-leading-char         Remove leading character from global symbols\n\
  554.     --reverse-bytes=<num>         Reverse <num> bytes at a time, in output sections with content\n\
  555.     --redefine-sym <old>=<new>    Redefine symbol name <old> to <new>\n\
  556.     --redefine-syms <file>        --redefine-sym for all symbol pairs \n\
  557.                                     listed in <file>\n\
  558.     --srec-len <number>           Restrict the length of generated Srecords\n\
  559.     --srec-forceS3                Restrict the type of generated Srecords to S3\n\
  560.     --strip-symbols <file>        -N for all symbols listed in <file>\n\
  561.     --strip-unneeded-symbols <file>\n\
  562.                                   --strip-unneeded-symbol for all symbols listed\n\
  563.                                     in <file>\n\
  564.     --keep-symbols <file>         -K for all symbols listed in <file>\n\
  565.     --localize-symbols <file>     -L for all symbols listed in <file>\n\
  566.     --globalize-symbols <file>    --globalize-symbol for all in <file>\n\
  567.     --keep-global-symbols <file>  -G for all symbols listed in <file>\n\
  568.     --weaken-symbols <file>       -W for all symbols listed in <file>\n\
  569.     --alt-machine-code <index>    Use the target's <index>'th alternative machine\n\
  570.     --writable-text               Mark the output text as writable\n\
  571.     --readonly-text               Make the output text write protected\n\
  572.     --pure                        Mark the output file as demand paged\n\
  573.     --impure                      Mark the output file as impure\n\
  574.     --prefix-symbols <prefix>     Add <prefix> to start of every symbol name\n\
  575.     --prefix-sections <prefix>    Add <prefix> to start of every section name\n\
  576.     --prefix-alloc-sections <prefix>\n\
  577.                                   Add <prefix> to start of every allocatable\n\
  578.                                     section name\n\
  579.     --file-alignment <num>        Set PE file alignment to <num>\n\
  580.     --heap <reserve>[,<commit>]   Set PE reserve/commit heap to <reserve>/\n\
  581.                                   <commit>\n\
  582.     --image-base <address>        Set PE image base to <address>\n\
  583.     --section-alignment <num>     Set PE section alignment to <num>\n\
  584.     --stack <reserve>[,<commit>]  Set PE reserve/commit stack to <reserve>/\n\
  585.                                   <commit>\n\
  586.     --subsystem <name>[:<version>]\n\
  587.                                   Set PE subsystem to <name> [& <version>]\n\
  588.     --compress-debug-sections     Compress DWARF debug sections using zlib\n\
  589.     --decompress-debug-sections   Decompress DWARF debug sections using zlib\n\
  590.  -v --verbose                     List all object files modified\n\
  591.  @<file>                          Read options from <file>\n\
  592.  -V --version                     Display this program's version number\n\
  593.  -h --help                        Display this output\n\
  594.     --info                        List object formats & architectures supported\n\
  595. "));
  596.   list_supported_targets (program_name, stream);
  597.   if (REPORT_BUGS_TO[0] && exit_status == 0)
  598.     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  599.   exit (exit_status);
  600. }
  601.  
  602. static void
  603. strip_usage (FILE *stream, int exit_status)
  604. {
  605.   fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
  606.   fprintf (stream, _(" Removes symbols and sections from files\n"));
  607.   fprintf (stream, _(" The options are:\n"));
  608.   fprintf (stream, _("\
  609.  -I --input-target=<bfdname>      Assume input file is in format <bfdname>\n\
  610.  -O --output-target=<bfdname>     Create an output file in format <bfdname>\n\
  611.  -F --target=<bfdname>            Set both input and output format to <bfdname>\n\
  612.  -p --preserve-dates              Copy modified/access timestamps to the output\n\
  613. "));
  614.   if (DEFAULT_AR_DETERMINISTIC)
  615.     fprintf (stream, _("\
  616.  -D --enable-deterministic-archives\n\
  617.                                   Produce deterministic output when stripping archives (default)\n\
  618.  -U --disable-deterministic-archives\n\
  619.                                   Disable -D behavior\n"));
  620.   else
  621.     fprintf (stream, _("\
  622.  -D --enable-deterministic-archives\n\
  623.                                   Produce deterministic output when stripping archives\n\
  624.  -U --disable-deterministic-archives\n\
  625.                                   Disable -D behavior (default)\n"));
  626.   fprintf (stream, _("\
  627.  -R --remove-section=<name>       Remove section <name> from the output\n\
  628.  -s --strip-all                   Remove all symbol and relocation information\n\
  629.  -g -S -d --strip-debug           Remove all debugging symbols & sections\n\
  630.     --strip-dwo                   Remove all DWO sections\n\
  631.     --strip-unneeded              Remove all symbols not needed by relocations\n\
  632.     --only-keep-debug             Strip everything but the debug information\n\
  633.  -N --strip-symbol=<name>         Do not copy symbol <name>\n\
  634.  -K --keep-symbol=<name>          Do not strip symbol <name>\n\
  635.     --keep-file-symbols           Do not strip file symbol(s)\n\
  636.  -w --wildcard                    Permit wildcard in symbol comparison\n\
  637.  -x --discard-all                 Remove all non-global symbols\n\
  638.  -X --discard-locals              Remove any compiler-generated symbols\n\
  639.  -v --verbose                     List all object files modified\n\
  640.  -V --version                     Display this program's version number\n\
  641.  -h --help                        Display this output\n\
  642.     --info                        List object formats & architectures supported\n\
  643.  -o <file>                        Place stripped output into <file>\n\
  644. "));
  645.  
  646.   list_supported_targets (program_name, stream);
  647.   if (REPORT_BUGS_TO[0] && exit_status == 0)
  648.     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  649.   exit (exit_status);
  650. }
  651.  
  652. /* Parse section flags into a flagword, with a fatal error if the
  653.    string can't be parsed.  */
  654.  
  655. static flagword
  656. parse_flags (const char *s)
  657. {
  658.   flagword ret;
  659.   const char *snext;
  660.   int len;
  661.  
  662.   ret = SEC_NO_FLAGS;
  663.  
  664.   do
  665.     {
  666.       snext = strchr (s, ',');
  667.       if (snext == NULL)
  668.         len = strlen (s);
  669.       else
  670.         {
  671.           len = snext - s;
  672.           ++snext;
  673.         }
  674.  
  675.       if (0) ;
  676. #define PARSE_FLAG(fname,fval) \
  677.   else if (strncasecmp (fname, s, len) == 0) ret |= fval
  678.       PARSE_FLAG ("alloc", SEC_ALLOC);
  679.       PARSE_FLAG ("load", SEC_LOAD);
  680.       PARSE_FLAG ("noload", SEC_NEVER_LOAD);
  681.       PARSE_FLAG ("readonly", SEC_READONLY);
  682.       PARSE_FLAG ("debug", SEC_DEBUGGING);
  683.       PARSE_FLAG ("code", SEC_CODE);
  684.       PARSE_FLAG ("data", SEC_DATA);
  685.       PARSE_FLAG ("rom", SEC_ROM);
  686.       PARSE_FLAG ("share", SEC_COFF_SHARED);
  687.       PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
  688.       PARSE_FLAG ("merge", SEC_MERGE);
  689.       PARSE_FLAG ("strings", SEC_STRINGS);
  690. #undef PARSE_FLAG
  691.       else
  692.         {
  693.           char *copy;
  694.  
  695.           copy = (char *) xmalloc (len + 1);
  696.           strncpy (copy, s, len);
  697.           copy[len] = '\0';
  698.           non_fatal (_("unrecognized section flag `%s'"), copy);
  699.           fatal (_("supported flags: %s"),
  700.                  "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
  701.         }
  702.  
  703.       s = snext;
  704.     }
  705.   while (s != NULL);
  706.  
  707.   return ret;
  708. }
  709.  
  710. /* Find and optionally add an entry in the change_sections list.
  711.  
  712.    We need to be careful in how we match section names because of the support
  713.    for wildcard characters.  For example suppose that the user has invoked
  714.    objcopy like this:
  715.  
  716.        --set-section-flags .debug_*=debug
  717.        --set-section-flags .debug_str=readonly,debug
  718.        --change-section-address .debug_*ranges=0x1000
  719.  
  720.    With the idea that all debug sections will receive the DEBUG flag, the
  721.    .debug_str section will also receive the READONLY flag and the
  722.    .debug_ranges and .debug_aranges sections will have their address set to
  723.    0x1000.  (This may not make much sense, but it is just an example).
  724.  
  725.    When adding the section name patterns to the section list we need to make
  726.    sure that previous entries do not match with the new entry, unless the
  727.    match is exact.  (In which case we assume that the user is overriding
  728.    the previous entry with the new context).
  729.  
  730.    When matching real section names to the section list we make use of the
  731.    wildcard characters, but we must do so in context.  Eg if we are setting
  732.    section addresses then we match for .debug_ranges but not for .debug_info.
  733.  
  734.    Finally, if ADD is false and we do find a match, we mark the section list
  735.    entry as used.  */
  736.  
  737. static struct section_list *
  738. find_section_list (const char *name, bfd_boolean add, unsigned int context)
  739. {
  740.   struct section_list *p;
  741.  
  742.   /* assert ((context & ((1 << 7) - 1)) != 0); */
  743.  
  744.   for (p = change_sections; p != NULL; p = p->next)
  745.     {
  746.       if (add)
  747.         {
  748.           if (strcmp (p->pattern, name) == 0)
  749.             {
  750.               /* Check for context conflicts.  */
  751.               if (((p->context & SECTION_CONTEXT_REMOVE)
  752.                    && (context & SECTION_CONTEXT_COPY))
  753.                   || ((context & SECTION_CONTEXT_REMOVE)
  754.                       && (p->context & SECTION_CONTEXT_COPY)))
  755.                 fatal (_("error: %s both copied and removed"), name);
  756.  
  757.               if (((p->context & SECTION_CONTEXT_SET_VMA)
  758.                   && (context & SECTION_CONTEXT_ALTER_VMA))
  759.                   || ((context & SECTION_CONTEXT_SET_VMA)
  760.                       && (context & SECTION_CONTEXT_ALTER_VMA)))
  761.                 fatal (_("error: %s both sets and alters VMA"), name);
  762.  
  763.               if (((p->context & SECTION_CONTEXT_SET_LMA)
  764.                   && (context & SECTION_CONTEXT_ALTER_LMA))
  765.                   || ((context & SECTION_CONTEXT_SET_LMA)
  766.                       && (context & SECTION_CONTEXT_ALTER_LMA)))
  767.                 fatal (_("error: %s both sets and alters LMA"), name);
  768.  
  769.               /* Extend the context.  */
  770.               p->context |= context;
  771.               return p;
  772.             }
  773.         }
  774.       /* If we are not adding a new name/pattern then
  775.          only check for a match if the context applies.  */
  776.       else if ((p->context & context)
  777.                /* We could check for the presence of wildchar characters
  778.                   first and choose between calling strcmp and fnmatch,
  779.                   but is that really worth it ?  */
  780.                && fnmatch (p->pattern, name, 0) == 0)
  781.         {
  782.           p->used = TRUE;
  783.           return p;
  784.         }
  785.     }
  786.  
  787.   if (! add)
  788.     return NULL;
  789.  
  790.   p = (struct section_list *) xmalloc (sizeof (struct section_list));
  791.   p->pattern = name;
  792.   p->used = FALSE;
  793.   p->context = context;
  794.   p->vma_val = 0;
  795.   p->lma_val = 0;
  796.   p->flags = 0;
  797.   p->next = change_sections;
  798.   change_sections = p;
  799.  
  800.   return p;
  801. }
  802.  
  803. /* There is htab_hash_string but no htab_eq_string. Makes sense.  */
  804.  
  805. static int
  806. eq_string (const void *s1, const void *s2)
  807. {
  808.   return strcmp ((const char *) s1, (const char *) s2) == 0;
  809. }
  810.  
  811. static htab_t
  812. create_symbol_htab (void)
  813. {
  814.   return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
  815. }
  816.  
  817. static void
  818. create_symbol_htabs (void)
  819. {
  820.   strip_specific_htab = create_symbol_htab ();
  821.   strip_unneeded_htab = create_symbol_htab ();
  822.   keep_specific_htab = create_symbol_htab ();
  823.   localize_specific_htab = create_symbol_htab ();
  824.   globalize_specific_htab = create_symbol_htab ();
  825.   keepglobal_specific_htab = create_symbol_htab ();
  826.   weaken_specific_htab = create_symbol_htab ();
  827. }
  828.  
  829. /* Add a symbol to strip_specific_list.  */
  830.  
  831. static void
  832. add_specific_symbol (const char *name, htab_t htab)
  833. {
  834.   *htab_find_slot (htab, name, INSERT) = (char *) name;
  835. }
  836.  
  837. /* Add symbols listed in `filename' to strip_specific_list.  */
  838.  
  839. #define IS_WHITESPACE(c)      ((c) == ' ' || (c) == '\t')
  840. #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
  841.  
  842. static void
  843. add_specific_symbols (const char *filename, htab_t htab)
  844. {
  845.   off_t  size;
  846.   FILE * f;
  847.   char * line;
  848.   char * buffer;
  849.   unsigned int line_count;
  850.  
  851.   size = get_file_size (filename);
  852.   if (size == 0)
  853.     {
  854.       status = 1;
  855.       return;
  856.     }
  857.  
  858.   buffer = (char *) xmalloc (size + 2);
  859.   f = fopen (filename, FOPEN_RT);
  860.   if (f == NULL)
  861.     fatal (_("cannot open '%s': %s"), filename, strerror (errno));
  862.  
  863.   if (fread (buffer, 1, size, f) == 0 || ferror (f))
  864.     fatal (_("%s: fread failed"), filename);
  865.  
  866.   fclose (f);
  867.   buffer [size] = '\n';
  868.   buffer [size + 1] = '\0';
  869.  
  870.   line_count = 1;
  871.  
  872.   for (line = buffer; * line != '\0'; line ++)
  873.     {
  874.       char * eol;
  875.       char * name;
  876.       char * name_end;
  877.       int finished = FALSE;
  878.  
  879.       for (eol = line;; eol ++)
  880.         {
  881.           switch (* eol)
  882.             {
  883.             case '\n':
  884.               * eol = '\0';
  885.               /* Cope with \n\r.  */
  886.               if (eol[1] == '\r')
  887.                 ++ eol;
  888.               finished = TRUE;
  889.               break;
  890.  
  891.             case '\r':
  892.               * eol = '\0';
  893.               /* Cope with \r\n.  */
  894.               if (eol[1] == '\n')
  895.                 ++ eol;
  896.               finished = TRUE;
  897.               break;
  898.  
  899.             case 0:
  900.               finished = TRUE;
  901.               break;
  902.  
  903.             case '#':
  904.               /* Line comment, Terminate the line here, in case a
  905.                  name is present and then allow the rest of the
  906.                  loop to find the real end of the line.  */
  907.               * eol = '\0';
  908.               break;
  909.  
  910.             default:
  911.               break;
  912.             }
  913.  
  914.           if (finished)
  915.             break;
  916.         }
  917.  
  918.       /* A name may now exist somewhere between 'line' and 'eol'.
  919.          Strip off leading whitespace and trailing whitespace,
  920.          then add it to the list.  */
  921.       for (name = line; IS_WHITESPACE (* name); name ++)
  922.         ;
  923.       for (name_end = name;
  924.            (! IS_WHITESPACE (* name_end))
  925.            && (! IS_LINE_TERMINATOR (* name_end));
  926.            name_end ++)
  927.         ;
  928.  
  929.       if (! IS_LINE_TERMINATOR (* name_end))
  930.         {
  931.           char * extra;
  932.  
  933.           for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
  934.             ;
  935.  
  936.           if (! IS_LINE_TERMINATOR (* extra))
  937.             non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
  938.                        filename, line_count);
  939.         }
  940.  
  941.       * name_end = '\0';
  942.  
  943.       if (name_end > name)
  944.         add_specific_symbol (name, htab);
  945.  
  946.       /* Advance line pointer to end of line.  The 'eol ++' in the for
  947.          loop above will then advance us to the start of the next line.  */
  948.       line = eol;
  949.       line_count ++;
  950.     }
  951. }
  952.  
  953. /* See whether a symbol should be stripped or kept
  954.    based on strip_specific_list and keep_symbols.  */
  955.  
  956. static int
  957. is_specified_symbol_predicate (void **slot, void *data)
  958. {
  959.   struct is_specified_symbol_predicate_data *d =
  960.       (struct is_specified_symbol_predicate_data *) data;
  961.   const char *slot_name = (char *) *slot;
  962.  
  963.   if (*slot_name != '!')
  964.     {
  965.       if (! fnmatch (slot_name, d->name, 0))
  966.         {
  967.           d->found = TRUE;
  968.           /* Stop traversal.  */
  969.           return 0;
  970.         }
  971.     }
  972.   else
  973.     {
  974.       if (fnmatch (slot_name + 1, d->name, 0))
  975.         {
  976.           d->found = TRUE;
  977.           /* Stop traversal.  */
  978.           return 0;
  979.         }
  980.     }
  981.  
  982.   /* Continue traversal.  */
  983.   return 1;
  984. }
  985.  
  986. static bfd_boolean
  987. is_specified_symbol (const char *name, htab_t htab)
  988. {
  989.   if (wildcard)
  990.     {
  991.       struct is_specified_symbol_predicate_data data;
  992.  
  993.       data.name = name;
  994.       data.found = FALSE;
  995.  
  996.       htab_traverse (htab, is_specified_symbol_predicate, &data);
  997.  
  998.       return data.found;
  999.     }
  1000.  
  1001.   return htab_find (htab, name) != NULL;
  1002. }
  1003.  
  1004. /* Return a pointer to the symbol used as a signature for GROUP.  */
  1005.  
  1006. static asymbol *
  1007. group_signature (asection *group)
  1008. {
  1009.   bfd *abfd = group->owner;
  1010.   Elf_Internal_Shdr *ghdr;
  1011.  
  1012.   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  1013.     return NULL;
  1014.  
  1015.   ghdr = &elf_section_data (group)->this_hdr;
  1016.   if (ghdr->sh_link < elf_numsections (abfd))
  1017.     {
  1018.       const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1019.       Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
  1020.  
  1021.       if (symhdr->sh_type == SHT_SYMTAB
  1022.           && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
  1023.         return isympp[ghdr->sh_info - 1];
  1024.     }
  1025.   return NULL;
  1026. }
  1027.  
  1028. /* Return TRUE if the section is a DWO section.  */
  1029.  
  1030. static bfd_boolean
  1031. is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  1032. {
  1033.   const char *name = bfd_get_section_name (abfd, sec);
  1034.   int len = strlen (name);
  1035.  
  1036.   return strncmp (name + len - 4, ".dwo", 4) == 0;
  1037. }
  1038.  
  1039. /* See if a non-group section is being removed.  */
  1040.  
  1041. static bfd_boolean
  1042. is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  1043. {
  1044.   if (sections_removed || sections_copied)
  1045.     {
  1046.       struct section_list *p;
  1047.       struct section_list *q;
  1048.  
  1049.       p = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
  1050.                              SECTION_CONTEXT_REMOVE);
  1051.       q = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
  1052.                              SECTION_CONTEXT_COPY);
  1053.  
  1054.       if (p && q)
  1055.         fatal (_("error: section %s matches both remove and copy options"),
  1056.                bfd_get_section_name (abfd, sec));
  1057.  
  1058.       if (p != NULL)
  1059.         return TRUE;
  1060.       if (sections_copied && q == NULL)
  1061.         return TRUE;
  1062.     }
  1063.  
  1064.   if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
  1065.     {
  1066.       if (strip_symbols == STRIP_DEBUG
  1067.           || strip_symbols == STRIP_UNNEEDED
  1068.           || strip_symbols == STRIP_ALL
  1069.           || discard_locals == LOCALS_ALL
  1070.           || convert_debugging)
  1071.         {
  1072.           /* By default we don't want to strip .reloc section.
  1073.              This section has for pe-coff special meaning.   See
  1074.              pe-dll.c file in ld, and peXXigen.c in bfd for details.  */
  1075.           if (strcmp (bfd_get_section_name (abfd, sec), ".reloc") != 0)
  1076.             return TRUE;
  1077.         }
  1078.  
  1079.       if (strip_symbols == STRIP_DWO)
  1080.         return is_dwo_section (abfd, sec);
  1081.  
  1082.       if (strip_symbols == STRIP_NONDEBUG)
  1083.         return FALSE;
  1084.     }
  1085.  
  1086.   if (strip_symbols == STRIP_NONDWO)
  1087.     return !is_dwo_section (abfd, sec);
  1088.  
  1089.   return FALSE;
  1090. }
  1091.  
  1092. /* See if a section is being removed.  */
  1093.  
  1094. static bfd_boolean
  1095. is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  1096. {
  1097.   if (is_strip_section_1 (abfd, sec))
  1098.     return TRUE;
  1099.  
  1100.   if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
  1101.     {
  1102.       asymbol *gsym;
  1103.       const char *gname;
  1104.       asection *elt, *first;
  1105.  
  1106.       /* PR binutils/3181
  1107.          If we are going to strip the group signature symbol, then
  1108.          strip the group section too.  */
  1109.       gsym = group_signature (sec);
  1110.       if (gsym != NULL)
  1111.         gname = gsym->name;
  1112.       else
  1113.         gname = sec->name;
  1114.       if ((strip_symbols == STRIP_ALL
  1115.            && !is_specified_symbol (gname, keep_specific_htab))
  1116.           || is_specified_symbol (gname, strip_specific_htab))
  1117.         return TRUE;
  1118.  
  1119.       /* Remove the group section if all members are removed.  */
  1120.       first = elt = elf_next_in_group (sec);
  1121.       while (elt != NULL)
  1122.         {
  1123.           if (!is_strip_section_1 (abfd, elt))
  1124.             return FALSE;
  1125.           elt = elf_next_in_group (elt);
  1126.           if (elt == first)
  1127.             break;
  1128.         }
  1129.  
  1130.       return TRUE;
  1131.     }
  1132.  
  1133.   return FALSE;
  1134. }
  1135.  
  1136. /* Return true if SYM is a hidden symbol.  */
  1137.  
  1138. static bfd_boolean
  1139. is_hidden_symbol (asymbol *sym)
  1140. {
  1141.   elf_symbol_type *elf_sym;
  1142.  
  1143.   elf_sym = elf_symbol_from (sym->the_bfd, sym);
  1144.   if (elf_sym != NULL)
  1145.     switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
  1146.       {
  1147.       case STV_HIDDEN:
  1148.       case STV_INTERNAL:
  1149.         return TRUE;
  1150.       }
  1151.   return FALSE;
  1152. }
  1153.  
  1154. /* Choose which symbol entries to copy; put the result in OSYMS.
  1155.    We don't copy in place, because that confuses the relocs.
  1156.    Return the number of symbols to print.  */
  1157.  
  1158. static unsigned int
  1159. filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
  1160.                 asymbol **isyms, long symcount)
  1161. {
  1162.   asymbol **from = isyms, **to = osyms;
  1163.   long src_count = 0, dst_count = 0;
  1164.   int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
  1165.  
  1166.   for (; src_count < symcount; src_count++)
  1167.     {
  1168.       asymbol *sym = from[src_count];
  1169.       flagword flags = sym->flags;
  1170.       char *name = (char *) bfd_asymbol_name (sym);
  1171.       bfd_boolean keep;
  1172.       bfd_boolean used_in_reloc = FALSE;
  1173.       bfd_boolean undefined;
  1174.       bfd_boolean rem_leading_char;
  1175.       bfd_boolean add_leading_char;
  1176.  
  1177.       undefined = bfd_is_und_section (bfd_get_section (sym));
  1178.  
  1179.       if (redefine_sym_list)
  1180.         {
  1181.           char *old_name, *new_name;
  1182.  
  1183.           old_name = (char *) bfd_asymbol_name (sym);
  1184.           new_name = (char *) lookup_sym_redefinition (old_name);
  1185.           bfd_asymbol_name (sym) = new_name;
  1186.           name = new_name;
  1187.         }
  1188.  
  1189.       /* Check if we will remove the current leading character.  */
  1190.       rem_leading_char =
  1191.         (name[0] == bfd_get_symbol_leading_char (abfd))
  1192.         && (change_leading_char
  1193.             || (remove_leading_char
  1194.                 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
  1195.                     || undefined
  1196.                     || bfd_is_com_section (bfd_get_section (sym)))));
  1197.  
  1198.       /* Check if we will add a new leading character.  */
  1199.       add_leading_char =
  1200.         change_leading_char
  1201.         && (bfd_get_symbol_leading_char (obfd) != '\0')
  1202.         && (bfd_get_symbol_leading_char (abfd) == '\0'
  1203.             || (name[0] == bfd_get_symbol_leading_char (abfd)));
  1204.  
  1205.       /* Short circuit for change_leading_char if we can do it in-place.  */
  1206.       if (rem_leading_char && add_leading_char && !prefix_symbols_string)
  1207.         {
  1208.           name[0] = bfd_get_symbol_leading_char (obfd);
  1209.           bfd_asymbol_name (sym) = name;
  1210.           rem_leading_char = FALSE;
  1211.           add_leading_char = FALSE;
  1212.         }
  1213.  
  1214.       /* Remove leading char.  */
  1215.       if (rem_leading_char)
  1216.         bfd_asymbol_name (sym) = ++name;
  1217.  
  1218.       /* Add new leading char and/or prefix.  */
  1219.       if (add_leading_char || prefix_symbols_string)
  1220.         {
  1221.           char *n, *ptr;
  1222.  
  1223.           ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
  1224.                                       + strlen (name) + 1);
  1225.           if (add_leading_char)
  1226.             *ptr++ = bfd_get_symbol_leading_char (obfd);
  1227.  
  1228.           if (prefix_symbols_string)
  1229.             {
  1230.               strcpy (ptr, prefix_symbols_string);
  1231.               ptr += strlen (prefix_symbols_string);
  1232.            }
  1233.  
  1234.           strcpy (ptr, name);
  1235.           bfd_asymbol_name (sym) = n;
  1236.           name = n;
  1237.         }
  1238.  
  1239.       if (strip_symbols == STRIP_ALL)
  1240.         keep = FALSE;
  1241.       else if ((flags & BSF_KEEP) != 0          /* Used in relocation.  */
  1242.                || ((flags & BSF_SECTION_SYM) != 0
  1243.                    && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
  1244.                        & BSF_KEEP) != 0))
  1245.         {
  1246.           keep = TRUE;
  1247.           used_in_reloc = TRUE;
  1248.         }
  1249.       else if (relocatable                      /* Relocatable file.  */
  1250.                && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
  1251.                    || bfd_is_com_section (bfd_get_section (sym))))
  1252.         keep = TRUE;
  1253.       else if (bfd_decode_symclass (sym) == 'I')
  1254.         /* Global symbols in $idata sections need to be retained
  1255.            even if relocatable is FALSE.  External users of the
  1256.            library containing the $idata section may reference these
  1257.            symbols.  */
  1258.         keep = TRUE;
  1259.       else if ((flags & BSF_GLOBAL) != 0        /* Global symbol.  */
  1260.                || (flags & BSF_WEAK) != 0
  1261.                || undefined
  1262.                || bfd_is_com_section (bfd_get_section (sym)))
  1263.         keep = strip_symbols != STRIP_UNNEEDED;
  1264.       else if ((flags & BSF_DEBUGGING) != 0)    /* Debugging symbol.  */
  1265.         keep = (strip_symbols != STRIP_DEBUG
  1266.                 && strip_symbols != STRIP_UNNEEDED
  1267.                 && ! convert_debugging);
  1268.       else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
  1269.         /* COMDAT sections store special information in local
  1270.            symbols, so we cannot risk stripping any of them.  */
  1271.         keep = TRUE;
  1272.       else                      /* Local symbol.  */
  1273.         keep = (strip_symbols != STRIP_UNNEEDED
  1274.                 && (discard_locals != LOCALS_ALL
  1275.                     && (discard_locals != LOCALS_START_L
  1276.                         || ! bfd_is_local_label (abfd, sym))));
  1277.  
  1278.       if (keep && is_specified_symbol (name, strip_specific_htab))
  1279.         {
  1280.           /* There are multiple ways to set 'keep' above, but if it
  1281.              was the relocatable symbol case, then that's an error.  */
  1282.           if (used_in_reloc)
  1283.             {
  1284.               non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
  1285.               status = 1;
  1286.             }
  1287.           else
  1288.             keep = FALSE;
  1289.         }
  1290.  
  1291.       if (keep
  1292.           && !(flags & BSF_KEEP)
  1293.           && is_specified_symbol (name, strip_unneeded_htab))
  1294.         keep = FALSE;
  1295.  
  1296.       if (!keep
  1297.           && ((keep_file_symbols && (flags & BSF_FILE))
  1298.               || is_specified_symbol (name, keep_specific_htab)))
  1299.         keep = TRUE;
  1300.  
  1301.       if (keep && is_strip_section (abfd, bfd_get_section (sym)))
  1302.         keep = FALSE;
  1303.  
  1304.       if (keep)
  1305.         {
  1306.           if ((flags & BSF_GLOBAL) != 0
  1307.               && (weaken || is_specified_symbol (name, weaken_specific_htab)))
  1308.             {
  1309.               sym->flags &= ~ BSF_GLOBAL;
  1310.               sym->flags |= BSF_WEAK;
  1311.             }
  1312.  
  1313.           if (!undefined
  1314.               && (flags & (BSF_GLOBAL | BSF_WEAK))
  1315.               && (is_specified_symbol (name, localize_specific_htab)
  1316.                   || (htab_elements (keepglobal_specific_htab) != 0
  1317.                       && ! is_specified_symbol (name, keepglobal_specific_htab))
  1318.                   || (localize_hidden && is_hidden_symbol (sym))))
  1319.             {
  1320.               sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
  1321.               sym->flags |= BSF_LOCAL;
  1322.             }
  1323.  
  1324.           if (!undefined
  1325.               && (flags & BSF_LOCAL)
  1326.               && is_specified_symbol (name, globalize_specific_htab))
  1327.             {
  1328.               sym->flags &= ~ BSF_LOCAL;
  1329.               sym->flags |= BSF_GLOBAL;
  1330.             }
  1331.  
  1332.           to[dst_count++] = sym;
  1333.         }
  1334.     }
  1335.  
  1336.   to[dst_count] = NULL;
  1337.  
  1338.   return dst_count;
  1339. }
  1340.  
  1341. /* Find the redefined name of symbol SOURCE.  */
  1342.  
  1343. static const char *
  1344. lookup_sym_redefinition (const char *source)
  1345. {
  1346.   struct redefine_node *list;
  1347.  
  1348.   for (list = redefine_sym_list; list != NULL; list = list->next)
  1349.     if (strcmp (source, list->source) == 0)
  1350.       return list->target;
  1351.  
  1352.   return source;
  1353. }
  1354.  
  1355. /* Add a node to a symbol redefine list.  */
  1356.  
  1357. static void
  1358. redefine_list_append (const char *cause, const char *source, const char *target)
  1359. {
  1360.   struct redefine_node **p;
  1361.   struct redefine_node *list;
  1362.   struct redefine_node *new_node;
  1363.  
  1364.   for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
  1365.     {
  1366.       if (strcmp (source, list->source) == 0)
  1367.         fatal (_("%s: Multiple redefinition of symbol \"%s\""),
  1368.                cause, source);
  1369.  
  1370.       if (strcmp (target, list->target) == 0)
  1371.         fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
  1372.                cause, target);
  1373.     }
  1374.  
  1375.   new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
  1376.  
  1377.   new_node->source = strdup (source);
  1378.   new_node->target = strdup (target);
  1379.   new_node->next = NULL;
  1380.  
  1381.   *p = new_node;
  1382. }
  1383.  
  1384. /* Handle the --redefine-syms option.  Read lines containing "old new"
  1385.    from the file, and add them to the symbol redefine list.  */
  1386.  
  1387. static void
  1388. add_redefine_syms_file (const char *filename)
  1389. {
  1390.   FILE *file;
  1391.   char *buf;
  1392.   size_t bufsize;
  1393.   size_t len;
  1394.   size_t outsym_off;
  1395.   int c, lineno;
  1396.  
  1397.   file = fopen (filename, "r");
  1398.   if (file == NULL)
  1399.     fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
  1400.            filename, strerror (errno));
  1401.  
  1402.   bufsize = 100;
  1403.   buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL.  */);
  1404.  
  1405.   lineno = 1;
  1406.   c = getc (file);
  1407.   len = 0;
  1408.   outsym_off = 0;
  1409.   while (c != EOF)
  1410.     {
  1411.       /* Collect the input symbol name.  */
  1412.       while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
  1413.         {
  1414.           if (c == '#')
  1415.             goto comment;
  1416.           buf[len++] = c;
  1417.           if (len >= bufsize)
  1418.             {
  1419.               bufsize *= 2;
  1420.               buf = (char *) xrealloc (buf, bufsize + 1);
  1421.             }
  1422.           c = getc (file);
  1423.         }
  1424.       buf[len++] = '\0';
  1425.       if (c == EOF)
  1426.         break;
  1427.  
  1428.       /* Eat white space between the symbol names.  */
  1429.       while (IS_WHITESPACE (c))
  1430.         c = getc (file);
  1431.       if (c == '#' || IS_LINE_TERMINATOR (c))
  1432.         goto comment;
  1433.       if (c == EOF)
  1434.         break;
  1435.  
  1436.       /* Collect the output symbol name.  */
  1437.       outsym_off = len;
  1438.       while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
  1439.         {
  1440.           if (c == '#')
  1441.             goto comment;
  1442.           buf[len++] = c;
  1443.           if (len >= bufsize)
  1444.             {
  1445.               bufsize *= 2;
  1446.               buf = (char *) xrealloc (buf, bufsize + 1);
  1447.             }
  1448.           c = getc (file);
  1449.         }
  1450.       buf[len++] = '\0';
  1451.       if (c == EOF)
  1452.         break;
  1453.  
  1454.       /* Eat white space at end of line.  */
  1455.       while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
  1456.         c = getc (file);
  1457.       if (c == '#')
  1458.         goto comment;
  1459.       /* Handle \r\n.  */
  1460.       if ((c == '\r' && (c = getc (file)) == '\n')
  1461.           || c == '\n' || c == EOF)
  1462.         {
  1463.  end_of_line:
  1464.           /* Append the redefinition to the list.  */
  1465.           if (buf[0] != '\0')
  1466.             redefine_list_append (filename, &buf[0], &buf[outsym_off]);
  1467.  
  1468.           lineno++;
  1469.           len = 0;
  1470.           outsym_off = 0;
  1471.           if (c == EOF)
  1472.             break;
  1473.           c = getc (file);
  1474.           continue;
  1475.         }
  1476.       else
  1477.         fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
  1478.  comment:
  1479.       if (len != 0 && (outsym_off == 0 || outsym_off == len))
  1480.         fatal (_("%s:%d: missing new symbol name"), filename, lineno);
  1481.       buf[len++] = '\0';
  1482.  
  1483.       /* Eat the rest of the line and finish it.  */
  1484.       while (c != '\n' && c != EOF)
  1485.         c = getc (file);
  1486.       goto end_of_line;
  1487.     }
  1488.  
  1489.   if (len != 0)
  1490.     fatal (_("%s:%d: premature end of file"), filename, lineno);
  1491.  
  1492.   free (buf);
  1493. }
  1494.  
  1495. /* Copy unkown object file IBFD onto OBFD.
  1496.    Returns TRUE upon success, FALSE otherwise.  */
  1497.  
  1498. static bfd_boolean
  1499. copy_unknown_object (bfd *ibfd, bfd *obfd)
  1500. {
  1501.   char *cbuf;
  1502.   int tocopy;
  1503.   long ncopied;
  1504.   long size;
  1505.   struct stat buf;
  1506.  
  1507.   if (bfd_stat_arch_elt (ibfd, &buf) != 0)
  1508.     {
  1509.       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1510.       return FALSE;
  1511.     }
  1512.  
  1513.   size = buf.st_size;
  1514.   if (size < 0)
  1515.     {
  1516.       non_fatal (_("stat returns negative size for `%s'"),
  1517.                  bfd_get_archive_filename (ibfd));
  1518.       return FALSE;
  1519.     }
  1520.  
  1521.   if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
  1522.     {
  1523.       bfd_nonfatal (bfd_get_archive_filename (ibfd));
  1524.       return FALSE;
  1525.     }
  1526.  
  1527.   if (verbose)
  1528.     printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
  1529.             bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
  1530.  
  1531.   cbuf = (char *) xmalloc (BUFSIZE);
  1532.   ncopied = 0;
  1533.   while (ncopied < size)
  1534.     {
  1535.       tocopy = size - ncopied;
  1536.       if (tocopy > BUFSIZE)
  1537.         tocopy = BUFSIZE;
  1538.  
  1539.       if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
  1540.           != (bfd_size_type) tocopy)
  1541.         {
  1542.           bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1543.           free (cbuf);
  1544.           return FALSE;
  1545.         }
  1546.  
  1547.       if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
  1548.           != (bfd_size_type) tocopy)
  1549.         {
  1550.           bfd_nonfatal_message (NULL, obfd, NULL, NULL);
  1551.           free (cbuf);
  1552.           return FALSE;
  1553.         }
  1554.  
  1555.       ncopied += tocopy;
  1556.     }
  1557.  
  1558.   /* We should at least to be able to read it back when copying an
  1559.      unknown object in an archive.  */
  1560. //  chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
  1561.   free (cbuf);
  1562.   return TRUE;
  1563. }
  1564.  
  1565. /* Copy object file IBFD onto OBFD.
  1566.    Returns TRUE upon success, FALSE otherwise.  */
  1567.  
  1568. static bfd_boolean
  1569. copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
  1570. {
  1571.   bfd_vma start;
  1572.   long symcount;
  1573.   asection **osections = NULL;
  1574.   asection *gnu_debuglink_section = NULL;
  1575.   bfd_size_type *gaps = NULL;
  1576.   bfd_size_type max_gap = 0;
  1577.   long symsize;
  1578.   void *dhandle;
  1579.   enum bfd_architecture iarch;
  1580.   unsigned int imach;
  1581.  
  1582.   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
  1583.       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
  1584.       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
  1585.     fatal (_("Unable to change endianness of input file(s)"));
  1586.  
  1587.   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  1588.     {
  1589.       bfd_nonfatal_message (NULL, obfd, NULL, NULL);
  1590.       return FALSE;
  1591.     }
  1592.  
  1593.   if (verbose)
  1594.     printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
  1595.             bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
  1596.             bfd_get_filename (obfd), bfd_get_target (obfd));
  1597.  
  1598.   if (extract_symbol)
  1599.     start = 0;
  1600.   else
  1601.     {
  1602.       if (set_start_set)
  1603.         start = set_start;
  1604.       else
  1605.         start = bfd_get_start_address (ibfd);
  1606.       start += change_start;
  1607.     }
  1608.  
  1609.   /* Neither the start address nor the flags
  1610.      need to be set for a core file.  */
  1611.   if (bfd_get_format (obfd) != bfd_core)
  1612.     {
  1613.       flagword flags;
  1614.  
  1615.       flags = bfd_get_file_flags (ibfd);
  1616.       flags |= bfd_flags_to_set;
  1617.       flags &= ~bfd_flags_to_clear;
  1618.       flags &= bfd_applicable_file_flags (obfd);
  1619.  
  1620.       if (strip_symbols == STRIP_ALL)
  1621.         flags &= ~HAS_RELOC;
  1622.  
  1623.       if (!bfd_set_start_address (obfd, start)
  1624.           || !bfd_set_file_flags (obfd, flags))
  1625.         {
  1626.           bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1627.           return FALSE;
  1628.         }
  1629.     }
  1630.  
  1631.   /* Copy architecture of input file to output file.  */
  1632.   iarch = bfd_get_arch (ibfd);
  1633.   imach = bfd_get_mach (ibfd);
  1634.   if (input_arch)
  1635.     {
  1636.       if (bfd_get_arch_info (ibfd) == NULL
  1637.           || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
  1638.         {
  1639.           iarch = input_arch->arch;
  1640.           imach = input_arch->mach;
  1641.         }
  1642.       else
  1643.         non_fatal (_("Input file `%s' ignores binary architecture parameter."),
  1644.                    bfd_get_archive_filename (ibfd));
  1645.     }
  1646.   if (!bfd_set_arch_mach (obfd, iarch, imach)
  1647.       && (ibfd->target_defaulted
  1648.           || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
  1649.     {
  1650.       if (bfd_get_arch (ibfd) == bfd_arch_unknown)
  1651.         non_fatal (_("Unable to recognise the format of the input file `%s'"),
  1652.                    bfd_get_archive_filename (ibfd));
  1653.       else
  1654.         non_fatal (_("Output file cannot represent architecture `%s'"),
  1655.                    bfd_printable_arch_mach (bfd_get_arch (ibfd),
  1656.                                             bfd_get_mach (ibfd)));
  1657.       return FALSE;
  1658.     }
  1659.  
  1660.   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  1661.     {
  1662.       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1663.       return FALSE;
  1664.     }
  1665.  
  1666.   if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
  1667.       && bfd_pei_p (obfd))
  1668.     {
  1669.       /* Set up PE parameters.  */
  1670.       pe_data_type *pe = pe_data (obfd);
  1671.  
  1672.       /* Copy PE parameters before changing them.  */
  1673.       if (ibfd->xvec->flavour == bfd_target_coff_flavour
  1674.           && bfd_pei_p (ibfd))
  1675.         pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
  1676.  
  1677.       if (pe_file_alignment != (bfd_vma) -1)
  1678.         pe->pe_opthdr.FileAlignment = pe_file_alignment;
  1679.       else
  1680.         pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
  1681.  
  1682.       if (pe_heap_commit != (bfd_vma) -1)
  1683.         pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
  1684.  
  1685.       if (pe_heap_reserve != (bfd_vma) -1)
  1686.         pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
  1687.  
  1688.       if (pe_image_base != (bfd_vma) -1)
  1689.         pe->pe_opthdr.ImageBase = pe_image_base;
  1690.  
  1691.       if (pe_section_alignment != (bfd_vma) -1)
  1692.         pe->pe_opthdr.SectionAlignment = pe_section_alignment;
  1693.       else
  1694.         pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
  1695.  
  1696.       if (pe_stack_commit != (bfd_vma) -1)
  1697.         pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
  1698.  
  1699.       if (pe_stack_reserve != (bfd_vma) -1)
  1700.         pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
  1701.  
  1702.       if (pe_subsystem != -1)
  1703.         pe->pe_opthdr.Subsystem = pe_subsystem;
  1704.  
  1705.       if (pe_major_subsystem_version != -1)
  1706.         pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
  1707.  
  1708.       if (pe_minor_subsystem_version != -1)
  1709.         pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
  1710.  
  1711.       if (pe_file_alignment > pe_section_alignment)
  1712.         {
  1713.           char file_alignment[20], section_alignment[20];
  1714.  
  1715.           sprintf_vma (file_alignment, pe_file_alignment);
  1716.           sprintf_vma (section_alignment, pe_section_alignment);
  1717.           non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
  1718.  
  1719.                      file_alignment, section_alignment);
  1720.         }
  1721.     }
  1722.  
  1723.   if (isympp)
  1724.     free (isympp);
  1725.  
  1726.   if (osympp != isympp)
  1727.     free (osympp);
  1728.  
  1729.   isympp = NULL;
  1730.   osympp = NULL;
  1731.  
  1732.   symsize = bfd_get_symtab_upper_bound (ibfd);
  1733.   if (symsize < 0)
  1734.     {
  1735.       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1736.       return FALSE;
  1737.     }
  1738.  
  1739.   osympp = isympp = (asymbol **) xmalloc (symsize);
  1740.   symcount = bfd_canonicalize_symtab (ibfd, isympp);
  1741.   if (symcount < 0)
  1742.     {
  1743.       bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1744.       return FALSE;
  1745.     }
  1746.  
  1747.   /* BFD mandates that all output sections be created and sizes set before
  1748.      any output is done.  Thus, we traverse all sections multiple times.  */
  1749.   bfd_map_over_sections (ibfd, setup_section, obfd);
  1750.  
  1751.   if (!extract_symbol)
  1752.     setup_bfd_headers (ibfd, obfd);
  1753.  
  1754.   if (add_sections != NULL)
  1755.     {
  1756.       struct section_add *padd;
  1757.       struct section_list *pset;
  1758.  
  1759.       for (padd = add_sections; padd != NULL; padd = padd->next)
  1760.         {
  1761.           flagword flags;
  1762.  
  1763.           pset = find_section_list (padd->name, FALSE,
  1764.                                     SECTION_CONTEXT_SET_FLAGS);
  1765.           if (pset != NULL)
  1766.             flags = pset->flags | SEC_HAS_CONTENTS;
  1767.           else
  1768.             flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
  1769.  
  1770.           /* bfd_make_section_with_flags() does not return very helpful
  1771.              error codes, so check for the most likely user error first.  */
  1772.           if (bfd_get_section_by_name (obfd, padd->name))
  1773.             {
  1774.               bfd_nonfatal_message (NULL, obfd, NULL,
  1775.                                  _("can't add section '%s'"), padd->name);
  1776.               return FALSE;
  1777.             }
  1778.           else
  1779.             {
  1780.               /* We use LINKER_CREATED here so that the backend hooks
  1781.                  will create any special section type information,
  1782.                  instead of presuming we know what we're doing merely
  1783.                  because we set the flags.  */
  1784.               padd->section = bfd_make_section_with_flags
  1785.                 (obfd, padd->name, flags | SEC_LINKER_CREATED);
  1786.               if (padd->section == NULL)
  1787.                 {
  1788.                   bfd_nonfatal_message (NULL, obfd, NULL,
  1789.                                         _("can't create section `%s'"),
  1790.                                         padd->name);
  1791.                   return FALSE;
  1792.                 }
  1793.             }
  1794.  
  1795.           if (! bfd_set_section_size (obfd, padd->section, padd->size))
  1796.             {
  1797.               bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  1798.               return FALSE;
  1799.             }
  1800.  
  1801.           pset = find_section_list (padd->name, FALSE,
  1802.                                     SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
  1803.           if (pset != NULL
  1804.               && ! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
  1805.             {
  1806.               bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  1807.               return FALSE;
  1808.             }
  1809.  
  1810.           pset = find_section_list (padd->name, FALSE,
  1811.                                     SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
  1812.           if (pset != NULL)
  1813.             {
  1814.               padd->section->lma = pset->lma_val;
  1815.  
  1816.               if (! bfd_set_section_alignment
  1817.                   (obfd, padd->section,
  1818.                    bfd_section_alignment (obfd, padd->section)))
  1819.                 {
  1820.                   bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  1821.                   return FALSE;
  1822.                 }
  1823.             }
  1824.         }
  1825.     }
  1826.  
  1827.   if (gnu_debuglink_filename != NULL)
  1828.     {
  1829.       /* PR 15125: Give a helpful warning message if
  1830.          the debuglink section already exists, and
  1831.          allow the rest of the copy to complete.  */
  1832.       if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
  1833.         {
  1834.           non_fatal (_("%s: debuglink section already exists"),
  1835.                      bfd_get_filename (obfd));
  1836.           gnu_debuglink_filename = NULL;
  1837.         }
  1838.       else
  1839.         {
  1840.           gnu_debuglink_section = bfd_create_gnu_debuglink_section
  1841.             (obfd, gnu_debuglink_filename);
  1842.  
  1843.           if (gnu_debuglink_section == NULL)
  1844.             {
  1845.               bfd_nonfatal_message (NULL, obfd, NULL,
  1846.                                     _("cannot create debug link section `%s'"),
  1847.                                     gnu_debuglink_filename);
  1848.               return FALSE;
  1849.             }
  1850.  
  1851.           /* Special processing for PE format files.  We
  1852.              have no way to distinguish PE from COFF here.  */
  1853.           if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
  1854.             {
  1855.               bfd_vma debuglink_vma;
  1856.               asection * highest_section;
  1857.               asection * sec;
  1858.  
  1859.               /* The PE spec requires that all sections be adjacent and sorted
  1860.                  in ascending order of VMA.  It also specifies that debug
  1861.                  sections should be last.  This is despite the fact that debug
  1862.                  sections are not loaded into memory and so in theory have no
  1863.                  use for a VMA.
  1864.  
  1865.                  This means that the debuglink section must be given a non-zero
  1866.                  VMA which makes it contiguous with other debug sections.  So
  1867.                  walk the current section list, find the section with the
  1868.                  highest VMA and start the debuglink section after that one.  */
  1869.               for (sec = obfd->sections, highest_section = NULL;
  1870.                    sec != NULL;
  1871.                    sec = sec->next)
  1872.                 if (sec->vma > 0
  1873.                     && (highest_section == NULL
  1874.                         || sec->vma > highest_section->vma))
  1875.                   highest_section = sec;
  1876.  
  1877.               if (highest_section)
  1878.                 debuglink_vma = BFD_ALIGN (highest_section->vma
  1879.                                            + highest_section->size,
  1880.                                            /* FIXME: We ought to be using
  1881.                                               COFF_PAGE_SIZE here or maybe
  1882.                                               bfd_get_section_alignment() (if it
  1883.                                               was set) but since this is for PE
  1884.                                               and we know the required alignment
  1885.                                               it is easier just to hard code it.  */
  1886.                                            0x1000);
  1887.               else
  1888.                 /* Umm, not sure what to do in this case.  */
  1889.                 debuglink_vma = 0x1000;
  1890.  
  1891.               bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
  1892.             }
  1893.         }
  1894.     }
  1895.  
  1896.   if (bfd_count_sections (obfd) != 0
  1897.       && (gap_fill_set || pad_to_set))
  1898.     {
  1899.       asection **set;
  1900.       unsigned int c, i;
  1901.  
  1902.       /* We must fill in gaps between the sections and/or we must pad
  1903.          the last section to a specified address.  We do this by
  1904.          grabbing a list of the sections, sorting them by VMA, and
  1905.          increasing the section sizes as required to fill the gaps.
  1906.          We write out the gap contents below.  */
  1907.  
  1908.       c = bfd_count_sections (obfd);
  1909.       osections = (asection **) xmalloc (c * sizeof (asection *));
  1910.       set = osections;
  1911.       bfd_map_over_sections (obfd, get_sections, &set);
  1912.  
  1913.       qsort (osections, c, sizeof (asection *), compare_section_lma);
  1914.  
  1915.       gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
  1916.       memset (gaps, 0, c * sizeof (bfd_size_type));
  1917.  
  1918.       if (gap_fill_set)
  1919.         {
  1920.           for (i = 0; i < c - 1; i++)
  1921.             {
  1922.               flagword flags;
  1923.               bfd_size_type size;
  1924.               bfd_vma gap_start, gap_stop;
  1925.  
  1926.               flags = bfd_get_section_flags (obfd, osections[i]);
  1927.               if ((flags & SEC_HAS_CONTENTS) == 0
  1928.                   || (flags & SEC_LOAD) == 0)
  1929.                 continue;
  1930.  
  1931.               size = bfd_section_size (obfd, osections[i]);
  1932.               gap_start = bfd_section_lma (obfd, osections[i]) + size;
  1933.               gap_stop = bfd_section_lma (obfd, osections[i + 1]);
  1934.               if (gap_start < gap_stop)
  1935.                 {
  1936.                   if (! bfd_set_section_size (obfd, osections[i],
  1937.                                               size + (gap_stop - gap_start)))
  1938.                     {
  1939.                       bfd_nonfatal_message (NULL, obfd, osections[i],
  1940.                                             _("Can't fill gap after section"));
  1941.                       status = 1;
  1942.                       break;
  1943.                     }
  1944.                   gaps[i] = gap_stop - gap_start;
  1945.                   if (max_gap < gap_stop - gap_start)
  1946.                     max_gap = gap_stop - gap_start;
  1947.                 }
  1948.             }
  1949.         }
  1950.  
  1951.       if (pad_to_set)
  1952.         {
  1953.           bfd_vma lma;
  1954.           bfd_size_type size;
  1955.  
  1956.           lma = bfd_section_lma (obfd, osections[c - 1]);
  1957.           size = bfd_section_size (obfd, osections[c - 1]);
  1958.           if (lma + size < pad_to)
  1959.             {
  1960.               if (! bfd_set_section_size (obfd, osections[c - 1],
  1961.                                           pad_to - lma))
  1962.                 {
  1963.                   bfd_nonfatal_message (NULL, obfd, osections[c - 1],
  1964.                                         _("can't add padding"));
  1965.                   status = 1;
  1966.                 }
  1967.               else
  1968.                 {
  1969.                   gaps[c - 1] = pad_to - (lma + size);
  1970.                   if (max_gap < pad_to - (lma + size))
  1971.                     max_gap = pad_to - (lma + size);
  1972.                 }
  1973.             }
  1974.         }
  1975.     }
  1976.  
  1977.   /* Symbol filtering must happen after the output sections
  1978.      have been created, but before their contents are set.  */
  1979.   dhandle = NULL;
  1980.   if (convert_debugging)
  1981.     dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
  1982.  
  1983.   if (strip_symbols == STRIP_DEBUG
  1984.       || strip_symbols == STRIP_ALL
  1985.       || strip_symbols == STRIP_UNNEEDED
  1986.       || strip_symbols == STRIP_NONDEBUG
  1987.       || strip_symbols == STRIP_DWO
  1988.       || strip_symbols == STRIP_NONDWO
  1989.       || discard_locals != LOCALS_UNDEF
  1990.       || localize_hidden
  1991.       || htab_elements (strip_specific_htab) != 0
  1992.       || htab_elements (keep_specific_htab) != 0
  1993.       || htab_elements (localize_specific_htab) != 0
  1994.       || htab_elements (globalize_specific_htab) != 0
  1995.       || htab_elements (keepglobal_specific_htab) != 0
  1996.       || htab_elements (weaken_specific_htab) != 0
  1997.       || prefix_symbols_string
  1998.       || sections_removed
  1999.       || sections_copied
  2000.       || convert_debugging
  2001.       || change_leading_char
  2002.       || remove_leading_char
  2003.       || redefine_sym_list
  2004.       || weaken)
  2005.     {
  2006.       /* Mark symbols used in output relocations so that they
  2007.          are kept, even if they are local labels or static symbols.
  2008.  
  2009.          Note we iterate over the input sections examining their
  2010.          relocations since the relocations for the output sections
  2011.          haven't been set yet.  mark_symbols_used_in_relocations will
  2012.          ignore input sections which have no corresponding output
  2013.          section.  */
  2014.       if (strip_symbols != STRIP_ALL)
  2015.         bfd_map_over_sections (ibfd,
  2016.                                mark_symbols_used_in_relocations,
  2017.                                isympp);
  2018.       osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
  2019.       symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
  2020.     }
  2021.  
  2022.   if (convert_debugging && dhandle != NULL)
  2023.     {
  2024.       if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
  2025.         {
  2026.           status = 1;
  2027.           return FALSE;
  2028.         }
  2029.     }
  2030.  
  2031.   bfd_set_symtab (obfd, osympp, symcount);
  2032.  
  2033.   /* This has to happen before section positions are set.  */
  2034.   bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
  2035.  
  2036.   /* This has to happen after the symbol table has been set.  */
  2037.   bfd_map_over_sections (ibfd, copy_section, obfd);
  2038.  
  2039.   if (add_sections != NULL)
  2040.     {
  2041.       struct section_add *padd;
  2042.  
  2043.       for (padd = add_sections; padd != NULL; padd = padd->next)
  2044.         {
  2045.           if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
  2046.                                           0, padd->size))
  2047.             {
  2048.               bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  2049.               return FALSE;
  2050.             }
  2051.         }
  2052.     }
  2053.  
  2054.   if (gnu_debuglink_filename != NULL)
  2055.     {
  2056.       if (! bfd_fill_in_gnu_debuglink_section
  2057.           (obfd, gnu_debuglink_section, gnu_debuglink_filename))
  2058.         {
  2059.           bfd_nonfatal_message (NULL, obfd, NULL,
  2060.                                 _("cannot fill debug link section `%s'"),
  2061.                                 gnu_debuglink_filename);
  2062.           return FALSE;
  2063.         }
  2064.     }
  2065.  
  2066.   if (gap_fill_set || pad_to_set)
  2067.     {
  2068.       bfd_byte *buf;
  2069.       int c, i;
  2070.  
  2071.       /* Fill in the gaps.  */
  2072.       if (max_gap > 8192)
  2073.         max_gap = 8192;
  2074.       buf = (bfd_byte *) xmalloc (max_gap);
  2075.       memset (buf, gap_fill, max_gap);
  2076.  
  2077.       c = bfd_count_sections (obfd);
  2078.       for (i = 0; i < c; i++)
  2079.         {
  2080.           if (gaps[i] != 0)
  2081.             {
  2082.               bfd_size_type left;
  2083.               file_ptr off;
  2084.  
  2085.               left = gaps[i];
  2086.               off = bfd_section_size (obfd, osections[i]) - left;
  2087.  
  2088.               while (left > 0)
  2089.                 {
  2090.                   bfd_size_type now;
  2091.  
  2092.                   if (left > 8192)
  2093.                     now = 8192;
  2094.                   else
  2095.                     now = left;
  2096.  
  2097.                   if (! bfd_set_section_contents (obfd, osections[i], buf,
  2098.                                                   off, now))
  2099.                     {
  2100.                       bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
  2101.                       return FALSE;
  2102.                     }
  2103.  
  2104.                   left -= now;
  2105.                   off += now;
  2106.                 }
  2107.             }
  2108.         }
  2109.     }
  2110.  
  2111.   /* Do not copy backend data if --extract-symbol is passed; anything
  2112.      that needs to look at the section contents will fail.  */
  2113.   if (extract_symbol)
  2114.     return TRUE;
  2115.  
  2116.   /* Allow the BFD backend to copy any private data it understands
  2117.      from the input BFD to the output BFD.  This is done last to
  2118.      permit the routine to look at the filtered symbol table, which is
  2119.      important for the ECOFF code at least.  */
  2120.   if (! bfd_copy_private_bfd_data (ibfd, obfd))
  2121.     {
  2122.       bfd_nonfatal_message (NULL, obfd, NULL,
  2123.                             _("error copying private BFD data"));
  2124.       return FALSE;
  2125.     }
  2126.  
  2127.   /* Switch to the alternate machine code.  We have to do this at the
  2128.      very end, because we only initialize the header when we create
  2129.      the first section.  */
  2130.   if (use_alt_mach_code != 0)
  2131.     {
  2132.       if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
  2133.         {
  2134.           non_fatal (_("this target does not support %lu alternative machine codes"),
  2135.                      use_alt_mach_code);
  2136.           if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
  2137.             {
  2138.               non_fatal (_("treating that number as an absolute e_machine value instead"));
  2139.               elf_elfheader (obfd)->e_machine = use_alt_mach_code;
  2140.             }
  2141.           else
  2142.             non_fatal (_("ignoring the alternative value"));
  2143.         }
  2144.     }
  2145.  
  2146.   return TRUE;
  2147. }
  2148.  
  2149. /* Read each archive element in turn from IBFD, copy the
  2150.    contents to temp file, and keep the temp file handle.
  2151.    If 'force_output_target' is TRUE then make sure that
  2152.    all elements in the new archive are of the type
  2153.    'output_target'.  */
  2154.  
  2155. static void
  2156. copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
  2157.               bfd_boolean force_output_target,
  2158.               const bfd_arch_info_type *input_arch)
  2159. {
  2160.   struct name_list
  2161.     {
  2162.       struct name_list *next;
  2163.       const char *name;
  2164.       bfd *obfd;
  2165.     } *list, *l;
  2166.   bfd **ptr = &obfd->archive_head;
  2167.   bfd *this_element;
  2168.   char *dir;
  2169.   const char *filename;
  2170.  
  2171.   /* Make a temp directory to hold the contents.  */
  2172.   dir = make_tempdir (bfd_get_filename (obfd));
  2173.   if (dir == NULL)
  2174.       fatal (_("cannot create tempdir for archive copying (error: %s)"),
  2175.            strerror (errno));
  2176.  
  2177.   if (strip_symbols == STRIP_ALL)
  2178.     obfd->has_armap = FALSE;
  2179.   else
  2180.     obfd->has_armap = ibfd->has_armap;
  2181.   obfd->is_thin_archive = ibfd->is_thin_archive;
  2182.  
  2183.   if (deterministic)
  2184.     obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
  2185.  
  2186.   list = NULL;
  2187.  
  2188.   this_element = bfd_openr_next_archived_file (ibfd, NULL);
  2189.  
  2190.   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  2191.     {
  2192.       status = 1;
  2193.       bfd_nonfatal_message (NULL, obfd, NULL, NULL);
  2194.       return;
  2195.     }
  2196.  
  2197.   while (!status && this_element != NULL)
  2198.     {
  2199.       char *output_name;
  2200.       bfd *output_bfd;
  2201.       bfd *last_element;
  2202.       struct stat buf;
  2203.       int stat_status = 0;
  2204.       bfd_boolean del = TRUE;
  2205.       bfd_boolean ok_object;
  2206.  
  2207.       /* Create an output file for this member.  */
  2208.       output_name = concat (dir, "/",
  2209.                             bfd_get_filename (this_element), (char *) 0);
  2210.  
  2211.       /* If the file already exists, make another temp dir.  */
  2212.       if (stat (output_name, &buf) >= 0)
  2213.         {
  2214.           output_name = make_tempdir (output_name);
  2215.           if (output_name == NULL)
  2216.             fatal (_("cannot create tempdir for archive copying (error: %s)"),
  2217.                    strerror (errno));
  2218.  
  2219.           l = (struct name_list *) xmalloc (sizeof (struct name_list));
  2220.           l->name = output_name;
  2221.           l->next = list;
  2222.           l->obfd = NULL;
  2223.           list = l;
  2224.           output_name = concat (output_name, "/",
  2225.                                 bfd_get_filename (this_element), (char *) 0);
  2226.         }
  2227.  
  2228.       if (preserve_dates)
  2229.         {
  2230.           stat_status = bfd_stat_arch_elt (this_element, &buf);
  2231.  
  2232.           if (stat_status != 0)
  2233.             non_fatal (_("internal stat error on %s"),
  2234.                        bfd_get_filename (this_element));
  2235.         }
  2236.  
  2237.       l = (struct name_list *) xmalloc (sizeof (struct name_list));
  2238.       l->name = output_name;
  2239.       l->next = list;
  2240.       l->obfd = NULL;
  2241.       list = l;
  2242.  
  2243.       ok_object = bfd_check_format (this_element, bfd_object);
  2244.       if (!ok_object)
  2245.         bfd_nonfatal_message (NULL, this_element, NULL,
  2246.                               _("Unable to recognise the format of file"));
  2247.  
  2248.       /* PR binutils/3110: Cope with archives
  2249.          containing multiple target types.  */
  2250.       if (force_output_target || !ok_object)
  2251.         output_bfd = bfd_openw (output_name, output_target);
  2252.       else
  2253.         output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
  2254.  
  2255.       if (output_bfd == NULL)
  2256.         {
  2257.           bfd_nonfatal_message (output_name, NULL, NULL, NULL);
  2258.           status = 1;
  2259.           return;
  2260.         }
  2261.  
  2262.       if (ok_object)
  2263.         {
  2264.           del = !copy_object (this_element, output_bfd, input_arch);
  2265.  
  2266.           if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
  2267.             /* Try again as an unknown object file.  */
  2268.             ok_object = FALSE;
  2269.           else if (!bfd_close (output_bfd))
  2270.             {
  2271.               bfd_nonfatal_message (output_name, NULL, NULL, NULL);
  2272.               /* Error in new object file. Don't change archive.  */
  2273.               status = 1;
  2274.             }
  2275.         }
  2276.  
  2277.       if (!ok_object)
  2278.         {
  2279.           del = !copy_unknown_object (this_element, output_bfd);
  2280.           if (!bfd_close_all_done (output_bfd))
  2281.             {
  2282.               bfd_nonfatal_message (output_name, NULL, NULL, NULL);
  2283.               /* Error in new object file. Don't change archive.  */
  2284.               status = 1;
  2285.             }
  2286.         }
  2287.  
  2288.       if (del)
  2289.         {
  2290.           unlink (output_name);
  2291.           status = 1;
  2292.         }
  2293.       else
  2294.         {
  2295.           if (preserve_dates && stat_status == 0)
  2296.             set_times (output_name, &buf);
  2297.  
  2298.           /* Open the newly output file and attach to our list.  */
  2299.           output_bfd = bfd_openr (output_name, output_target);
  2300.  
  2301.           l->obfd = output_bfd;
  2302.  
  2303.           *ptr = output_bfd;
  2304.           ptr = &output_bfd->archive_next;
  2305.  
  2306.           last_element = this_element;
  2307.  
  2308.           this_element = bfd_openr_next_archived_file (ibfd, last_element);
  2309.  
  2310.           bfd_close (last_element);
  2311.         }
  2312.     }
  2313.   *ptr = NULL;
  2314.  
  2315.   filename = bfd_get_filename (obfd);
  2316.   if (!bfd_close (obfd))
  2317.     {
  2318.       status = 1;
  2319.       bfd_nonfatal_message (filename, NULL, NULL, NULL);
  2320.       return;
  2321.     }
  2322.  
  2323.   filename = bfd_get_filename (ibfd);
  2324.   if (!bfd_close (ibfd))
  2325.     {
  2326.       status = 1;
  2327.       bfd_nonfatal_message (filename, NULL, NULL, NULL);
  2328.       return;
  2329.     }
  2330.  
  2331.   /* Delete all the files that we opened.  */
  2332. #if 0
  2333.   for (l = list; l != NULL; l = l->next)
  2334.     {
  2335.       if (l->obfd == NULL)
  2336.         rmdir (l->name);
  2337.       else
  2338.         {
  2339.           bfd_close (l->obfd);
  2340.           unlink (l->name);
  2341.         }
  2342.     }
  2343.   rmdir (dir);
  2344. #endif
  2345.  
  2346. }
  2347.  
  2348. static void
  2349. set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
  2350. {
  2351.   /* This is only relevant to Coff targets.  */
  2352.   if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
  2353.     {
  2354.       if (style == KEEP
  2355.           && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
  2356.         style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
  2357.       bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
  2358.     }
  2359. }
  2360.  
  2361. /* The top-level control.  */
  2362.  
  2363. static void
  2364. copy_file (const char *input_filename, const char *output_filename,
  2365.            const char *input_target,   const char *output_target,
  2366.            const bfd_arch_info_type *input_arch)
  2367. {
  2368.   bfd *ibfd;
  2369.   char **obj_matching;
  2370.   char **core_matching;
  2371.   off_t size = get_file_size (input_filename);
  2372.  
  2373.   if (size < 1)
  2374.     {
  2375.       if (size == 0)
  2376.         non_fatal (_("error: the input file '%s' is empty"),
  2377.                    input_filename);
  2378.       status = 1;
  2379.       return;
  2380.     }
  2381.  
  2382.   /* To allow us to do "strip *" without dying on the first
  2383.      non-object file, failures are nonfatal.  */
  2384.   ibfd = bfd_openr (input_filename, input_target);
  2385.   if (ibfd == NULL)
  2386.     {
  2387.       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
  2388.       status = 1;
  2389.       return;
  2390.     }
  2391.  
  2392.   switch (do_debug_sections)
  2393.     {
  2394.     case compress:
  2395.       ibfd->flags |= BFD_COMPRESS;
  2396.       break;
  2397.     case decompress:
  2398.       ibfd->flags |= BFD_DECOMPRESS;
  2399.       break;
  2400.     default:
  2401.       break;
  2402.     }
  2403.  
  2404.   if (bfd_check_format (ibfd, bfd_archive))
  2405.     {
  2406.       bfd_boolean force_output_target;
  2407.       bfd *obfd;
  2408.  
  2409.       /* bfd_get_target does not return the correct value until
  2410.          bfd_check_format succeeds.  */
  2411.       if (output_target == NULL)
  2412.         {
  2413.           output_target = bfd_get_target (ibfd);
  2414.           force_output_target = FALSE;
  2415.         }
  2416.       else
  2417.         force_output_target = TRUE;
  2418.  
  2419.       obfd = bfd_openw (output_filename, output_target);
  2420.       if (obfd == NULL)
  2421.         {
  2422.           bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
  2423.           status = 1;
  2424.           return;
  2425.         }
  2426.       /* This is a no-op on non-Coff targets.  */
  2427.       set_long_section_mode (obfd, ibfd, long_section_names);
  2428.  
  2429.       copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
  2430.     }
  2431.   else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
  2432.     {
  2433.       bfd *obfd;
  2434.     do_copy:
  2435.  
  2436.       /* bfd_get_target does not return the correct value until
  2437.          bfd_check_format succeeds.  */
  2438.       if (output_target == NULL)
  2439.         output_target = bfd_get_target (ibfd);
  2440.  
  2441.       obfd = bfd_openw (output_filename, output_target);
  2442.       if (obfd == NULL)
  2443.         {
  2444.           bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
  2445.           status = 1;
  2446.           return;
  2447.         }
  2448.       /* This is a no-op on non-Coff targets.  */
  2449.       set_long_section_mode (obfd, ibfd, long_section_names);
  2450.  
  2451.       if (! copy_object (ibfd, obfd, input_arch))
  2452.         status = 1;
  2453.  
  2454.       if (!bfd_close (obfd))
  2455.         {
  2456.           status = 1;
  2457.           bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
  2458.           return;
  2459.         }
  2460.  
  2461.       if (!bfd_close (ibfd))
  2462.         {
  2463.           status = 1;
  2464.           bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
  2465.           return;
  2466.         }
  2467.     }
  2468.   else
  2469.     {
  2470.       bfd_error_type obj_error = bfd_get_error ();
  2471.       bfd_error_type core_error;
  2472.  
  2473.       if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
  2474.         {
  2475.           /* This probably can't happen..  */
  2476.           if (obj_error == bfd_error_file_ambiguously_recognized)
  2477.             free (obj_matching);
  2478.           goto do_copy;
  2479.         }
  2480.  
  2481.       core_error = bfd_get_error ();
  2482.       /* Report the object error in preference to the core error.  */
  2483.       if (obj_error != core_error)
  2484.         bfd_set_error (obj_error);
  2485.  
  2486.       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
  2487.  
  2488.       if (obj_error == bfd_error_file_ambiguously_recognized)
  2489.         {
  2490.           list_matching_formats (obj_matching);
  2491.           free (obj_matching);
  2492.         }
  2493.       if (core_error == bfd_error_file_ambiguously_recognized)
  2494.         {
  2495.           list_matching_formats (core_matching);
  2496.           free (core_matching);
  2497.         }
  2498.  
  2499.       status = 1;
  2500.     }
  2501. }
  2502.  
  2503. /* Add a name to the section renaming list.  */
  2504.  
  2505. static void
  2506. add_section_rename (const char * old_name, const char * new_name,
  2507.                     flagword flags)
  2508. {
  2509.   section_rename * srename;
  2510.  
  2511.   /* Check for conflicts first.  */
  2512.   for (srename = section_rename_list; srename != NULL; srename = srename->next)
  2513.     if (strcmp (srename->old_name, old_name) == 0)
  2514.       {
  2515.         /* Silently ignore duplicate definitions.  */
  2516.         if (strcmp (srename->new_name, new_name) == 0
  2517.             && srename->flags == flags)
  2518.           return;
  2519.  
  2520.         fatal (_("Multiple renames of section %s"), old_name);
  2521.       }
  2522.  
  2523.   srename = (section_rename *) xmalloc (sizeof (* srename));
  2524.  
  2525.   srename->old_name = old_name;
  2526.   srename->new_name = new_name;
  2527.   srename->flags    = flags;
  2528.   srename->next     = section_rename_list;
  2529.  
  2530.   section_rename_list = srename;
  2531. }
  2532.  
  2533. /* Check the section rename list for a new name of the input section
  2534.    ISECTION.  Return the new name if one is found.
  2535.    Also set RETURNED_FLAGS to the flags to be used for this section.  */
  2536.  
  2537. static const char *
  2538. find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
  2539.                      flagword * returned_flags)
  2540. {
  2541.   const char * old_name = bfd_section_name (ibfd, isection);
  2542.   section_rename * srename;
  2543.  
  2544.   /* Default to using the flags of the input section.  */
  2545.   * returned_flags = bfd_get_section_flags (ibfd, isection);
  2546.  
  2547.   for (srename = section_rename_list; srename != NULL; srename = srename->next)
  2548.     if (strcmp (srename->old_name, old_name) == 0)
  2549.       {
  2550.         if (srename->flags != (flagword) -1)
  2551.           * returned_flags = srename->flags;
  2552.  
  2553.         return srename->new_name;
  2554.       }
  2555.  
  2556.   return old_name;
  2557. }
  2558.  
  2559. /* Once each of the sections is copied, we may still need to do some
  2560.    finalization work for private section headers.  Do that here.  */
  2561.  
  2562. static void
  2563. setup_bfd_headers (bfd *ibfd, bfd *obfd)
  2564. {
  2565.   /* Allow the BFD backend to copy any private data it understands
  2566.      from the input section to the output section.  */
  2567.   if (! bfd_copy_private_header_data (ibfd, obfd))
  2568.     {
  2569.       status = 1;
  2570.       bfd_nonfatal_message (NULL, ibfd, NULL,
  2571.                             _("error in private header data"));
  2572.       return;
  2573.     }
  2574.  
  2575.   /* All went well.  */
  2576.   return;
  2577. }
  2578.  
  2579. /* Create a section in OBFD with the same
  2580.    name and attributes as ISECTION in IBFD.  */
  2581.  
  2582. static void
  2583. setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
  2584. {
  2585.   bfd *obfd = (bfd *) obfdarg;
  2586.   struct section_list *p;
  2587.   sec_ptr osection;
  2588.   bfd_size_type size;
  2589.   bfd_vma vma;
  2590.   bfd_vma lma;
  2591.   flagword flags;
  2592.   const char *err;
  2593.   const char * name;
  2594.   char *prefix = NULL;
  2595.   bfd_boolean make_nobits;
  2596.  
  2597.   if (is_strip_section (ibfd, isection))
  2598.     return;
  2599.  
  2600.   /* Get the, possibly new, name of the output section.  */
  2601.   name = find_section_rename (ibfd, isection, & flags);
  2602.  
  2603.   /* Prefix sections.  */
  2604.   if ((prefix_alloc_sections_string)
  2605.       && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
  2606.     prefix = prefix_alloc_sections_string;
  2607.   else if (prefix_sections_string)
  2608.     prefix = prefix_sections_string;
  2609.  
  2610.   if (prefix)
  2611.     {
  2612.       char *n;
  2613.  
  2614.       n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
  2615.       strcpy (n, prefix);
  2616.       strcat (n, name);
  2617.       name = n;
  2618.     }
  2619.  
  2620.   make_nobits = FALSE;
  2621.  
  2622.   p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
  2623.                          SECTION_CONTEXT_SET_FLAGS);
  2624.   if (p != NULL)
  2625.     flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
  2626.   else if (strip_symbols == STRIP_NONDEBUG
  2627.            && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
  2628.            && !(ibfd->xvec->flavour == bfd_target_elf_flavour
  2629.                 && elf_section_type (isection) == SHT_NOTE))
  2630.     {
  2631.       flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
  2632.       if (obfd->xvec->flavour == bfd_target_elf_flavour)
  2633.         {
  2634.           make_nobits = TRUE;
  2635.  
  2636.           /* Twiddle the input section flags so that it seems to
  2637.              elf.c:copy_private_bfd_data that section flags have not
  2638.              changed between input and output sections.  This hack
  2639.              prevents wholesale rewriting of the program headers.  */
  2640.           isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
  2641.         }
  2642.     }
  2643.  
  2644.   osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
  2645.  
  2646.   if (osection == NULL)
  2647.     {
  2648.       err = _("failed to create output section");
  2649.       goto loser;
  2650.     }
  2651.  
  2652.   if (make_nobits)
  2653.     elf_section_type (osection) = SHT_NOBITS;
  2654.  
  2655.   size = bfd_section_size (ibfd, isection);
  2656.   if (copy_byte >= 0)
  2657.     size = (size + interleave - 1) / interleave * copy_width;
  2658.   else if (extract_symbol)
  2659.     size = 0;
  2660.   if (! bfd_set_section_size (obfd, osection, size))
  2661.     {
  2662.       err = _("failed to set size");
  2663.       goto loser;
  2664.     }
  2665.  
  2666.   vma = bfd_section_vma (ibfd, isection);
  2667.   p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
  2668.                          SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
  2669.   if (p != NULL)
  2670.     {
  2671.       if (p->context & SECTION_CONTEXT_SET_VMA)
  2672.         vma = p->vma_val;
  2673.       else
  2674.         vma += p->vma_val;
  2675.     }
  2676.   else
  2677.     vma += change_section_address;
  2678.  
  2679.   if (! bfd_set_section_vma (obfd, osection, vma))
  2680.     {
  2681.       err = _("failed to set vma");
  2682.       goto loser;
  2683.     }
  2684.  
  2685.   lma = isection->lma;
  2686.   p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
  2687.                          SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
  2688.   if (p != NULL)
  2689.     {
  2690.       if (p->context & SECTION_CONTEXT_ALTER_LMA)
  2691.         lma += p->lma_val;
  2692.       else
  2693.         lma = p->lma_val;
  2694.     }
  2695.   else
  2696.     lma += change_section_address;
  2697.  
  2698.   osection->lma = lma;
  2699.  
  2700.   /* FIXME: This is probably not enough.  If we change the LMA we
  2701.      may have to recompute the header for the file as well.  */
  2702.   if (!bfd_set_section_alignment (obfd,
  2703.                                   osection,
  2704.                                   bfd_section_alignment (ibfd, isection)))
  2705.     {
  2706.       err = _("failed to set alignment");
  2707.       goto loser;
  2708.     }
  2709.  
  2710.   /* Copy merge entity size.  */
  2711.   osection->entsize = isection->entsize;
  2712.  
  2713.   /* This used to be mangle_section; we do here to avoid using
  2714.      bfd_get_section_by_name since some formats allow multiple
  2715.      sections with the same name.  */
  2716.   isection->output_section = osection;
  2717.   isection->output_offset = 0;
  2718.  
  2719.   /* Do not copy backend data if --extract-symbol is passed; anything
  2720.      that needs to look at the section contents will fail.  */
  2721.   if (extract_symbol)
  2722.     return;
  2723.  
  2724.   if ((isection->flags & SEC_GROUP) != 0)
  2725.     {
  2726.       asymbol *gsym = group_signature (isection);
  2727.  
  2728.       if (gsym != NULL)
  2729.         {
  2730.           gsym->flags |= BSF_KEEP;
  2731.           if (ibfd->xvec->flavour == bfd_target_elf_flavour)
  2732.             elf_group_id (isection) = gsym;
  2733.         }
  2734.     }
  2735.  
  2736.   /* Allow the BFD backend to copy any private data it understands
  2737.      from the input section to the output section.  */
  2738.   if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
  2739.     {
  2740.       err = _("failed to copy private data");
  2741.       goto loser;
  2742.     }
  2743.  
  2744.   /* All went well.  */
  2745.   return;
  2746.  
  2747. loser:
  2748.   status = 1;
  2749.   bfd_nonfatal_message (NULL, obfd, osection, err);
  2750. }
  2751.  
  2752. /* Return TRUE if input section ISECTION should be skipped.  */
  2753.  
  2754. static bfd_boolean
  2755. skip_section (bfd *ibfd, sec_ptr isection)
  2756. {
  2757.   sec_ptr osection;
  2758.   bfd_size_type size;
  2759.   flagword flags;
  2760.  
  2761.   /* If we have already failed earlier on,
  2762.      do not keep on generating complaints now.  */
  2763.   if (status != 0)
  2764.     return TRUE;
  2765.  
  2766.   if (extract_symbol)
  2767.     return TRUE;
  2768.  
  2769.   if (is_strip_section (ibfd, isection))
  2770.     return TRUE;
  2771.  
  2772.   flags = bfd_get_section_flags (ibfd, isection);
  2773.   if ((flags & SEC_GROUP) != 0)
  2774.     return TRUE;
  2775.  
  2776.   osection = isection->output_section;
  2777.   size = bfd_get_section_size (isection);
  2778.  
  2779.   if (size == 0 || osection == 0)
  2780.     return TRUE;
  2781.  
  2782.   return FALSE;
  2783. }
  2784.  
  2785. /* Copy relocations in input section ISECTION of IBFD to an output
  2786.    section with the same name in OBFDARG.  If stripping then don't
  2787.    copy any relocation info.  */
  2788.  
  2789. static void
  2790. copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
  2791. {
  2792.   bfd *obfd = (bfd *) obfdarg;
  2793.   long relsize;
  2794.   arelent **relpp;
  2795.   long relcount;
  2796.   sec_ptr osection;
  2797.  
  2798.   if (skip_section (ibfd, isection))
  2799.     return;
  2800.  
  2801.   osection = isection->output_section;
  2802.  
  2803.   /* Core files and DWO files do not need to be relocated.  */
  2804.   if (bfd_get_format (obfd) == bfd_core || strip_symbols == STRIP_NONDWO)
  2805.     relsize = 0;
  2806.   else
  2807.     {
  2808.       relsize = bfd_get_reloc_upper_bound (ibfd, isection);
  2809.  
  2810.       if (relsize < 0)
  2811.         {
  2812.           /* Do not complain if the target does not support relocations.  */
  2813.           if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
  2814.             relsize = 0;
  2815.           else
  2816.             {
  2817.               status = 1;
  2818.               bfd_nonfatal_message (NULL, ibfd, isection, NULL);
  2819.               return;
  2820.             }
  2821.         }
  2822.     }
  2823.  
  2824.   if (relsize == 0)
  2825.     {
  2826.       bfd_set_reloc (obfd, osection, NULL, 0);
  2827.       osection->flags &= ~SEC_RELOC;
  2828.     }
  2829.   else
  2830.     {
  2831.       relpp = (arelent **) xmalloc (relsize);
  2832.       relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
  2833.       if (relcount < 0)
  2834.         {
  2835.           status = 1;
  2836.           bfd_nonfatal_message (NULL, ibfd, isection,
  2837.                                 _("relocation count is negative"));
  2838.           return;
  2839.         }
  2840.  
  2841.       if (strip_symbols == STRIP_ALL)
  2842.         {
  2843.           /* Remove relocations which are not in
  2844.              keep_strip_specific_list.  */
  2845.           arelent **temp_relpp;
  2846.           long temp_relcount = 0;
  2847.           long i;
  2848.  
  2849.           temp_relpp = (arelent **) xmalloc (relsize);
  2850.           for (i = 0; i < relcount; i++)
  2851.             if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
  2852.                                      keep_specific_htab))
  2853.               temp_relpp [temp_relcount++] = relpp [i];
  2854.           relcount = temp_relcount;
  2855.           free (relpp);
  2856.           relpp = temp_relpp;
  2857.         }
  2858.  
  2859.       bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
  2860.       if (relcount == 0)
  2861.         {
  2862.           osection->flags &= ~SEC_RELOC;
  2863.           free (relpp);
  2864.         }
  2865.     }
  2866. }
  2867.  
  2868. /* Copy the data of input section ISECTION of IBFD
  2869.    to an output section with the same name in OBFD.  */
  2870.  
  2871. static void
  2872. copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
  2873. {
  2874.   bfd *obfd = (bfd *) obfdarg;
  2875.   struct section_list *p;
  2876.   sec_ptr osection;
  2877.   bfd_size_type size;
  2878.  
  2879.   if (skip_section (ibfd, isection))
  2880.     return;
  2881.  
  2882.   osection = isection->output_section;
  2883.   size = bfd_get_section_size (isection);
  2884.  
  2885.   if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
  2886.       && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
  2887.     {
  2888.       bfd_byte *memhunk = NULL;
  2889.  
  2890.       if (!bfd_get_full_section_contents (ibfd, isection, &memhunk))
  2891.         {
  2892.           status = 1;
  2893.           bfd_nonfatal_message (NULL, ibfd, isection, NULL);
  2894.           return;
  2895.         }
  2896.  
  2897.       if (reverse_bytes)
  2898.         {
  2899.           /* We don't handle leftover bytes (too many possible behaviors,
  2900.              and we don't know what the user wants).  The section length
  2901.              must be a multiple of the number of bytes to swap.  */
  2902.           if ((size % reverse_bytes) == 0)
  2903.             {
  2904.               unsigned long i, j;
  2905.               bfd_byte b;
  2906.  
  2907.               for (i = 0; i < size; i += reverse_bytes)
  2908.                 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
  2909.                   {
  2910.                     bfd_byte *m = (bfd_byte *) memhunk;
  2911.  
  2912.                     b = m[i + j];
  2913.                     m[i + j] = m[(i + reverse_bytes) - (j + 1)];
  2914.                     m[(i + reverse_bytes) - (j + 1)] = b;
  2915.                   }
  2916.             }
  2917.           else
  2918.             /* User must pad the section up in order to do this.  */
  2919.             fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
  2920.                    bfd_section_name (ibfd, isection), reverse_bytes);
  2921.         }
  2922.  
  2923.       if (copy_byte >= 0)
  2924.         {
  2925.           /* Keep only every `copy_byte'th byte in MEMHUNK.  */
  2926.           char *from = (char *) memhunk + copy_byte;
  2927.           char *to = (char *) memhunk;
  2928.           char *end = (char *) memhunk + size;
  2929.           int i;
  2930.  
  2931.           for (; from < end; from += interleave)
  2932.             for (i = 0; i < copy_width; i++)
  2933.               {
  2934.                 if (&from[i] >= end)
  2935.                   break;
  2936.                 *to++ = from[i];
  2937.               }
  2938.  
  2939.           size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
  2940.           osection->lma /= interleave;
  2941.         }
  2942.  
  2943.       if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
  2944.         {
  2945.           status = 1;
  2946.           bfd_nonfatal_message (NULL, obfd, osection, NULL);
  2947.           return;
  2948.         }
  2949.       free (memhunk);
  2950.     }
  2951.   else if ((p = find_section_list (bfd_get_section_name (ibfd, isection),
  2952.                                    FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL
  2953.            && (p->flags & SEC_HAS_CONTENTS) != 0)
  2954.     {
  2955.       void *memhunk = xmalloc (size);
  2956.  
  2957.       /* We don't permit the user to turn off the SEC_HAS_CONTENTS
  2958.          flag--they can just remove the section entirely and add it
  2959.          back again.  However, we do permit them to turn on the
  2960.          SEC_HAS_CONTENTS flag, and take it to mean that the section
  2961.          contents should be zeroed out.  */
  2962.  
  2963.       memset (memhunk, 0, size);
  2964.       if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
  2965.         {
  2966.           status = 1;
  2967.           bfd_nonfatal_message (NULL, obfd, osection, NULL);
  2968.           return;
  2969.         }
  2970.       free (memhunk);
  2971.     }
  2972. }
  2973.  
  2974. /* Get all the sections.  This is used when --gap-fill or --pad-to is
  2975.    used.  */
  2976.  
  2977. static void
  2978. get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
  2979. {
  2980.   asection ***secppp = (asection ***) secppparg;
  2981.  
  2982.   **secppp = osection;
  2983.   ++(*secppp);
  2984. }
  2985.  
  2986. /* Sort sections by VMA.  This is called via qsort, and is used when
  2987.    --gap-fill or --pad-to is used.  We force non loadable or empty
  2988.    sections to the front, where they are easier to ignore.  */
  2989.  
  2990. static int
  2991. compare_section_lma (const void *arg1, const void *arg2)
  2992. {
  2993.   const asection *const *sec1 = (const asection * const *) arg1;
  2994.   const asection *const *sec2 = (const asection * const *) arg2;
  2995.   flagword flags1, flags2;
  2996.  
  2997.   /* Sort non loadable sections to the front.  */
  2998.   flags1 = (*sec1)->flags;
  2999.   flags2 = (*sec2)->flags;
  3000.   if ((flags1 & SEC_HAS_CONTENTS) == 0
  3001.       || (flags1 & SEC_LOAD) == 0)
  3002.     {
  3003.       if ((flags2 & SEC_HAS_CONTENTS) != 0
  3004.           && (flags2 & SEC_LOAD) != 0)
  3005.         return -1;
  3006.     }
  3007.   else
  3008.     {
  3009.       if ((flags2 & SEC_HAS_CONTENTS) == 0
  3010.           || (flags2 & SEC_LOAD) == 0)
  3011.         return 1;
  3012.     }
  3013.  
  3014.   /* Sort sections by LMA.  */
  3015.   if ((*sec1)->lma > (*sec2)->lma)
  3016.     return 1;
  3017.   else if ((*sec1)->lma < (*sec2)->lma)
  3018.     return -1;
  3019.  
  3020.   /* Sort sections with the same LMA by size.  */
  3021.   if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
  3022.     return 1;
  3023.   else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
  3024.     return -1;
  3025.  
  3026.   return 0;
  3027. }
  3028.  
  3029. /* Mark all the symbols which will be used in output relocations with
  3030.    the BSF_KEEP flag so that those symbols will not be stripped.
  3031.  
  3032.    Ignore relocations which will not appear in the output file.  */
  3033.  
  3034. static void
  3035. mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
  3036. {
  3037.   asymbol **symbols = (asymbol **) symbolsarg;
  3038.   long relsize;
  3039.   arelent **relpp;
  3040.   long relcount, i;
  3041.  
  3042.   /* Ignore an input section with no corresponding output section.  */
  3043.   if (isection->output_section == NULL)
  3044.     return;
  3045.  
  3046.   relsize = bfd_get_reloc_upper_bound (ibfd, isection);
  3047.   if (relsize < 0)
  3048.     {
  3049.       /* Do not complain if the target does not support relocations.  */
  3050.       if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
  3051.         return;
  3052.       bfd_fatal (bfd_get_filename (ibfd));
  3053.     }
  3054.  
  3055.   if (relsize == 0)
  3056.     return;
  3057.  
  3058.   relpp = (arelent **) xmalloc (relsize);
  3059.   relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
  3060.   if (relcount < 0)
  3061.     bfd_fatal (bfd_get_filename (ibfd));
  3062.  
  3063.   /* Examine each symbol used in a relocation.  If it's not one of the
  3064.      special bfd section symbols, then mark it with BSF_KEEP.  */
  3065.   for (i = 0; i < relcount; i++)
  3066.     {
  3067.       if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
  3068.           && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
  3069.           && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
  3070.         (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
  3071.     }
  3072.  
  3073.   if (relpp != NULL)
  3074.     free (relpp);
  3075. }
  3076.  
  3077. /* Write out debugging information.  */
  3078.  
  3079. static bfd_boolean
  3080. write_debugging_info (bfd *obfd, void *dhandle,
  3081.                       long *symcountp ATTRIBUTE_UNUSED,
  3082.                       asymbol ***symppp ATTRIBUTE_UNUSED)
  3083. {
  3084.   if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
  3085.     return write_ieee_debugging_info (obfd, dhandle);
  3086.  
  3087.   if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
  3088.       || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
  3089.     {
  3090.       bfd_byte *syms, *strings;
  3091.       bfd_size_type symsize, stringsize;
  3092.       asection *stabsec, *stabstrsec;
  3093.       flagword flags;
  3094.  
  3095.       if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
  3096.                                                     &symsize, &strings,
  3097.                                                     &stringsize))
  3098.         return FALSE;
  3099.  
  3100.       flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
  3101.       stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
  3102.       stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
  3103.       if (stabsec == NULL
  3104.           || stabstrsec == NULL
  3105.           || ! bfd_set_section_size (obfd, stabsec, symsize)
  3106.           || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
  3107.           || ! bfd_set_section_alignment (obfd, stabsec, 2)
  3108.           || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
  3109.         {
  3110.           bfd_nonfatal_message (NULL, obfd, NULL,
  3111.                                 _("can't create debugging section"));
  3112.           return FALSE;
  3113.         }
  3114.  
  3115.       /* We can get away with setting the section contents now because
  3116.          the next thing the caller is going to do is copy over the
  3117.          real sections.  We may someday have to split the contents
  3118.          setting out of this function.  */
  3119.       if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
  3120.           || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
  3121.                                          stringsize))
  3122.         {
  3123.           bfd_nonfatal_message (NULL, obfd, NULL,
  3124.                                 _("can't set debugging section contents"));
  3125.           return FALSE;
  3126.         }
  3127.  
  3128.       return TRUE;
  3129.     }
  3130.  
  3131.   bfd_nonfatal_message (NULL, obfd, NULL,
  3132.                         _("don't know how to write debugging information for %s"),
  3133.              bfd_get_target (obfd));
  3134.   return FALSE;
  3135. }
  3136.  
  3137. /* If neither -D nor -U was specified explicitly,
  3138.    then use the configured default.  */
  3139. static void
  3140. default_deterministic (void)
  3141. {
  3142.   if (deterministic < 0)
  3143.     deterministic = DEFAULT_AR_DETERMINISTIC;
  3144. }
  3145.  
  3146. static int
  3147. strip_main (int argc, char *argv[])
  3148. {
  3149.   char *input_target = NULL;
  3150.   char *output_target = NULL;
  3151.   bfd_boolean show_version = FALSE;
  3152.   bfd_boolean formats_info = FALSE;
  3153.   int c;
  3154.   int i;
  3155.   char *output_file = NULL;
  3156.  
  3157.   while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
  3158.                            strip_options, (int *) 0)) != EOF)
  3159.     {
  3160.       switch (c)
  3161.         {
  3162.         case 'I':
  3163.           input_target = optarg;
  3164.           break;
  3165.         case 'O':
  3166.           output_target = optarg;
  3167.           break;
  3168.         case 'F':
  3169.           input_target = output_target = optarg;
  3170.           break;
  3171.         case 'R':
  3172.           find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE);
  3173.           sections_removed = TRUE;
  3174.           break;
  3175.         case 's':
  3176.           strip_symbols = STRIP_ALL;
  3177.           break;
  3178.         case 'S':
  3179.         case 'g':
  3180.         case 'd':       /* Historic BSD alias for -g.  Used by early NetBSD.  */
  3181.           strip_symbols = STRIP_DEBUG;
  3182.           break;
  3183.         case OPTION_STRIP_DWO:
  3184.           strip_symbols = STRIP_DWO;
  3185.           break;
  3186.         case OPTION_STRIP_UNNEEDED:
  3187.           strip_symbols = STRIP_UNNEEDED;
  3188.           break;
  3189.         case 'K':
  3190.           add_specific_symbol (optarg, keep_specific_htab);
  3191.           break;
  3192.         case 'N':
  3193.           add_specific_symbol (optarg, strip_specific_htab);
  3194.           break;
  3195.         case 'o':
  3196.           output_file = optarg;
  3197.           break;
  3198.         case 'p':
  3199.           preserve_dates = TRUE;
  3200.           break;
  3201.         case 'D':
  3202.           deterministic = TRUE;
  3203.           break;
  3204.         case 'U':
  3205.           deterministic = FALSE;
  3206.           break;
  3207.         case 'x':
  3208.           discard_locals = LOCALS_ALL;
  3209.           break;
  3210.         case 'X':
  3211.           discard_locals = LOCALS_START_L;
  3212.           break;
  3213.         case 'v':
  3214.           verbose = TRUE;
  3215.           break;
  3216.         case 'V':
  3217.           show_version = TRUE;
  3218.           break;
  3219.         case OPTION_FORMATS_INFO:
  3220.           formats_info = TRUE;
  3221.           break;
  3222.         case OPTION_ONLY_KEEP_DEBUG:
  3223.           strip_symbols = STRIP_NONDEBUG;
  3224.           break;
  3225.         case OPTION_KEEP_FILE_SYMBOLS:
  3226.           keep_file_symbols = 1;
  3227.           break;
  3228.         case 0:
  3229.           /* We've been given a long option.  */
  3230.           break;
  3231.         case 'w':
  3232.           wildcard = TRUE;
  3233.           break;
  3234.         case 'H':
  3235.         case 'h':
  3236.           strip_usage (stdout, 0);
  3237.         default:
  3238.           strip_usage (stderr, 1);
  3239.         }
  3240.     }
  3241.  
  3242.   if (formats_info)
  3243.     {
  3244.       display_info ();
  3245.       return 0;
  3246.     }
  3247.  
  3248.   if (show_version)
  3249.     print_version ("strip");
  3250.  
  3251.   default_deterministic ();
  3252.  
  3253.   /* Default is to strip all symbols.  */
  3254.   if (strip_symbols == STRIP_UNDEF
  3255.       && discard_locals == LOCALS_UNDEF
  3256.       && htab_elements (strip_specific_htab) == 0)
  3257.     strip_symbols = STRIP_ALL;
  3258.  
  3259.   if (output_target == NULL)
  3260.     output_target = input_target;
  3261.  
  3262.   i = optind;
  3263.   if (i == argc
  3264.       || (output_file != NULL && (i + 1) < argc))
  3265.     strip_usage (stderr, 1);
  3266.  
  3267.   for (; i < argc; i++)
  3268.     {
  3269.       int hold_status = status;
  3270.       struct stat statbuf;
  3271.       char *tmpname;
  3272.  
  3273.       if (get_file_size (argv[i]) < 1)
  3274.         {
  3275.           status = 1;
  3276.           continue;
  3277.         }
  3278.  
  3279.       if (preserve_dates)
  3280.         /* No need to check the return value of stat().
  3281.            It has already been checked in get_file_size().  */
  3282.         stat (argv[i], &statbuf);
  3283.  
  3284.       if (output_file == NULL
  3285.           || filename_cmp (argv[i], output_file) == 0)
  3286.         tmpname = make_tempname (argv[i]);
  3287.       else
  3288.         tmpname = output_file;
  3289.  
  3290.       if (tmpname == NULL)
  3291.         {
  3292.           bfd_nonfatal_message (argv[i], NULL, NULL,
  3293.                                 _("could not create temporary file to hold stripped copy"));
  3294.           status = 1;
  3295.           continue;
  3296.         }
  3297.  
  3298.       status = 0;
  3299.       copy_file (argv[i], tmpname, input_target, output_target, NULL);
  3300.       if (status == 0)
  3301.         {
  3302.           if (preserve_dates)
  3303.             set_times (tmpname, &statbuf);
  3304.           if (output_file != tmpname)
  3305.             status = (smart_rename (tmpname,
  3306.                                     output_file ? output_file : argv[i],
  3307.                                     preserve_dates) != 0);
  3308.           if (status == 0)
  3309.             status = hold_status;
  3310.         }
  3311.       else
  3312.         unlink_if_ordinary (tmpname);
  3313.       if (output_file != tmpname)
  3314.         free (tmpname);
  3315.     }
  3316.  
  3317.   return status;
  3318. }
  3319.  
  3320. /* Set up PE subsystem.  */
  3321.  
  3322. static void
  3323. set_pe_subsystem (const char *s)
  3324. {
  3325.   const char *version, *subsystem;
  3326.   size_t i;
  3327.   static const struct
  3328.     {
  3329.       const char *name;
  3330.       const char set_def;
  3331.       const short value;
  3332.     }
  3333.   v[] =
  3334.     {
  3335.       { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
  3336.       { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
  3337.       { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
  3338.       { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
  3339.       { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
  3340.       { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
  3341.       { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
  3342.       { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
  3343.       { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
  3344.       { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
  3345.     };
  3346.   short value;
  3347.   char *copy;
  3348.   int set_def = -1;
  3349.  
  3350.   /* Check for the presence of a version number.  */
  3351.   version = strchr (s, ':');
  3352.   if (version == NULL)
  3353.     subsystem = s;
  3354.   else
  3355.     {
  3356.       int len = version - s;
  3357.       copy = xstrdup (s);
  3358.       subsystem = copy;
  3359.       copy[len] = '\0';
  3360.       version = copy + 1 + len;
  3361.       pe_major_subsystem_version = strtoul (version, &copy, 0);
  3362.       if (*copy == '.')
  3363.         pe_minor_subsystem_version = strtoul (copy + 1, &copy, 0);
  3364.       if (*copy != '\0')
  3365.         non_fatal (_("%s: bad version in PE subsystem"), s);
  3366.     }
  3367.  
  3368.   /* Check for numeric subsystem.  */
  3369.   value = (short) strtol (subsystem, &copy, 0);
  3370.   if (*copy == '\0')
  3371.     {
  3372.       for (i = 0; i < ARRAY_SIZE (v); i++)
  3373.         if (v[i].value == value)
  3374.           {
  3375.             pe_subsystem = value;
  3376.             set_def = v[i].set_def;
  3377.             break;
  3378.           }
  3379.     }
  3380.   else
  3381.     {
  3382.       /* Search for subsystem by name.  */
  3383.       for (i = 0; i < ARRAY_SIZE (v); i++)
  3384.         if (strcmp (subsystem, v[i].name) == 0)
  3385.           {
  3386.             pe_subsystem = v[i].value;
  3387.             set_def = v[i].set_def;
  3388.             break;
  3389.           }
  3390.     }
  3391.  
  3392.   switch (set_def)
  3393.     {
  3394.     case -1:
  3395.       fatal (_("unknown PE subsystem: %s"), s);
  3396.       break;
  3397.     case 0:
  3398.       break;
  3399.     default:
  3400.       if (pe_file_alignment == (bfd_vma) -1)
  3401.         pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
  3402.       if (pe_section_alignment == (bfd_vma) -1)
  3403.         pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
  3404.       break;
  3405.     }
  3406.   if (s != subsystem)
  3407.     free ((char *) subsystem);
  3408. }
  3409.  
  3410. /* Convert EFI target to PEI target.  */
  3411.  
  3412. static void
  3413. convert_efi_target (char *efi)
  3414. {
  3415.   efi[0] = 'p';
  3416.   efi[1] = 'e';
  3417.   efi[2] = 'i';
  3418.  
  3419.   if (strcmp (efi + 4, "ia32") == 0)
  3420.     {
  3421.       /* Change ia32 to i386.  */
  3422.       efi[5]= '3';
  3423.       efi[6]= '8';
  3424.       efi[7]= '6';
  3425.     }
  3426.   else if (strcmp (efi + 4, "x86_64") == 0)
  3427.     {
  3428.       /* Change x86_64 to x86-64.  */
  3429.       efi[7] = '-';
  3430.     }
  3431. }
  3432.  
  3433. static int
  3434. copy_main (int argc, char *argv[])
  3435. {
  3436.   char *input_filename = NULL;
  3437.   char *output_filename = NULL;
  3438.   char *tmpname;
  3439.   char *input_target = NULL;
  3440.   char *output_target = NULL;
  3441.   bfd_boolean show_version = FALSE;
  3442.   bfd_boolean change_warn = TRUE;
  3443.   bfd_boolean formats_info = FALSE;
  3444.   int c;
  3445.   struct stat statbuf;
  3446.   const bfd_arch_info_type *input_arch = NULL;
  3447.  
  3448.   while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
  3449.                            copy_options, (int *) 0)) != EOF)
  3450.     {
  3451.       switch (c)
  3452.         {
  3453.         case 'b':
  3454.           copy_byte = atoi (optarg);
  3455.           if (copy_byte < 0)
  3456.             fatal (_("byte number must be non-negative"));
  3457.           break;
  3458.  
  3459.         case 'B':
  3460.           input_arch = bfd_scan_arch (optarg);
  3461.           if (input_arch == NULL)
  3462.             fatal (_("architecture %s unknown"), optarg);
  3463.           break;
  3464.  
  3465.         case 'i':
  3466.           if (optarg)
  3467.             {
  3468.               interleave = atoi (optarg);
  3469.               if (interleave < 1)
  3470.                 fatal (_("interleave must be positive"));
  3471.             }
  3472.           else
  3473.             interleave = 4;
  3474.           break;
  3475.  
  3476.         case OPTION_INTERLEAVE_WIDTH:
  3477.           copy_width = atoi (optarg);
  3478.           if (copy_width < 1)
  3479.             fatal(_("interleave width must be positive"));
  3480.           break;
  3481.  
  3482.         case 'I':
  3483.         case 's':               /* "source" - 'I' is preferred */
  3484.           input_target = optarg;
  3485.           break;
  3486.  
  3487.         case 'O':
  3488.         case 'd':               /* "destination" - 'O' is preferred */
  3489.           output_target = optarg;
  3490.           break;
  3491.  
  3492.         case 'F':
  3493.           input_target = output_target = optarg;
  3494.           break;
  3495.  
  3496.         case 'j':
  3497.           find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY);
  3498.           sections_copied = TRUE;
  3499.           break;
  3500.  
  3501.         case 'R':
  3502.           find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE);
  3503.           sections_removed = TRUE;
  3504.           break;
  3505.  
  3506.         case 'S':
  3507.           strip_symbols = STRIP_ALL;
  3508.           break;
  3509.  
  3510.         case 'g':
  3511.           strip_symbols = STRIP_DEBUG;
  3512.           break;
  3513.  
  3514.         case OPTION_STRIP_DWO:
  3515.           strip_symbols = STRIP_DWO;
  3516.           break;
  3517.  
  3518.         case OPTION_STRIP_UNNEEDED:
  3519.           strip_symbols = STRIP_UNNEEDED;
  3520.           break;
  3521.  
  3522.         case OPTION_ONLY_KEEP_DEBUG:
  3523.           strip_symbols = STRIP_NONDEBUG;
  3524.           break;
  3525.  
  3526.         case OPTION_KEEP_FILE_SYMBOLS:
  3527.           keep_file_symbols = 1;
  3528.           break;
  3529.  
  3530.         case OPTION_ADD_GNU_DEBUGLINK:
  3531.           long_section_names = ENABLE ;
  3532.           gnu_debuglink_filename = optarg;
  3533.           break;
  3534.  
  3535.         case 'K':
  3536.           add_specific_symbol (optarg, keep_specific_htab);
  3537.           break;
  3538.  
  3539.         case 'N':
  3540.           add_specific_symbol (optarg, strip_specific_htab);
  3541.           break;
  3542.  
  3543.         case OPTION_STRIP_UNNEEDED_SYMBOL:
  3544.           add_specific_symbol (optarg, strip_unneeded_htab);
  3545.           break;
  3546.  
  3547.         case 'L':
  3548.           add_specific_symbol (optarg, localize_specific_htab);
  3549.           break;
  3550.  
  3551.         case OPTION_GLOBALIZE_SYMBOL:
  3552.           add_specific_symbol (optarg, globalize_specific_htab);
  3553.           break;
  3554.  
  3555.         case 'G':
  3556.           add_specific_symbol (optarg, keepglobal_specific_htab);
  3557.           break;
  3558.  
  3559.         case 'W':
  3560.           add_specific_symbol (optarg, weaken_specific_htab);
  3561.           break;
  3562.  
  3563.         case 'p':
  3564.           preserve_dates = TRUE;
  3565.           break;
  3566.  
  3567.         case 'D':
  3568.           deterministic = TRUE;
  3569.           break;
  3570.  
  3571.         case 'U':
  3572.           deterministic = FALSE;
  3573.           break;
  3574.  
  3575.         case 'w':
  3576.           wildcard = TRUE;
  3577.           break;
  3578.  
  3579.         case 'x':
  3580.           discard_locals = LOCALS_ALL;
  3581.           break;
  3582.  
  3583.         case 'X':
  3584.           discard_locals = LOCALS_START_L;
  3585.           break;
  3586.  
  3587.         case 'v':
  3588.           verbose = TRUE;
  3589.           break;
  3590.  
  3591.         case 'V':
  3592.           show_version = TRUE;
  3593.           break;
  3594.  
  3595.         case OPTION_FORMATS_INFO:
  3596.           formats_info = TRUE;
  3597.           break;
  3598.  
  3599.         case OPTION_WEAKEN:
  3600.           weaken = TRUE;
  3601.           break;
  3602.  
  3603.         case OPTION_ADD_SECTION:
  3604.           {
  3605.             const char *s;
  3606.             size_t off, alloc;
  3607.             struct section_add *pa;
  3608.             FILE *f;
  3609.  
  3610.             s = strchr (optarg, '=');
  3611.  
  3612.             if (s == NULL)
  3613.               fatal (_("bad format for %s"), "--add-section");
  3614.  
  3615.             pa = (struct section_add *) xmalloc (sizeof (struct section_add));
  3616.             pa->name = xstrndup (optarg, s - optarg);
  3617.             pa->filename = s + 1;
  3618.  
  3619.             /* We don't use get_file_size so that we can do
  3620.                  --add-section .note.GNU_stack=/dev/null
  3621.                get_file_size doesn't work on /dev/null.  */
  3622.  
  3623.             f = fopen (pa->filename, FOPEN_RB);
  3624.             if (f == NULL)
  3625.               fatal (_("cannot open: %s: %s"),
  3626.                      pa->filename, strerror (errno));
  3627.  
  3628.             off = 0;
  3629.             alloc = 4096;
  3630.             pa->contents = (bfd_byte *) xmalloc (alloc);
  3631.             while (!feof (f))
  3632.               {
  3633.                 off_t got;
  3634.  
  3635.                 if (off == alloc)
  3636.                   {
  3637.                     alloc <<= 1;
  3638.                     pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
  3639.                   }
  3640.  
  3641.                 got = fread (pa->contents + off, 1, alloc - off, f);
  3642.                 if (ferror (f))
  3643.                   fatal (_("%s: fread failed"), pa->filename);
  3644.  
  3645.                 off += got;
  3646.               }
  3647.  
  3648.             pa->size = off;
  3649.  
  3650.             fclose (f);
  3651.  
  3652.             pa->next = add_sections;
  3653.             add_sections = pa;
  3654.           }
  3655.           break;
  3656.  
  3657.         case OPTION_CHANGE_START:
  3658.           change_start = parse_vma (optarg, "--change-start");
  3659.           break;
  3660.  
  3661.         case OPTION_CHANGE_SECTION_ADDRESS:
  3662.         case OPTION_CHANGE_SECTION_LMA:
  3663.         case OPTION_CHANGE_SECTION_VMA:
  3664.           {
  3665.             struct section_list * p;
  3666.             unsigned int context = 0;
  3667.             const char *s;
  3668.             int len;
  3669.             char *name;
  3670.             char *option = NULL;
  3671.             bfd_vma val;
  3672.  
  3673.             switch (c)
  3674.               {
  3675.               case OPTION_CHANGE_SECTION_ADDRESS:
  3676.                 option = "--change-section-address";
  3677.                 context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
  3678.                 break;
  3679.               case OPTION_CHANGE_SECTION_LMA:
  3680.                 option = "--change-section-lma";
  3681.                 context = SECTION_CONTEXT_ALTER_LMA;
  3682.                 break;
  3683.               case OPTION_CHANGE_SECTION_VMA:
  3684.                 option = "--change-section-vma";
  3685.                 context = SECTION_CONTEXT_ALTER_VMA;
  3686.                 break;
  3687.               }
  3688.  
  3689.             s = strchr (optarg, '=');
  3690.             if (s == NULL)
  3691.               {
  3692.                 s = strchr (optarg, '+');
  3693.                 if (s == NULL)
  3694.                   {
  3695.                     s = strchr (optarg, '-');
  3696.                     if (s == NULL)
  3697.                       fatal (_("bad format for %s"), option);
  3698.                   }
  3699.               }
  3700.             else
  3701.               {
  3702.                 /* Correct the context.  */
  3703.                 switch (c)
  3704.                   {
  3705.                   case OPTION_CHANGE_SECTION_ADDRESS:
  3706.                     context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
  3707.                     break;
  3708.                   case OPTION_CHANGE_SECTION_LMA:
  3709.                     context = SECTION_CONTEXT_SET_LMA;
  3710.                     break;
  3711.                   case OPTION_CHANGE_SECTION_VMA:
  3712.                     context = SECTION_CONTEXT_SET_VMA;
  3713.                     break;
  3714.                   }
  3715.               }
  3716.  
  3717.             len = s - optarg;
  3718.             name = (char *) xmalloc (len + 1);
  3719.             strncpy (name, optarg, len);
  3720.             name[len] = '\0';
  3721.  
  3722.             p = find_section_list (name, TRUE, context);
  3723.  
  3724.             val = parse_vma (s + 1, option);
  3725.             if (*s == '-')
  3726.               val = - val;
  3727.  
  3728.             switch (c)
  3729.               {
  3730.               case OPTION_CHANGE_SECTION_ADDRESS:
  3731.                 p->vma_val = val;
  3732.                 /* Drop through.  */
  3733.  
  3734.               case OPTION_CHANGE_SECTION_LMA:
  3735.                 p->lma_val = val;
  3736.                 break;
  3737.  
  3738.               case OPTION_CHANGE_SECTION_VMA:
  3739.                 p->vma_val = val;
  3740.                 break;
  3741.               }
  3742.           }
  3743.           break;
  3744.  
  3745.         case OPTION_CHANGE_ADDRESSES:
  3746.           change_section_address = parse_vma (optarg, "--change-addresses");
  3747.           change_start = change_section_address;
  3748.           break;
  3749.  
  3750.         case OPTION_CHANGE_WARNINGS:
  3751.           change_warn = TRUE;
  3752.           break;
  3753.  
  3754.         case OPTION_CHANGE_LEADING_CHAR:
  3755.           change_leading_char = TRUE;
  3756.           break;
  3757.  
  3758.         case OPTION_COMPRESS_DEBUG_SECTIONS:
  3759.           do_debug_sections = compress;
  3760.           break;
  3761.  
  3762.         case OPTION_DEBUGGING:
  3763.           convert_debugging = TRUE;
  3764.           break;
  3765.  
  3766.         case OPTION_DECOMPRESS_DEBUG_SECTIONS:
  3767.           do_debug_sections = decompress;
  3768.           break;
  3769.  
  3770.         case OPTION_GAP_FILL:
  3771.           {
  3772.             bfd_vma gap_fill_vma;
  3773.  
  3774.             gap_fill_vma = parse_vma (optarg, "--gap-fill");
  3775.             gap_fill = (bfd_byte) gap_fill_vma;
  3776.             if ((bfd_vma) gap_fill != gap_fill_vma)
  3777.               {
  3778.                 char buff[20];
  3779.  
  3780.                 sprintf_vma (buff, gap_fill_vma);
  3781.  
  3782.                 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
  3783.                            buff, gap_fill);
  3784.               }
  3785.             gap_fill_set = TRUE;
  3786.           }
  3787.           break;
  3788.  
  3789.         case OPTION_NO_CHANGE_WARNINGS:
  3790.           change_warn = FALSE;
  3791.           break;
  3792.  
  3793.         case OPTION_PAD_TO:
  3794.           pad_to = parse_vma (optarg, "--pad-to");
  3795.           pad_to_set = TRUE;
  3796.           break;
  3797.  
  3798.         case OPTION_REMOVE_LEADING_CHAR:
  3799.           remove_leading_char = TRUE;
  3800.           break;
  3801.  
  3802.         case OPTION_REDEFINE_SYM:
  3803.           {
  3804.             /* Push this redefinition onto redefine_symbol_list.  */
  3805.  
  3806.             int len;
  3807.             const char *s;
  3808.             const char *nextarg;
  3809.             char *source, *target;
  3810.  
  3811.             s = strchr (optarg, '=');
  3812.             if (s == NULL)
  3813.               fatal (_("bad format for %s"), "--redefine-sym");
  3814.  
  3815.             len = s - optarg;
  3816.             source = (char *) xmalloc (len + 1);
  3817.             strncpy (source, optarg, len);
  3818.             source[len] = '\0';
  3819.  
  3820.             nextarg = s + 1;
  3821.             len = strlen (nextarg);
  3822.             target = (char *) xmalloc (len + 1);
  3823.             strcpy (target, nextarg);
  3824.  
  3825.             redefine_list_append ("--redefine-sym", source, target);
  3826.  
  3827.             free (source);
  3828.             free (target);
  3829.           }
  3830.           break;
  3831.  
  3832.         case OPTION_REDEFINE_SYMS:
  3833.           add_redefine_syms_file (optarg);
  3834.           break;
  3835.  
  3836.         case OPTION_SET_SECTION_FLAGS:
  3837.           {
  3838.             struct section_list *p;
  3839.             const char *s;
  3840.             int len;
  3841.             char *name;
  3842.  
  3843.             s = strchr (optarg, '=');
  3844.             if (s == NULL)
  3845.               fatal (_("bad format for %s"), "--set-section-flags");
  3846.  
  3847.             len = s - optarg;
  3848.             name = (char *) xmalloc (len + 1);
  3849.             strncpy (name, optarg, len);
  3850.             name[len] = '\0';
  3851.  
  3852.             p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS);
  3853.  
  3854.             p->flags = parse_flags (s + 1);
  3855.           }
  3856.           break;
  3857.  
  3858.         case OPTION_RENAME_SECTION:
  3859.           {
  3860.             flagword flags;
  3861.             const char *eq, *fl;
  3862.             char *old_name;
  3863.             char *new_name;
  3864.             unsigned int len;
  3865.  
  3866.             eq = strchr (optarg, '=');
  3867.             if (eq == NULL)
  3868.               fatal (_("bad format for %s"), "--rename-section");
  3869.  
  3870.             len = eq - optarg;
  3871.             if (len == 0)
  3872.               fatal (_("bad format for %s"), "--rename-section");
  3873.  
  3874.             old_name = (char *) xmalloc (len + 1);
  3875.             strncpy (old_name, optarg, len);
  3876.             old_name[len] = 0;
  3877.  
  3878.             eq++;
  3879.             fl = strchr (eq, ',');
  3880.             if (fl)
  3881.               {
  3882.                 flags = parse_flags (fl + 1);
  3883.                 len = fl - eq;
  3884.               }
  3885.             else
  3886.               {
  3887.                 flags = -1;
  3888.                 len = strlen (eq);
  3889.               }
  3890.  
  3891.             if (len == 0)
  3892.               fatal (_("bad format for %s"), "--rename-section");
  3893.  
  3894.             new_name = (char *) xmalloc (len + 1);
  3895.             strncpy (new_name, eq, len);
  3896.             new_name[len] = 0;
  3897.  
  3898.             add_section_rename (old_name, new_name, flags);
  3899.           }
  3900.           break;
  3901.  
  3902.         case OPTION_SET_START:
  3903.           set_start = parse_vma (optarg, "--set-start");
  3904.           set_start_set = TRUE;
  3905.           break;
  3906.  
  3907.         case OPTION_SREC_LEN:
  3908.           Chunk = parse_vma (optarg, "--srec-len");
  3909.           break;
  3910.  
  3911.         case OPTION_SREC_FORCES3:
  3912.           S3Forced = TRUE;
  3913.           break;
  3914.  
  3915.         case OPTION_STRIP_SYMBOLS:
  3916.           add_specific_symbols (optarg, strip_specific_htab);
  3917.           break;
  3918.  
  3919.         case OPTION_STRIP_UNNEEDED_SYMBOLS:
  3920.           add_specific_symbols (optarg, strip_unneeded_htab);
  3921.           break;
  3922.  
  3923.         case OPTION_KEEP_SYMBOLS:
  3924.           add_specific_symbols (optarg, keep_specific_htab);
  3925.           break;
  3926.  
  3927.         case OPTION_LOCALIZE_HIDDEN:
  3928.           localize_hidden = TRUE;
  3929.           break;
  3930.  
  3931.         case OPTION_LOCALIZE_SYMBOLS:
  3932.           add_specific_symbols (optarg, localize_specific_htab);
  3933.           break;
  3934.  
  3935.         case OPTION_LONG_SECTION_NAMES:
  3936.           if (!strcmp ("enable", optarg))
  3937.             long_section_names = ENABLE;
  3938.           else if (!strcmp ("disable", optarg))
  3939.             long_section_names = DISABLE;
  3940.           else if (!strcmp ("keep", optarg))
  3941.             long_section_names = KEEP;
  3942.           else
  3943.             fatal (_("unknown long section names option '%s'"), optarg);
  3944.           break;
  3945.  
  3946.         case OPTION_GLOBALIZE_SYMBOLS:
  3947.           add_specific_symbols (optarg, globalize_specific_htab);
  3948.           break;
  3949.  
  3950.         case OPTION_KEEPGLOBAL_SYMBOLS:
  3951.           add_specific_symbols (optarg, keepglobal_specific_htab);
  3952.           break;
  3953.  
  3954.         case OPTION_WEAKEN_SYMBOLS:
  3955.           add_specific_symbols (optarg, weaken_specific_htab);
  3956.           break;
  3957.  
  3958.         case OPTION_ALT_MACH_CODE:
  3959.           use_alt_mach_code = strtoul (optarg, NULL, 0);
  3960.           if (use_alt_mach_code == 0)
  3961.             fatal (_("unable to parse alternative machine code"));
  3962.           break;
  3963.  
  3964.         case OPTION_PREFIX_SYMBOLS:
  3965.           prefix_symbols_string = optarg;
  3966.           break;
  3967.  
  3968.         case OPTION_PREFIX_SECTIONS:
  3969.           prefix_sections_string = optarg;
  3970.           break;
  3971.  
  3972.         case OPTION_PREFIX_ALLOC_SECTIONS:
  3973.           prefix_alloc_sections_string = optarg;
  3974.           break;
  3975.  
  3976.         case OPTION_READONLY_TEXT:
  3977.           bfd_flags_to_set |= WP_TEXT;
  3978.           bfd_flags_to_clear &= ~WP_TEXT;
  3979.           break;
  3980.  
  3981.         case OPTION_WRITABLE_TEXT:
  3982.           bfd_flags_to_clear |= WP_TEXT;
  3983.           bfd_flags_to_set &= ~WP_TEXT;
  3984.           break;
  3985.  
  3986.         case OPTION_PURE:
  3987.           bfd_flags_to_set |= D_PAGED;
  3988.           bfd_flags_to_clear &= ~D_PAGED;
  3989.           break;
  3990.  
  3991.         case OPTION_IMPURE:
  3992.           bfd_flags_to_clear |= D_PAGED;
  3993.           bfd_flags_to_set &= ~D_PAGED;
  3994.           break;
  3995.  
  3996.         case OPTION_EXTRACT_DWO:
  3997.           strip_symbols = STRIP_NONDWO;
  3998.           break;
  3999.  
  4000.         case OPTION_EXTRACT_SYMBOL:
  4001.           extract_symbol = TRUE;
  4002.           break;
  4003.  
  4004.         case OPTION_REVERSE_BYTES:
  4005.           {
  4006.             int prev = reverse_bytes;
  4007.  
  4008.             reverse_bytes = atoi (optarg);
  4009.             if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
  4010.               fatal (_("number of bytes to reverse must be positive and even"));
  4011.  
  4012.             if (prev && prev != reverse_bytes)
  4013.               non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
  4014.                          prev);
  4015.             break;
  4016.           }
  4017.  
  4018.         case OPTION_FILE_ALIGNMENT:
  4019.           pe_file_alignment = parse_vma (optarg, "--file-alignment");
  4020.           break;
  4021.  
  4022.         case OPTION_HEAP:
  4023.             {
  4024.               char *end;
  4025.               pe_heap_reserve = strtoul (optarg, &end, 0);
  4026.               if (end == optarg
  4027.                   || (*end != '.' && *end != '\0'))
  4028.                 non_fatal (_("%s: invalid reserve value for --heap"),
  4029.                            optarg);
  4030.               else if (*end != '\0')
  4031.                 {
  4032.                   pe_heap_commit = strtoul (end + 1, &end, 0);
  4033.                   if (*end != '\0')
  4034.                     non_fatal (_("%s: invalid commit value for --heap"),
  4035.                                optarg);
  4036.                 }
  4037.             }
  4038.           break;
  4039.  
  4040.         case OPTION_IMAGE_BASE:
  4041.           pe_image_base = parse_vma (optarg, "--image-base");
  4042.           break;
  4043.  
  4044.         case OPTION_SECTION_ALIGNMENT:
  4045.           pe_section_alignment = parse_vma (optarg,
  4046.                                             "--section-alignment");
  4047.           break;
  4048.  
  4049.         case OPTION_SUBSYSTEM:
  4050.           set_pe_subsystem (optarg);
  4051.           break;
  4052.  
  4053.         case OPTION_STACK:
  4054.             {
  4055.               char *end;
  4056.               pe_stack_reserve = strtoul (optarg, &end, 0);
  4057.               if (end == optarg
  4058.                   || (*end != '.' && *end != '\0'))
  4059.                 non_fatal (_("%s: invalid reserve value for --stack"),
  4060.                            optarg);
  4061.               else if (*end != '\0')
  4062.                 {
  4063.                   pe_stack_commit = strtoul (end + 1, &end, 0);
  4064.                   if (*end != '\0')
  4065.                     non_fatal (_("%s: invalid commit value for --stack"),
  4066.                                optarg);
  4067.                 }
  4068.             }
  4069.           break;
  4070.  
  4071.         case 0:
  4072.           /* We've been given a long option.  */
  4073.           break;
  4074.  
  4075.         case 'H':
  4076.         case 'h':
  4077.           copy_usage (stdout, 0);
  4078.  
  4079.         default:
  4080.           copy_usage (stderr, 1);
  4081.         }
  4082.     }
  4083.  
  4084.   if (formats_info)
  4085.     {
  4086.       display_info ();
  4087.       return 0;
  4088.     }
  4089.  
  4090.   if (show_version)
  4091.     print_version ("objcopy");
  4092.  
  4093.   if (interleave && copy_byte == -1)
  4094.     fatal (_("interleave start byte must be set with --byte"));
  4095.  
  4096.   if (copy_byte >= interleave)
  4097.     fatal (_("byte number must be less than interleave"));
  4098.  
  4099.   if (copy_width > interleave - copy_byte)
  4100.     fatal (_("interleave width must be less than or equal to interleave - byte`"));
  4101.  
  4102.   if (optind == argc || optind + 2 < argc)
  4103.     copy_usage (stderr, 1);
  4104.  
  4105.   input_filename = argv[optind];
  4106.   if (optind + 1 < argc)
  4107.     output_filename = argv[optind + 1];
  4108.  
  4109.   default_deterministic ();
  4110.  
  4111.   /* Default is to strip no symbols.  */
  4112.   if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
  4113.     strip_symbols = STRIP_NONE;
  4114.  
  4115.   if (output_target == NULL)
  4116.     output_target = input_target;
  4117.  
  4118.   /* Convert input EFI target to PEI target.  */
  4119.   if (input_target != NULL
  4120.       && strncmp (input_target, "efi-", 4) == 0)
  4121.     {
  4122.       char *efi;
  4123.  
  4124.       efi = xstrdup (output_target + 4);
  4125.       if (strncmp (efi, "bsdrv-", 6) == 0
  4126.           || strncmp (efi, "rtdrv-", 6) == 0)
  4127.         efi += 2;
  4128.       else if (strncmp (efi, "app-", 4) != 0)
  4129.         fatal (_("unknown input EFI target: %s"), input_target);
  4130.  
  4131.       input_target = efi;
  4132.       convert_efi_target (efi);
  4133.     }
  4134.  
  4135.   /* Convert output EFI target to PEI target.  */
  4136.   if (output_target != NULL
  4137.       && strncmp (output_target, "efi-", 4) == 0)
  4138.     {
  4139.       char *efi;
  4140.  
  4141.       efi = xstrdup (output_target + 4);
  4142.       if (strncmp (efi, "app-", 4) == 0)
  4143.         {
  4144.           if (pe_subsystem == -1)
  4145.             pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  4146.         }
  4147.       else if (strncmp (efi, "bsdrv-", 6) == 0)
  4148.         {
  4149.           if (pe_subsystem == -1)
  4150.             pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
  4151.           efi += 2;
  4152.         }
  4153.       else if (strncmp (efi, "rtdrv-", 6) == 0)
  4154.         {
  4155.           if (pe_subsystem == -1)
  4156.             pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
  4157.           efi += 2;
  4158.         }
  4159.       else
  4160.         fatal (_("unknown output EFI target: %s"), output_target);
  4161.  
  4162.       if (pe_file_alignment == (bfd_vma) -1)
  4163.         pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
  4164.       if (pe_section_alignment == (bfd_vma) -1)
  4165.         pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
  4166.  
  4167.       output_target = efi;
  4168.       convert_efi_target (efi);
  4169.     }
  4170.  
  4171.   if (preserve_dates)
  4172.     if (stat (input_filename, & statbuf) < 0)
  4173.       fatal (_("warning: could not locate '%s'.  System error message: %s"),
  4174.              input_filename, strerror (errno));
  4175.  
  4176.   /* If there is no destination file, or the source and destination files
  4177.      are the same, then create a temp and rename the result into the input.  */
  4178.   if (output_filename == NULL
  4179.       || filename_cmp (input_filename, output_filename) == 0)
  4180.     tmpname = make_tempname (input_filename);
  4181.   else
  4182.     tmpname = output_filename;
  4183.  
  4184.   if (tmpname == NULL)
  4185.     fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
  4186.            input_filename, strerror (errno));
  4187.  
  4188.   copy_file (input_filename, tmpname, input_target, output_target, input_arch);
  4189.   if (status == 0)
  4190.     {
  4191.       if (preserve_dates)
  4192.         set_times (tmpname, &statbuf);
  4193.       if (tmpname != output_filename)
  4194.         status = (smart_rename (tmpname, input_filename,
  4195.                                 preserve_dates) != 0);
  4196.     }
  4197.   else
  4198.     unlink_if_ordinary (tmpname);
  4199.  
  4200.   if (change_warn)
  4201.     {
  4202.       struct section_list *p;
  4203.  
  4204.       for (p = change_sections; p != NULL; p = p->next)
  4205.         {
  4206.           if (! p->used)
  4207.             {
  4208.               if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
  4209.                 {
  4210.                   char buff [20];
  4211.  
  4212.                   sprintf_vma (buff, p->vma_val);
  4213.  
  4214.                   /* xgettext:c-format */
  4215.                   non_fatal (_("%s %s%c0x%s never used"),
  4216.                              "--change-section-vma",
  4217.                              p->pattern,
  4218.                              p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
  4219.                              buff);
  4220.                 }
  4221.  
  4222.               if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
  4223.                 {
  4224.                   char buff [20];
  4225.  
  4226.                   sprintf_vma (buff, p->lma_val);
  4227.  
  4228.                   /* xgettext:c-format */
  4229.                   non_fatal (_("%s %s%c0x%s never used"),
  4230.                              "--change-section-lma",
  4231.                              p->pattern,
  4232.                              p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
  4233.                              buff);
  4234.                 }
  4235.             }
  4236.         }
  4237.     }
  4238.  
  4239.   return 0;
  4240. }
  4241.  
  4242. int
  4243. main (int argc, char *argv[])
  4244. {
  4245. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  4246.   setlocale (LC_MESSAGES, "");
  4247. #endif
  4248. #if defined (HAVE_SETLOCALE)
  4249.   setlocale (LC_CTYPE, "");
  4250. #endif
  4251.   bindtextdomain (PACKAGE, LOCALEDIR);
  4252.   textdomain (PACKAGE);
  4253.  
  4254.   program_name = argv[0];
  4255.   xmalloc_set_program_name (program_name);
  4256.  
  4257.   START_PROGRESS (program_name, 0);
  4258.  
  4259.   expandargv (&argc, &argv);
  4260.  
  4261.   strip_symbols = STRIP_UNDEF;
  4262.   discard_locals = LOCALS_UNDEF;
  4263.  
  4264.   bfd_init ();
  4265.   set_default_bfd_target ();
  4266.  
  4267.   if (is_strip < 0)
  4268.     {
  4269.       int i = strlen (program_name);
  4270. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  4271.       /* Drop the .exe suffix, if any.  */
  4272.       if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
  4273.         {
  4274.           i -= 4;
  4275.           program_name[i] = '\0';
  4276.         }
  4277. #endif
  4278.       is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
  4279.     }
  4280.  
  4281.   create_symbol_htabs ();
  4282.  
  4283.   if (is_strip)
  4284.     strip_main (argc, argv);
  4285.   else
  4286.     copy_main (argc, argv);
  4287.  
  4288.   END_PROGRESS (program_name);
  4289.  
  4290.   return status;
  4291. }
  4292.