* bucomm.c (bfd_nonfatal_message): New.
[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
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
1834   list = NULL;
1835
1836   this_element = bfd_openr_next_archived_file (ibfd, NULL);
1837
1838   if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1839     RETURN_NONFATAL (obfd);
1840
1841   while (!status && this_element != NULL)
1842     {
1843       char *output_name;
1844       bfd *output_bfd;
1845       bfd *last_element;
1846       struct stat buf;
1847       int stat_status = 0;
1848       bfd_boolean delete = TRUE;
1849
1850       /* Create an output file for this member.  */
1851       output_name = concat (dir, "/",
1852                             bfd_get_filename (this_element), (char *) 0);
1853
1854       /* If the file already exists, make another temp dir.  */
1855       if (stat (output_name, &buf) >= 0)
1856         {
1857           output_name = make_tempdir (output_name);
1858           if (output_name == NULL)
1859             fatal (_("cannot create tempdir for archive copying (error: %s)"),
1860                    strerror (errno));
1861
1862           l = xmalloc (sizeof (struct name_list));
1863           l->name = output_name;
1864           l->next = list;
1865           l->obfd = NULL;
1866           list = l;
1867           output_name = concat (output_name, "/",
1868                                 bfd_get_filename (this_element), (char *) 0);
1869         }
1870
1871       if (preserve_dates)
1872         {
1873           stat_status = bfd_stat_arch_elt (this_element, &buf);
1874
1875           if (stat_status != 0)
1876             non_fatal (_("internal stat error on %s"),
1877                        bfd_get_filename (this_element));
1878         }
1879
1880       l = xmalloc (sizeof (struct name_list));
1881       l->name = output_name;
1882       l->next = list;
1883       l->obfd = NULL;
1884       list = l;
1885
1886       if (bfd_check_format (this_element, bfd_object))
1887         {
1888           /* PR binutils/3110: Cope with archives
1889              containing multiple target types.  */
1890           if (force_output_target)
1891             output_bfd = bfd_openw (output_name, output_target);
1892           else
1893             output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
1894
1895           if (output_bfd == NULL)
1896             {
1897               bfd_nonfatal_message (output_name, NULL, NULL, NULL);
1898               status = 1;
1899               return;
1900             }
1901
1902           delete = ! copy_object (this_element, output_bfd);
1903
1904           if (! delete
1905               || bfd_get_arch (this_element) != bfd_arch_unknown)
1906             {
1907               if (!bfd_close (output_bfd))
1908                 {
1909                   bfd_nonfatal_message (NULL, output_bfd, NULL, NULL);
1910                   /* Error in new object file. Don't change archive.  */
1911                   status = 1;
1912                 }
1913             }
1914           else
1915             goto copy_unknown_element;
1916         }
1917       else
1918         {
1919           bfd_nonfatal_message (bfd_get_archive_filename (this_element),
1920                                 NULL, NULL,
1921                                 _("Unable to recognise the format of file"));
1922
1923           output_bfd = bfd_openw (output_name, output_target);
1924 copy_unknown_element:
1925           delete = !copy_unknown_object (this_element, output_bfd);
1926           if (!bfd_close_all_done (output_bfd))
1927             {
1928               bfd_nonfatal_message (NULL, output_bfd, NULL, NULL);
1929               /* Error in new object file. Don't change archive.  */
1930               status = 1;
1931             }
1932         }
1933
1934       if (delete)
1935         {
1936           unlink (output_name);
1937           status = 1;
1938         }
1939       else
1940         {
1941           if (preserve_dates && stat_status == 0)
1942             set_times (output_name, &buf);
1943
1944           /* Open the newly output file and attach to our list.  */
1945           output_bfd = bfd_openr (output_name, output_target);
1946
1947           l->obfd = output_bfd;
1948
1949           *ptr = output_bfd;
1950           ptr = &output_bfd->archive_next;
1951
1952           last_element = this_element;
1953
1954           this_element = bfd_openr_next_archived_file (ibfd, last_element);
1955
1956           bfd_close (last_element);
1957         }
1958     }
1959   *ptr = NULL;
1960
1961   if (!bfd_close (obfd))
1962     RETURN_NONFATAL (obfd);
1963
1964   if (!bfd_close (ibfd))
1965     RETURN_NONFATAL (obfd);
1966
1967   /* Delete all the files that we opened.  */
1968   for (l = list; l != NULL; l = l->next)
1969     {
1970       if (l->obfd == NULL)
1971         rmdir (l->name);
1972       else
1973         {
1974           bfd_close (l->obfd);
1975           unlink (l->name);
1976         }
1977     }
1978   rmdir (dir);
1979 }
1980
1981 /* The top-level control.  */
1982
1983 static void
1984 copy_file (const char *input_filename, const char *output_filename,
1985            const char *input_target,   const char *output_target)
1986 {
1987   bfd *ibfd;
1988   char **obj_matching;
1989   char **core_matching;
1990
1991   if (get_file_size (input_filename) < 1)
1992     {
1993       status = 1;
1994       return;
1995     }
1996
1997   /* To allow us to do "strip *" without dying on the first
1998      non-object file, failures are nonfatal.  */
1999   ibfd = bfd_openr (input_filename, input_target);
2000   if (ibfd == NULL)
2001     {
2002       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2003       status = 1;
2004       return;
2005     }
2006
2007   if (bfd_check_format (ibfd, bfd_archive))
2008     {
2009       bfd_boolean force_output_target;
2010       bfd *obfd;
2011
2012       /* bfd_get_target does not return the correct value until
2013          bfd_check_format succeeds.  */
2014       if (output_target == NULL)
2015         {
2016           output_target = bfd_get_target (ibfd);
2017           force_output_target = FALSE;
2018         }
2019       else
2020         force_output_target = TRUE;
2021
2022       obfd = bfd_openw (output_filename, output_target);
2023       if (obfd == NULL)
2024         {
2025           bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2026           status = 1;
2027           return;
2028         }
2029
2030       copy_archive (ibfd, obfd, output_target, force_output_target);
2031     }
2032   else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2033     {
2034       bfd *obfd;
2035     do_copy:
2036
2037       /* bfd_get_target does not return the correct value until
2038          bfd_check_format succeeds.  */
2039       if (output_target == NULL)
2040         output_target = bfd_get_target (ibfd);
2041
2042       obfd = bfd_openw (output_filename, output_target);
2043       if (obfd == NULL)
2044         {
2045           bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2046           status = 1;
2047           return;
2048         }
2049
2050       if (! copy_object (ibfd, obfd))
2051         status = 1;
2052
2053       if (!bfd_close (obfd))
2054         RETURN_NONFATAL (obfd);
2055
2056       if (!bfd_close (ibfd))
2057         RETURN_NONFATAL (ibfd);
2058     }
2059   else
2060     {
2061       bfd_error_type obj_error = bfd_get_error ();
2062       bfd_error_type core_error;
2063
2064       if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2065         {
2066           /* This probably can't happen..  */
2067           if (obj_error == bfd_error_file_ambiguously_recognized)
2068             free (obj_matching);
2069           goto do_copy;
2070         }
2071
2072       core_error = bfd_get_error ();
2073       /* Report the object error in preference to the core error.  */
2074       if (obj_error != core_error)
2075         bfd_set_error (obj_error);
2076
2077       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2078
2079       if (obj_error == bfd_error_file_ambiguously_recognized)
2080         {
2081           list_matching_formats (obj_matching);
2082           free (obj_matching);
2083         }
2084       if (core_error == bfd_error_file_ambiguously_recognized)
2085         {
2086           list_matching_formats (core_matching);
2087           free (core_matching);
2088         }
2089
2090       status = 1;
2091     }
2092 }
2093
2094 /* Add a name to the section renaming list.  */
2095
2096 static void
2097 add_section_rename (const char * old_name, const char * new_name,
2098                     flagword flags)
2099 {
2100   section_rename * rename;
2101
2102   /* Check for conflicts first.  */
2103   for (rename = section_rename_list; rename != NULL; rename = rename->next)
2104     if (strcmp (rename->old_name, old_name) == 0)
2105       {
2106         /* Silently ignore duplicate definitions.  */
2107         if (strcmp (rename->new_name, new_name) == 0
2108             && rename->flags == flags)
2109           return;
2110
2111         fatal (_("Multiple renames of section %s"), old_name);
2112       }
2113
2114   rename = xmalloc (sizeof (* rename));
2115
2116   rename->old_name = old_name;
2117   rename->new_name = new_name;
2118   rename->flags    = flags;
2119   rename->next     = section_rename_list;
2120
2121   section_rename_list = rename;
2122 }
2123
2124 /* Check the section rename list for a new name of the input section
2125    ISECTION.  Return the new name if one is found.
2126    Also set RETURNED_FLAGS to the flags to be used for this section.  */
2127
2128 static const char *
2129 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2130                      flagword * returned_flags)
2131 {
2132   const char * old_name = bfd_section_name (ibfd, isection);
2133   section_rename * rename;
2134
2135   /* Default to using the flags of the input section.  */
2136   * returned_flags = bfd_get_section_flags (ibfd, isection);
2137
2138   for (rename = section_rename_list; rename != NULL; rename = rename->next)
2139     if (strcmp (rename->old_name, old_name) == 0)
2140       {
2141         if (rename->flags != (flagword) -1)
2142           * returned_flags = rename->flags;
2143
2144         return rename->new_name;
2145       }
2146
2147   return old_name;
2148 }
2149
2150 /* Once each of the sections is copied, we may still need to do some
2151    finalization work for private section headers.  Do that here.  */
2152
2153 static void
2154 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2155 {
2156   /* Allow the BFD backend to copy any private data it understands
2157      from the input section to the output section.  */
2158   if (! bfd_copy_private_header_data (ibfd, obfd))
2159     {
2160       status = 1;
2161       bfd_nonfatal_message (NULL, ibfd, NULL,
2162                             _("error in private h       eader data"));
2163       return;
2164     }
2165
2166   /* All went well.  */
2167   return;
2168 }
2169
2170 /* Create a section in OBFD with the same
2171    name and attributes as ISECTION in IBFD.  */
2172
2173 static void
2174 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2175 {
2176   bfd *obfd = obfdarg;
2177   struct section_list *p;
2178   sec_ptr osection;
2179   bfd_size_type size;
2180   bfd_vma vma;
2181   bfd_vma lma;
2182   flagword flags;
2183   const char *err;
2184   const char * name;
2185   char *prefix = NULL;
2186   bfd_boolean make_nobits;
2187
2188   if (is_strip_section (ibfd, isection))
2189     return;
2190
2191   p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2192   if (p != NULL)
2193     p->used = TRUE;
2194
2195   /* Get the, possibly new, name of the output section.  */
2196   name = find_section_rename (ibfd, isection, & flags);
2197
2198   /* Prefix sections.  */
2199   if ((prefix_alloc_sections_string)
2200       && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2201     prefix = prefix_alloc_sections_string;
2202   else if (prefix_sections_string)
2203     prefix = prefix_sections_string;
2204
2205   if (prefix)
2206     {
2207       char *n;
2208
2209       n = xmalloc (strlen (prefix) + strlen (name) + 1);
2210       strcpy (n, prefix);
2211       strcat (n, name);
2212       name = n;
2213     }
2214
2215   make_nobits = FALSE;
2216   if (p != NULL && p->set_flags)
2217     flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2218   else if (strip_symbols == STRIP_NONDEBUG
2219            && (flags & SEC_ALLOC) != 0
2220            && (ibfd->xvec->flavour != bfd_target_elf_flavour
2221                || elf_section_type (isection) != SHT_NOTE))
2222     {
2223       flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2224       if (obfd->xvec->flavour == bfd_target_elf_flavour)
2225         {
2226           make_nobits = TRUE;
2227
2228           /* Twiddle the input section flags so that it seems to
2229              elf.c:copy_private_bfd_data that section flags have not
2230              changed between input and output sections.  This hack
2231              prevents wholesale rewriting of the program headers.  */
2232           isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2233         }
2234     }
2235
2236   osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2237
2238   if (osection == NULL)
2239     {
2240       err = _("failed to create output section");
2241       goto loser;
2242     }
2243
2244   if (make_nobits)
2245     elf_section_type (osection) = SHT_NOBITS;
2246
2247   size = bfd_section_size (ibfd, isection);
2248   if (copy_byte >= 0)
2249     size = (size + interleave - 1) / interleave;
2250   else if (extract_symbol)
2251     size = 0;
2252   if (! bfd_set_section_size (obfd, osection, size))
2253     {
2254       err = _("failed to set size");
2255       goto loser;
2256     }
2257
2258   vma = bfd_section_vma (ibfd, isection);
2259   if (p != NULL && p->change_vma == CHANGE_MODIFY)
2260     vma += p->vma_val;
2261   else if (p != NULL && p->change_vma == CHANGE_SET)
2262     vma = p->vma_val;
2263   else
2264     vma += change_section_address;
2265
2266   if (! bfd_set_section_vma (obfd, osection, extract_symbol ? 0 : vma))
2267     {
2268       err = _("failed to set vma");
2269       goto loser;
2270     }
2271
2272   lma = isection->lma;
2273   if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2274     {
2275       if (p->change_lma == CHANGE_MODIFY)
2276         lma += p->lma_val;
2277       else if (p->change_lma == CHANGE_SET)
2278         lma = p->lma_val;
2279       else
2280         abort ();
2281     }
2282   else
2283     lma += change_section_address;
2284
2285   osection->lma = extract_symbol ? 0 : lma;
2286
2287   /* FIXME: This is probably not enough.  If we change the LMA we
2288      may have to recompute the header for the file as well.  */
2289   if (!bfd_set_section_alignment (obfd,
2290                                   osection,
2291                                   bfd_section_alignment (ibfd, isection)))
2292     {
2293       err = _("failed to set alignment");
2294       goto loser;
2295     }
2296
2297   /* Copy merge entity size.  */
2298   osection->entsize = isection->entsize;
2299
2300   /* This used to be mangle_section; we do here to avoid using
2301      bfd_get_section_by_name since some formats allow multiple
2302      sections with the same name.  */
2303   isection->output_section = osection;
2304   isection->output_offset = extract_symbol ? vma : 0;
2305
2306   /* Do not copy backend data if --extract-symbol is passed; anything
2307      that needs to look at the section contents will fail.  */
2308   if (extract_symbol)
2309     return;
2310
2311   /* Allow the BFD backend to copy any private data it understands
2312      from the input section to the output section.  */
2313   if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2314     {
2315       err = _("failed to copy private data");
2316       goto loser;
2317     }
2318   else if ((isection->flags & SEC_GROUP) != 0)
2319     {
2320       asymbol *gsym = group_signature (isection);
2321
2322       if (gsym != NULL)
2323         gsym->flags |= BSF_KEEP;
2324     }
2325
2326   /* All went well.  */
2327   return;
2328
2329 loser:
2330   status = 1;
2331   bfd_nonfatal_message (NULL, obfd, osection, err);
2332 }
2333
2334 /* Copy the data of input section ISECTION of IBFD
2335    to an output section with the same name in OBFD.
2336    If stripping then don't copy any relocation info.  */
2337
2338 static void
2339 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2340 {
2341   bfd *obfd = obfdarg;
2342   struct section_list *p;
2343   arelent **relpp;
2344   long relcount;
2345   sec_ptr osection;
2346   bfd_size_type size;
2347   long relsize;
2348   flagword flags;
2349
2350   /* If we have already failed earlier on,
2351      do not keep on generating complaints now.  */
2352   if (status != 0)
2353     return;
2354
2355   if (is_strip_section (ibfd, isection))
2356     return;
2357
2358   flags = bfd_get_section_flags (ibfd, isection);
2359   if ((flags & SEC_GROUP) != 0)
2360     return;
2361
2362   osection = isection->output_section;
2363   size = bfd_get_section_size (isection);
2364
2365   if (size == 0 || osection == 0)
2366     return;
2367
2368   p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2369
2370   /* Core files do not need to be relocated.  */
2371   if (bfd_get_format (obfd) == bfd_core)
2372     relsize = 0;
2373   else
2374     {
2375       relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2376
2377       if (relsize < 0)
2378         {
2379           /* Do not complain if the target does not support relocations.  */
2380           if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2381             relsize = 0;
2382           else
2383             {
2384               status = 1;
2385               bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2386               return;
2387             }
2388         }
2389     }
2390
2391   if (relsize == 0)
2392     bfd_set_reloc (obfd, osection, NULL, 0);
2393   else
2394     {
2395       relpp = xmalloc (relsize);
2396       relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2397       if (relcount < 0)
2398         {
2399           status = 1;
2400           bfd_nonfatal_message (NULL, ibfd, isection,
2401                                 _("relocation count is negative"));
2402           return;
2403         }
2404
2405       if (strip_symbols == STRIP_ALL)
2406         {
2407           /* Remove relocations which are not in
2408              keep_strip_specific_list.  */
2409           arelent **temp_relpp;
2410           long temp_relcount = 0;
2411           long i;
2412
2413           temp_relpp = xmalloc (relsize);
2414           for (i = 0; i < relcount; i++)
2415             if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2416                                      keep_specific_list))
2417               temp_relpp [temp_relcount++] = relpp [i];
2418           relcount = temp_relcount;
2419           free (relpp);
2420           relpp = temp_relpp;
2421         }
2422
2423       bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2424       if (relcount == 0)
2425         free (relpp);
2426     }
2427
2428   if (extract_symbol)
2429     return;
2430
2431   if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2432       && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2433     {
2434       void *memhunk = xmalloc (size);
2435
2436       if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2437         {
2438           status = 1;
2439           bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2440           return;
2441         }
2442
2443       if (reverse_bytes)
2444         {
2445           /* We don't handle leftover bytes (too many possible behaviors,
2446              and we don't know what the user wants).  The section length
2447              must be a multiple of the number of bytes to swap.  */
2448           if ((size % reverse_bytes) == 0)
2449             {
2450               unsigned long i, j;
2451               bfd_byte b;
2452
2453               for (i = 0; i < size; i += reverse_bytes)
2454                 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
2455                   {
2456                     bfd_byte *m = (bfd_byte *) memhunk;
2457
2458                     b = m[i + j];
2459                     m[i + j] = m[(i + reverse_bytes) - (j + 1)];
2460                     m[(i + reverse_bytes) - (j + 1)] = b;
2461                   }
2462             }
2463           else
2464             /* User must pad the section up in order to do this.  */
2465             fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2466                    bfd_section_name (ibfd, isection), reverse_bytes);
2467         }
2468
2469       if (copy_byte >= 0)
2470         {
2471           /* Keep only every `copy_byte'th byte in MEMHUNK.  */
2472           char *from = (char *) memhunk + copy_byte;
2473           char *to = memhunk;
2474           char *end = (char *) memhunk + size;
2475
2476           for (; from < end; from += interleave)
2477             *to++ = *from;
2478
2479           size = (size + interleave - 1 - copy_byte) / interleave;
2480           osection->lma /= interleave;
2481         }
2482
2483       if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2484         {
2485           status = 1;
2486           bfd_nonfatal_message (NULL, obfd, osection, NULL);
2487           return;
2488         }
2489       free (memhunk);
2490     }
2491   else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2492     {
2493       void *memhunk = xmalloc (size);
2494
2495       /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2496          flag--they can just remove the section entirely and add it
2497          back again.  However, we do permit them to turn on the
2498          SEC_HAS_CONTENTS flag, and take it to mean that the section
2499          contents should be zeroed out.  */
2500
2501       memset (memhunk, 0, size);
2502       if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2503         {
2504           status = 1;
2505           bfd_nonfatal_message (NULL, obfd, osection, NULL);
2506           return;
2507         }
2508       free (memhunk);
2509     }
2510 }
2511
2512 /* Get all the sections.  This is used when --gap-fill or --pad-to is
2513    used.  */
2514
2515 static void
2516 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2517 {
2518   asection ***secppp = secppparg;
2519
2520   **secppp = osection;
2521   ++(*secppp);
2522 }
2523
2524 /* Sort sections by VMA.  This is called via qsort, and is used when
2525    --gap-fill or --pad-to is used.  We force non loadable or empty
2526    sections to the front, where they are easier to ignore.  */
2527
2528 static int
2529 compare_section_lma (const void *arg1, const void *arg2)
2530 {
2531   const asection *const *sec1 = arg1;
2532   const asection *const *sec2 = arg2;
2533   flagword flags1, flags2;
2534
2535   /* Sort non loadable sections to the front.  */
2536   flags1 = (*sec1)->flags;
2537   flags2 = (*sec2)->flags;
2538   if ((flags1 & SEC_HAS_CONTENTS) == 0
2539       || (flags1 & SEC_LOAD) == 0)
2540     {
2541       if ((flags2 & SEC_HAS_CONTENTS) != 0
2542           && (flags2 & SEC_LOAD) != 0)
2543         return -1;
2544     }
2545   else
2546     {
2547       if ((flags2 & SEC_HAS_CONTENTS) == 0
2548           || (flags2 & SEC_LOAD) == 0)
2549         return 1;
2550     }
2551
2552   /* Sort sections by LMA.  */
2553   if ((*sec1)->lma > (*sec2)->lma)
2554     return 1;
2555   else if ((*sec1)->lma < (*sec2)->lma)
2556     return -1;
2557
2558   /* Sort sections with the same LMA by size.  */
2559   if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2560     return 1;
2561   else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2562     return -1;
2563
2564   return 0;
2565 }
2566
2567 /* Mark all the symbols which will be used in output relocations with
2568    the BSF_KEEP flag so that those symbols will not be stripped.
2569
2570    Ignore relocations which will not appear in the output file.  */
2571
2572 static void
2573 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2574 {
2575   asymbol **symbols = symbolsarg;
2576   long relsize;
2577   arelent **relpp;
2578   long relcount, i;
2579
2580   /* Ignore an input section with no corresponding output section.  */
2581   if (isection->output_section == NULL)
2582     return;
2583
2584   relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2585   if (relsize < 0)
2586     {
2587       /* Do not complain if the target does not support relocations.  */
2588       if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2589         return;
2590       bfd_fatal (bfd_get_filename (ibfd));
2591     }
2592
2593   if (relsize == 0)
2594     return;
2595
2596   relpp = xmalloc (relsize);
2597   relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2598   if (relcount < 0)
2599     bfd_fatal (bfd_get_filename (ibfd));
2600
2601   /* Examine each symbol used in a relocation.  If it's not one of the
2602      special bfd section symbols, then mark it with BSF_KEEP.  */
2603   for (i = 0; i < relcount; i++)
2604     {
2605       if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2606           && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2607           && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2608         (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2609     }
2610
2611   if (relpp != NULL)
2612     free (relpp);
2613 }
2614
2615 /* Write out debugging information.  */
2616
2617 static bfd_boolean
2618 write_debugging_info (bfd *obfd, void *dhandle,
2619                       long *symcountp ATTRIBUTE_UNUSED,
2620                       asymbol ***symppp ATTRIBUTE_UNUSED)
2621 {
2622   if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2623     return write_ieee_debugging_info (obfd, dhandle);
2624
2625   if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2626       || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2627     {
2628       bfd_byte *syms, *strings;
2629       bfd_size_type symsize, stringsize;
2630       asection *stabsec, *stabstrsec;
2631       flagword flags;
2632
2633       if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2634                                                     &symsize, &strings,
2635                                                     &stringsize))
2636         return FALSE;
2637
2638       flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2639       stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2640       stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2641       if (stabsec == NULL
2642           || stabstrsec == NULL
2643           || ! bfd_set_section_size (obfd, stabsec, symsize)
2644           || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2645           || ! bfd_set_section_alignment (obfd, stabsec, 2)
2646           || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2647         {
2648           bfd_nonfatal_message (NULL, obfd, NULL,
2649                                 _("can't create debugging section"));
2650           return FALSE;
2651         }
2652
2653       /* We can get away with setting the section contents now because
2654          the next thing the caller is going to do is copy over the
2655          real sections.  We may someday have to split the contents
2656          setting out of this function.  */
2657       if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2658           || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2659                                          stringsize))
2660         {
2661           bfd_nonfatal_message (NULL, obfd, NULL,
2662                                 _("can't set debugging section contents"));
2663           return FALSE;
2664         }
2665
2666       return TRUE;
2667     }
2668
2669   bfd_nonfatal_message (NULL, obfd, NULL,
2670                         _("don't know how to write debugging information for %s"),
2671              bfd_get_target (obfd));
2672   return FALSE;
2673 }
2674
2675 static int
2676 strip_main (int argc, char *argv[])
2677 {
2678   char *input_target = NULL;
2679   char *output_target = NULL;
2680   bfd_boolean show_version = FALSE;
2681   bfd_boolean formats_info = FALSE;
2682   int c;
2683   int i;
2684   struct section_list *p;
2685   char *output_file = NULL;
2686
2687   while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2688                            strip_options, (int *) 0)) != EOF)
2689     {
2690       switch (c)
2691         {
2692         case 'I':
2693           input_target = optarg;
2694           break;
2695         case 'O':
2696           output_target = optarg;
2697           break;
2698         case 'F':
2699           input_target = output_target = optarg;
2700           break;
2701         case 'R':
2702           p = find_section_list (optarg, TRUE);
2703           p->remove = TRUE;
2704           sections_removed = TRUE;
2705           break;
2706         case 's':
2707           strip_symbols = STRIP_ALL;
2708           break;
2709         case 'S':
2710         case 'g':
2711         case 'd':       /* Historic BSD alias for -g.  Used by early NetBSD.  */
2712           strip_symbols = STRIP_DEBUG;
2713           break;
2714         case OPTION_STRIP_UNNEEDED:
2715           strip_symbols = STRIP_UNNEEDED;
2716           break;
2717         case 'K':
2718           add_specific_symbol (optarg, &keep_specific_list);
2719           break;
2720         case 'N':
2721           add_specific_symbol (optarg, &strip_specific_list);
2722           break;
2723         case 'o':
2724           output_file = optarg;
2725           break;
2726         case 'p':
2727           preserve_dates = TRUE;
2728           break;
2729         case 'x':
2730           discard_locals = LOCALS_ALL;
2731           break;
2732         case 'X':
2733           discard_locals = LOCALS_START_L;
2734           break;
2735         case 'v':
2736           verbose = TRUE;
2737           break;
2738         case 'V':
2739           show_version = TRUE;
2740           break;
2741         case OPTION_FORMATS_INFO:
2742           formats_info = TRUE;
2743           break;
2744         case OPTION_ONLY_KEEP_DEBUG:
2745           strip_symbols = STRIP_NONDEBUG;
2746           break;
2747         case OPTION_KEEP_FILE_SYMBOLS:
2748           keep_file_symbols = 1;
2749           break;
2750         case 0:
2751           /* We've been given a long option.  */
2752           break;
2753         case 'w':
2754           wildcard = TRUE;
2755           break;
2756         case 'H':
2757         case 'h':
2758           strip_usage (stdout, 0);
2759         default:
2760           strip_usage (stderr, 1);
2761         }
2762     }
2763
2764   if (formats_info)
2765     {
2766       display_info ();
2767       return 0;
2768     }
2769
2770   if (show_version)
2771     print_version ("strip");
2772
2773   /* Default is to strip all symbols.  */
2774   if (strip_symbols == STRIP_UNDEF
2775       && discard_locals == LOCALS_UNDEF
2776       && strip_specific_list == NULL)
2777     strip_symbols = STRIP_ALL;
2778
2779   if (output_target == NULL)
2780     output_target = input_target;
2781
2782   i = optind;
2783   if (i == argc
2784       || (output_file != NULL && (i + 1) < argc))
2785     strip_usage (stderr, 1);
2786
2787   for (; i < argc; i++)
2788     {
2789       int hold_status = status;
2790       struct stat statbuf;
2791       char *tmpname;
2792
2793       if (get_file_size (argv[i]) < 1)
2794         {
2795           status = 1;
2796           continue;
2797         }
2798
2799       if (preserve_dates)
2800         /* No need to check the return value of stat().
2801            It has already been checked in get_file_size().  */
2802         stat (argv[i], &statbuf);
2803
2804       if (output_file == NULL || strcmp (argv[i], output_file) == 0)
2805         tmpname = make_tempname (argv[i]);
2806       else
2807         tmpname = output_file;
2808
2809       if (tmpname == NULL)
2810         {
2811           bfd_nonfatal_message (argv[i], NULL, NULL,
2812                                 _("could not create temporary file to hold stripped copy"));
2813           status = 1;
2814           continue;
2815         }
2816
2817       status = 0;
2818       copy_file (argv[i], tmpname, input_target, output_target);
2819       if (status == 0)
2820         {
2821           if (preserve_dates)
2822             set_times (tmpname, &statbuf);
2823           if (output_file != tmpname)
2824             smart_rename (tmpname, output_file ? output_file : argv[i],
2825                           preserve_dates);
2826           status = hold_status;
2827         }
2828       else
2829         unlink_if_ordinary (tmpname);
2830       if (output_file != tmpname)
2831         free (tmpname);
2832     }
2833
2834   return status;
2835 }
2836
2837 static int
2838 copy_main (int argc, char *argv[])
2839 {
2840   char * binary_architecture = NULL;
2841   char *input_filename = NULL;
2842   char *output_filename = NULL;
2843   char *tmpname;
2844   char *input_target = NULL;
2845   char *output_target = NULL;
2846   bfd_boolean show_version = FALSE;
2847   bfd_boolean change_warn = TRUE;
2848   bfd_boolean formats_info = FALSE;
2849   int c;
2850   struct section_list *p;
2851   struct stat statbuf;
2852
2853   while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2854                            copy_options, (int *) 0)) != EOF)
2855     {
2856       switch (c)
2857         {
2858         case 'b':
2859           copy_byte = atoi (optarg);
2860           if (copy_byte < 0)
2861             fatal (_("byte number must be non-negative"));
2862           break;
2863
2864         case 'B':
2865           binary_architecture = optarg;
2866           break;
2867
2868         case 'i':
2869           interleave = atoi (optarg);
2870           if (interleave < 1)
2871             fatal (_("interleave must be positive"));
2872           break;
2873
2874         case 'I':
2875         case 's':               /* "source" - 'I' is preferred */
2876           input_target = optarg;
2877           break;
2878
2879         case 'O':
2880         case 'd':               /* "destination" - 'O' is preferred */
2881           output_target = optarg;
2882           break;
2883
2884         case 'F':
2885           input_target = output_target = optarg;
2886           break;
2887
2888         case 'j':
2889           p = find_section_list (optarg, TRUE);
2890           if (p->remove)
2891             fatal (_("%s both copied and removed"), optarg);
2892           p->copy = TRUE;
2893           sections_copied = TRUE;
2894           break;
2895
2896         case 'R':
2897           p = find_section_list (optarg, TRUE);
2898           if (p->copy)
2899             fatal (_("%s both copied and removed"), optarg);
2900           p->remove = TRUE;
2901           sections_removed = TRUE;
2902           break;
2903
2904         case 'S':
2905           strip_symbols = STRIP_ALL;
2906           break;
2907
2908         case 'g':
2909           strip_symbols = STRIP_DEBUG;
2910           break;
2911
2912         case OPTION_STRIP_UNNEEDED:
2913           strip_symbols = STRIP_UNNEEDED;
2914           break;
2915
2916         case OPTION_ONLY_KEEP_DEBUG:
2917           strip_symbols = STRIP_NONDEBUG;
2918           break;
2919
2920         case OPTION_KEEP_FILE_SYMBOLS:
2921           keep_file_symbols = 1;
2922           break;
2923
2924         case OPTION_ADD_GNU_DEBUGLINK:
2925           gnu_debuglink_filename = optarg;
2926           break;
2927
2928         case 'K':
2929           add_specific_symbol (optarg, &keep_specific_list);
2930           break;
2931
2932         case 'N':
2933           add_specific_symbol (optarg, &strip_specific_list);
2934           break;
2935
2936         case OPTION_STRIP_UNNEEDED_SYMBOL:
2937           add_specific_symbol (optarg, &strip_unneeded_list);
2938           break;
2939
2940         case 'L':
2941           add_specific_symbol (optarg, &localize_specific_list);
2942           break;
2943
2944         case OPTION_GLOBALIZE_SYMBOL:
2945           add_specific_symbol (optarg, &globalize_specific_list);
2946           break;
2947
2948         case 'G':
2949           add_specific_symbol (optarg, &keepglobal_specific_list);
2950           break;
2951
2952         case 'W':
2953           add_specific_symbol (optarg, &weaken_specific_list);
2954           break;
2955
2956         case 'p':
2957           preserve_dates = TRUE;
2958           break;
2959
2960         case 'w':
2961           wildcard = TRUE;
2962           break;
2963
2964         case 'x':
2965           discard_locals = LOCALS_ALL;
2966           break;
2967
2968         case 'X':
2969           discard_locals = LOCALS_START_L;
2970           break;
2971
2972         case 'v':
2973           verbose = TRUE;
2974           break;
2975
2976         case 'V':
2977           show_version = TRUE;
2978           break;
2979
2980         case OPTION_FORMATS_INFO:
2981           formats_info = TRUE;
2982           break;
2983
2984         case OPTION_WEAKEN:
2985           weaken = TRUE;
2986           break;
2987
2988         case OPTION_ADD_SECTION:
2989           {
2990             const char *s;
2991             off_t size;
2992             struct section_add *pa;
2993             int len;
2994             char *name;
2995             FILE *f;
2996
2997             s = strchr (optarg, '=');
2998
2999             if (s == NULL)
3000               fatal (_("bad format for %s"), "--add-section");
3001
3002             size = get_file_size (s + 1);
3003             if (size < 1)
3004               {
3005                 status = 1;
3006                 break;
3007               }
3008
3009             pa = xmalloc (sizeof (struct section_add));
3010
3011             len = s - optarg;
3012             name = xmalloc (len + 1);
3013             strncpy (name, optarg, len);
3014             name[len] = '\0';
3015             pa->name = name;
3016
3017             pa->filename = s + 1;
3018             pa->size = size;
3019             pa->contents = xmalloc (size);
3020
3021             f = fopen (pa->filename, FOPEN_RB);
3022
3023             if (f == NULL)
3024               fatal (_("cannot open: %s: %s"),
3025                      pa->filename, strerror (errno));
3026
3027             if (fread (pa->contents, 1, pa->size, f) == 0
3028                 || ferror (f))
3029               fatal (_("%s: fread failed"), pa->filename);
3030
3031             fclose (f);
3032
3033             pa->next = add_sections;
3034             add_sections = pa;
3035           }
3036           break;
3037
3038         case OPTION_CHANGE_START:
3039           change_start = parse_vma (optarg, "--change-start");
3040           break;
3041
3042         case OPTION_CHANGE_SECTION_ADDRESS:
3043         case OPTION_CHANGE_SECTION_LMA:
3044         case OPTION_CHANGE_SECTION_VMA:
3045           {
3046             const char *s;
3047             int len;
3048             char *name;
3049             char *option = NULL;
3050             bfd_vma val;
3051             enum change_action what = CHANGE_IGNORE;
3052
3053             switch (c)
3054               {
3055               case OPTION_CHANGE_SECTION_ADDRESS:
3056                 option = "--change-section-address";
3057                 break;
3058               case OPTION_CHANGE_SECTION_LMA:
3059                 option = "--change-section-lma";
3060                 break;
3061               case OPTION_CHANGE_SECTION_VMA:
3062                 option = "--change-section-vma";
3063                 break;
3064               }
3065
3066             s = strchr (optarg, '=');
3067             if (s == NULL)
3068               {
3069                 s = strchr (optarg, '+');
3070                 if (s == NULL)
3071                   {
3072                     s = strchr (optarg, '-');
3073                     if (s == NULL)
3074                       fatal (_("bad format for %s"), option);
3075                   }
3076               }
3077
3078             len = s - optarg;
3079             name = xmalloc (len + 1);
3080             strncpy (name, optarg, len);
3081             name[len] = '\0';
3082
3083             p = find_section_list (name, TRUE);
3084
3085             val = parse_vma (s + 1, option);
3086
3087             switch (*s)
3088               {
3089               case '=': what = CHANGE_SET; break;
3090               case '-': val  = - val; /* Drop through.  */
3091               case '+': what = CHANGE_MODIFY; break;
3092               }
3093
3094             switch (c)
3095               {
3096               case OPTION_CHANGE_SECTION_ADDRESS:
3097                 p->change_vma = what;
3098                 p->vma_val    = val;
3099                 /* Drop through.  */
3100
3101               case OPTION_CHANGE_SECTION_LMA:
3102                 p->change_lma = what;
3103                 p->lma_val    = val;
3104                 break;
3105
3106               case OPTION_CHANGE_SECTION_VMA:
3107                 p->change_vma = what;
3108                 p->vma_val    = val;
3109                 break;
3110               }
3111           }
3112           break;
3113
3114         case OPTION_CHANGE_ADDRESSES:
3115           change_section_address = parse_vma (optarg, "--change-addresses");
3116           change_start = change_section_address;
3117           break;
3118
3119         case OPTION_CHANGE_WARNINGS:
3120           change_warn = TRUE;
3121           break;
3122
3123         case OPTION_CHANGE_LEADING_CHAR:
3124           change_leading_char = TRUE;
3125           break;
3126
3127         case OPTION_DEBUGGING:
3128           convert_debugging = TRUE;
3129           break;
3130
3131         case OPTION_GAP_FILL:
3132           {
3133             bfd_vma gap_fill_vma;
3134
3135             gap_fill_vma = parse_vma (optarg, "--gap-fill");
3136             gap_fill = (bfd_byte) gap_fill_vma;
3137             if ((bfd_vma) gap_fill != gap_fill_vma)
3138               {
3139                 char buff[20];
3140
3141                 sprintf_vma (buff, gap_fill_vma);
3142
3143                 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3144                            buff, gap_fill);
3145               }
3146             gap_fill_set = TRUE;
3147           }
3148           break;
3149
3150         case OPTION_NO_CHANGE_WARNINGS:
3151           change_warn = FALSE;
3152           break;
3153
3154         case OPTION_PAD_TO:
3155           pad_to = parse_vma (optarg, "--pad-to");
3156           pad_to_set = TRUE;
3157           break;
3158
3159         case OPTION_REMOVE_LEADING_CHAR:
3160           remove_leading_char = TRUE;
3161           break;
3162
3163         case OPTION_REDEFINE_SYM:
3164           {
3165             /* Push this redefinition onto redefine_symbol_list.  */
3166
3167             int len;
3168             const char *s;
3169             const char *nextarg;
3170             char *source, *target;
3171
3172             s = strchr (optarg, '=');
3173             if (s == NULL)
3174               fatal (_("bad format for %s"), "--redefine-sym");
3175
3176             len = s - optarg;
3177             source = xmalloc (len + 1);
3178             strncpy (source, optarg, len);
3179             source[len] = '\0';
3180
3181             nextarg = s + 1;
3182             len = strlen (nextarg);
3183             target = xmalloc (len + 1);
3184             strcpy (target, nextarg);
3185
3186             redefine_list_append ("--redefine-sym", source, target);
3187
3188             free (source);
3189             free (target);
3190           }
3191           break;
3192
3193         case OPTION_REDEFINE_SYMS:
3194           add_redefine_syms_file (optarg);
3195           break;
3196
3197         case OPTION_SET_SECTION_FLAGS:
3198           {
3199             const char *s;
3200             int len;
3201             char *name;
3202
3203             s = strchr (optarg, '=');
3204             if (s == NULL)
3205               fatal (_("bad format for %s"), "--set-section-flags");
3206
3207             len = s - optarg;
3208             name = xmalloc (len + 1);
3209             strncpy (name, optarg, len);
3210             name[len] = '\0';
3211
3212             p = find_section_list (name, TRUE);
3213
3214             p->set_flags = TRUE;
3215             p->flags = parse_flags (s + 1);
3216           }
3217           break;
3218
3219         case OPTION_RENAME_SECTION:
3220           {
3221             flagword flags;
3222             const char *eq, *fl;
3223             char *old_name;
3224             char *new_name;
3225             unsigned int len;
3226
3227             eq = strchr (optarg, '=');
3228             if (eq == NULL)
3229               fatal (_("bad format for %s"), "--rename-section");
3230
3231             len = eq - optarg;
3232             if (len == 0)
3233               fatal (_("bad format for %s"), "--rename-section");
3234
3235             old_name = xmalloc (len + 1);
3236             strncpy (old_name, optarg, len);
3237             old_name[len] = 0;
3238
3239             eq++;
3240             fl = strchr (eq, ',');
3241             if (fl)
3242               {
3243                 flags = parse_flags (fl + 1);
3244                 len = fl - eq;
3245               }
3246             else
3247               {
3248                 flags = -1;
3249                 len = strlen (eq);
3250               }
3251
3252             if (len == 0)
3253               fatal (_("bad format for %s"), "--rename-section");
3254
3255             new_name = xmalloc (len + 1);
3256             strncpy (new_name, eq, len);
3257             new_name[len] = 0;
3258
3259             add_section_rename (old_name, new_name, flags);
3260           }
3261           break;
3262
3263         case OPTION_SET_START:
3264           set_start = parse_vma (optarg, "--set-start");
3265           set_start_set = TRUE;
3266           break;
3267
3268         case OPTION_SREC_LEN:
3269           Chunk = parse_vma (optarg, "--srec-len");
3270           break;
3271
3272         case OPTION_SREC_FORCES3:
3273           S3Forced = TRUE;
3274           break;
3275
3276         case OPTION_STRIP_SYMBOLS:
3277           add_specific_symbols (optarg, &strip_specific_list);
3278           break;
3279
3280         case OPTION_STRIP_UNNEEDED_SYMBOLS:
3281           add_specific_symbols (optarg, &strip_unneeded_list);
3282           break;
3283
3284         case OPTION_KEEP_SYMBOLS:
3285           add_specific_symbols (optarg, &keep_specific_list);
3286           break;
3287
3288         case OPTION_LOCALIZE_HIDDEN:
3289           localize_hidden = TRUE;
3290           break;
3291
3292         case OPTION_LOCALIZE_SYMBOLS:
3293           add_specific_symbols (optarg, &localize_specific_list);
3294           break;
3295
3296         case OPTION_GLOBALIZE_SYMBOLS:
3297           add_specific_symbols (optarg, &globalize_specific_list);
3298           break;
3299
3300         case OPTION_KEEPGLOBAL_SYMBOLS:
3301           add_specific_symbols (optarg, &keepglobal_specific_list);
3302           break;
3303
3304         case OPTION_WEAKEN_SYMBOLS:
3305           add_specific_symbols (optarg, &weaken_specific_list);
3306           break;
3307
3308         case OPTION_ALT_MACH_CODE:
3309           use_alt_mach_code = strtoul (optarg, NULL, 0);
3310           if (use_alt_mach_code == 0)
3311             fatal (_("unable to parse alternative machine code"));
3312           break;
3313
3314         case OPTION_PREFIX_SYMBOLS:
3315           prefix_symbols_string = optarg;
3316           break;
3317
3318         case OPTION_PREFIX_SECTIONS:
3319           prefix_sections_string = optarg;
3320           break;
3321
3322         case OPTION_PREFIX_ALLOC_SECTIONS:
3323           prefix_alloc_sections_string = optarg;
3324           break;
3325
3326         case OPTION_READONLY_TEXT:
3327           bfd_flags_to_set |= WP_TEXT;
3328           bfd_flags_to_clear &= ~WP_TEXT;
3329           break;
3330
3331         case OPTION_WRITABLE_TEXT:
3332           bfd_flags_to_clear |= WP_TEXT;
3333           bfd_flags_to_set &= ~WP_TEXT;
3334           break;
3335
3336         case OPTION_PURE:
3337           bfd_flags_to_set |= D_PAGED;
3338           bfd_flags_to_clear &= ~D_PAGED;
3339           break;
3340
3341         case OPTION_IMPURE:
3342           bfd_flags_to_clear |= D_PAGED;
3343           bfd_flags_to_set &= ~D_PAGED;
3344           break;
3345
3346         case OPTION_EXTRACT_SYMBOL:
3347           extract_symbol = TRUE;
3348           break;
3349
3350         case OPTION_REVERSE_BYTES:
3351           {
3352             int prev = reverse_bytes;
3353
3354             reverse_bytes = atoi (optarg);
3355             if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
3356               fatal (_("number of bytes to reverse must be positive and even"));
3357
3358             if (prev && prev != reverse_bytes)
3359               non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3360                          prev);
3361             break;
3362           }
3363
3364         case 0:
3365           /* We've been given a long option.  */
3366           break;
3367
3368         case 'H':
3369         case 'h':
3370           copy_usage (stdout, 0);
3371
3372         default:
3373           copy_usage (stderr, 1);
3374         }
3375     }
3376
3377   if (formats_info)
3378     {
3379       display_info ();
3380       return 0;
3381     }
3382
3383   if (show_version)
3384     print_version ("objcopy");
3385
3386   if (copy_byte >= interleave)
3387     fatal (_("byte number must be less than interleave"));
3388
3389   if (optind == argc || optind + 2 < argc)
3390     copy_usage (stderr, 1);
3391
3392   input_filename = argv[optind];
3393   if (optind + 1 < argc)
3394     output_filename = argv[optind + 1];
3395
3396   /* Default is to strip no symbols.  */
3397   if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3398     strip_symbols = STRIP_NONE;
3399
3400   if (output_target == NULL)
3401     output_target = input_target;
3402
3403   if (binary_architecture != NULL)
3404     {
3405       if (input_target && strcmp (input_target, "binary") == 0)
3406         {
3407           const bfd_arch_info_type * temp_arch_info;
3408
3409           temp_arch_info = bfd_scan_arch (binary_architecture);
3410
3411           if (temp_arch_info != NULL)
3412             {
3413               bfd_external_binary_architecture = temp_arch_info->arch;
3414               bfd_external_machine             = temp_arch_info->mach;
3415             }
3416           else
3417             fatal (_("architecture %s unknown"), binary_architecture);
3418         }
3419       else
3420         {
3421           non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3422           non_fatal (_(" Argument %s ignored"), binary_architecture);
3423         }
3424     }
3425
3426   if (preserve_dates)
3427     if (stat (input_filename, & statbuf) < 0)
3428       fatal (_("warning: could not locate '%s'.  System error message: %s"),
3429              input_filename, strerror (errno));
3430
3431   /* If there is no destination file, or the source and destination files
3432      are the same, then create a temp and rename the result into the input.  */
3433   if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3434     tmpname = make_tempname (input_filename);
3435   else
3436     tmpname = output_filename;
3437
3438   if (tmpname == NULL)
3439     fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3440            input_filename, strerror (errno));
3441
3442   copy_file (input_filename, tmpname, input_target, output_target);
3443   if (status == 0)
3444     {
3445       if (preserve_dates)
3446         set_times (tmpname, &statbuf);
3447       if (tmpname != output_filename)
3448         smart_rename (tmpname, input_filename, preserve_dates);
3449     }
3450   else
3451     unlink_if_ordinary (tmpname);
3452
3453   if (change_warn)
3454     {
3455       for (p = change_sections; p != NULL; p = p->next)
3456         {
3457           if (! p->used)
3458             {
3459               if (p->change_vma != CHANGE_IGNORE)
3460                 {
3461                   char buff [20];
3462
3463                   sprintf_vma (buff, p->vma_val);
3464
3465                   /* xgettext:c-format */
3466                   non_fatal (_("%s %s%c0x%s never used"),
3467                              "--change-section-vma",
3468                              p->name,
3469                              p->change_vma == CHANGE_SET ? '=' : '+',
3470                              buff);
3471                 }
3472
3473               if (p->change_lma != CHANGE_IGNORE)
3474                 {
3475                   char buff [20];
3476
3477                   sprintf_vma (buff, p->lma_val);
3478
3479                   /* xgettext:c-format */
3480                   non_fatal (_("%s %s%c0x%s never used"),
3481                              "--change-section-lma",
3482                              p->name,
3483                              p->change_lma == CHANGE_SET ? '=' : '+',
3484                              buff);
3485                 }
3486             }
3487         }
3488     }
3489
3490   return 0;
3491 }
3492
3493 int
3494 main (int argc, char *argv[])
3495 {
3496 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3497   setlocale (LC_MESSAGES, "");
3498 #endif
3499 #if defined (HAVE_SETLOCALE)
3500   setlocale (LC_CTYPE, "");
3501 #endif
3502   bindtextdomain (PACKAGE, LOCALEDIR);
3503   textdomain (PACKAGE);
3504
3505   program_name = argv[0];
3506   xmalloc_set_program_name (program_name);
3507
3508   START_PROGRESS (program_name, 0);
3509
3510   expandargv (&argc, &argv);
3511
3512   strip_symbols = STRIP_UNDEF;
3513   discard_locals = LOCALS_UNDEF;
3514
3515   bfd_init ();
3516   set_default_bfd_target ();
3517
3518   if (is_strip < 0)
3519     {
3520       int i = strlen (program_name);
3521 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3522       /* Drop the .exe suffix, if any.  */
3523       if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3524         {
3525           i -= 4;
3526           program_name[i] = '\0';
3527         }
3528 #endif
3529       is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3530     }
3531
3532   if (is_strip)
3533     strip_main (argc, argv);
3534   else
3535     copy_main (argc, argv);
3536
3537   END_PROGRESS (program_name);
3538
3539   return status;
3540 }