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, 2010, 2011, 2012
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"
34 #include "coff/internal.h"
37 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
38 header in generic PE code. */
39 #include "coff/i386.h"
42 static bfd_vma pe_file_alignment
= (bfd_vma
) -1;
43 static bfd_vma pe_heap_commit
= (bfd_vma
) -1;
44 static bfd_vma pe_heap_reserve
= (bfd_vma
) -1;
45 static bfd_vma pe_image_base
= (bfd_vma
) -1;
46 static bfd_vma pe_section_alignment
= (bfd_vma
) -1;
47 static bfd_vma pe_stack_commit
= (bfd_vma
) -1;
48 static bfd_vma pe_stack_reserve
= (bfd_vma
) -1;
49 static short pe_subsystem
= -1;
50 static short pe_major_subsystem_version
= -1;
51 static short pe_minor_subsystem_version
= -1;
53 struct is_specified_symbol_predicate_data
59 /* A list to support redefine_sym. */
64 struct redefine_node
*next
;
67 typedef struct section_rename
69 const char * old_name
;
70 const char * new_name
;
72 struct section_rename
* next
;
76 /* List of sections to be renamed. */
77 static section_rename
*section_rename_list
;
79 static asymbol
**isympp
= NULL
; /* Input symbols. */
80 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
82 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
83 static int copy_byte
= -1;
84 static int interleave
= 0; /* Initialised to 4 in copy_main(). */
85 static int copy_width
= 1;
87 static bfd_boolean verbose
; /* Print file and target names. */
88 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
89 static bfd_boolean deterministic
; /* Enable deterministic archives. */
90 static int status
= 0; /* Exit status. */
95 STRIP_NONE
, /* Don't strip. */
96 STRIP_DEBUG
, /* Strip all debugger symbols. */
97 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
98 STRIP_NONDEBUG
, /* Strip everything but debug info. */
99 STRIP_ALL
/* Strip all symbols. */
102 /* Which symbols to remove. */
103 static enum strip_action strip_symbols
;
108 LOCALS_START_L
, /* Discard locals starting with L. */
109 LOCALS_ALL
/* Discard all locals. */
112 /* Which local symbols to remove. Overrides STRIP_ALL. */
113 static enum locals_action discard_locals
;
115 /* What kind of change to perform. */
123 /* Structure used to hold lists of sections and actions to take. */
126 struct section_list
* next
; /* Next section to change. */
127 const char * name
; /* Section name. */
128 bfd_boolean used
; /* Whether this entry was used. */
129 bfd_boolean remove
; /* Whether to remove this section. */
130 bfd_boolean copy
; /* Whether to copy this section. */
131 enum change_action change_vma
;/* Whether to change or set VMA. */
132 bfd_vma vma_val
; /* Amount to change by or set to. */
133 enum change_action change_lma
;/* Whether to change or set LMA. */
134 bfd_vma lma_val
; /* Amount to change by or set to. */
135 bfd_boolean set_flags
; /* Whether to set the section flags. */
136 flagword flags
; /* What to set the section flags to. */
139 static struct section_list
*change_sections
;
141 /* TRUE if some sections are to be removed. */
142 static bfd_boolean sections_removed
;
144 /* TRUE if only some sections are to be copied. */
145 static bfd_boolean sections_copied
;
147 /* Changes to the start address. */
148 static bfd_vma change_start
= 0;
149 static bfd_boolean set_start_set
= FALSE
;
150 static bfd_vma set_start
;
152 /* Changes to section addresses. */
153 static bfd_vma change_section_address
= 0;
155 /* Filling gaps between sections. */
156 static bfd_boolean gap_fill_set
= FALSE
;
157 static bfd_byte gap_fill
= 0;
159 /* Pad to a given address. */
160 static bfd_boolean pad_to_set
= FALSE
;
161 static bfd_vma pad_to
;
163 /* Use alternative machine code? */
164 static unsigned long use_alt_mach_code
= 0;
166 /* Output BFD flags user wants to set or clear */
167 static flagword bfd_flags_to_set
;
168 static flagword bfd_flags_to_clear
;
170 /* List of sections to add. */
173 /* Next section to add. */
174 struct section_add
*next
;
175 /* Name of section to add. */
177 /* Name of file holding section contents. */
178 const char *filename
;
181 /* Contents of file. */
183 /* BFD section, after it has been added. */
187 /* List of sections to add to the output BFD. */
188 static struct section_add
*add_sections
;
190 /* If non-NULL the argument to --add-gnu-debuglink.
191 This should be the filename to store in the .gnu_debuglink section. */
192 static const char * gnu_debuglink_filename
= NULL
;
194 /* Whether to convert debugging information. */
195 static bfd_boolean convert_debugging
= FALSE
;
197 /* Whether to compress/decompress DWARF debug sections. */
203 } do_debug_sections
= nothing
;
205 /* Whether to change the leading character in symbol names. */
206 static bfd_boolean change_leading_char
= FALSE
;
208 /* Whether to remove the leading character from global symbol names. */
209 static bfd_boolean remove_leading_char
= FALSE
;
211 /* Whether to permit wildcard in symbol comparison. */
212 static bfd_boolean wildcard
= FALSE
;
214 /* True if --localize-hidden is in effect. */
215 static bfd_boolean localize_hidden
= FALSE
;
217 /* List of symbols to strip, keep, localize, keep-global, weaken,
219 static htab_t strip_specific_htab
= NULL
;
220 static htab_t strip_unneeded_htab
= NULL
;
221 static htab_t keep_specific_htab
= NULL
;
222 static htab_t localize_specific_htab
= NULL
;
223 static htab_t globalize_specific_htab
= NULL
;
224 static htab_t keepglobal_specific_htab
= NULL
;
225 static htab_t weaken_specific_htab
= NULL
;
226 static struct redefine_node
*redefine_sym_list
= NULL
;
228 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
229 static bfd_boolean weaken
= FALSE
;
231 /* If this is TRUE, we retain BSF_FILE symbols. */
232 static bfd_boolean keep_file_symbols
= FALSE
;
234 /* Prefix symbols/sections. */
235 static char *prefix_symbols_string
= 0;
236 static char *prefix_sections_string
= 0;
237 static char *prefix_alloc_sections_string
= 0;
239 /* True if --extract-symbol was passed on the command line. */
240 static bfd_boolean extract_symbol
= FALSE
;
242 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
243 of <reverse_bytes> bytes within each output section. */
244 static int reverse_bytes
= 0;
246 /* For Coff objects, we may want to allow or disallow long section names,
247 or preserve them where found in the inputs. Debug info relies on them. */
248 enum long_section_name_handling
255 /* The default long section handling mode is to preserve them.
256 This is also the only behaviour for 'strip'. */
257 static enum long_section_name_handling long_section_names
= KEEP
;
259 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
260 enum command_line_switch
262 OPTION_ADD_SECTION
=150,
263 OPTION_CHANGE_ADDRESSES
,
264 OPTION_CHANGE_LEADING_CHAR
,
266 OPTION_CHANGE_SECTION_ADDRESS
,
267 OPTION_CHANGE_SECTION_LMA
,
268 OPTION_CHANGE_SECTION_VMA
,
269 OPTION_CHANGE_WARNINGS
,
270 OPTION_COMPRESS_DEBUG_SECTIONS
,
272 OPTION_DECOMPRESS_DEBUG_SECTIONS
,
274 OPTION_NO_CHANGE_WARNINGS
,
276 OPTION_REMOVE_LEADING_CHAR
,
277 OPTION_SET_SECTION_FLAGS
,
279 OPTION_STRIP_UNNEEDED
,
282 OPTION_REDEFINE_SYMS
,
285 OPTION_STRIP_SYMBOLS
,
286 OPTION_STRIP_UNNEEDED_SYMBOL
,
287 OPTION_STRIP_UNNEEDED_SYMBOLS
,
289 OPTION_LOCALIZE_HIDDEN
,
290 OPTION_LOCALIZE_SYMBOLS
,
291 OPTION_LONG_SECTION_NAMES
,
292 OPTION_GLOBALIZE_SYMBOL
,
293 OPTION_GLOBALIZE_SYMBOLS
,
294 OPTION_KEEPGLOBAL_SYMBOLS
,
295 OPTION_WEAKEN_SYMBOLS
,
296 OPTION_RENAME_SECTION
,
297 OPTION_ALT_MACH_CODE
,
298 OPTION_PREFIX_SYMBOLS
,
299 OPTION_PREFIX_SECTIONS
,
300 OPTION_PREFIX_ALLOC_SECTIONS
,
302 OPTION_ADD_GNU_DEBUGLINK
,
303 OPTION_ONLY_KEEP_DEBUG
,
304 OPTION_KEEP_FILE_SYMBOLS
,
305 OPTION_READONLY_TEXT
,
306 OPTION_WRITABLE_TEXT
,
309 OPTION_EXTRACT_SYMBOL
,
310 OPTION_REVERSE_BYTES
,
311 OPTION_FILE_ALIGNMENT
,
314 OPTION_SECTION_ALIGNMENT
,
316 OPTION_INTERLEAVE_WIDTH
,
320 /* Options to handle if running as "strip". */
322 static struct option strip_options
[] =
324 {"discard-all", no_argument
, 0, 'x'},
325 {"discard-locals", no_argument
, 0, 'X'},
326 {"enable-deterministic-archives", no_argument
, 0, 'D'},
327 {"format", required_argument
, 0, 'F'}, /* Obsolete */
328 {"help", no_argument
, 0, 'h'},
329 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
330 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
331 {"input-target", required_argument
, 0, 'I'},
332 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
333 {"keep-symbol", required_argument
, 0, 'K'},
334 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
335 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
336 {"output-target", required_argument
, 0, 'O'},
337 {"output-file", required_argument
, 0, 'o'},
338 {"preserve-dates", no_argument
, 0, 'p'},
339 {"remove-section", required_argument
, 0, 'R'},
340 {"strip-all", no_argument
, 0, 's'},
341 {"strip-debug", no_argument
, 0, 'S'},
342 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
343 {"strip-symbol", required_argument
, 0, 'N'},
344 {"target", required_argument
, 0, 'F'},
345 {"verbose", no_argument
, 0, 'v'},
346 {"version", no_argument
, 0, 'V'},
347 {"wildcard", no_argument
, 0, 'w'},
348 {0, no_argument
, 0, 0}
351 /* Options to handle if running as "objcopy". */
353 static struct option copy_options
[] =
355 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
356 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
357 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
358 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
359 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
360 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
361 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
362 {"binary-architecture", required_argument
, 0, 'B'},
363 {"byte", required_argument
, 0, 'b'},
364 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
365 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
366 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
367 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
368 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
369 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
370 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
371 {"compress-debug-sections", no_argument
, 0, OPTION_COMPRESS_DEBUG_SECTIONS
},
372 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
373 {"decompress-debug-sections", no_argument
, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS
},
374 {"discard-all", no_argument
, 0, 'x'},
375 {"discard-locals", no_argument
, 0, 'X'},
376 {"enable-deterministic-archives", no_argument
, 0, 'D'},
377 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
378 {"format", required_argument
, 0, 'F'}, /* Obsolete */
379 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
380 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
381 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
382 {"help", no_argument
, 0, 'h'},
383 {"impure", no_argument
, 0, OPTION_IMPURE
},
384 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
385 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
386 {"input-target", required_argument
, 0, 'I'},
387 {"interleave", optional_argument
, 0, 'i'},
388 {"interleave-width", required_argument
, 0, OPTION_INTERLEAVE_WIDTH
},
389 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
390 {"keep-global-symbol", required_argument
, 0, 'G'},
391 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
392 {"keep-symbol", required_argument
, 0, 'K'},
393 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
394 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
395 {"localize-symbol", required_argument
, 0, 'L'},
396 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
397 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
398 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
399 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
400 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
401 {"only-section", required_argument
, 0, 'j'},
402 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
403 {"output-target", required_argument
, 0, 'O'},
404 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
405 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
406 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
407 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
408 {"preserve-dates", no_argument
, 0, 'p'},
409 {"pure", no_argument
, 0, OPTION_PURE
},
410 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
411 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
412 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
413 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
414 {"remove-section", required_argument
, 0, 'R'},
415 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
416 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
417 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
418 {"set-start", required_argument
, 0, OPTION_SET_START
},
419 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
420 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
421 {"strip-all", no_argument
, 0, 'S'},
422 {"strip-debug", no_argument
, 0, 'g'},
423 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
424 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
425 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
426 {"strip-symbol", required_argument
, 0, 'N'},
427 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
428 {"target", required_argument
, 0, 'F'},
429 {"verbose", no_argument
, 0, 'v'},
430 {"version", no_argument
, 0, 'V'},
431 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
432 {"weaken-symbol", required_argument
, 0, 'W'},
433 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
434 {"wildcard", no_argument
, 0, 'w'},
435 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
436 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
437 {"heap", required_argument
, 0, OPTION_HEAP
},
438 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
439 {"section-alignment", required_argument
, 0, OPTION_SECTION_ALIGNMENT
},
440 {"stack", required_argument
, 0, OPTION_STACK
},
441 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
442 {0, no_argument
, 0, 0}
446 extern char *program_name
;
448 /* This flag distinguishes between strip and objcopy:
449 1 means this is 'strip'; 0 means this is 'objcopy'.
450 -1 means if we should use argv[0] to decide. */
453 /* The maximum length of an S record. This variable is declared in srec.c
454 and can be modified by the --srec-len parameter. */
455 extern unsigned int Chunk
;
457 /* Restrict the generation of Srecords to type S3 only.
458 This variable is declare in bfd/srec.c and can be toggled
459 on by the --srec-forceS3 command line switch. */
460 extern bfd_boolean S3Forced
;
462 /* Forward declarations. */
463 static void setup_section (bfd
*, asection
*, void *);
464 static void setup_bfd_headers (bfd
*, bfd
*);
465 static void copy_relocations_in_section (bfd
*, asection
*, void *);
466 static void copy_section (bfd
*, asection
*, void *);
467 static void get_sections (bfd
*, asection
*, void *);
468 static int compare_section_lma (const void *, const void *);
469 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
470 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
471 static const char *lookup_sym_redefinition (const char *);
474 copy_usage (FILE *stream
, int exit_status
)
476 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
477 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
478 fprintf (stream
, _(" The options are:\n"));
479 fprintf (stream
, _("\
480 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
481 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
482 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
483 -F --target <bfdname> Set both input and output format to <bfdname>\n\
484 --debugging Convert debugging information, if possible\n\
485 -p --preserve-dates Copy modified/access timestamps to the output\n\
486 -D --enable-deterministic-archives\n\
487 Produce deterministic output when stripping archives\n\
488 -j --only-section <name> Only copy section <name> into the output\n\
489 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
490 -R --remove-section <name> Remove section <name> from the output\n\
491 -S --strip-all Remove all symbol and relocation information\n\
492 -g --strip-debug Remove all debugging symbols & sections\n\
493 --strip-unneeded Remove all symbols not needed by relocations\n\
494 -N --strip-symbol <name> Do not copy symbol <name>\n\
495 --strip-unneeded-symbol <name>\n\
496 Do not copy symbol <name> unless needed by\n\
498 --only-keep-debug Strip everything but the debug information\n\
499 --extract-symbol Remove section contents but keep symbols\n\
500 -K --keep-symbol <name> Do not strip symbol <name>\n\
501 --keep-file-symbols Do not strip file symbol(s)\n\
502 --localize-hidden Turn all ELF hidden symbols into locals\n\
503 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
504 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
505 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
506 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
507 --weaken Force all global symbols to be marked as weak\n\
508 -w --wildcard Permit wildcard in symbol comparison\n\
509 -x --discard-all Remove all non-global symbols\n\
510 -X --discard-locals Remove any compiler-generated symbols\n\
511 -i --interleave [<number>] Only copy N out of every <number> bytes\n\
512 --interleave-width <number> Set N for --interleave\n\
513 -b --byte <num> Select byte <num> in every interleaved block\n\
514 --gap-fill <val> Fill gaps between sections with <val>\n\
515 --pad-to <addr> Pad the last section up to address <addr>\n\
516 --set-start <addr> Set the start address to <addr>\n\
517 {--change-start|--adjust-start} <incr>\n\
518 Add <incr> to the start address\n\
519 {--change-addresses|--adjust-vma} <incr>\n\
520 Add <incr> to LMA, VMA and start addresses\n\
521 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
522 Change LMA and VMA of section <name> by <val>\n\
523 --change-section-lma <name>{=|+|-}<val>\n\
524 Change the LMA of section <name> by <val>\n\
525 --change-section-vma <name>{=|+|-}<val>\n\
526 Change the VMA of section <name> by <val>\n\
527 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
528 Warn if a named section does not exist\n\
529 --set-section-flags <name>=<flags>\n\
530 Set section <name>'s properties to <flags>\n\
531 --add-section <name>=<file> Add section <name> found in <file> to output\n\
532 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
533 --long-section-names {enable|disable|keep}\n\
534 Handle long section names in Coff objects.\n\
535 --change-leading-char Force output format's leading character style\n\
536 --remove-leading-char Remove leading character from global symbols\n\
537 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
538 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
539 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
541 --srec-len <number> Restrict the length of generated Srecords\n\
542 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
543 --strip-symbols <file> -N for all symbols listed in <file>\n\
544 --strip-unneeded-symbols <file>\n\
545 --strip-unneeded-symbol for all symbols listed\n\
547 --keep-symbols <file> -K for all symbols listed in <file>\n\
548 --localize-symbols <file> -L for all symbols listed in <file>\n\
549 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
550 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
551 --weaken-symbols <file> -W for all symbols listed in <file>\n\
552 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
553 --writable-text Mark the output text as writable\n\
554 --readonly-text Make the output text write protected\n\
555 --pure Mark the output file as demand paged\n\
556 --impure Mark the output file as impure\n\
557 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
558 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
559 --prefix-alloc-sections <prefix>\n\
560 Add <prefix> to start of every allocatable\n\
562 --file-alignment <num> Set PE file alignment to <num>\n\
563 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
565 --image-base <address> Set PE image base to <address>\n\
566 --section-alignment <num> Set PE section alignment to <num>\n\
567 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
569 --subsystem <name>[:<version>]\n\
570 Set PE subsystem to <name> [& <version>]\n\
571 --compress-debug-sections Compress DWARF debug sections using zlib\n\
572 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
573 -v --verbose List all object files modified\n\
574 @<file> Read options from <file>\n\
575 -V --version Display this program's version number\n\
576 -h --help Display this output\n\
577 --info List object formats & architectures supported\n\
579 list_supported_targets (program_name
, stream
);
580 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
581 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
586 strip_usage (FILE *stream
, int exit_status
)
588 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
589 fprintf (stream
, _(" Removes symbols and sections from files\n"));
590 fprintf (stream
, _(" The options are:\n"));
591 fprintf (stream
, _("\
592 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
593 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
594 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
595 -p --preserve-dates Copy modified/access timestamps to the output\n\
596 -D --enable-deterministic-archives\n\
597 Produce deterministic output when stripping archives\n\
598 -R --remove-section=<name> Remove section <name> from the output\n\
599 -s --strip-all Remove all symbol and relocation information\n\
600 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
601 --strip-unneeded Remove all symbols not needed by relocations\n\
602 --only-keep-debug Strip everything but the debug information\n\
603 -N --strip-symbol=<name> Do not copy symbol <name>\n\
604 -K --keep-symbol=<name> Do not strip symbol <name>\n\
605 --keep-file-symbols Do not strip file symbol(s)\n\
606 -w --wildcard Permit wildcard in symbol comparison\n\
607 -x --discard-all Remove all non-global symbols\n\
608 -X --discard-locals Remove any compiler-generated symbols\n\
609 -v --verbose List all object files modified\n\
610 -V --version Display this program's version number\n\
611 -h --help Display this output\n\
612 --info List object formats & architectures supported\n\
613 -o <file> Place stripped output into <file>\n\
616 list_supported_targets (program_name
, stream
);
617 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
618 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
622 /* Parse section flags into a flagword, with a fatal error if the
623 string can't be parsed. */
626 parse_flags (const char *s
)
636 snext
= strchr (s
, ',');
646 #define PARSE_FLAG(fname,fval) \
647 else if (strncasecmp (fname, s, len) == 0) ret |= fval
648 PARSE_FLAG ("alloc", SEC_ALLOC
);
649 PARSE_FLAG ("load", SEC_LOAD
);
650 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
651 PARSE_FLAG ("readonly", SEC_READONLY
);
652 PARSE_FLAG ("debug", SEC_DEBUGGING
);
653 PARSE_FLAG ("code", SEC_CODE
);
654 PARSE_FLAG ("data", SEC_DATA
);
655 PARSE_FLAG ("rom", SEC_ROM
);
656 PARSE_FLAG ("share", SEC_COFF_SHARED
);
657 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
663 copy
= (char *) xmalloc (len
+ 1);
664 strncpy (copy
, s
, len
);
666 non_fatal (_("unrecognized section flag `%s'"), copy
);
667 fatal (_("supported flags: %s"),
668 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
678 /* Find and optionally add an entry in the change_sections list. */
680 static struct section_list
*
681 find_section_list (const char *name
, bfd_boolean add
)
683 struct section_list
*p
;
685 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
686 if (strcmp (p
->name
, name
) == 0)
692 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
697 p
->change_vma
= CHANGE_IGNORE
;
698 p
->change_lma
= CHANGE_IGNORE
;
701 p
->set_flags
= FALSE
;
704 p
->next
= change_sections
;
710 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
713 eq_string (const void *s1
, const void *s2
)
715 return strcmp ((const char *) s1
, (const char *) s2
) == 0;
719 create_symbol_htab (void)
721 return htab_create_alloc (16, htab_hash_string
, eq_string
, NULL
, xcalloc
, free
);
725 create_symbol_htabs (void)
727 strip_specific_htab
= create_symbol_htab ();
728 strip_unneeded_htab
= create_symbol_htab ();
729 keep_specific_htab
= create_symbol_htab ();
730 localize_specific_htab
= create_symbol_htab ();
731 globalize_specific_htab
= create_symbol_htab ();
732 keepglobal_specific_htab
= create_symbol_htab ();
733 weaken_specific_htab
= create_symbol_htab ();
736 /* Add a symbol to strip_specific_list. */
739 add_specific_symbol (const char *name
, htab_t htab
)
741 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
744 /* Add symbols listed in `filename' to strip_specific_list. */
746 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
747 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
750 add_specific_symbols (const char *filename
, htab_t htab
)
756 unsigned int line_count
;
758 size
= get_file_size (filename
);
765 buffer
= (char *) xmalloc (size
+ 2);
766 f
= fopen (filename
, FOPEN_RT
);
768 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
770 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
771 fatal (_("%s: fread failed"), filename
);
774 buffer
[size
] = '\n';
775 buffer
[size
+ 1] = '\0';
779 for (line
= buffer
; * line
!= '\0'; line
++)
784 int finished
= FALSE
;
786 for (eol
= line
;; eol
++)
792 /* Cope with \n\r. */
800 /* Cope with \r\n. */
811 /* Line comment, Terminate the line here, in case a
812 name is present and then allow the rest of the
813 loop to find the real end of the line. */
825 /* A name may now exist somewhere between 'line' and 'eol'.
826 Strip off leading whitespace and trailing whitespace,
827 then add it to the list. */
828 for (name
= line
; IS_WHITESPACE (* name
); name
++)
830 for (name_end
= name
;
831 (! IS_WHITESPACE (* name_end
))
832 && (! IS_LINE_TERMINATOR (* name_end
));
836 if (! IS_LINE_TERMINATOR (* name_end
))
840 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
843 if (! IS_LINE_TERMINATOR (* extra
))
844 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
845 filename
, line_count
);
851 add_specific_symbol (name
, htab
);
853 /* Advance line pointer to end of line. The 'eol ++' in the for
854 loop above will then advance us to the start of the next line. */
860 /* See whether a symbol should be stripped or kept
861 based on strip_specific_list and keep_symbols. */
864 is_specified_symbol_predicate (void **slot
, void *data
)
866 struct is_specified_symbol_predicate_data
*d
=
867 (struct is_specified_symbol_predicate_data
*) data
;
868 const char *slot_name
= (char *) *slot
;
870 if (*slot_name
!= '!')
872 if (! fnmatch (slot_name
, d
->name
, 0))
875 /* Stop traversal. */
881 if (fnmatch (slot_name
+ 1, d
->name
, 0))
884 /* Stop traversal. */
889 /* Continue traversal. */
894 is_specified_symbol (const char *name
, htab_t htab
)
898 struct is_specified_symbol_predicate_data data
;
903 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
908 return htab_find (htab
, name
) != NULL
;
911 /* Return a pointer to the symbol used as a signature for GROUP. */
914 group_signature (asection
*group
)
916 bfd
*abfd
= group
->owner
;
917 Elf_Internal_Shdr
*ghdr
;
919 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
922 ghdr
= &elf_section_data (group
)->this_hdr
;
923 if (ghdr
->sh_link
< elf_numsections (abfd
))
925 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
926 Elf_Internal_Shdr
*symhdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
928 if (symhdr
->sh_type
== SHT_SYMTAB
929 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
930 return isympp
[ghdr
->sh_info
- 1];
935 /* See if a non-group section is being removed. */
938 is_strip_section_1 (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
940 if (sections_removed
|| sections_copied
)
942 struct section_list
*p
;
944 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
946 if (sections_removed
&& p
!= NULL
&& p
->remove
)
948 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
952 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
954 if (strip_symbols
== STRIP_DEBUG
955 || strip_symbols
== STRIP_UNNEEDED
956 || strip_symbols
== STRIP_ALL
957 || discard_locals
== LOCALS_ALL
958 || convert_debugging
)
961 if (strip_symbols
== STRIP_NONDEBUG
)
968 /* See if a section is being removed. */
971 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
973 if (is_strip_section_1 (abfd
, sec
))
976 if ((bfd_get_section_flags (abfd
, sec
) & SEC_GROUP
) != 0)
980 asection
*elt
, *first
;
983 If we are going to strip the group signature symbol, then
984 strip the group section too. */
985 gsym
= group_signature (sec
);
990 if ((strip_symbols
== STRIP_ALL
991 && !is_specified_symbol (gname
, keep_specific_htab
))
992 || is_specified_symbol (gname
, strip_specific_htab
))
995 /* Remove the group section if all members are removed. */
996 first
= elt
= elf_next_in_group (sec
);
999 if (!is_strip_section_1 (abfd
, elt
))
1001 elt
= elf_next_in_group (elt
);
1012 /* Return true if SYM is a hidden symbol. */
1015 is_hidden_symbol (asymbol
*sym
)
1017 elf_symbol_type
*elf_sym
;
1019 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
1020 if (elf_sym
!= NULL
)
1021 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
1030 /* Choose which symbol entries to copy; put the result in OSYMS.
1031 We don't copy in place, because that confuses the relocs.
1032 Return the number of symbols to print. */
1035 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
1036 asymbol
**isyms
, long symcount
)
1038 asymbol
**from
= isyms
, **to
= osyms
;
1039 long src_count
= 0, dst_count
= 0;
1040 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
1042 for (; src_count
< symcount
; src_count
++)
1044 asymbol
*sym
= from
[src_count
];
1045 flagword flags
= sym
->flags
;
1046 char *name
= (char *) bfd_asymbol_name (sym
);
1048 bfd_boolean used_in_reloc
= FALSE
;
1049 bfd_boolean undefined
;
1050 bfd_boolean rem_leading_char
;
1051 bfd_boolean add_leading_char
;
1053 undefined
= bfd_is_und_section (bfd_get_section (sym
));
1055 if (redefine_sym_list
)
1057 char *old_name
, *new_name
;
1059 old_name
= (char *) bfd_asymbol_name (sym
);
1060 new_name
= (char *) lookup_sym_redefinition (old_name
);
1061 bfd_asymbol_name (sym
) = new_name
;
1065 /* Check if we will remove the current leading character. */
1067 (name
[0] == bfd_get_symbol_leading_char (abfd
))
1068 && (change_leading_char
1069 || (remove_leading_char
1070 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1072 || bfd_is_com_section (bfd_get_section (sym
)))));
1074 /* Check if we will add a new leading character. */
1077 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1078 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1079 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1081 /* Short circuit for change_leading_char if we can do it in-place. */
1082 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1084 name
[0] = bfd_get_symbol_leading_char (obfd
);
1085 bfd_asymbol_name (sym
) = name
;
1086 rem_leading_char
= FALSE
;
1087 add_leading_char
= FALSE
;
1090 /* Remove leading char. */
1091 if (rem_leading_char
)
1092 bfd_asymbol_name (sym
) = ++name
;
1094 /* Add new leading char and/or prefix. */
1095 if (add_leading_char
|| prefix_symbols_string
)
1099 ptr
= n
= (char *) xmalloc (1 + strlen (prefix_symbols_string
)
1100 + strlen (name
) + 1);
1101 if (add_leading_char
)
1102 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1104 if (prefix_symbols_string
)
1106 strcpy (ptr
, prefix_symbols_string
);
1107 ptr
+= strlen (prefix_symbols_string
);
1111 bfd_asymbol_name (sym
) = n
;
1115 if (strip_symbols
== STRIP_ALL
)
1117 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1118 || ((flags
& BSF_SECTION_SYM
) != 0
1119 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
1123 used_in_reloc
= TRUE
;
1125 else if (relocatable
/* Relocatable file. */
1126 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1127 || bfd_is_com_section (bfd_get_section (sym
))))
1129 else if (bfd_decode_symclass (sym
) == 'I')
1130 /* Global symbols in $idata sections need to be retained
1131 even if relocatable is FALSE. External users of the
1132 library containing the $idata section may reference these
1135 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1136 || (flags
& BSF_WEAK
) != 0
1138 || bfd_is_com_section (bfd_get_section (sym
)))
1139 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1140 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1141 keep
= (strip_symbols
!= STRIP_DEBUG
1142 && strip_symbols
!= STRIP_UNNEEDED
1143 && ! convert_debugging
);
1144 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
1145 /* COMDAT sections store special information in local
1146 symbols, so we cannot risk stripping any of them. */
1148 else /* Local symbol. */
1149 keep
= (strip_symbols
!= STRIP_UNNEEDED
1150 && (discard_locals
!= LOCALS_ALL
1151 && (discard_locals
!= LOCALS_START_L
1152 || ! bfd_is_local_label (abfd
, sym
))));
1154 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1156 /* There are multiple ways to set 'keep' above, but if it
1157 was the relocatable symbol case, then that's an error. */
1160 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1168 && !(flags
& BSF_KEEP
)
1169 && is_specified_symbol (name
, strip_unneeded_htab
))
1173 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1174 || is_specified_symbol (name
, keep_specific_htab
)))
1177 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
1182 if ((flags
& BSF_GLOBAL
) != 0
1183 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1185 sym
->flags
&= ~ BSF_GLOBAL
;
1186 sym
->flags
|= BSF_WEAK
;
1190 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1191 && (is_specified_symbol (name
, localize_specific_htab
)
1192 || (htab_elements (keepglobal_specific_htab
) != 0
1193 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1194 || (localize_hidden
&& is_hidden_symbol (sym
))))
1196 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1197 sym
->flags
|= BSF_LOCAL
;
1201 && (flags
& BSF_LOCAL
)
1202 && is_specified_symbol (name
, globalize_specific_htab
))
1204 sym
->flags
&= ~ BSF_LOCAL
;
1205 sym
->flags
|= BSF_GLOBAL
;
1208 to
[dst_count
++] = sym
;
1212 to
[dst_count
] = NULL
;
1217 /* Find the redefined name of symbol SOURCE. */
1220 lookup_sym_redefinition (const char *source
)
1222 struct redefine_node
*list
;
1224 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1225 if (strcmp (source
, list
->source
) == 0)
1226 return list
->target
;
1231 /* Add a node to a symbol redefine list. */
1234 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1236 struct redefine_node
**p
;
1237 struct redefine_node
*list
;
1238 struct redefine_node
*new_node
;
1240 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1242 if (strcmp (source
, list
->source
) == 0)
1243 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1246 if (strcmp (target
, list
->target
) == 0)
1247 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1251 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
1253 new_node
->source
= strdup (source
);
1254 new_node
->target
= strdup (target
);
1255 new_node
->next
= NULL
;
1260 /* Handle the --redefine-syms option. Read lines containing "old new"
1261 from the file, and add them to the symbol redefine list. */
1264 add_redefine_syms_file (const char *filename
)
1273 file
= fopen (filename
, "r");
1275 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1276 filename
, strerror (errno
));
1279 buf
= (char *) xmalloc (bufsize
+ 1 /* For the terminating NUL. */);
1287 /* Collect the input symbol name. */
1288 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1296 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1304 /* Eat white space between the symbol names. */
1305 while (IS_WHITESPACE (c
))
1307 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1312 /* Collect the output symbol name. */
1314 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1322 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1330 /* Eat white space at end of line. */
1331 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1336 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1337 || c
== '\n' || c
== EOF
)
1340 /* Append the redefinition to the list. */
1342 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1353 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1355 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1356 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1359 /* Eat the rest of the line and finish it. */
1360 while (c
!= '\n' && c
!= EOF
)
1366 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1371 /* Copy unkown object file IBFD onto OBFD.
1372 Returns TRUE upon success, FALSE otherwise. */
1375 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1383 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1385 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1392 non_fatal (_("stat returns negative size for `%s'"),
1393 bfd_get_archive_filename (ibfd
));
1397 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1399 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1404 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1405 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1407 cbuf
= (char *) xmalloc (BUFSIZE
);
1409 while (ncopied
< size
)
1411 tocopy
= size
- ncopied
;
1412 if (tocopy
> BUFSIZE
)
1415 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1416 != (bfd_size_type
) tocopy
)
1418 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1423 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1424 != (bfd_size_type
) tocopy
)
1426 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1434 /* We should at least to be able to read it back when copying an
1435 unknown object in an archive. */
1436 chmod (bfd_get_filename (obfd
), buf
.st_mode
| S_IRUSR
);
1441 /* Copy object file IBFD onto OBFD.
1442 Returns TRUE upon success, FALSE otherwise. */
1445 copy_object (bfd
*ibfd
, bfd
*obfd
, const bfd_arch_info_type
*input_arch
)
1449 asection
**osections
= NULL
;
1450 asection
*gnu_debuglink_section
= NULL
;
1451 bfd_size_type
*gaps
= NULL
;
1452 bfd_size_type max_gap
= 0;
1455 enum bfd_architecture iarch
;
1458 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1459 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1460 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1461 fatal (_("Unable to change endianness of input file(s)"));
1463 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1465 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1470 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1471 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1472 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1481 start
= bfd_get_start_address (ibfd
);
1482 start
+= change_start
;
1485 /* Neither the start address nor the flags
1486 need to be set for a core file. */
1487 if (bfd_get_format (obfd
) != bfd_core
)
1491 flags
= bfd_get_file_flags (ibfd
);
1492 flags
|= bfd_flags_to_set
;
1493 flags
&= ~bfd_flags_to_clear
;
1494 flags
&= bfd_applicable_file_flags (obfd
);
1496 if (strip_symbols
== STRIP_ALL
)
1497 flags
&= ~HAS_RELOC
;
1499 if (!bfd_set_start_address (obfd
, start
)
1500 || !bfd_set_file_flags (obfd
, flags
))
1502 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1507 /* Copy architecture of input file to output file. */
1508 iarch
= bfd_get_arch (ibfd
);
1509 imach
= bfd_get_mach (ibfd
);
1512 if (bfd_get_arch_info (ibfd
) == NULL
1513 || bfd_get_arch_info (ibfd
)->arch
== bfd_arch_unknown
)
1515 iarch
= input_arch
->arch
;
1516 imach
= input_arch
->mach
;
1519 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1520 bfd_get_archive_filename (ibfd
));
1522 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1523 && (ibfd
->target_defaulted
1524 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1526 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1527 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1528 bfd_get_archive_filename (ibfd
));
1530 non_fatal (_("Output file cannot represent architecture `%s'"),
1531 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1532 bfd_get_mach (ibfd
)));
1536 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1538 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1542 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1543 && bfd_pei_p (obfd
))
1545 /* Set up PE parameters. */
1546 pe_data_type
*pe
= pe_data (obfd
);
1548 /* Copy PE parameters before changing them. */
1549 if (ibfd
->xvec
->flavour
== bfd_target_coff_flavour
1550 && bfd_pei_p (ibfd
))
1551 pe
->pe_opthdr
= pe_data (ibfd
)->pe_opthdr
;
1553 if (pe_file_alignment
!= (bfd_vma
) -1)
1554 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
1556 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
1558 if (pe_heap_commit
!= (bfd_vma
) -1)
1559 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
1561 if (pe_heap_reserve
!= (bfd_vma
) -1)
1562 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
1564 if (pe_image_base
!= (bfd_vma
) -1)
1565 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
1567 if (pe_section_alignment
!= (bfd_vma
) -1)
1568 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
1570 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
1572 if (pe_stack_commit
!= (bfd_vma
) -1)
1573 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
1575 if (pe_stack_reserve
!= (bfd_vma
) -1)
1576 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_reserve
;
1578 if (pe_subsystem
!= -1)
1579 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
1581 if (pe_major_subsystem_version
!= -1)
1582 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
1584 if (pe_minor_subsystem_version
!= -1)
1585 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
1587 if (pe_file_alignment
> pe_section_alignment
)
1589 char file_alignment
[20], section_alignment
[20];
1591 sprintf_vma (file_alignment
, pe_file_alignment
);
1592 sprintf_vma (section_alignment
, pe_section_alignment
);
1593 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1595 file_alignment
, section_alignment
);
1602 if (osympp
!= isympp
)
1608 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1611 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1615 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1616 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1619 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1623 /* BFD mandates that all output sections be created and sizes set before
1624 any output is done. Thus, we traverse all sections multiple times. */
1625 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1627 if (!extract_symbol
)
1628 setup_bfd_headers (ibfd
, obfd
);
1630 if (add_sections
!= NULL
)
1632 struct section_add
*padd
;
1633 struct section_list
*pset
;
1635 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1639 pset
= find_section_list (padd
->name
, FALSE
);
1643 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1644 if (pset
!= NULL
&& pset
->set_flags
)
1645 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1647 /* bfd_make_section_with_flags() does not return very helpful
1648 error codes, so check for the most likely user error first. */
1649 if (bfd_get_section_by_name (obfd
, padd
->name
))
1651 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1652 _("can't add section '%s'"), padd
->name
);
1657 /* We use LINKER_CREATED here so that the backend hooks
1658 will create any special section type information,
1659 instead of presuming we know what we're doing merely
1660 because we set the flags. */
1661 padd
->section
= bfd_make_section_with_flags
1662 (obfd
, padd
->name
, flags
| SEC_LINKER_CREATED
);
1663 if (padd
->section
== NULL
)
1665 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1666 _("can't create section `%s'"),
1672 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1674 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1680 if (pset
->change_vma
!= CHANGE_IGNORE
)
1681 if (! bfd_set_section_vma (obfd
, padd
->section
,
1684 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1688 if (pset
->change_lma
!= CHANGE_IGNORE
)
1690 padd
->section
->lma
= pset
->lma_val
;
1692 if (! bfd_set_section_alignment
1693 (obfd
, padd
->section
,
1694 bfd_section_alignment (obfd
, padd
->section
)))
1696 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1704 if (gnu_debuglink_filename
!= NULL
)
1706 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1707 (obfd
, gnu_debuglink_filename
);
1709 if (gnu_debuglink_section
== NULL
)
1711 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1712 _("cannot create debug link section `%s'"),
1713 gnu_debuglink_filename
);
1717 /* Special processing for PE format files. We
1718 have no way to distinguish PE from COFF here. */
1719 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1721 bfd_vma debuglink_vma
;
1722 asection
* highest_section
;
1725 /* The PE spec requires that all sections be adjacent and sorted
1726 in ascending order of VMA. It also specifies that debug
1727 sections should be last. This is despite the fact that debug
1728 sections are not loaded into memory and so in theory have no
1731 This means that the debuglink section must be given a non-zero
1732 VMA which makes it contiguous with other debug sections. So
1733 walk the current section list, find the section with the
1734 highest VMA and start the debuglink section after that one. */
1735 for (sec
= obfd
->sections
, highest_section
= NULL
;
1739 && (highest_section
== NULL
1740 || sec
->vma
> highest_section
->vma
))
1741 highest_section
= sec
;
1743 if (highest_section
)
1744 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1745 + highest_section
->size
,
1746 /* FIXME: We ought to be using
1747 COFF_PAGE_SIZE here or maybe
1748 bfd_get_section_alignment() (if it
1749 was set) but since this is for PE
1750 and we know the required alignment
1751 it is easier just to hard code it. */
1754 /* Umm, not sure what to do in this case. */
1755 debuglink_vma
= 0x1000;
1757 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1761 if (bfd_count_sections (obfd
) != 0
1762 && (gap_fill_set
|| pad_to_set
))
1767 /* We must fill in gaps between the sections and/or we must pad
1768 the last section to a specified address. We do this by
1769 grabbing a list of the sections, sorting them by VMA, and
1770 increasing the section sizes as required to fill the gaps.
1771 We write out the gap contents below. */
1773 c
= bfd_count_sections (obfd
);
1774 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
1776 bfd_map_over_sections (obfd
, get_sections
, &set
);
1778 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1780 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
1781 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1785 for (i
= 0; i
< c
- 1; i
++)
1789 bfd_vma gap_start
, gap_stop
;
1791 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1792 if ((flags
& SEC_HAS_CONTENTS
) == 0
1793 || (flags
& SEC_LOAD
) == 0)
1796 size
= bfd_section_size (obfd
, osections
[i
]);
1797 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1798 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1799 if (gap_start
< gap_stop
)
1801 if (! bfd_set_section_size (obfd
, osections
[i
],
1802 size
+ (gap_stop
- gap_start
)))
1804 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
1805 _("Can't fill gap after section"));
1809 gaps
[i
] = gap_stop
- gap_start
;
1810 if (max_gap
< gap_stop
- gap_start
)
1811 max_gap
= gap_stop
- gap_start
;
1821 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1822 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1823 if (lma
+ size
< pad_to
)
1825 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1828 bfd_nonfatal_message (NULL
, obfd
, osections
[c
- 1],
1829 _("can't add padding"));
1834 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1835 if (max_gap
< pad_to
- (lma
+ size
))
1836 max_gap
= pad_to
- (lma
+ size
);
1842 /* Symbol filtering must happen after the output sections
1843 have been created, but before their contents are set. */
1845 if (convert_debugging
)
1846 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, FALSE
);
1848 if (strip_symbols
== STRIP_DEBUG
1849 || strip_symbols
== STRIP_ALL
1850 || strip_symbols
== STRIP_UNNEEDED
1851 || strip_symbols
== STRIP_NONDEBUG
1852 || discard_locals
!= LOCALS_UNDEF
1854 || htab_elements (strip_specific_htab
) != 0
1855 || htab_elements (keep_specific_htab
) != 0
1856 || htab_elements (localize_specific_htab
) != 0
1857 || htab_elements (globalize_specific_htab
) != 0
1858 || htab_elements (keepglobal_specific_htab
) != 0
1859 || htab_elements (weaken_specific_htab
) != 0
1860 || prefix_symbols_string
1863 || convert_debugging
1864 || change_leading_char
1865 || remove_leading_char
1866 || redefine_sym_list
1869 /* Mark symbols used in output relocations so that they
1870 are kept, even if they are local labels or static symbols.
1872 Note we iterate over the input sections examining their
1873 relocations since the relocations for the output sections
1874 haven't been set yet. mark_symbols_used_in_relocations will
1875 ignore input sections which have no corresponding output
1877 if (strip_symbols
!= STRIP_ALL
)
1878 bfd_map_over_sections (ibfd
,
1879 mark_symbols_used_in_relocations
,
1881 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1882 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1885 if (convert_debugging
&& dhandle
!= NULL
)
1887 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1894 bfd_set_symtab (obfd
, osympp
, symcount
);
1896 /* This has to happen before section positions are set. */
1897 bfd_map_over_sections (ibfd
, copy_relocations_in_section
, obfd
);
1899 /* This has to happen after the symbol table has been set. */
1900 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1902 if (add_sections
!= NULL
)
1904 struct section_add
*padd
;
1906 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1908 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1911 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1917 if (gnu_debuglink_filename
!= NULL
)
1919 if (! bfd_fill_in_gnu_debuglink_section
1920 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1922 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1923 _("cannot fill debug link section `%s'"),
1924 gnu_debuglink_filename
);
1929 if (gap_fill_set
|| pad_to_set
)
1934 /* Fill in the gaps. */
1937 buf
= (bfd_byte
*) xmalloc (max_gap
);
1938 memset (buf
, gap_fill
, max_gap
);
1940 c
= bfd_count_sections (obfd
);
1941 for (i
= 0; i
< c
; i
++)
1949 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1960 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1963 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
1974 /* Do not copy backend data if --extract-symbol is passed; anything
1975 that needs to look at the section contents will fail. */
1979 /* Allow the BFD backend to copy any private data it understands
1980 from the input BFD to the output BFD. This is done last to
1981 permit the routine to look at the filtered symbol table, which is
1982 important for the ECOFF code at least. */
1983 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1985 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1986 _("error copying private BFD data"));
1990 /* Switch to the alternate machine code. We have to do this at the
1991 very end, because we only initialize the header when we create
1992 the first section. */
1993 if (use_alt_mach_code
!= 0)
1995 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1997 non_fatal (_("this target does not support %lu alternative machine codes"),
1999 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2001 non_fatal (_("treating that number as an absolute e_machine value instead"));
2002 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
2005 non_fatal (_("ignoring the alternative value"));
2012 /* Read each archive element in turn from IBFD, copy the
2013 contents to temp file, and keep the temp file handle.
2014 If 'force_output_target' is TRUE then make sure that
2015 all elements in the new archive are of the type
2019 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
2020 bfd_boolean force_output_target
,
2021 const bfd_arch_info_type
*input_arch
)
2025 struct name_list
*next
;
2029 bfd
**ptr
= &obfd
->archive_head
;
2032 const char *filename
;
2034 /* Make a temp directory to hold the contents. */
2035 dir
= make_tempdir (bfd_get_filename (obfd
));
2037 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2040 if (strip_symbols
== STRIP_ALL
)
2041 obfd
->has_armap
= FALSE
;
2043 obfd
->has_armap
= ibfd
->has_armap
;
2044 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
2047 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
2051 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
2053 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2056 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
2060 while (!status
&& this_element
!= NULL
)
2066 int stat_status
= 0;
2067 bfd_boolean del
= TRUE
;
2068 bfd_boolean ok_object
;
2070 /* Create an output file for this member. */
2071 output_name
= concat (dir
, "/",
2072 bfd_get_filename (this_element
), (char *) 0);
2074 /* If the file already exists, make another temp dir. */
2075 if (stat (output_name
, &buf
) >= 0)
2077 output_name
= make_tempdir (output_name
);
2078 if (output_name
== NULL
)
2079 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2082 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
2083 l
->name
= output_name
;
2087 output_name
= concat (output_name
, "/",
2088 bfd_get_filename (this_element
), (char *) 0);
2093 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
2095 if (stat_status
!= 0)
2096 non_fatal (_("internal stat error on %s"),
2097 bfd_get_filename (this_element
));
2100 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
2101 l
->name
= output_name
;
2106 ok_object
= bfd_check_format (this_element
, bfd_object
);
2108 bfd_nonfatal_message (NULL
, this_element
, NULL
,
2109 _("Unable to recognise the format of file"));
2111 /* PR binutils/3110: Cope with archives
2112 containing multiple target types. */
2113 if (force_output_target
|| !ok_object
)
2114 output_bfd
= bfd_openw (output_name
, output_target
);
2116 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
2118 if (output_bfd
== NULL
)
2120 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2127 del
= !copy_object (this_element
, output_bfd
, input_arch
);
2129 if (del
&& bfd_get_arch (this_element
) == bfd_arch_unknown
)
2130 /* Try again as an unknown object file. */
2132 else if (!bfd_close (output_bfd
))
2134 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2135 /* Error in new object file. Don't change archive. */
2142 del
= !copy_unknown_object (this_element
, output_bfd
);
2143 if (!bfd_close_all_done (output_bfd
))
2145 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2146 /* Error in new object file. Don't change archive. */
2153 unlink (output_name
);
2158 if (preserve_dates
&& stat_status
== 0)
2159 set_times (output_name
, &buf
);
2161 /* Open the newly output file and attach to our list. */
2162 output_bfd
= bfd_openr (output_name
, output_target
);
2164 l
->obfd
= output_bfd
;
2167 ptr
= &output_bfd
->archive_next
;
2169 last_element
= this_element
;
2171 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
2173 bfd_close (last_element
);
2178 filename
= bfd_get_filename (obfd
);
2179 if (!bfd_close (obfd
))
2182 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2186 filename
= bfd_get_filename (ibfd
);
2187 if (!bfd_close (ibfd
))
2190 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2194 /* Delete all the files that we opened. */
2195 for (l
= list
; l
!= NULL
; l
= l
->next
)
2197 if (l
->obfd
== NULL
)
2201 bfd_close (l
->obfd
);
2209 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
2211 /* This is only relevant to Coff targets. */
2212 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
2215 && bfd_get_flavour (input_bfd
) == bfd_target_coff_flavour
)
2216 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
2217 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
2221 /* The top-level control. */
2224 copy_file (const char *input_filename
, const char *output_filename
,
2225 const char *input_target
, const char *output_target
,
2226 const bfd_arch_info_type
*input_arch
)
2229 char **obj_matching
;
2230 char **core_matching
;
2231 off_t size
= get_file_size (input_filename
);
2236 non_fatal (_("error: the input file '%s' is empty"),
2242 /* To allow us to do "strip *" without dying on the first
2243 non-object file, failures are nonfatal. */
2244 ibfd
= bfd_openr (input_filename
, input_target
);
2247 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2252 switch (do_debug_sections
)
2255 ibfd
->flags
|= BFD_COMPRESS
;
2258 ibfd
->flags
|= BFD_DECOMPRESS
;
2264 if (bfd_check_format (ibfd
, bfd_archive
))
2266 bfd_boolean force_output_target
;
2269 /* bfd_get_target does not return the correct value until
2270 bfd_check_format succeeds. */
2271 if (output_target
== NULL
)
2273 output_target
= bfd_get_target (ibfd
);
2274 force_output_target
= FALSE
;
2277 force_output_target
= TRUE
;
2279 obfd
= bfd_openw (output_filename
, output_target
);
2282 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2286 /* This is a no-op on non-Coff targets. */
2287 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2289 copy_archive (ibfd
, obfd
, output_target
, force_output_target
, input_arch
);
2291 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
2296 /* bfd_get_target does not return the correct value until
2297 bfd_check_format succeeds. */
2298 if (output_target
== NULL
)
2299 output_target
= bfd_get_target (ibfd
);
2301 obfd
= bfd_openw (output_filename
, output_target
);
2304 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2308 /* This is a no-op on non-Coff targets. */
2309 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2311 if (! copy_object (ibfd
, obfd
, input_arch
))
2314 if (!bfd_close (obfd
))
2317 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2321 if (!bfd_close (ibfd
))
2324 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2330 bfd_error_type obj_error
= bfd_get_error ();
2331 bfd_error_type core_error
;
2333 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
2335 /* This probably can't happen.. */
2336 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2337 free (obj_matching
);
2341 core_error
= bfd_get_error ();
2342 /* Report the object error in preference to the core error. */
2343 if (obj_error
!= core_error
)
2344 bfd_set_error (obj_error
);
2346 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2348 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2350 list_matching_formats (obj_matching
);
2351 free (obj_matching
);
2353 if (core_error
== bfd_error_file_ambiguously_recognized
)
2355 list_matching_formats (core_matching
);
2356 free (core_matching
);
2363 /* Add a name to the section renaming list. */
2366 add_section_rename (const char * old_name
, const char * new_name
,
2369 section_rename
* srename
;
2371 /* Check for conflicts first. */
2372 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
2373 if (strcmp (srename
->old_name
, old_name
) == 0)
2375 /* Silently ignore duplicate definitions. */
2376 if (strcmp (srename
->new_name
, new_name
) == 0
2377 && srename
->flags
== flags
)
2380 fatal (_("Multiple renames of section %s"), old_name
);
2383 srename
= (section_rename
*) xmalloc (sizeof (* srename
));
2385 srename
->old_name
= old_name
;
2386 srename
->new_name
= new_name
;
2387 srename
->flags
= flags
;
2388 srename
->next
= section_rename_list
;
2390 section_rename_list
= srename
;
2393 /* Check the section rename list for a new name of the input section
2394 ISECTION. Return the new name if one is found.
2395 Also set RETURNED_FLAGS to the flags to be used for this section. */
2398 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2399 flagword
* returned_flags
)
2401 const char * old_name
= bfd_section_name (ibfd
, isection
);
2402 section_rename
* srename
;
2404 /* Default to using the flags of the input section. */
2405 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2407 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
2408 if (strcmp (srename
->old_name
, old_name
) == 0)
2410 if (srename
->flags
!= (flagword
) -1)
2411 * returned_flags
= srename
->flags
;
2413 return srename
->new_name
;
2419 /* Once each of the sections is copied, we may still need to do some
2420 finalization work for private section headers. Do that here. */
2423 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2425 /* Allow the BFD backend to copy any private data it understands
2426 from the input section to the output section. */
2427 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2430 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
2431 _("error in private header data"));
2435 /* All went well. */
2439 /* Create a section in OBFD with the same
2440 name and attributes as ISECTION in IBFD. */
2443 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2445 bfd
*obfd
= (bfd
*) obfdarg
;
2446 struct section_list
*p
;
2454 char *prefix
= NULL
;
2455 bfd_boolean make_nobits
;
2457 if (is_strip_section (ibfd
, isection
))
2460 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2464 /* Get the, possibly new, name of the output section. */
2465 name
= find_section_rename (ibfd
, isection
, & flags
);
2467 /* Prefix sections. */
2468 if ((prefix_alloc_sections_string
)
2469 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2470 prefix
= prefix_alloc_sections_string
;
2471 else if (prefix_sections_string
)
2472 prefix
= prefix_sections_string
;
2478 n
= (char *) xmalloc (strlen (prefix
) + strlen (name
) + 1);
2484 make_nobits
= FALSE
;
2485 if (p
!= NULL
&& p
->set_flags
)
2486 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2487 else if (strip_symbols
== STRIP_NONDEBUG
2488 && (flags
& (SEC_ALLOC
| SEC_GROUP
)) != 0
2489 && !(ibfd
->xvec
->flavour
== bfd_target_elf_flavour
2490 && elf_section_type (isection
) == SHT_NOTE
))
2492 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
2493 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2497 /* Twiddle the input section flags so that it seems to
2498 elf.c:copy_private_bfd_data that section flags have not
2499 changed between input and output sections. This hack
2500 prevents wholesale rewriting of the program headers. */
2501 isection
->flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
2505 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2507 if (osection
== NULL
)
2509 err
= _("failed to create output section");
2514 elf_section_type (osection
) = SHT_NOBITS
;
2516 size
= bfd_section_size (ibfd
, isection
);
2518 size
= (size
+ interleave
- 1) / interleave
* copy_width
;
2519 else if (extract_symbol
)
2521 if (! bfd_set_section_size (obfd
, osection
, size
))
2523 err
= _("failed to set size");
2527 vma
= bfd_section_vma (ibfd
, isection
);
2528 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2530 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2533 vma
+= change_section_address
;
2535 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2537 err
= _("failed to set vma");
2541 lma
= isection
->lma
;
2542 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2544 if (p
->change_lma
== CHANGE_MODIFY
)
2546 else if (p
->change_lma
== CHANGE_SET
)
2552 lma
+= change_section_address
;
2554 osection
->lma
= lma
;
2556 /* FIXME: This is probably not enough. If we change the LMA we
2557 may have to recompute the header for the file as well. */
2558 if (!bfd_set_section_alignment (obfd
,
2560 bfd_section_alignment (ibfd
, isection
)))
2562 err
= _("failed to set alignment");
2566 /* Copy merge entity size. */
2567 osection
->entsize
= isection
->entsize
;
2569 /* This used to be mangle_section; we do here to avoid using
2570 bfd_get_section_by_name since some formats allow multiple
2571 sections with the same name. */
2572 isection
->output_section
= osection
;
2573 isection
->output_offset
= 0;
2575 /* Do not copy backend data if --extract-symbol is passed; anything
2576 that needs to look at the section contents will fail. */
2580 if ((isection
->flags
& SEC_GROUP
) != 0)
2582 asymbol
*gsym
= group_signature (isection
);
2586 gsym
->flags
|= BSF_KEEP
;
2587 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2588 elf_group_id (isection
) = gsym
;
2592 /* Allow the BFD backend to copy any private data it understands
2593 from the input section to the output section. */
2594 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2596 err
= _("failed to copy private data");
2600 /* All went well. */
2605 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
2608 /* Return TRUE if input section ISECTION should be skipped. */
2611 skip_section (bfd
*ibfd
, sec_ptr isection
)
2617 /* If we have already failed earlier on,
2618 do not keep on generating complaints now. */
2625 if (is_strip_section (ibfd
, isection
))
2628 flags
= bfd_get_section_flags (ibfd
, isection
);
2629 if ((flags
& SEC_GROUP
) != 0)
2632 osection
= isection
->output_section
;
2633 size
= bfd_get_section_size (isection
);
2635 if (size
== 0 || osection
== 0)
2641 /* Copy relocations in input section ISECTION of IBFD to an output
2642 section with the same name in OBFDARG. If stripping then don't
2643 copy any relocation info. */
2646 copy_relocations_in_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2648 bfd
*obfd
= (bfd
*) obfdarg
;
2654 if (skip_section (ibfd
, isection
))
2657 osection
= isection
->output_section
;
2659 /* Core files do not need to be relocated. */
2660 if (bfd_get_format (obfd
) == bfd_core
)
2664 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2668 /* Do not complain if the target does not support relocations. */
2669 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2674 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2681 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2684 relpp
= (arelent
**) xmalloc (relsize
);
2685 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2689 bfd_nonfatal_message (NULL
, ibfd
, isection
,
2690 _("relocation count is negative"));
2694 if (strip_symbols
== STRIP_ALL
)
2696 /* Remove relocations which are not in
2697 keep_strip_specific_list. */
2698 arelent
**temp_relpp
;
2699 long temp_relcount
= 0;
2702 temp_relpp
= (arelent
**) xmalloc (relsize
);
2703 for (i
= 0; i
< relcount
; i
++)
2704 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2705 keep_specific_htab
))
2706 temp_relpp
[temp_relcount
++] = relpp
[i
];
2707 relcount
= temp_relcount
;
2712 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2715 osection
->flags
&= ~SEC_RELOC
;
2721 /* Copy the data of input section ISECTION of IBFD
2722 to an output section with the same name in OBFD. */
2725 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2727 bfd
*obfd
= (bfd
*) obfdarg
;
2728 struct section_list
*p
;
2732 if (skip_section (ibfd
, isection
))
2735 osection
= isection
->output_section
;
2736 size
= bfd_get_section_size (isection
);
2738 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2740 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2741 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2743 bfd_byte
*memhunk
= NULL
;
2745 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
))
2748 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2754 /* We don't handle leftover bytes (too many possible behaviors,
2755 and we don't know what the user wants). The section length
2756 must be a multiple of the number of bytes to swap. */
2757 if ((size
% reverse_bytes
) == 0)
2762 for (i
= 0; i
< size
; i
+= reverse_bytes
)
2763 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
2765 bfd_byte
*m
= (bfd_byte
*) memhunk
;
2768 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
2769 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
2773 /* User must pad the section up in order to do this. */
2774 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2775 bfd_section_name (ibfd
, isection
), reverse_bytes
);
2780 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2781 char *from
= (char *) memhunk
+ copy_byte
;
2782 char *to
= (char *) memhunk
;
2783 char *end
= (char *) memhunk
+ size
;
2786 for (; from
< end
; from
+= interleave
)
2787 for (i
= 0; i
< copy_width
; i
++)
2790 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
* copy_width
;
2791 osection
->lma
/= interleave
;
2794 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2797 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2802 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2804 void *memhunk
= xmalloc (size
);
2806 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2807 flag--they can just remove the section entirely and add it
2808 back again. However, we do permit them to turn on the
2809 SEC_HAS_CONTENTS flag, and take it to mean that the section
2810 contents should be zeroed out. */
2812 memset (memhunk
, 0, size
);
2813 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2816 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2823 /* Get all the sections. This is used when --gap-fill or --pad-to is
2827 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2829 asection
***secppp
= (asection
***) secppparg
;
2831 **secppp
= osection
;
2835 /* Sort sections by VMA. This is called via qsort, and is used when
2836 --gap-fill or --pad-to is used. We force non loadable or empty
2837 sections to the front, where they are easier to ignore. */
2840 compare_section_lma (const void *arg1
, const void *arg2
)
2842 const asection
*const *sec1
= (const asection
* const *) arg1
;
2843 const asection
*const *sec2
= (const asection
* const *) arg2
;
2844 flagword flags1
, flags2
;
2846 /* Sort non loadable sections to the front. */
2847 flags1
= (*sec1
)->flags
;
2848 flags2
= (*sec2
)->flags
;
2849 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2850 || (flags1
& SEC_LOAD
) == 0)
2852 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2853 && (flags2
& SEC_LOAD
) != 0)
2858 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2859 || (flags2
& SEC_LOAD
) == 0)
2863 /* Sort sections by LMA. */
2864 if ((*sec1
)->lma
> (*sec2
)->lma
)
2866 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2869 /* Sort sections with the same LMA by size. */
2870 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2872 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2878 /* Mark all the symbols which will be used in output relocations with
2879 the BSF_KEEP flag so that those symbols will not be stripped.
2881 Ignore relocations which will not appear in the output file. */
2884 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2886 asymbol
**symbols
= (asymbol
**) symbolsarg
;
2891 /* Ignore an input section with no corresponding output section. */
2892 if (isection
->output_section
== NULL
)
2895 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2898 /* Do not complain if the target does not support relocations. */
2899 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2901 bfd_fatal (bfd_get_filename (ibfd
));
2907 relpp
= (arelent
**) xmalloc (relsize
);
2908 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2910 bfd_fatal (bfd_get_filename (ibfd
));
2912 /* Examine each symbol used in a relocation. If it's not one of the
2913 special bfd section symbols, then mark it with BSF_KEEP. */
2914 for (i
= 0; i
< relcount
; i
++)
2916 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2917 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2918 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2919 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2926 /* Write out debugging information. */
2929 write_debugging_info (bfd
*obfd
, void *dhandle
,
2930 long *symcountp ATTRIBUTE_UNUSED
,
2931 asymbol
***symppp ATTRIBUTE_UNUSED
)
2933 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2934 return write_ieee_debugging_info (obfd
, dhandle
);
2936 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2937 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2939 bfd_byte
*syms
, *strings
;
2940 bfd_size_type symsize
, stringsize
;
2941 asection
*stabsec
, *stabstrsec
;
2944 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2949 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2950 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2951 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2953 || stabstrsec
== NULL
2954 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2955 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2956 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2957 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2959 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2960 _("can't create debugging section"));
2964 /* We can get away with setting the section contents now because
2965 the next thing the caller is going to do is copy over the
2966 real sections. We may someday have to split the contents
2967 setting out of this function. */
2968 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2969 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2972 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2973 _("can't set debugging section contents"));
2980 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2981 _("don't know how to write debugging information for %s"),
2982 bfd_get_target (obfd
));
2987 strip_main (int argc
, char *argv
[])
2989 char *input_target
= NULL
;
2990 char *output_target
= NULL
;
2991 bfd_boolean show_version
= FALSE
;
2992 bfd_boolean formats_info
= FALSE
;
2995 struct section_list
*p
;
2996 char *output_file
= NULL
;
2998 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2999 strip_options
, (int *) 0)) != EOF
)
3004 input_target
= optarg
;
3007 output_target
= optarg
;
3010 input_target
= output_target
= optarg
;
3013 p
= find_section_list (optarg
, TRUE
);
3015 sections_removed
= TRUE
;
3018 strip_symbols
= STRIP_ALL
;
3022 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
3023 strip_symbols
= STRIP_DEBUG
;
3025 case OPTION_STRIP_UNNEEDED
:
3026 strip_symbols
= STRIP_UNNEEDED
;
3029 add_specific_symbol (optarg
, keep_specific_htab
);
3032 add_specific_symbol (optarg
, strip_specific_htab
);
3035 output_file
= optarg
;
3038 preserve_dates
= TRUE
;
3041 deterministic
= TRUE
;
3044 discard_locals
= LOCALS_ALL
;
3047 discard_locals
= LOCALS_START_L
;
3053 show_version
= TRUE
;
3055 case OPTION_FORMATS_INFO
:
3056 formats_info
= TRUE
;
3058 case OPTION_ONLY_KEEP_DEBUG
:
3059 strip_symbols
= STRIP_NONDEBUG
;
3061 case OPTION_KEEP_FILE_SYMBOLS
:
3062 keep_file_symbols
= 1;
3065 /* We've been given a long option. */
3072 strip_usage (stdout
, 0);
3074 strip_usage (stderr
, 1);
3085 print_version ("strip");
3087 /* Default is to strip all symbols. */
3088 if (strip_symbols
== STRIP_UNDEF
3089 && discard_locals
== LOCALS_UNDEF
3090 && htab_elements (strip_specific_htab
) == 0)
3091 strip_symbols
= STRIP_ALL
;
3093 if (output_target
== NULL
)
3094 output_target
= input_target
;
3098 || (output_file
!= NULL
&& (i
+ 1) < argc
))
3099 strip_usage (stderr
, 1);
3101 for (; i
< argc
; i
++)
3103 int hold_status
= status
;
3104 struct stat statbuf
;
3107 if (get_file_size (argv
[i
]) < 1)
3114 /* No need to check the return value of stat().
3115 It has already been checked in get_file_size(). */
3116 stat (argv
[i
], &statbuf
);
3118 if (output_file
== NULL
3119 || filename_cmp (argv
[i
], output_file
) == 0)
3120 tmpname
= make_tempname (argv
[i
]);
3122 tmpname
= output_file
;
3124 if (tmpname
== NULL
)
3126 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
3127 _("could not create temporary file to hold stripped copy"));
3133 copy_file (argv
[i
], tmpname
, input_target
, output_target
, NULL
);
3137 set_times (tmpname
, &statbuf
);
3138 if (output_file
!= tmpname
)
3139 status
= (smart_rename (tmpname
,
3140 output_file
? output_file
: argv
[i
],
3141 preserve_dates
) != 0);
3143 status
= hold_status
;
3146 unlink_if_ordinary (tmpname
);
3147 if (output_file
!= tmpname
)
3154 /* Set up PE subsystem. */
3157 set_pe_subsystem (const char *s
)
3159 const char *version
, *subsystem
;
3169 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
3170 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
3171 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
3172 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
3173 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
3174 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
3175 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
3176 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
3177 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
},
3178 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
3184 /* Check for the presence of a version number. */
3185 version
= strchr (s
, ':');
3186 if (version
== NULL
)
3190 int len
= version
- s
;
3194 version
= copy
+ 1 + len
;
3195 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
3197 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
3199 non_fatal (_("%s: bad version in PE subsystem"), s
);
3202 /* Check for numeric subsystem. */
3203 value
= (short) strtol (subsystem
, ©
, 0);
3206 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3207 if (v
[i
].value
== value
)
3209 pe_subsystem
= value
;
3210 set_def
= v
[i
].set_def
;
3216 /* Search for subsystem by name. */
3217 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3218 if (strcmp (subsystem
, v
[i
].name
) == 0)
3220 pe_subsystem
= v
[i
].value
;
3221 set_def
= v
[i
].set_def
;
3229 fatal (_("unknown PE subsystem: %s"), s
);
3234 if (pe_file_alignment
== (bfd_vma
) -1)
3235 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
3236 if (pe_section_alignment
== (bfd_vma
) -1)
3237 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
3241 free ((char *) subsystem
);
3244 /* Convert EFI target to PEI target. */
3247 convert_efi_target (char *efi
)
3253 if (strcmp (efi
+ 4, "ia32") == 0)
3255 /* Change ia32 to i386. */
3260 else if (strcmp (efi
+ 4, "x86_64") == 0)
3262 /* Change x86_64 to x86-64. */
3268 copy_main (int argc
, char *argv
[])
3270 char *input_filename
= NULL
;
3271 char *output_filename
= NULL
;
3273 char *input_target
= NULL
;
3274 char *output_target
= NULL
;
3275 bfd_boolean show_version
= FALSE
;
3276 bfd_boolean change_warn
= TRUE
;
3277 bfd_boolean formats_info
= FALSE
;
3279 struct section_list
*p
;
3280 struct stat statbuf
;
3281 const bfd_arch_info_type
*input_arch
= NULL
;
3283 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
3284 copy_options
, (int *) 0)) != EOF
)
3289 copy_byte
= atoi (optarg
);
3291 fatal (_("byte number must be non-negative"));
3295 input_arch
= bfd_scan_arch (optarg
);
3296 if (input_arch
== NULL
)
3297 fatal (_("architecture %s unknown"), optarg
);
3303 interleave
= atoi (optarg
);
3305 fatal (_("interleave must be positive"));
3311 case OPTION_INTERLEAVE_WIDTH
:
3312 copy_width
= atoi (optarg
);
3314 fatal(_("interleave width must be positive"));
3318 case 's': /* "source" - 'I' is preferred */
3319 input_target
= optarg
;
3323 case 'd': /* "destination" - 'O' is preferred */
3324 output_target
= optarg
;
3328 input_target
= output_target
= optarg
;
3332 p
= find_section_list (optarg
, TRUE
);
3334 fatal (_("%s both copied and removed"), optarg
);
3336 sections_copied
= TRUE
;
3340 p
= find_section_list (optarg
, TRUE
);
3342 fatal (_("%s both copied and removed"), optarg
);
3344 sections_removed
= TRUE
;
3348 strip_symbols
= STRIP_ALL
;
3352 strip_symbols
= STRIP_DEBUG
;
3355 case OPTION_STRIP_UNNEEDED
:
3356 strip_symbols
= STRIP_UNNEEDED
;
3359 case OPTION_ONLY_KEEP_DEBUG
:
3360 strip_symbols
= STRIP_NONDEBUG
;
3363 case OPTION_KEEP_FILE_SYMBOLS
:
3364 keep_file_symbols
= 1;
3367 case OPTION_ADD_GNU_DEBUGLINK
:
3368 gnu_debuglink_filename
= optarg
;
3372 add_specific_symbol (optarg
, keep_specific_htab
);
3376 add_specific_symbol (optarg
, strip_specific_htab
);
3379 case OPTION_STRIP_UNNEEDED_SYMBOL
:
3380 add_specific_symbol (optarg
, strip_unneeded_htab
);
3384 add_specific_symbol (optarg
, localize_specific_htab
);
3387 case OPTION_GLOBALIZE_SYMBOL
:
3388 add_specific_symbol (optarg
, globalize_specific_htab
);
3392 add_specific_symbol (optarg
, keepglobal_specific_htab
);
3396 add_specific_symbol (optarg
, weaken_specific_htab
);
3400 preserve_dates
= TRUE
;
3404 deterministic
= TRUE
;
3412 discard_locals
= LOCALS_ALL
;
3416 discard_locals
= LOCALS_START_L
;
3424 show_version
= TRUE
;
3427 case OPTION_FORMATS_INFO
:
3428 formats_info
= TRUE
;
3435 case OPTION_ADD_SECTION
:
3439 struct section_add
*pa
;
3442 s
= strchr (optarg
, '=');
3445 fatal (_("bad format for %s"), "--add-section");
3447 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
3448 pa
->name
= xstrndup (optarg
, s
- optarg
);
3449 pa
->filename
= s
+ 1;
3451 /* We don't use get_file_size so that we can do
3452 --add-section .note.GNU_stack=/dev/null
3453 get_file_size doesn't work on /dev/null. */
3455 f
= fopen (pa
->filename
, FOPEN_RB
);
3457 fatal (_("cannot open: %s: %s"),
3458 pa
->filename
, strerror (errno
));
3462 pa
->contents
= (bfd_byte
*) xmalloc (alloc
);
3470 pa
->contents
= (bfd_byte
*) xrealloc (pa
->contents
, alloc
);
3473 got
= fread (pa
->contents
+ off
, 1, alloc
- off
, f
);
3475 fatal (_("%s: fread failed"), pa
->filename
);
3484 pa
->next
= add_sections
;
3489 case OPTION_CHANGE_START
:
3490 change_start
= parse_vma (optarg
, "--change-start");
3493 case OPTION_CHANGE_SECTION_ADDRESS
:
3494 case OPTION_CHANGE_SECTION_LMA
:
3495 case OPTION_CHANGE_SECTION_VMA
:
3500 char *option
= NULL
;
3502 enum change_action what
= CHANGE_IGNORE
;
3506 case OPTION_CHANGE_SECTION_ADDRESS
:
3507 option
= "--change-section-address";
3509 case OPTION_CHANGE_SECTION_LMA
:
3510 option
= "--change-section-lma";
3512 case OPTION_CHANGE_SECTION_VMA
:
3513 option
= "--change-section-vma";
3517 s
= strchr (optarg
, '=');
3520 s
= strchr (optarg
, '+');
3523 s
= strchr (optarg
, '-');
3525 fatal (_("bad format for %s"), option
);
3530 name
= (char *) xmalloc (len
+ 1);
3531 strncpy (name
, optarg
, len
);
3534 p
= find_section_list (name
, TRUE
);
3536 val
= parse_vma (s
+ 1, option
);
3540 case '=': what
= CHANGE_SET
; break;
3541 case '-': val
= - val
; /* Drop through. */
3542 case '+': what
= CHANGE_MODIFY
; break;
3547 case OPTION_CHANGE_SECTION_ADDRESS
:
3548 p
->change_vma
= what
;
3552 case OPTION_CHANGE_SECTION_LMA
:
3553 p
->change_lma
= what
;
3557 case OPTION_CHANGE_SECTION_VMA
:
3558 p
->change_vma
= what
;
3565 case OPTION_CHANGE_ADDRESSES
:
3566 change_section_address
= parse_vma (optarg
, "--change-addresses");
3567 change_start
= change_section_address
;
3570 case OPTION_CHANGE_WARNINGS
:
3574 case OPTION_CHANGE_LEADING_CHAR
:
3575 change_leading_char
= TRUE
;
3578 case OPTION_COMPRESS_DEBUG_SECTIONS
:
3579 do_debug_sections
= compress
;
3582 case OPTION_DEBUGGING
:
3583 convert_debugging
= TRUE
;
3586 case OPTION_DECOMPRESS_DEBUG_SECTIONS
:
3587 do_debug_sections
= decompress
;
3590 case OPTION_GAP_FILL
:
3592 bfd_vma gap_fill_vma
;
3594 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
3595 gap_fill
= (bfd_byte
) gap_fill_vma
;
3596 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
3600 sprintf_vma (buff
, gap_fill_vma
);
3602 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3605 gap_fill_set
= TRUE
;
3609 case OPTION_NO_CHANGE_WARNINGS
:
3610 change_warn
= FALSE
;
3614 pad_to
= parse_vma (optarg
, "--pad-to");
3618 case OPTION_REMOVE_LEADING_CHAR
:
3619 remove_leading_char
= TRUE
;
3622 case OPTION_REDEFINE_SYM
:
3624 /* Push this redefinition onto redefine_symbol_list. */
3628 const char *nextarg
;
3629 char *source
, *target
;
3631 s
= strchr (optarg
, '=');
3633 fatal (_("bad format for %s"), "--redefine-sym");
3636 source
= (char *) xmalloc (len
+ 1);
3637 strncpy (source
, optarg
, len
);
3641 len
= strlen (nextarg
);
3642 target
= (char *) xmalloc (len
+ 1);
3643 strcpy (target
, nextarg
);
3645 redefine_list_append ("--redefine-sym", source
, target
);
3652 case OPTION_REDEFINE_SYMS
:
3653 add_redefine_syms_file (optarg
);
3656 case OPTION_SET_SECTION_FLAGS
:
3662 s
= strchr (optarg
, '=');
3664 fatal (_("bad format for %s"), "--set-section-flags");
3667 name
= (char *) xmalloc (len
+ 1);
3668 strncpy (name
, optarg
, len
);
3671 p
= find_section_list (name
, TRUE
);
3673 p
->set_flags
= TRUE
;
3674 p
->flags
= parse_flags (s
+ 1);
3678 case OPTION_RENAME_SECTION
:
3681 const char *eq
, *fl
;
3686 eq
= strchr (optarg
, '=');
3688 fatal (_("bad format for %s"), "--rename-section");
3692 fatal (_("bad format for %s"), "--rename-section");
3694 old_name
= (char *) xmalloc (len
+ 1);
3695 strncpy (old_name
, optarg
, len
);
3699 fl
= strchr (eq
, ',');
3702 flags
= parse_flags (fl
+ 1);
3712 fatal (_("bad format for %s"), "--rename-section");
3714 new_name
= (char *) xmalloc (len
+ 1);
3715 strncpy (new_name
, eq
, len
);
3718 add_section_rename (old_name
, new_name
, flags
);
3722 case OPTION_SET_START
:
3723 set_start
= parse_vma (optarg
, "--set-start");
3724 set_start_set
= TRUE
;
3727 case OPTION_SREC_LEN
:
3728 Chunk
= parse_vma (optarg
, "--srec-len");
3731 case OPTION_SREC_FORCES3
:
3735 case OPTION_STRIP_SYMBOLS
:
3736 add_specific_symbols (optarg
, strip_specific_htab
);
3739 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3740 add_specific_symbols (optarg
, strip_unneeded_htab
);
3743 case OPTION_KEEP_SYMBOLS
:
3744 add_specific_symbols (optarg
, keep_specific_htab
);
3747 case OPTION_LOCALIZE_HIDDEN
:
3748 localize_hidden
= TRUE
;
3751 case OPTION_LOCALIZE_SYMBOLS
:
3752 add_specific_symbols (optarg
, localize_specific_htab
);
3755 case OPTION_LONG_SECTION_NAMES
:
3756 if (!strcmp ("enable", optarg
))
3757 long_section_names
= ENABLE
;
3758 else if (!strcmp ("disable", optarg
))
3759 long_section_names
= DISABLE
;
3760 else if (!strcmp ("keep", optarg
))
3761 long_section_names
= KEEP
;
3763 fatal (_("unknown long section names option '%s'"), optarg
);
3766 case OPTION_GLOBALIZE_SYMBOLS
:
3767 add_specific_symbols (optarg
, globalize_specific_htab
);
3770 case OPTION_KEEPGLOBAL_SYMBOLS
:
3771 add_specific_symbols (optarg
, keepglobal_specific_htab
);
3774 case OPTION_WEAKEN_SYMBOLS
:
3775 add_specific_symbols (optarg
, weaken_specific_htab
);
3778 case OPTION_ALT_MACH_CODE
:
3779 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3780 if (use_alt_mach_code
== 0)
3781 fatal (_("unable to parse alternative machine code"));
3784 case OPTION_PREFIX_SYMBOLS
:
3785 prefix_symbols_string
= optarg
;
3788 case OPTION_PREFIX_SECTIONS
:
3789 prefix_sections_string
= optarg
;
3792 case OPTION_PREFIX_ALLOC_SECTIONS
:
3793 prefix_alloc_sections_string
= optarg
;
3796 case OPTION_READONLY_TEXT
:
3797 bfd_flags_to_set
|= WP_TEXT
;
3798 bfd_flags_to_clear
&= ~WP_TEXT
;
3801 case OPTION_WRITABLE_TEXT
:
3802 bfd_flags_to_clear
|= WP_TEXT
;
3803 bfd_flags_to_set
&= ~WP_TEXT
;
3807 bfd_flags_to_set
|= D_PAGED
;
3808 bfd_flags_to_clear
&= ~D_PAGED
;
3812 bfd_flags_to_clear
|= D_PAGED
;
3813 bfd_flags_to_set
&= ~D_PAGED
;
3816 case OPTION_EXTRACT_SYMBOL
:
3817 extract_symbol
= TRUE
;
3820 case OPTION_REVERSE_BYTES
:
3822 int prev
= reverse_bytes
;
3824 reverse_bytes
= atoi (optarg
);
3825 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
3826 fatal (_("number of bytes to reverse must be positive and even"));
3828 if (prev
&& prev
!= reverse_bytes
)
3829 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3834 case OPTION_FILE_ALIGNMENT
:
3835 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
3841 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
3843 || (*end
!= '.' && *end
!= '\0'))
3844 non_fatal (_("%s: invalid reserve value for --heap"),
3846 else if (*end
!= '\0')
3848 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
3850 non_fatal (_("%s: invalid commit value for --heap"),
3856 case OPTION_IMAGE_BASE
:
3857 pe_image_base
= parse_vma (optarg
, "--image-base");
3860 case OPTION_SECTION_ALIGNMENT
:
3861 pe_section_alignment
= parse_vma (optarg
,
3862 "--section-alignment");
3865 case OPTION_SUBSYSTEM
:
3866 set_pe_subsystem (optarg
);
3872 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
3874 || (*end
!= '.' && *end
!= '\0'))
3875 non_fatal (_("%s: invalid reserve value for --stack"),
3877 else if (*end
!= '\0')
3879 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
3881 non_fatal (_("%s: invalid commit value for --stack"),
3888 /* We've been given a long option. */
3893 copy_usage (stdout
, 0);
3896 copy_usage (stderr
, 1);
3907 print_version ("objcopy");
3909 if (interleave
&& copy_byte
== -1)
3910 fatal (_("interleave start byte must be set with --byte"));
3912 if (copy_byte
>= interleave
)
3913 fatal (_("byte number must be less than interleave"));
3915 if (copy_width
> interleave
- copy_byte
)
3916 fatal (_("interleave width must be less than or equal to interleave - byte`"));
3918 if (optind
== argc
|| optind
+ 2 < argc
)
3919 copy_usage (stderr
, 1);
3921 input_filename
= argv
[optind
];
3922 if (optind
+ 1 < argc
)
3923 output_filename
= argv
[optind
+ 1];
3925 /* Default is to strip no symbols. */
3926 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3927 strip_symbols
= STRIP_NONE
;
3929 if (output_target
== NULL
)
3930 output_target
= input_target
;
3932 /* Convert input EFI target to PEI target. */
3933 if (input_target
!= NULL
3934 && strncmp (input_target
, "efi-", 4) == 0)
3938 efi
= xstrdup (output_target
+ 4);
3939 if (strncmp (efi
, "bsdrv-", 6) == 0
3940 || strncmp (efi
, "rtdrv-", 6) == 0)
3942 else if (strncmp (efi
, "app-", 4) != 0)
3943 fatal (_("unknown input EFI target: %s"), input_target
);
3946 convert_efi_target (efi
);
3949 /* Convert output EFI target to PEI target. */
3950 if (output_target
!= NULL
3951 && strncmp (output_target
, "efi-", 4) == 0)
3955 efi
= xstrdup (output_target
+ 4);
3956 if (strncmp (efi
, "app-", 4) == 0)
3958 if (pe_subsystem
== -1)
3959 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
3961 else if (strncmp (efi
, "bsdrv-", 6) == 0)
3963 if (pe_subsystem
== -1)
3964 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
3967 else if (strncmp (efi
, "rtdrv-", 6) == 0)
3969 if (pe_subsystem
== -1)
3970 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
3974 fatal (_("unknown output EFI target: %s"), output_target
);
3976 if (pe_file_alignment
== (bfd_vma
) -1)
3977 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
3978 if (pe_section_alignment
== (bfd_vma
) -1)
3979 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
3981 output_target
= efi
;
3982 convert_efi_target (efi
);
3986 if (stat (input_filename
, & statbuf
) < 0)
3987 fatal (_("warning: could not locate '%s'. System error message: %s"),
3988 input_filename
, strerror (errno
));
3990 /* If there is no destination file, or the source and destination files
3991 are the same, then create a temp and rename the result into the input. */
3992 if (output_filename
== NULL
3993 || filename_cmp (input_filename
, output_filename
) == 0)
3994 tmpname
= make_tempname (input_filename
);
3996 tmpname
= output_filename
;
3998 if (tmpname
== NULL
)
3999 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
4000 input_filename
, strerror (errno
));
4002 copy_file (input_filename
, tmpname
, input_target
, output_target
, input_arch
);
4006 set_times (tmpname
, &statbuf
);
4007 if (tmpname
!= output_filename
)
4008 status
= (smart_rename (tmpname
, input_filename
,
4009 preserve_dates
) != 0);
4012 unlink_if_ordinary (tmpname
);
4016 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
4020 if (p
->change_vma
!= CHANGE_IGNORE
)
4024 sprintf_vma (buff
, p
->vma_val
);
4026 /* xgettext:c-format */
4027 non_fatal (_("%s %s%c0x%s never used"),
4028 "--change-section-vma",
4030 p
->change_vma
== CHANGE_SET
? '=' : '+',
4034 if (p
->change_lma
!= CHANGE_IGNORE
)
4038 sprintf_vma (buff
, p
->lma_val
);
4040 /* xgettext:c-format */
4041 non_fatal (_("%s %s%c0x%s never used"),
4042 "--change-section-lma",
4044 p
->change_lma
== CHANGE_SET
? '=' : '+',
4055 main (int argc
, char *argv
[])
4057 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4058 setlocale (LC_MESSAGES
, "");
4060 #if defined (HAVE_SETLOCALE)
4061 setlocale (LC_CTYPE
, "");
4063 bindtextdomain (PACKAGE
, LOCALEDIR
);
4064 textdomain (PACKAGE
);
4066 program_name
= argv
[0];
4067 xmalloc_set_program_name (program_name
);
4069 START_PROGRESS (program_name
, 0);
4071 expandargv (&argc
, &argv
);
4073 strip_symbols
= STRIP_UNDEF
;
4074 discard_locals
= LOCALS_UNDEF
;
4077 set_default_bfd_target ();
4081 int i
= strlen (program_name
);
4082 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4083 /* Drop the .exe suffix, if any. */
4084 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
4087 program_name
[i
] = '\0';
4090 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
4093 create_symbol_htabs ();
4096 strip_main (argc
, argv
);
4098 copy_main (argc
, argv
);
4100 END_PROGRESS (program_name
);