opcodes/
[binutils.git] / binutils / objcopy.c
blob0b2badab5fa22114fcd8112943669e206b3e8550
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
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
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 2 of the License, or
11 (at your option) any later version.
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.
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. */
23 #include "bfd.h"
24 #include "progress.h"
25 #include "bucomm.h"
26 #include "getopt.h"
27 #include "libiberty.h"
28 #include "budbg.h"
29 #include "filenames.h"
30 #include "fnmatch.h"
31 #include "elf-bfd.h"
32 #include <sys/stat.h>
33 #include "libbfd.h"
35 /* A list of symbols to explicitly strip out, or to keep. A linked
36 list is good enough for a small number from the command line, but
37 this will slow things down a lot if many symbols are being
38 deleted. */
40 struct symlist
42 const char *name;
43 struct symlist *next;
46 /* A list to support redefine_sym. */
47 struct redefine_node
49 char *source;
50 char *target;
51 struct redefine_node *next;
54 typedef struct section_rename
56 const char * old_name;
57 const char * new_name;
58 flagword flags;
59 struct section_rename * next;
61 section_rename;
63 /* List of sections to be renamed. */
64 static section_rename *section_rename_list;
66 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
68 static asymbol **isympp = NULL; /* Input symbols. */
69 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
71 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
72 static int copy_byte = -1;
73 static int interleave = 4;
75 static bfd_boolean verbose; /* Print file and target names. */
76 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
77 static int status = 0; /* Exit status. */
79 enum strip_action
81 STRIP_UNDEF,
82 STRIP_NONE, /* Don't strip. */
83 STRIP_DEBUG, /* Strip all debugger symbols. */
84 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
85 STRIP_NONDEBUG, /* Strip everything but debug info. */
86 STRIP_ALL /* Strip all symbols. */
89 /* Which symbols to remove. */
90 static enum strip_action strip_symbols;
92 enum locals_action
94 LOCALS_UNDEF,
95 LOCALS_START_L, /* Discard locals starting with L. */
96 LOCALS_ALL /* Discard all locals. */
99 /* Which local symbols to remove. Overrides STRIP_ALL. */
100 static enum locals_action discard_locals;
102 /* What kind of change to perform. */
103 enum change_action
105 CHANGE_IGNORE,
106 CHANGE_MODIFY,
107 CHANGE_SET
110 /* Structure used to hold lists of sections and actions to take. */
111 struct section_list
113 struct section_list * next; /* Next section to change. */
114 const char * name; /* Section name. */
115 bfd_boolean used; /* Whether this entry was used. */
116 bfd_boolean remove; /* Whether to remove this section. */
117 bfd_boolean copy; /* Whether to copy this section. */
118 enum change_action change_vma;/* Whether to change or set VMA. */
119 bfd_vma vma_val; /* Amount to change by or set to. */
120 enum change_action change_lma;/* Whether to change or set LMA. */
121 bfd_vma lma_val; /* Amount to change by or set to. */
122 bfd_boolean set_flags; /* Whether to set the section flags. */
123 flagword flags; /* What to set the section flags to. */
126 static struct section_list *change_sections;
128 /* TRUE if some sections are to be removed. */
129 static bfd_boolean sections_removed;
131 /* TRUE if only some sections are to be copied. */
132 static bfd_boolean sections_copied;
134 /* Changes to the start address. */
135 static bfd_vma change_start = 0;
136 static bfd_boolean set_start_set = FALSE;
137 static bfd_vma set_start;
139 /* Changes to section addresses. */
140 static bfd_vma change_section_address = 0;
142 /* Filling gaps between sections. */
143 static bfd_boolean gap_fill_set = FALSE;
144 static bfd_byte gap_fill = 0;
146 /* Pad to a given address. */
147 static bfd_boolean pad_to_set = FALSE;
148 static bfd_vma pad_to;
150 /* Use alternative machine code? */
151 static unsigned long use_alt_mach_code = 0;
153 /* Output BFD flags user wants to set or clear */
154 static flagword bfd_flags_to_set;
155 static flagword bfd_flags_to_clear;
157 /* List of sections to add. */
158 struct section_add
160 /* Next section to add. */
161 struct section_add *next;
162 /* Name of section to add. */
163 const char *name;
164 /* Name of file holding section contents. */
165 const char *filename;
166 /* Size of file. */
167 size_t size;
168 /* Contents of file. */
169 bfd_byte *contents;
170 /* BFD section, after it has been added. */
171 asection *section;
174 /* List of sections to add to the output BFD. */
175 static struct section_add *add_sections;
177 /* If non-NULL the argument to --add-gnu-debuglink.
178 This should be the filename to store in the .gnu_debuglink section. */
179 static const char * gnu_debuglink_filename = NULL;
181 /* Whether to convert debugging information. */
182 static bfd_boolean convert_debugging = FALSE;
184 /* Whether to change the leading character in symbol names. */
185 static bfd_boolean change_leading_char = FALSE;
187 /* Whether to remove the leading character from global symbol names. */
188 static bfd_boolean remove_leading_char = FALSE;
190 /* Whether to permit wildcard in symbol comparison. */
191 static bfd_boolean wildcard = FALSE;
193 /* True if --localize-hidden is in effect. */
194 static bfd_boolean localize_hidden = FALSE;
196 /* List of symbols to strip, keep, localize, keep-global, weaken,
197 or redefine. */
198 static struct symlist *strip_specific_list = NULL;
199 static struct symlist *strip_unneeded_list = NULL;
200 static struct symlist *keep_specific_list = NULL;
201 static struct symlist *localize_specific_list = NULL;
202 static struct symlist *globalize_specific_list = NULL;
203 static struct symlist *keepglobal_specific_list = NULL;
204 static struct symlist *weaken_specific_list = NULL;
205 static struct redefine_node *redefine_sym_list = NULL;
207 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
208 static bfd_boolean weaken = FALSE;
210 /* If this is TRUE, we retain BSF_FILE symbols. */
211 static bfd_boolean keep_file_symbols = FALSE;
213 /* Prefix symbols/sections. */
214 static char *prefix_symbols_string = 0;
215 static char *prefix_sections_string = 0;
216 static char *prefix_alloc_sections_string = 0;
218 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
219 enum command_line_switch
221 OPTION_ADD_SECTION=150,
222 OPTION_CHANGE_ADDRESSES,
223 OPTION_CHANGE_LEADING_CHAR,
224 OPTION_CHANGE_START,
225 OPTION_CHANGE_SECTION_ADDRESS,
226 OPTION_CHANGE_SECTION_LMA,
227 OPTION_CHANGE_SECTION_VMA,
228 OPTION_CHANGE_WARNINGS,
229 OPTION_DEBUGGING,
230 OPTION_GAP_FILL,
231 OPTION_NO_CHANGE_WARNINGS,
232 OPTION_PAD_TO,
233 OPTION_REMOVE_LEADING_CHAR,
234 OPTION_SET_SECTION_FLAGS,
235 OPTION_SET_START,
236 OPTION_STRIP_UNNEEDED,
237 OPTION_WEAKEN,
238 OPTION_REDEFINE_SYM,
239 OPTION_REDEFINE_SYMS,
240 OPTION_SREC_LEN,
241 OPTION_SREC_FORCES3,
242 OPTION_STRIP_SYMBOLS,
243 OPTION_STRIP_UNNEEDED_SYMBOL,
244 OPTION_STRIP_UNNEEDED_SYMBOLS,
245 OPTION_KEEP_SYMBOLS,
246 OPTION_LOCALIZE_HIDDEN,
247 OPTION_LOCALIZE_SYMBOLS,
248 OPTION_GLOBALIZE_SYMBOL,
249 OPTION_GLOBALIZE_SYMBOLS,
250 OPTION_KEEPGLOBAL_SYMBOLS,
251 OPTION_WEAKEN_SYMBOLS,
252 OPTION_RENAME_SECTION,
253 OPTION_ALT_MACH_CODE,
254 OPTION_PREFIX_SYMBOLS,
255 OPTION_PREFIX_SECTIONS,
256 OPTION_PREFIX_ALLOC_SECTIONS,
257 OPTION_FORMATS_INFO,
258 OPTION_ADD_GNU_DEBUGLINK,
259 OPTION_ONLY_KEEP_DEBUG,
260 OPTION_KEEP_FILE_SYMBOLS,
261 OPTION_READONLY_TEXT,
262 OPTION_WRITABLE_TEXT,
263 OPTION_PURE,
264 OPTION_IMPURE
267 /* Options to handle if running as "strip". */
269 static struct option strip_options[] =
271 {"discard-all", no_argument, 0, 'x'},
272 {"discard-locals", no_argument, 0, 'X'},
273 {"format", required_argument, 0, 'F'}, /* Obsolete */
274 {"help", no_argument, 0, 'h'},
275 {"info", no_argument, 0, OPTION_FORMATS_INFO},
276 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
277 {"input-target", required_argument, 0, 'I'},
278 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
279 {"keep-symbol", required_argument, 0, 'K'},
280 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
281 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
282 {"output-target", required_argument, 0, 'O'},
283 {"output-file", required_argument, 0, 'o'},
284 {"preserve-dates", no_argument, 0, 'p'},
285 {"remove-section", required_argument, 0, 'R'},
286 {"strip-all", no_argument, 0, 's'},
287 {"strip-debug", no_argument, 0, 'S'},
288 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
289 {"strip-symbol", required_argument, 0, 'N'},
290 {"target", required_argument, 0, 'F'},
291 {"verbose", no_argument, 0, 'v'},
292 {"version", no_argument, 0, 'V'},
293 {"wildcard", no_argument, 0, 'w'},
294 {0, no_argument, 0, 0}
297 /* Options to handle if running as "objcopy". */
299 static struct option copy_options[] =
301 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
302 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
303 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
304 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
305 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
306 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
307 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
308 {"binary-architecture", required_argument, 0, 'B'},
309 {"byte", required_argument, 0, 'b'},
310 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
311 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
312 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
313 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
314 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
315 {"change-start", required_argument, 0, OPTION_CHANGE_START},
316 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
317 {"debugging", no_argument, 0, OPTION_DEBUGGING},
318 {"discard-all", no_argument, 0, 'x'},
319 {"discard-locals", no_argument, 0, 'X'},
320 {"format", required_argument, 0, 'F'}, /* Obsolete */
321 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
322 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
323 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
324 {"help", no_argument, 0, 'h'},
325 {"impure", no_argument, 0, OPTION_IMPURE},
326 {"info", no_argument, 0, OPTION_FORMATS_INFO},
327 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
328 {"input-target", required_argument, 0, 'I'},
329 {"interleave", required_argument, 0, 'i'},
330 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
331 {"keep-global-symbol", required_argument, 0, 'G'},
332 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
333 {"keep-symbol", required_argument, 0, 'K'},
334 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
335 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
336 {"localize-symbol", required_argument, 0, 'L'},
337 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
338 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
339 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
340 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
341 {"only-section", required_argument, 0, 'j'},
342 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
343 {"output-target", required_argument, 0, 'O'},
344 {"pad-to", required_argument, 0, OPTION_PAD_TO},
345 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
346 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
347 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
348 {"preserve-dates", no_argument, 0, 'p'},
349 {"pure", no_argument, 0, OPTION_PURE},
350 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
351 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
352 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
353 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
354 {"remove-section", required_argument, 0, 'R'},
355 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
356 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
357 {"set-start", required_argument, 0, OPTION_SET_START},
358 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
359 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
360 {"strip-all", no_argument, 0, 'S'},
361 {"strip-debug", no_argument, 0, 'g'},
362 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
363 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
364 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
365 {"strip-symbol", required_argument, 0, 'N'},
366 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
367 {"target", required_argument, 0, 'F'},
368 {"verbose", no_argument, 0, 'v'},
369 {"version", no_argument, 0, 'V'},
370 {"weaken", no_argument, 0, OPTION_WEAKEN},
371 {"weaken-symbol", required_argument, 0, 'W'},
372 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
373 {"wildcard", no_argument, 0, 'w'},
374 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
375 {0, no_argument, 0, 0}
378 /* IMPORTS */
379 extern char *program_name;
381 /* This flag distinguishes between strip and objcopy:
382 1 means this is 'strip'; 0 means this is 'objcopy'.
383 -1 means if we should use argv[0] to decide. */
384 extern int is_strip;
386 /* The maximum length of an S record. This variable is declared in srec.c
387 and can be modified by the --srec-len parameter. */
388 extern unsigned int Chunk;
390 /* Restrict the generation of Srecords to type S3 only.
391 This variable is declare in bfd/srec.c and can be toggled
392 on by the --srec-forceS3 command line switch. */
393 extern bfd_boolean S3Forced;
395 /* Defined in bfd/binary.c. Used to set architecture and machine of input
396 binary files. */
397 extern enum bfd_architecture bfd_external_binary_architecture;
398 extern unsigned long bfd_external_machine;
400 /* Forward declarations. */
401 static void setup_section (bfd *, asection *, void *);
402 static void setup_bfd_headers (bfd *, bfd *);
403 static void copy_section (bfd *, asection *, void *);
404 static void get_sections (bfd *, asection *, void *);
405 static int compare_section_lma (const void *, const void *);
406 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
407 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
408 static const char *lookup_sym_redefinition (const char *);
410 static void
411 copy_usage (FILE *stream, int exit_status)
413 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
414 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
415 fprintf (stream, _(" The options are:\n"));
416 fprintf (stream, _("\
417 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
418 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
419 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
420 -F --target <bfdname> Set both input and output format to <bfdname>\n\
421 --debugging Convert debugging information, if possible\n\
422 -p --preserve-dates Copy modified/access timestamps to the output\n\
423 -j --only-section <name> Only copy section <name> into the output\n\
424 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
425 -R --remove-section <name> Remove section <name> from the output\n\
426 -S --strip-all Remove all symbol and relocation information\n\
427 -g --strip-debug Remove all debugging symbols & sections\n\
428 --strip-unneeded Remove all symbols not needed by relocations\n\
429 -N --strip-symbol <name> Do not copy symbol <name>\n\
430 --strip-unneeded-symbol <name>\n\
431 Do not copy symbol <name> unless needed by\n\
432 relocations\n\
433 --only-keep-debug Strip everything but the debug information\n\
434 -K --keep-symbol <name> Do not strip symbol <name>\n\
435 --keep-file-symbols Do not strip file symbol(s)\n\
436 --localize-hidden Turn all ELF hidden symbols into locals\n\
437 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
438 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
439 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
440 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
441 --weaken Force all global symbols to be marked as weak\n\
442 -w --wildcard Permit wildcard in symbol comparison\n\
443 -x --discard-all Remove all non-global symbols\n\
444 -X --discard-locals Remove any compiler-generated symbols\n\
445 -i --interleave <number> Only copy one out of every <number> bytes\n\
446 -b --byte <num> Select byte <num> in every interleaved block\n\
447 --gap-fill <val> Fill gaps between sections with <val>\n\
448 --pad-to <addr> Pad the last section up to address <addr>\n\
449 --set-start <addr> Set the start address to <addr>\n\
450 {--change-start|--adjust-start} <incr>\n\
451 Add <incr> to the start address\n\
452 {--change-addresses|--adjust-vma} <incr>\n\
453 Add <incr> to LMA, VMA and start addresses\n\
454 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
455 Change LMA and VMA of section <name> by <val>\n\
456 --change-section-lma <name>{=|+|-}<val>\n\
457 Change the LMA of section <name> by <val>\n\
458 --change-section-vma <name>{=|+|-}<val>\n\
459 Change the VMA of section <name> by <val>\n\
460 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
461 Warn if a named section does not exist\n\
462 --set-section-flags <name>=<flags>\n\
463 Set section <name>'s properties to <flags>\n\
464 --add-section <name>=<file> Add section <name> found in <file> to output\n\
465 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
466 --change-leading-char Force output format's leading character style\n\
467 --remove-leading-char Remove leading character from global symbols\n\
468 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
469 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
470 listed in <file>\n\
471 --srec-len <number> Restrict the length of generated Srecords\n\
472 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
473 --strip-symbols <file> -N for all symbols listed in <file>\n\
474 --strip-unneeded-symbols <file>\n\
475 --strip-unneeded-symbol for all symbols listed\n\
476 in <file>\n\
477 --keep-symbols <file> -K for all symbols listed in <file>\n\
478 --localize-symbols <file> -L for all symbols listed in <file>\n\
479 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
480 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
481 --weaken-symbols <file> -W for all symbols listed in <file>\n\
482 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
483 --writable-text Mark the output text as writable\n\
484 --readonly-text Make the output text write protected\n\
485 --pure Mark the output file as demand paged\n\
486 --impure Mark the output file as impure\n\
487 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
488 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
489 --prefix-alloc-sections <prefix>\n\
490 Add <prefix> to start of every allocatable\n\
491 section name\n\
492 -v --verbose List all object files modified\n\
493 @<file> Read options from <file>\n\
494 -V --version Display this program's version number\n\
495 -h --help Display this output\n\
496 --info List object formats & architectures supported\n\
497 "));
498 list_supported_targets (program_name, stream);
499 if (exit_status == 0)
500 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
501 exit (exit_status);
504 static void
505 strip_usage (FILE *stream, int exit_status)
507 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
508 fprintf (stream, _(" Removes symbols and sections from files\n"));
509 fprintf (stream, _(" The options are:\n"));
510 fprintf (stream, _("\
511 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
512 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
513 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
514 -p --preserve-dates Copy modified/access timestamps to the output\n\
515 -R --remove-section=<name> Remove section <name> from the output\n\
516 -s --strip-all Remove all symbol and relocation information\n\
517 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
518 --strip-unneeded Remove all symbols not needed by relocations\n\
519 --only-keep-debug Strip everything but the debug information\n\
520 -N --strip-symbol=<name> Do not copy symbol <name>\n\
521 -K --keep-symbol=<name> Do not strip symbol <name>\n\
522 --keep-file-symbols Do not strip file symbol(s)\n\
523 -w --wildcard Permit wildcard in symbol comparison\n\
524 -x --discard-all Remove all non-global symbols\n\
525 -X --discard-locals Remove any compiler-generated symbols\n\
526 -v --verbose List all object files modified\n\
527 -V --version Display this program's version number\n\
528 -h --help Display this output\n\
529 --info List object formats & architectures supported\n\
530 -o <file> Place stripped output into <file>\n\
531 "));
533 list_supported_targets (program_name, stream);
534 if (exit_status == 0)
535 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
536 exit (exit_status);
539 /* Parse section flags into a flagword, with a fatal error if the
540 string can't be parsed. */
542 static flagword
543 parse_flags (const char *s)
545 flagword ret;
546 const char *snext;
547 int len;
549 ret = SEC_NO_FLAGS;
553 snext = strchr (s, ',');
554 if (snext == NULL)
555 len = strlen (s);
556 else
558 len = snext - s;
559 ++snext;
562 if (0) ;
563 #define PARSE_FLAG(fname,fval) \
564 else if (strncasecmp (fname, s, len) == 0) ret |= fval
565 PARSE_FLAG ("alloc", SEC_ALLOC);
566 PARSE_FLAG ("load", SEC_LOAD);
567 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
568 PARSE_FLAG ("readonly", SEC_READONLY);
569 PARSE_FLAG ("debug", SEC_DEBUGGING);
570 PARSE_FLAG ("code", SEC_CODE);
571 PARSE_FLAG ("data", SEC_DATA);
572 PARSE_FLAG ("rom", SEC_ROM);
573 PARSE_FLAG ("share", SEC_COFF_SHARED);
574 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
575 #undef PARSE_FLAG
576 else
578 char *copy;
580 copy = xmalloc (len + 1);
581 strncpy (copy, s, len);
582 copy[len] = '\0';
583 non_fatal (_("unrecognized section flag `%s'"), copy);
584 fatal (_("supported flags: %s"),
585 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
588 s = snext;
590 while (s != NULL);
592 return ret;
595 /* Find and optionally add an entry in the change_sections list. */
597 static struct section_list *
598 find_section_list (const char *name, bfd_boolean add)
600 struct section_list *p;
602 for (p = change_sections; p != NULL; p = p->next)
603 if (strcmp (p->name, name) == 0)
604 return p;
606 if (! add)
607 return NULL;
609 p = xmalloc (sizeof (struct section_list));
610 p->name = name;
611 p->used = FALSE;
612 p->remove = FALSE;
613 p->copy = FALSE;
614 p->change_vma = CHANGE_IGNORE;
615 p->change_lma = CHANGE_IGNORE;
616 p->vma_val = 0;
617 p->lma_val = 0;
618 p->set_flags = FALSE;
619 p->flags = 0;
621 p->next = change_sections;
622 change_sections = p;
624 return p;
627 /* Add a symbol to strip_specific_list. */
629 static void
630 add_specific_symbol (const char *name, struct symlist **list)
632 struct symlist *tmp_list;
634 tmp_list = xmalloc (sizeof (struct symlist));
635 tmp_list->name = name;
636 tmp_list->next = *list;
637 *list = tmp_list;
640 /* Add symbols listed in `filename' to strip_specific_list. */
642 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
643 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
645 static void
646 add_specific_symbols (const char *filename, struct symlist **list)
648 off_t size;
649 FILE * f;
650 char * line;
651 char * buffer;
652 unsigned int line_count;
654 size = get_file_size (filename);
655 if (size == 0)
657 status = 1;
658 return;
661 buffer = xmalloc (size + 2);
662 f = fopen (filename, FOPEN_RT);
663 if (f == NULL)
664 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
666 if (fread (buffer, 1, size, f) == 0 || ferror (f))
667 fatal (_("%s: fread failed"), filename);
669 fclose (f);
670 buffer [size] = '\n';
671 buffer [size + 1] = '\0';
673 line_count = 1;
675 for (line = buffer; * line != '\0'; line ++)
677 char * eol;
678 char * name;
679 char * name_end;
680 int finished = FALSE;
682 for (eol = line;; eol ++)
684 switch (* eol)
686 case '\n':
687 * eol = '\0';
688 /* Cope with \n\r. */
689 if (eol[1] == '\r')
690 ++ eol;
691 finished = TRUE;
692 break;
694 case '\r':
695 * eol = '\0';
696 /* Cope with \r\n. */
697 if (eol[1] == '\n')
698 ++ eol;
699 finished = TRUE;
700 break;
702 case 0:
703 finished = TRUE;
704 break;
706 case '#':
707 /* Line comment, Terminate the line here, in case a
708 name is present and then allow the rest of the
709 loop to find the real end of the line. */
710 * eol = '\0';
711 break;
713 default:
714 break;
717 if (finished)
718 break;
721 /* A name may now exist somewhere between 'line' and 'eol'.
722 Strip off leading whitespace and trailing whitespace,
723 then add it to the list. */
724 for (name = line; IS_WHITESPACE (* name); name ++)
726 for (name_end = name;
727 (! IS_WHITESPACE (* name_end))
728 && (! IS_LINE_TERMINATOR (* name_end));
729 name_end ++)
732 if (! IS_LINE_TERMINATOR (* name_end))
734 char * extra;
736 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
739 if (! IS_LINE_TERMINATOR (* extra))
740 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
741 filename, line_count);
744 * name_end = '\0';
746 if (name_end > name)
747 add_specific_symbol (name, list);
749 /* Advance line pointer to end of line. The 'eol ++' in the for
750 loop above will then advance us to the start of the next line. */
751 line = eol;
752 line_count ++;
756 /* See whether a symbol should be stripped or kept based on
757 strip_specific_list and keep_symbols. */
759 static bfd_boolean
760 is_specified_symbol (const char *name, struct symlist *list)
762 struct symlist *tmp_list;
764 if (wildcard)
766 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
767 if (*(tmp_list->name) != '!')
769 if (!fnmatch (tmp_list->name, name, 0))
770 return TRUE;
772 else
774 if (fnmatch (tmp_list->name + 1, name, 0))
775 return TRUE;
778 else
780 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
781 if (strcmp (name, tmp_list->name) == 0)
782 return TRUE;
785 return FALSE;
788 /* Return a pointer to the symbol used as a signature for GROUP. */
790 static asymbol *
791 group_signature (asection *group)
793 bfd *abfd = group->owner;
794 Elf_Internal_Shdr *ghdr;
796 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
797 return NULL;
799 ghdr = &elf_section_data (group)->this_hdr;
800 if (ghdr->sh_link < elf_numsections (abfd))
802 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
803 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
805 if (symhdr->sh_type == SHT_SYMTAB
806 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
807 return isympp[ghdr->sh_info - 1];
809 return NULL;
812 /* See if a section is being removed. */
814 static bfd_boolean
815 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
817 if (sections_removed || sections_copied)
819 struct section_list *p;
821 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
823 if (sections_removed && p != NULL && p->remove)
824 return TRUE;
825 if (sections_copied && (p == NULL || ! p->copy))
826 return TRUE;
829 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
831 if (strip_symbols == STRIP_DEBUG
832 || strip_symbols == STRIP_UNNEEDED
833 || strip_symbols == STRIP_ALL
834 || discard_locals == LOCALS_ALL
835 || convert_debugging)
836 return TRUE;
838 if (strip_symbols == STRIP_NONDEBUG)
839 return FALSE;
842 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
844 asymbol *gsym;
845 const char *gname;
847 /* PR binutils/3166
848 Group sections look like debugging sections but they are not.
849 (They have a non-zero size but they are not ALLOCated). */
850 if (strip_symbols == STRIP_NONDEBUG)
851 return TRUE;
853 /* PR binutils/3181
854 If we are going to strip the group signature symbol, then
855 strip the group section too. */
856 gsym = group_signature (sec);
857 if (gsym != NULL)
858 gname = gsym->name;
859 else
860 gname = sec->name;
861 if ((strip_symbols == STRIP_ALL
862 && !is_specified_symbol (gname, keep_specific_list))
863 || is_specified_symbol (gname, strip_specific_list))
864 return TRUE;
867 return FALSE;
870 /* Return true if SYM is a hidden symbol. */
872 static bfd_boolean
873 is_hidden_symbol (asymbol *sym)
875 elf_symbol_type *elf_sym;
877 elf_sym = elf_symbol_from (sym->the_bfd, sym);
878 if (elf_sym != NULL)
879 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
881 case STV_HIDDEN:
882 case STV_INTERNAL:
883 return TRUE;
885 return FALSE;
888 /* Choose which symbol entries to copy; put the result in OSYMS.
889 We don't copy in place, because that confuses the relocs.
890 Return the number of symbols to print. */
892 static unsigned int
893 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
894 asymbol **isyms, long symcount)
896 asymbol **from = isyms, **to = osyms;
897 long src_count = 0, dst_count = 0;
898 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
899 == HAS_RELOC;
901 for (; src_count < symcount; src_count++)
903 asymbol *sym = from[src_count];
904 flagword flags = sym->flags;
905 char *name = (char *) bfd_asymbol_name (sym);
906 int keep;
907 bfd_boolean undefined;
908 bfd_boolean rem_leading_char;
909 bfd_boolean add_leading_char;
911 undefined = bfd_is_und_section (bfd_get_section (sym));
913 if (redefine_sym_list)
915 char *old_name, *new_name;
917 old_name = (char *) bfd_asymbol_name (sym);
918 new_name = (char *) lookup_sym_redefinition (old_name);
919 bfd_asymbol_name (sym) = new_name;
920 name = new_name;
923 /* Check if we will remove the current leading character. */
924 rem_leading_char =
925 (name[0] == bfd_get_symbol_leading_char (abfd))
926 && (change_leading_char
927 || (remove_leading_char
928 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
929 || undefined
930 || bfd_is_com_section (bfd_get_section (sym)))));
932 /* Check if we will add a new leading character. */
933 add_leading_char =
934 change_leading_char
935 && (bfd_get_symbol_leading_char (obfd) != '\0')
936 && (bfd_get_symbol_leading_char (abfd) == '\0'
937 || (name[0] == bfd_get_symbol_leading_char (abfd)));
939 /* Short circuit for change_leading_char if we can do it in-place. */
940 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
942 name[0] = bfd_get_symbol_leading_char (obfd);
943 bfd_asymbol_name (sym) = name;
944 rem_leading_char = FALSE;
945 add_leading_char = FALSE;
948 /* Remove leading char. */
949 if (rem_leading_char)
950 bfd_asymbol_name (sym) = ++name;
952 /* Add new leading char and/or prefix. */
953 if (add_leading_char || prefix_symbols_string)
955 char *n, *ptr;
957 ptr = n = xmalloc (1 + strlen (prefix_symbols_string)
958 + strlen (name) + 1);
959 if (add_leading_char)
960 *ptr++ = bfd_get_symbol_leading_char (obfd);
962 if (prefix_symbols_string)
964 strcpy (ptr, prefix_symbols_string);
965 ptr += strlen (prefix_symbols_string);
968 strcpy (ptr, name);
969 bfd_asymbol_name (sym) = n;
970 name = n;
973 if (strip_symbols == STRIP_ALL)
974 keep = 0;
975 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
976 || ((flags & BSF_SECTION_SYM) != 0
977 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
978 & BSF_KEEP) != 0))
979 keep = 1;
980 else if (relocatable /* Relocatable file. */
981 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
982 keep = 1;
983 else if (bfd_decode_symclass (sym) == 'I')
984 /* Global symbols in $idata sections need to be retained
985 even if relocatable is FALSE. External users of the
986 library containing the $idata section may reference these
987 symbols. */
988 keep = 1;
989 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
990 || (flags & BSF_WEAK) != 0
991 || undefined
992 || bfd_is_com_section (bfd_get_section (sym)))
993 keep = strip_symbols != STRIP_UNNEEDED;
994 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
995 keep = (strip_symbols != STRIP_DEBUG
996 && strip_symbols != STRIP_UNNEEDED
997 && ! convert_debugging);
998 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
999 /* COMDAT sections store special information in local
1000 symbols, so we cannot risk stripping any of them. */
1001 keep = 1;
1002 else /* Local symbol. */
1003 keep = (strip_symbols != STRIP_UNNEEDED
1004 && (discard_locals != LOCALS_ALL
1005 && (discard_locals != LOCALS_START_L
1006 || ! bfd_is_local_label (abfd, sym))));
1008 if (keep && is_specified_symbol (name, strip_specific_list))
1009 keep = 0;
1010 if (keep
1011 && !(flags & BSF_KEEP)
1012 && is_specified_symbol (name, strip_unneeded_list))
1013 keep = 0;
1014 if (!keep
1015 && ((keep_file_symbols && (flags & BSF_FILE))
1016 || is_specified_symbol (name, keep_specific_list)))
1017 keep = 1;
1018 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1019 keep = 0;
1021 if (keep)
1023 if ((flags & BSF_GLOBAL) != 0
1024 && (weaken || is_specified_symbol (name, weaken_specific_list)))
1026 sym->flags &= ~ BSF_GLOBAL;
1027 sym->flags |= BSF_WEAK;
1030 if (!undefined
1031 && (flags & (BSF_GLOBAL | BSF_WEAK))
1032 && (is_specified_symbol (name, localize_specific_list)
1033 || (keepglobal_specific_list != NULL
1034 && ! is_specified_symbol (name, keepglobal_specific_list))
1035 || (localize_hidden && is_hidden_symbol (sym))))
1037 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1038 sym->flags |= BSF_LOCAL;
1041 if (!undefined
1042 && (flags & BSF_LOCAL)
1043 && is_specified_symbol (name, globalize_specific_list))
1045 sym->flags &= ~ BSF_LOCAL;
1046 sym->flags |= BSF_GLOBAL;
1049 to[dst_count++] = sym;
1053 to[dst_count] = NULL;
1055 return dst_count;
1058 /* Find the redefined name of symbol SOURCE. */
1060 static const char *
1061 lookup_sym_redefinition (const char *source)
1063 struct redefine_node *list;
1065 for (list = redefine_sym_list; list != NULL; list = list->next)
1066 if (strcmp (source, list->source) == 0)
1067 return list->target;
1069 return source;
1072 /* Add a node to a symbol redefine list. */
1074 static void
1075 redefine_list_append (const char *cause, const char *source, const char *target)
1077 struct redefine_node **p;
1078 struct redefine_node *list;
1079 struct redefine_node *new_node;
1081 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1083 if (strcmp (source, list->source) == 0)
1084 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1085 cause, source);
1087 if (strcmp (target, list->target) == 0)
1088 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1089 cause, target);
1092 new_node = xmalloc (sizeof (struct redefine_node));
1094 new_node->source = strdup (source);
1095 new_node->target = strdup (target);
1096 new_node->next = NULL;
1098 *p = new_node;
1101 /* Handle the --redefine-syms option. Read lines containing "old new"
1102 from the file, and add them to the symbol redefine list. */
1104 static void
1105 add_redefine_syms_file (const char *filename)
1107 FILE *file;
1108 char *buf;
1109 size_t bufsize;
1110 size_t len;
1111 size_t outsym_off;
1112 int c, lineno;
1114 file = fopen (filename, "r");
1115 if (file == NULL)
1116 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1117 filename, strerror (errno));
1119 bufsize = 100;
1120 buf = xmalloc (bufsize);
1122 lineno = 1;
1123 c = getc (file);
1124 len = 0;
1125 outsym_off = 0;
1126 while (c != EOF)
1128 /* Collect the input symbol name. */
1129 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1131 if (c == '#')
1132 goto comment;
1133 buf[len++] = c;
1134 if (len >= bufsize)
1136 bufsize *= 2;
1137 buf = xrealloc (buf, bufsize);
1139 c = getc (file);
1141 buf[len++] = '\0';
1142 if (c == EOF)
1143 break;
1145 /* Eat white space between the symbol names. */
1146 while (IS_WHITESPACE (c))
1147 c = getc (file);
1148 if (c == '#' || IS_LINE_TERMINATOR (c))
1149 goto comment;
1150 if (c == EOF)
1151 break;
1153 /* Collect the output symbol name. */
1154 outsym_off = len;
1155 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1157 if (c == '#')
1158 goto comment;
1159 buf[len++] = c;
1160 if (len >= bufsize)
1162 bufsize *= 2;
1163 buf = xrealloc (buf, bufsize);
1165 c = getc (file);
1167 buf[len++] = '\0';
1168 if (c == EOF)
1169 break;
1171 /* Eat white space at end of line. */
1172 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1173 c = getc (file);
1174 if (c == '#')
1175 goto comment;
1176 /* Handle \r\n. */
1177 if ((c == '\r' && (c = getc (file)) == '\n')
1178 || c == '\n' || c == EOF)
1180 end_of_line:
1181 /* Append the redefinition to the list. */
1182 if (buf[0] != '\0')
1183 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1185 lineno++;
1186 len = 0;
1187 outsym_off = 0;
1188 if (c == EOF)
1189 break;
1190 c = getc (file);
1191 continue;
1193 else
1194 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1195 comment:
1196 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1197 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1198 buf[len++] = '\0';
1200 /* Eat the rest of the line and finish it. */
1201 while (c != '\n' && c != EOF)
1202 c = getc (file);
1203 goto end_of_line;
1206 if (len != 0)
1207 fatal (_("%s:%d: premature end of file"), filename, lineno);
1209 free (buf);
1212 /* Copy unkown object file IBFD onto OBFD.
1213 Returns TRUE upon success, FALSE otherwise. */
1215 static bfd_boolean
1216 copy_unknown_object (bfd *ibfd, bfd *obfd)
1218 char *cbuf;
1219 int tocopy;
1220 long ncopied;
1221 long size;
1222 struct stat buf;
1224 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1226 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1227 return FALSE;
1230 size = buf.st_size;
1231 if (size < 0)
1233 non_fatal (_("stat returns negative size for `%s'"),
1234 bfd_get_archive_filename (ibfd));
1235 return FALSE;
1238 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1240 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1241 return FALSE;
1244 if (verbose)
1245 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1246 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1248 cbuf = xmalloc (BUFSIZE);
1249 ncopied = 0;
1250 while (ncopied < size)
1252 tocopy = size - ncopied;
1253 if (tocopy > BUFSIZE)
1254 tocopy = BUFSIZE;
1256 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1257 != (bfd_size_type) tocopy)
1259 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1260 free (cbuf);
1261 return FALSE;
1264 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1265 != (bfd_size_type) tocopy)
1267 bfd_nonfatal (bfd_get_filename (obfd));
1268 free (cbuf);
1269 return FALSE;
1272 ncopied += tocopy;
1275 chmod (bfd_get_filename (obfd), buf.st_mode);
1276 free (cbuf);
1277 return TRUE;
1280 /* Copy object file IBFD onto OBFD.
1281 Returns TRUE upon success, FALSE otherwise. */
1283 static bfd_boolean
1284 copy_object (bfd *ibfd, bfd *obfd)
1286 bfd_vma start;
1287 long symcount;
1288 asection **osections = NULL;
1289 asection *gnu_debuglink_section = NULL;
1290 bfd_size_type *gaps = NULL;
1291 bfd_size_type max_gap = 0;
1292 long symsize;
1293 void *dhandle;
1294 enum bfd_architecture iarch;
1295 unsigned int imach;
1297 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1298 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1299 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1300 fatal (_("Unable to change endianness of input file(s)"));
1302 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1304 bfd_nonfatal (bfd_get_filename (obfd));
1305 return FALSE;
1308 if (verbose)
1309 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1310 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1311 bfd_get_filename (obfd), bfd_get_target (obfd));
1313 if (set_start_set)
1314 start = set_start;
1315 else
1316 start = bfd_get_start_address (ibfd);
1317 start += change_start;
1319 /* Neither the start address nor the flags
1320 need to be set for a core file. */
1321 if (bfd_get_format (obfd) != bfd_core)
1323 flagword flags;
1325 flags = bfd_get_file_flags (ibfd);
1326 flags |= bfd_flags_to_set;
1327 flags &= ~bfd_flags_to_clear;
1328 flags &= bfd_applicable_file_flags (obfd);
1330 if (!bfd_set_start_address (obfd, start)
1331 || !bfd_set_file_flags (obfd, flags))
1333 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1334 return FALSE;
1338 /* Copy architecture of input file to output file. */
1339 iarch = bfd_get_arch (ibfd);
1340 imach = bfd_get_mach (ibfd);
1341 if (!bfd_set_arch_mach (obfd, iarch, imach)
1342 && (ibfd->target_defaulted
1343 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1345 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1346 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1347 bfd_get_archive_filename (ibfd));
1348 else
1349 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1350 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1351 bfd_get_mach (ibfd)));
1352 return FALSE;
1355 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1357 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1358 return FALSE;
1361 if (isympp)
1362 free (isympp);
1364 if (osympp != isympp)
1365 free (osympp);
1367 isympp = NULL;
1368 osympp = NULL;
1370 symsize = bfd_get_symtab_upper_bound (ibfd);
1371 if (symsize < 0)
1373 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1374 return FALSE;
1377 osympp = isympp = xmalloc (symsize);
1378 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1379 if (symcount < 0)
1381 bfd_nonfatal (bfd_get_filename (ibfd));
1382 return FALSE;
1385 /* BFD mandates that all output sections be created and sizes set before
1386 any output is done. Thus, we traverse all sections multiple times. */
1387 bfd_map_over_sections (ibfd, setup_section, obfd);
1389 setup_bfd_headers (ibfd, obfd);
1391 if (add_sections != NULL)
1393 struct section_add *padd;
1394 struct section_list *pset;
1396 for (padd = add_sections; padd != NULL; padd = padd->next)
1398 flagword flags;
1400 pset = find_section_list (padd->name, FALSE);
1401 if (pset != NULL)
1402 pset->used = TRUE;
1404 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1405 if (pset != NULL && pset->set_flags)
1406 flags = pset->flags | SEC_HAS_CONTENTS;
1408 /* bfd_make_section_with_flags() does not return very helpful
1409 error codes, so check for the most likely user error first. */
1410 if (bfd_get_section_by_name (obfd, padd->name))
1412 non_fatal (_("can't add section '%s' - it already exists!"), padd->name);
1413 return FALSE;
1415 else
1417 padd->section = bfd_make_section_with_flags (obfd, padd->name, flags);
1418 if (padd->section == NULL)
1420 non_fatal (_("can't create section `%s': %s"),
1421 padd->name, bfd_errmsg (bfd_get_error ()));
1422 return FALSE;
1426 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1428 bfd_nonfatal (bfd_get_filename (obfd));
1429 return FALSE;
1432 if (pset != NULL)
1434 if (pset->change_vma != CHANGE_IGNORE)
1435 if (! bfd_set_section_vma (obfd, padd->section,
1436 pset->vma_val))
1438 bfd_nonfatal (bfd_get_filename (obfd));
1439 return FALSE;
1442 if (pset->change_lma != CHANGE_IGNORE)
1444 padd->section->lma = pset->lma_val;
1446 if (! bfd_set_section_alignment
1447 (obfd, padd->section,
1448 bfd_section_alignment (obfd, padd->section)))
1450 bfd_nonfatal (bfd_get_filename (obfd));
1451 return FALSE;
1458 if (gnu_debuglink_filename != NULL)
1460 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1461 (obfd, gnu_debuglink_filename);
1463 if (gnu_debuglink_section == NULL)
1465 bfd_nonfatal (gnu_debuglink_filename);
1466 return FALSE;
1469 /* Special processing for PE format files. We
1470 have no way to distinguish PE from COFF here. */
1471 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1473 bfd_vma debuglink_vma;
1474 asection * highest_section;
1475 asection * sec;
1477 /* The PE spec requires that all sections be adjacent and sorted
1478 in ascending order of VMA. It also specifies that debug
1479 sections should be last. This is despite the fact that debug
1480 sections are not loaded into memory and so in theory have no
1481 use for a VMA.
1483 This means that the debuglink section must be given a non-zero
1484 VMA which makes it contiguous with other debug sections. So
1485 walk the current section list, find the section with the
1486 highest VMA and start the debuglink section after that one. */
1487 for (sec = obfd->sections, highest_section = NULL;
1488 sec != NULL;
1489 sec = sec->next)
1490 if (sec->vma > 0
1491 && (highest_section == NULL
1492 || sec->vma > highest_section->vma))
1493 highest_section = sec;
1495 if (highest_section)
1496 debuglink_vma = BFD_ALIGN (highest_section->vma
1497 + highest_section->size,
1498 /* FIXME: We ought to be using
1499 COFF_PAGE_SIZE here or maybe
1500 bfd_get_section_alignment() (if it
1501 was set) but since this is for PE
1502 and we know the required alignment
1503 it is easier just to hard code it. */
1504 0x1000);
1505 else
1506 /* Umm, not sure what to do in this case. */
1507 debuglink_vma = 0x1000;
1509 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1513 if (bfd_count_sections (obfd) == 0)
1515 non_fatal (_("there are no sections to be copied!"));
1516 return FALSE;
1519 if (gap_fill_set || pad_to_set)
1521 asection **set;
1522 unsigned int c, i;
1524 /* We must fill in gaps between the sections and/or we must pad
1525 the last section to a specified address. We do this by
1526 grabbing a list of the sections, sorting them by VMA, and
1527 increasing the section sizes as required to fill the gaps.
1528 We write out the gap contents below. */
1530 c = bfd_count_sections (obfd);
1531 osections = xmalloc (c * sizeof (asection *));
1532 set = osections;
1533 bfd_map_over_sections (obfd, get_sections, &set);
1535 qsort (osections, c, sizeof (asection *), compare_section_lma);
1537 gaps = xmalloc (c * sizeof (bfd_size_type));
1538 memset (gaps, 0, c * sizeof (bfd_size_type));
1540 if (gap_fill_set)
1542 for (i = 0; i < c - 1; i++)
1544 flagword flags;
1545 bfd_size_type size;
1546 bfd_vma gap_start, gap_stop;
1548 flags = bfd_get_section_flags (obfd, osections[i]);
1549 if ((flags & SEC_HAS_CONTENTS) == 0
1550 || (flags & SEC_LOAD) == 0)
1551 continue;
1553 size = bfd_section_size (obfd, osections[i]);
1554 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1555 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1556 if (gap_start < gap_stop)
1558 if (! bfd_set_section_size (obfd, osections[i],
1559 size + (gap_stop - gap_start)))
1561 non_fatal (_("Can't fill gap after %s: %s"),
1562 bfd_get_section_name (obfd, osections[i]),
1563 bfd_errmsg (bfd_get_error ()));
1564 status = 1;
1565 break;
1567 gaps[i] = gap_stop - gap_start;
1568 if (max_gap < gap_stop - gap_start)
1569 max_gap = gap_stop - gap_start;
1574 if (pad_to_set)
1576 bfd_vma lma;
1577 bfd_size_type size;
1579 lma = bfd_section_lma (obfd, osections[c - 1]);
1580 size = bfd_section_size (obfd, osections[c - 1]);
1581 if (lma + size < pad_to)
1583 if (! bfd_set_section_size (obfd, osections[c - 1],
1584 pad_to - lma))
1586 non_fatal (_("Can't add padding to %s: %s"),
1587 bfd_get_section_name (obfd, osections[c - 1]),
1588 bfd_errmsg (bfd_get_error ()));
1589 status = 1;
1591 else
1593 gaps[c - 1] = pad_to - (lma + size);
1594 if (max_gap < pad_to - (lma + size))
1595 max_gap = pad_to - (lma + size);
1601 /* Symbol filtering must happen after the output sections
1602 have been created, but before their contents are set. */
1603 dhandle = NULL;
1604 if (convert_debugging)
1605 dhandle = read_debugging_info (ibfd, isympp, symcount);
1607 if (strip_symbols == STRIP_DEBUG
1608 || strip_symbols == STRIP_ALL
1609 || strip_symbols == STRIP_UNNEEDED
1610 || strip_symbols == STRIP_NONDEBUG
1611 || discard_locals != LOCALS_UNDEF
1612 || localize_hidden
1613 || strip_specific_list != NULL
1614 || keep_specific_list != NULL
1615 || localize_specific_list != NULL
1616 || globalize_specific_list != NULL
1617 || keepglobal_specific_list != NULL
1618 || weaken_specific_list != NULL
1619 || prefix_symbols_string
1620 || sections_removed
1621 || sections_copied
1622 || convert_debugging
1623 || change_leading_char
1624 || remove_leading_char
1625 || redefine_sym_list
1626 || weaken)
1628 /* Mark symbols used in output relocations so that they
1629 are kept, even if they are local labels or static symbols.
1631 Note we iterate over the input sections examining their
1632 relocations since the relocations for the output sections
1633 haven't been set yet. mark_symbols_used_in_relocations will
1634 ignore input sections which have no corresponding output
1635 section. */
1636 if (strip_symbols != STRIP_ALL)
1637 bfd_map_over_sections (ibfd,
1638 mark_symbols_used_in_relocations,
1639 isympp);
1640 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1641 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1644 if (convert_debugging && dhandle != NULL)
1646 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1648 status = 1;
1649 return FALSE;
1653 bfd_set_symtab (obfd, osympp, symcount);
1655 /* This has to happen after the symbol table has been set. */
1656 bfd_map_over_sections (ibfd, copy_section, obfd);
1658 if (add_sections != NULL)
1660 struct section_add *padd;
1662 for (padd = add_sections; padd != NULL; padd = padd->next)
1664 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1665 0, padd->size))
1667 bfd_nonfatal (bfd_get_filename (obfd));
1668 return FALSE;
1673 if (gnu_debuglink_filename != NULL)
1675 if (! bfd_fill_in_gnu_debuglink_section
1676 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1678 bfd_nonfatal (gnu_debuglink_filename);
1679 return FALSE;
1683 if (gap_fill_set || pad_to_set)
1685 bfd_byte *buf;
1686 int c, i;
1688 /* Fill in the gaps. */
1689 if (max_gap > 8192)
1690 max_gap = 8192;
1691 buf = xmalloc (max_gap);
1692 memset (buf, gap_fill, max_gap);
1694 c = bfd_count_sections (obfd);
1695 for (i = 0; i < c; i++)
1697 if (gaps[i] != 0)
1699 bfd_size_type left;
1700 file_ptr off;
1702 left = gaps[i];
1703 off = bfd_section_size (obfd, osections[i]) - left;
1705 while (left > 0)
1707 bfd_size_type now;
1709 if (left > 8192)
1710 now = 8192;
1711 else
1712 now = left;
1714 if (! bfd_set_section_contents (obfd, osections[i], buf,
1715 off, now))
1717 bfd_nonfatal (bfd_get_filename (obfd));
1718 return FALSE;
1721 left -= now;
1722 off += now;
1728 /* Allow the BFD backend to copy any private data it understands
1729 from the input BFD to the output BFD. This is done last to
1730 permit the routine to look at the filtered symbol table, which is
1731 important for the ECOFF code at least. */
1732 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1733 && strip_symbols == STRIP_NONDEBUG)
1734 /* Do not copy the private data when creating an ELF format
1735 debug info file. We do not want the program headers. */
1737 else if (! bfd_copy_private_bfd_data (ibfd, obfd))
1739 non_fatal (_("%s: error copying private BFD data: %s"),
1740 bfd_get_filename (obfd),
1741 bfd_errmsg (bfd_get_error ()));
1742 return FALSE;
1745 /* Switch to the alternate machine code. We have to do this at the
1746 very end, because we only initialize the header when we create
1747 the first section. */
1748 if (use_alt_mach_code != 0)
1750 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
1752 non_fatal (_("this target does not support %lu alternative machine codes"),
1753 use_alt_mach_code);
1754 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1756 non_fatal (_("treating that number as an absolute e_machine value instead"));
1757 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
1759 else
1760 non_fatal (_("ignoring the alternative value"));
1764 return TRUE;
1767 #if ! defined(HAVE_MKDTEMP)
1768 #undef MKDIR
1769 #if defined (_WIN32) && !defined (__CYGWIN32__)
1770 #define MKDIR(DIR, MODE) mkdir (DIR)
1771 #else
1772 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1773 #endif
1774 #endif
1776 /* Read each archive element in turn from IBFD, copy the
1777 contents to temp file, and keep the temp file handle.
1778 If 'force_output_target' is TRUE then make sure that
1779 all elements in the new archive are of the type
1780 'output_target'. */
1782 static void
1783 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
1784 bfd_boolean force_output_target)
1786 struct name_list
1788 struct name_list *next;
1789 const char *name;
1790 bfd *obfd;
1791 } *list, *l;
1792 bfd **ptr = &obfd->archive_head;
1793 bfd *this_element;
1794 char * dir;
1796 /* Make a temp directory to hold the contents. */
1797 #if defined(HAVE_MKDTEMP)
1798 dir = make_tempdir (bfd_get_filename (obfd));
1800 if (dir == NULL)
1801 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1802 strerror (errno));
1803 #else
1804 dir = make_tempname (bfd_get_filename (obfd));
1806 if (MKDIR (dir, 0700) != 0)
1807 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1808 dir, strerror (errno));
1809 #endif
1811 obfd->has_armap = ibfd->has_armap;
1813 list = NULL;
1815 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1817 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1818 RETURN_NONFATAL (bfd_get_filename (obfd));
1820 while (!status && this_element != NULL)
1822 char *output_name;
1823 bfd *output_bfd;
1824 bfd *last_element;
1825 struct stat buf;
1826 int stat_status = 0;
1827 bfd_boolean delete = TRUE;
1829 /* Create an output file for this member. */
1830 output_name = concat (dir, "/",
1831 bfd_get_filename (this_element), (char *) 0);
1833 /* If the file already exists, make another temp dir. */
1834 if (stat (output_name, &buf) >= 0)
1836 #if defined(HAVE_MKDTEMP)
1837 output_name = make_tempdir (output_name);
1838 if (output_name == NULL)
1839 fatal (_("cannot create temporary dir '%s' for archive copying (error: %s)"),
1840 output_name, strerror (errno));
1841 #else
1842 output_name = make_tempname (output_name);
1843 if (MKDIR (output_name, 0700) != 0)
1844 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1845 output_name, strerror (errno));
1846 #endif
1848 l = xmalloc (sizeof (struct name_list));
1849 l->name = output_name;
1850 l->next = list;
1851 l->obfd = NULL;
1852 list = l;
1853 output_name = concat (output_name, "/",
1854 bfd_get_filename (this_element), (char *) 0);
1857 if (preserve_dates)
1859 stat_status = bfd_stat_arch_elt (this_element, &buf);
1861 if (stat_status != 0)
1862 non_fatal (_("internal stat error on %s"),
1863 bfd_get_filename (this_element));
1866 l = xmalloc (sizeof (struct name_list));
1867 l->name = output_name;
1868 l->next = list;
1869 l->obfd = NULL;
1870 list = l;
1872 if (bfd_check_format (this_element, bfd_object))
1874 /* PR binutils/3110: Cope with archives
1875 containing multiple target types. */
1876 if (force_output_target)
1877 output_bfd = bfd_openw (output_name, output_target);
1878 else
1879 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
1881 if (output_bfd == NULL)
1882 RETURN_NONFATAL (output_name);
1884 delete = ! copy_object (this_element, output_bfd);
1886 if (! delete
1887 || bfd_get_arch (this_element) != bfd_arch_unknown)
1889 if (!bfd_close (output_bfd))
1891 bfd_nonfatal (bfd_get_filename (output_bfd));
1892 /* Error in new object file. Don't change archive. */
1893 status = 1;
1896 else
1897 goto copy_unknown_element;
1899 else
1901 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1902 bfd_get_archive_filename (this_element));
1904 output_bfd = bfd_openw (output_name, output_target);
1905 copy_unknown_element:
1906 delete = !copy_unknown_object (this_element, output_bfd);
1907 if (!bfd_close_all_done (output_bfd))
1909 bfd_nonfatal (bfd_get_filename (output_bfd));
1910 /* Error in new object file. Don't change archive. */
1911 status = 1;
1915 if (delete)
1917 unlink (output_name);
1918 status = 1;
1920 else
1922 if (preserve_dates && stat_status == 0)
1923 set_times (output_name, &buf);
1925 /* Open the newly output file and attach to our list. */
1926 output_bfd = bfd_openr (output_name, output_target);
1928 l->obfd = output_bfd;
1930 *ptr = output_bfd;
1931 ptr = &output_bfd->next;
1933 last_element = this_element;
1935 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1937 bfd_close (last_element);
1940 *ptr = NULL;
1942 if (!bfd_close (obfd))
1943 RETURN_NONFATAL (bfd_get_filename (obfd));
1945 if (!bfd_close (ibfd))
1946 RETURN_NONFATAL (bfd_get_filename (ibfd));
1948 /* Delete all the files that we opened. */
1949 for (l = list; l != NULL; l = l->next)
1951 if (l->obfd == NULL)
1952 rmdir (l->name);
1953 else
1955 bfd_close (l->obfd);
1956 unlink (l->name);
1959 rmdir (dir);
1962 /* The top-level control. */
1964 static void
1965 copy_file (const char *input_filename, const char *output_filename,
1966 const char *input_target, const char *output_target)
1968 bfd *ibfd;
1969 char **obj_matching;
1970 char **core_matching;
1972 if (get_file_size (input_filename) < 1)
1974 status = 1;
1975 return;
1978 /* To allow us to do "strip *" without dying on the first
1979 non-object file, failures are nonfatal. */
1980 ibfd = bfd_openr (input_filename, input_target);
1981 if (ibfd == NULL)
1982 RETURN_NONFATAL (input_filename);
1984 if (bfd_check_format (ibfd, bfd_archive))
1986 bfd_boolean force_output_target;
1987 bfd *obfd;
1989 /* bfd_get_target does not return the correct value until
1990 bfd_check_format succeeds. */
1991 if (output_target == NULL)
1993 output_target = bfd_get_target (ibfd);
1994 force_output_target = FALSE;
1996 else
1997 force_output_target = TRUE;
1999 obfd = bfd_openw (output_filename, output_target);
2000 if (obfd == NULL)
2001 RETURN_NONFATAL (output_filename);
2003 copy_archive (ibfd, obfd, output_target, force_output_target);
2005 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2007 bfd *obfd;
2008 do_copy:
2010 /* bfd_get_target does not return the correct value until
2011 bfd_check_format succeeds. */
2012 if (output_target == NULL)
2013 output_target = bfd_get_target (ibfd);
2015 obfd = bfd_openw (output_filename, output_target);
2016 if (obfd == NULL)
2017 RETURN_NONFATAL (output_filename);
2019 if (! copy_object (ibfd, obfd))
2020 status = 1;
2022 if (!bfd_close (obfd))
2023 RETURN_NONFATAL (output_filename);
2025 if (!bfd_close (ibfd))
2026 RETURN_NONFATAL (input_filename);
2029 else
2031 bfd_error_type obj_error = bfd_get_error ();
2032 bfd_error_type core_error;
2034 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2036 /* This probably can't happen.. */
2037 if (obj_error == bfd_error_file_ambiguously_recognized)
2038 free (obj_matching);
2039 goto do_copy;
2042 core_error = bfd_get_error ();
2043 /* Report the object error in preference to the core error. */
2044 if (obj_error != core_error)
2045 bfd_set_error (obj_error);
2047 bfd_nonfatal (input_filename);
2049 if (obj_error == bfd_error_file_ambiguously_recognized)
2051 list_matching_formats (obj_matching);
2052 free (obj_matching);
2054 if (core_error == bfd_error_file_ambiguously_recognized)
2056 list_matching_formats (core_matching);
2057 free (core_matching);
2060 status = 1;
2064 /* Add a name to the section renaming list. */
2066 static void
2067 add_section_rename (const char * old_name, const char * new_name,
2068 flagword flags)
2070 section_rename * rename;
2072 /* Check for conflicts first. */
2073 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2074 if (strcmp (rename->old_name, old_name) == 0)
2076 /* Silently ignore duplicate definitions. */
2077 if (strcmp (rename->new_name, new_name) == 0
2078 && rename->flags == flags)
2079 return;
2081 fatal (_("Multiple renames of section %s"), old_name);
2084 rename = xmalloc (sizeof (* rename));
2086 rename->old_name = old_name;
2087 rename->new_name = new_name;
2088 rename->flags = flags;
2089 rename->next = section_rename_list;
2091 section_rename_list = rename;
2094 /* Check the section rename list for a new name of the input section
2095 ISECTION. Return the new name if one is found.
2096 Also set RETURNED_FLAGS to the flags to be used for this section. */
2098 static const char *
2099 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2100 flagword * returned_flags)
2102 const char * old_name = bfd_section_name (ibfd, isection);
2103 section_rename * rename;
2105 /* Default to using the flags of the input section. */
2106 * returned_flags = bfd_get_section_flags (ibfd, isection);
2108 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2109 if (strcmp (rename->old_name, old_name) == 0)
2111 if (rename->flags != (flagword) -1)
2112 * returned_flags = rename->flags;
2114 return rename->new_name;
2117 return old_name;
2120 /* Once each of the sections is copied, we may still need to do some
2121 finalization work for private section headers. Do that here. */
2123 static void
2124 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2126 const char *err;
2128 /* Allow the BFD backend to copy any private data it understands
2129 from the input section to the output section. */
2130 if (! bfd_copy_private_header_data (ibfd, obfd))
2132 err = _("private header data");
2133 goto loser;
2136 /* All went well. */
2137 return;
2139 loser:
2140 non_fatal (_("%s: error in %s: %s"),
2141 bfd_get_filename (ibfd),
2142 err, bfd_errmsg (bfd_get_error ()));
2143 status = 1;
2146 /* Create a section in OBFD with the same
2147 name and attributes as ISECTION in IBFD. */
2149 static void
2150 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2152 bfd *obfd = obfdarg;
2153 struct section_list *p;
2154 sec_ptr osection;
2155 bfd_size_type size;
2156 bfd_vma vma;
2157 bfd_vma lma;
2158 flagword flags;
2159 const char *err;
2160 const char * name;
2161 char *prefix = NULL;
2163 if (is_strip_section (ibfd, isection))
2164 return;
2166 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2167 if (p != NULL)
2168 p->used = TRUE;
2170 /* Get the, possibly new, name of the output section. */
2171 name = find_section_rename (ibfd, isection, & flags);
2173 /* Prefix sections. */
2174 if ((prefix_alloc_sections_string)
2175 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2176 prefix = prefix_alloc_sections_string;
2177 else if (prefix_sections_string)
2178 prefix = prefix_sections_string;
2180 if (prefix)
2182 char *n;
2184 n = xmalloc (strlen (prefix) + strlen (name) + 1);
2185 strcpy (n, prefix);
2186 strcat (n, name);
2187 name = n;
2190 if (p != NULL && p->set_flags)
2191 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2192 else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
2193 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2195 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2197 if (osection == NULL)
2199 err = _("making");
2200 goto loser;
2203 if (strip_symbols == STRIP_NONDEBUG
2204 && obfd->xvec->flavour == bfd_target_elf_flavour
2205 && (flags & SEC_ALLOC) != 0
2206 && (p == NULL || !p->set_flags))
2207 elf_section_type (osection) = SHT_NOBITS;
2209 size = bfd_section_size (ibfd, isection);
2210 if (copy_byte >= 0)
2211 size = (size + interleave - 1) / interleave;
2212 if (! bfd_set_section_size (obfd, osection, size))
2214 err = _("size");
2215 goto loser;
2218 vma = bfd_section_vma (ibfd, isection);
2219 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2220 vma += p->vma_val;
2221 else if (p != NULL && p->change_vma == CHANGE_SET)
2222 vma = p->vma_val;
2223 else
2224 vma += change_section_address;
2226 if (! bfd_set_section_vma (obfd, osection, vma))
2228 err = _("vma");
2229 goto loser;
2232 lma = isection->lma;
2233 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2235 if (p->change_lma == CHANGE_MODIFY)
2236 lma += p->lma_val;
2237 else if (p->change_lma == CHANGE_SET)
2238 lma = p->lma_val;
2239 else
2240 abort ();
2242 else
2243 lma += change_section_address;
2245 osection->lma = lma;
2247 /* FIXME: This is probably not enough. If we change the LMA we
2248 may have to recompute the header for the file as well. */
2249 if (!bfd_set_section_alignment (obfd,
2250 osection,
2251 bfd_section_alignment (ibfd, isection)))
2253 err = _("alignment");
2254 goto loser;
2257 /* Copy merge entity size. */
2258 osection->entsize = isection->entsize;
2260 /* This used to be mangle_section; we do here to avoid using
2261 bfd_get_section_by_name since some formats allow multiple
2262 sections with the same name. */
2263 isection->output_section = osection;
2264 isection->output_offset = 0;
2266 /* Allow the BFD backend to copy any private data it understands
2267 from the input section to the output section. */
2268 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
2269 && strip_symbols == STRIP_NONDEBUG)
2270 /* Do not copy the private data when creating an ELF format
2271 debug info file. We do not want the program headers. */
2273 else if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2275 err = _("private data");
2276 goto loser;
2278 else if ((isection->flags & SEC_GROUP) != 0)
2280 asymbol *gsym = group_signature (isection);
2282 if (gsym != NULL)
2283 gsym->flags |= BSF_KEEP;
2286 /* All went well. */
2287 return;
2289 loser:
2290 non_fatal (_("%s: section `%s': error in %s: %s"),
2291 bfd_get_filename (ibfd),
2292 bfd_section_name (ibfd, isection),
2293 err, bfd_errmsg (bfd_get_error ()));
2294 status = 1;
2297 /* Copy the data of input section ISECTION of IBFD
2298 to an output section with the same name in OBFD.
2299 If stripping then don't copy any relocation info. */
2301 static void
2302 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2304 bfd *obfd = obfdarg;
2305 struct section_list *p;
2306 arelent **relpp;
2307 long relcount;
2308 sec_ptr osection;
2309 bfd_size_type size;
2310 long relsize;
2311 flagword flags;
2313 /* If we have already failed earlier on,
2314 do not keep on generating complaints now. */
2315 if (status != 0)
2316 return;
2318 if (is_strip_section (ibfd, isection))
2319 return;
2321 flags = bfd_get_section_flags (ibfd, isection);
2322 if ((flags & SEC_GROUP) != 0)
2323 return;
2325 osection = isection->output_section;
2326 size = bfd_get_section_size (isection);
2328 if (size == 0 || osection == 0)
2329 return;
2331 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2333 /* Core files do not need to be relocated. */
2334 if (bfd_get_format (obfd) == bfd_core)
2335 relsize = 0;
2336 else
2338 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2340 if (relsize < 0)
2342 /* Do not complain if the target does not support relocations. */
2343 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2344 relsize = 0;
2345 else
2346 RETURN_NONFATAL (bfd_get_filename (ibfd));
2350 if (relsize == 0)
2351 bfd_set_reloc (obfd, osection, NULL, 0);
2352 else
2354 relpp = xmalloc (relsize);
2355 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2356 if (relcount < 0)
2357 RETURN_NONFATAL (bfd_get_filename (ibfd));
2359 if (strip_symbols == STRIP_ALL)
2361 /* Remove relocations which are not in
2362 keep_strip_specific_list. */
2363 arelent **temp_relpp;
2364 long temp_relcount = 0;
2365 long i;
2367 temp_relpp = xmalloc (relsize);
2368 for (i = 0; i < relcount; i++)
2369 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2370 keep_specific_list))
2371 temp_relpp [temp_relcount++] = relpp [i];
2372 relcount = temp_relcount;
2373 free (relpp);
2374 relpp = temp_relpp;
2377 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2378 if (relcount == 0)
2379 free (relpp);
2382 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2383 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2385 void *memhunk = xmalloc (size);
2387 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2388 RETURN_NONFATAL (bfd_get_filename (ibfd));
2390 if (copy_byte >= 0)
2392 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2393 char *from = (char *) memhunk + copy_byte;
2394 char *to = memhunk;
2395 char *end = (char *) memhunk + size;
2397 for (; from < end; from += interleave)
2398 *to++ = *from;
2400 size = (size + interleave - 1 - copy_byte) / interleave;
2401 osection->lma /= interleave;
2404 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2405 RETURN_NONFATAL (bfd_get_filename (obfd));
2407 free (memhunk);
2409 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2411 void *memhunk = xmalloc (size);
2413 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2414 flag--they can just remove the section entirely and add it
2415 back again. However, we do permit them to turn on the
2416 SEC_HAS_CONTENTS flag, and take it to mean that the section
2417 contents should be zeroed out. */
2419 memset (memhunk, 0, size);
2420 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2421 RETURN_NONFATAL (bfd_get_filename (obfd));
2422 free (memhunk);
2426 /* Get all the sections. This is used when --gap-fill or --pad-to is
2427 used. */
2429 static void
2430 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2432 asection ***secppp = secppparg;
2434 **secppp = osection;
2435 ++(*secppp);
2438 /* Sort sections by VMA. This is called via qsort, and is used when
2439 --gap-fill or --pad-to is used. We force non loadable or empty
2440 sections to the front, where they are easier to ignore. */
2442 static int
2443 compare_section_lma (const void *arg1, const void *arg2)
2445 const asection *const *sec1 = arg1;
2446 const asection *const *sec2 = arg2;
2447 flagword flags1, flags2;
2449 /* Sort non loadable sections to the front. */
2450 flags1 = (*sec1)->flags;
2451 flags2 = (*sec2)->flags;
2452 if ((flags1 & SEC_HAS_CONTENTS) == 0
2453 || (flags1 & SEC_LOAD) == 0)
2455 if ((flags2 & SEC_HAS_CONTENTS) != 0
2456 && (flags2 & SEC_LOAD) != 0)
2457 return -1;
2459 else
2461 if ((flags2 & SEC_HAS_CONTENTS) == 0
2462 || (flags2 & SEC_LOAD) == 0)
2463 return 1;
2466 /* Sort sections by LMA. */
2467 if ((*sec1)->lma > (*sec2)->lma)
2468 return 1;
2469 else if ((*sec1)->lma < (*sec2)->lma)
2470 return -1;
2472 /* Sort sections with the same LMA by size. */
2473 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2474 return 1;
2475 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2476 return -1;
2478 return 0;
2481 /* Mark all the symbols which will be used in output relocations with
2482 the BSF_KEEP flag so that those symbols will not be stripped.
2484 Ignore relocations which will not appear in the output file. */
2486 static void
2487 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2489 asymbol **symbols = symbolsarg;
2490 long relsize;
2491 arelent **relpp;
2492 long relcount, i;
2494 /* Ignore an input section with no corresponding output section. */
2495 if (isection->output_section == NULL)
2496 return;
2498 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2499 if (relsize < 0)
2501 /* Do not complain if the target does not support relocations. */
2502 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2503 return;
2504 bfd_fatal (bfd_get_filename (ibfd));
2507 if (relsize == 0)
2508 return;
2510 relpp = xmalloc (relsize);
2511 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2512 if (relcount < 0)
2513 bfd_fatal (bfd_get_filename (ibfd));
2515 /* Examine each symbol used in a relocation. If it's not one of the
2516 special bfd section symbols, then mark it with BSF_KEEP. */
2517 for (i = 0; i < relcount; i++)
2519 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2520 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2521 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2522 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2525 if (relpp != NULL)
2526 free (relpp);
2529 /* Write out debugging information. */
2531 static bfd_boolean
2532 write_debugging_info (bfd *obfd, void *dhandle,
2533 long *symcountp ATTRIBUTE_UNUSED,
2534 asymbol ***symppp ATTRIBUTE_UNUSED)
2536 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2537 return write_ieee_debugging_info (obfd, dhandle);
2539 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2540 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2542 bfd_byte *syms, *strings;
2543 bfd_size_type symsize, stringsize;
2544 asection *stabsec, *stabstrsec;
2545 flagword flags;
2547 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2548 &symsize, &strings,
2549 &stringsize))
2550 return FALSE;
2552 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2553 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2554 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2555 if (stabsec == NULL
2556 || stabstrsec == NULL
2557 || ! bfd_set_section_size (obfd, stabsec, symsize)
2558 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2559 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2560 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2562 non_fatal (_("%s: can't create debugging section: %s"),
2563 bfd_get_filename (obfd),
2564 bfd_errmsg (bfd_get_error ()));
2565 return FALSE;
2568 /* We can get away with setting the section contents now because
2569 the next thing the caller is going to do is copy over the
2570 real sections. We may someday have to split the contents
2571 setting out of this function. */
2572 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2573 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2574 stringsize))
2576 non_fatal (_("%s: can't set debugging section contents: %s"),
2577 bfd_get_filename (obfd),
2578 bfd_errmsg (bfd_get_error ()));
2579 return FALSE;
2582 return TRUE;
2585 non_fatal (_("%s: don't know how to write debugging information for %s"),
2586 bfd_get_filename (obfd), bfd_get_target (obfd));
2587 return FALSE;
2590 static int
2591 strip_main (int argc, char *argv[])
2593 char *input_target = NULL;
2594 char *output_target = NULL;
2595 bfd_boolean show_version = FALSE;
2596 bfd_boolean formats_info = FALSE;
2597 int c;
2598 int i;
2599 struct section_list *p;
2600 char *output_file = NULL;
2602 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2603 strip_options, (int *) 0)) != EOF)
2605 switch (c)
2607 case 'I':
2608 input_target = optarg;
2609 break;
2610 case 'O':
2611 output_target = optarg;
2612 break;
2613 case 'F':
2614 input_target = output_target = optarg;
2615 break;
2616 case 'R':
2617 p = find_section_list (optarg, TRUE);
2618 p->remove = TRUE;
2619 sections_removed = TRUE;
2620 break;
2621 case 's':
2622 strip_symbols = STRIP_ALL;
2623 break;
2624 case 'S':
2625 case 'g':
2626 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2627 strip_symbols = STRIP_DEBUG;
2628 break;
2629 case OPTION_STRIP_UNNEEDED:
2630 strip_symbols = STRIP_UNNEEDED;
2631 break;
2632 case 'K':
2633 add_specific_symbol (optarg, &keep_specific_list);
2634 break;
2635 case 'N':
2636 add_specific_symbol (optarg, &strip_specific_list);
2637 break;
2638 case 'o':
2639 output_file = optarg;
2640 break;
2641 case 'p':
2642 preserve_dates = TRUE;
2643 break;
2644 case 'x':
2645 discard_locals = LOCALS_ALL;
2646 break;
2647 case 'X':
2648 discard_locals = LOCALS_START_L;
2649 break;
2650 case 'v':
2651 verbose = TRUE;
2652 break;
2653 case 'V':
2654 show_version = TRUE;
2655 break;
2656 case OPTION_FORMATS_INFO:
2657 formats_info = TRUE;
2658 break;
2659 case OPTION_ONLY_KEEP_DEBUG:
2660 strip_symbols = STRIP_NONDEBUG;
2661 break;
2662 case OPTION_KEEP_FILE_SYMBOLS:
2663 keep_file_symbols = 1;
2664 break;
2665 case 0:
2666 /* We've been given a long option. */
2667 break;
2668 case 'w':
2669 wildcard = TRUE;
2670 break;
2671 case 'H':
2672 case 'h':
2673 strip_usage (stdout, 0);
2674 default:
2675 strip_usage (stderr, 1);
2679 if (formats_info)
2681 display_info ();
2682 return 0;
2685 if (show_version)
2686 print_version ("strip");
2688 /* Default is to strip all symbols. */
2689 if (strip_symbols == STRIP_UNDEF
2690 && discard_locals == LOCALS_UNDEF
2691 && strip_specific_list == NULL)
2692 strip_symbols = STRIP_ALL;
2694 if (output_target == NULL)
2695 output_target = input_target;
2697 i = optind;
2698 if (i == argc
2699 || (output_file != NULL && (i + 1) < argc))
2700 strip_usage (stderr, 1);
2702 for (; i < argc; i++)
2704 int hold_status = status;
2705 struct stat statbuf;
2706 char *tmpname;
2708 if (get_file_size (argv[i]) < 1)
2710 status = 1;
2711 continue;
2714 if (preserve_dates)
2715 /* No need to check the return value of stat().
2716 It has already been checked in get_file_size(). */
2717 stat (argv[i], &statbuf);
2719 if (output_file != NULL)
2720 tmpname = output_file;
2721 else
2722 tmpname = make_tempname (argv[i]);
2724 if (tmpname == NULL)
2726 non_fatal (_("could not create temporary file to hold stripped copy of '%s'"),
2727 argv[i]);
2728 status = 1;
2729 continue;
2732 status = 0;
2733 copy_file (argv[i], tmpname, input_target, output_target);
2734 if (status == 0)
2736 if (preserve_dates)
2737 set_times (tmpname, &statbuf);
2738 if (output_file == NULL)
2739 smart_rename (tmpname, argv[i], preserve_dates);
2740 status = hold_status;
2742 else
2743 unlink_if_ordinary (tmpname);
2744 if (output_file == NULL)
2745 free (tmpname);
2748 return status;
2751 static int
2752 copy_main (int argc, char *argv[])
2754 char * binary_architecture = NULL;
2755 char *input_filename = NULL;
2756 char *output_filename = NULL;
2757 char *input_target = NULL;
2758 char *output_target = NULL;
2759 bfd_boolean show_version = FALSE;
2760 bfd_boolean change_warn = TRUE;
2761 bfd_boolean formats_info = FALSE;
2762 int c;
2763 struct section_list *p;
2764 struct stat statbuf;
2766 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2767 copy_options, (int *) 0)) != EOF)
2769 switch (c)
2771 case 'b':
2772 copy_byte = atoi (optarg);
2773 if (copy_byte < 0)
2774 fatal (_("byte number must be non-negative"));
2775 break;
2777 case 'B':
2778 binary_architecture = optarg;
2779 break;
2781 case 'i':
2782 interleave = atoi (optarg);
2783 if (interleave < 1)
2784 fatal (_("interleave must be positive"));
2785 break;
2787 case 'I':
2788 case 's': /* "source" - 'I' is preferred */
2789 input_target = optarg;
2790 break;
2792 case 'O':
2793 case 'd': /* "destination" - 'O' is preferred */
2794 output_target = optarg;
2795 break;
2797 case 'F':
2798 input_target = output_target = optarg;
2799 break;
2801 case 'j':
2802 p = find_section_list (optarg, TRUE);
2803 if (p->remove)
2804 fatal (_("%s both copied and removed"), optarg);
2805 p->copy = TRUE;
2806 sections_copied = TRUE;
2807 break;
2809 case 'R':
2810 p = find_section_list (optarg, TRUE);
2811 if (p->copy)
2812 fatal (_("%s both copied and removed"), optarg);
2813 p->remove = TRUE;
2814 sections_removed = TRUE;
2815 break;
2817 case 'S':
2818 strip_symbols = STRIP_ALL;
2819 break;
2821 case 'g':
2822 strip_symbols = STRIP_DEBUG;
2823 break;
2825 case OPTION_STRIP_UNNEEDED:
2826 strip_symbols = STRIP_UNNEEDED;
2827 break;
2829 case OPTION_ONLY_KEEP_DEBUG:
2830 strip_symbols = STRIP_NONDEBUG;
2831 break;
2833 case OPTION_KEEP_FILE_SYMBOLS:
2834 keep_file_symbols = 1;
2835 break;
2837 case OPTION_ADD_GNU_DEBUGLINK:
2838 gnu_debuglink_filename = optarg;
2839 break;
2841 case 'K':
2842 add_specific_symbol (optarg, &keep_specific_list);
2843 break;
2845 case 'N':
2846 add_specific_symbol (optarg, &strip_specific_list);
2847 break;
2849 case OPTION_STRIP_UNNEEDED_SYMBOL:
2850 add_specific_symbol (optarg, &strip_unneeded_list);
2851 break;
2853 case 'L':
2854 add_specific_symbol (optarg, &localize_specific_list);
2855 break;
2857 case OPTION_GLOBALIZE_SYMBOL:
2858 add_specific_symbol (optarg, &globalize_specific_list);
2859 break;
2861 case 'G':
2862 add_specific_symbol (optarg, &keepglobal_specific_list);
2863 break;
2865 case 'W':
2866 add_specific_symbol (optarg, &weaken_specific_list);
2867 break;
2869 case 'p':
2870 preserve_dates = TRUE;
2871 break;
2873 case 'w':
2874 wildcard = TRUE;
2875 break;
2877 case 'x':
2878 discard_locals = LOCALS_ALL;
2879 break;
2881 case 'X':
2882 discard_locals = LOCALS_START_L;
2883 break;
2885 case 'v':
2886 verbose = TRUE;
2887 break;
2889 case 'V':
2890 show_version = TRUE;
2891 break;
2893 case OPTION_FORMATS_INFO:
2894 formats_info = TRUE;
2895 break;
2897 case OPTION_WEAKEN:
2898 weaken = TRUE;
2899 break;
2901 case OPTION_ADD_SECTION:
2903 const char *s;
2904 off_t size;
2905 struct section_add *pa;
2906 int len;
2907 char *name;
2908 FILE *f;
2910 s = strchr (optarg, '=');
2912 if (s == NULL)
2913 fatal (_("bad format for %s"), "--add-section");
2915 size = get_file_size (s + 1);
2916 if (size < 1)
2918 status = 1;
2919 break;
2922 pa = xmalloc (sizeof (struct section_add));
2924 len = s - optarg;
2925 name = xmalloc (len + 1);
2926 strncpy (name, optarg, len);
2927 name[len] = '\0';
2928 pa->name = name;
2930 pa->filename = s + 1;
2931 pa->size = size;
2932 pa->contents = xmalloc (size);
2934 f = fopen (pa->filename, FOPEN_RB);
2936 if (f == NULL)
2937 fatal (_("cannot open: %s: %s"),
2938 pa->filename, strerror (errno));
2940 if (fread (pa->contents, 1, pa->size, f) == 0
2941 || ferror (f))
2942 fatal (_("%s: fread failed"), pa->filename);
2944 fclose (f);
2946 pa->next = add_sections;
2947 add_sections = pa;
2949 break;
2951 case OPTION_CHANGE_START:
2952 change_start = parse_vma (optarg, "--change-start");
2953 break;
2955 case OPTION_CHANGE_SECTION_ADDRESS:
2956 case OPTION_CHANGE_SECTION_LMA:
2957 case OPTION_CHANGE_SECTION_VMA:
2959 const char *s;
2960 int len;
2961 char *name;
2962 char *option = NULL;
2963 bfd_vma val;
2964 enum change_action what = CHANGE_IGNORE;
2966 switch (c)
2968 case OPTION_CHANGE_SECTION_ADDRESS:
2969 option = "--change-section-address";
2970 break;
2971 case OPTION_CHANGE_SECTION_LMA:
2972 option = "--change-section-lma";
2973 break;
2974 case OPTION_CHANGE_SECTION_VMA:
2975 option = "--change-section-vma";
2976 break;
2979 s = strchr (optarg, '=');
2980 if (s == NULL)
2982 s = strchr (optarg, '+');
2983 if (s == NULL)
2985 s = strchr (optarg, '-');
2986 if (s == NULL)
2987 fatal (_("bad format for %s"), option);
2991 len = s - optarg;
2992 name = xmalloc (len + 1);
2993 strncpy (name, optarg, len);
2994 name[len] = '\0';
2996 p = find_section_list (name, TRUE);
2998 val = parse_vma (s + 1, option);
3000 switch (*s)
3002 case '=': what = CHANGE_SET; break;
3003 case '-': val = - val; /* Drop through. */
3004 case '+': what = CHANGE_MODIFY; break;
3007 switch (c)
3009 case OPTION_CHANGE_SECTION_ADDRESS:
3010 p->change_vma = what;
3011 p->vma_val = val;
3012 /* Drop through. */
3014 case OPTION_CHANGE_SECTION_LMA:
3015 p->change_lma = what;
3016 p->lma_val = val;
3017 break;
3019 case OPTION_CHANGE_SECTION_VMA:
3020 p->change_vma = what;
3021 p->vma_val = val;
3022 break;
3025 break;
3027 case OPTION_CHANGE_ADDRESSES:
3028 change_section_address = parse_vma (optarg, "--change-addresses");
3029 change_start = change_section_address;
3030 break;
3032 case OPTION_CHANGE_WARNINGS:
3033 change_warn = TRUE;
3034 break;
3036 case OPTION_CHANGE_LEADING_CHAR:
3037 change_leading_char = TRUE;
3038 break;
3040 case OPTION_DEBUGGING:
3041 convert_debugging = TRUE;
3042 break;
3044 case OPTION_GAP_FILL:
3046 bfd_vma gap_fill_vma;
3048 gap_fill_vma = parse_vma (optarg, "--gap-fill");
3049 gap_fill = (bfd_byte) gap_fill_vma;
3050 if ((bfd_vma) gap_fill != gap_fill_vma)
3052 char buff[20];
3054 sprintf_vma (buff, gap_fill_vma);
3056 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3057 buff, gap_fill);
3059 gap_fill_set = TRUE;
3061 break;
3063 case OPTION_NO_CHANGE_WARNINGS:
3064 change_warn = FALSE;
3065 break;
3067 case OPTION_PAD_TO:
3068 pad_to = parse_vma (optarg, "--pad-to");
3069 pad_to_set = TRUE;
3070 break;
3072 case OPTION_REMOVE_LEADING_CHAR:
3073 remove_leading_char = TRUE;
3074 break;
3076 case OPTION_REDEFINE_SYM:
3078 /* Push this redefinition onto redefine_symbol_list. */
3080 int len;
3081 const char *s;
3082 const char *nextarg;
3083 char *source, *target;
3085 s = strchr (optarg, '=');
3086 if (s == NULL)
3087 fatal (_("bad format for %s"), "--redefine-sym");
3089 len = s - optarg;
3090 source = xmalloc (len + 1);
3091 strncpy (source, optarg, len);
3092 source[len] = '\0';
3094 nextarg = s + 1;
3095 len = strlen (nextarg);
3096 target = xmalloc (len + 1);
3097 strcpy (target, nextarg);
3099 redefine_list_append ("--redefine-sym", source, target);
3101 free (source);
3102 free (target);
3104 break;
3106 case OPTION_REDEFINE_SYMS:
3107 add_redefine_syms_file (optarg);
3108 break;
3110 case OPTION_SET_SECTION_FLAGS:
3112 const char *s;
3113 int len;
3114 char *name;
3116 s = strchr (optarg, '=');
3117 if (s == NULL)
3118 fatal (_("bad format for %s"), "--set-section-flags");
3120 len = s - optarg;
3121 name = xmalloc (len + 1);
3122 strncpy (name, optarg, len);
3123 name[len] = '\0';
3125 p = find_section_list (name, TRUE);
3127 p->set_flags = TRUE;
3128 p->flags = parse_flags (s + 1);
3130 break;
3132 case OPTION_RENAME_SECTION:
3134 flagword flags;
3135 const char *eq, *fl;
3136 char *old_name;
3137 char *new_name;
3138 unsigned int len;
3140 eq = strchr (optarg, '=');
3141 if (eq == NULL)
3142 fatal (_("bad format for %s"), "--rename-section");
3144 len = eq - optarg;
3145 if (len == 0)
3146 fatal (_("bad format for %s"), "--rename-section");
3148 old_name = xmalloc (len + 1);
3149 strncpy (old_name, optarg, len);
3150 old_name[len] = 0;
3152 eq++;
3153 fl = strchr (eq, ',');
3154 if (fl)
3156 flags = parse_flags (fl + 1);
3157 len = fl - eq;
3159 else
3161 flags = -1;
3162 len = strlen (eq);
3165 if (len == 0)
3166 fatal (_("bad format for %s"), "--rename-section");
3168 new_name = xmalloc (len + 1);
3169 strncpy (new_name, eq, len);
3170 new_name[len] = 0;
3172 add_section_rename (old_name, new_name, flags);
3174 break;
3176 case OPTION_SET_START:
3177 set_start = parse_vma (optarg, "--set-start");
3178 set_start_set = TRUE;
3179 break;
3181 case OPTION_SREC_LEN:
3182 Chunk = parse_vma (optarg, "--srec-len");
3183 break;
3185 case OPTION_SREC_FORCES3:
3186 S3Forced = TRUE;
3187 break;
3189 case OPTION_STRIP_SYMBOLS:
3190 add_specific_symbols (optarg, &strip_specific_list);
3191 break;
3193 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3194 add_specific_symbols (optarg, &strip_unneeded_list);
3195 break;
3197 case OPTION_KEEP_SYMBOLS:
3198 add_specific_symbols (optarg, &keep_specific_list);
3199 break;
3201 case OPTION_LOCALIZE_HIDDEN:
3202 localize_hidden = TRUE;
3203 break;
3205 case OPTION_LOCALIZE_SYMBOLS:
3206 add_specific_symbols (optarg, &localize_specific_list);
3207 break;
3209 case OPTION_GLOBALIZE_SYMBOLS:
3210 add_specific_symbols (optarg, &globalize_specific_list);
3211 break;
3213 case OPTION_KEEPGLOBAL_SYMBOLS:
3214 add_specific_symbols (optarg, &keepglobal_specific_list);
3215 break;
3217 case OPTION_WEAKEN_SYMBOLS:
3218 add_specific_symbols (optarg, &weaken_specific_list);
3219 break;
3221 case OPTION_ALT_MACH_CODE:
3222 use_alt_mach_code = strtoul (optarg, NULL, 0);
3223 if (use_alt_mach_code == 0)
3224 fatal (_("unable to parse alternative machine code"));
3225 break;
3227 case OPTION_PREFIX_SYMBOLS:
3228 prefix_symbols_string = optarg;
3229 break;
3231 case OPTION_PREFIX_SECTIONS:
3232 prefix_sections_string = optarg;
3233 break;
3235 case OPTION_PREFIX_ALLOC_SECTIONS:
3236 prefix_alloc_sections_string = optarg;
3237 break;
3239 case OPTION_READONLY_TEXT:
3240 bfd_flags_to_set |= WP_TEXT;
3241 bfd_flags_to_clear &= ~WP_TEXT;
3242 break;
3244 case OPTION_WRITABLE_TEXT:
3245 bfd_flags_to_clear |= WP_TEXT;
3246 bfd_flags_to_set &= ~WP_TEXT;
3247 break;
3249 case OPTION_PURE:
3250 bfd_flags_to_set |= D_PAGED;
3251 bfd_flags_to_clear &= ~D_PAGED;
3252 break;
3254 case OPTION_IMPURE:
3255 bfd_flags_to_clear |= D_PAGED;
3256 bfd_flags_to_set &= ~D_PAGED;
3257 break;
3259 case 0:
3260 /* We've been given a long option. */
3261 break;
3263 case 'H':
3264 case 'h':
3265 copy_usage (stdout, 0);
3267 default:
3268 copy_usage (stderr, 1);
3272 if (formats_info)
3274 display_info ();
3275 return 0;
3278 if (show_version)
3279 print_version ("objcopy");
3281 if (copy_byte >= interleave)
3282 fatal (_("byte number must be less than interleave"));
3284 if (optind == argc || optind + 2 < argc)
3285 copy_usage (stderr, 1);
3287 input_filename = argv[optind];
3288 if (optind + 1 < argc)
3289 output_filename = argv[optind + 1];
3291 /* Default is to strip no symbols. */
3292 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3293 strip_symbols = STRIP_NONE;
3295 if (output_target == NULL)
3296 output_target = input_target;
3298 if (binary_architecture != NULL)
3300 if (input_target && strcmp (input_target, "binary") == 0)
3302 const bfd_arch_info_type * temp_arch_info;
3304 temp_arch_info = bfd_scan_arch (binary_architecture);
3306 if (temp_arch_info != NULL)
3308 bfd_external_binary_architecture = temp_arch_info->arch;
3309 bfd_external_machine = temp_arch_info->mach;
3311 else
3312 fatal (_("architecture %s unknown"), binary_architecture);
3314 else
3316 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3317 non_fatal (_(" Argument %s ignored"), binary_architecture);
3321 if (preserve_dates)
3322 if (stat (input_filename, & statbuf) < 0)
3323 fatal (_("warning: could not locate '%s'. System error message: %s"),
3324 input_filename, strerror (errno));
3326 /* If there is no destination file, or the source and destination files
3327 are the same, then create a temp and rename the result into the input. */
3328 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3330 char *tmpname = make_tempname (input_filename);
3332 if (tmpname == NULL)
3333 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3334 input_filename, strerror (errno));
3336 copy_file (input_filename, tmpname, input_target, output_target);
3337 if (status == 0)
3339 if (preserve_dates)
3340 set_times (tmpname, &statbuf);
3341 smart_rename (tmpname, input_filename, preserve_dates);
3343 else
3344 unlink (tmpname);
3346 else
3348 copy_file (input_filename, output_filename, input_target, output_target);
3350 if (status == 0 && preserve_dates)
3351 set_times (output_filename, &statbuf);
3352 else if (status != 0)
3353 unlink_if_ordinary (output_filename);
3356 if (change_warn)
3358 for (p = change_sections; p != NULL; p = p->next)
3360 if (! p->used)
3362 if (p->change_vma != CHANGE_IGNORE)
3364 char buff [20];
3366 sprintf_vma (buff, p->vma_val);
3368 /* xgettext:c-format */
3369 non_fatal (_("%s %s%c0x%s never used"),
3370 "--change-section-vma",
3371 p->name,
3372 p->change_vma == CHANGE_SET ? '=' : '+',
3373 buff);
3376 if (p->change_lma != CHANGE_IGNORE)
3378 char buff [20];
3380 sprintf_vma (buff, p->lma_val);
3382 /* xgettext:c-format */
3383 non_fatal (_("%s %s%c0x%s never used"),
3384 "--change-section-lma",
3385 p->name,
3386 p->change_lma == CHANGE_SET ? '=' : '+',
3387 buff);
3393 return 0;
3397 main (int argc, char *argv[])
3399 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3400 setlocale (LC_MESSAGES, "");
3401 #endif
3402 #if defined (HAVE_SETLOCALE)
3403 setlocale (LC_CTYPE, "");
3404 #endif
3405 bindtextdomain (PACKAGE, LOCALEDIR);
3406 textdomain (PACKAGE);
3408 program_name = argv[0];
3409 xmalloc_set_program_name (program_name);
3411 START_PROGRESS (program_name, 0);
3413 expandargv (&argc, &argv);
3415 strip_symbols = STRIP_UNDEF;
3416 discard_locals = LOCALS_UNDEF;
3418 bfd_init ();
3419 set_default_bfd_target ();
3421 if (is_strip < 0)
3423 int i = strlen (program_name);
3424 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3425 /* Drop the .exe suffix, if any. */
3426 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3428 i -= 4;
3429 program_name[i] = '\0';
3431 #endif
3432 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3435 if (is_strip)
3436 strip_main (argc, argv);
3437 else
3438 copy_main (argc, argv);
3440 END_PROGRESS (program_name);
3442 return status;