1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
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
27 #include "libiberty.h"
30 #include "filenames.h"
36 /* A list of symbols to explicitly strip out, or to keep. A linked
37 list is good enough for a small number from the command line, but
38 this will slow things down a lot if many symbols are being
47 /* A list to support redefine_sym. */
52 struct redefine_node
*next
;
55 typedef struct section_rename
57 const char * old_name
;
58 const char * new_name
;
60 struct section_rename
* next
;
64 /* List of sections to be renamed. */
65 static section_rename
*section_rename_list
;
67 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
69 static asymbol
**isympp
= NULL
; /* Input symbols. */
70 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
72 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
73 static int copy_byte
= -1;
74 static int interleave
= 4;
76 static bfd_boolean verbose
; /* Print file and target names. */
77 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
78 static int status
= 0; /* Exit status. */
83 STRIP_NONE
, /* Don't strip. */
84 STRIP_DEBUG
, /* Strip all debugger symbols. */
85 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
86 STRIP_NONDEBUG
, /* Strip everything but debug info. */
87 STRIP_ALL
/* Strip all symbols. */
90 /* Which symbols to remove. */
91 static enum strip_action strip_symbols
;
96 LOCALS_START_L
, /* Discard locals starting with L. */
97 LOCALS_ALL
/* Discard all locals. */
100 /* Which local symbols to remove. Overrides STRIP_ALL. */
101 static enum locals_action discard_locals
;
103 /* What kind of change to perform. */
111 /* Structure used to hold lists of sections and actions to take. */
114 struct section_list
* next
; /* Next section to change. */
115 const char * name
; /* Section name. */
116 bfd_boolean used
; /* Whether this entry was used. */
117 bfd_boolean remove
; /* Whether to remove this section. */
118 bfd_boolean copy
; /* Whether to copy this section. */
119 enum change_action change_vma
;/* Whether to change or set VMA. */
120 bfd_vma vma_val
; /* Amount to change by or set to. */
121 enum change_action change_lma
;/* Whether to change or set LMA. */
122 bfd_vma lma_val
; /* Amount to change by or set to. */
123 bfd_boolean set_flags
; /* Whether to set the section flags. */
124 flagword flags
; /* What to set the section flags to. */
127 static struct section_list
*change_sections
;
129 /* TRUE if some sections are to be removed. */
130 static bfd_boolean sections_removed
;
132 /* TRUE if only some sections are to be copied. */
133 static bfd_boolean sections_copied
;
135 /* Changes to the start address. */
136 static bfd_vma change_start
= 0;
137 static bfd_boolean set_start_set
= FALSE
;
138 static bfd_vma set_start
;
140 /* Changes to section addresses. */
141 static bfd_vma change_section_address
= 0;
143 /* Filling gaps between sections. */
144 static bfd_boolean gap_fill_set
= FALSE
;
145 static bfd_byte gap_fill
= 0;
147 /* Pad to a given address. */
148 static bfd_boolean pad_to_set
= FALSE
;
149 static bfd_vma pad_to
;
151 /* Use alternative machine code? */
152 static unsigned long use_alt_mach_code
= 0;
154 /* Output BFD flags user wants to set or clear */
155 static flagword bfd_flags_to_set
;
156 static flagword bfd_flags_to_clear
;
158 /* List of sections to add. */
161 /* Next section to add. */
162 struct section_add
*next
;
163 /* Name of section to add. */
165 /* Name of file holding section contents. */
166 const char *filename
;
169 /* Contents of file. */
171 /* BFD section, after it has been added. */
175 /* List of sections to add to the output BFD. */
176 static struct section_add
*add_sections
;
178 /* If non-NULL the argument to --add-gnu-debuglink.
179 This should be the filename to store in the .gnu_debuglink section. */
180 static const char * gnu_debuglink_filename
= NULL
;
182 /* Whether to convert debugging information. */
183 static bfd_boolean convert_debugging
= FALSE
;
185 /* Whether to change the leading character in symbol names. */
186 static bfd_boolean change_leading_char
= FALSE
;
188 /* Whether to remove the leading character from global symbol names. */
189 static bfd_boolean remove_leading_char
= FALSE
;
191 /* Whether to permit wildcard in symbol comparison. */
192 static bfd_boolean wildcard
= FALSE
;
194 /* True if --localize-hidden is in effect. */
195 static bfd_boolean localize_hidden
= FALSE
;
197 /* List of symbols to strip, keep, localize, keep-global, weaken,
199 static struct symlist
*strip_specific_list
= NULL
;
200 static struct symlist
*strip_unneeded_list
= NULL
;
201 static struct symlist
*keep_specific_list
= NULL
;
202 static struct symlist
*localize_specific_list
= NULL
;
203 static struct symlist
*globalize_specific_list
= NULL
;
204 static struct symlist
*keepglobal_specific_list
= NULL
;
205 static struct symlist
*weaken_specific_list
= NULL
;
206 static struct redefine_node
*redefine_sym_list
= NULL
;
208 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
209 static bfd_boolean weaken
= FALSE
;
211 /* If this is TRUE, we retain BSF_FILE symbols. */
212 static bfd_boolean keep_file_symbols
= FALSE
;
214 /* Prefix symbols/sections. */
215 static char *prefix_symbols_string
= 0;
216 static char *prefix_sections_string
= 0;
217 static char *prefix_alloc_sections_string
= 0;
219 /* True if --extract-symbol was passed on the command line. */
220 static bfd_boolean extract_symbol
= FALSE
;
222 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
223 of <reverse_bytes> bytes within each output section. */
224 static int reverse_bytes
= 0;
227 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
228 enum command_line_switch
230 OPTION_ADD_SECTION
=150,
231 OPTION_CHANGE_ADDRESSES
,
232 OPTION_CHANGE_LEADING_CHAR
,
234 OPTION_CHANGE_SECTION_ADDRESS
,
235 OPTION_CHANGE_SECTION_LMA
,
236 OPTION_CHANGE_SECTION_VMA
,
237 OPTION_CHANGE_WARNINGS
,
240 OPTION_NO_CHANGE_WARNINGS
,
242 OPTION_REMOVE_LEADING_CHAR
,
243 OPTION_SET_SECTION_FLAGS
,
245 OPTION_STRIP_UNNEEDED
,
248 OPTION_REDEFINE_SYMS
,
251 OPTION_STRIP_SYMBOLS
,
252 OPTION_STRIP_UNNEEDED_SYMBOL
,
253 OPTION_STRIP_UNNEEDED_SYMBOLS
,
255 OPTION_LOCALIZE_HIDDEN
,
256 OPTION_LOCALIZE_SYMBOLS
,
257 OPTION_GLOBALIZE_SYMBOL
,
258 OPTION_GLOBALIZE_SYMBOLS
,
259 OPTION_KEEPGLOBAL_SYMBOLS
,
260 OPTION_WEAKEN_SYMBOLS
,
261 OPTION_RENAME_SECTION
,
262 OPTION_ALT_MACH_CODE
,
263 OPTION_PREFIX_SYMBOLS
,
264 OPTION_PREFIX_SECTIONS
,
265 OPTION_PREFIX_ALLOC_SECTIONS
,
267 OPTION_ADD_GNU_DEBUGLINK
,
268 OPTION_ONLY_KEEP_DEBUG
,
269 OPTION_KEEP_FILE_SYMBOLS
,
270 OPTION_READONLY_TEXT
,
271 OPTION_WRITABLE_TEXT
,
274 OPTION_EXTRACT_SYMBOL
,
278 /* Options to handle if running as "strip". */
280 static struct option strip_options
[] =
282 {"discard-all", no_argument
, 0, 'x'},
283 {"discard-locals", no_argument
, 0, 'X'},
284 {"format", required_argument
, 0, 'F'}, /* Obsolete */
285 {"help", no_argument
, 0, 'h'},
286 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
287 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
288 {"input-target", required_argument
, 0, 'I'},
289 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
290 {"keep-symbol", required_argument
, 0, 'K'},
291 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
292 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
293 {"output-target", required_argument
, 0, 'O'},
294 {"output-file", required_argument
, 0, 'o'},
295 {"preserve-dates", no_argument
, 0, 'p'},
296 {"remove-section", required_argument
, 0, 'R'},
297 {"strip-all", no_argument
, 0, 's'},
298 {"strip-debug", no_argument
, 0, 'S'},
299 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
300 {"strip-symbol", required_argument
, 0, 'N'},
301 {"target", required_argument
, 0, 'F'},
302 {"verbose", no_argument
, 0, 'v'},
303 {"version", no_argument
, 0, 'V'},
304 {"wildcard", no_argument
, 0, 'w'},
305 {0, no_argument
, 0, 0}
308 /* Options to handle if running as "objcopy". */
310 static struct option copy_options
[] =
312 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
313 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
314 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
315 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
316 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
317 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
318 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
319 {"binary-architecture", required_argument
, 0, 'B'},
320 {"byte", required_argument
, 0, 'b'},
321 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
322 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
323 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
324 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
325 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
326 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
327 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
328 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
329 {"discard-all", no_argument
, 0, 'x'},
330 {"discard-locals", no_argument
, 0, 'X'},
331 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
332 {"format", required_argument
, 0, 'F'}, /* Obsolete */
333 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
334 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
335 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
336 {"help", no_argument
, 0, 'h'},
337 {"impure", no_argument
, 0, OPTION_IMPURE
},
338 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
339 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
340 {"input-target", required_argument
, 0, 'I'},
341 {"interleave", required_argument
, 0, 'i'},
342 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
343 {"keep-global-symbol", required_argument
, 0, 'G'},
344 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
345 {"keep-symbol", required_argument
, 0, 'K'},
346 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
347 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
348 {"localize-symbol", required_argument
, 0, 'L'},
349 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
350 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
351 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
352 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
353 {"only-section", required_argument
, 0, 'j'},
354 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
355 {"output-target", required_argument
, 0, 'O'},
356 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
357 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
358 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
359 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
360 {"preserve-dates", no_argument
, 0, 'p'},
361 {"pure", no_argument
, 0, OPTION_PURE
},
362 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
363 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
364 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
365 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
366 {"remove-section", required_argument
, 0, 'R'},
367 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
368 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
369 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
370 {"set-start", required_argument
, 0, OPTION_SET_START
},
371 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
372 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
373 {"strip-all", no_argument
, 0, 'S'},
374 {"strip-debug", no_argument
, 0, 'g'},
375 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
376 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
377 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
378 {"strip-symbol", required_argument
, 0, 'N'},
379 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
380 {"target", required_argument
, 0, 'F'},
381 {"verbose", no_argument
, 0, 'v'},
382 {"version", no_argument
, 0, 'V'},
383 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
384 {"weaken-symbol", required_argument
, 0, 'W'},
385 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
386 {"wildcard", no_argument
, 0, 'w'},
387 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
388 {0, no_argument
, 0, 0}
392 extern char *program_name
;
394 /* This flag distinguishes between strip and objcopy:
395 1 means this is 'strip'; 0 means this is 'objcopy'.
396 -1 means if we should use argv[0] to decide. */
399 /* The maximum length of an S record. This variable is declared in srec.c
400 and can be modified by the --srec-len parameter. */
401 extern unsigned int Chunk
;
403 /* Restrict the generation of Srecords to type S3 only.
404 This variable is declare in bfd/srec.c and can be toggled
405 on by the --srec-forceS3 command line switch. */
406 extern bfd_boolean S3Forced
;
408 /* Defined in bfd/binary.c. Used to set architecture and machine of input
410 extern enum bfd_architecture bfd_external_binary_architecture
;
411 extern unsigned long bfd_external_machine
;
413 /* Forward declarations. */
414 static void setup_section (bfd
*, asection
*, void *);
415 static void setup_bfd_headers (bfd
*, bfd
*);
416 static void copy_section (bfd
*, asection
*, void *);
417 static void get_sections (bfd
*, asection
*, void *);
418 static int compare_section_lma (const void *, const void *);
419 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
420 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
421 static const char *lookup_sym_redefinition (const char *);
424 copy_usage (FILE *stream
, int exit_status
)
426 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
427 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
428 fprintf (stream
, _(" The options are:\n"));
429 fprintf (stream
, _("\
430 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
431 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
432 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
433 -F --target <bfdname> Set both input and output format to <bfdname>\n\
434 --debugging Convert debugging information, if possible\n\
435 -p --preserve-dates Copy modified/access timestamps to the output\n\
436 -j --only-section <name> Only copy section <name> into the output\n\
437 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
438 -R --remove-section <name> Remove section <name> from the output\n\
439 -S --strip-all Remove all symbol and relocation information\n\
440 -g --strip-debug Remove all debugging symbols & sections\n\
441 --strip-unneeded Remove all symbols not needed by relocations\n\
442 -N --strip-symbol <name> Do not copy symbol <name>\n\
443 --strip-unneeded-symbol <name>\n\
444 Do not copy symbol <name> unless needed by\n\
446 --only-keep-debug Strip everything but the debug information\n\
447 --extract-symbol Remove section contents but keep symbols\n\
448 -K --keep-symbol <name> Do not strip symbol <name>\n\
449 --keep-file-symbols Do not strip file symbol(s)\n\
450 --localize-hidden Turn all ELF hidden symbols into locals\n\
451 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
452 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
453 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
454 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
455 --weaken Force all global symbols to be marked as weak\n\
456 -w --wildcard Permit wildcard in symbol comparison\n\
457 -x --discard-all Remove all non-global symbols\n\
458 -X --discard-locals Remove any compiler-generated symbols\n\
459 -i --interleave <number> Only copy one out of every <number> bytes\n\
460 -b --byte <num> Select byte <num> in every interleaved block\n\
461 --gap-fill <val> Fill gaps between sections with <val>\n\
462 --pad-to <addr> Pad the last section up to address <addr>\n\
463 --set-start <addr> Set the start address to <addr>\n\
464 {--change-start|--adjust-start} <incr>\n\
465 Add <incr> to the start address\n\
466 {--change-addresses|--adjust-vma} <incr>\n\
467 Add <incr> to LMA, VMA and start addresses\n\
468 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
469 Change LMA and VMA of section <name> by <val>\n\
470 --change-section-lma <name>{=|+|-}<val>\n\
471 Change the LMA of section <name> by <val>\n\
472 --change-section-vma <name>{=|+|-}<val>\n\
473 Change the VMA of section <name> by <val>\n\
474 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
475 Warn if a named section does not exist\n\
476 --set-section-flags <name>=<flags>\n\
477 Set section <name>'s properties to <flags>\n\
478 --add-section <name>=<file> Add section <name> found in <file> to output\n\
479 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
480 --change-leading-char Force output format's leading character style\n\
481 --remove-leading-char Remove leading character from global symbols\n\
482 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
483 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
484 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
486 --srec-len <number> Restrict the length of generated Srecords\n\
487 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
488 --strip-symbols <file> -N for all symbols listed in <file>\n\
489 --strip-unneeded-symbols <file>\n\
490 --strip-unneeded-symbol for all symbols listed\n\
492 --keep-symbols <file> -K for all symbols listed in <file>\n\
493 --localize-symbols <file> -L for all symbols listed in <file>\n\
494 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
495 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
496 --weaken-symbols <file> -W for all symbols listed in <file>\n\
497 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
498 --writable-text Mark the output text as writable\n\
499 --readonly-text Make the output text write protected\n\
500 --pure Mark the output file as demand paged\n\
501 --impure Mark the output file as impure\n\
502 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
503 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
504 --prefix-alloc-sections <prefix>\n\
505 Add <prefix> to start of every allocatable\n\
507 -v --verbose List all object files modified\n\
508 @<file> Read options from <file>\n\
509 -V --version Display this program's version number\n\
510 -h --help Display this output\n\
511 --info List object formats & architectures supported\n\
513 list_supported_targets (program_name
, stream
);
514 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
515 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
520 strip_usage (FILE *stream
, int exit_status
)
522 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
523 fprintf (stream
, _(" Removes symbols and sections from files\n"));
524 fprintf (stream
, _(" The options are:\n"));
525 fprintf (stream
, _("\
526 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
527 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
528 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
529 -p --preserve-dates Copy modified/access timestamps to the output\n\
530 -R --remove-section=<name> Remove section <name> from the output\n\
531 -s --strip-all Remove all symbol and relocation information\n\
532 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
533 --strip-unneeded Remove all symbols not needed by relocations\n\
534 --only-keep-debug Strip everything but the debug information\n\
535 -N --strip-symbol=<name> Do not copy symbol <name>\n\
536 -K --keep-symbol=<name> Do not strip symbol <name>\n\
537 --keep-file-symbols Do not strip file symbol(s)\n\
538 -w --wildcard Permit wildcard in symbol comparison\n\
539 -x --discard-all Remove all non-global symbols\n\
540 -X --discard-locals Remove any compiler-generated symbols\n\
541 -v --verbose List all object files modified\n\
542 -V --version Display this program's version number\n\
543 -h --help Display this output\n\
544 --info List object formats & architectures supported\n\
545 -o <file> Place stripped output into <file>\n\
548 list_supported_targets (program_name
, stream
);
549 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
550 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
554 /* Parse section flags into a flagword, with a fatal error if the
555 string can't be parsed. */
558 parse_flags (const char *s
)
568 snext
= strchr (s
, ',');
578 #define PARSE_FLAG(fname,fval) \
579 else if (strncasecmp (fname, s, len) == 0) ret |= fval
580 PARSE_FLAG ("alloc", SEC_ALLOC
);
581 PARSE_FLAG ("load", SEC_LOAD
);
582 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
583 PARSE_FLAG ("readonly", SEC_READONLY
);
584 PARSE_FLAG ("debug", SEC_DEBUGGING
);
585 PARSE_FLAG ("code", SEC_CODE
);
586 PARSE_FLAG ("data", SEC_DATA
);
587 PARSE_FLAG ("rom", SEC_ROM
);
588 PARSE_FLAG ("share", SEC_COFF_SHARED
);
589 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
595 copy
= xmalloc (len
+ 1);
596 strncpy (copy
, s
, len
);
598 non_fatal (_("unrecognized section flag `%s'"), copy
);
599 fatal (_("supported flags: %s"),
600 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
610 /* Find and optionally add an entry in the change_sections list. */
612 static struct section_list
*
613 find_section_list (const char *name
, bfd_boolean add
)
615 struct section_list
*p
;
617 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
618 if (strcmp (p
->name
, name
) == 0)
624 p
= xmalloc (sizeof (struct section_list
));
629 p
->change_vma
= CHANGE_IGNORE
;
630 p
->change_lma
= CHANGE_IGNORE
;
633 p
->set_flags
= FALSE
;
636 p
->next
= change_sections
;
642 /* Add a symbol to strip_specific_list. */
645 add_specific_symbol (const char *name
, struct symlist
**list
)
647 struct symlist
*tmp_list
;
649 tmp_list
= xmalloc (sizeof (struct symlist
));
650 tmp_list
->name
= name
;
651 tmp_list
->next
= *list
;
655 /* Add symbols listed in `filename' to strip_specific_list. */
657 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
658 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
661 add_specific_symbols (const char *filename
, struct symlist
**list
)
667 unsigned int line_count
;
669 size
= get_file_size (filename
);
676 buffer
= xmalloc (size
+ 2);
677 f
= fopen (filename
, FOPEN_RT
);
679 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
681 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
682 fatal (_("%s: fread failed"), filename
);
685 buffer
[size
] = '\n';
686 buffer
[size
+ 1] = '\0';
690 for (line
= buffer
; * line
!= '\0'; line
++)
695 int finished
= FALSE
;
697 for (eol
= line
;; eol
++)
703 /* Cope with \n\r. */
711 /* Cope with \r\n. */
722 /* Line comment, Terminate the line here, in case a
723 name is present and then allow the rest of the
724 loop to find the real end of the line. */
736 /* A name may now exist somewhere between 'line' and 'eol'.
737 Strip off leading whitespace and trailing whitespace,
738 then add it to the list. */
739 for (name
= line
; IS_WHITESPACE (* name
); name
++)
741 for (name_end
= name
;
742 (! IS_WHITESPACE (* name_end
))
743 && (! IS_LINE_TERMINATOR (* name_end
));
747 if (! IS_LINE_TERMINATOR (* name_end
))
751 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
754 if (! IS_LINE_TERMINATOR (* extra
))
755 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
756 filename
, line_count
);
762 add_specific_symbol (name
, list
);
764 /* Advance line pointer to end of line. The 'eol ++' in the for
765 loop above will then advance us to the start of the next line. */
771 /* See whether a symbol should be stripped or kept based on
772 strip_specific_list and keep_symbols. */
775 is_specified_symbol (const char *name
, struct symlist
*list
)
777 struct symlist
*tmp_list
;
781 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
782 if (*(tmp_list
->name
) != '!')
784 if (!fnmatch (tmp_list
->name
, name
, 0))
789 if (fnmatch (tmp_list
->name
+ 1, name
, 0))
795 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
796 if (strcmp (name
, tmp_list
->name
) == 0)
803 /* Return a pointer to the symbol used as a signature for GROUP. */
806 group_signature (asection
*group
)
808 bfd
*abfd
= group
->owner
;
809 Elf_Internal_Shdr
*ghdr
;
811 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
814 ghdr
= &elf_section_data (group
)->this_hdr
;
815 if (ghdr
->sh_link
< elf_numsections (abfd
))
817 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
818 Elf_Internal_Shdr
*symhdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
820 if (symhdr
->sh_type
== SHT_SYMTAB
821 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
822 return isympp
[ghdr
->sh_info
- 1];
827 /* See if a section is being removed. */
830 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
832 if (sections_removed
|| sections_copied
)
834 struct section_list
*p
;
836 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
838 if (sections_removed
&& p
!= NULL
&& p
->remove
)
840 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
844 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
846 if (strip_symbols
== STRIP_DEBUG
847 || strip_symbols
== STRIP_UNNEEDED
848 || strip_symbols
== STRIP_ALL
849 || discard_locals
== LOCALS_ALL
850 || convert_debugging
)
853 if (strip_symbols
== STRIP_NONDEBUG
)
857 if ((bfd_get_section_flags (abfd
, sec
) & SEC_GROUP
) != 0)
863 Group sections look like debugging sections but they are not.
864 (They have a non-zero size but they are not ALLOCated). */
865 if (strip_symbols
== STRIP_NONDEBUG
)
869 If we are going to strip the group signature symbol, then
870 strip the group section too. */
871 gsym
= group_signature (sec
);
876 if ((strip_symbols
== STRIP_ALL
877 && !is_specified_symbol (gname
, keep_specific_list
))
878 || is_specified_symbol (gname
, strip_specific_list
))
885 /* Return true if SYM is a hidden symbol. */
888 is_hidden_symbol (asymbol
*sym
)
890 elf_symbol_type
*elf_sym
;
892 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
894 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
903 /* Choose which symbol entries to copy; put the result in OSYMS.
904 We don't copy in place, because that confuses the relocs.
905 Return the number of symbols to print. */
908 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
909 asymbol
**isyms
, long symcount
)
911 asymbol
**from
= isyms
, **to
= osyms
;
912 long src_count
= 0, dst_count
= 0;
913 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
916 for (; src_count
< symcount
; src_count
++)
918 asymbol
*sym
= from
[src_count
];
919 flagword flags
= sym
->flags
;
920 char *name
= (char *) bfd_asymbol_name (sym
);
922 bfd_boolean used_in_reloc
= FALSE
;
923 bfd_boolean undefined
;
924 bfd_boolean rem_leading_char
;
925 bfd_boolean add_leading_char
;
927 undefined
= bfd_is_und_section (bfd_get_section (sym
));
929 if (redefine_sym_list
)
931 char *old_name
, *new_name
;
933 old_name
= (char *) bfd_asymbol_name (sym
);
934 new_name
= (char *) lookup_sym_redefinition (old_name
);
935 bfd_asymbol_name (sym
) = new_name
;
939 /* Check if we will remove the current leading character. */
941 (name
[0] == bfd_get_symbol_leading_char (abfd
))
942 && (change_leading_char
943 || (remove_leading_char
944 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
946 || bfd_is_com_section (bfd_get_section (sym
)))));
948 /* Check if we will add a new leading character. */
951 && (bfd_get_symbol_leading_char (obfd
) != '\0')
952 && (bfd_get_symbol_leading_char (abfd
) == '\0'
953 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
955 /* Short circuit for change_leading_char if we can do it in-place. */
956 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
958 name
[0] = bfd_get_symbol_leading_char (obfd
);
959 bfd_asymbol_name (sym
) = name
;
960 rem_leading_char
= FALSE
;
961 add_leading_char
= FALSE
;
964 /* Remove leading char. */
965 if (rem_leading_char
)
966 bfd_asymbol_name (sym
) = ++name
;
968 /* Add new leading char and/or prefix. */
969 if (add_leading_char
|| prefix_symbols_string
)
973 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
974 + strlen (name
) + 1);
975 if (add_leading_char
)
976 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
978 if (prefix_symbols_string
)
980 strcpy (ptr
, prefix_symbols_string
);
981 ptr
+= strlen (prefix_symbols_string
);
985 bfd_asymbol_name (sym
) = n
;
989 if (strip_symbols
== STRIP_ALL
)
991 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
992 || ((flags
& BSF_SECTION_SYM
) != 0
993 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
997 used_in_reloc
= TRUE
;
999 else if (relocatable
/* Relocatable file. */
1000 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
1002 else if (bfd_decode_symclass (sym
) == 'I')
1003 /* Global symbols in $idata sections need to be retained
1004 even if relocatable is FALSE. External users of the
1005 library containing the $idata section may reference these
1008 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1009 || (flags
& BSF_WEAK
) != 0
1011 || bfd_is_com_section (bfd_get_section (sym
)))
1012 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1013 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1014 keep
= (strip_symbols
!= STRIP_DEBUG
1015 && strip_symbols
!= STRIP_UNNEEDED
1016 && ! convert_debugging
);
1017 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
1018 /* COMDAT sections store special information in local
1019 symbols, so we cannot risk stripping any of them. */
1021 else /* Local symbol. */
1022 keep
= (strip_symbols
!= STRIP_UNNEEDED
1023 && (discard_locals
!= LOCALS_ALL
1024 && (discard_locals
!= LOCALS_START_L
1025 || ! bfd_is_local_label (abfd
, sym
))));
1027 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
1029 /* There are multiple ways to set 'keep' above, but if it
1030 was the relocatable symbol case, then that's an error. */
1033 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1041 && !(flags
& BSF_KEEP
)
1042 && is_specified_symbol (name
, strip_unneeded_list
))
1046 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1047 || is_specified_symbol (name
, keep_specific_list
)))
1050 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
1055 if ((flags
& BSF_GLOBAL
) != 0
1056 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
1058 sym
->flags
&= ~ BSF_GLOBAL
;
1059 sym
->flags
|= BSF_WEAK
;
1063 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1064 && (is_specified_symbol (name
, localize_specific_list
)
1065 || (keepglobal_specific_list
!= NULL
1066 && ! is_specified_symbol (name
, keepglobal_specific_list
))
1067 || (localize_hidden
&& is_hidden_symbol (sym
))))
1069 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1070 sym
->flags
|= BSF_LOCAL
;
1074 && (flags
& BSF_LOCAL
)
1075 && is_specified_symbol (name
, globalize_specific_list
))
1077 sym
->flags
&= ~ BSF_LOCAL
;
1078 sym
->flags
|= BSF_GLOBAL
;
1081 to
[dst_count
++] = sym
;
1085 to
[dst_count
] = NULL
;
1090 /* Find the redefined name of symbol SOURCE. */
1093 lookup_sym_redefinition (const char *source
)
1095 struct redefine_node
*list
;
1097 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1098 if (strcmp (source
, list
->source
) == 0)
1099 return list
->target
;
1104 /* Add a node to a symbol redefine list. */
1107 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1109 struct redefine_node
**p
;
1110 struct redefine_node
*list
;
1111 struct redefine_node
*new_node
;
1113 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1115 if (strcmp (source
, list
->source
) == 0)
1116 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1119 if (strcmp (target
, list
->target
) == 0)
1120 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1124 new_node
= xmalloc (sizeof (struct redefine_node
));
1126 new_node
->source
= strdup (source
);
1127 new_node
->target
= strdup (target
);
1128 new_node
->next
= NULL
;
1133 /* Handle the --redefine-syms option. Read lines containing "old new"
1134 from the file, and add them to the symbol redefine list. */
1137 add_redefine_syms_file (const char *filename
)
1146 file
= fopen (filename
, "r");
1148 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1149 filename
, strerror (errno
));
1152 buf
= xmalloc (bufsize
);
1160 /* Collect the input symbol name. */
1161 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1169 buf
= xrealloc (buf
, bufsize
);
1177 /* Eat white space between the symbol names. */
1178 while (IS_WHITESPACE (c
))
1180 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1185 /* Collect the output symbol name. */
1187 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1195 buf
= xrealloc (buf
, bufsize
);
1203 /* Eat white space at end of line. */
1204 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1209 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1210 || c
== '\n' || c
== EOF
)
1213 /* Append the redefinition to the list. */
1215 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1226 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1228 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1229 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1232 /* Eat the rest of the line and finish it. */
1233 while (c
!= '\n' && c
!= EOF
)
1239 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1244 /* Copy unkown object file IBFD onto OBFD.
1245 Returns TRUE upon success, FALSE otherwise. */
1248 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1256 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1258 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1265 non_fatal (_("stat returns negative size for `%s'"),
1266 bfd_get_archive_filename (ibfd
));
1270 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1272 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1277 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1278 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1280 cbuf
= xmalloc (BUFSIZE
);
1282 while (ncopied
< size
)
1284 tocopy
= size
- ncopied
;
1285 if (tocopy
> BUFSIZE
)
1288 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1289 != (bfd_size_type
) tocopy
)
1291 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1296 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1297 != (bfd_size_type
) tocopy
)
1299 bfd_nonfatal (bfd_get_filename (obfd
));
1307 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1312 /* Copy object file IBFD onto OBFD.
1313 Returns TRUE upon success, FALSE otherwise. */
1316 copy_object (bfd
*ibfd
, bfd
*obfd
)
1320 asection
**osections
= NULL
;
1321 asection
*gnu_debuglink_section
= NULL
;
1322 bfd_size_type
*gaps
= NULL
;
1323 bfd_size_type max_gap
= 0;
1326 enum bfd_architecture iarch
;
1329 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1330 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1331 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1332 fatal (_("Unable to change endianness of input file(s)"));
1334 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1336 bfd_nonfatal (bfd_get_filename (obfd
));
1341 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1342 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1343 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1352 start
= bfd_get_start_address (ibfd
);
1353 start
+= change_start
;
1356 /* Neither the start address nor the flags
1357 need to be set for a core file. */
1358 if (bfd_get_format (obfd
) != bfd_core
)
1362 flags
= bfd_get_file_flags (ibfd
);
1363 flags
|= bfd_flags_to_set
;
1364 flags
&= ~bfd_flags_to_clear
;
1365 flags
&= bfd_applicable_file_flags (obfd
);
1367 if (!bfd_set_start_address (obfd
, start
)
1368 || !bfd_set_file_flags (obfd
, flags
))
1370 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1375 /* Copy architecture of input file to output file. */
1376 iarch
= bfd_get_arch (ibfd
);
1377 imach
= bfd_get_mach (ibfd
);
1378 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1379 && (ibfd
->target_defaulted
1380 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1382 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1383 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1384 bfd_get_archive_filename (ibfd
));
1386 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1387 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1388 bfd_get_mach (ibfd
)));
1392 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1394 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1401 if (osympp
!= isympp
)
1407 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1410 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1414 osympp
= isympp
= xmalloc (symsize
);
1415 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1418 bfd_nonfatal (bfd_get_filename (ibfd
));
1422 /* BFD mandates that all output sections be created and sizes set before
1423 any output is done. Thus, we traverse all sections multiple times. */
1424 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1426 /* Don't copy headers when creating an ELF format debug info file. */
1427 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1428 && strip_symbols
== STRIP_NONDEBUG
)
1431 setup_bfd_headers (ibfd
, obfd
);
1433 if (add_sections
!= NULL
)
1435 struct section_add
*padd
;
1436 struct section_list
*pset
;
1438 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1442 pset
= find_section_list (padd
->name
, FALSE
);
1446 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1447 if (pset
!= NULL
&& pset
->set_flags
)
1448 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1450 /* bfd_make_section_with_flags() does not return very helpful
1451 error codes, so check for the most likely user error first. */
1452 if (bfd_get_section_by_name (obfd
, padd
->name
))
1454 non_fatal (_("can't add section '%s' - it already exists!"), padd
->name
);
1459 padd
->section
= bfd_make_section_with_flags (obfd
, padd
->name
, flags
);
1460 if (padd
->section
== NULL
)
1462 non_fatal (_("can't create section `%s': %s"),
1463 padd
->name
, bfd_errmsg (bfd_get_error ()));
1468 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1470 bfd_nonfatal (bfd_get_filename (obfd
));
1476 if (pset
->change_vma
!= CHANGE_IGNORE
)
1477 if (! bfd_set_section_vma (obfd
, padd
->section
,
1480 bfd_nonfatal (bfd_get_filename (obfd
));
1484 if (pset
->change_lma
!= CHANGE_IGNORE
)
1486 padd
->section
->lma
= pset
->lma_val
;
1488 if (! bfd_set_section_alignment
1489 (obfd
, padd
->section
,
1490 bfd_section_alignment (obfd
, padd
->section
)))
1492 bfd_nonfatal (bfd_get_filename (obfd
));
1500 if (gnu_debuglink_filename
!= NULL
)
1502 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1503 (obfd
, gnu_debuglink_filename
);
1505 if (gnu_debuglink_section
== NULL
)
1507 bfd_nonfatal (gnu_debuglink_filename
);
1511 /* Special processing for PE format files. We
1512 have no way to distinguish PE from COFF here. */
1513 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1515 bfd_vma debuglink_vma
;
1516 asection
* highest_section
;
1519 /* The PE spec requires that all sections be adjacent and sorted
1520 in ascending order of VMA. It also specifies that debug
1521 sections should be last. This is despite the fact that debug
1522 sections are not loaded into memory and so in theory have no
1525 This means that the debuglink section must be given a non-zero
1526 VMA which makes it contiguous with other debug sections. So
1527 walk the current section list, find the section with the
1528 highest VMA and start the debuglink section after that one. */
1529 for (sec
= obfd
->sections
, highest_section
= NULL
;
1533 && (highest_section
== NULL
1534 || sec
->vma
> highest_section
->vma
))
1535 highest_section
= sec
;
1537 if (highest_section
)
1538 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1539 + highest_section
->size
,
1540 /* FIXME: We ought to be using
1541 COFF_PAGE_SIZE here or maybe
1542 bfd_get_section_alignment() (if it
1543 was set) but since this is for PE
1544 and we know the required alignment
1545 it is easier just to hard code it. */
1548 /* Umm, not sure what to do in this case. */
1549 debuglink_vma
= 0x1000;
1551 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1555 if (bfd_count_sections (obfd
) != 0
1556 && (gap_fill_set
|| pad_to_set
))
1561 /* We must fill in gaps between the sections and/or we must pad
1562 the last section to a specified address. We do this by
1563 grabbing a list of the sections, sorting them by VMA, and
1564 increasing the section sizes as required to fill the gaps.
1565 We write out the gap contents below. */
1567 c
= bfd_count_sections (obfd
);
1568 osections
= xmalloc (c
* sizeof (asection
*));
1570 bfd_map_over_sections (obfd
, get_sections
, &set
);
1572 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1574 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1575 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1579 for (i
= 0; i
< c
- 1; i
++)
1583 bfd_vma gap_start
, gap_stop
;
1585 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1586 if ((flags
& SEC_HAS_CONTENTS
) == 0
1587 || (flags
& SEC_LOAD
) == 0)
1590 size
= bfd_section_size (obfd
, osections
[i
]);
1591 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1592 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1593 if (gap_start
< gap_stop
)
1595 if (! bfd_set_section_size (obfd
, osections
[i
],
1596 size
+ (gap_stop
- gap_start
)))
1598 non_fatal (_("Can't fill gap after %s: %s"),
1599 bfd_get_section_name (obfd
, osections
[i
]),
1600 bfd_errmsg (bfd_get_error ()));
1604 gaps
[i
] = gap_stop
- gap_start
;
1605 if (max_gap
< gap_stop
- gap_start
)
1606 max_gap
= gap_stop
- gap_start
;
1616 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1617 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1618 if (lma
+ size
< pad_to
)
1620 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1623 non_fatal (_("Can't add padding to %s: %s"),
1624 bfd_get_section_name (obfd
, osections
[c
- 1]),
1625 bfd_errmsg (bfd_get_error ()));
1630 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1631 if (max_gap
< pad_to
- (lma
+ size
))
1632 max_gap
= pad_to
- (lma
+ size
);
1638 /* Symbol filtering must happen after the output sections
1639 have been created, but before their contents are set. */
1641 if (convert_debugging
)
1642 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1644 if (strip_symbols
== STRIP_DEBUG
1645 || strip_symbols
== STRIP_ALL
1646 || strip_symbols
== STRIP_UNNEEDED
1647 || strip_symbols
== STRIP_NONDEBUG
1648 || discard_locals
!= LOCALS_UNDEF
1650 || strip_specific_list
!= NULL
1651 || keep_specific_list
!= NULL
1652 || localize_specific_list
!= NULL
1653 || globalize_specific_list
!= NULL
1654 || keepglobal_specific_list
!= NULL
1655 || weaken_specific_list
!= NULL
1656 || prefix_symbols_string
1659 || convert_debugging
1660 || change_leading_char
1661 || remove_leading_char
1662 || redefine_sym_list
1665 /* Mark symbols used in output relocations so that they
1666 are kept, even if they are local labels or static symbols.
1668 Note we iterate over the input sections examining their
1669 relocations since the relocations for the output sections
1670 haven't been set yet. mark_symbols_used_in_relocations will
1671 ignore input sections which have no corresponding output
1673 if (strip_symbols
!= STRIP_ALL
)
1674 bfd_map_over_sections (ibfd
,
1675 mark_symbols_used_in_relocations
,
1677 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1678 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1681 if (convert_debugging
&& dhandle
!= NULL
)
1683 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1690 bfd_set_symtab (obfd
, osympp
, symcount
);
1692 /* This has to happen after the symbol table has been set. */
1693 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1695 if (add_sections
!= NULL
)
1697 struct section_add
*padd
;
1699 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1701 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1704 bfd_nonfatal (bfd_get_filename (obfd
));
1710 if (gnu_debuglink_filename
!= NULL
)
1712 if (! bfd_fill_in_gnu_debuglink_section
1713 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1715 bfd_nonfatal (gnu_debuglink_filename
);
1720 if (gap_fill_set
|| pad_to_set
)
1725 /* Fill in the gaps. */
1728 buf
= xmalloc (max_gap
);
1729 memset (buf
, gap_fill
, max_gap
);
1731 c
= bfd_count_sections (obfd
);
1732 for (i
= 0; i
< c
; i
++)
1740 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1751 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1754 bfd_nonfatal (bfd_get_filename (obfd
));
1765 /* Do not copy backend data if --extract-symbol is passed; anything
1766 that needs to look at the section contents will fail. */
1770 /* Allow the BFD backend to copy any private data it understands
1771 from the input BFD to the output BFD. This is done last to
1772 permit the routine to look at the filtered symbol table, which is
1773 important for the ECOFF code at least. */
1774 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1775 && strip_symbols
== STRIP_NONDEBUG
)
1776 /* Do not copy the private data when creating an ELF format
1777 debug info file. We do not want the program headers. */
1779 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1781 non_fatal (_("%s: error copying private BFD data: %s"),
1782 bfd_get_filename (obfd
),
1783 bfd_errmsg (bfd_get_error ()));
1787 /* Switch to the alternate machine code. We have to do this at the
1788 very end, because we only initialize the header when we create
1789 the first section. */
1790 if (use_alt_mach_code
!= 0)
1792 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1794 non_fatal (_("this target does not support %lu alternative machine codes"),
1796 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1798 non_fatal (_("treating that number as an absolute e_machine value instead"));
1799 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
1802 non_fatal (_("ignoring the alternative value"));
1809 /* Read each archive element in turn from IBFD, copy the
1810 contents to temp file, and keep the temp file handle.
1811 If 'force_output_target' is TRUE then make sure that
1812 all elements in the new archive are of the type
1816 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
1817 bfd_boolean force_output_target
)
1821 struct name_list
*next
;
1825 bfd
**ptr
= &obfd
->archive_head
;
1829 /* Make a temp directory to hold the contents. */
1830 dir
= make_tempdir (bfd_get_filename (obfd
));
1832 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1835 obfd
->has_armap
= ibfd
->has_armap
;
1839 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1841 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1842 RETURN_NONFATAL (bfd_get_filename (obfd
));
1844 while (!status
&& this_element
!= NULL
)
1850 int stat_status
= 0;
1851 bfd_boolean
delete = TRUE
;
1853 /* Create an output file for this member. */
1854 output_name
= concat (dir
, "/",
1855 bfd_get_filename (this_element
), (char *) 0);
1857 /* If the file already exists, make another temp dir. */
1858 if (stat (output_name
, &buf
) >= 0)
1860 output_name
= make_tempdir (output_name
);
1861 if (output_name
== NULL
)
1862 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1865 l
= xmalloc (sizeof (struct name_list
));
1866 l
->name
= output_name
;
1870 output_name
= concat (output_name
, "/",
1871 bfd_get_filename (this_element
), (char *) 0);
1876 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1878 if (stat_status
!= 0)
1879 non_fatal (_("internal stat error on %s"),
1880 bfd_get_filename (this_element
));
1883 l
= xmalloc (sizeof (struct name_list
));
1884 l
->name
= output_name
;
1889 if (bfd_check_format (this_element
, bfd_object
))
1891 /* PR binutils/3110: Cope with archives
1892 containing multiple target types. */
1893 if (force_output_target
)
1894 output_bfd
= bfd_openw (output_name
, output_target
);
1896 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
1898 if (output_bfd
== NULL
)
1899 RETURN_NONFATAL (output_name
);
1901 delete = ! copy_object (this_element
, output_bfd
);
1904 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
1906 if (!bfd_close (output_bfd
))
1908 bfd_nonfatal (bfd_get_filename (output_bfd
));
1909 /* Error in new object file. Don't change archive. */
1914 goto copy_unknown_element
;
1918 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1919 bfd_get_archive_filename (this_element
));
1921 output_bfd
= bfd_openw (output_name
, output_target
);
1922 copy_unknown_element
:
1923 delete = !copy_unknown_object (this_element
, output_bfd
);
1924 if (!bfd_close_all_done (output_bfd
))
1926 bfd_nonfatal (bfd_get_filename (output_bfd
));
1927 /* Error in new object file. Don't change archive. */
1934 unlink (output_name
);
1939 if (preserve_dates
&& stat_status
== 0)
1940 set_times (output_name
, &buf
);
1942 /* Open the newly output file and attach to our list. */
1943 output_bfd
= bfd_openr (output_name
, output_target
);
1945 l
->obfd
= output_bfd
;
1948 ptr
= &output_bfd
->next
;
1950 last_element
= this_element
;
1952 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1954 bfd_close (last_element
);
1959 if (!bfd_close (obfd
))
1960 RETURN_NONFATAL (bfd_get_filename (obfd
));
1962 if (!bfd_close (ibfd
))
1963 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1965 /* Delete all the files that we opened. */
1966 for (l
= list
; l
!= NULL
; l
= l
->next
)
1968 if (l
->obfd
== NULL
)
1972 bfd_close (l
->obfd
);
1979 /* The top-level control. */
1982 copy_file (const char *input_filename
, const char *output_filename
,
1983 const char *input_target
, const char *output_target
)
1986 char **obj_matching
;
1987 char **core_matching
;
1989 if (get_file_size (input_filename
) < 1)
1995 /* To allow us to do "strip *" without dying on the first
1996 non-object file, failures are nonfatal. */
1997 ibfd
= bfd_openr (input_filename
, input_target
);
1999 RETURN_NONFATAL (input_filename
);
2001 if (bfd_check_format (ibfd
, bfd_archive
))
2003 bfd_boolean force_output_target
;
2006 /* bfd_get_target does not return the correct value until
2007 bfd_check_format succeeds. */
2008 if (output_target
== NULL
)
2010 output_target
= bfd_get_target (ibfd
);
2011 force_output_target
= FALSE
;
2014 force_output_target
= TRUE
;
2016 obfd
= bfd_openw (output_filename
, output_target
);
2018 RETURN_NONFATAL (output_filename
);
2020 copy_archive (ibfd
, obfd
, output_target
, force_output_target
);
2022 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
2027 /* bfd_get_target does not return the correct value until
2028 bfd_check_format succeeds. */
2029 if (output_target
== NULL
)
2030 output_target
= bfd_get_target (ibfd
);
2032 obfd
= bfd_openw (output_filename
, output_target
);
2034 RETURN_NONFATAL (output_filename
);
2036 if (! copy_object (ibfd
, obfd
))
2039 if (!bfd_close (obfd
))
2040 RETURN_NONFATAL (output_filename
);
2042 if (!bfd_close (ibfd
))
2043 RETURN_NONFATAL (input_filename
);
2048 bfd_error_type obj_error
= bfd_get_error ();
2049 bfd_error_type core_error
;
2051 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
2053 /* This probably can't happen.. */
2054 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2055 free (obj_matching
);
2059 core_error
= bfd_get_error ();
2060 /* Report the object error in preference to the core error. */
2061 if (obj_error
!= core_error
)
2062 bfd_set_error (obj_error
);
2064 bfd_nonfatal (input_filename
);
2066 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2068 list_matching_formats (obj_matching
);
2069 free (obj_matching
);
2071 if (core_error
== bfd_error_file_ambiguously_recognized
)
2073 list_matching_formats (core_matching
);
2074 free (core_matching
);
2081 /* Add a name to the section renaming list. */
2084 add_section_rename (const char * old_name
, const char * new_name
,
2087 section_rename
* rename
;
2089 /* Check for conflicts first. */
2090 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2091 if (strcmp (rename
->old_name
, old_name
) == 0)
2093 /* Silently ignore duplicate definitions. */
2094 if (strcmp (rename
->new_name
, new_name
) == 0
2095 && rename
->flags
== flags
)
2098 fatal (_("Multiple renames of section %s"), old_name
);
2101 rename
= xmalloc (sizeof (* rename
));
2103 rename
->old_name
= old_name
;
2104 rename
->new_name
= new_name
;
2105 rename
->flags
= flags
;
2106 rename
->next
= section_rename_list
;
2108 section_rename_list
= rename
;
2111 /* Check the section rename list for a new name of the input section
2112 ISECTION. Return the new name if one is found.
2113 Also set RETURNED_FLAGS to the flags to be used for this section. */
2116 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2117 flagword
* returned_flags
)
2119 const char * old_name
= bfd_section_name (ibfd
, isection
);
2120 section_rename
* rename
;
2122 /* Default to using the flags of the input section. */
2123 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2125 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2126 if (strcmp (rename
->old_name
, old_name
) == 0)
2128 if (rename
->flags
!= (flagword
) -1)
2129 * returned_flags
= rename
->flags
;
2131 return rename
->new_name
;
2137 /* Once each of the sections is copied, we may still need to do some
2138 finalization work for private section headers. Do that here. */
2141 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2145 /* Allow the BFD backend to copy any private data it understands
2146 from the input section to the output section. */
2147 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2149 err
= _("private header data");
2153 /* All went well. */
2157 non_fatal (_("%s: error in %s: %s"),
2158 bfd_get_filename (ibfd
),
2159 err
, bfd_errmsg (bfd_get_error ()));
2163 /* Create a section in OBFD with the same
2164 name and attributes as ISECTION in IBFD. */
2167 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2169 bfd
*obfd
= obfdarg
;
2170 struct section_list
*p
;
2178 char *prefix
= NULL
;
2180 if (is_strip_section (ibfd
, isection
))
2183 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2187 /* Get the, possibly new, name of the output section. */
2188 name
= find_section_rename (ibfd
, isection
, & flags
);
2190 /* Prefix sections. */
2191 if ((prefix_alloc_sections_string
)
2192 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2193 prefix
= prefix_alloc_sections_string
;
2194 else if (prefix_sections_string
)
2195 prefix
= prefix_sections_string
;
2201 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
2207 if (p
!= NULL
&& p
->set_flags
)
2208 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2209 else if (strip_symbols
== STRIP_NONDEBUG
&& (flags
& SEC_ALLOC
) != 0)
2210 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2212 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2214 if (osection
== NULL
)
2220 if (strip_symbols
== STRIP_NONDEBUG
2221 && obfd
->xvec
->flavour
== bfd_target_elf_flavour
2222 && (flags
& SEC_ALLOC
) != 0
2223 && (p
== NULL
|| !p
->set_flags
))
2224 elf_section_type (osection
) = SHT_NOBITS
;
2226 size
= bfd_section_size (ibfd
, isection
);
2228 size
= (size
+ interleave
- 1) / interleave
;
2229 else if (extract_symbol
)
2231 if (! bfd_set_section_size (obfd
, osection
, size
))
2237 vma
= bfd_section_vma (ibfd
, isection
);
2238 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2240 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2243 vma
+= change_section_address
;
2245 if (! bfd_set_section_vma (obfd
, osection
, extract_symbol
? 0 : vma
))
2251 lma
= isection
->lma
;
2252 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2254 if (p
->change_lma
== CHANGE_MODIFY
)
2256 else if (p
->change_lma
== CHANGE_SET
)
2262 lma
+= change_section_address
;
2264 osection
->lma
= extract_symbol
? 0 : lma
;
2266 /* FIXME: This is probably not enough. If we change the LMA we
2267 may have to recompute the header for the file as well. */
2268 if (!bfd_set_section_alignment (obfd
,
2270 bfd_section_alignment (ibfd
, isection
)))
2272 err
= _("alignment");
2276 /* Copy merge entity size. */
2277 osection
->entsize
= isection
->entsize
;
2279 /* This used to be mangle_section; we do here to avoid using
2280 bfd_get_section_by_name since some formats allow multiple
2281 sections with the same name. */
2282 isection
->output_section
= osection
;
2283 isection
->output_offset
= extract_symbol
? vma
: 0;
2285 /* Do not copy backend data if --extract-symbol is passed; anything
2286 that needs to look at the section contents will fail. */
2290 /* Allow the BFD backend to copy any private data it understands
2291 from the input section to the output section. */
2292 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
2293 && strip_symbols
== STRIP_NONDEBUG
)
2294 /* Do not copy the private data when creating an ELF format
2295 debug info file. We do not want the program headers. */
2297 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2299 err
= _("private data");
2302 else if ((isection
->flags
& SEC_GROUP
) != 0)
2304 asymbol
*gsym
= group_signature (isection
);
2307 gsym
->flags
|= BSF_KEEP
;
2310 /* All went well. */
2314 non_fatal (_("%s: section `%s': error in %s: %s"),
2315 bfd_get_filename (ibfd
),
2316 bfd_section_name (ibfd
, isection
),
2317 err
, bfd_errmsg (bfd_get_error ()));
2321 /* Copy the data of input section ISECTION of IBFD
2322 to an output section with the same name in OBFD.
2323 If stripping then don't copy any relocation info. */
2326 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2328 bfd
*obfd
= obfdarg
;
2329 struct section_list
*p
;
2337 /* If we have already failed earlier on,
2338 do not keep on generating complaints now. */
2342 if (is_strip_section (ibfd
, isection
))
2345 flags
= bfd_get_section_flags (ibfd
, isection
);
2346 if ((flags
& SEC_GROUP
) != 0)
2349 osection
= isection
->output_section
;
2350 size
= bfd_get_section_size (isection
);
2352 if (size
== 0 || osection
== 0)
2355 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2357 /* Core files do not need to be relocated. */
2358 if (bfd_get_format (obfd
) == bfd_core
)
2362 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2366 /* Do not complain if the target does not support relocations. */
2367 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2370 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2375 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2378 relpp
= xmalloc (relsize
);
2379 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2381 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2383 if (strip_symbols
== STRIP_ALL
)
2385 /* Remove relocations which are not in
2386 keep_strip_specific_list. */
2387 arelent
**temp_relpp
;
2388 long temp_relcount
= 0;
2391 temp_relpp
= xmalloc (relsize
);
2392 for (i
= 0; i
< relcount
; i
++)
2393 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2394 keep_specific_list
))
2395 temp_relpp
[temp_relcount
++] = relpp
[i
];
2396 relcount
= temp_relcount
;
2401 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2409 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2410 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2412 void *memhunk
= xmalloc (size
);
2414 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2415 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2419 /* We don't handle leftover bytes (too many possible behaviors,
2420 and we don't know what the user wants). The section length
2421 must be a multiple of the number of bytes to swap. */
2422 if ((size
% reverse_bytes
) == 0)
2427 for (i
= 0; i
< size
; i
+= reverse_bytes
)
2428 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
2430 bfd_byte
*m
= (bfd_byte
*) memhunk
;
2433 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
2434 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
2438 /* User must pad the section up in order to do this. */
2439 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2440 bfd_section_name (ibfd
, isection
), reverse_bytes
);
2445 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2446 char *from
= (char *) memhunk
+ copy_byte
;
2448 char *end
= (char *) memhunk
+ size
;
2450 for (; from
< end
; from
+= interleave
)
2453 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2454 osection
->lma
/= interleave
;
2457 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2458 RETURN_NONFATAL (bfd_get_filename (obfd
));
2462 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2464 void *memhunk
= xmalloc (size
);
2466 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2467 flag--they can just remove the section entirely and add it
2468 back again. However, we do permit them to turn on the
2469 SEC_HAS_CONTENTS flag, and take it to mean that the section
2470 contents should be zeroed out. */
2472 memset (memhunk
, 0, size
);
2473 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2474 RETURN_NONFATAL (bfd_get_filename (obfd
));
2479 /* Get all the sections. This is used when --gap-fill or --pad-to is
2483 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2485 asection
***secppp
= secppparg
;
2487 **secppp
= osection
;
2491 /* Sort sections by VMA. This is called via qsort, and is used when
2492 --gap-fill or --pad-to is used. We force non loadable or empty
2493 sections to the front, where they are easier to ignore. */
2496 compare_section_lma (const void *arg1
, const void *arg2
)
2498 const asection
*const *sec1
= arg1
;
2499 const asection
*const *sec2
= arg2
;
2500 flagword flags1
, flags2
;
2502 /* Sort non loadable sections to the front. */
2503 flags1
= (*sec1
)->flags
;
2504 flags2
= (*sec2
)->flags
;
2505 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2506 || (flags1
& SEC_LOAD
) == 0)
2508 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2509 && (flags2
& SEC_LOAD
) != 0)
2514 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2515 || (flags2
& SEC_LOAD
) == 0)
2519 /* Sort sections by LMA. */
2520 if ((*sec1
)->lma
> (*sec2
)->lma
)
2522 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2525 /* Sort sections with the same LMA by size. */
2526 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2528 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2534 /* Mark all the symbols which will be used in output relocations with
2535 the BSF_KEEP flag so that those symbols will not be stripped.
2537 Ignore relocations which will not appear in the output file. */
2540 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2542 asymbol
**symbols
= symbolsarg
;
2547 /* Ignore an input section with no corresponding output section. */
2548 if (isection
->output_section
== NULL
)
2551 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2554 /* Do not complain if the target does not support relocations. */
2555 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2557 bfd_fatal (bfd_get_filename (ibfd
));
2563 relpp
= xmalloc (relsize
);
2564 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2566 bfd_fatal (bfd_get_filename (ibfd
));
2568 /* Examine each symbol used in a relocation. If it's not one of the
2569 special bfd section symbols, then mark it with BSF_KEEP. */
2570 for (i
= 0; i
< relcount
; i
++)
2572 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2573 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2574 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2575 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2582 /* Write out debugging information. */
2585 write_debugging_info (bfd
*obfd
, void *dhandle
,
2586 long *symcountp ATTRIBUTE_UNUSED
,
2587 asymbol
***symppp ATTRIBUTE_UNUSED
)
2589 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2590 return write_ieee_debugging_info (obfd
, dhandle
);
2592 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2593 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2595 bfd_byte
*syms
, *strings
;
2596 bfd_size_type symsize
, stringsize
;
2597 asection
*stabsec
, *stabstrsec
;
2600 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2605 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2606 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2607 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2609 || stabstrsec
== NULL
2610 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2611 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2612 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2613 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2615 non_fatal (_("%s: can't create debugging section: %s"),
2616 bfd_get_filename (obfd
),
2617 bfd_errmsg (bfd_get_error ()));
2621 /* We can get away with setting the section contents now because
2622 the next thing the caller is going to do is copy over the
2623 real sections. We may someday have to split the contents
2624 setting out of this function. */
2625 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2626 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2629 non_fatal (_("%s: can't set debugging section contents: %s"),
2630 bfd_get_filename (obfd
),
2631 bfd_errmsg (bfd_get_error ()));
2638 non_fatal (_("%s: don't know how to write debugging information for %s"),
2639 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2644 strip_main (int argc
, char *argv
[])
2646 char *input_target
= NULL
;
2647 char *output_target
= NULL
;
2648 bfd_boolean show_version
= FALSE
;
2649 bfd_boolean formats_info
= FALSE
;
2652 struct section_list
*p
;
2653 char *output_file
= NULL
;
2655 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2656 strip_options
, (int *) 0)) != EOF
)
2661 input_target
= optarg
;
2664 output_target
= optarg
;
2667 input_target
= output_target
= optarg
;
2670 p
= find_section_list (optarg
, TRUE
);
2672 sections_removed
= TRUE
;
2675 strip_symbols
= STRIP_ALL
;
2679 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2680 strip_symbols
= STRIP_DEBUG
;
2682 case OPTION_STRIP_UNNEEDED
:
2683 strip_symbols
= STRIP_UNNEEDED
;
2686 add_specific_symbol (optarg
, &keep_specific_list
);
2689 add_specific_symbol (optarg
, &strip_specific_list
);
2692 output_file
= optarg
;
2695 preserve_dates
= TRUE
;
2698 discard_locals
= LOCALS_ALL
;
2701 discard_locals
= LOCALS_START_L
;
2707 show_version
= TRUE
;
2709 case OPTION_FORMATS_INFO
:
2710 formats_info
= TRUE
;
2712 case OPTION_ONLY_KEEP_DEBUG
:
2713 strip_symbols
= STRIP_NONDEBUG
;
2715 case OPTION_KEEP_FILE_SYMBOLS
:
2716 keep_file_symbols
= 1;
2719 /* We've been given a long option. */
2726 strip_usage (stdout
, 0);
2728 strip_usage (stderr
, 1);
2739 print_version ("strip");
2741 /* Default is to strip all symbols. */
2742 if (strip_symbols
== STRIP_UNDEF
2743 && discard_locals
== LOCALS_UNDEF
2744 && strip_specific_list
== NULL
)
2745 strip_symbols
= STRIP_ALL
;
2747 if (output_target
== NULL
)
2748 output_target
= input_target
;
2752 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2753 strip_usage (stderr
, 1);
2755 for (; i
< argc
; i
++)
2757 int hold_status
= status
;
2758 struct stat statbuf
;
2761 if (get_file_size (argv
[i
]) < 1)
2768 /* No need to check the return value of stat().
2769 It has already been checked in get_file_size(). */
2770 stat (argv
[i
], &statbuf
);
2772 if (output_file
== NULL
|| strcmp (argv
[i
], output_file
) == 0)
2773 tmpname
= make_tempname (argv
[i
]);
2775 tmpname
= output_file
;
2777 if (tmpname
== NULL
)
2779 non_fatal (_("could not create temporary file to hold stripped copy of '%s'"),
2786 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2790 set_times (tmpname
, &statbuf
);
2791 if (output_file
!= tmpname
)
2792 smart_rename (tmpname
, output_file
? output_file
: argv
[i
],
2794 status
= hold_status
;
2797 unlink_if_ordinary (tmpname
);
2798 if (output_file
!= tmpname
)
2806 copy_main (int argc
, char *argv
[])
2808 char * binary_architecture
= NULL
;
2809 char *input_filename
= NULL
;
2810 char *output_filename
= NULL
;
2812 char *input_target
= NULL
;
2813 char *output_target
= NULL
;
2814 bfd_boolean show_version
= FALSE
;
2815 bfd_boolean change_warn
= TRUE
;
2816 bfd_boolean formats_info
= FALSE
;
2818 struct section_list
*p
;
2819 struct stat statbuf
;
2821 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2822 copy_options
, (int *) 0)) != EOF
)
2827 copy_byte
= atoi (optarg
);
2829 fatal (_("byte number must be non-negative"));
2833 binary_architecture
= optarg
;
2837 interleave
= atoi (optarg
);
2839 fatal (_("interleave must be positive"));
2843 case 's': /* "source" - 'I' is preferred */
2844 input_target
= optarg
;
2848 case 'd': /* "destination" - 'O' is preferred */
2849 output_target
= optarg
;
2853 input_target
= output_target
= optarg
;
2857 p
= find_section_list (optarg
, TRUE
);
2859 fatal (_("%s both copied and removed"), optarg
);
2861 sections_copied
= TRUE
;
2865 p
= find_section_list (optarg
, TRUE
);
2867 fatal (_("%s both copied and removed"), optarg
);
2869 sections_removed
= TRUE
;
2873 strip_symbols
= STRIP_ALL
;
2877 strip_symbols
= STRIP_DEBUG
;
2880 case OPTION_STRIP_UNNEEDED
:
2881 strip_symbols
= STRIP_UNNEEDED
;
2884 case OPTION_ONLY_KEEP_DEBUG
:
2885 strip_symbols
= STRIP_NONDEBUG
;
2888 case OPTION_KEEP_FILE_SYMBOLS
:
2889 keep_file_symbols
= 1;
2892 case OPTION_ADD_GNU_DEBUGLINK
:
2893 gnu_debuglink_filename
= optarg
;
2897 add_specific_symbol (optarg
, &keep_specific_list
);
2901 add_specific_symbol (optarg
, &strip_specific_list
);
2904 case OPTION_STRIP_UNNEEDED_SYMBOL
:
2905 add_specific_symbol (optarg
, &strip_unneeded_list
);
2909 add_specific_symbol (optarg
, &localize_specific_list
);
2912 case OPTION_GLOBALIZE_SYMBOL
:
2913 add_specific_symbol (optarg
, &globalize_specific_list
);
2917 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2921 add_specific_symbol (optarg
, &weaken_specific_list
);
2925 preserve_dates
= TRUE
;
2933 discard_locals
= LOCALS_ALL
;
2937 discard_locals
= LOCALS_START_L
;
2945 show_version
= TRUE
;
2948 case OPTION_FORMATS_INFO
:
2949 formats_info
= TRUE
;
2956 case OPTION_ADD_SECTION
:
2960 struct section_add
*pa
;
2965 s
= strchr (optarg
, '=');
2968 fatal (_("bad format for %s"), "--add-section");
2970 size
= get_file_size (s
+ 1);
2977 pa
= xmalloc (sizeof (struct section_add
));
2980 name
= xmalloc (len
+ 1);
2981 strncpy (name
, optarg
, len
);
2985 pa
->filename
= s
+ 1;
2987 pa
->contents
= xmalloc (size
);
2989 f
= fopen (pa
->filename
, FOPEN_RB
);
2992 fatal (_("cannot open: %s: %s"),
2993 pa
->filename
, strerror (errno
));
2995 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2997 fatal (_("%s: fread failed"), pa
->filename
);
3001 pa
->next
= add_sections
;
3006 case OPTION_CHANGE_START
:
3007 change_start
= parse_vma (optarg
, "--change-start");
3010 case OPTION_CHANGE_SECTION_ADDRESS
:
3011 case OPTION_CHANGE_SECTION_LMA
:
3012 case OPTION_CHANGE_SECTION_VMA
:
3017 char *option
= NULL
;
3019 enum change_action what
= CHANGE_IGNORE
;
3023 case OPTION_CHANGE_SECTION_ADDRESS
:
3024 option
= "--change-section-address";
3026 case OPTION_CHANGE_SECTION_LMA
:
3027 option
= "--change-section-lma";
3029 case OPTION_CHANGE_SECTION_VMA
:
3030 option
= "--change-section-vma";
3034 s
= strchr (optarg
, '=');
3037 s
= strchr (optarg
, '+');
3040 s
= strchr (optarg
, '-');
3042 fatal (_("bad format for %s"), option
);
3047 name
= xmalloc (len
+ 1);
3048 strncpy (name
, optarg
, len
);
3051 p
= find_section_list (name
, TRUE
);
3053 val
= parse_vma (s
+ 1, option
);
3057 case '=': what
= CHANGE_SET
; break;
3058 case '-': val
= - val
; /* Drop through. */
3059 case '+': what
= CHANGE_MODIFY
; break;
3064 case OPTION_CHANGE_SECTION_ADDRESS
:
3065 p
->change_vma
= what
;
3069 case OPTION_CHANGE_SECTION_LMA
:
3070 p
->change_lma
= what
;
3074 case OPTION_CHANGE_SECTION_VMA
:
3075 p
->change_vma
= what
;
3082 case OPTION_CHANGE_ADDRESSES
:
3083 change_section_address
= parse_vma (optarg
, "--change-addresses");
3084 change_start
= change_section_address
;
3087 case OPTION_CHANGE_WARNINGS
:
3091 case OPTION_CHANGE_LEADING_CHAR
:
3092 change_leading_char
= TRUE
;
3095 case OPTION_DEBUGGING
:
3096 convert_debugging
= TRUE
;
3099 case OPTION_GAP_FILL
:
3101 bfd_vma gap_fill_vma
;
3103 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
3104 gap_fill
= (bfd_byte
) gap_fill_vma
;
3105 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
3109 sprintf_vma (buff
, gap_fill_vma
);
3111 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3114 gap_fill_set
= TRUE
;
3118 case OPTION_NO_CHANGE_WARNINGS
:
3119 change_warn
= FALSE
;
3123 pad_to
= parse_vma (optarg
, "--pad-to");
3127 case OPTION_REMOVE_LEADING_CHAR
:
3128 remove_leading_char
= TRUE
;
3131 case OPTION_REDEFINE_SYM
:
3133 /* Push this redefinition onto redefine_symbol_list. */
3137 const char *nextarg
;
3138 char *source
, *target
;
3140 s
= strchr (optarg
, '=');
3142 fatal (_("bad format for %s"), "--redefine-sym");
3145 source
= xmalloc (len
+ 1);
3146 strncpy (source
, optarg
, len
);
3150 len
= strlen (nextarg
);
3151 target
= xmalloc (len
+ 1);
3152 strcpy (target
, nextarg
);
3154 redefine_list_append ("--redefine-sym", source
, target
);
3161 case OPTION_REDEFINE_SYMS
:
3162 add_redefine_syms_file (optarg
);
3165 case OPTION_SET_SECTION_FLAGS
:
3171 s
= strchr (optarg
, '=');
3173 fatal (_("bad format for %s"), "--set-section-flags");
3176 name
= xmalloc (len
+ 1);
3177 strncpy (name
, optarg
, len
);
3180 p
= find_section_list (name
, TRUE
);
3182 p
->set_flags
= TRUE
;
3183 p
->flags
= parse_flags (s
+ 1);
3187 case OPTION_RENAME_SECTION
:
3190 const char *eq
, *fl
;
3195 eq
= strchr (optarg
, '=');
3197 fatal (_("bad format for %s"), "--rename-section");
3201 fatal (_("bad format for %s"), "--rename-section");
3203 old_name
= xmalloc (len
+ 1);
3204 strncpy (old_name
, optarg
, len
);
3208 fl
= strchr (eq
, ',');
3211 flags
= parse_flags (fl
+ 1);
3221 fatal (_("bad format for %s"), "--rename-section");
3223 new_name
= xmalloc (len
+ 1);
3224 strncpy (new_name
, eq
, len
);
3227 add_section_rename (old_name
, new_name
, flags
);
3231 case OPTION_SET_START
:
3232 set_start
= parse_vma (optarg
, "--set-start");
3233 set_start_set
= TRUE
;
3236 case OPTION_SREC_LEN
:
3237 Chunk
= parse_vma (optarg
, "--srec-len");
3240 case OPTION_SREC_FORCES3
:
3244 case OPTION_STRIP_SYMBOLS
:
3245 add_specific_symbols (optarg
, &strip_specific_list
);
3248 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3249 add_specific_symbols (optarg
, &strip_unneeded_list
);
3252 case OPTION_KEEP_SYMBOLS
:
3253 add_specific_symbols (optarg
, &keep_specific_list
);
3256 case OPTION_LOCALIZE_HIDDEN
:
3257 localize_hidden
= TRUE
;
3260 case OPTION_LOCALIZE_SYMBOLS
:
3261 add_specific_symbols (optarg
, &localize_specific_list
);
3264 case OPTION_GLOBALIZE_SYMBOLS
:
3265 add_specific_symbols (optarg
, &globalize_specific_list
);
3268 case OPTION_KEEPGLOBAL_SYMBOLS
:
3269 add_specific_symbols (optarg
, &keepglobal_specific_list
);
3272 case OPTION_WEAKEN_SYMBOLS
:
3273 add_specific_symbols (optarg
, &weaken_specific_list
);
3276 case OPTION_ALT_MACH_CODE
:
3277 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3278 if (use_alt_mach_code
== 0)
3279 fatal (_("unable to parse alternative machine code"));
3282 case OPTION_PREFIX_SYMBOLS
:
3283 prefix_symbols_string
= optarg
;
3286 case OPTION_PREFIX_SECTIONS
:
3287 prefix_sections_string
= optarg
;
3290 case OPTION_PREFIX_ALLOC_SECTIONS
:
3291 prefix_alloc_sections_string
= optarg
;
3294 case OPTION_READONLY_TEXT
:
3295 bfd_flags_to_set
|= WP_TEXT
;
3296 bfd_flags_to_clear
&= ~WP_TEXT
;
3299 case OPTION_WRITABLE_TEXT
:
3300 bfd_flags_to_clear
|= WP_TEXT
;
3301 bfd_flags_to_set
&= ~WP_TEXT
;
3305 bfd_flags_to_set
|= D_PAGED
;
3306 bfd_flags_to_clear
&= ~D_PAGED
;
3310 bfd_flags_to_clear
|= D_PAGED
;
3311 bfd_flags_to_set
&= ~D_PAGED
;
3314 case OPTION_EXTRACT_SYMBOL
:
3315 extract_symbol
= TRUE
;
3318 case OPTION_REVERSE_BYTES
:
3320 int prev
= reverse_bytes
;
3322 reverse_bytes
= atoi (optarg
);
3323 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
3324 fatal (_("number of bytes to reverse must be positive and even"));
3326 if (prev
&& prev
!= reverse_bytes
)
3327 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3333 /* We've been given a long option. */
3338 copy_usage (stdout
, 0);
3341 copy_usage (stderr
, 1);
3352 print_version ("objcopy");
3354 if (copy_byte
>= interleave
)
3355 fatal (_("byte number must be less than interleave"));
3357 if (optind
== argc
|| optind
+ 2 < argc
)
3358 copy_usage (stderr
, 1);
3360 input_filename
= argv
[optind
];
3361 if (optind
+ 1 < argc
)
3362 output_filename
= argv
[optind
+ 1];
3364 /* Default is to strip no symbols. */
3365 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3366 strip_symbols
= STRIP_NONE
;
3368 if (output_target
== NULL
)
3369 output_target
= input_target
;
3371 if (binary_architecture
!= NULL
)
3373 if (input_target
&& strcmp (input_target
, "binary") == 0)
3375 const bfd_arch_info_type
* temp_arch_info
;
3377 temp_arch_info
= bfd_scan_arch (binary_architecture
);
3379 if (temp_arch_info
!= NULL
)
3381 bfd_external_binary_architecture
= temp_arch_info
->arch
;
3382 bfd_external_machine
= temp_arch_info
->mach
;
3385 fatal (_("architecture %s unknown"), binary_architecture
);
3389 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3390 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3395 if (stat (input_filename
, & statbuf
) < 0)
3396 fatal (_("warning: could not locate '%s'. System error message: %s"),
3397 input_filename
, strerror (errno
));
3399 /* If there is no destination file, or the source and destination files
3400 are the same, then create a temp and rename the result into the input. */
3401 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3402 tmpname
= make_tempname (input_filename
);
3404 tmpname
= output_filename
;
3406 if (tmpname
== NULL
)
3407 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3408 input_filename
, strerror (errno
));
3410 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3414 set_times (tmpname
, &statbuf
);
3415 if (tmpname
!= output_filename
)
3416 smart_rename (tmpname
, input_filename
, preserve_dates
);
3419 unlink_if_ordinary (tmpname
);
3423 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3427 if (p
->change_vma
!= CHANGE_IGNORE
)
3431 sprintf_vma (buff
, p
->vma_val
);
3433 /* xgettext:c-format */
3434 non_fatal (_("%s %s%c0x%s never used"),
3435 "--change-section-vma",
3437 p
->change_vma
== CHANGE_SET
? '=' : '+',
3441 if (p
->change_lma
!= CHANGE_IGNORE
)
3445 sprintf_vma (buff
, p
->lma_val
);
3447 /* xgettext:c-format */
3448 non_fatal (_("%s %s%c0x%s never used"),
3449 "--change-section-lma",
3451 p
->change_lma
== CHANGE_SET
? '=' : '+',
3462 main (int argc
, char *argv
[])
3464 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3465 setlocale (LC_MESSAGES
, "");
3467 #if defined (HAVE_SETLOCALE)
3468 setlocale (LC_CTYPE
, "");
3470 bindtextdomain (PACKAGE
, LOCALEDIR
);
3471 textdomain (PACKAGE
);
3473 program_name
= argv
[0];
3474 xmalloc_set_program_name (program_name
);
3476 START_PROGRESS (program_name
, 0);
3478 expandargv (&argc
, &argv
);
3480 strip_symbols
= STRIP_UNDEF
;
3481 discard_locals
= LOCALS_UNDEF
;
3484 set_default_bfd_target ();
3488 int i
= strlen (program_name
);
3489 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3490 /* Drop the .exe suffix, if any. */
3491 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3494 program_name
[i
] = '\0';
3497 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3501 strip_main (argc
, argv
);
3503 copy_main (argc
, argv
);
3505 END_PROGRESS (program_name
);