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 3 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(bfd) \
69 status = 1; bfd_nonfatal_message (NULL, bfd, NULL, NULL); return; \
72 static asymbol
**isympp
= NULL
; /* Input symbols. */
73 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
75 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
76 static int copy_byte
= -1;
77 static int interleave
= 4;
79 static bfd_boolean verbose
; /* Print file and target names. */
80 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
81 static int status
= 0; /* Exit status. */
86 STRIP_NONE
, /* Don't strip. */
87 STRIP_DEBUG
, /* Strip all debugger symbols. */
88 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
89 STRIP_NONDEBUG
, /* Strip everything but debug info. */
90 STRIP_ALL
/* Strip all symbols. */
93 /* Which symbols to remove. */
94 static enum strip_action strip_symbols
;
99 LOCALS_START_L
, /* Discard locals starting with L. */
100 LOCALS_ALL
/* Discard all locals. */
103 /* Which local symbols to remove. Overrides STRIP_ALL. */
104 static enum locals_action discard_locals
;
106 /* What kind of change to perform. */
114 /* Structure used to hold lists of sections and actions to take. */
117 struct section_list
* next
; /* Next section to change. */
118 const char * name
; /* Section name. */
119 bfd_boolean used
; /* Whether this entry was used. */
120 bfd_boolean remove
; /* Whether to remove this section. */
121 bfd_boolean copy
; /* Whether to copy this section. */
122 enum change_action change_vma
;/* Whether to change or set VMA. */
123 bfd_vma vma_val
; /* Amount to change by or set to. */
124 enum change_action change_lma
;/* Whether to change or set LMA. */
125 bfd_vma lma_val
; /* Amount to change by or set to. */
126 bfd_boolean set_flags
; /* Whether to set the section flags. */
127 flagword flags
; /* What to set the section flags to. */
130 static struct section_list
*change_sections
;
132 /* TRUE if some sections are to be removed. */
133 static bfd_boolean sections_removed
;
135 /* TRUE if only some sections are to be copied. */
136 static bfd_boolean sections_copied
;
138 /* Changes to the start address. */
139 static bfd_vma change_start
= 0;
140 static bfd_boolean set_start_set
= FALSE
;
141 static bfd_vma set_start
;
143 /* Changes to section addresses. */
144 static bfd_vma change_section_address
= 0;
146 /* Filling gaps between sections. */
147 static bfd_boolean gap_fill_set
= FALSE
;
148 static bfd_byte gap_fill
= 0;
150 /* Pad to a given address. */
151 static bfd_boolean pad_to_set
= FALSE
;
152 static bfd_vma pad_to
;
154 /* Use alternative machine code? */
155 static unsigned long use_alt_mach_code
= 0;
157 /* Output BFD flags user wants to set or clear */
158 static flagword bfd_flags_to_set
;
159 static flagword bfd_flags_to_clear
;
161 /* List of sections to add. */
164 /* Next section to add. */
165 struct section_add
*next
;
166 /* Name of section to add. */
168 /* Name of file holding section contents. */
169 const char *filename
;
172 /* Contents of file. */
174 /* BFD section, after it has been added. */
178 /* List of sections to add to the output BFD. */
179 static struct section_add
*add_sections
;
181 /* If non-NULL the argument to --add-gnu-debuglink.
182 This should be the filename to store in the .gnu_debuglink section. */
183 static const char * gnu_debuglink_filename
= NULL
;
185 /* Whether to convert debugging information. */
186 static bfd_boolean convert_debugging
= FALSE
;
188 /* Whether to change the leading character in symbol names. */
189 static bfd_boolean change_leading_char
= FALSE
;
191 /* Whether to remove the leading character from global symbol names. */
192 static bfd_boolean remove_leading_char
= FALSE
;
194 /* Whether to permit wildcard in symbol comparison. */
195 static bfd_boolean wildcard
= FALSE
;
197 /* True if --localize-hidden is in effect. */
198 static bfd_boolean localize_hidden
= FALSE
;
200 /* List of symbols to strip, keep, localize, keep-global, weaken,
202 static struct symlist
*strip_specific_list
= NULL
;
203 static struct symlist
*strip_unneeded_list
= NULL
;
204 static struct symlist
*keep_specific_list
= NULL
;
205 static struct symlist
*localize_specific_list
= NULL
;
206 static struct symlist
*globalize_specific_list
= NULL
;
207 static struct symlist
*keepglobal_specific_list
= NULL
;
208 static struct symlist
*weaken_specific_list
= NULL
;
209 static struct redefine_node
*redefine_sym_list
= NULL
;
211 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
212 static bfd_boolean weaken
= FALSE
;
214 /* If this is TRUE, we retain BSF_FILE symbols. */
215 static bfd_boolean keep_file_symbols
= FALSE
;
217 /* Prefix symbols/sections. */
218 static char *prefix_symbols_string
= 0;
219 static char *prefix_sections_string
= 0;
220 static char *prefix_alloc_sections_string
= 0;
222 /* True if --extract-symbol was passed on the command line. */
223 static bfd_boolean extract_symbol
= FALSE
;
225 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
226 of <reverse_bytes> bytes within each output section. */
227 static int reverse_bytes
= 0;
230 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
231 enum command_line_switch
233 OPTION_ADD_SECTION
=150,
234 OPTION_CHANGE_ADDRESSES
,
235 OPTION_CHANGE_LEADING_CHAR
,
237 OPTION_CHANGE_SECTION_ADDRESS
,
238 OPTION_CHANGE_SECTION_LMA
,
239 OPTION_CHANGE_SECTION_VMA
,
240 OPTION_CHANGE_WARNINGS
,
243 OPTION_NO_CHANGE_WARNINGS
,
245 OPTION_REMOVE_LEADING_CHAR
,
246 OPTION_SET_SECTION_FLAGS
,
248 OPTION_STRIP_UNNEEDED
,
251 OPTION_REDEFINE_SYMS
,
254 OPTION_STRIP_SYMBOLS
,
255 OPTION_STRIP_UNNEEDED_SYMBOL
,
256 OPTION_STRIP_UNNEEDED_SYMBOLS
,
258 OPTION_LOCALIZE_HIDDEN
,
259 OPTION_LOCALIZE_SYMBOLS
,
260 OPTION_GLOBALIZE_SYMBOL
,
261 OPTION_GLOBALIZE_SYMBOLS
,
262 OPTION_KEEPGLOBAL_SYMBOLS
,
263 OPTION_WEAKEN_SYMBOLS
,
264 OPTION_RENAME_SECTION
,
265 OPTION_ALT_MACH_CODE
,
266 OPTION_PREFIX_SYMBOLS
,
267 OPTION_PREFIX_SECTIONS
,
268 OPTION_PREFIX_ALLOC_SECTIONS
,
270 OPTION_ADD_GNU_DEBUGLINK
,
271 OPTION_ONLY_KEEP_DEBUG
,
272 OPTION_KEEP_FILE_SYMBOLS
,
273 OPTION_READONLY_TEXT
,
274 OPTION_WRITABLE_TEXT
,
277 OPTION_EXTRACT_SYMBOL
,
281 /* Options to handle if running as "strip". */
283 static struct option strip_options
[] =
285 {"discard-all", no_argument
, 0, 'x'},
286 {"discard-locals", no_argument
, 0, 'X'},
287 {"format", required_argument
, 0, 'F'}, /* Obsolete */
288 {"help", no_argument
, 0, 'h'},
289 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
290 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
291 {"input-target", required_argument
, 0, 'I'},
292 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
293 {"keep-symbol", required_argument
, 0, 'K'},
294 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
295 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
296 {"output-target", required_argument
, 0, 'O'},
297 {"output-file", required_argument
, 0, 'o'},
298 {"preserve-dates", no_argument
, 0, 'p'},
299 {"remove-section", required_argument
, 0, 'R'},
300 {"strip-all", no_argument
, 0, 's'},
301 {"strip-debug", no_argument
, 0, 'S'},
302 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
303 {"strip-symbol", required_argument
, 0, 'N'},
304 {"target", required_argument
, 0, 'F'},
305 {"verbose", no_argument
, 0, 'v'},
306 {"version", no_argument
, 0, 'V'},
307 {"wildcard", no_argument
, 0, 'w'},
308 {0, no_argument
, 0, 0}
311 /* Options to handle if running as "objcopy". */
313 static struct option copy_options
[] =
315 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
316 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
317 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
318 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
319 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
320 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
321 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
322 {"binary-architecture", required_argument
, 0, 'B'},
323 {"byte", required_argument
, 0, 'b'},
324 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
325 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
326 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
327 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
328 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
329 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
330 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
331 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
332 {"discard-all", no_argument
, 0, 'x'},
333 {"discard-locals", no_argument
, 0, 'X'},
334 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
335 {"format", required_argument
, 0, 'F'}, /* Obsolete */
336 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
337 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
338 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
339 {"help", no_argument
, 0, 'h'},
340 {"impure", no_argument
, 0, OPTION_IMPURE
},
341 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
342 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
343 {"input-target", required_argument
, 0, 'I'},
344 {"interleave", required_argument
, 0, 'i'},
345 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
346 {"keep-global-symbol", required_argument
, 0, 'G'},
347 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
348 {"keep-symbol", required_argument
, 0, 'K'},
349 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
350 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
351 {"localize-symbol", required_argument
, 0, 'L'},
352 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
353 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
354 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
355 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
356 {"only-section", required_argument
, 0, 'j'},
357 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
358 {"output-target", required_argument
, 0, 'O'},
359 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
360 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
361 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
362 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
363 {"preserve-dates", no_argument
, 0, 'p'},
364 {"pure", no_argument
, 0, OPTION_PURE
},
365 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
366 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
367 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
368 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
369 {"remove-section", required_argument
, 0, 'R'},
370 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
371 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
372 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
373 {"set-start", required_argument
, 0, OPTION_SET_START
},
374 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
375 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
376 {"strip-all", no_argument
, 0, 'S'},
377 {"strip-debug", no_argument
, 0, 'g'},
378 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
379 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
380 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
381 {"strip-symbol", required_argument
, 0, 'N'},
382 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
383 {"target", required_argument
, 0, 'F'},
384 {"verbose", no_argument
, 0, 'v'},
385 {"version", no_argument
, 0, 'V'},
386 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
387 {"weaken-symbol", required_argument
, 0, 'W'},
388 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
389 {"wildcard", no_argument
, 0, 'w'},
390 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
391 {0, no_argument
, 0, 0}
395 extern char *program_name
;
397 /* This flag distinguishes between strip and objcopy:
398 1 means this is 'strip'; 0 means this is 'objcopy'.
399 -1 means if we should use argv[0] to decide. */
402 /* The maximum length of an S record. This variable is declared in srec.c
403 and can be modified by the --srec-len parameter. */
404 extern unsigned int Chunk
;
406 /* Restrict the generation of Srecords to type S3 only.
407 This variable is declare in bfd/srec.c and can be toggled
408 on by the --srec-forceS3 command line switch. */
409 extern bfd_boolean S3Forced
;
411 /* Defined in bfd/binary.c. Used to set architecture and machine of input
413 extern enum bfd_architecture bfd_external_binary_architecture
;
414 extern unsigned long bfd_external_machine
;
416 /* Forward declarations. */
417 static void setup_section (bfd
*, asection
*, void *);
418 static void setup_bfd_headers (bfd
*, bfd
*);
419 static void copy_section (bfd
*, asection
*, void *);
420 static void get_sections (bfd
*, asection
*, void *);
421 static int compare_section_lma (const void *, const void *);
422 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
423 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
424 static const char *lookup_sym_redefinition (const char *);
427 copy_usage (FILE *stream
, int exit_status
)
429 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
430 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
431 fprintf (stream
, _(" The options are:\n"));
432 fprintf (stream
, _("\
433 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
434 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
435 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
436 -F --target <bfdname> Set both input and output format to <bfdname>\n\
437 --debugging Convert debugging information, if possible\n\
438 -p --preserve-dates Copy modified/access timestamps to the output\n\
439 -j --only-section <name> Only copy section <name> into the output\n\
440 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
441 -R --remove-section <name> Remove section <name> from the output\n\
442 -S --strip-all Remove all symbol and relocation information\n\
443 -g --strip-debug Remove all debugging symbols & sections\n\
444 --strip-unneeded Remove all symbols not needed by relocations\n\
445 -N --strip-symbol <name> Do not copy symbol <name>\n\
446 --strip-unneeded-symbol <name>\n\
447 Do not copy symbol <name> unless needed by\n\
449 --only-keep-debug Strip everything but the debug information\n\
450 --extract-symbol Remove section contents but keep symbols\n\
451 -K --keep-symbol <name> Do not strip symbol <name>\n\
452 --keep-file-symbols Do not strip file symbol(s)\n\
453 --localize-hidden Turn all ELF hidden symbols into locals\n\
454 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
455 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
456 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
457 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
458 --weaken Force all global symbols to be marked as weak\n\
459 -w --wildcard Permit wildcard in symbol comparison\n\
460 -x --discard-all Remove all non-global symbols\n\
461 -X --discard-locals Remove any compiler-generated symbols\n\
462 -i --interleave <number> Only copy one out of every <number> bytes\n\
463 -b --byte <num> Select byte <num> in every interleaved block\n\
464 --gap-fill <val> Fill gaps between sections with <val>\n\
465 --pad-to <addr> Pad the last section up to address <addr>\n\
466 --set-start <addr> Set the start address to <addr>\n\
467 {--change-start|--adjust-start} <incr>\n\
468 Add <incr> to the start address\n\
469 {--change-addresses|--adjust-vma} <incr>\n\
470 Add <incr> to LMA, VMA and start addresses\n\
471 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
472 Change LMA and VMA of section <name> by <val>\n\
473 --change-section-lma <name>{=|+|-}<val>\n\
474 Change the LMA of section <name> by <val>\n\
475 --change-section-vma <name>{=|+|-}<val>\n\
476 Change the VMA of section <name> by <val>\n\
477 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
478 Warn if a named section does not exist\n\
479 --set-section-flags <name>=<flags>\n\
480 Set section <name>'s properties to <flags>\n\
481 --add-section <name>=<file> Add section <name> found in <file> to output\n\
482 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
483 --change-leading-char Force output format's leading character style\n\
484 --remove-leading-char Remove leading character from global symbols\n\
485 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
486 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
487 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
489 --srec-len <number> Restrict the length of generated Srecords\n\
490 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
491 --strip-symbols <file> -N for all symbols listed in <file>\n\
492 --strip-unneeded-symbols <file>\n\
493 --strip-unneeded-symbol for all symbols listed\n\
495 --keep-symbols <file> -K for all symbols listed in <file>\n\
496 --localize-symbols <file> -L for all symbols listed in <file>\n\
497 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
498 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
499 --weaken-symbols <file> -W for all symbols listed in <file>\n\
500 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
501 --writable-text Mark the output text as writable\n\
502 --readonly-text Make the output text write protected\n\
503 --pure Mark the output file as demand paged\n\
504 --impure Mark the output file as impure\n\
505 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
506 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
507 --prefix-alloc-sections <prefix>\n\
508 Add <prefix> to start of every allocatable\n\
510 -v --verbose List all object files modified\n\
511 @<file> Read options from <file>\n\
512 -V --version Display this program's version number\n\
513 -h --help Display this output\n\
514 --info List object formats & architectures supported\n\
516 list_supported_targets (program_name
, stream
);
517 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
518 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
523 strip_usage (FILE *stream
, int exit_status
)
525 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
526 fprintf (stream
, _(" Removes symbols and sections from files\n"));
527 fprintf (stream
, _(" The options are:\n"));
528 fprintf (stream
, _("\
529 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
530 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
531 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
532 -p --preserve-dates Copy modified/access timestamps to the output\n\
533 -R --remove-section=<name> Remove section <name> from the output\n\
534 -s --strip-all Remove all symbol and relocation information\n\
535 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
536 --strip-unneeded Remove all symbols not needed by relocations\n\
537 --only-keep-debug Strip everything but the debug information\n\
538 -N --strip-symbol=<name> Do not copy symbol <name>\n\
539 -K --keep-symbol=<name> Do not strip symbol <name>\n\
540 --keep-file-symbols Do not strip file symbol(s)\n\
541 -w --wildcard Permit wildcard in symbol comparison\n\
542 -x --discard-all Remove all non-global symbols\n\
543 -X --discard-locals Remove any compiler-generated symbols\n\
544 -v --verbose List all object files modified\n\
545 -V --version Display this program's version number\n\
546 -h --help Display this output\n\
547 --info List object formats & architectures supported\n\
548 -o <file> Place stripped output into <file>\n\
551 list_supported_targets (program_name
, stream
);
552 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
553 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
557 /* Parse section flags into a flagword, with a fatal error if the
558 string can't be parsed. */
561 parse_flags (const char *s
)
571 snext
= strchr (s
, ',');
581 #define PARSE_FLAG(fname,fval) \
582 else if (strncasecmp (fname, s, len) == 0) ret |= fval
583 PARSE_FLAG ("alloc", SEC_ALLOC
);
584 PARSE_FLAG ("load", SEC_LOAD
);
585 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
586 PARSE_FLAG ("readonly", SEC_READONLY
);
587 PARSE_FLAG ("debug", SEC_DEBUGGING
);
588 PARSE_FLAG ("code", SEC_CODE
);
589 PARSE_FLAG ("data", SEC_DATA
);
590 PARSE_FLAG ("rom", SEC_ROM
);
591 PARSE_FLAG ("share", SEC_COFF_SHARED
);
592 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
598 copy
= xmalloc (len
+ 1);
599 strncpy (copy
, s
, len
);
601 non_fatal (_("unrecognized section flag `%s'"), copy
);
602 fatal (_("supported flags: %s"),
603 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
613 /* Find and optionally add an entry in the change_sections list. */
615 static struct section_list
*
616 find_section_list (const char *name
, bfd_boolean add
)
618 struct section_list
*p
;
620 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
621 if (strcmp (p
->name
, name
) == 0)
627 p
= xmalloc (sizeof (struct section_list
));
632 p
->change_vma
= CHANGE_IGNORE
;
633 p
->change_lma
= CHANGE_IGNORE
;
636 p
->set_flags
= FALSE
;
639 p
->next
= change_sections
;
645 /* Add a symbol to strip_specific_list. */
648 add_specific_symbol (const char *name
, struct symlist
**list
)
650 struct symlist
*tmp_list
;
652 tmp_list
= xmalloc (sizeof (struct symlist
));
653 tmp_list
->name
= name
;
654 tmp_list
->next
= *list
;
658 /* Add symbols listed in `filename' to strip_specific_list. */
660 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
661 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
664 add_specific_symbols (const char *filename
, struct symlist
**list
)
670 unsigned int line_count
;
672 size
= get_file_size (filename
);
679 buffer
= xmalloc (size
+ 2);
680 f
= fopen (filename
, FOPEN_RT
);
682 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
684 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
685 fatal (_("%s: fread failed"), filename
);
688 buffer
[size
] = '\n';
689 buffer
[size
+ 1] = '\0';
693 for (line
= buffer
; * line
!= '\0'; line
++)
698 int finished
= FALSE
;
700 for (eol
= line
;; eol
++)
706 /* Cope with \n\r. */
714 /* Cope with \r\n. */
725 /* Line comment, Terminate the line here, in case a
726 name is present and then allow the rest of the
727 loop to find the real end of the line. */
739 /* A name may now exist somewhere between 'line' and 'eol'.
740 Strip off leading whitespace and trailing whitespace,
741 then add it to the list. */
742 for (name
= line
; IS_WHITESPACE (* name
); name
++)
744 for (name_end
= name
;
745 (! IS_WHITESPACE (* name_end
))
746 && (! IS_LINE_TERMINATOR (* name_end
));
750 if (! IS_LINE_TERMINATOR (* name_end
))
754 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
757 if (! IS_LINE_TERMINATOR (* extra
))
758 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
759 filename
, line_count
);
765 add_specific_symbol (name
, list
);
767 /* Advance line pointer to end of line. The 'eol ++' in the for
768 loop above will then advance us to the start of the next line. */
774 /* See whether a symbol should be stripped or kept based on
775 strip_specific_list and keep_symbols. */
778 is_specified_symbol (const char *name
, struct symlist
*list
)
780 struct symlist
*tmp_list
;
784 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
785 if (*(tmp_list
->name
) != '!')
787 if (!fnmatch (tmp_list
->name
, name
, 0))
792 if (fnmatch (tmp_list
->name
+ 1, name
, 0))
798 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
799 if (strcmp (name
, tmp_list
->name
) == 0)
806 /* Return a pointer to the symbol used as a signature for GROUP. */
809 group_signature (asection
*group
)
811 bfd
*abfd
= group
->owner
;
812 Elf_Internal_Shdr
*ghdr
;
814 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
817 ghdr
= &elf_section_data (group
)->this_hdr
;
818 if (ghdr
->sh_link
< elf_numsections (abfd
))
820 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
821 Elf_Internal_Shdr
*symhdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
823 if (symhdr
->sh_type
== SHT_SYMTAB
824 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
825 return isympp
[ghdr
->sh_info
- 1];
830 /* See if a section is being removed. */
833 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
835 if (sections_removed
|| sections_copied
)
837 struct section_list
*p
;
839 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
841 if (sections_removed
&& p
!= NULL
&& p
->remove
)
843 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
847 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
849 if (strip_symbols
== STRIP_DEBUG
850 || strip_symbols
== STRIP_UNNEEDED
851 || strip_symbols
== STRIP_ALL
852 || discard_locals
== LOCALS_ALL
853 || convert_debugging
)
856 if (strip_symbols
== STRIP_NONDEBUG
)
860 if ((bfd_get_section_flags (abfd
, sec
) & SEC_GROUP
) != 0)
866 Group sections look like debugging sections but they are not.
867 (They have a non-zero size but they are not ALLOCated). */
868 if (strip_symbols
== STRIP_NONDEBUG
)
872 If we are going to strip the group signature symbol, then
873 strip the group section too. */
874 gsym
= group_signature (sec
);
879 if ((strip_symbols
== STRIP_ALL
880 && !is_specified_symbol (gname
, keep_specific_list
))
881 || is_specified_symbol (gname
, strip_specific_list
))
888 /* Return true if SYM is a hidden symbol. */
891 is_hidden_symbol (asymbol
*sym
)
893 elf_symbol_type
*elf_sym
;
895 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
897 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
906 /* Choose which symbol entries to copy; put the result in OSYMS.
907 We don't copy in place, because that confuses the relocs.
908 Return the number of symbols to print. */
911 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
912 asymbol
**isyms
, long symcount
)
914 asymbol
**from
= isyms
, **to
= osyms
;
915 long src_count
= 0, dst_count
= 0;
916 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
918 for (; src_count
< symcount
; src_count
++)
920 asymbol
*sym
= from
[src_count
];
921 flagword flags
= sym
->flags
;
922 char *name
= (char *) bfd_asymbol_name (sym
);
924 bfd_boolean used_in_reloc
= FALSE
;
925 bfd_boolean undefined
;
926 bfd_boolean rem_leading_char
;
927 bfd_boolean add_leading_char
;
929 undefined
= bfd_is_und_section (bfd_get_section (sym
));
931 if (redefine_sym_list
)
933 char *old_name
, *new_name
;
935 old_name
= (char *) bfd_asymbol_name (sym
);
936 new_name
= (char *) lookup_sym_redefinition (old_name
);
937 bfd_asymbol_name (sym
) = new_name
;
941 /* Check if we will remove the current leading character. */
943 (name
[0] == bfd_get_symbol_leading_char (abfd
))
944 && (change_leading_char
945 || (remove_leading_char
946 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
948 || bfd_is_com_section (bfd_get_section (sym
)))));
950 /* Check if we will add a new leading character. */
953 && (bfd_get_symbol_leading_char (obfd
) != '\0')
954 && (bfd_get_symbol_leading_char (abfd
) == '\0'
955 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
957 /* Short circuit for change_leading_char if we can do it in-place. */
958 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
960 name
[0] = bfd_get_symbol_leading_char (obfd
);
961 bfd_asymbol_name (sym
) = name
;
962 rem_leading_char
= FALSE
;
963 add_leading_char
= FALSE
;
966 /* Remove leading char. */
967 if (rem_leading_char
)
968 bfd_asymbol_name (sym
) = ++name
;
970 /* Add new leading char and/or prefix. */
971 if (add_leading_char
|| prefix_symbols_string
)
975 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
976 + strlen (name
) + 1);
977 if (add_leading_char
)
978 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
980 if (prefix_symbols_string
)
982 strcpy (ptr
, prefix_symbols_string
);
983 ptr
+= strlen (prefix_symbols_string
);
987 bfd_asymbol_name (sym
) = n
;
991 if (strip_symbols
== STRIP_ALL
)
993 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
994 || ((flags
& BSF_SECTION_SYM
) != 0
995 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
999 used_in_reloc
= TRUE
;
1001 else if (relocatable
/* Relocatable file. */
1002 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
1004 else if (bfd_decode_symclass (sym
) == 'I')
1005 /* Global symbols in $idata sections need to be retained
1006 even if relocatable is FALSE. External users of the
1007 library containing the $idata section may reference these
1010 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1011 || (flags
& BSF_WEAK
) != 0
1013 || bfd_is_com_section (bfd_get_section (sym
)))
1014 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1015 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1016 keep
= (strip_symbols
!= STRIP_DEBUG
1017 && strip_symbols
!= STRIP_UNNEEDED
1018 && ! convert_debugging
);
1019 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
1020 /* COMDAT sections store special information in local
1021 symbols, so we cannot risk stripping any of them. */
1023 else /* Local symbol. */
1024 keep
= (strip_symbols
!= STRIP_UNNEEDED
1025 && (discard_locals
!= LOCALS_ALL
1026 && (discard_locals
!= LOCALS_START_L
1027 || ! bfd_is_local_label (abfd
, sym
))));
1029 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
1031 /* There are multiple ways to set 'keep' above, but if it
1032 was the relocatable symbol case, then that's an error. */
1035 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1043 && !(flags
& BSF_KEEP
)
1044 && is_specified_symbol (name
, strip_unneeded_list
))
1048 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1049 || is_specified_symbol (name
, keep_specific_list
)))
1052 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
1057 if ((flags
& BSF_GLOBAL
) != 0
1058 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
1060 sym
->flags
&= ~ BSF_GLOBAL
;
1061 sym
->flags
|= BSF_WEAK
;
1065 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1066 && (is_specified_symbol (name
, localize_specific_list
)
1067 || (keepglobal_specific_list
!= NULL
1068 && ! is_specified_symbol (name
, keepglobal_specific_list
))
1069 || (localize_hidden
&& is_hidden_symbol (sym
))))
1071 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1072 sym
->flags
|= BSF_LOCAL
;
1076 && (flags
& BSF_LOCAL
)
1077 && is_specified_symbol (name
, globalize_specific_list
))
1079 sym
->flags
&= ~ BSF_LOCAL
;
1080 sym
->flags
|= BSF_GLOBAL
;
1083 to
[dst_count
++] = sym
;
1087 to
[dst_count
] = NULL
;
1092 /* Find the redefined name of symbol SOURCE. */
1095 lookup_sym_redefinition (const char *source
)
1097 struct redefine_node
*list
;
1099 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1100 if (strcmp (source
, list
->source
) == 0)
1101 return list
->target
;
1106 /* Add a node to a symbol redefine list. */
1109 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1111 struct redefine_node
**p
;
1112 struct redefine_node
*list
;
1113 struct redefine_node
*new_node
;
1115 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1117 if (strcmp (source
, list
->source
) == 0)
1118 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1121 if (strcmp (target
, list
->target
) == 0)
1122 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1126 new_node
= xmalloc (sizeof (struct redefine_node
));
1128 new_node
->source
= strdup (source
);
1129 new_node
->target
= strdup (target
);
1130 new_node
->next
= NULL
;
1135 /* Handle the --redefine-syms option. Read lines containing "old new"
1136 from the file, and add them to the symbol redefine list. */
1139 add_redefine_syms_file (const char *filename
)
1148 file
= fopen (filename
, "r");
1150 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1151 filename
, strerror (errno
));
1154 buf
= xmalloc (bufsize
);
1162 /* Collect the input symbol name. */
1163 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1171 buf
= xrealloc (buf
, bufsize
);
1179 /* Eat white space between the symbol names. */
1180 while (IS_WHITESPACE (c
))
1182 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1187 /* Collect the output symbol name. */
1189 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1197 buf
= xrealloc (buf
, bufsize
);
1205 /* Eat white space at end of line. */
1206 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1211 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1212 || c
== '\n' || c
== EOF
)
1215 /* Append the redefinition to the list. */
1217 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1228 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1230 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1231 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1234 /* Eat the rest of the line and finish it. */
1235 while (c
!= '\n' && c
!= EOF
)
1241 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1246 /* Copy unkown object file IBFD onto OBFD.
1247 Returns TRUE upon success, FALSE otherwise. */
1250 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1258 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1260 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
), NULL
, NULL
, NULL
);
1267 non_fatal (_("stat returns negative size for `%s'"),
1268 bfd_get_archive_filename (ibfd
));
1272 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1274 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1279 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1280 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1282 cbuf
= xmalloc (BUFSIZE
);
1284 while (ncopied
< size
)
1286 tocopy
= size
- ncopied
;
1287 if (tocopy
> BUFSIZE
)
1290 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1291 != (bfd_size_type
) tocopy
)
1293 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
),
1299 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1300 != (bfd_size_type
) tocopy
)
1302 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1310 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1315 /* Copy object file IBFD onto OBFD.
1316 Returns TRUE upon success, FALSE otherwise. */
1319 copy_object (bfd
*ibfd
, bfd
*obfd
)
1323 asection
**osections
= NULL
;
1324 asection
*gnu_debuglink_section
= NULL
;
1325 bfd_size_type
*gaps
= NULL
;
1326 bfd_size_type max_gap
= 0;
1329 enum bfd_architecture iarch
;
1332 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1333 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1334 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1335 fatal (_("Unable to change endianness of input file(s)"));
1337 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1339 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1344 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1345 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1346 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1355 start
= bfd_get_start_address (ibfd
);
1356 start
+= change_start
;
1359 /* Neither the start address nor the flags
1360 need to be set for a core file. */
1361 if (bfd_get_format (obfd
) != bfd_core
)
1365 flags
= bfd_get_file_flags (ibfd
);
1366 flags
|= bfd_flags_to_set
;
1367 flags
&= ~bfd_flags_to_clear
;
1368 flags
&= bfd_applicable_file_flags (obfd
);
1370 if (!bfd_set_start_address (obfd
, start
)
1371 || !bfd_set_file_flags (obfd
, flags
))
1373 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
),
1379 /* Copy architecture of input file to output file. */
1380 iarch
= bfd_get_arch (ibfd
);
1381 imach
= bfd_get_mach (ibfd
);
1382 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1383 && (ibfd
->target_defaulted
1384 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1386 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1387 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1388 bfd_get_archive_filename (ibfd
));
1390 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1391 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1392 bfd_get_mach (ibfd
)));
1396 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1398 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
), NULL
, NULL
, NULL
);
1405 if (osympp
!= isympp
)
1411 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1414 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
), NULL
, NULL
, NULL
);
1418 osympp
= isympp
= xmalloc (symsize
);
1419 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1422 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1426 /* BFD mandates that all output sections be created and sizes set before
1427 any output is done. Thus, we traverse all sections multiple times. */
1428 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1430 setup_bfd_headers (ibfd
, obfd
);
1432 if (add_sections
!= NULL
)
1434 struct section_add
*padd
;
1435 struct section_list
*pset
;
1437 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1441 pset
= find_section_list (padd
->name
, FALSE
);
1445 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1446 if (pset
!= NULL
&& pset
->set_flags
)
1447 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1449 /* bfd_make_section_with_flags() does not return very helpful
1450 error codes, so check for the most likely user error first. */
1451 if (bfd_get_section_by_name (obfd
, padd
->name
))
1453 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1454 _("can't add section '%s'"), padd
->name
);
1459 padd
->section
= bfd_make_section_with_flags (obfd
, padd
->name
, flags
);
1460 if (padd
->section
== NULL
)
1462 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1463 _("can't create section `%s'"),
1469 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1471 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1477 if (pset
->change_vma
!= CHANGE_IGNORE
)
1478 if (! bfd_set_section_vma (obfd
, padd
->section
,
1481 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1485 if (pset
->change_lma
!= CHANGE_IGNORE
)
1487 padd
->section
->lma
= pset
->lma_val
;
1489 if (! bfd_set_section_alignment
1490 (obfd
, padd
->section
,
1491 bfd_section_alignment (obfd
, padd
->section
)))
1493 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1501 if (gnu_debuglink_filename
!= NULL
)
1503 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1504 (obfd
, gnu_debuglink_filename
);
1506 if (gnu_debuglink_section
== NULL
)
1508 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1509 _("cannot create debug link section `%s'"),
1510 gnu_debuglink_filename
);
1514 /* Special processing for PE format files. We
1515 have no way to distinguish PE from COFF here. */
1516 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1518 bfd_vma debuglink_vma
;
1519 asection
* highest_section
;
1522 /* The PE spec requires that all sections be adjacent and sorted
1523 in ascending order of VMA. It also specifies that debug
1524 sections should be last. This is despite the fact that debug
1525 sections are not loaded into memory and so in theory have no
1528 This means that the debuglink section must be given a non-zero
1529 VMA which makes it contiguous with other debug sections. So
1530 walk the current section list, find the section with the
1531 highest VMA and start the debuglink section after that one. */
1532 for (sec
= obfd
->sections
, highest_section
= NULL
;
1536 && (highest_section
== NULL
1537 || sec
->vma
> highest_section
->vma
))
1538 highest_section
= sec
;
1540 if (highest_section
)
1541 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1542 + highest_section
->size
,
1543 /* FIXME: We ought to be using
1544 COFF_PAGE_SIZE here or maybe
1545 bfd_get_section_alignment() (if it
1546 was set) but since this is for PE
1547 and we know the required alignment
1548 it is easier just to hard code it. */
1551 /* Umm, not sure what to do in this case. */
1552 debuglink_vma
= 0x1000;
1554 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1558 if (bfd_count_sections (obfd
) != 0
1559 && (gap_fill_set
|| pad_to_set
))
1564 /* We must fill in gaps between the sections and/or we must pad
1565 the last section to a specified address. We do this by
1566 grabbing a list of the sections, sorting them by VMA, and
1567 increasing the section sizes as required to fill the gaps.
1568 We write out the gap contents below. */
1570 c
= bfd_count_sections (obfd
);
1571 osections
= xmalloc (c
* sizeof (asection
*));
1573 bfd_map_over_sections (obfd
, get_sections
, &set
);
1575 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1577 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1578 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1582 for (i
= 0; i
< c
- 1; i
++)
1586 bfd_vma gap_start
, gap_stop
;
1588 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1589 if ((flags
& SEC_HAS_CONTENTS
) == 0
1590 || (flags
& SEC_LOAD
) == 0)
1593 size
= bfd_section_size (obfd
, osections
[i
]);
1594 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1595 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1596 if (gap_start
< gap_stop
)
1598 if (! bfd_set_section_size (obfd
, osections
[i
],
1599 size
+ (gap_stop
- gap_start
)))
1601 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
1602 _("Can't fill gap after section"));
1606 gaps
[i
] = gap_stop
- gap_start
;
1607 if (max_gap
< gap_stop
- gap_start
)
1608 max_gap
= gap_stop
- gap_start
;
1618 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1619 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1620 if (lma
+ size
< pad_to
)
1622 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1625 bfd_nonfatal_message (NULL
, obfd
, osections
[c
- 1],
1626 _("can't add padding"));
1631 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1632 if (max_gap
< pad_to
- (lma
+ size
))
1633 max_gap
= pad_to
- (lma
+ size
);
1639 /* Symbol filtering must happen after the output sections
1640 have been created, but before their contents are set. */
1642 if (convert_debugging
)
1643 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1645 if (strip_symbols
== STRIP_DEBUG
1646 || strip_symbols
== STRIP_ALL
1647 || strip_symbols
== STRIP_UNNEEDED
1648 || strip_symbols
== STRIP_NONDEBUG
1649 || discard_locals
!= LOCALS_UNDEF
1651 || strip_specific_list
!= NULL
1652 || keep_specific_list
!= NULL
1653 || localize_specific_list
!= NULL
1654 || globalize_specific_list
!= NULL
1655 || keepglobal_specific_list
!= NULL
1656 || weaken_specific_list
!= NULL
1657 || prefix_symbols_string
1660 || convert_debugging
1661 || change_leading_char
1662 || remove_leading_char
1663 || redefine_sym_list
1666 /* Mark symbols used in output relocations so that they
1667 are kept, even if they are local labels or static symbols.
1669 Note we iterate over the input sections examining their
1670 relocations since the relocations for the output sections
1671 haven't been set yet. mark_symbols_used_in_relocations will
1672 ignore input sections which have no corresponding output
1674 if (strip_symbols
!= STRIP_ALL
)
1675 bfd_map_over_sections (ibfd
,
1676 mark_symbols_used_in_relocations
,
1678 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1679 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1682 if (convert_debugging
&& dhandle
!= NULL
)
1684 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1691 bfd_set_symtab (obfd
, osympp
, symcount
);
1693 /* This has to happen after the symbol table has been set. */
1694 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1696 if (add_sections
!= NULL
)
1698 struct section_add
*padd
;
1700 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1702 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1705 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1711 if (gnu_debuglink_filename
!= NULL
)
1713 if (! bfd_fill_in_gnu_debuglink_section
1714 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1716 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1717 _("cannot fill debug link section `%s'"),
1718 gnu_debuglink_filename
);
1723 if (gap_fill_set
|| pad_to_set
)
1728 /* Fill in the gaps. */
1731 buf
= xmalloc (max_gap
);
1732 memset (buf
, gap_fill
, max_gap
);
1734 c
= bfd_count_sections (obfd
);
1735 for (i
= 0; i
< c
; i
++)
1743 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1754 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1757 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
1768 /* Do not copy backend data if --extract-symbol is passed; anything
1769 that needs to look at the section contents will fail. */
1773 /* Allow the BFD backend to copy any private data it understands
1774 from the input BFD to the output BFD. This is done last to
1775 permit the routine to look at the filtered symbol table, which is
1776 important for the ECOFF code at least. */
1777 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1779 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1780 _("error copying private BFD data"));
1784 /* Switch to the alternate machine code. We have to do this at the
1785 very end, because we only initialize the header when we create
1786 the first section. */
1787 if (use_alt_mach_code
!= 0)
1789 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1791 non_fatal (_("this target does not support %lu alternative machine codes"),
1793 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1795 non_fatal (_("treating that number as an absolute e_machine value instead"));
1796 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
1799 non_fatal (_("ignoring the alternative value"));
1806 /* Read each archive element in turn from IBFD, copy the
1807 contents to temp file, and keep the temp file handle.
1808 If 'force_output_target' is TRUE then make sure that
1809 all elements in the new archive are of the type
1813 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
1814 bfd_boolean force_output_target
)
1818 struct name_list
*next
;
1822 bfd
**ptr
= &obfd
->archive_head
;
1826 /* Make a temp directory to hold the contents. */
1827 dir
= make_tempdir (bfd_get_filename (obfd
));
1829 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1832 obfd
->has_armap
= ibfd
->has_armap
;
1836 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1838 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1839 RETURN_NONFATAL (obfd
);
1841 while (!status
&& this_element
!= NULL
)
1847 int stat_status
= 0;
1848 bfd_boolean
delete = TRUE
;
1850 /* Create an output file for this member. */
1851 output_name
= concat (dir
, "/",
1852 bfd_get_filename (this_element
), (char *) 0);
1854 /* If the file already exists, make another temp dir. */
1855 if (stat (output_name
, &buf
) >= 0)
1857 output_name
= make_tempdir (output_name
);
1858 if (output_name
== NULL
)
1859 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1862 l
= xmalloc (sizeof (struct name_list
));
1863 l
->name
= output_name
;
1867 output_name
= concat (output_name
, "/",
1868 bfd_get_filename (this_element
), (char *) 0);
1873 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1875 if (stat_status
!= 0)
1876 non_fatal (_("internal stat error on %s"),
1877 bfd_get_filename (this_element
));
1880 l
= xmalloc (sizeof (struct name_list
));
1881 l
->name
= output_name
;
1886 if (bfd_check_format (this_element
, bfd_object
))
1888 /* PR binutils/3110: Cope with archives
1889 containing multiple target types. */
1890 if (force_output_target
)
1891 output_bfd
= bfd_openw (output_name
, output_target
);
1893 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
1895 if (output_bfd
== NULL
)
1897 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
1902 delete = ! copy_object (this_element
, output_bfd
);
1905 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
1907 if (!bfd_close (output_bfd
))
1909 bfd_nonfatal_message (NULL
, output_bfd
, NULL
, NULL
);
1910 /* Error in new object file. Don't change archive. */
1915 goto copy_unknown_element
;
1919 bfd_nonfatal_message (bfd_get_archive_filename (this_element
),
1921 _("Unable to recognise the format of file"));
1923 output_bfd
= bfd_openw (output_name
, output_target
);
1924 copy_unknown_element
:
1925 delete = !copy_unknown_object (this_element
, output_bfd
);
1926 if (!bfd_close_all_done (output_bfd
))
1928 bfd_nonfatal_message (NULL
, output_bfd
, NULL
, NULL
);
1929 /* Error in new object file. Don't change archive. */
1936 unlink (output_name
);
1941 if (preserve_dates
&& stat_status
== 0)
1942 set_times (output_name
, &buf
);
1944 /* Open the newly output file and attach to our list. */
1945 output_bfd
= bfd_openr (output_name
, output_target
);
1947 l
->obfd
= output_bfd
;
1950 ptr
= &output_bfd
->archive_next
;
1952 last_element
= this_element
;
1954 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1956 bfd_close (last_element
);
1961 if (!bfd_close (obfd
))
1962 RETURN_NONFATAL (obfd
);
1964 if (!bfd_close (ibfd
))
1965 RETURN_NONFATAL (obfd
);
1967 /* Delete all the files that we opened. */
1968 for (l
= list
; l
!= NULL
; l
= l
->next
)
1970 if (l
->obfd
== NULL
)
1974 bfd_close (l
->obfd
);
1981 /* The top-level control. */
1984 copy_file (const char *input_filename
, const char *output_filename
,
1985 const char *input_target
, const char *output_target
)
1988 char **obj_matching
;
1989 char **core_matching
;
1991 if (get_file_size (input_filename
) < 1)
1997 /* To allow us to do "strip *" without dying on the first
1998 non-object file, failures are nonfatal. */
1999 ibfd
= bfd_openr (input_filename
, input_target
);
2002 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2007 if (bfd_check_format (ibfd
, bfd_archive
))
2009 bfd_boolean force_output_target
;
2012 /* bfd_get_target does not return the correct value until
2013 bfd_check_format succeeds. */
2014 if (output_target
== NULL
)
2016 output_target
= bfd_get_target (ibfd
);
2017 force_output_target
= FALSE
;
2020 force_output_target
= TRUE
;
2022 obfd
= bfd_openw (output_filename
, output_target
);
2025 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2030 copy_archive (ibfd
, obfd
, output_target
, force_output_target
);
2032 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
2037 /* bfd_get_target does not return the correct value until
2038 bfd_check_format succeeds. */
2039 if (output_target
== NULL
)
2040 output_target
= bfd_get_target (ibfd
);
2042 obfd
= bfd_openw (output_filename
, output_target
);
2045 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2050 if (! copy_object (ibfd
, obfd
))
2053 if (!bfd_close (obfd
))
2054 RETURN_NONFATAL (obfd
);
2056 if (!bfd_close (ibfd
))
2057 RETURN_NONFATAL (ibfd
);
2061 bfd_error_type obj_error
= bfd_get_error ();
2062 bfd_error_type core_error
;
2064 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
2066 /* This probably can't happen.. */
2067 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2068 free (obj_matching
);
2072 core_error
= bfd_get_error ();
2073 /* Report the object error in preference to the core error. */
2074 if (obj_error
!= core_error
)
2075 bfd_set_error (obj_error
);
2077 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2079 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2081 list_matching_formats (obj_matching
);
2082 free (obj_matching
);
2084 if (core_error
== bfd_error_file_ambiguously_recognized
)
2086 list_matching_formats (core_matching
);
2087 free (core_matching
);
2094 /* Add a name to the section renaming list. */
2097 add_section_rename (const char * old_name
, const char * new_name
,
2100 section_rename
* rename
;
2102 /* Check for conflicts first. */
2103 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2104 if (strcmp (rename
->old_name
, old_name
) == 0)
2106 /* Silently ignore duplicate definitions. */
2107 if (strcmp (rename
->new_name
, new_name
) == 0
2108 && rename
->flags
== flags
)
2111 fatal (_("Multiple renames of section %s"), old_name
);
2114 rename
= xmalloc (sizeof (* rename
));
2116 rename
->old_name
= old_name
;
2117 rename
->new_name
= new_name
;
2118 rename
->flags
= flags
;
2119 rename
->next
= section_rename_list
;
2121 section_rename_list
= rename
;
2124 /* Check the section rename list for a new name of the input section
2125 ISECTION. Return the new name if one is found.
2126 Also set RETURNED_FLAGS to the flags to be used for this section. */
2129 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2130 flagword
* returned_flags
)
2132 const char * old_name
= bfd_section_name (ibfd
, isection
);
2133 section_rename
* rename
;
2135 /* Default to using the flags of the input section. */
2136 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2138 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2139 if (strcmp (rename
->old_name
, old_name
) == 0)
2141 if (rename
->flags
!= (flagword
) -1)
2142 * returned_flags
= rename
->flags
;
2144 return rename
->new_name
;
2150 /* Once each of the sections is copied, we may still need to do some
2151 finalization work for private section headers. Do that here. */
2154 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2156 /* Allow the BFD backend to copy any private data it understands
2157 from the input section to the output section. */
2158 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2161 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
2162 _("error in private h eader data"));
2166 /* All went well. */
2170 /* Create a section in OBFD with the same
2171 name and attributes as ISECTION in IBFD. */
2174 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2176 bfd
*obfd
= obfdarg
;
2177 struct section_list
*p
;
2185 char *prefix
= NULL
;
2186 bfd_boolean make_nobits
;
2188 if (is_strip_section (ibfd
, isection
))
2191 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2195 /* Get the, possibly new, name of the output section. */
2196 name
= find_section_rename (ibfd
, isection
, & flags
);
2198 /* Prefix sections. */
2199 if ((prefix_alloc_sections_string
)
2200 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2201 prefix
= prefix_alloc_sections_string
;
2202 else if (prefix_sections_string
)
2203 prefix
= prefix_sections_string
;
2209 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
2215 make_nobits
= FALSE
;
2216 if (p
!= NULL
&& p
->set_flags
)
2217 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2218 else if (strip_symbols
== STRIP_NONDEBUG
2219 && (flags
& SEC_ALLOC
) != 0
2220 && (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
2221 || elf_section_type (isection
) != SHT_NOTE
))
2223 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2224 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2228 /* Twiddle the input section flags so that it seems to
2229 elf.c:copy_private_bfd_data that section flags have not
2230 changed between input and output sections. This hack
2231 prevents wholesale rewriting of the program headers. */
2232 isection
->flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2236 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2238 if (osection
== NULL
)
2240 err
= _("failed to create output section");
2245 elf_section_type (osection
) = SHT_NOBITS
;
2247 size
= bfd_section_size (ibfd
, isection
);
2249 size
= (size
+ interleave
- 1) / interleave
;
2250 else if (extract_symbol
)
2252 if (! bfd_set_section_size (obfd
, osection
, size
))
2254 err
= _("failed to set size");
2258 vma
= bfd_section_vma (ibfd
, isection
);
2259 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2261 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2264 vma
+= change_section_address
;
2266 if (! bfd_set_section_vma (obfd
, osection
, extract_symbol
? 0 : vma
))
2268 err
= _("failed to set vma");
2272 lma
= isection
->lma
;
2273 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2275 if (p
->change_lma
== CHANGE_MODIFY
)
2277 else if (p
->change_lma
== CHANGE_SET
)
2283 lma
+= change_section_address
;
2285 osection
->lma
= extract_symbol
? 0 : lma
;
2287 /* FIXME: This is probably not enough. If we change the LMA we
2288 may have to recompute the header for the file as well. */
2289 if (!bfd_set_section_alignment (obfd
,
2291 bfd_section_alignment (ibfd
, isection
)))
2293 err
= _("failed to set alignment");
2297 /* Copy merge entity size. */
2298 osection
->entsize
= isection
->entsize
;
2300 /* This used to be mangle_section; we do here to avoid using
2301 bfd_get_section_by_name since some formats allow multiple
2302 sections with the same name. */
2303 isection
->output_section
= osection
;
2304 isection
->output_offset
= extract_symbol
? vma
: 0;
2306 /* Do not copy backend data if --extract-symbol is passed; anything
2307 that needs to look at the section contents will fail. */
2311 /* Allow the BFD backend to copy any private data it understands
2312 from the input section to the output section. */
2313 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2315 err
= _("failed to copy private data");
2318 else if ((isection
->flags
& SEC_GROUP
) != 0)
2320 asymbol
*gsym
= group_signature (isection
);
2323 gsym
->flags
|= BSF_KEEP
;
2326 /* All went well. */
2331 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
2334 /* Copy the data of input section ISECTION of IBFD
2335 to an output section with the same name in OBFD.
2336 If stripping then don't copy any relocation info. */
2339 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2341 bfd
*obfd
= obfdarg
;
2342 struct section_list
*p
;
2350 /* If we have already failed earlier on,
2351 do not keep on generating complaints now. */
2355 if (is_strip_section (ibfd
, isection
))
2358 flags
= bfd_get_section_flags (ibfd
, isection
);
2359 if ((flags
& SEC_GROUP
) != 0)
2362 osection
= isection
->output_section
;
2363 size
= bfd_get_section_size (isection
);
2365 if (size
== 0 || osection
== 0)
2368 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2370 /* Core files do not need to be relocated. */
2371 if (bfd_get_format (obfd
) == bfd_core
)
2375 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2379 /* Do not complain if the target does not support relocations. */
2380 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2385 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2392 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2395 relpp
= xmalloc (relsize
);
2396 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2400 bfd_nonfatal_message (NULL
, ibfd
, isection
,
2401 _("relocation count is negative"));
2405 if (strip_symbols
== STRIP_ALL
)
2407 /* Remove relocations which are not in
2408 keep_strip_specific_list. */
2409 arelent
**temp_relpp
;
2410 long temp_relcount
= 0;
2413 temp_relpp
= xmalloc (relsize
);
2414 for (i
= 0; i
< relcount
; i
++)
2415 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2416 keep_specific_list
))
2417 temp_relpp
[temp_relcount
++] = relpp
[i
];
2418 relcount
= temp_relcount
;
2423 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2431 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2432 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2434 void *memhunk
= xmalloc (size
);
2436 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2439 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2445 /* We don't handle leftover bytes (too many possible behaviors,
2446 and we don't know what the user wants). The section length
2447 must be a multiple of the number of bytes to swap. */
2448 if ((size
% reverse_bytes
) == 0)
2453 for (i
= 0; i
< size
; i
+= reverse_bytes
)
2454 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
2456 bfd_byte
*m
= (bfd_byte
*) memhunk
;
2459 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
2460 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
2464 /* User must pad the section up in order to do this. */
2465 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2466 bfd_section_name (ibfd
, isection
), reverse_bytes
);
2471 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2472 char *from
= (char *) memhunk
+ copy_byte
;
2474 char *end
= (char *) memhunk
+ size
;
2476 for (; from
< end
; from
+= interleave
)
2479 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2480 osection
->lma
/= interleave
;
2483 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2486 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2491 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2493 void *memhunk
= xmalloc (size
);
2495 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2496 flag--they can just remove the section entirely and add it
2497 back again. However, we do permit them to turn on the
2498 SEC_HAS_CONTENTS flag, and take it to mean that the section
2499 contents should be zeroed out. */
2501 memset (memhunk
, 0, size
);
2502 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2505 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2512 /* Get all the sections. This is used when --gap-fill or --pad-to is
2516 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2518 asection
***secppp
= secppparg
;
2520 **secppp
= osection
;
2524 /* Sort sections by VMA. This is called via qsort, and is used when
2525 --gap-fill or --pad-to is used. We force non loadable or empty
2526 sections to the front, where they are easier to ignore. */
2529 compare_section_lma (const void *arg1
, const void *arg2
)
2531 const asection
*const *sec1
= arg1
;
2532 const asection
*const *sec2
= arg2
;
2533 flagword flags1
, flags2
;
2535 /* Sort non loadable sections to the front. */
2536 flags1
= (*sec1
)->flags
;
2537 flags2
= (*sec2
)->flags
;
2538 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2539 || (flags1
& SEC_LOAD
) == 0)
2541 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2542 && (flags2
& SEC_LOAD
) != 0)
2547 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2548 || (flags2
& SEC_LOAD
) == 0)
2552 /* Sort sections by LMA. */
2553 if ((*sec1
)->lma
> (*sec2
)->lma
)
2555 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2558 /* Sort sections with the same LMA by size. */
2559 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2561 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2567 /* Mark all the symbols which will be used in output relocations with
2568 the BSF_KEEP flag so that those symbols will not be stripped.
2570 Ignore relocations which will not appear in the output file. */
2573 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2575 asymbol
**symbols
= symbolsarg
;
2580 /* Ignore an input section with no corresponding output section. */
2581 if (isection
->output_section
== NULL
)
2584 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2587 /* Do not complain if the target does not support relocations. */
2588 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2590 bfd_fatal (bfd_get_filename (ibfd
));
2596 relpp
= xmalloc (relsize
);
2597 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2599 bfd_fatal (bfd_get_filename (ibfd
));
2601 /* Examine each symbol used in a relocation. If it's not one of the
2602 special bfd section symbols, then mark it with BSF_KEEP. */
2603 for (i
= 0; i
< relcount
; i
++)
2605 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2606 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2607 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2608 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2615 /* Write out debugging information. */
2618 write_debugging_info (bfd
*obfd
, void *dhandle
,
2619 long *symcountp ATTRIBUTE_UNUSED
,
2620 asymbol
***symppp ATTRIBUTE_UNUSED
)
2622 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2623 return write_ieee_debugging_info (obfd
, dhandle
);
2625 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2626 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2628 bfd_byte
*syms
, *strings
;
2629 bfd_size_type symsize
, stringsize
;
2630 asection
*stabsec
, *stabstrsec
;
2633 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2638 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2639 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2640 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2642 || stabstrsec
== NULL
2643 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2644 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2645 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2646 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2648 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2649 _("can't create debugging section"));
2653 /* We can get away with setting the section contents now because
2654 the next thing the caller is going to do is copy over the
2655 real sections. We may someday have to split the contents
2656 setting out of this function. */
2657 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2658 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2661 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2662 _("can't set debugging section contents"));
2669 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2670 _("don't know how to write debugging information for %s"),
2671 bfd_get_target (obfd
));
2676 strip_main (int argc
, char *argv
[])
2678 char *input_target
= NULL
;
2679 char *output_target
= NULL
;
2680 bfd_boolean show_version
= FALSE
;
2681 bfd_boolean formats_info
= FALSE
;
2684 struct section_list
*p
;
2685 char *output_file
= NULL
;
2687 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2688 strip_options
, (int *) 0)) != EOF
)
2693 input_target
= optarg
;
2696 output_target
= optarg
;
2699 input_target
= output_target
= optarg
;
2702 p
= find_section_list (optarg
, TRUE
);
2704 sections_removed
= TRUE
;
2707 strip_symbols
= STRIP_ALL
;
2711 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2712 strip_symbols
= STRIP_DEBUG
;
2714 case OPTION_STRIP_UNNEEDED
:
2715 strip_symbols
= STRIP_UNNEEDED
;
2718 add_specific_symbol (optarg
, &keep_specific_list
);
2721 add_specific_symbol (optarg
, &strip_specific_list
);
2724 output_file
= optarg
;
2727 preserve_dates
= TRUE
;
2730 discard_locals
= LOCALS_ALL
;
2733 discard_locals
= LOCALS_START_L
;
2739 show_version
= TRUE
;
2741 case OPTION_FORMATS_INFO
:
2742 formats_info
= TRUE
;
2744 case OPTION_ONLY_KEEP_DEBUG
:
2745 strip_symbols
= STRIP_NONDEBUG
;
2747 case OPTION_KEEP_FILE_SYMBOLS
:
2748 keep_file_symbols
= 1;
2751 /* We've been given a long option. */
2758 strip_usage (stdout
, 0);
2760 strip_usage (stderr
, 1);
2771 print_version ("strip");
2773 /* Default is to strip all symbols. */
2774 if (strip_symbols
== STRIP_UNDEF
2775 && discard_locals
== LOCALS_UNDEF
2776 && strip_specific_list
== NULL
)
2777 strip_symbols
= STRIP_ALL
;
2779 if (output_target
== NULL
)
2780 output_target
= input_target
;
2784 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2785 strip_usage (stderr
, 1);
2787 for (; i
< argc
; i
++)
2789 int hold_status
= status
;
2790 struct stat statbuf
;
2793 if (get_file_size (argv
[i
]) < 1)
2800 /* No need to check the return value of stat().
2801 It has already been checked in get_file_size(). */
2802 stat (argv
[i
], &statbuf
);
2804 if (output_file
== NULL
|| strcmp (argv
[i
], output_file
) == 0)
2805 tmpname
= make_tempname (argv
[i
]);
2807 tmpname
= output_file
;
2809 if (tmpname
== NULL
)
2811 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
2812 _("could not create temporary file to hold stripped copy"));
2818 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2822 set_times (tmpname
, &statbuf
);
2823 if (output_file
!= tmpname
)
2824 smart_rename (tmpname
, output_file
? output_file
: argv
[i
],
2826 status
= hold_status
;
2829 unlink_if_ordinary (tmpname
);
2830 if (output_file
!= tmpname
)
2838 copy_main (int argc
, char *argv
[])
2840 char * binary_architecture
= NULL
;
2841 char *input_filename
= NULL
;
2842 char *output_filename
= NULL
;
2844 char *input_target
= NULL
;
2845 char *output_target
= NULL
;
2846 bfd_boolean show_version
= FALSE
;
2847 bfd_boolean change_warn
= TRUE
;
2848 bfd_boolean formats_info
= FALSE
;
2850 struct section_list
*p
;
2851 struct stat statbuf
;
2853 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2854 copy_options
, (int *) 0)) != EOF
)
2859 copy_byte
= atoi (optarg
);
2861 fatal (_("byte number must be non-negative"));
2865 binary_architecture
= optarg
;
2869 interleave
= atoi (optarg
);
2871 fatal (_("interleave must be positive"));
2875 case 's': /* "source" - 'I' is preferred */
2876 input_target
= optarg
;
2880 case 'd': /* "destination" - 'O' is preferred */
2881 output_target
= optarg
;
2885 input_target
= output_target
= optarg
;
2889 p
= find_section_list (optarg
, TRUE
);
2891 fatal (_("%s both copied and removed"), optarg
);
2893 sections_copied
= TRUE
;
2897 p
= find_section_list (optarg
, TRUE
);
2899 fatal (_("%s both copied and removed"), optarg
);
2901 sections_removed
= TRUE
;
2905 strip_symbols
= STRIP_ALL
;
2909 strip_symbols
= STRIP_DEBUG
;
2912 case OPTION_STRIP_UNNEEDED
:
2913 strip_symbols
= STRIP_UNNEEDED
;
2916 case OPTION_ONLY_KEEP_DEBUG
:
2917 strip_symbols
= STRIP_NONDEBUG
;
2920 case OPTION_KEEP_FILE_SYMBOLS
:
2921 keep_file_symbols
= 1;
2924 case OPTION_ADD_GNU_DEBUGLINK
:
2925 gnu_debuglink_filename
= optarg
;
2929 add_specific_symbol (optarg
, &keep_specific_list
);
2933 add_specific_symbol (optarg
, &strip_specific_list
);
2936 case OPTION_STRIP_UNNEEDED_SYMBOL
:
2937 add_specific_symbol (optarg
, &strip_unneeded_list
);
2941 add_specific_symbol (optarg
, &localize_specific_list
);
2944 case OPTION_GLOBALIZE_SYMBOL
:
2945 add_specific_symbol (optarg
, &globalize_specific_list
);
2949 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2953 add_specific_symbol (optarg
, &weaken_specific_list
);
2957 preserve_dates
= TRUE
;
2965 discard_locals
= LOCALS_ALL
;
2969 discard_locals
= LOCALS_START_L
;
2977 show_version
= TRUE
;
2980 case OPTION_FORMATS_INFO
:
2981 formats_info
= TRUE
;
2988 case OPTION_ADD_SECTION
:
2992 struct section_add
*pa
;
2997 s
= strchr (optarg
, '=');
3000 fatal (_("bad format for %s"), "--add-section");
3002 size
= get_file_size (s
+ 1);
3009 pa
= xmalloc (sizeof (struct section_add
));
3012 name
= xmalloc (len
+ 1);
3013 strncpy (name
, optarg
, len
);
3017 pa
->filename
= s
+ 1;
3019 pa
->contents
= xmalloc (size
);
3021 f
= fopen (pa
->filename
, FOPEN_RB
);
3024 fatal (_("cannot open: %s: %s"),
3025 pa
->filename
, strerror (errno
));
3027 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
3029 fatal (_("%s: fread failed"), pa
->filename
);
3033 pa
->next
= add_sections
;
3038 case OPTION_CHANGE_START
:
3039 change_start
= parse_vma (optarg
, "--change-start");
3042 case OPTION_CHANGE_SECTION_ADDRESS
:
3043 case OPTION_CHANGE_SECTION_LMA
:
3044 case OPTION_CHANGE_SECTION_VMA
:
3049 char *option
= NULL
;
3051 enum change_action what
= CHANGE_IGNORE
;
3055 case OPTION_CHANGE_SECTION_ADDRESS
:
3056 option
= "--change-section-address";
3058 case OPTION_CHANGE_SECTION_LMA
:
3059 option
= "--change-section-lma";
3061 case OPTION_CHANGE_SECTION_VMA
:
3062 option
= "--change-section-vma";
3066 s
= strchr (optarg
, '=');
3069 s
= strchr (optarg
, '+');
3072 s
= strchr (optarg
, '-');
3074 fatal (_("bad format for %s"), option
);
3079 name
= xmalloc (len
+ 1);
3080 strncpy (name
, optarg
, len
);
3083 p
= find_section_list (name
, TRUE
);
3085 val
= parse_vma (s
+ 1, option
);
3089 case '=': what
= CHANGE_SET
; break;
3090 case '-': val
= - val
; /* Drop through. */
3091 case '+': what
= CHANGE_MODIFY
; break;
3096 case OPTION_CHANGE_SECTION_ADDRESS
:
3097 p
->change_vma
= what
;
3101 case OPTION_CHANGE_SECTION_LMA
:
3102 p
->change_lma
= what
;
3106 case OPTION_CHANGE_SECTION_VMA
:
3107 p
->change_vma
= what
;
3114 case OPTION_CHANGE_ADDRESSES
:
3115 change_section_address
= parse_vma (optarg
, "--change-addresses");
3116 change_start
= change_section_address
;
3119 case OPTION_CHANGE_WARNINGS
:
3123 case OPTION_CHANGE_LEADING_CHAR
:
3124 change_leading_char
= TRUE
;
3127 case OPTION_DEBUGGING
:
3128 convert_debugging
= TRUE
;
3131 case OPTION_GAP_FILL
:
3133 bfd_vma gap_fill_vma
;
3135 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
3136 gap_fill
= (bfd_byte
) gap_fill_vma
;
3137 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
3141 sprintf_vma (buff
, gap_fill_vma
);
3143 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3146 gap_fill_set
= TRUE
;
3150 case OPTION_NO_CHANGE_WARNINGS
:
3151 change_warn
= FALSE
;
3155 pad_to
= parse_vma (optarg
, "--pad-to");
3159 case OPTION_REMOVE_LEADING_CHAR
:
3160 remove_leading_char
= TRUE
;
3163 case OPTION_REDEFINE_SYM
:
3165 /* Push this redefinition onto redefine_symbol_list. */
3169 const char *nextarg
;
3170 char *source
, *target
;
3172 s
= strchr (optarg
, '=');
3174 fatal (_("bad format for %s"), "--redefine-sym");
3177 source
= xmalloc (len
+ 1);
3178 strncpy (source
, optarg
, len
);
3182 len
= strlen (nextarg
);
3183 target
= xmalloc (len
+ 1);
3184 strcpy (target
, nextarg
);
3186 redefine_list_append ("--redefine-sym", source
, target
);
3193 case OPTION_REDEFINE_SYMS
:
3194 add_redefine_syms_file (optarg
);
3197 case OPTION_SET_SECTION_FLAGS
:
3203 s
= strchr (optarg
, '=');
3205 fatal (_("bad format for %s"), "--set-section-flags");
3208 name
= xmalloc (len
+ 1);
3209 strncpy (name
, optarg
, len
);
3212 p
= find_section_list (name
, TRUE
);
3214 p
->set_flags
= TRUE
;
3215 p
->flags
= parse_flags (s
+ 1);
3219 case OPTION_RENAME_SECTION
:
3222 const char *eq
, *fl
;
3227 eq
= strchr (optarg
, '=');
3229 fatal (_("bad format for %s"), "--rename-section");
3233 fatal (_("bad format for %s"), "--rename-section");
3235 old_name
= xmalloc (len
+ 1);
3236 strncpy (old_name
, optarg
, len
);
3240 fl
= strchr (eq
, ',');
3243 flags
= parse_flags (fl
+ 1);
3253 fatal (_("bad format for %s"), "--rename-section");
3255 new_name
= xmalloc (len
+ 1);
3256 strncpy (new_name
, eq
, len
);
3259 add_section_rename (old_name
, new_name
, flags
);
3263 case OPTION_SET_START
:
3264 set_start
= parse_vma (optarg
, "--set-start");
3265 set_start_set
= TRUE
;
3268 case OPTION_SREC_LEN
:
3269 Chunk
= parse_vma (optarg
, "--srec-len");
3272 case OPTION_SREC_FORCES3
:
3276 case OPTION_STRIP_SYMBOLS
:
3277 add_specific_symbols (optarg
, &strip_specific_list
);
3280 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3281 add_specific_symbols (optarg
, &strip_unneeded_list
);
3284 case OPTION_KEEP_SYMBOLS
:
3285 add_specific_symbols (optarg
, &keep_specific_list
);
3288 case OPTION_LOCALIZE_HIDDEN
:
3289 localize_hidden
= TRUE
;
3292 case OPTION_LOCALIZE_SYMBOLS
:
3293 add_specific_symbols (optarg
, &localize_specific_list
);
3296 case OPTION_GLOBALIZE_SYMBOLS
:
3297 add_specific_symbols (optarg
, &globalize_specific_list
);
3300 case OPTION_KEEPGLOBAL_SYMBOLS
:
3301 add_specific_symbols (optarg
, &keepglobal_specific_list
);
3304 case OPTION_WEAKEN_SYMBOLS
:
3305 add_specific_symbols (optarg
, &weaken_specific_list
);
3308 case OPTION_ALT_MACH_CODE
:
3309 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3310 if (use_alt_mach_code
== 0)
3311 fatal (_("unable to parse alternative machine code"));
3314 case OPTION_PREFIX_SYMBOLS
:
3315 prefix_symbols_string
= optarg
;
3318 case OPTION_PREFIX_SECTIONS
:
3319 prefix_sections_string
= optarg
;
3322 case OPTION_PREFIX_ALLOC_SECTIONS
:
3323 prefix_alloc_sections_string
= optarg
;
3326 case OPTION_READONLY_TEXT
:
3327 bfd_flags_to_set
|= WP_TEXT
;
3328 bfd_flags_to_clear
&= ~WP_TEXT
;
3331 case OPTION_WRITABLE_TEXT
:
3332 bfd_flags_to_clear
|= WP_TEXT
;
3333 bfd_flags_to_set
&= ~WP_TEXT
;
3337 bfd_flags_to_set
|= D_PAGED
;
3338 bfd_flags_to_clear
&= ~D_PAGED
;
3342 bfd_flags_to_clear
|= D_PAGED
;
3343 bfd_flags_to_set
&= ~D_PAGED
;
3346 case OPTION_EXTRACT_SYMBOL
:
3347 extract_symbol
= TRUE
;
3350 case OPTION_REVERSE_BYTES
:
3352 int prev
= reverse_bytes
;
3354 reverse_bytes
= atoi (optarg
);
3355 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
3356 fatal (_("number of bytes to reverse must be positive and even"));
3358 if (prev
&& prev
!= reverse_bytes
)
3359 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3365 /* We've been given a long option. */
3370 copy_usage (stdout
, 0);
3373 copy_usage (stderr
, 1);
3384 print_version ("objcopy");
3386 if (copy_byte
>= interleave
)
3387 fatal (_("byte number must be less than interleave"));
3389 if (optind
== argc
|| optind
+ 2 < argc
)
3390 copy_usage (stderr
, 1);
3392 input_filename
= argv
[optind
];
3393 if (optind
+ 1 < argc
)
3394 output_filename
= argv
[optind
+ 1];
3396 /* Default is to strip no symbols. */
3397 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3398 strip_symbols
= STRIP_NONE
;
3400 if (output_target
== NULL
)
3401 output_target
= input_target
;
3403 if (binary_architecture
!= NULL
)
3405 if (input_target
&& strcmp (input_target
, "binary") == 0)
3407 const bfd_arch_info_type
* temp_arch_info
;
3409 temp_arch_info
= bfd_scan_arch (binary_architecture
);
3411 if (temp_arch_info
!= NULL
)
3413 bfd_external_binary_architecture
= temp_arch_info
->arch
;
3414 bfd_external_machine
= temp_arch_info
->mach
;
3417 fatal (_("architecture %s unknown"), binary_architecture
);
3421 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3422 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3427 if (stat (input_filename
, & statbuf
) < 0)
3428 fatal (_("warning: could not locate '%s'. System error message: %s"),
3429 input_filename
, strerror (errno
));
3431 /* If there is no destination file, or the source and destination files
3432 are the same, then create a temp and rename the result into the input. */
3433 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3434 tmpname
= make_tempname (input_filename
);
3436 tmpname
= output_filename
;
3438 if (tmpname
== NULL
)
3439 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3440 input_filename
, strerror (errno
));
3442 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3446 set_times (tmpname
, &statbuf
);
3447 if (tmpname
!= output_filename
)
3448 smart_rename (tmpname
, input_filename
, preserve_dates
);
3451 unlink_if_ordinary (tmpname
);
3455 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3459 if (p
->change_vma
!= CHANGE_IGNORE
)
3463 sprintf_vma (buff
, p
->vma_val
);
3465 /* xgettext:c-format */
3466 non_fatal (_("%s %s%c0x%s never used"),
3467 "--change-section-vma",
3469 p
->change_vma
== CHANGE_SET
? '=' : '+',
3473 if (p
->change_lma
!= CHANGE_IGNORE
)
3477 sprintf_vma (buff
, p
->lma_val
);
3479 /* xgettext:c-format */
3480 non_fatal (_("%s %s%c0x%s never used"),
3481 "--change-section-lma",
3483 p
->change_lma
== CHANGE_SET
? '=' : '+',
3494 main (int argc
, char *argv
[])
3496 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3497 setlocale (LC_MESSAGES
, "");
3499 #if defined (HAVE_SETLOCALE)
3500 setlocale (LC_CTYPE
, "");
3502 bindtextdomain (PACKAGE
, LOCALEDIR
);
3503 textdomain (PACKAGE
);
3505 program_name
= argv
[0];
3506 xmalloc_set_program_name (program_name
);
3508 START_PROGRESS (program_name
, 0);
3510 expandargv (&argc
, &argv
);
3512 strip_symbols
= STRIP_UNDEF
;
3513 discard_locals
= LOCALS_UNDEF
;
3516 set_default_bfd_target ();
3520 int i
= strlen (program_name
);
3521 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3522 /* Drop the .exe suffix, if any. */
3523 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3526 program_name
[i
] = '\0';
3529 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3533 strip_main (argc
, argv
);
3535 copy_main (argc
, argv
);
3537 END_PROGRESS (program_name
);