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