1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
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"
35 #include "coff/internal.h"
38 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
39 header in generic PE code. */
40 #include "coff/i386.h"
43 static bfd_vma pe_file_alignment
= (bfd_vma
) -1;
44 static bfd_vma pe_heap_commit
= (bfd_vma
) -1;
45 static bfd_vma pe_heap_reserve
= (bfd_vma
) -1;
46 static bfd_vma pe_image_base
= (bfd_vma
) -1;
47 static bfd_vma pe_section_alignment
= (bfd_vma
) -1;
48 static bfd_vma pe_stack_commit
= (bfd_vma
) -1;
49 static bfd_vma pe_stack_reserve
= (bfd_vma
) -1;
50 static short pe_subsystem
= -1;
51 static short pe_major_subsystem_version
= -1;
52 static short pe_minor_subsystem_version
= -1;
54 struct is_specified_symbol_predicate_data
60 /* A list to support redefine_sym. */
65 struct redefine_node
*next
;
68 typedef struct section_rename
70 const char * old_name
;
71 const char * new_name
;
73 struct section_rename
* next
;
77 /* List of sections to be renamed. */
78 static section_rename
*section_rename_list
;
80 static asymbol
**isympp
= NULL
; /* Input symbols. */
81 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
83 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
84 static int copy_byte
= -1;
85 static int interleave
= 4;
87 static bfd_boolean verbose
; /* Print file and target names. */
88 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
89 static int status
= 0; /* Exit status. */
94 STRIP_NONE
, /* Don't strip. */
95 STRIP_DEBUG
, /* Strip all debugger symbols. */
96 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
97 STRIP_NONDEBUG
, /* Strip everything but debug info. */
98 STRIP_ALL
/* Strip all symbols. */
101 /* Which symbols to remove. */
102 static enum strip_action strip_symbols
;
107 LOCALS_START_L
, /* Discard locals starting with L. */
108 LOCALS_ALL
/* Discard all locals. */
111 /* Which local symbols to remove. Overrides STRIP_ALL. */
112 static enum locals_action discard_locals
;
114 /* What kind of change to perform. */
122 /* Structure used to hold lists of sections and actions to take. */
125 struct section_list
* next
; /* Next section to change. */
126 const char * name
; /* Section name. */
127 bfd_boolean used
; /* Whether this entry was used. */
128 bfd_boolean remove
; /* Whether to remove this section. */
129 bfd_boolean copy
; /* Whether to copy this section. */
130 enum change_action change_vma
;/* Whether to change or set VMA. */
131 bfd_vma vma_val
; /* Amount to change by or set to. */
132 enum change_action change_lma
;/* Whether to change or set LMA. */
133 bfd_vma lma_val
; /* Amount to change by or set to. */
134 bfd_boolean set_flags
; /* Whether to set the section flags. */
135 flagword flags
; /* What to set the section flags to. */
138 static struct section_list
*change_sections
;
140 /* TRUE if some sections are to be removed. */
141 static bfd_boolean sections_removed
;
143 /* TRUE if only some sections are to be copied. */
144 static bfd_boolean sections_copied
;
146 /* Changes to the start address. */
147 static bfd_vma change_start
= 0;
148 static bfd_boolean set_start_set
= FALSE
;
149 static bfd_vma set_start
;
151 /* Changes to section addresses. */
152 static bfd_vma change_section_address
= 0;
154 /* Filling gaps between sections. */
155 static bfd_boolean gap_fill_set
= FALSE
;
156 static bfd_byte gap_fill
= 0;
158 /* Pad to a given address. */
159 static bfd_boolean pad_to_set
= FALSE
;
160 static bfd_vma pad_to
;
162 /* Use alternative machine code? */
163 static unsigned long use_alt_mach_code
= 0;
165 /* Output BFD flags user wants to set or clear */
166 static flagword bfd_flags_to_set
;
167 static flagword bfd_flags_to_clear
;
169 /* List of sections to add. */
172 /* Next section to add. */
173 struct section_add
*next
;
174 /* Name of section to add. */
176 /* Name of file holding section contents. */
177 const char *filename
;
180 /* Contents of file. */
182 /* BFD section, after it has been added. */
186 /* List of sections to add to the output BFD. */
187 static struct section_add
*add_sections
;
189 /* If non-NULL the argument to --add-gnu-debuglink.
190 This should be the filename to store in the .gnu_debuglink section. */
191 static const char * gnu_debuglink_filename
= NULL
;
193 /* Whether to convert debugging information. */
194 static bfd_boolean convert_debugging
= FALSE
;
196 /* Whether to change the leading character in symbol names. */
197 static bfd_boolean change_leading_char
= FALSE
;
199 /* Whether to remove the leading character from global symbol names. */
200 static bfd_boolean remove_leading_char
= FALSE
;
202 /* Whether to permit wildcard in symbol comparison. */
203 static bfd_boolean wildcard
= FALSE
;
205 /* True if --localize-hidden is in effect. */
206 static bfd_boolean localize_hidden
= FALSE
;
208 /* List of symbols to strip, keep, localize, keep-global, weaken,
210 static htab_t strip_specific_htab
= NULL
;
211 static htab_t strip_unneeded_htab
= NULL
;
212 static htab_t keep_specific_htab
= NULL
;
213 static htab_t localize_specific_htab
= NULL
;
214 static htab_t globalize_specific_htab
= NULL
;
215 static htab_t keepglobal_specific_htab
= NULL
;
216 static htab_t weaken_specific_htab
= NULL
;
217 static struct redefine_node
*redefine_sym_list
= NULL
;
219 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
220 static bfd_boolean weaken
= FALSE
;
222 /* If this is TRUE, we retain BSF_FILE symbols. */
223 static bfd_boolean keep_file_symbols
= FALSE
;
225 /* Prefix symbols/sections. */
226 static char *prefix_symbols_string
= 0;
227 static char *prefix_sections_string
= 0;
228 static char *prefix_alloc_sections_string
= 0;
230 /* True if --extract-symbol was passed on the command line. */
231 static bfd_boolean extract_symbol
= FALSE
;
233 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
234 of <reverse_bytes> bytes within each output section. */
235 static int reverse_bytes
= 0;
237 /* For Coff objects, we may want to allow or disallow long section names,
238 or preserve them where found in the inputs. Debug info relies on them. */
239 enum long_section_name_handling
246 /* The default long section handling mode is to preserve them.
247 This is also the only behaviour for 'strip'. */
248 static enum long_section_name_handling long_section_names
= KEEP
;
250 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
251 enum command_line_switch
253 OPTION_ADD_SECTION
=150,
254 OPTION_CHANGE_ADDRESSES
,
255 OPTION_CHANGE_LEADING_CHAR
,
257 OPTION_CHANGE_SECTION_ADDRESS
,
258 OPTION_CHANGE_SECTION_LMA
,
259 OPTION_CHANGE_SECTION_VMA
,
260 OPTION_CHANGE_WARNINGS
,
263 OPTION_NO_CHANGE_WARNINGS
,
265 OPTION_REMOVE_LEADING_CHAR
,
266 OPTION_SET_SECTION_FLAGS
,
268 OPTION_STRIP_UNNEEDED
,
271 OPTION_REDEFINE_SYMS
,
274 OPTION_STRIP_SYMBOLS
,
275 OPTION_STRIP_UNNEEDED_SYMBOL
,
276 OPTION_STRIP_UNNEEDED_SYMBOLS
,
278 OPTION_LOCALIZE_HIDDEN
,
279 OPTION_LOCALIZE_SYMBOLS
,
280 OPTION_LONG_SECTION_NAMES
,
281 OPTION_GLOBALIZE_SYMBOL
,
282 OPTION_GLOBALIZE_SYMBOLS
,
283 OPTION_KEEPGLOBAL_SYMBOLS
,
284 OPTION_WEAKEN_SYMBOLS
,
285 OPTION_RENAME_SECTION
,
286 OPTION_ALT_MACH_CODE
,
287 OPTION_PREFIX_SYMBOLS
,
288 OPTION_PREFIX_SECTIONS
,
289 OPTION_PREFIX_ALLOC_SECTIONS
,
291 OPTION_ADD_GNU_DEBUGLINK
,
292 OPTION_ONLY_KEEP_DEBUG
,
293 OPTION_KEEP_FILE_SYMBOLS
,
294 OPTION_READONLY_TEXT
,
295 OPTION_WRITABLE_TEXT
,
298 OPTION_EXTRACT_SYMBOL
,
299 OPTION_REVERSE_BYTES
,
300 OPTION_FILE_ALIGNMENT
,
303 OPTION_SECTION_ALIGNMENT
,
308 /* Options to handle if running as "strip". */
310 static struct option strip_options
[] =
312 {"discard-all", no_argument
, 0, 'x'},
313 {"discard-locals", no_argument
, 0, 'X'},
314 {"format", required_argument
, 0, 'F'}, /* Obsolete */
315 {"help", no_argument
, 0, 'h'},
316 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
317 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
318 {"input-target", required_argument
, 0, 'I'},
319 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
320 {"keep-symbol", required_argument
, 0, 'K'},
321 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
322 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
323 {"output-target", required_argument
, 0, 'O'},
324 {"output-file", required_argument
, 0, 'o'},
325 {"preserve-dates", no_argument
, 0, 'p'},
326 {"remove-section", required_argument
, 0, 'R'},
327 {"strip-all", no_argument
, 0, 's'},
328 {"strip-debug", no_argument
, 0, 'S'},
329 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
330 {"strip-symbol", required_argument
, 0, 'N'},
331 {"target", required_argument
, 0, 'F'},
332 {"verbose", no_argument
, 0, 'v'},
333 {"version", no_argument
, 0, 'V'},
334 {"wildcard", no_argument
, 0, 'w'},
335 {0, no_argument
, 0, 0}
338 /* Options to handle if running as "objcopy". */
340 static struct option copy_options
[] =
342 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
343 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
344 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
345 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
346 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
347 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
348 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
349 {"binary-architecture", required_argument
, 0, 'B'},
350 {"byte", required_argument
, 0, 'b'},
351 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
352 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
353 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
354 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
355 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
356 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
357 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
358 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
359 {"discard-all", no_argument
, 0, 'x'},
360 {"discard-locals", no_argument
, 0, 'X'},
361 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
362 {"format", required_argument
, 0, 'F'}, /* Obsolete */
363 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
364 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
365 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
366 {"help", no_argument
, 0, 'h'},
367 {"impure", no_argument
, 0, OPTION_IMPURE
},
368 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
369 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
370 {"input-target", required_argument
, 0, 'I'},
371 {"interleave", required_argument
, 0, 'i'},
372 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
373 {"keep-global-symbol", required_argument
, 0, 'G'},
374 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
375 {"keep-symbol", required_argument
, 0, 'K'},
376 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
377 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
378 {"localize-symbol", required_argument
, 0, 'L'},
379 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
380 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
381 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
382 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
383 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
384 {"only-section", required_argument
, 0, 'j'},
385 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
386 {"output-target", required_argument
, 0, 'O'},
387 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
388 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
389 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
390 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
391 {"preserve-dates", no_argument
, 0, 'p'},
392 {"pure", no_argument
, 0, OPTION_PURE
},
393 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
394 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
395 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
396 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
397 {"remove-section", required_argument
, 0, 'R'},
398 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
399 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
400 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
401 {"set-start", required_argument
, 0, OPTION_SET_START
},
402 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
403 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
404 {"strip-all", no_argument
, 0, 'S'},
405 {"strip-debug", no_argument
, 0, 'g'},
406 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
407 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
408 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
409 {"strip-symbol", required_argument
, 0, 'N'},
410 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
411 {"target", required_argument
, 0, 'F'},
412 {"verbose", no_argument
, 0, 'v'},
413 {"version", no_argument
, 0, 'V'},
414 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
415 {"weaken-symbol", required_argument
, 0, 'W'},
416 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
417 {"wildcard", no_argument
, 0, 'w'},
418 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
419 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
420 {"heap", required_argument
, 0, OPTION_HEAP
},
421 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
422 {"section-alignment", required_argument
, 0, OPTION_SECTION_ALIGNMENT
},
423 {"stack", required_argument
, 0, OPTION_STACK
},
424 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
425 {0, no_argument
, 0, 0}
429 extern char *program_name
;
431 /* This flag distinguishes between strip and objcopy:
432 1 means this is 'strip'; 0 means this is 'objcopy'.
433 -1 means if we should use argv[0] to decide. */
436 /* The maximum length of an S record. This variable is declared in srec.c
437 and can be modified by the --srec-len parameter. */
438 extern unsigned int Chunk
;
440 /* Restrict the generation of Srecords to type S3 only.
441 This variable is declare in bfd/srec.c and can be toggled
442 on by the --srec-forceS3 command line switch. */
443 extern bfd_boolean S3Forced
;
445 /* Defined in bfd/binary.c. Used to set architecture and machine of input
447 extern enum bfd_architecture bfd_external_binary_architecture
;
448 extern unsigned long bfd_external_machine
;
450 /* Forward declarations. */
451 static void setup_section (bfd
*, asection
*, void *);
452 static void setup_bfd_headers (bfd
*, bfd
*);
453 static void copy_section (bfd
*, asection
*, void *);
454 static void get_sections (bfd
*, asection
*, void *);
455 static int compare_section_lma (const void *, const void *);
456 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
457 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
458 static const char *lookup_sym_redefinition (const char *);
461 copy_usage (FILE *stream
, int exit_status
)
463 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
464 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
465 fprintf (stream
, _(" The options are:\n"));
466 fprintf (stream
, _("\
467 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
468 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
469 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
470 -F --target <bfdname> Set both input and output format to <bfdname>\n\
471 --debugging Convert debugging information, if possible\n\
472 -p --preserve-dates Copy modified/access timestamps to the output\n\
473 -j --only-section <name> Only copy section <name> into the output\n\
474 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
475 -R --remove-section <name> Remove section <name> from the output\n\
476 -S --strip-all Remove all symbol and relocation information\n\
477 -g --strip-debug Remove all debugging symbols & sections\n\
478 --strip-unneeded Remove all symbols not needed by relocations\n\
479 -N --strip-symbol <name> Do not copy symbol <name>\n\
480 --strip-unneeded-symbol <name>\n\
481 Do not copy symbol <name> unless needed by\n\
483 --only-keep-debug Strip everything but the debug information\n\
484 --extract-symbol Remove section contents but keep symbols\n\
485 -K --keep-symbol <name> Do not strip symbol <name>\n\
486 --keep-file-symbols Do not strip file symbol(s)\n\
487 --localize-hidden Turn all ELF hidden symbols into locals\n\
488 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
489 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
490 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
491 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
492 --weaken Force all global symbols to be marked as weak\n\
493 -w --wildcard Permit wildcard in symbol comparison\n\
494 -x --discard-all Remove all non-global symbols\n\
495 -X --discard-locals Remove any compiler-generated symbols\n\
496 -i --interleave <number> Only copy one out of every <number> bytes\n\
497 -b --byte <num> Select byte <num> in every interleaved block\n\
498 --gap-fill <val> Fill gaps between sections with <val>\n\
499 --pad-to <addr> Pad the last section up to address <addr>\n\
500 --set-start <addr> Set the start address to <addr>\n\
501 {--change-start|--adjust-start} <incr>\n\
502 Add <incr> to the start address\n\
503 {--change-addresses|--adjust-vma} <incr>\n\
504 Add <incr> to LMA, VMA and start addresses\n\
505 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
506 Change LMA and VMA of section <name> by <val>\n\
507 --change-section-lma <name>{=|+|-}<val>\n\
508 Change the LMA of section <name> by <val>\n\
509 --change-section-vma <name>{=|+|-}<val>\n\
510 Change the VMA of section <name> by <val>\n\
511 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
512 Warn if a named section does not exist\n\
513 --set-section-flags <name>=<flags>\n\
514 Set section <name>'s properties to <flags>\n\
515 --add-section <name>=<file> Add section <name> found in <file> to output\n\
516 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
517 --long-section-names {enable|disable|keep}\n\
518 Handle long section names in Coff objects.\n\
519 --change-leading-char Force output format's leading character style\n\
520 --remove-leading-char Remove leading character from global symbols\n\
521 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
522 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
523 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
525 --srec-len <number> Restrict the length of generated Srecords\n\
526 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
527 --strip-symbols <file> -N for all symbols listed in <file>\n\
528 --strip-unneeded-symbols <file>\n\
529 --strip-unneeded-symbol for all symbols listed\n\
531 --keep-symbols <file> -K for all symbols listed in <file>\n\
532 --localize-symbols <file> -L for all symbols listed in <file>\n\
533 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
534 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
535 --weaken-symbols <file> -W for all symbols listed in <file>\n\
536 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
537 --writable-text Mark the output text as writable\n\
538 --readonly-text Make the output text write protected\n\
539 --pure Mark the output file as demand paged\n\
540 --impure Mark the output file as impure\n\
541 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
542 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
543 --prefix-alloc-sections <prefix>\n\
544 Add <prefix> to start of every allocatable\n\
546 --file-alignment <num> Set PE file alignment to <num>\n\
547 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
549 --image-base <address> Set PE image base to <address>\n\
550 --section-alignment <num> Set PE section alignment to <num>\n\
551 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
553 --subsystem <name>[:<version>]\n\
554 Set PE subsystem to <name> [& <version>]\n]\
555 -v --verbose List all object files modified\n\
556 @<file> Read options from <file>\n\
557 -V --version Display this program's version number\n\
558 -h --help Display this output\n\
559 --info List object formats & architectures supported\n\
561 list_supported_targets (program_name
, stream
);
562 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
563 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
568 strip_usage (FILE *stream
, int exit_status
)
570 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
571 fprintf (stream
, _(" Removes symbols and sections from files\n"));
572 fprintf (stream
, _(" The options are:\n"));
573 fprintf (stream
, _("\
574 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
575 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
576 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
577 -p --preserve-dates Copy modified/access timestamps to the output\n\
578 -R --remove-section=<name> Remove section <name> from the output\n\
579 -s --strip-all Remove all symbol and relocation information\n\
580 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
581 --strip-unneeded Remove all symbols not needed by relocations\n\
582 --only-keep-debug Strip everything but the debug information\n\
583 -N --strip-symbol=<name> Do not copy symbol <name>\n\
584 -K --keep-symbol=<name> Do not strip symbol <name>\n\
585 --keep-file-symbols Do not strip file symbol(s)\n\
586 -w --wildcard Permit wildcard in symbol comparison\n\
587 -x --discard-all Remove all non-global symbols\n\
588 -X --discard-locals Remove any compiler-generated symbols\n\
589 -v --verbose List all object files modified\n\
590 -V --version Display this program's version number\n\
591 -h --help Display this output\n\
592 --info List object formats & architectures supported\n\
593 -o <file> Place stripped output into <file>\n\
596 list_supported_targets (program_name
, stream
);
597 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
598 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
602 /* Parse section flags into a flagword, with a fatal error if the
603 string can't be parsed. */
606 parse_flags (const char *s
)
616 snext
= strchr (s
, ',');
626 #define PARSE_FLAG(fname,fval) \
627 else if (strncasecmp (fname, s, len) == 0) ret |= fval
628 PARSE_FLAG ("alloc", SEC_ALLOC
);
629 PARSE_FLAG ("load", SEC_LOAD
);
630 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
631 PARSE_FLAG ("readonly", SEC_READONLY
);
632 PARSE_FLAG ("debug", SEC_DEBUGGING
);
633 PARSE_FLAG ("code", SEC_CODE
);
634 PARSE_FLAG ("data", SEC_DATA
);
635 PARSE_FLAG ("rom", SEC_ROM
);
636 PARSE_FLAG ("share", SEC_COFF_SHARED
);
637 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
643 copy
= xmalloc (len
+ 1);
644 strncpy (copy
, s
, len
);
646 non_fatal (_("unrecognized section flag `%s'"), copy
);
647 fatal (_("supported flags: %s"),
648 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
658 /* Find and optionally add an entry in the change_sections list. */
660 static struct section_list
*
661 find_section_list (const char *name
, bfd_boolean add
)
663 struct section_list
*p
;
665 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
666 if (strcmp (p
->name
, name
) == 0)
672 p
= xmalloc (sizeof (struct section_list
));
677 p
->change_vma
= CHANGE_IGNORE
;
678 p
->change_lma
= CHANGE_IGNORE
;
681 p
->set_flags
= FALSE
;
684 p
->next
= change_sections
;
690 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
693 eq_string (const void *s1
, const void *s2
)
695 return strcmp (s1
, s2
) == 0;
699 create_symbol_htab (void)
701 return htab_create_alloc (16, htab_hash_string
, eq_string
, NULL
, xcalloc
, free
);
705 create_symbol_htabs (void)
707 strip_specific_htab
= create_symbol_htab ();
708 strip_unneeded_htab
= create_symbol_htab ();
709 keep_specific_htab
= create_symbol_htab ();
710 localize_specific_htab
= create_symbol_htab ();
711 globalize_specific_htab
= create_symbol_htab ();
712 keepglobal_specific_htab
= create_symbol_htab ();
713 weaken_specific_htab
= create_symbol_htab ();
716 /* Add a symbol to strip_specific_list. */
719 add_specific_symbol (const char *name
, htab_t htab
)
721 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
724 /* Add symbols listed in `filename' to strip_specific_list. */
726 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
727 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
730 add_specific_symbols (const char *filename
, htab_t htab
)
736 unsigned int line_count
;
738 size
= get_file_size (filename
);
745 buffer
= xmalloc (size
+ 2);
746 f
= fopen (filename
, FOPEN_RT
);
748 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
750 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
751 fatal (_("%s: fread failed"), filename
);
754 buffer
[size
] = '\n';
755 buffer
[size
+ 1] = '\0';
759 for (line
= buffer
; * line
!= '\0'; line
++)
764 int finished
= FALSE
;
766 for (eol
= line
;; eol
++)
772 /* Cope with \n\r. */
780 /* Cope with \r\n. */
791 /* Line comment, Terminate the line here, in case a
792 name is present and then allow the rest of the
793 loop to find the real end of the line. */
805 /* A name may now exist somewhere between 'line' and 'eol'.
806 Strip off leading whitespace and trailing whitespace,
807 then add it to the list. */
808 for (name
= line
; IS_WHITESPACE (* name
); name
++)
810 for (name_end
= name
;
811 (! IS_WHITESPACE (* name_end
))
812 && (! IS_LINE_TERMINATOR (* name_end
));
816 if (! IS_LINE_TERMINATOR (* name_end
))
820 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
823 if (! IS_LINE_TERMINATOR (* extra
))
824 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
825 filename
, line_count
);
831 add_specific_symbol (name
, htab
);
833 /* Advance line pointer to end of line. The 'eol ++' in the for
834 loop above will then advance us to the start of the next line. */
840 /* See whether a symbol should be stripped or kept
841 based on strip_specific_list and keep_symbols. */
844 is_specified_symbol_predicate (void **slot
, void *data
)
846 struct is_specified_symbol_predicate_data
*d
= data
;
847 const char *slot_name
= *slot
;
849 if (*slot_name
!= '!')
851 if (! fnmatch (slot_name
, d
->name
, 0))
854 /* Stop traversal. */
860 if (fnmatch (slot_name
+ 1, d
->name
, 0))
863 /* Stop traversal. */
868 /* Continue traversal. */
873 is_specified_symbol (const char *name
, htab_t htab
)
877 struct is_specified_symbol_predicate_data data
;
882 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
887 return htab_find (htab
, name
) != NULL
;
890 /* Return a pointer to the symbol used as a signature for GROUP. */
893 group_signature (asection
*group
)
895 bfd
*abfd
= group
->owner
;
896 Elf_Internal_Shdr
*ghdr
;
898 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
901 ghdr
= &elf_section_data (group
)->this_hdr
;
902 if (ghdr
->sh_link
< elf_numsections (abfd
))
904 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
905 Elf_Internal_Shdr
*symhdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
907 if (symhdr
->sh_type
== SHT_SYMTAB
908 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
909 return isympp
[ghdr
->sh_info
- 1];
914 /* See if a section is being removed. */
917 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
919 if (sections_removed
|| sections_copied
)
921 struct section_list
*p
;
923 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
925 if (sections_removed
&& p
!= NULL
&& p
->remove
)
927 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
931 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
933 if (strip_symbols
== STRIP_DEBUG
934 || strip_symbols
== STRIP_UNNEEDED
935 || strip_symbols
== STRIP_ALL
936 || discard_locals
== LOCALS_ALL
937 || convert_debugging
)
940 if (strip_symbols
== STRIP_NONDEBUG
)
944 if ((bfd_get_section_flags (abfd
, sec
) & SEC_GROUP
) != 0)
950 Group sections look like debugging sections but they are not.
951 (They have a non-zero size but they are not ALLOCated). */
952 if (strip_symbols
== STRIP_NONDEBUG
)
956 If we are going to strip the group signature symbol, then
957 strip the group section too. */
958 gsym
= group_signature (sec
);
963 if ((strip_symbols
== STRIP_ALL
964 && !is_specified_symbol (gname
, keep_specific_htab
))
965 || is_specified_symbol (gname
, strip_specific_htab
))
972 /* Return true if SYM is a hidden symbol. */
975 is_hidden_symbol (asymbol
*sym
)
977 elf_symbol_type
*elf_sym
;
979 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
981 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
990 /* Choose which symbol entries to copy; put the result in OSYMS.
991 We don't copy in place, because that confuses the relocs.
992 Return the number of symbols to print. */
995 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
996 asymbol
**isyms
, long symcount
)
998 asymbol
**from
= isyms
, **to
= osyms
;
999 long src_count
= 0, dst_count
= 0;
1000 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
1002 for (; src_count
< symcount
; src_count
++)
1004 asymbol
*sym
= from
[src_count
];
1005 flagword flags
= sym
->flags
;
1006 char *name
= (char *) bfd_asymbol_name (sym
);
1008 bfd_boolean used_in_reloc
= FALSE
;
1009 bfd_boolean undefined
;
1010 bfd_boolean rem_leading_char
;
1011 bfd_boolean add_leading_char
;
1013 undefined
= bfd_is_und_section (bfd_get_section (sym
));
1015 if (redefine_sym_list
)
1017 char *old_name
, *new_name
;
1019 old_name
= (char *) bfd_asymbol_name (sym
);
1020 new_name
= (char *) lookup_sym_redefinition (old_name
);
1021 bfd_asymbol_name (sym
) = new_name
;
1025 /* Check if we will remove the current leading character. */
1027 (name
[0] == bfd_get_symbol_leading_char (abfd
))
1028 && (change_leading_char
1029 || (remove_leading_char
1030 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1032 || bfd_is_com_section (bfd_get_section (sym
)))));
1034 /* Check if we will add a new leading character. */
1037 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1038 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1039 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1041 /* Short circuit for change_leading_char if we can do it in-place. */
1042 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1044 name
[0] = bfd_get_symbol_leading_char (obfd
);
1045 bfd_asymbol_name (sym
) = name
;
1046 rem_leading_char
= FALSE
;
1047 add_leading_char
= FALSE
;
1050 /* Remove leading char. */
1051 if (rem_leading_char
)
1052 bfd_asymbol_name (sym
) = ++name
;
1054 /* Add new leading char and/or prefix. */
1055 if (add_leading_char
|| prefix_symbols_string
)
1059 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
1060 + strlen (name
) + 1);
1061 if (add_leading_char
)
1062 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1064 if (prefix_symbols_string
)
1066 strcpy (ptr
, prefix_symbols_string
);
1067 ptr
+= strlen (prefix_symbols_string
);
1071 bfd_asymbol_name (sym
) = n
;
1075 if (strip_symbols
== STRIP_ALL
)
1077 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1078 || ((flags
& BSF_SECTION_SYM
) != 0
1079 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
1083 used_in_reloc
= TRUE
;
1085 else if (relocatable
/* Relocatable file. */
1086 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1087 || bfd_is_com_section (bfd_get_section (sym
))))
1089 else if (bfd_decode_symclass (sym
) == 'I')
1090 /* Global symbols in $idata sections need to be retained
1091 even if relocatable is FALSE. External users of the
1092 library containing the $idata section may reference these
1095 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1096 || (flags
& BSF_WEAK
) != 0
1098 || bfd_is_com_section (bfd_get_section (sym
)))
1099 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1100 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1101 keep
= (strip_symbols
!= STRIP_DEBUG
1102 && strip_symbols
!= STRIP_UNNEEDED
1103 && ! convert_debugging
);
1104 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
1105 /* COMDAT sections store special information in local
1106 symbols, so we cannot risk stripping any of them. */
1108 else /* Local symbol. */
1109 keep
= (strip_symbols
!= STRIP_UNNEEDED
1110 && (discard_locals
!= LOCALS_ALL
1111 && (discard_locals
!= LOCALS_START_L
1112 || ! bfd_is_local_label (abfd
, sym
))));
1114 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1116 /* There are multiple ways to set 'keep' above, but if it
1117 was the relocatable symbol case, then that's an error. */
1120 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1128 && !(flags
& BSF_KEEP
)
1129 && is_specified_symbol (name
, strip_unneeded_htab
))
1133 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1134 || is_specified_symbol (name
, keep_specific_htab
)))
1137 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
1142 if ((flags
& BSF_GLOBAL
) != 0
1143 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1145 sym
->flags
&= ~ BSF_GLOBAL
;
1146 sym
->flags
|= BSF_WEAK
;
1150 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1151 && (is_specified_symbol (name
, localize_specific_htab
)
1152 || (htab_elements (keepglobal_specific_htab
) != 0
1153 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1154 || (localize_hidden
&& is_hidden_symbol (sym
))))
1156 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1157 sym
->flags
|= BSF_LOCAL
;
1161 && (flags
& BSF_LOCAL
)
1162 && is_specified_symbol (name
, globalize_specific_htab
))
1164 sym
->flags
&= ~ BSF_LOCAL
;
1165 sym
->flags
|= BSF_GLOBAL
;
1168 to
[dst_count
++] = sym
;
1172 to
[dst_count
] = NULL
;
1177 /* Find the redefined name of symbol SOURCE. */
1180 lookup_sym_redefinition (const char *source
)
1182 struct redefine_node
*list
;
1184 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1185 if (strcmp (source
, list
->source
) == 0)
1186 return list
->target
;
1191 /* Add a node to a symbol redefine list. */
1194 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1196 struct redefine_node
**p
;
1197 struct redefine_node
*list
;
1198 struct redefine_node
*new_node
;
1200 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1202 if (strcmp (source
, list
->source
) == 0)
1203 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1206 if (strcmp (target
, list
->target
) == 0)
1207 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1211 new_node
= xmalloc (sizeof (struct redefine_node
));
1213 new_node
->source
= strdup (source
);
1214 new_node
->target
= strdup (target
);
1215 new_node
->next
= NULL
;
1220 /* Handle the --redefine-syms option. Read lines containing "old new"
1221 from the file, and add them to the symbol redefine list. */
1224 add_redefine_syms_file (const char *filename
)
1233 file
= fopen (filename
, "r");
1235 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1236 filename
, strerror (errno
));
1239 buf
= xmalloc (bufsize
);
1247 /* Collect the input symbol name. */
1248 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1256 buf
= xrealloc (buf
, bufsize
);
1264 /* Eat white space between the symbol names. */
1265 while (IS_WHITESPACE (c
))
1267 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1272 /* Collect the output symbol name. */
1274 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1282 buf
= xrealloc (buf
, bufsize
);
1290 /* Eat white space at end of line. */
1291 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1296 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1297 || c
== '\n' || c
== EOF
)
1300 /* Append the redefinition to the list. */
1302 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1313 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1315 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1316 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1319 /* Eat the rest of the line and finish it. */
1320 while (c
!= '\n' && c
!= EOF
)
1326 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1331 /* Copy unkown object file IBFD onto OBFD.
1332 Returns TRUE upon success, FALSE otherwise. */
1335 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1343 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1345 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1352 non_fatal (_("stat returns negative size for `%s'"),
1353 bfd_get_archive_filename (ibfd
));
1357 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1359 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1364 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1365 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1367 cbuf
= xmalloc (BUFSIZE
);
1369 while (ncopied
< size
)
1371 tocopy
= size
- ncopied
;
1372 if (tocopy
> BUFSIZE
)
1375 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1376 != (bfd_size_type
) tocopy
)
1378 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1383 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1384 != (bfd_size_type
) tocopy
)
1386 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1394 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1399 /* Copy object file IBFD onto OBFD.
1400 Returns TRUE upon success, FALSE otherwise. */
1403 copy_object (bfd
*ibfd
, bfd
*obfd
)
1407 asection
**osections
= NULL
;
1408 asection
*gnu_debuglink_section
= NULL
;
1409 bfd_size_type
*gaps
= NULL
;
1410 bfd_size_type max_gap
= 0;
1413 enum bfd_architecture iarch
;
1416 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1417 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1418 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1419 fatal (_("Unable to change endianness of input file(s)"));
1421 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1423 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1428 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1429 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1430 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1439 start
= bfd_get_start_address (ibfd
);
1440 start
+= change_start
;
1443 /* Neither the start address nor the flags
1444 need to be set for a core file. */
1445 if (bfd_get_format (obfd
) != bfd_core
)
1449 flags
= bfd_get_file_flags (ibfd
);
1450 flags
|= bfd_flags_to_set
;
1451 flags
&= ~bfd_flags_to_clear
;
1452 flags
&= bfd_applicable_file_flags (obfd
);
1454 if (strip_symbols
== STRIP_ALL
)
1455 flags
&= ~HAS_RELOC
;
1457 if (!bfd_set_start_address (obfd
, start
)
1458 || !bfd_set_file_flags (obfd
, flags
))
1460 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1465 /* Copy architecture of input file to output file. */
1466 iarch
= bfd_get_arch (ibfd
);
1467 imach
= bfd_get_mach (ibfd
);
1468 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1469 && (ibfd
->target_defaulted
1470 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1472 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1473 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1474 bfd_get_archive_filename (ibfd
));
1476 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1477 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1478 bfd_get_mach (ibfd
)));
1482 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1484 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1488 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1489 && bfd_pei_p (obfd
))
1491 /* Set up PE parameters. */
1492 pe_data_type
*pe
= pe_data (obfd
);
1494 if (pe_file_alignment
!= (bfd_vma
) -1)
1495 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
1497 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
1499 if (pe_heap_commit
!= (bfd_vma
) -1)
1500 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
1502 if (pe_heap_reserve
!= (bfd_vma
) -1)
1503 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
1505 if (pe_image_base
!= (bfd_vma
) -1)
1506 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
1508 if (pe_section_alignment
!= (bfd_vma
) -1)
1509 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
1511 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
1513 if (pe_stack_commit
!= (bfd_vma
) -1)
1514 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
1516 if (pe_stack_reserve
!= (bfd_vma
) -1)
1517 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_reserve
;
1519 if (pe_subsystem
!= -1)
1520 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
1522 if (pe_major_subsystem_version
!= -1)
1523 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
1525 if (pe_minor_subsystem_version
!= -1)
1526 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
1528 if (pe_file_alignment
> pe_section_alignment
)
1530 char file_alignment
[20], section_alignment
[20];
1532 sprintf_vma (file_alignment
, pe_file_alignment
);
1533 sprintf_vma (section_alignment
, pe_section_alignment
);
1534 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1536 file_alignment
, section_alignment
);
1543 if (osympp
!= isympp
)
1549 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1552 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1556 osympp
= isympp
= xmalloc (symsize
);
1557 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1560 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1564 /* BFD mandates that all output sections be created and sizes set before
1565 any output is done. Thus, we traverse all sections multiple times. */
1566 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1568 if (!extract_symbol
)
1569 setup_bfd_headers (ibfd
, obfd
);
1571 if (add_sections
!= NULL
)
1573 struct section_add
*padd
;
1574 struct section_list
*pset
;
1576 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1580 pset
= find_section_list (padd
->name
, FALSE
);
1584 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1585 if (pset
!= NULL
&& pset
->set_flags
)
1586 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1588 /* bfd_make_section_with_flags() does not return very helpful
1589 error codes, so check for the most likely user error first. */
1590 if (bfd_get_section_by_name (obfd
, padd
->name
))
1592 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1593 _("can't add section '%s'"), padd
->name
);
1598 padd
->section
= bfd_make_section_with_flags (obfd
, padd
->name
, flags
);
1599 if (padd
->section
== NULL
)
1601 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1602 _("can't create section `%s'"),
1608 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1610 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1616 if (pset
->change_vma
!= CHANGE_IGNORE
)
1617 if (! bfd_set_section_vma (obfd
, padd
->section
,
1620 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1624 if (pset
->change_lma
!= CHANGE_IGNORE
)
1626 padd
->section
->lma
= pset
->lma_val
;
1628 if (! bfd_set_section_alignment
1629 (obfd
, padd
->section
,
1630 bfd_section_alignment (obfd
, padd
->section
)))
1632 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1640 if (gnu_debuglink_filename
!= NULL
)
1642 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1643 (obfd
, gnu_debuglink_filename
);
1645 if (gnu_debuglink_section
== NULL
)
1647 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1648 _("cannot create debug link section `%s'"),
1649 gnu_debuglink_filename
);
1653 /* Special processing for PE format files. We
1654 have no way to distinguish PE from COFF here. */
1655 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1657 bfd_vma debuglink_vma
;
1658 asection
* highest_section
;
1661 /* The PE spec requires that all sections be adjacent and sorted
1662 in ascending order of VMA. It also specifies that debug
1663 sections should be last. This is despite the fact that debug
1664 sections are not loaded into memory and so in theory have no
1667 This means that the debuglink section must be given a non-zero
1668 VMA which makes it contiguous with other debug sections. So
1669 walk the current section list, find the section with the
1670 highest VMA and start the debuglink section after that one. */
1671 for (sec
= obfd
->sections
, highest_section
= NULL
;
1675 && (highest_section
== NULL
1676 || sec
->vma
> highest_section
->vma
))
1677 highest_section
= sec
;
1679 if (highest_section
)
1680 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1681 + highest_section
->size
,
1682 /* FIXME: We ought to be using
1683 COFF_PAGE_SIZE here or maybe
1684 bfd_get_section_alignment() (if it
1685 was set) but since this is for PE
1686 and we know the required alignment
1687 it is easier just to hard code it. */
1690 /* Umm, not sure what to do in this case. */
1691 debuglink_vma
= 0x1000;
1693 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1697 if (bfd_count_sections (obfd
) != 0
1698 && (gap_fill_set
|| pad_to_set
))
1703 /* We must fill in gaps between the sections and/or we must pad
1704 the last section to a specified address. We do this by
1705 grabbing a list of the sections, sorting them by VMA, and
1706 increasing the section sizes as required to fill the gaps.
1707 We write out the gap contents below. */
1709 c
= bfd_count_sections (obfd
);
1710 osections
= xmalloc (c
* sizeof (asection
*));
1712 bfd_map_over_sections (obfd
, get_sections
, &set
);
1714 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1716 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1717 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1721 for (i
= 0; i
< c
- 1; i
++)
1725 bfd_vma gap_start
, gap_stop
;
1727 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1728 if ((flags
& SEC_HAS_CONTENTS
) == 0
1729 || (flags
& SEC_LOAD
) == 0)
1732 size
= bfd_section_size (obfd
, osections
[i
]);
1733 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1734 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1735 if (gap_start
< gap_stop
)
1737 if (! bfd_set_section_size (obfd
, osections
[i
],
1738 size
+ (gap_stop
- gap_start
)))
1740 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
1741 _("Can't fill gap after section"));
1745 gaps
[i
] = gap_stop
- gap_start
;
1746 if (max_gap
< gap_stop
- gap_start
)
1747 max_gap
= gap_stop
- gap_start
;
1757 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1758 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1759 if (lma
+ size
< pad_to
)
1761 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1764 bfd_nonfatal_message (NULL
, obfd
, osections
[c
- 1],
1765 _("can't add padding"));
1770 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1771 if (max_gap
< pad_to
- (lma
+ size
))
1772 max_gap
= pad_to
- (lma
+ size
);
1778 /* Symbol filtering must happen after the output sections
1779 have been created, but before their contents are set. */
1781 if (convert_debugging
)
1782 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, FALSE
);
1784 if (strip_symbols
== STRIP_DEBUG
1785 || strip_symbols
== STRIP_ALL
1786 || strip_symbols
== STRIP_UNNEEDED
1787 || strip_symbols
== STRIP_NONDEBUG
1788 || discard_locals
!= LOCALS_UNDEF
1790 || htab_elements (strip_specific_htab
) != 0
1791 || htab_elements (keep_specific_htab
) != 0
1792 || htab_elements (localize_specific_htab
) != 0
1793 || htab_elements (globalize_specific_htab
) != 0
1794 || htab_elements (keepglobal_specific_htab
) != 0
1795 || htab_elements (weaken_specific_htab
) != 0
1796 || prefix_symbols_string
1799 || convert_debugging
1800 || change_leading_char
1801 || remove_leading_char
1802 || redefine_sym_list
1805 /* Mark symbols used in output relocations so that they
1806 are kept, even if they are local labels or static symbols.
1808 Note we iterate over the input sections examining their
1809 relocations since the relocations for the output sections
1810 haven't been set yet. mark_symbols_used_in_relocations will
1811 ignore input sections which have no corresponding output
1813 if (strip_symbols
!= STRIP_ALL
)
1814 bfd_map_over_sections (ibfd
,
1815 mark_symbols_used_in_relocations
,
1817 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1818 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1821 if (convert_debugging
&& dhandle
!= NULL
)
1823 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1830 bfd_set_symtab (obfd
, osympp
, symcount
);
1832 /* This has to happen after the symbol table has been set. */
1833 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1835 if (add_sections
!= NULL
)
1837 struct section_add
*padd
;
1839 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1841 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1844 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1850 if (gnu_debuglink_filename
!= NULL
)
1852 if (! bfd_fill_in_gnu_debuglink_section
1853 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1855 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1856 _("cannot fill debug link section `%s'"),
1857 gnu_debuglink_filename
);
1862 if (gap_fill_set
|| pad_to_set
)
1867 /* Fill in the gaps. */
1870 buf
= xmalloc (max_gap
);
1871 memset (buf
, gap_fill
, max_gap
);
1873 c
= bfd_count_sections (obfd
);
1874 for (i
= 0; i
< c
; i
++)
1882 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1893 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1896 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
1907 /* Do not copy backend data if --extract-symbol is passed; anything
1908 that needs to look at the section contents will fail. */
1912 /* Allow the BFD backend to copy any private data it understands
1913 from the input BFD to the output BFD. This is done last to
1914 permit the routine to look at the filtered symbol table, which is
1915 important for the ECOFF code at least. */
1916 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1918 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1919 _("error copying private BFD data"));
1923 /* Switch to the alternate machine code. We have to do this at the
1924 very end, because we only initialize the header when we create
1925 the first section. */
1926 if (use_alt_mach_code
!= 0)
1928 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1930 non_fatal (_("this target does not support %lu alternative machine codes"),
1932 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1934 non_fatal (_("treating that number as an absolute e_machine value instead"));
1935 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
1938 non_fatal (_("ignoring the alternative value"));
1945 /* Read each archive element in turn from IBFD, copy the
1946 contents to temp file, and keep the temp file handle.
1947 If 'force_output_target' is TRUE then make sure that
1948 all elements in the new archive are of the type
1952 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
1953 bfd_boolean force_output_target
)
1957 struct name_list
*next
;
1961 bfd
**ptr
= &obfd
->archive_head
;
1964 const char *filename
;
1966 /* Make a temp directory to hold the contents. */
1967 dir
= make_tempdir (bfd_get_filename (obfd
));
1969 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1972 obfd
->has_armap
= ibfd
->has_armap
;
1973 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
1977 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1979 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1982 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1986 while (!status
&& this_element
!= NULL
)
1992 int stat_status
= 0;
1993 bfd_boolean
delete = TRUE
;
1995 /* Create an output file for this member. */
1996 output_name
= concat (dir
, "/",
1997 bfd_get_filename (this_element
), (char *) 0);
1999 /* If the file already exists, make another temp dir. */
2000 if (stat (output_name
, &buf
) >= 0)
2002 output_name
= make_tempdir (output_name
);
2003 if (output_name
== NULL
)
2004 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2007 l
= xmalloc (sizeof (struct name_list
));
2008 l
->name
= output_name
;
2012 output_name
= concat (output_name
, "/",
2013 bfd_get_filename (this_element
), (char *) 0);
2018 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
2020 if (stat_status
!= 0)
2021 non_fatal (_("internal stat error on %s"),
2022 bfd_get_filename (this_element
));
2025 l
= xmalloc (sizeof (struct name_list
));
2026 l
->name
= output_name
;
2031 if (bfd_check_format (this_element
, bfd_object
))
2033 /* PR binutils/3110: Cope with archives
2034 containing multiple target types. */
2035 if (force_output_target
)
2036 output_bfd
= bfd_openw (output_name
, output_target
);
2038 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
2040 if (output_bfd
== NULL
)
2042 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2047 delete = ! copy_object (this_element
, output_bfd
);
2050 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
2052 if (!bfd_close (output_bfd
))
2054 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2055 /* Error in new object file. Don't change archive. */
2060 goto copy_unknown_element
;
2064 bfd_nonfatal_message (NULL
, this_element
, NULL
,
2065 _("Unable to recognise the format of file"));
2067 output_bfd
= bfd_openw (output_name
, output_target
);
2068 copy_unknown_element
:
2069 delete = !copy_unknown_object (this_element
, output_bfd
);
2070 if (!bfd_close_all_done (output_bfd
))
2072 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2073 /* Error in new object file. Don't change archive. */
2080 unlink (output_name
);
2085 if (preserve_dates
&& stat_status
== 0)
2086 set_times (output_name
, &buf
);
2088 /* Open the newly output file and attach to our list. */
2089 output_bfd
= bfd_openr (output_name
, output_target
);
2091 l
->obfd
= output_bfd
;
2094 ptr
= &output_bfd
->archive_next
;
2096 last_element
= this_element
;
2098 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
2100 bfd_close (last_element
);
2105 filename
= bfd_get_filename (obfd
);
2106 if (!bfd_close (obfd
))
2109 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2113 filename
= bfd_get_filename (ibfd
);
2114 if (!bfd_close (ibfd
))
2117 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2121 /* Delete all the files that we opened. */
2122 for (l
= list
; l
!= NULL
; l
= l
->next
)
2124 if (l
->obfd
== NULL
)
2128 bfd_close (l
->obfd
);
2136 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
2138 /* This is only relevant to Coff targets. */
2139 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
2142 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
2143 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
2147 /* The top-level control. */
2150 copy_file (const char *input_filename
, const char *output_filename
,
2151 const char *input_target
, const char *output_target
)
2154 char **obj_matching
;
2155 char **core_matching
;
2157 if (get_file_size (input_filename
) < 1)
2163 /* To allow us to do "strip *" without dying on the first
2164 non-object file, failures are nonfatal. */
2165 ibfd
= bfd_openr (input_filename
, input_target
);
2168 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2173 if (bfd_check_format (ibfd
, bfd_archive
))
2175 bfd_boolean force_output_target
;
2178 /* bfd_get_target does not return the correct value until
2179 bfd_check_format succeeds. */
2180 if (output_target
== NULL
)
2182 output_target
= bfd_get_target (ibfd
);
2183 force_output_target
= FALSE
;
2186 force_output_target
= TRUE
;
2188 obfd
= bfd_openw (output_filename
, output_target
);
2191 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2195 /* This is a no-op on non-Coff targets. */
2196 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2198 copy_archive (ibfd
, obfd
, output_target
, force_output_target
);
2200 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
2205 /* bfd_get_target does not return the correct value until
2206 bfd_check_format succeeds. */
2207 if (output_target
== NULL
)
2208 output_target
= bfd_get_target (ibfd
);
2210 obfd
= bfd_openw (output_filename
, output_target
);
2213 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2217 /* This is a no-op on non-Coff targets. */
2218 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2220 if (! copy_object (ibfd
, obfd
))
2223 if (!bfd_close (obfd
))
2226 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2230 if (!bfd_close (ibfd
))
2233 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2239 bfd_error_type obj_error
= bfd_get_error ();
2240 bfd_error_type core_error
;
2242 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
2244 /* This probably can't happen.. */
2245 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2246 free (obj_matching
);
2250 core_error
= bfd_get_error ();
2251 /* Report the object error in preference to the core error. */
2252 if (obj_error
!= core_error
)
2253 bfd_set_error (obj_error
);
2255 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2257 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2259 list_matching_formats (obj_matching
);
2260 free (obj_matching
);
2262 if (core_error
== bfd_error_file_ambiguously_recognized
)
2264 list_matching_formats (core_matching
);
2265 free (core_matching
);
2272 /* Add a name to the section renaming list. */
2275 add_section_rename (const char * old_name
, const char * new_name
,
2278 section_rename
* rename
;
2280 /* Check for conflicts first. */
2281 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2282 if (strcmp (rename
->old_name
, old_name
) == 0)
2284 /* Silently ignore duplicate definitions. */
2285 if (strcmp (rename
->new_name
, new_name
) == 0
2286 && rename
->flags
== flags
)
2289 fatal (_("Multiple renames of section %s"), old_name
);
2292 rename
= xmalloc (sizeof (* rename
));
2294 rename
->old_name
= old_name
;
2295 rename
->new_name
= new_name
;
2296 rename
->flags
= flags
;
2297 rename
->next
= section_rename_list
;
2299 section_rename_list
= rename
;
2302 /* Check the section rename list for a new name of the input section
2303 ISECTION. Return the new name if one is found.
2304 Also set RETURNED_FLAGS to the flags to be used for this section. */
2307 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2308 flagword
* returned_flags
)
2310 const char * old_name
= bfd_section_name (ibfd
, isection
);
2311 section_rename
* rename
;
2313 /* Default to using the flags of the input section. */
2314 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2316 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2317 if (strcmp (rename
->old_name
, old_name
) == 0)
2319 if (rename
->flags
!= (flagword
) -1)
2320 * returned_flags
= rename
->flags
;
2322 return rename
->new_name
;
2328 /* Once each of the sections is copied, we may still need to do some
2329 finalization work for private section headers. Do that here. */
2332 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2334 /* Allow the BFD backend to copy any private data it understands
2335 from the input section to the output section. */
2336 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2339 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
2340 _("error in private header data"));
2344 /* All went well. */
2348 /* Create a section in OBFD with the same
2349 name and attributes as ISECTION in IBFD. */
2352 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2354 bfd
*obfd
= obfdarg
;
2355 struct section_list
*p
;
2363 char *prefix
= NULL
;
2364 bfd_boolean make_nobits
;
2366 if (is_strip_section (ibfd
, isection
))
2369 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2373 /* Get the, possibly new, name of the output section. */
2374 name
= find_section_rename (ibfd
, isection
, & flags
);
2376 /* Prefix sections. */
2377 if ((prefix_alloc_sections_string
)
2378 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2379 prefix
= prefix_alloc_sections_string
;
2380 else if (prefix_sections_string
)
2381 prefix
= prefix_sections_string
;
2387 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
2393 make_nobits
= FALSE
;
2394 if (p
!= NULL
&& p
->set_flags
)
2395 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2396 else if (strip_symbols
== STRIP_NONDEBUG
2397 && (flags
& SEC_ALLOC
) != 0
2398 && (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
2399 || elf_section_type (isection
) != SHT_NOTE
))
2401 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2402 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2406 /* Twiddle the input section flags so that it seems to
2407 elf.c:copy_private_bfd_data that section flags have not
2408 changed between input and output sections. This hack
2409 prevents wholesale rewriting of the program headers. */
2410 isection
->flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2414 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2416 if (osection
== NULL
)
2418 err
= _("failed to create output section");
2423 elf_section_type (osection
) = SHT_NOBITS
;
2425 size
= bfd_section_size (ibfd
, isection
);
2427 size
= (size
+ interleave
- 1) / interleave
;
2428 else if (extract_symbol
)
2430 if (! bfd_set_section_size (obfd
, osection
, size
))
2432 err
= _("failed to set size");
2436 vma
= bfd_section_vma (ibfd
, isection
);
2437 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2439 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2442 vma
+= change_section_address
;
2444 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2446 err
= _("failed to set vma");
2450 lma
= isection
->lma
;
2451 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2453 if (p
->change_lma
== CHANGE_MODIFY
)
2455 else if (p
->change_lma
== CHANGE_SET
)
2461 lma
+= change_section_address
;
2463 osection
->lma
= lma
;
2465 /* FIXME: This is probably not enough. If we change the LMA we
2466 may have to recompute the header for the file as well. */
2467 if (!bfd_set_section_alignment (obfd
,
2469 bfd_section_alignment (ibfd
, isection
)))
2471 err
= _("failed to set alignment");
2475 /* Copy merge entity size. */
2476 osection
->entsize
= isection
->entsize
;
2478 /* This used to be mangle_section; we do here to avoid using
2479 bfd_get_section_by_name since some formats allow multiple
2480 sections with the same name. */
2481 isection
->output_section
= osection
;
2482 isection
->output_offset
= 0;
2484 /* Do not copy backend data if --extract-symbol is passed; anything
2485 that needs to look at the section contents will fail. */
2489 if ((isection
->flags
& SEC_GROUP
) != 0)
2491 asymbol
*gsym
= group_signature (isection
);
2495 gsym
->flags
|= BSF_KEEP
;
2496 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2497 elf_group_id (isection
) = gsym
;
2501 /* Allow the BFD backend to copy any private data it understands
2502 from the input section to the output section. */
2503 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2505 err
= _("failed to copy private data");
2509 /* All went well. */
2514 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
2517 /* Copy the data of input section ISECTION of IBFD
2518 to an output section with the same name in OBFD.
2519 If stripping then don't copy any relocation info. */
2522 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2524 bfd
*obfd
= obfdarg
;
2525 struct section_list
*p
;
2533 /* If we have already failed earlier on,
2534 do not keep on generating complaints now. */
2538 if (is_strip_section (ibfd
, isection
))
2541 flags
= bfd_get_section_flags (ibfd
, isection
);
2542 if ((flags
& SEC_GROUP
) != 0)
2545 osection
= isection
->output_section
;
2546 size
= bfd_get_section_size (isection
);
2548 if (size
== 0 || osection
== 0)
2554 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2556 /* Core files do not need to be relocated. */
2557 if (bfd_get_format (obfd
) == bfd_core
)
2561 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2565 /* Do not complain if the target does not support relocations. */
2566 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2571 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2578 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2581 relpp
= xmalloc (relsize
);
2582 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2586 bfd_nonfatal_message (NULL
, ibfd
, isection
,
2587 _("relocation count is negative"));
2591 if (strip_symbols
== STRIP_ALL
)
2593 /* Remove relocations which are not in
2594 keep_strip_specific_list. */
2595 arelent
**temp_relpp
;
2596 long temp_relcount
= 0;
2599 temp_relpp
= xmalloc (relsize
);
2600 for (i
= 0; i
< relcount
; i
++)
2601 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2602 keep_specific_htab
))
2603 temp_relpp
[temp_relcount
++] = relpp
[i
];
2604 relcount
= temp_relcount
;
2609 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2614 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2615 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2617 void *memhunk
= xmalloc (size
);
2619 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2622 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2628 /* We don't handle leftover bytes (too many possible behaviors,
2629 and we don't know what the user wants). The section length
2630 must be a multiple of the number of bytes to swap. */
2631 if ((size
% reverse_bytes
) == 0)
2636 for (i
= 0; i
< size
; i
+= reverse_bytes
)
2637 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
2639 bfd_byte
*m
= (bfd_byte
*) memhunk
;
2642 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
2643 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
2647 /* User must pad the section up in order to do this. */
2648 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2649 bfd_section_name (ibfd
, isection
), reverse_bytes
);
2654 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2655 char *from
= (char *) memhunk
+ copy_byte
;
2657 char *end
= (char *) memhunk
+ size
;
2659 for (; from
< end
; from
+= interleave
)
2662 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2663 osection
->lma
/= interleave
;
2666 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2669 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2674 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2676 void *memhunk
= xmalloc (size
);
2678 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2679 flag--they can just remove the section entirely and add it
2680 back again. However, we do permit them to turn on the
2681 SEC_HAS_CONTENTS flag, and take it to mean that the section
2682 contents should be zeroed out. */
2684 memset (memhunk
, 0, size
);
2685 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2688 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2695 /* Get all the sections. This is used when --gap-fill or --pad-to is
2699 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2701 asection
***secppp
= secppparg
;
2703 **secppp
= osection
;
2707 /* Sort sections by VMA. This is called via qsort, and is used when
2708 --gap-fill or --pad-to is used. We force non loadable or empty
2709 sections to the front, where they are easier to ignore. */
2712 compare_section_lma (const void *arg1
, const void *arg2
)
2714 const asection
*const *sec1
= arg1
;
2715 const asection
*const *sec2
= arg2
;
2716 flagword flags1
, flags2
;
2718 /* Sort non loadable sections to the front. */
2719 flags1
= (*sec1
)->flags
;
2720 flags2
= (*sec2
)->flags
;
2721 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2722 || (flags1
& SEC_LOAD
) == 0)
2724 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2725 && (flags2
& SEC_LOAD
) != 0)
2730 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2731 || (flags2
& SEC_LOAD
) == 0)
2735 /* Sort sections by LMA. */
2736 if ((*sec1
)->lma
> (*sec2
)->lma
)
2738 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2741 /* Sort sections with the same LMA by size. */
2742 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2744 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2750 /* Mark all the symbols which will be used in output relocations with
2751 the BSF_KEEP flag so that those symbols will not be stripped.
2753 Ignore relocations which will not appear in the output file. */
2756 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2758 asymbol
**symbols
= symbolsarg
;
2763 /* Ignore an input section with no corresponding output section. */
2764 if (isection
->output_section
== NULL
)
2767 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2770 /* Do not complain if the target does not support relocations. */
2771 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2773 bfd_fatal (bfd_get_filename (ibfd
));
2779 relpp
= xmalloc (relsize
);
2780 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2782 bfd_fatal (bfd_get_filename (ibfd
));
2784 /* Examine each symbol used in a relocation. If it's not one of the
2785 special bfd section symbols, then mark it with BSF_KEEP. */
2786 for (i
= 0; i
< relcount
; i
++)
2788 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2789 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2790 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2791 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2798 /* Write out debugging information. */
2801 write_debugging_info (bfd
*obfd
, void *dhandle
,
2802 long *symcountp ATTRIBUTE_UNUSED
,
2803 asymbol
***symppp ATTRIBUTE_UNUSED
)
2805 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2806 return write_ieee_debugging_info (obfd
, dhandle
);
2808 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2809 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2811 bfd_byte
*syms
, *strings
;
2812 bfd_size_type symsize
, stringsize
;
2813 asection
*stabsec
, *stabstrsec
;
2816 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2821 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2822 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2823 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2825 || stabstrsec
== NULL
2826 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2827 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2828 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2829 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2831 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2832 _("can't create debugging section"));
2836 /* We can get away with setting the section contents now because
2837 the next thing the caller is going to do is copy over the
2838 real sections. We may someday have to split the contents
2839 setting out of this function. */
2840 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2841 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2844 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2845 _("can't set debugging section contents"));
2852 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2853 _("don't know how to write debugging information for %s"),
2854 bfd_get_target (obfd
));
2859 strip_main (int argc
, char *argv
[])
2861 char *input_target
= NULL
;
2862 char *output_target
= NULL
;
2863 bfd_boolean show_version
= FALSE
;
2864 bfd_boolean formats_info
= FALSE
;
2867 struct section_list
*p
;
2868 char *output_file
= NULL
;
2870 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2871 strip_options
, (int *) 0)) != EOF
)
2876 input_target
= optarg
;
2879 output_target
= optarg
;
2882 input_target
= output_target
= optarg
;
2885 p
= find_section_list (optarg
, TRUE
);
2887 sections_removed
= TRUE
;
2890 strip_symbols
= STRIP_ALL
;
2894 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2895 strip_symbols
= STRIP_DEBUG
;
2897 case OPTION_STRIP_UNNEEDED
:
2898 strip_symbols
= STRIP_UNNEEDED
;
2901 add_specific_symbol (optarg
, keep_specific_htab
);
2904 add_specific_symbol (optarg
, strip_specific_htab
);
2907 output_file
= optarg
;
2910 preserve_dates
= TRUE
;
2913 discard_locals
= LOCALS_ALL
;
2916 discard_locals
= LOCALS_START_L
;
2922 show_version
= TRUE
;
2924 case OPTION_FORMATS_INFO
:
2925 formats_info
= TRUE
;
2927 case OPTION_ONLY_KEEP_DEBUG
:
2928 strip_symbols
= STRIP_NONDEBUG
;
2930 case OPTION_KEEP_FILE_SYMBOLS
:
2931 keep_file_symbols
= 1;
2934 /* We've been given a long option. */
2941 strip_usage (stdout
, 0);
2943 strip_usage (stderr
, 1);
2954 print_version ("strip");
2956 /* Default is to strip all symbols. */
2957 if (strip_symbols
== STRIP_UNDEF
2958 && discard_locals
== LOCALS_UNDEF
2959 && htab_elements (strip_specific_htab
) == 0)
2960 strip_symbols
= STRIP_ALL
;
2962 if (output_target
== NULL
)
2963 output_target
= input_target
;
2967 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2968 strip_usage (stderr
, 1);
2970 for (; i
< argc
; i
++)
2972 int hold_status
= status
;
2973 struct stat statbuf
;
2976 if (get_file_size (argv
[i
]) < 1)
2983 /* No need to check the return value of stat().
2984 It has already been checked in get_file_size(). */
2985 stat (argv
[i
], &statbuf
);
2987 if (output_file
== NULL
|| strcmp (argv
[i
], output_file
) == 0)
2988 tmpname
= make_tempname (argv
[i
]);
2990 tmpname
= output_file
;
2992 if (tmpname
== NULL
)
2994 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
2995 _("could not create temporary file to hold stripped copy"));
3001 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
3005 set_times (tmpname
, &statbuf
);
3006 if (output_file
!= tmpname
)
3007 smart_rename (tmpname
, output_file
? output_file
: argv
[i
],
3009 status
= hold_status
;
3012 unlink_if_ordinary (tmpname
);
3013 if (output_file
!= tmpname
)
3020 /* Set up PE subsystem. */
3023 set_pe_subsystem (const char *s
)
3025 const char *version
, *subsystem
;
3035 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
3036 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
3037 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
3038 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
3039 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
3040 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
3041 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
3042 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
3043 { "efi-rom", 1, IMAGE_SUBSYSTEM_EFI_ROM
},
3044 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
3050 /* Check for the presence of a version number. */
3051 version
= strchr (s
, ':');
3052 if (version
== NULL
)
3056 int len
= version
- s
;
3060 version
= copy
+ 1 + len
;
3061 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
3063 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
3065 non_fatal (_("%s: bad version in PE subsystem"), s
);
3068 /* Check for numeric subsystem. */
3069 value
= (short) strtol (subsystem
, ©
, 0);
3072 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3073 if (v
[i
].value
== value
)
3075 pe_subsystem
= value
;
3076 set_def
= v
[i
].set_def
;
3082 /* Search for subsystem by name. */
3083 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3084 if (strcmp (subsystem
, v
[i
].name
) == 0)
3086 pe_subsystem
= v
[i
].value
;
3087 set_def
= v
[i
].set_def
;
3095 fatal (_("unknown PE subsystem: %s"), s
);
3100 if (pe_file_alignment
== (bfd_vma
) -1)
3101 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
3102 if (pe_section_alignment
== (bfd_vma
) -1)
3103 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
3108 /* Convert EFI target to PEI target. */
3111 convert_efi_target (char *efi
)
3117 if (strcmp (efi
+ 4, "ia32") == 0)
3119 /* Change ia32 to i386. */
3124 else if (strcmp (efi
+ 4, "x86_64") == 0)
3126 /* Change x86_64 to x86-64. */
3132 copy_main (int argc
, char *argv
[])
3134 char * binary_architecture
= NULL
;
3135 char *input_filename
= NULL
;
3136 char *output_filename
= NULL
;
3138 char *input_target
= NULL
;
3139 char *output_target
= NULL
;
3140 bfd_boolean show_version
= FALSE
;
3141 bfd_boolean change_warn
= TRUE
;
3142 bfd_boolean formats_info
= FALSE
;
3144 struct section_list
*p
;
3145 struct stat statbuf
;
3147 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
3148 copy_options
, (int *) 0)) != EOF
)
3153 copy_byte
= atoi (optarg
);
3155 fatal (_("byte number must be non-negative"));
3159 binary_architecture
= optarg
;
3163 interleave
= atoi (optarg
);
3165 fatal (_("interleave must be positive"));
3169 case 's': /* "source" - 'I' is preferred */
3170 input_target
= optarg
;
3174 case 'd': /* "destination" - 'O' is preferred */
3175 output_target
= optarg
;
3179 input_target
= output_target
= optarg
;
3183 p
= find_section_list (optarg
, TRUE
);
3185 fatal (_("%s both copied and removed"), optarg
);
3187 sections_copied
= TRUE
;
3191 p
= find_section_list (optarg
, TRUE
);
3193 fatal (_("%s both copied and removed"), optarg
);
3195 sections_removed
= TRUE
;
3199 strip_symbols
= STRIP_ALL
;
3203 strip_symbols
= STRIP_DEBUG
;
3206 case OPTION_STRIP_UNNEEDED
:
3207 strip_symbols
= STRIP_UNNEEDED
;
3210 case OPTION_ONLY_KEEP_DEBUG
:
3211 strip_symbols
= STRIP_NONDEBUG
;
3214 case OPTION_KEEP_FILE_SYMBOLS
:
3215 keep_file_symbols
= 1;
3218 case OPTION_ADD_GNU_DEBUGLINK
:
3219 gnu_debuglink_filename
= optarg
;
3223 add_specific_symbol (optarg
, keep_specific_htab
);
3227 add_specific_symbol (optarg
, strip_specific_htab
);
3230 case OPTION_STRIP_UNNEEDED_SYMBOL
:
3231 add_specific_symbol (optarg
, strip_unneeded_htab
);
3235 add_specific_symbol (optarg
, localize_specific_htab
);
3238 case OPTION_GLOBALIZE_SYMBOL
:
3239 add_specific_symbol (optarg
, globalize_specific_htab
);
3243 add_specific_symbol (optarg
, keepglobal_specific_htab
);
3247 add_specific_symbol (optarg
, weaken_specific_htab
);
3251 preserve_dates
= TRUE
;
3259 discard_locals
= LOCALS_ALL
;
3263 discard_locals
= LOCALS_START_L
;
3271 show_version
= TRUE
;
3274 case OPTION_FORMATS_INFO
:
3275 formats_info
= TRUE
;
3282 case OPTION_ADD_SECTION
:
3286 struct section_add
*pa
;
3291 s
= strchr (optarg
, '=');
3294 fatal (_("bad format for %s"), "--add-section");
3296 size
= get_file_size (s
+ 1);
3303 pa
= xmalloc (sizeof (struct section_add
));
3306 name
= xmalloc (len
+ 1);
3307 strncpy (name
, optarg
, len
);
3311 pa
->filename
= s
+ 1;
3313 pa
->contents
= xmalloc (size
);
3315 f
= fopen (pa
->filename
, FOPEN_RB
);
3318 fatal (_("cannot open: %s: %s"),
3319 pa
->filename
, strerror (errno
));
3321 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
3323 fatal (_("%s: fread failed"), pa
->filename
);
3327 pa
->next
= add_sections
;
3332 case OPTION_CHANGE_START
:
3333 change_start
= parse_vma (optarg
, "--change-start");
3336 case OPTION_CHANGE_SECTION_ADDRESS
:
3337 case OPTION_CHANGE_SECTION_LMA
:
3338 case OPTION_CHANGE_SECTION_VMA
:
3343 char *option
= NULL
;
3345 enum change_action what
= CHANGE_IGNORE
;
3349 case OPTION_CHANGE_SECTION_ADDRESS
:
3350 option
= "--change-section-address";
3352 case OPTION_CHANGE_SECTION_LMA
:
3353 option
= "--change-section-lma";
3355 case OPTION_CHANGE_SECTION_VMA
:
3356 option
= "--change-section-vma";
3360 s
= strchr (optarg
, '=');
3363 s
= strchr (optarg
, '+');
3366 s
= strchr (optarg
, '-');
3368 fatal (_("bad format for %s"), option
);
3373 name
= xmalloc (len
+ 1);
3374 strncpy (name
, optarg
, len
);
3377 p
= find_section_list (name
, TRUE
);
3379 val
= parse_vma (s
+ 1, option
);
3383 case '=': what
= CHANGE_SET
; break;
3384 case '-': val
= - val
; /* Drop through. */
3385 case '+': what
= CHANGE_MODIFY
; break;
3390 case OPTION_CHANGE_SECTION_ADDRESS
:
3391 p
->change_vma
= what
;
3395 case OPTION_CHANGE_SECTION_LMA
:
3396 p
->change_lma
= what
;
3400 case OPTION_CHANGE_SECTION_VMA
:
3401 p
->change_vma
= what
;
3408 case OPTION_CHANGE_ADDRESSES
:
3409 change_section_address
= parse_vma (optarg
, "--change-addresses");
3410 change_start
= change_section_address
;
3413 case OPTION_CHANGE_WARNINGS
:
3417 case OPTION_CHANGE_LEADING_CHAR
:
3418 change_leading_char
= TRUE
;
3421 case OPTION_DEBUGGING
:
3422 convert_debugging
= TRUE
;
3425 case OPTION_GAP_FILL
:
3427 bfd_vma gap_fill_vma
;
3429 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
3430 gap_fill
= (bfd_byte
) gap_fill_vma
;
3431 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
3435 sprintf_vma (buff
, gap_fill_vma
);
3437 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3440 gap_fill_set
= TRUE
;
3444 case OPTION_NO_CHANGE_WARNINGS
:
3445 change_warn
= FALSE
;
3449 pad_to
= parse_vma (optarg
, "--pad-to");
3453 case OPTION_REMOVE_LEADING_CHAR
:
3454 remove_leading_char
= TRUE
;
3457 case OPTION_REDEFINE_SYM
:
3459 /* Push this redefinition onto redefine_symbol_list. */
3463 const char *nextarg
;
3464 char *source
, *target
;
3466 s
= strchr (optarg
, '=');
3468 fatal (_("bad format for %s"), "--redefine-sym");
3471 source
= xmalloc (len
+ 1);
3472 strncpy (source
, optarg
, len
);
3476 len
= strlen (nextarg
);
3477 target
= xmalloc (len
+ 1);
3478 strcpy (target
, nextarg
);
3480 redefine_list_append ("--redefine-sym", source
, target
);
3487 case OPTION_REDEFINE_SYMS
:
3488 add_redefine_syms_file (optarg
);
3491 case OPTION_SET_SECTION_FLAGS
:
3497 s
= strchr (optarg
, '=');
3499 fatal (_("bad format for %s"), "--set-section-flags");
3502 name
= xmalloc (len
+ 1);
3503 strncpy (name
, optarg
, len
);
3506 p
= find_section_list (name
, TRUE
);
3508 p
->set_flags
= TRUE
;
3509 p
->flags
= parse_flags (s
+ 1);
3513 case OPTION_RENAME_SECTION
:
3516 const char *eq
, *fl
;
3521 eq
= strchr (optarg
, '=');
3523 fatal (_("bad format for %s"), "--rename-section");
3527 fatal (_("bad format for %s"), "--rename-section");
3529 old_name
= xmalloc (len
+ 1);
3530 strncpy (old_name
, optarg
, len
);
3534 fl
= strchr (eq
, ',');
3537 flags
= parse_flags (fl
+ 1);
3547 fatal (_("bad format for %s"), "--rename-section");
3549 new_name
= xmalloc (len
+ 1);
3550 strncpy (new_name
, eq
, len
);
3553 add_section_rename (old_name
, new_name
, flags
);
3557 case OPTION_SET_START
:
3558 set_start
= parse_vma (optarg
, "--set-start");
3559 set_start_set
= TRUE
;
3562 case OPTION_SREC_LEN
:
3563 Chunk
= parse_vma (optarg
, "--srec-len");
3566 case OPTION_SREC_FORCES3
:
3570 case OPTION_STRIP_SYMBOLS
:
3571 add_specific_symbols (optarg
, strip_specific_htab
);
3574 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3575 add_specific_symbols (optarg
, strip_unneeded_htab
);
3578 case OPTION_KEEP_SYMBOLS
:
3579 add_specific_symbols (optarg
, keep_specific_htab
);
3582 case OPTION_LOCALIZE_HIDDEN
:
3583 localize_hidden
= TRUE
;
3586 case OPTION_LOCALIZE_SYMBOLS
:
3587 add_specific_symbols (optarg
, localize_specific_htab
);
3590 case OPTION_LONG_SECTION_NAMES
:
3591 if (!strcmp ("enable", optarg
))
3592 long_section_names
= ENABLE
;
3593 else if (!strcmp ("disable", optarg
))
3594 long_section_names
= DISABLE
;
3595 else if (!strcmp ("keep", optarg
))
3596 long_section_names
= KEEP
;
3598 fatal (_("unknown long section names option '%s'"), optarg
);
3601 case OPTION_GLOBALIZE_SYMBOLS
:
3602 add_specific_symbols (optarg
, globalize_specific_htab
);
3605 case OPTION_KEEPGLOBAL_SYMBOLS
:
3606 add_specific_symbols (optarg
, keepglobal_specific_htab
);
3609 case OPTION_WEAKEN_SYMBOLS
:
3610 add_specific_symbols (optarg
, weaken_specific_htab
);
3613 case OPTION_ALT_MACH_CODE
:
3614 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3615 if (use_alt_mach_code
== 0)
3616 fatal (_("unable to parse alternative machine code"));
3619 case OPTION_PREFIX_SYMBOLS
:
3620 prefix_symbols_string
= optarg
;
3623 case OPTION_PREFIX_SECTIONS
:
3624 prefix_sections_string
= optarg
;
3627 case OPTION_PREFIX_ALLOC_SECTIONS
:
3628 prefix_alloc_sections_string
= optarg
;
3631 case OPTION_READONLY_TEXT
:
3632 bfd_flags_to_set
|= WP_TEXT
;
3633 bfd_flags_to_clear
&= ~WP_TEXT
;
3636 case OPTION_WRITABLE_TEXT
:
3637 bfd_flags_to_clear
|= WP_TEXT
;
3638 bfd_flags_to_set
&= ~WP_TEXT
;
3642 bfd_flags_to_set
|= D_PAGED
;
3643 bfd_flags_to_clear
&= ~D_PAGED
;
3647 bfd_flags_to_clear
|= D_PAGED
;
3648 bfd_flags_to_set
&= ~D_PAGED
;
3651 case OPTION_EXTRACT_SYMBOL
:
3652 extract_symbol
= TRUE
;
3655 case OPTION_REVERSE_BYTES
:
3657 int prev
= reverse_bytes
;
3659 reverse_bytes
= atoi (optarg
);
3660 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
3661 fatal (_("number of bytes to reverse must be positive and even"));
3663 if (prev
&& prev
!= reverse_bytes
)
3664 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3669 case OPTION_FILE_ALIGNMENT
:
3670 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
3676 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
3678 || (*end
!= '.' && *end
!= '\0'))
3679 non_fatal (_("%s: invalid reserve value for --heap"),
3681 else if (*end
!= '\0')
3683 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
3685 non_fatal (_("%s: invalid commit value for --heap"),
3691 case OPTION_IMAGE_BASE
:
3692 pe_image_base
= parse_vma (optarg
, "--image-base");
3695 case OPTION_SECTION_ALIGNMENT
:
3696 pe_section_alignment
= parse_vma (optarg
,
3697 "--section-alignment");
3700 case OPTION_SUBSYSTEM
:
3701 set_pe_subsystem (optarg
);
3707 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
3709 || (*end
!= '.' && *end
!= '\0'))
3710 non_fatal (_("%s: invalid reserve value for --stack"),
3712 else if (*end
!= '\0')
3714 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
3716 non_fatal (_("%s: invalid commit value for --stack"),
3723 /* We've been given a long option. */
3728 copy_usage (stdout
, 0);
3731 copy_usage (stderr
, 1);
3742 print_version ("objcopy");
3744 if (copy_byte
>= interleave
)
3745 fatal (_("byte number must be less than interleave"));
3747 if (optind
== argc
|| optind
+ 2 < argc
)
3748 copy_usage (stderr
, 1);
3750 input_filename
= argv
[optind
];
3751 if (optind
+ 1 < argc
)
3752 output_filename
= argv
[optind
+ 1];
3754 /* Default is to strip no symbols. */
3755 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3756 strip_symbols
= STRIP_NONE
;
3758 if (output_target
== NULL
)
3759 output_target
= input_target
;
3761 /* Convert input EFI target to PEI target. */
3762 if (input_target
!= NULL
3763 && strncmp (input_target
, "efi-", 4) == 0)
3767 efi
= xstrdup (output_target
+ 4);
3768 if (strncmp (efi
, "bsdrv-", 6) == 0
3769 || strncmp (efi
, "rtdrv-", 6) == 0)
3771 else if (strncmp (efi
, "app-", 4) != 0)
3772 fatal (_("unknown input EFI target: %s"), input_target
);
3775 convert_efi_target (efi
);
3778 /* Convert output EFI target to PEI target. */
3779 if (output_target
!= NULL
3780 && strncmp (output_target
, "efi-", 4) == 0)
3784 efi
= xstrdup (output_target
+ 4);
3785 if (strncmp (efi
, "app-", 4) == 0)
3787 if (pe_subsystem
== -1)
3788 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
3790 else if (strncmp (efi
, "bsdrv-", 6) == 0)
3792 if (pe_subsystem
== -1)
3793 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
3796 else if (strncmp (efi
, "rtdrv-", 6) == 0)
3798 if (pe_subsystem
== -1)
3799 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
3803 fatal (_("unknown output EFI target: %s"), output_target
);
3805 if (pe_file_alignment
== (bfd_vma
) -1)
3806 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
3807 if (pe_section_alignment
== (bfd_vma
) -1)
3808 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
3810 output_target
= efi
;
3811 convert_efi_target (efi
);
3814 if (binary_architecture
!= NULL
)
3816 if (input_target
&& strcmp (input_target
, "binary") == 0)
3818 const bfd_arch_info_type
* temp_arch_info
;
3820 temp_arch_info
= bfd_scan_arch (binary_architecture
);
3822 if (temp_arch_info
!= NULL
)
3824 bfd_external_binary_architecture
= temp_arch_info
->arch
;
3825 bfd_external_machine
= temp_arch_info
->mach
;
3828 fatal (_("architecture %s unknown"), binary_architecture
);
3832 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3833 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3838 if (stat (input_filename
, & statbuf
) < 0)
3839 fatal (_("warning: could not locate '%s'. System error message: %s"),
3840 input_filename
, strerror (errno
));
3842 /* If there is no destination file, or the source and destination files
3843 are the same, then create a temp and rename the result into the input. */
3844 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3845 tmpname
= make_tempname (input_filename
);
3847 tmpname
= output_filename
;
3849 if (tmpname
== NULL
)
3850 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3851 input_filename
, strerror (errno
));
3853 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3857 set_times (tmpname
, &statbuf
);
3858 if (tmpname
!= output_filename
)
3859 smart_rename (tmpname
, input_filename
, preserve_dates
);
3862 unlink_if_ordinary (tmpname
);
3866 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3870 if (p
->change_vma
!= CHANGE_IGNORE
)
3874 sprintf_vma (buff
, p
->vma_val
);
3876 /* xgettext:c-format */
3877 non_fatal (_("%s %s%c0x%s never used"),
3878 "--change-section-vma",
3880 p
->change_vma
== CHANGE_SET
? '=' : '+',
3884 if (p
->change_lma
!= CHANGE_IGNORE
)
3888 sprintf_vma (buff
, p
->lma_val
);
3890 /* xgettext:c-format */
3891 non_fatal (_("%s %s%c0x%s never used"),
3892 "--change-section-lma",
3894 p
->change_lma
== CHANGE_SET
? '=' : '+',
3905 main (int argc
, char *argv
[])
3907 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3908 setlocale (LC_MESSAGES
, "");
3910 #if defined (HAVE_SETLOCALE)
3911 setlocale (LC_CTYPE
, "");
3913 bindtextdomain (PACKAGE
, LOCALEDIR
);
3914 textdomain (PACKAGE
);
3916 program_name
= argv
[0];
3917 xmalloc_set_program_name (program_name
);
3919 START_PROGRESS (program_name
, 0);
3921 expandargv (&argc
, &argv
);
3923 strip_symbols
= STRIP_UNDEF
;
3924 discard_locals
= LOCALS_UNDEF
;
3927 set_default_bfd_target ();
3931 int i
= strlen (program_name
);
3932 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3933 /* Drop the .exe suffix, if any. */
3934 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3937 program_name
[i
] = '\0';
3940 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3943 create_symbol_htabs ();
3946 strip_main (argc
, argv
);
3948 copy_main (argc
, argv
);
3950 END_PROGRESS (program_name
);