1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 struct is_specified_symbol_predicate_data
42 /* A list to support redefine_sym. */
47 struct redefine_node
*next
;
50 typedef struct section_rename
52 const char * old_name
;
53 const char * new_name
;
55 struct section_rename
* next
;
59 /* List of sections to be renamed. */
60 static section_rename
*section_rename_list
;
62 #define RETURN_NONFATAL(bfd) \
64 status = 1; bfd_nonfatal_message (NULL, bfd, NULL, NULL); return; \
67 static asymbol
**isympp
= NULL
; /* Input symbols. */
68 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
70 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
71 static int copy_byte
= -1;
72 static int interleave
= 4;
74 static bfd_boolean verbose
; /* Print file and target names. */
75 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
76 static int status
= 0; /* Exit status. */
81 STRIP_NONE
, /* Don't strip. */
82 STRIP_DEBUG
, /* Strip all debugger symbols. */
83 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
84 STRIP_NONDEBUG
, /* Strip everything but debug info. */
85 STRIP_ALL
/* Strip all symbols. */
88 /* Which symbols to remove. */
89 static enum strip_action strip_symbols
;
94 LOCALS_START_L
, /* Discard locals starting with L. */
95 LOCALS_ALL
/* Discard all locals. */
98 /* Which local symbols to remove. Overrides STRIP_ALL. */
99 static enum locals_action discard_locals
;
101 /* What kind of change to perform. */
109 /* Structure used to hold lists of sections and actions to take. */
112 struct section_list
* next
; /* Next section to change. */
113 const char * name
; /* Section name. */
114 bfd_boolean used
; /* Whether this entry was used. */
115 bfd_boolean remove
; /* Whether to remove this section. */
116 bfd_boolean copy
; /* Whether to copy this section. */
117 enum change_action change_vma
;/* Whether to change or set VMA. */
118 bfd_vma vma_val
; /* Amount to change by or set to. */
119 enum change_action change_lma
;/* Whether to change or set LMA. */
120 bfd_vma lma_val
; /* Amount to change by or set to. */
121 bfd_boolean set_flags
; /* Whether to set the section flags. */
122 flagword flags
; /* What to set the section flags to. */
125 static struct section_list
*change_sections
;
127 /* TRUE if some sections are to be removed. */
128 static bfd_boolean sections_removed
;
130 /* TRUE if only some sections are to be copied. */
131 static bfd_boolean sections_copied
;
133 /* Changes to the start address. */
134 static bfd_vma change_start
= 0;
135 static bfd_boolean set_start_set
= FALSE
;
136 static bfd_vma set_start
;
138 /* Changes to section addresses. */
139 static bfd_vma change_section_address
= 0;
141 /* Filling gaps between sections. */
142 static bfd_boolean gap_fill_set
= FALSE
;
143 static bfd_byte gap_fill
= 0;
145 /* Pad to a given address. */
146 static bfd_boolean pad_to_set
= FALSE
;
147 static bfd_vma pad_to
;
149 /* Use alternative machine code? */
150 static unsigned long use_alt_mach_code
= 0;
152 /* Output BFD flags user wants to set or clear */
153 static flagword bfd_flags_to_set
;
154 static flagword bfd_flags_to_clear
;
156 /* List of sections to add. */
159 /* Next section to add. */
160 struct section_add
*next
;
161 /* Name of section to add. */
163 /* Name of file holding section contents. */
164 const char *filename
;
167 /* Contents of file. */
169 /* BFD section, after it has been added. */
173 /* List of sections to add to the output BFD. */
174 static struct section_add
*add_sections
;
176 /* If non-NULL the argument to --add-gnu-debuglink.
177 This should be the filename to store in the .gnu_debuglink section. */
178 static const char * gnu_debuglink_filename
= NULL
;
180 /* Whether to convert debugging information. */
181 static bfd_boolean convert_debugging
= FALSE
;
183 /* Whether to change the leading character in symbol names. */
184 static bfd_boolean change_leading_char
= FALSE
;
186 /* Whether to remove the leading character from global symbol names. */
187 static bfd_boolean remove_leading_char
= FALSE
;
189 /* Whether to permit wildcard in symbol comparison. */
190 static bfd_boolean wildcard
= FALSE
;
192 /* True if --localize-hidden is in effect. */
193 static bfd_boolean localize_hidden
= FALSE
;
195 /* List of symbols to strip, keep, localize, keep-global, weaken,
197 static htab_t strip_specific_htab
= NULL
;
198 static htab_t strip_unneeded_htab
= NULL
;
199 static htab_t keep_specific_htab
= NULL
;
200 static htab_t localize_specific_htab
= NULL
;
201 static htab_t globalize_specific_htab
= NULL
;
202 static htab_t keepglobal_specific_htab
= NULL
;
203 static htab_t weaken_specific_htab
= NULL
;
204 static struct redefine_node
*redefine_sym_list
= NULL
;
206 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
207 static bfd_boolean weaken
= FALSE
;
209 /* If this is TRUE, we retain BSF_FILE symbols. */
210 static bfd_boolean keep_file_symbols
= FALSE
;
212 /* Prefix symbols/sections. */
213 static char *prefix_symbols_string
= 0;
214 static char *prefix_sections_string
= 0;
215 static char *prefix_alloc_sections_string
= 0;
217 /* True if --extract-symbol was passed on the command line. */
218 static bfd_boolean extract_symbol
= FALSE
;
220 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
221 of <reverse_bytes> bytes within each output section. */
222 static int reverse_bytes
= 0;
225 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
226 enum command_line_switch
228 OPTION_ADD_SECTION
=150,
229 OPTION_CHANGE_ADDRESSES
,
230 OPTION_CHANGE_LEADING_CHAR
,
232 OPTION_CHANGE_SECTION_ADDRESS
,
233 OPTION_CHANGE_SECTION_LMA
,
234 OPTION_CHANGE_SECTION_VMA
,
235 OPTION_CHANGE_WARNINGS
,
238 OPTION_NO_CHANGE_WARNINGS
,
240 OPTION_REMOVE_LEADING_CHAR
,
241 OPTION_SET_SECTION_FLAGS
,
243 OPTION_STRIP_UNNEEDED
,
246 OPTION_REDEFINE_SYMS
,
249 OPTION_STRIP_SYMBOLS
,
250 OPTION_STRIP_UNNEEDED_SYMBOL
,
251 OPTION_STRIP_UNNEEDED_SYMBOLS
,
253 OPTION_LOCALIZE_HIDDEN
,
254 OPTION_LOCALIZE_SYMBOLS
,
255 OPTION_GLOBALIZE_SYMBOL
,
256 OPTION_GLOBALIZE_SYMBOLS
,
257 OPTION_KEEPGLOBAL_SYMBOLS
,
258 OPTION_WEAKEN_SYMBOLS
,
259 OPTION_RENAME_SECTION
,
260 OPTION_ALT_MACH_CODE
,
261 OPTION_PREFIX_SYMBOLS
,
262 OPTION_PREFIX_SECTIONS
,
263 OPTION_PREFIX_ALLOC_SECTIONS
,
265 OPTION_ADD_GNU_DEBUGLINK
,
266 OPTION_ONLY_KEEP_DEBUG
,
267 OPTION_KEEP_FILE_SYMBOLS
,
268 OPTION_READONLY_TEXT
,
269 OPTION_WRITABLE_TEXT
,
272 OPTION_EXTRACT_SYMBOL
,
276 /* Options to handle if running as "strip". */
278 static struct option strip_options
[] =
280 {"discard-all", no_argument
, 0, 'x'},
281 {"discard-locals", no_argument
, 0, 'X'},
282 {"format", required_argument
, 0, 'F'}, /* Obsolete */
283 {"help", no_argument
, 0, 'h'},
284 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
285 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
286 {"input-target", required_argument
, 0, 'I'},
287 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
288 {"keep-symbol", required_argument
, 0, 'K'},
289 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
290 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
291 {"output-target", required_argument
, 0, 'O'},
292 {"output-file", required_argument
, 0, 'o'},
293 {"preserve-dates", no_argument
, 0, 'p'},
294 {"remove-section", required_argument
, 0, 'R'},
295 {"strip-all", no_argument
, 0, 's'},
296 {"strip-debug", no_argument
, 0, 'S'},
297 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
298 {"strip-symbol", required_argument
, 0, 'N'},
299 {"target", required_argument
, 0, 'F'},
300 {"verbose", no_argument
, 0, 'v'},
301 {"version", no_argument
, 0, 'V'},
302 {"wildcard", no_argument
, 0, 'w'},
303 {0, no_argument
, 0, 0}
306 /* Options to handle if running as "objcopy". */
308 static struct option copy_options
[] =
310 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
311 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
312 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
313 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
314 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
315 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
316 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
317 {"binary-architecture", required_argument
, 0, 'B'},
318 {"byte", required_argument
, 0, 'b'},
319 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
320 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
321 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
322 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
323 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
324 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
325 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
326 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
327 {"discard-all", no_argument
, 0, 'x'},
328 {"discard-locals", no_argument
, 0, 'X'},
329 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
330 {"format", required_argument
, 0, 'F'}, /* Obsolete */
331 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
332 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
333 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
334 {"help", no_argument
, 0, 'h'},
335 {"impure", no_argument
, 0, OPTION_IMPURE
},
336 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
337 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
338 {"input-target", required_argument
, 0, 'I'},
339 {"interleave", required_argument
, 0, 'i'},
340 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
341 {"keep-global-symbol", required_argument
, 0, 'G'},
342 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
343 {"keep-symbol", required_argument
, 0, 'K'},
344 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
345 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
346 {"localize-symbol", required_argument
, 0, 'L'},
347 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
348 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
349 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
350 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
351 {"only-section", required_argument
, 0, 'j'},
352 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
353 {"output-target", required_argument
, 0, 'O'},
354 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
355 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
356 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
357 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
358 {"preserve-dates", no_argument
, 0, 'p'},
359 {"pure", no_argument
, 0, OPTION_PURE
},
360 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
361 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
362 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
363 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
364 {"remove-section", required_argument
, 0, 'R'},
365 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
366 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
367 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
368 {"set-start", required_argument
, 0, OPTION_SET_START
},
369 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
370 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
371 {"strip-all", no_argument
, 0, 'S'},
372 {"strip-debug", no_argument
, 0, 'g'},
373 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
374 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
375 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
376 {"strip-symbol", required_argument
, 0, 'N'},
377 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
378 {"target", required_argument
, 0, 'F'},
379 {"verbose", no_argument
, 0, 'v'},
380 {"version", no_argument
, 0, 'V'},
381 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
382 {"weaken-symbol", required_argument
, 0, 'W'},
383 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
384 {"wildcard", no_argument
, 0, 'w'},
385 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
386 {0, no_argument
, 0, 0}
390 extern char *program_name
;
392 /* This flag distinguishes between strip and objcopy:
393 1 means this is 'strip'; 0 means this is 'objcopy'.
394 -1 means if we should use argv[0] to decide. */
397 /* The maximum length of an S record. This variable is declared in srec.c
398 and can be modified by the --srec-len parameter. */
399 extern unsigned int Chunk
;
401 /* Restrict the generation of Srecords to type S3 only.
402 This variable is declare in bfd/srec.c and can be toggled
403 on by the --srec-forceS3 command line switch. */
404 extern bfd_boolean S3Forced
;
406 /* Defined in bfd/binary.c. Used to set architecture and machine of input
408 extern enum bfd_architecture bfd_external_binary_architecture
;
409 extern unsigned long bfd_external_machine
;
411 /* Forward declarations. */
412 static void setup_section (bfd
*, asection
*, void *);
413 static void setup_bfd_headers (bfd
*, bfd
*);
414 static void copy_section (bfd
*, asection
*, void *);
415 static void get_sections (bfd
*, asection
*, void *);
416 static int compare_section_lma (const void *, const void *);
417 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
418 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
419 static const char *lookup_sym_redefinition (const char *);
422 copy_usage (FILE *stream
, int exit_status
)
424 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
425 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
426 fprintf (stream
, _(" The options are:\n"));
427 fprintf (stream
, _("\
428 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
429 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
430 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
431 -F --target <bfdname> Set both input and output format to <bfdname>\n\
432 --debugging Convert debugging information, if possible\n\
433 -p --preserve-dates Copy modified/access timestamps to the output\n\
434 -j --only-section <name> Only copy section <name> into the output\n\
435 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
436 -R --remove-section <name> Remove section <name> from the output\n\
437 -S --strip-all Remove all symbol and relocation information\n\
438 -g --strip-debug Remove all debugging symbols & sections\n\
439 --strip-unneeded Remove all symbols not needed by relocations\n\
440 -N --strip-symbol <name> Do not copy symbol <name>\n\
441 --strip-unneeded-symbol <name>\n\
442 Do not copy symbol <name> unless needed by\n\
444 --only-keep-debug Strip everything but the debug information\n\
445 --extract-symbol Remove section contents but keep symbols\n\
446 -K --keep-symbol <name> Do not strip symbol <name>\n\
447 --keep-file-symbols Do not strip file symbol(s)\n\
448 --localize-hidden Turn all ELF hidden symbols into locals\n\
449 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
450 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
451 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
452 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
453 --weaken Force all global symbols to be marked as weak\n\
454 -w --wildcard Permit wildcard in symbol comparison\n\
455 -x --discard-all Remove all non-global symbols\n\
456 -X --discard-locals Remove any compiler-generated symbols\n\
457 -i --interleave <number> Only copy one out of every <number> bytes\n\
458 -b --byte <num> Select byte <num> in every interleaved block\n\
459 --gap-fill <val> Fill gaps between sections with <val>\n\
460 --pad-to <addr> Pad the last section up to address <addr>\n\
461 --set-start <addr> Set the start address to <addr>\n\
462 {--change-start|--adjust-start} <incr>\n\
463 Add <incr> to the start address\n\
464 {--change-addresses|--adjust-vma} <incr>\n\
465 Add <incr> to LMA, VMA and start addresses\n\
466 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
467 Change LMA and VMA of section <name> by <val>\n\
468 --change-section-lma <name>{=|+|-}<val>\n\
469 Change the LMA of section <name> by <val>\n\
470 --change-section-vma <name>{=|+|-}<val>\n\
471 Change the VMA of section <name> by <val>\n\
472 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
473 Warn if a named section does not exist\n\
474 --set-section-flags <name>=<flags>\n\
475 Set section <name>'s properties to <flags>\n\
476 --add-section <name>=<file> Add section <name> found in <file> to output\n\
477 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
478 --change-leading-char Force output format's leading character style\n\
479 --remove-leading-char Remove leading character from global symbols\n\
480 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
481 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
482 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
484 --srec-len <number> Restrict the length of generated Srecords\n\
485 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
486 --strip-symbols <file> -N for all symbols listed in <file>\n\
487 --strip-unneeded-symbols <file>\n\
488 --strip-unneeded-symbol for all symbols listed\n\
490 --keep-symbols <file> -K for all symbols listed in <file>\n\
491 --localize-symbols <file> -L for all symbols listed in <file>\n\
492 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
493 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
494 --weaken-symbols <file> -W for all symbols listed in <file>\n\
495 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
496 --writable-text Mark the output text as writable\n\
497 --readonly-text Make the output text write protected\n\
498 --pure Mark the output file as demand paged\n\
499 --impure Mark the output file as impure\n\
500 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
501 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
502 --prefix-alloc-sections <prefix>\n\
503 Add <prefix> to start of every allocatable\n\
505 -v --verbose List all object files modified\n\
506 @<file> Read options from <file>\n\
507 -V --version Display this program's version number\n\
508 -h --help Display this output\n\
509 --info List object formats & architectures supported\n\
511 list_supported_targets (program_name
, stream
);
512 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
513 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
518 strip_usage (FILE *stream
, int exit_status
)
520 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
521 fprintf (stream
, _(" Removes symbols and sections from files\n"));
522 fprintf (stream
, _(" The options are:\n"));
523 fprintf (stream
, _("\
524 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
525 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
526 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
527 -p --preserve-dates Copy modified/access timestamps to the output\n\
528 -R --remove-section=<name> Remove section <name> from the output\n\
529 -s --strip-all Remove all symbol and relocation information\n\
530 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
531 --strip-unneeded Remove all symbols not needed by relocations\n\
532 --only-keep-debug Strip everything but the debug information\n\
533 -N --strip-symbol=<name> Do not copy symbol <name>\n\
534 -K --keep-symbol=<name> Do not strip symbol <name>\n\
535 --keep-file-symbols Do not strip file symbol(s)\n\
536 -w --wildcard Permit wildcard in symbol comparison\n\
537 -x --discard-all Remove all non-global symbols\n\
538 -X --discard-locals Remove any compiler-generated symbols\n\
539 -v --verbose List all object files modified\n\
540 -V --version Display this program's version number\n\
541 -h --help Display this output\n\
542 --info List object formats & architectures supported\n\
543 -o <file> Place stripped output into <file>\n\
546 list_supported_targets (program_name
, stream
);
547 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
548 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
552 /* Parse section flags into a flagword, with a fatal error if the
553 string can't be parsed. */
556 parse_flags (const char *s
)
566 snext
= strchr (s
, ',');
576 #define PARSE_FLAG(fname,fval) \
577 else if (strncasecmp (fname, s, len) == 0) ret |= fval
578 PARSE_FLAG ("alloc", SEC_ALLOC
);
579 PARSE_FLAG ("load", SEC_LOAD
);
580 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
581 PARSE_FLAG ("readonly", SEC_READONLY
);
582 PARSE_FLAG ("debug", SEC_DEBUGGING
);
583 PARSE_FLAG ("code", SEC_CODE
);
584 PARSE_FLAG ("data", SEC_DATA
);
585 PARSE_FLAG ("rom", SEC_ROM
);
586 PARSE_FLAG ("share", SEC_COFF_SHARED
);
587 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
593 copy
= xmalloc (len
+ 1);
594 strncpy (copy
, s
, len
);
596 non_fatal (_("unrecognized section flag `%s'"), copy
);
597 fatal (_("supported flags: %s"),
598 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
608 /* Find and optionally add an entry in the change_sections list. */
610 static struct section_list
*
611 find_section_list (const char *name
, bfd_boolean add
)
613 struct section_list
*p
;
615 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
616 if (strcmp (p
->name
, name
) == 0)
622 p
= xmalloc (sizeof (struct section_list
));
627 p
->change_vma
= CHANGE_IGNORE
;
628 p
->change_lma
= CHANGE_IGNORE
;
631 p
->set_flags
= FALSE
;
634 p
->next
= change_sections
;
640 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
643 eq_string (const void *s1
, const void *s2
)
645 return strcmp (s1
, s2
) == 0;
649 create_symbol_htab (void)
651 return htab_create_alloc (16, htab_hash_string
, eq_string
, NULL
, xcalloc
, free
);
655 create_symbol_htabs (void)
657 strip_specific_htab
= create_symbol_htab ();
658 strip_unneeded_htab
= create_symbol_htab ();
659 keep_specific_htab
= create_symbol_htab ();
660 localize_specific_htab
= create_symbol_htab ();
661 globalize_specific_htab
= create_symbol_htab ();
662 keepglobal_specific_htab
= create_symbol_htab ();
663 weaken_specific_htab
= create_symbol_htab ();
666 /* Add a symbol to strip_specific_list. */
669 add_specific_symbol (const char *name
, htab_t htab
)
671 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
674 /* Add symbols listed in `filename' to strip_specific_list. */
676 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
677 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
680 add_specific_symbols (const char *filename
, htab_t htab
)
686 unsigned int line_count
;
688 size
= get_file_size (filename
);
695 buffer
= xmalloc (size
+ 2);
696 f
= fopen (filename
, FOPEN_RT
);
698 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
700 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
701 fatal (_("%s: fread failed"), filename
);
704 buffer
[size
] = '\n';
705 buffer
[size
+ 1] = '\0';
709 for (line
= buffer
; * line
!= '\0'; line
++)
714 int finished
= FALSE
;
716 for (eol
= line
;; eol
++)
722 /* Cope with \n\r. */
730 /* Cope with \r\n. */
741 /* Line comment, Terminate the line here, in case a
742 name is present and then allow the rest of the
743 loop to find the real end of the line. */
755 /* A name may now exist somewhere between 'line' and 'eol'.
756 Strip off leading whitespace and trailing whitespace,
757 then add it to the list. */
758 for (name
= line
; IS_WHITESPACE (* name
); name
++)
760 for (name_end
= name
;
761 (! IS_WHITESPACE (* name_end
))
762 && (! IS_LINE_TERMINATOR (* name_end
));
766 if (! IS_LINE_TERMINATOR (* name_end
))
770 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
773 if (! IS_LINE_TERMINATOR (* extra
))
774 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
775 filename
, line_count
);
781 add_specific_symbol (name
, htab
);
783 /* Advance line pointer to end of line. The 'eol ++' in the for
784 loop above will then advance us to the start of the next line. */
790 /* See whether a symbol should be stripped or kept
791 based on strip_specific_list and keep_symbols. */
794 is_specified_symbol_predicate (void **slot
, void *data
)
796 struct is_specified_symbol_predicate_data
*d
= data
;
797 const char *slot_name
= *slot
;
799 if (*slot_name
!= '!')
801 if (! fnmatch (slot_name
, d
->name
, 0))
804 /* Stop traversal. */
810 if (fnmatch (slot_name
+ 1, d
->name
, 0))
813 /* Stop traversal. */
818 /* Continue traversal. */
823 is_specified_symbol (const char *name
, htab_t htab
)
827 struct is_specified_symbol_predicate_data data
;
832 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
837 return htab_find (htab
, name
) != NULL
;
840 /* Return a pointer to the symbol used as a signature for GROUP. */
843 group_signature (asection
*group
)
845 bfd
*abfd
= group
->owner
;
846 Elf_Internal_Shdr
*ghdr
;
848 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
851 ghdr
= &elf_section_data (group
)->this_hdr
;
852 if (ghdr
->sh_link
< elf_numsections (abfd
))
854 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
855 Elf_Internal_Shdr
*symhdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
857 if (symhdr
->sh_type
== SHT_SYMTAB
858 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
859 return isympp
[ghdr
->sh_info
- 1];
864 /* See if a section is being removed. */
867 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
869 if (sections_removed
|| sections_copied
)
871 struct section_list
*p
;
873 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
875 if (sections_removed
&& p
!= NULL
&& p
->remove
)
877 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
881 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
883 if (strip_symbols
== STRIP_DEBUG
884 || strip_symbols
== STRIP_UNNEEDED
885 || strip_symbols
== STRIP_ALL
886 || discard_locals
== LOCALS_ALL
887 || convert_debugging
)
890 if (strip_symbols
== STRIP_NONDEBUG
)
894 if ((bfd_get_section_flags (abfd
, sec
) & SEC_GROUP
) != 0)
900 Group sections look like debugging sections but they are not.
901 (They have a non-zero size but they are not ALLOCated). */
902 if (strip_symbols
== STRIP_NONDEBUG
)
906 If we are going to strip the group signature symbol, then
907 strip the group section too. */
908 gsym
= group_signature (sec
);
913 if ((strip_symbols
== STRIP_ALL
914 && !is_specified_symbol (gname
, keep_specific_htab
))
915 || is_specified_symbol (gname
, strip_specific_htab
))
922 /* Return true if SYM is a hidden symbol. */
925 is_hidden_symbol (asymbol
*sym
)
927 elf_symbol_type
*elf_sym
;
929 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
931 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
940 /* Choose which symbol entries to copy; put the result in OSYMS.
941 We don't copy in place, because that confuses the relocs.
942 Return the number of symbols to print. */
945 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
946 asymbol
**isyms
, long symcount
)
948 asymbol
**from
= isyms
, **to
= osyms
;
949 long src_count
= 0, dst_count
= 0;
950 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
952 for (; src_count
< symcount
; src_count
++)
954 asymbol
*sym
= from
[src_count
];
955 flagword flags
= sym
->flags
;
956 char *name
= (char *) bfd_asymbol_name (sym
);
958 bfd_boolean used_in_reloc
= FALSE
;
959 bfd_boolean undefined
;
960 bfd_boolean rem_leading_char
;
961 bfd_boolean add_leading_char
;
963 undefined
= bfd_is_und_section (bfd_get_section (sym
));
965 if (redefine_sym_list
)
967 char *old_name
, *new_name
;
969 old_name
= (char *) bfd_asymbol_name (sym
);
970 new_name
= (char *) lookup_sym_redefinition (old_name
);
971 bfd_asymbol_name (sym
) = new_name
;
975 /* Check if we will remove the current leading character. */
977 (name
[0] == bfd_get_symbol_leading_char (abfd
))
978 && (change_leading_char
979 || (remove_leading_char
980 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
982 || bfd_is_com_section (bfd_get_section (sym
)))));
984 /* Check if we will add a new leading character. */
987 && (bfd_get_symbol_leading_char (obfd
) != '\0')
988 && (bfd_get_symbol_leading_char (abfd
) == '\0'
989 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
991 /* Short circuit for change_leading_char if we can do it in-place. */
992 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
994 name
[0] = bfd_get_symbol_leading_char (obfd
);
995 bfd_asymbol_name (sym
) = name
;
996 rem_leading_char
= FALSE
;
997 add_leading_char
= FALSE
;
1000 /* Remove leading char. */
1001 if (rem_leading_char
)
1002 bfd_asymbol_name (sym
) = ++name
;
1004 /* Add new leading char and/or prefix. */
1005 if (add_leading_char
|| prefix_symbols_string
)
1009 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
1010 + strlen (name
) + 1);
1011 if (add_leading_char
)
1012 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1014 if (prefix_symbols_string
)
1016 strcpy (ptr
, prefix_symbols_string
);
1017 ptr
+= strlen (prefix_symbols_string
);
1021 bfd_asymbol_name (sym
) = n
;
1025 if (strip_symbols
== STRIP_ALL
)
1027 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1028 || ((flags
& BSF_SECTION_SYM
) != 0
1029 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
1033 used_in_reloc
= TRUE
;
1035 else if (relocatable
/* Relocatable file. */
1036 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
1038 else if (bfd_decode_symclass (sym
) == 'I')
1039 /* Global symbols in $idata sections need to be retained
1040 even if relocatable is FALSE. External users of the
1041 library containing the $idata section may reference these
1044 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1045 || (flags
& BSF_WEAK
) != 0
1047 || bfd_is_com_section (bfd_get_section (sym
)))
1048 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1049 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1050 keep
= (strip_symbols
!= STRIP_DEBUG
1051 && strip_symbols
!= STRIP_UNNEEDED
1052 && ! convert_debugging
);
1053 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
1054 /* COMDAT sections store special information in local
1055 symbols, so we cannot risk stripping any of them. */
1057 else /* Local symbol. */
1058 keep
= (strip_symbols
!= STRIP_UNNEEDED
1059 && (discard_locals
!= LOCALS_ALL
1060 && (discard_locals
!= LOCALS_START_L
1061 || ! bfd_is_local_label (abfd
, sym
))));
1063 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1065 /* There are multiple ways to set 'keep' above, but if it
1066 was the relocatable symbol case, then that's an error. */
1069 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1077 && !(flags
& BSF_KEEP
)
1078 && is_specified_symbol (name
, strip_unneeded_htab
))
1082 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1083 || is_specified_symbol (name
, keep_specific_htab
)))
1086 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
1091 if ((flags
& BSF_GLOBAL
) != 0
1092 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1094 sym
->flags
&= ~ BSF_GLOBAL
;
1095 sym
->flags
|= BSF_WEAK
;
1099 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1100 && (is_specified_symbol (name
, localize_specific_htab
)
1101 || (htab_elements (keepglobal_specific_htab
) != 0
1102 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1103 || (localize_hidden
&& is_hidden_symbol (sym
))))
1105 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1106 sym
->flags
|= BSF_LOCAL
;
1110 && (flags
& BSF_LOCAL
)
1111 && is_specified_symbol (name
, globalize_specific_htab
))
1113 sym
->flags
&= ~ BSF_LOCAL
;
1114 sym
->flags
|= BSF_GLOBAL
;
1117 to
[dst_count
++] = sym
;
1121 to
[dst_count
] = NULL
;
1126 /* Find the redefined name of symbol SOURCE. */
1129 lookup_sym_redefinition (const char *source
)
1131 struct redefine_node
*list
;
1133 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1134 if (strcmp (source
, list
->source
) == 0)
1135 return list
->target
;
1140 /* Add a node to a symbol redefine list. */
1143 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1145 struct redefine_node
**p
;
1146 struct redefine_node
*list
;
1147 struct redefine_node
*new_node
;
1149 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1151 if (strcmp (source
, list
->source
) == 0)
1152 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1155 if (strcmp (target
, list
->target
) == 0)
1156 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1160 new_node
= xmalloc (sizeof (struct redefine_node
));
1162 new_node
->source
= strdup (source
);
1163 new_node
->target
= strdup (target
);
1164 new_node
->next
= NULL
;
1169 /* Handle the --redefine-syms option. Read lines containing "old new"
1170 from the file, and add them to the symbol redefine list. */
1173 add_redefine_syms_file (const char *filename
)
1182 file
= fopen (filename
, "r");
1184 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1185 filename
, strerror (errno
));
1188 buf
= xmalloc (bufsize
);
1196 /* Collect the input symbol name. */
1197 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1205 buf
= xrealloc (buf
, bufsize
);
1213 /* Eat white space between the symbol names. */
1214 while (IS_WHITESPACE (c
))
1216 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1221 /* Collect the output symbol name. */
1223 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1231 buf
= xrealloc (buf
, bufsize
);
1239 /* Eat white space at end of line. */
1240 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1245 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1246 || c
== '\n' || c
== EOF
)
1249 /* Append the redefinition to the list. */
1251 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1262 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1264 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1265 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1268 /* Eat the rest of the line and finish it. */
1269 while (c
!= '\n' && c
!= EOF
)
1275 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1280 /* Copy unkown object file IBFD onto OBFD.
1281 Returns TRUE upon success, FALSE otherwise. */
1284 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1292 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1294 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
), NULL
, NULL
, NULL
);
1301 non_fatal (_("stat returns negative size for `%s'"),
1302 bfd_get_archive_filename (ibfd
));
1306 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1308 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1313 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1314 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1316 cbuf
= xmalloc (BUFSIZE
);
1318 while (ncopied
< size
)
1320 tocopy
= size
- ncopied
;
1321 if (tocopy
> BUFSIZE
)
1324 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1325 != (bfd_size_type
) tocopy
)
1327 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
),
1333 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1334 != (bfd_size_type
) tocopy
)
1336 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1344 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1349 /* Copy object file IBFD onto OBFD.
1350 Returns TRUE upon success, FALSE otherwise. */
1353 copy_object (bfd
*ibfd
, bfd
*obfd
)
1357 asection
**osections
= NULL
;
1358 asection
*gnu_debuglink_section
= NULL
;
1359 bfd_size_type
*gaps
= NULL
;
1360 bfd_size_type max_gap
= 0;
1363 enum bfd_architecture iarch
;
1366 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1367 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1368 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1369 fatal (_("Unable to change endianness of input file(s)"));
1371 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1373 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1378 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1379 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1380 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1389 start
= bfd_get_start_address (ibfd
);
1390 start
+= change_start
;
1393 /* Neither the start address nor the flags
1394 need to be set for a core file. */
1395 if (bfd_get_format (obfd
) != bfd_core
)
1399 flags
= bfd_get_file_flags (ibfd
);
1400 flags
|= bfd_flags_to_set
;
1401 flags
&= ~bfd_flags_to_clear
;
1402 flags
&= bfd_applicable_file_flags (obfd
);
1404 if (!bfd_set_start_address (obfd
, start
)
1405 || !bfd_set_file_flags (obfd
, flags
))
1407 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
),
1413 /* Copy architecture of input file to output file. */
1414 iarch
= bfd_get_arch (ibfd
);
1415 imach
= bfd_get_mach (ibfd
);
1416 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1417 && (ibfd
->target_defaulted
1418 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1420 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1421 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1422 bfd_get_archive_filename (ibfd
));
1424 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1425 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1426 bfd_get_mach (ibfd
)));
1430 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1432 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
), NULL
, NULL
, NULL
);
1439 if (osympp
!= isympp
)
1445 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1448 bfd_nonfatal_message (bfd_get_archive_filename (ibfd
), NULL
, NULL
, NULL
);
1452 osympp
= isympp
= xmalloc (symsize
);
1453 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1456 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1460 /* BFD mandates that all output sections be created and sizes set before
1461 any output is done. Thus, we traverse all sections multiple times. */
1462 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1464 if (!extract_symbol
)
1465 setup_bfd_headers (ibfd
, obfd
);
1467 if (add_sections
!= NULL
)
1469 struct section_add
*padd
;
1470 struct section_list
*pset
;
1472 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1476 pset
= find_section_list (padd
->name
, FALSE
);
1480 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1481 if (pset
!= NULL
&& pset
->set_flags
)
1482 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1484 /* bfd_make_section_with_flags() does not return very helpful
1485 error codes, so check for the most likely user error first. */
1486 if (bfd_get_section_by_name (obfd
, padd
->name
))
1488 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1489 _("can't add section '%s'"), padd
->name
);
1494 padd
->section
= bfd_make_section_with_flags (obfd
, padd
->name
, flags
);
1495 if (padd
->section
== NULL
)
1497 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1498 _("can't create section `%s'"),
1504 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1506 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1512 if (pset
->change_vma
!= CHANGE_IGNORE
)
1513 if (! bfd_set_section_vma (obfd
, padd
->section
,
1516 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1520 if (pset
->change_lma
!= CHANGE_IGNORE
)
1522 padd
->section
->lma
= pset
->lma_val
;
1524 if (! bfd_set_section_alignment
1525 (obfd
, padd
->section
,
1526 bfd_section_alignment (obfd
, padd
->section
)))
1528 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1536 if (gnu_debuglink_filename
!= NULL
)
1538 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1539 (obfd
, gnu_debuglink_filename
);
1541 if (gnu_debuglink_section
== NULL
)
1543 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1544 _("cannot create debug link section `%s'"),
1545 gnu_debuglink_filename
);
1549 /* Special processing for PE format files. We
1550 have no way to distinguish PE from COFF here. */
1551 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1553 bfd_vma debuglink_vma
;
1554 asection
* highest_section
;
1557 /* The PE spec requires that all sections be adjacent and sorted
1558 in ascending order of VMA. It also specifies that debug
1559 sections should be last. This is despite the fact that debug
1560 sections are not loaded into memory and so in theory have no
1563 This means that the debuglink section must be given a non-zero
1564 VMA which makes it contiguous with other debug sections. So
1565 walk the current section list, find the section with the
1566 highest VMA and start the debuglink section after that one. */
1567 for (sec
= obfd
->sections
, highest_section
= NULL
;
1571 && (highest_section
== NULL
1572 || sec
->vma
> highest_section
->vma
))
1573 highest_section
= sec
;
1575 if (highest_section
)
1576 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1577 + highest_section
->size
,
1578 /* FIXME: We ought to be using
1579 COFF_PAGE_SIZE here or maybe
1580 bfd_get_section_alignment() (if it
1581 was set) but since this is for PE
1582 and we know the required alignment
1583 it is easier just to hard code it. */
1586 /* Umm, not sure what to do in this case. */
1587 debuglink_vma
= 0x1000;
1589 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1593 if (bfd_count_sections (obfd
) != 0
1594 && (gap_fill_set
|| pad_to_set
))
1599 /* We must fill in gaps between the sections and/or we must pad
1600 the last section to a specified address. We do this by
1601 grabbing a list of the sections, sorting them by VMA, and
1602 increasing the section sizes as required to fill the gaps.
1603 We write out the gap contents below. */
1605 c
= bfd_count_sections (obfd
);
1606 osections
= xmalloc (c
* sizeof (asection
*));
1608 bfd_map_over_sections (obfd
, get_sections
, &set
);
1610 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1612 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1613 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1617 for (i
= 0; i
< c
- 1; i
++)
1621 bfd_vma gap_start
, gap_stop
;
1623 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1624 if ((flags
& SEC_HAS_CONTENTS
) == 0
1625 || (flags
& SEC_LOAD
) == 0)
1628 size
= bfd_section_size (obfd
, osections
[i
]);
1629 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1630 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1631 if (gap_start
< gap_stop
)
1633 if (! bfd_set_section_size (obfd
, osections
[i
],
1634 size
+ (gap_stop
- gap_start
)))
1636 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
1637 _("Can't fill gap after section"));
1641 gaps
[i
] = gap_stop
- gap_start
;
1642 if (max_gap
< gap_stop
- gap_start
)
1643 max_gap
= gap_stop
- gap_start
;
1653 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1654 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1655 if (lma
+ size
< pad_to
)
1657 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1660 bfd_nonfatal_message (NULL
, obfd
, osections
[c
- 1],
1661 _("can't add padding"));
1666 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1667 if (max_gap
< pad_to
- (lma
+ size
))
1668 max_gap
= pad_to
- (lma
+ size
);
1674 /* Symbol filtering must happen after the output sections
1675 have been created, but before their contents are set. */
1677 if (convert_debugging
)
1678 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, FALSE
);
1680 if (strip_symbols
== STRIP_DEBUG
1681 || strip_symbols
== STRIP_ALL
1682 || strip_symbols
== STRIP_UNNEEDED
1683 || strip_symbols
== STRIP_NONDEBUG
1684 || discard_locals
!= LOCALS_UNDEF
1686 || htab_elements (strip_specific_htab
) != 0
1687 || htab_elements (keep_specific_htab
) != 0
1688 || htab_elements (localize_specific_htab
) != 0
1689 || htab_elements (globalize_specific_htab
) != 0
1690 || htab_elements (keepglobal_specific_htab
) != 0
1691 || htab_elements (weaken_specific_htab
) != 0
1692 || prefix_symbols_string
1695 || convert_debugging
1696 || change_leading_char
1697 || remove_leading_char
1698 || redefine_sym_list
1701 /* Mark symbols used in output relocations so that they
1702 are kept, even if they are local labels or static symbols.
1704 Note we iterate over the input sections examining their
1705 relocations since the relocations for the output sections
1706 haven't been set yet. mark_symbols_used_in_relocations will
1707 ignore input sections which have no corresponding output
1709 if (strip_symbols
!= STRIP_ALL
)
1710 bfd_map_over_sections (ibfd
,
1711 mark_symbols_used_in_relocations
,
1713 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1714 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1717 if (convert_debugging
&& dhandle
!= NULL
)
1719 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1726 bfd_set_symtab (obfd
, osympp
, symcount
);
1728 /* This has to happen after the symbol table has been set. */
1729 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1731 if (add_sections
!= NULL
)
1733 struct section_add
*padd
;
1735 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1737 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1740 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1746 if (gnu_debuglink_filename
!= NULL
)
1748 if (! bfd_fill_in_gnu_debuglink_section
1749 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1751 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1752 _("cannot fill debug link section `%s'"),
1753 gnu_debuglink_filename
);
1758 if (gap_fill_set
|| pad_to_set
)
1763 /* Fill in the gaps. */
1766 buf
= xmalloc (max_gap
);
1767 memset (buf
, gap_fill
, max_gap
);
1769 c
= bfd_count_sections (obfd
);
1770 for (i
= 0; i
< c
; i
++)
1778 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1789 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1792 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
1803 /* Do not copy backend data if --extract-symbol is passed; anything
1804 that needs to look at the section contents will fail. */
1808 /* Allow the BFD backend to copy any private data it understands
1809 from the input BFD to the output BFD. This is done last to
1810 permit the routine to look at the filtered symbol table, which is
1811 important for the ECOFF code at least. */
1812 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1814 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1815 _("error copying private BFD data"));
1819 /* Switch to the alternate machine code. We have to do this at the
1820 very end, because we only initialize the header when we create
1821 the first section. */
1822 if (use_alt_mach_code
!= 0)
1824 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1826 non_fatal (_("this target does not support %lu alternative machine codes"),
1828 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1830 non_fatal (_("treating that number as an absolute e_machine value instead"));
1831 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
1834 non_fatal (_("ignoring the alternative value"));
1841 /* Read each archive element in turn from IBFD, copy the
1842 contents to temp file, and keep the temp file handle.
1843 If 'force_output_target' is TRUE then make sure that
1844 all elements in the new archive are of the type
1848 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
1849 bfd_boolean force_output_target
)
1853 struct name_list
*next
;
1857 bfd
**ptr
= &obfd
->archive_head
;
1861 /* Make a temp directory to hold the contents. */
1862 dir
= make_tempdir (bfd_get_filename (obfd
));
1864 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1867 obfd
->has_armap
= ibfd
->has_armap
;
1868 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
1872 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1874 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1875 RETURN_NONFATAL (obfd
);
1877 while (!status
&& this_element
!= NULL
)
1883 int stat_status
= 0;
1884 bfd_boolean
delete = TRUE
;
1886 /* Create an output file for this member. */
1887 output_name
= concat (dir
, "/",
1888 bfd_get_filename (this_element
), (char *) 0);
1890 /* If the file already exists, make another temp dir. */
1891 if (stat (output_name
, &buf
) >= 0)
1893 output_name
= make_tempdir (output_name
);
1894 if (output_name
== NULL
)
1895 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1898 l
= xmalloc (sizeof (struct name_list
));
1899 l
->name
= output_name
;
1903 output_name
= concat (output_name
, "/",
1904 bfd_get_filename (this_element
), (char *) 0);
1909 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1911 if (stat_status
!= 0)
1912 non_fatal (_("internal stat error on %s"),
1913 bfd_get_filename (this_element
));
1916 l
= xmalloc (sizeof (struct name_list
));
1917 l
->name
= output_name
;
1922 if (bfd_check_format (this_element
, bfd_object
))
1924 /* PR binutils/3110: Cope with archives
1925 containing multiple target types. */
1926 if (force_output_target
)
1927 output_bfd
= bfd_openw (output_name
, output_target
);
1929 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
1931 if (output_bfd
== NULL
)
1933 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
1938 delete = ! copy_object (this_element
, output_bfd
);
1941 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
1943 if (!bfd_close (output_bfd
))
1945 bfd_nonfatal_message (NULL
, output_bfd
, NULL
, NULL
);
1946 /* Error in new object file. Don't change archive. */
1951 goto copy_unknown_element
;
1955 bfd_nonfatal_message (bfd_get_archive_filename (this_element
),
1957 _("Unable to recognise the format of file"));
1959 output_bfd
= bfd_openw (output_name
, output_target
);
1960 copy_unknown_element
:
1961 delete = !copy_unknown_object (this_element
, output_bfd
);
1962 if (!bfd_close_all_done (output_bfd
))
1964 bfd_nonfatal_message (NULL
, output_bfd
, NULL
, NULL
);
1965 /* Error in new object file. Don't change archive. */
1972 unlink (output_name
);
1977 if (preserve_dates
&& stat_status
== 0)
1978 set_times (output_name
, &buf
);
1980 /* Open the newly output file and attach to our list. */
1981 output_bfd
= bfd_openr (output_name
, output_target
);
1983 l
->obfd
= output_bfd
;
1986 ptr
= &output_bfd
->archive_next
;
1988 last_element
= this_element
;
1990 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1992 bfd_close (last_element
);
1997 if (!bfd_close (obfd
))
1998 RETURN_NONFATAL (obfd
);
2000 if (!bfd_close (ibfd
))
2001 RETURN_NONFATAL (obfd
);
2003 /* Delete all the files that we opened. */
2004 for (l
= list
; l
!= NULL
; l
= l
->next
)
2006 if (l
->obfd
== NULL
)
2010 bfd_close (l
->obfd
);
2017 /* The top-level control. */
2020 copy_file (const char *input_filename
, const char *output_filename
,
2021 const char *input_target
, const char *output_target
)
2024 char **obj_matching
;
2025 char **core_matching
;
2027 if (get_file_size (input_filename
) < 1)
2033 /* To allow us to do "strip *" without dying on the first
2034 non-object file, failures are nonfatal. */
2035 ibfd
= bfd_openr (input_filename
, input_target
);
2038 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2043 if (bfd_check_format (ibfd
, bfd_archive
))
2045 bfd_boolean force_output_target
;
2048 /* bfd_get_target does not return the correct value until
2049 bfd_check_format succeeds. */
2050 if (output_target
== NULL
)
2052 output_target
= bfd_get_target (ibfd
);
2053 force_output_target
= FALSE
;
2056 force_output_target
= TRUE
;
2058 obfd
= bfd_openw (output_filename
, output_target
);
2061 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2066 copy_archive (ibfd
, obfd
, output_target
, force_output_target
);
2068 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
2073 /* bfd_get_target does not return the correct value until
2074 bfd_check_format succeeds. */
2075 if (output_target
== NULL
)
2076 output_target
= bfd_get_target (ibfd
);
2078 obfd
= bfd_openw (output_filename
, output_target
);
2081 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2086 if (! copy_object (ibfd
, obfd
))
2089 if (!bfd_close (obfd
))
2090 RETURN_NONFATAL (obfd
);
2092 if (!bfd_close (ibfd
))
2093 RETURN_NONFATAL (ibfd
);
2097 bfd_error_type obj_error
= bfd_get_error ();
2098 bfd_error_type core_error
;
2100 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
2102 /* This probably can't happen.. */
2103 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2104 free (obj_matching
);
2108 core_error
= bfd_get_error ();
2109 /* Report the object error in preference to the core error. */
2110 if (obj_error
!= core_error
)
2111 bfd_set_error (obj_error
);
2113 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2115 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2117 list_matching_formats (obj_matching
);
2118 free (obj_matching
);
2120 if (core_error
== bfd_error_file_ambiguously_recognized
)
2122 list_matching_formats (core_matching
);
2123 free (core_matching
);
2130 /* Add a name to the section renaming list. */
2133 add_section_rename (const char * old_name
, const char * new_name
,
2136 section_rename
* rename
;
2138 /* Check for conflicts first. */
2139 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2140 if (strcmp (rename
->old_name
, old_name
) == 0)
2142 /* Silently ignore duplicate definitions. */
2143 if (strcmp (rename
->new_name
, new_name
) == 0
2144 && rename
->flags
== flags
)
2147 fatal (_("Multiple renames of section %s"), old_name
);
2150 rename
= xmalloc (sizeof (* rename
));
2152 rename
->old_name
= old_name
;
2153 rename
->new_name
= new_name
;
2154 rename
->flags
= flags
;
2155 rename
->next
= section_rename_list
;
2157 section_rename_list
= rename
;
2160 /* Check the section rename list for a new name of the input section
2161 ISECTION. Return the new name if one is found.
2162 Also set RETURNED_FLAGS to the flags to be used for this section. */
2165 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2166 flagword
* returned_flags
)
2168 const char * old_name
= bfd_section_name (ibfd
, isection
);
2169 section_rename
* rename
;
2171 /* Default to using the flags of the input section. */
2172 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2174 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2175 if (strcmp (rename
->old_name
, old_name
) == 0)
2177 if (rename
->flags
!= (flagword
) -1)
2178 * returned_flags
= rename
->flags
;
2180 return rename
->new_name
;
2186 /* Once each of the sections is copied, we may still need to do some
2187 finalization work for private section headers. Do that here. */
2190 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2192 /* Allow the BFD backend to copy any private data it understands
2193 from the input section to the output section. */
2194 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2197 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
2198 _("error in private h eader data"));
2202 /* All went well. */
2206 /* Create a section in OBFD with the same
2207 name and attributes as ISECTION in IBFD. */
2210 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2212 bfd
*obfd
= obfdarg
;
2213 struct section_list
*p
;
2221 char *prefix
= NULL
;
2222 bfd_boolean make_nobits
;
2224 if (is_strip_section (ibfd
, isection
))
2227 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2231 /* Get the, possibly new, name of the output section. */
2232 name
= find_section_rename (ibfd
, isection
, & flags
);
2234 /* Prefix sections. */
2235 if ((prefix_alloc_sections_string
)
2236 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2237 prefix
= prefix_alloc_sections_string
;
2238 else if (prefix_sections_string
)
2239 prefix
= prefix_sections_string
;
2245 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
2251 make_nobits
= FALSE
;
2252 if (p
!= NULL
&& p
->set_flags
)
2253 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2254 else if (strip_symbols
== STRIP_NONDEBUG
2255 && (flags
& SEC_ALLOC
) != 0
2256 && (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
2257 || elf_section_type (isection
) != SHT_NOTE
))
2259 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2260 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2264 /* Twiddle the input section flags so that it seems to
2265 elf.c:copy_private_bfd_data that section flags have not
2266 changed between input and output sections. This hack
2267 prevents wholesale rewriting of the program headers. */
2268 isection
->flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2272 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2274 if (osection
== NULL
)
2276 err
= _("failed to create output section");
2281 elf_section_type (osection
) = SHT_NOBITS
;
2283 size
= bfd_section_size (ibfd
, isection
);
2285 size
= (size
+ interleave
- 1) / interleave
;
2286 else if (extract_symbol
)
2288 if (! bfd_set_section_size (obfd
, osection
, size
))
2290 err
= _("failed to set size");
2294 vma
= bfd_section_vma (ibfd
, isection
);
2295 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2297 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2300 vma
+= change_section_address
;
2302 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2304 err
= _("failed to set vma");
2308 lma
= isection
->lma
;
2309 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2311 if (p
->change_lma
== CHANGE_MODIFY
)
2313 else if (p
->change_lma
== CHANGE_SET
)
2319 lma
+= change_section_address
;
2321 osection
->lma
= lma
;
2323 /* FIXME: This is probably not enough. If we change the LMA we
2324 may have to recompute the header for the file as well. */
2325 if (!bfd_set_section_alignment (obfd
,
2327 bfd_section_alignment (ibfd
, isection
)))
2329 err
= _("failed to set alignment");
2333 /* Copy merge entity size. */
2334 osection
->entsize
= isection
->entsize
;
2336 /* This used to be mangle_section; we do here to avoid using
2337 bfd_get_section_by_name since some formats allow multiple
2338 sections with the same name. */
2339 isection
->output_section
= osection
;
2340 isection
->output_offset
= 0;
2342 /* Do not copy backend data if --extract-symbol is passed; anything
2343 that needs to look at the section contents will fail. */
2347 if ((isection
->flags
& SEC_GROUP
) != 0)
2349 asymbol
*gsym
= group_signature (isection
);
2353 gsym
->flags
|= BSF_KEEP
;
2354 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2355 elf_group_id (isection
) = gsym
;
2359 /* Allow the BFD backend to copy any private data it understands
2360 from the input section to the output section. */
2361 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2363 err
= _("failed to copy private data");
2367 /* All went well. */
2372 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
2375 /* Copy the data of input section ISECTION of IBFD
2376 to an output section with the same name in OBFD.
2377 If stripping then don't copy any relocation info. */
2380 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2382 bfd
*obfd
= obfdarg
;
2383 struct section_list
*p
;
2391 /* If we have already failed earlier on,
2392 do not keep on generating complaints now. */
2396 if (is_strip_section (ibfd
, isection
))
2399 flags
= bfd_get_section_flags (ibfd
, isection
);
2400 if ((flags
& SEC_GROUP
) != 0)
2403 osection
= isection
->output_section
;
2404 size
= bfd_get_section_size (isection
);
2406 if (size
== 0 || osection
== 0)
2412 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2414 /* Core files do not need to be relocated. */
2415 if (bfd_get_format (obfd
) == bfd_core
)
2419 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2423 /* Do not complain if the target does not support relocations. */
2424 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2429 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2436 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2439 relpp
= xmalloc (relsize
);
2440 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2444 bfd_nonfatal_message (NULL
, ibfd
, isection
,
2445 _("relocation count is negative"));
2449 if (strip_symbols
== STRIP_ALL
)
2451 /* Remove relocations which are not in
2452 keep_strip_specific_list. */
2453 arelent
**temp_relpp
;
2454 long temp_relcount
= 0;
2457 temp_relpp
= xmalloc (relsize
);
2458 for (i
= 0; i
< relcount
; i
++)
2459 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2460 keep_specific_htab
))
2461 temp_relpp
[temp_relcount
++] = relpp
[i
];
2462 relcount
= temp_relcount
;
2467 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2472 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2473 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2475 void *memhunk
= xmalloc (size
);
2477 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2480 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2486 /* We don't handle leftover bytes (too many possible behaviors,
2487 and we don't know what the user wants). The section length
2488 must be a multiple of the number of bytes to swap. */
2489 if ((size
% reverse_bytes
) == 0)
2494 for (i
= 0; i
< size
; i
+= reverse_bytes
)
2495 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
2497 bfd_byte
*m
= (bfd_byte
*) memhunk
;
2500 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
2501 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
2505 /* User must pad the section up in order to do this. */
2506 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2507 bfd_section_name (ibfd
, isection
), reverse_bytes
);
2512 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2513 char *from
= (char *) memhunk
+ copy_byte
;
2515 char *end
= (char *) memhunk
+ size
;
2517 for (; from
< end
; from
+= interleave
)
2520 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2521 osection
->lma
/= interleave
;
2524 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2527 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2532 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2534 void *memhunk
= xmalloc (size
);
2536 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2537 flag--they can just remove the section entirely and add it
2538 back again. However, we do permit them to turn on the
2539 SEC_HAS_CONTENTS flag, and take it to mean that the section
2540 contents should be zeroed out. */
2542 memset (memhunk
, 0, size
);
2543 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2546 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2553 /* Get all the sections. This is used when --gap-fill or --pad-to is
2557 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2559 asection
***secppp
= secppparg
;
2561 **secppp
= osection
;
2565 /* Sort sections by VMA. This is called via qsort, and is used when
2566 --gap-fill or --pad-to is used. We force non loadable or empty
2567 sections to the front, where they are easier to ignore. */
2570 compare_section_lma (const void *arg1
, const void *arg2
)
2572 const asection
*const *sec1
= arg1
;
2573 const asection
*const *sec2
= arg2
;
2574 flagword flags1
, flags2
;
2576 /* Sort non loadable sections to the front. */
2577 flags1
= (*sec1
)->flags
;
2578 flags2
= (*sec2
)->flags
;
2579 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2580 || (flags1
& SEC_LOAD
) == 0)
2582 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2583 && (flags2
& SEC_LOAD
) != 0)
2588 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2589 || (flags2
& SEC_LOAD
) == 0)
2593 /* Sort sections by LMA. */
2594 if ((*sec1
)->lma
> (*sec2
)->lma
)
2596 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2599 /* Sort sections with the same LMA by size. */
2600 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2602 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2608 /* Mark all the symbols which will be used in output relocations with
2609 the BSF_KEEP flag so that those symbols will not be stripped.
2611 Ignore relocations which will not appear in the output file. */
2614 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2616 asymbol
**symbols
= symbolsarg
;
2621 /* Ignore an input section with no corresponding output section. */
2622 if (isection
->output_section
== NULL
)
2625 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2628 /* Do not complain if the target does not support relocations. */
2629 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2631 bfd_fatal (bfd_get_filename (ibfd
));
2637 relpp
= xmalloc (relsize
);
2638 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2640 bfd_fatal (bfd_get_filename (ibfd
));
2642 /* Examine each symbol used in a relocation. If it's not one of the
2643 special bfd section symbols, then mark it with BSF_KEEP. */
2644 for (i
= 0; i
< relcount
; i
++)
2646 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2647 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2648 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2649 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2656 /* Write out debugging information. */
2659 write_debugging_info (bfd
*obfd
, void *dhandle
,
2660 long *symcountp ATTRIBUTE_UNUSED
,
2661 asymbol
***symppp ATTRIBUTE_UNUSED
)
2663 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2664 return write_ieee_debugging_info (obfd
, dhandle
);
2666 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2667 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2669 bfd_byte
*syms
, *strings
;
2670 bfd_size_type symsize
, stringsize
;
2671 asection
*stabsec
, *stabstrsec
;
2674 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2679 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2680 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2681 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2683 || stabstrsec
== NULL
2684 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2685 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2686 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2687 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2689 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2690 _("can't create debugging section"));
2694 /* We can get away with setting the section contents now because
2695 the next thing the caller is going to do is copy over the
2696 real sections. We may someday have to split the contents
2697 setting out of this function. */
2698 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2699 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2702 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2703 _("can't set debugging section contents"));
2710 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2711 _("don't know how to write debugging information for %s"),
2712 bfd_get_target (obfd
));
2717 strip_main (int argc
, char *argv
[])
2719 char *input_target
= NULL
;
2720 char *output_target
= NULL
;
2721 bfd_boolean show_version
= FALSE
;
2722 bfd_boolean formats_info
= FALSE
;
2725 struct section_list
*p
;
2726 char *output_file
= NULL
;
2728 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2729 strip_options
, (int *) 0)) != EOF
)
2734 input_target
= optarg
;
2737 output_target
= optarg
;
2740 input_target
= output_target
= optarg
;
2743 p
= find_section_list (optarg
, TRUE
);
2745 sections_removed
= TRUE
;
2748 strip_symbols
= STRIP_ALL
;
2752 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2753 strip_symbols
= STRIP_DEBUG
;
2755 case OPTION_STRIP_UNNEEDED
:
2756 strip_symbols
= STRIP_UNNEEDED
;
2759 add_specific_symbol (optarg
, keep_specific_htab
);
2762 add_specific_symbol (optarg
, strip_specific_htab
);
2765 output_file
= optarg
;
2768 preserve_dates
= TRUE
;
2771 discard_locals
= LOCALS_ALL
;
2774 discard_locals
= LOCALS_START_L
;
2780 show_version
= TRUE
;
2782 case OPTION_FORMATS_INFO
:
2783 formats_info
= TRUE
;
2785 case OPTION_ONLY_KEEP_DEBUG
:
2786 strip_symbols
= STRIP_NONDEBUG
;
2788 case OPTION_KEEP_FILE_SYMBOLS
:
2789 keep_file_symbols
= 1;
2792 /* We've been given a long option. */
2799 strip_usage (stdout
, 0);
2801 strip_usage (stderr
, 1);
2812 print_version ("strip");
2814 /* Default is to strip all symbols. */
2815 if (strip_symbols
== STRIP_UNDEF
2816 && discard_locals
== LOCALS_UNDEF
2817 && htab_elements (strip_specific_htab
) == 0)
2818 strip_symbols
= STRIP_ALL
;
2820 if (output_target
== NULL
)
2821 output_target
= input_target
;
2825 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2826 strip_usage (stderr
, 1);
2828 for (; i
< argc
; i
++)
2830 int hold_status
= status
;
2831 struct stat statbuf
;
2834 if (get_file_size (argv
[i
]) < 1)
2841 /* No need to check the return value of stat().
2842 It has already been checked in get_file_size(). */
2843 stat (argv
[i
], &statbuf
);
2845 if (output_file
== NULL
|| strcmp (argv
[i
], output_file
) == 0)
2846 tmpname
= make_tempname (argv
[i
]);
2848 tmpname
= output_file
;
2850 if (tmpname
== NULL
)
2852 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
2853 _("could not create temporary file to hold stripped copy"));
2859 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2863 set_times (tmpname
, &statbuf
);
2864 if (output_file
!= tmpname
)
2865 smart_rename (tmpname
, output_file
? output_file
: argv
[i
],
2867 status
= hold_status
;
2870 unlink_if_ordinary (tmpname
);
2871 if (output_file
!= tmpname
)
2879 copy_main (int argc
, char *argv
[])
2881 char * binary_architecture
= NULL
;
2882 char *input_filename
= NULL
;
2883 char *output_filename
= NULL
;
2885 char *input_target
= NULL
;
2886 char *output_target
= NULL
;
2887 bfd_boolean show_version
= FALSE
;
2888 bfd_boolean change_warn
= TRUE
;
2889 bfd_boolean formats_info
= FALSE
;
2891 struct section_list
*p
;
2892 struct stat statbuf
;
2894 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2895 copy_options
, (int *) 0)) != EOF
)
2900 copy_byte
= atoi (optarg
);
2902 fatal (_("byte number must be non-negative"));
2906 binary_architecture
= optarg
;
2910 interleave
= atoi (optarg
);
2912 fatal (_("interleave must be positive"));
2916 case 's': /* "source" - 'I' is preferred */
2917 input_target
= optarg
;
2921 case 'd': /* "destination" - 'O' is preferred */
2922 output_target
= optarg
;
2926 input_target
= output_target
= optarg
;
2930 p
= find_section_list (optarg
, TRUE
);
2932 fatal (_("%s both copied and removed"), optarg
);
2934 sections_copied
= TRUE
;
2938 p
= find_section_list (optarg
, TRUE
);
2940 fatal (_("%s both copied and removed"), optarg
);
2942 sections_removed
= TRUE
;
2946 strip_symbols
= STRIP_ALL
;
2950 strip_symbols
= STRIP_DEBUG
;
2953 case OPTION_STRIP_UNNEEDED
:
2954 strip_symbols
= STRIP_UNNEEDED
;
2957 case OPTION_ONLY_KEEP_DEBUG
:
2958 strip_symbols
= STRIP_NONDEBUG
;
2961 case OPTION_KEEP_FILE_SYMBOLS
:
2962 keep_file_symbols
= 1;
2965 case OPTION_ADD_GNU_DEBUGLINK
:
2966 gnu_debuglink_filename
= optarg
;
2970 add_specific_symbol (optarg
, keep_specific_htab
);
2974 add_specific_symbol (optarg
, strip_specific_htab
);
2977 case OPTION_STRIP_UNNEEDED_SYMBOL
:
2978 add_specific_symbol (optarg
, strip_unneeded_htab
);
2982 add_specific_symbol (optarg
, localize_specific_htab
);
2985 case OPTION_GLOBALIZE_SYMBOL
:
2986 add_specific_symbol (optarg
, globalize_specific_htab
);
2990 add_specific_symbol (optarg
, keepglobal_specific_htab
);
2994 add_specific_symbol (optarg
, weaken_specific_htab
);
2998 preserve_dates
= TRUE
;
3006 discard_locals
= LOCALS_ALL
;
3010 discard_locals
= LOCALS_START_L
;
3018 show_version
= TRUE
;
3021 case OPTION_FORMATS_INFO
:
3022 formats_info
= TRUE
;
3029 case OPTION_ADD_SECTION
:
3033 struct section_add
*pa
;
3038 s
= strchr (optarg
, '=');
3041 fatal (_("bad format for %s"), "--add-section");
3043 size
= get_file_size (s
+ 1);
3050 pa
= xmalloc (sizeof (struct section_add
));
3053 name
= xmalloc (len
+ 1);
3054 strncpy (name
, optarg
, len
);
3058 pa
->filename
= s
+ 1;
3060 pa
->contents
= xmalloc (size
);
3062 f
= fopen (pa
->filename
, FOPEN_RB
);
3065 fatal (_("cannot open: %s: %s"),
3066 pa
->filename
, strerror (errno
));
3068 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
3070 fatal (_("%s: fread failed"), pa
->filename
);
3074 pa
->next
= add_sections
;
3079 case OPTION_CHANGE_START
:
3080 change_start
= parse_vma (optarg
, "--change-start");
3083 case OPTION_CHANGE_SECTION_ADDRESS
:
3084 case OPTION_CHANGE_SECTION_LMA
:
3085 case OPTION_CHANGE_SECTION_VMA
:
3090 char *option
= NULL
;
3092 enum change_action what
= CHANGE_IGNORE
;
3096 case OPTION_CHANGE_SECTION_ADDRESS
:
3097 option
= "--change-section-address";
3099 case OPTION_CHANGE_SECTION_LMA
:
3100 option
= "--change-section-lma";
3102 case OPTION_CHANGE_SECTION_VMA
:
3103 option
= "--change-section-vma";
3107 s
= strchr (optarg
, '=');
3110 s
= strchr (optarg
, '+');
3113 s
= strchr (optarg
, '-');
3115 fatal (_("bad format for %s"), option
);
3120 name
= xmalloc (len
+ 1);
3121 strncpy (name
, optarg
, len
);
3124 p
= find_section_list (name
, TRUE
);
3126 val
= parse_vma (s
+ 1, option
);
3130 case '=': what
= CHANGE_SET
; break;
3131 case '-': val
= - val
; /* Drop through. */
3132 case '+': what
= CHANGE_MODIFY
; break;
3137 case OPTION_CHANGE_SECTION_ADDRESS
:
3138 p
->change_vma
= what
;
3142 case OPTION_CHANGE_SECTION_LMA
:
3143 p
->change_lma
= what
;
3147 case OPTION_CHANGE_SECTION_VMA
:
3148 p
->change_vma
= what
;
3155 case OPTION_CHANGE_ADDRESSES
:
3156 change_section_address
= parse_vma (optarg
, "--change-addresses");
3157 change_start
= change_section_address
;
3160 case OPTION_CHANGE_WARNINGS
:
3164 case OPTION_CHANGE_LEADING_CHAR
:
3165 change_leading_char
= TRUE
;
3168 case OPTION_DEBUGGING
:
3169 convert_debugging
= TRUE
;
3172 case OPTION_GAP_FILL
:
3174 bfd_vma gap_fill_vma
;
3176 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
3177 gap_fill
= (bfd_byte
) gap_fill_vma
;
3178 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
3182 sprintf_vma (buff
, gap_fill_vma
);
3184 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3187 gap_fill_set
= TRUE
;
3191 case OPTION_NO_CHANGE_WARNINGS
:
3192 change_warn
= FALSE
;
3196 pad_to
= parse_vma (optarg
, "--pad-to");
3200 case OPTION_REMOVE_LEADING_CHAR
:
3201 remove_leading_char
= TRUE
;
3204 case OPTION_REDEFINE_SYM
:
3206 /* Push this redefinition onto redefine_symbol_list. */
3210 const char *nextarg
;
3211 char *source
, *target
;
3213 s
= strchr (optarg
, '=');
3215 fatal (_("bad format for %s"), "--redefine-sym");
3218 source
= xmalloc (len
+ 1);
3219 strncpy (source
, optarg
, len
);
3223 len
= strlen (nextarg
);
3224 target
= xmalloc (len
+ 1);
3225 strcpy (target
, nextarg
);
3227 redefine_list_append ("--redefine-sym", source
, target
);
3234 case OPTION_REDEFINE_SYMS
:
3235 add_redefine_syms_file (optarg
);
3238 case OPTION_SET_SECTION_FLAGS
:
3244 s
= strchr (optarg
, '=');
3246 fatal (_("bad format for %s"), "--set-section-flags");
3249 name
= xmalloc (len
+ 1);
3250 strncpy (name
, optarg
, len
);
3253 p
= find_section_list (name
, TRUE
);
3255 p
->set_flags
= TRUE
;
3256 p
->flags
= parse_flags (s
+ 1);
3260 case OPTION_RENAME_SECTION
:
3263 const char *eq
, *fl
;
3268 eq
= strchr (optarg
, '=');
3270 fatal (_("bad format for %s"), "--rename-section");
3274 fatal (_("bad format for %s"), "--rename-section");
3276 old_name
= xmalloc (len
+ 1);
3277 strncpy (old_name
, optarg
, len
);
3281 fl
= strchr (eq
, ',');
3284 flags
= parse_flags (fl
+ 1);
3294 fatal (_("bad format for %s"), "--rename-section");
3296 new_name
= xmalloc (len
+ 1);
3297 strncpy (new_name
, eq
, len
);
3300 add_section_rename (old_name
, new_name
, flags
);
3304 case OPTION_SET_START
:
3305 set_start
= parse_vma (optarg
, "--set-start");
3306 set_start_set
= TRUE
;
3309 case OPTION_SREC_LEN
:
3310 Chunk
= parse_vma (optarg
, "--srec-len");
3313 case OPTION_SREC_FORCES3
:
3317 case OPTION_STRIP_SYMBOLS
:
3318 add_specific_symbols (optarg
, strip_specific_htab
);
3321 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3322 add_specific_symbols (optarg
, strip_unneeded_htab
);
3325 case OPTION_KEEP_SYMBOLS
:
3326 add_specific_symbols (optarg
, keep_specific_htab
);
3329 case OPTION_LOCALIZE_HIDDEN
:
3330 localize_hidden
= TRUE
;
3333 case OPTION_LOCALIZE_SYMBOLS
:
3334 add_specific_symbols (optarg
, localize_specific_htab
);
3337 case OPTION_GLOBALIZE_SYMBOLS
:
3338 add_specific_symbols (optarg
, globalize_specific_htab
);
3341 case OPTION_KEEPGLOBAL_SYMBOLS
:
3342 add_specific_symbols (optarg
, keepglobal_specific_htab
);
3345 case OPTION_WEAKEN_SYMBOLS
:
3346 add_specific_symbols (optarg
, weaken_specific_htab
);
3349 case OPTION_ALT_MACH_CODE
:
3350 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3351 if (use_alt_mach_code
== 0)
3352 fatal (_("unable to parse alternative machine code"));
3355 case OPTION_PREFIX_SYMBOLS
:
3356 prefix_symbols_string
= optarg
;
3359 case OPTION_PREFIX_SECTIONS
:
3360 prefix_sections_string
= optarg
;
3363 case OPTION_PREFIX_ALLOC_SECTIONS
:
3364 prefix_alloc_sections_string
= optarg
;
3367 case OPTION_READONLY_TEXT
:
3368 bfd_flags_to_set
|= WP_TEXT
;
3369 bfd_flags_to_clear
&= ~WP_TEXT
;
3372 case OPTION_WRITABLE_TEXT
:
3373 bfd_flags_to_clear
|= WP_TEXT
;
3374 bfd_flags_to_set
&= ~WP_TEXT
;
3378 bfd_flags_to_set
|= D_PAGED
;
3379 bfd_flags_to_clear
&= ~D_PAGED
;
3383 bfd_flags_to_clear
|= D_PAGED
;
3384 bfd_flags_to_set
&= ~D_PAGED
;
3387 case OPTION_EXTRACT_SYMBOL
:
3388 extract_symbol
= TRUE
;
3391 case OPTION_REVERSE_BYTES
:
3393 int prev
= reverse_bytes
;
3395 reverse_bytes
= atoi (optarg
);
3396 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
3397 fatal (_("number of bytes to reverse must be positive and even"));
3399 if (prev
&& prev
!= reverse_bytes
)
3400 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3406 /* We've been given a long option. */
3411 copy_usage (stdout
, 0);
3414 copy_usage (stderr
, 1);
3425 print_version ("objcopy");
3427 if (copy_byte
>= interleave
)
3428 fatal (_("byte number must be less than interleave"));
3430 if (optind
== argc
|| optind
+ 2 < argc
)
3431 copy_usage (stderr
, 1);
3433 input_filename
= argv
[optind
];
3434 if (optind
+ 1 < argc
)
3435 output_filename
= argv
[optind
+ 1];
3437 /* Default is to strip no symbols. */
3438 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3439 strip_symbols
= STRIP_NONE
;
3441 if (output_target
== NULL
)
3442 output_target
= input_target
;
3444 if (binary_architecture
!= NULL
)
3446 if (input_target
&& strcmp (input_target
, "binary") == 0)
3448 const bfd_arch_info_type
* temp_arch_info
;
3450 temp_arch_info
= bfd_scan_arch (binary_architecture
);
3452 if (temp_arch_info
!= NULL
)
3454 bfd_external_binary_architecture
= temp_arch_info
->arch
;
3455 bfd_external_machine
= temp_arch_info
->mach
;
3458 fatal (_("architecture %s unknown"), binary_architecture
);
3462 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3463 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3468 if (stat (input_filename
, & statbuf
) < 0)
3469 fatal (_("warning: could not locate '%s'. System error message: %s"),
3470 input_filename
, strerror (errno
));
3472 /* If there is no destination file, or the source and destination files
3473 are the same, then create a temp and rename the result into the input. */
3474 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3475 tmpname
= make_tempname (input_filename
);
3477 tmpname
= output_filename
;
3479 if (tmpname
== NULL
)
3480 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3481 input_filename
, strerror (errno
));
3483 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3487 set_times (tmpname
, &statbuf
);
3488 if (tmpname
!= output_filename
)
3489 smart_rename (tmpname
, input_filename
, preserve_dates
);
3492 unlink_if_ordinary (tmpname
);
3496 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3500 if (p
->change_vma
!= CHANGE_IGNORE
)
3504 sprintf_vma (buff
, p
->vma_val
);
3506 /* xgettext:c-format */
3507 non_fatal (_("%s %s%c0x%s never used"),
3508 "--change-section-vma",
3510 p
->change_vma
== CHANGE_SET
? '=' : '+',
3514 if (p
->change_lma
!= CHANGE_IGNORE
)
3518 sprintf_vma (buff
, p
->lma_val
);
3520 /* xgettext:c-format */
3521 non_fatal (_("%s %s%c0x%s never used"),
3522 "--change-section-lma",
3524 p
->change_lma
== CHANGE_SET
? '=' : '+',
3535 main (int argc
, char *argv
[])
3537 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3538 setlocale (LC_MESSAGES
, "");
3540 #if defined (HAVE_SETLOCALE)
3541 setlocale (LC_CTYPE
, "");
3543 bindtextdomain (PACKAGE
, LOCALEDIR
);
3544 textdomain (PACKAGE
);
3546 program_name
= argv
[0];
3547 xmalloc_set_program_name (program_name
);
3549 START_PROGRESS (program_name
, 0);
3551 expandargv (&argc
, &argv
);
3553 strip_symbols
= STRIP_UNDEF
;
3554 discard_locals
= LOCALS_UNDEF
;
3557 set_default_bfd_target ();
3561 int i
= strlen (program_name
);
3562 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3563 /* Drop the .exe suffix, if any. */
3564 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3567 program_name
[i
] = '\0';
3570 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3573 create_symbol_htabs ();
3576 strip_main (argc
, argv
);
3578 copy_main (argc
, argv
);
3580 END_PROGRESS (program_name
);