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