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
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
27 #include "libiberty.h"
30 #include "filenames.h"
35 #include "coff/internal.h"
38 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
39 header in generic PE code. */
40 #include "coff/i386.h"
43 static bfd_vma pe_file_alignment
= (bfd_vma
) -1;
44 static bfd_vma pe_heap_commit
= (bfd_vma
) -1;
45 static bfd_vma pe_heap_reserve
= (bfd_vma
) -1;
46 static bfd_vma pe_image_base
= (bfd_vma
) -1;
47 static bfd_vma pe_section_alignment
= (bfd_vma
) -1;
48 static bfd_vma pe_stack_commit
= (bfd_vma
) -1;
49 static bfd_vma pe_stack_reserve
= (bfd_vma
) -1;
50 static short pe_subsystem
= -1;
51 static short pe_major_subsystem_version
= -1;
52 static short pe_minor_subsystem_version
= -1;
54 struct is_specified_symbol_predicate_data
60 /* A list to support redefine_sym. */
65 struct redefine_node
*next
;
68 typedef struct section_rename
70 const char * old_name
;
71 const char * new_name
;
73 struct section_rename
* next
;
77 /* List of sections to be renamed. */
78 static section_rename
*section_rename_list
;
80 static asymbol
**isympp
= NULL
; /* Input symbols. */
81 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
83 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
84 static int copy_byte
= -1;
85 static int interleave
= 0; /* Initialised to 4 in copy_main(). */
86 static int copy_width
= 1;
88 static bfd_boolean verbose
; /* Print file and target names. */
89 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
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 change the leading character in symbol names. */
198 static bfd_boolean change_leading_char
= FALSE
;
200 /* Whether to remove the leading character from global symbol names. */
201 static bfd_boolean remove_leading_char
= FALSE
;
203 /* Whether to permit wildcard in symbol comparison. */
204 static bfd_boolean wildcard
= FALSE
;
206 /* True if --localize-hidden is in effect. */
207 static bfd_boolean localize_hidden
= FALSE
;
209 /* List of symbols to strip, keep, localize, keep-global, weaken,
211 static htab_t strip_specific_htab
= NULL
;
212 static htab_t strip_unneeded_htab
= NULL
;
213 static htab_t keep_specific_htab
= NULL
;
214 static htab_t localize_specific_htab
= NULL
;
215 static htab_t globalize_specific_htab
= NULL
;
216 static htab_t keepglobal_specific_htab
= NULL
;
217 static htab_t weaken_specific_htab
= NULL
;
218 static struct redefine_node
*redefine_sym_list
= NULL
;
220 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
221 static bfd_boolean weaken
= FALSE
;
223 /* If this is TRUE, we retain BSF_FILE symbols. */
224 static bfd_boolean keep_file_symbols
= FALSE
;
226 /* Prefix symbols/sections. */
227 static char *prefix_symbols_string
= 0;
228 static char *prefix_sections_string
= 0;
229 static char *prefix_alloc_sections_string
= 0;
231 /* True if --extract-symbol was passed on the command line. */
232 static bfd_boolean extract_symbol
= FALSE
;
234 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
235 of <reverse_bytes> bytes within each output section. */
236 static int reverse_bytes
= 0;
238 /* For Coff objects, we may want to allow or disallow long section names,
239 or preserve them where found in the inputs. Debug info relies on them. */
240 enum long_section_name_handling
247 /* The default long section handling mode is to preserve them.
248 This is also the only behaviour for 'strip'. */
249 static enum long_section_name_handling long_section_names
= KEEP
;
251 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
252 enum command_line_switch
254 OPTION_ADD_SECTION
=150,
255 OPTION_CHANGE_ADDRESSES
,
256 OPTION_CHANGE_LEADING_CHAR
,
258 OPTION_CHANGE_SECTION_ADDRESS
,
259 OPTION_CHANGE_SECTION_LMA
,
260 OPTION_CHANGE_SECTION_VMA
,
261 OPTION_CHANGE_WARNINGS
,
264 OPTION_NO_CHANGE_WARNINGS
,
266 OPTION_REMOVE_LEADING_CHAR
,
267 OPTION_SET_SECTION_FLAGS
,
269 OPTION_STRIP_UNNEEDED
,
272 OPTION_REDEFINE_SYMS
,
275 OPTION_STRIP_SYMBOLS
,
276 OPTION_STRIP_UNNEEDED_SYMBOL
,
277 OPTION_STRIP_UNNEEDED_SYMBOLS
,
279 OPTION_LOCALIZE_HIDDEN
,
280 OPTION_LOCALIZE_SYMBOLS
,
281 OPTION_LONG_SECTION_NAMES
,
282 OPTION_GLOBALIZE_SYMBOL
,
283 OPTION_GLOBALIZE_SYMBOLS
,
284 OPTION_KEEPGLOBAL_SYMBOLS
,
285 OPTION_WEAKEN_SYMBOLS
,
286 OPTION_RENAME_SECTION
,
287 OPTION_ALT_MACH_CODE
,
288 OPTION_PREFIX_SYMBOLS
,
289 OPTION_PREFIX_SECTIONS
,
290 OPTION_PREFIX_ALLOC_SECTIONS
,
292 OPTION_ADD_GNU_DEBUGLINK
,
293 OPTION_ONLY_KEEP_DEBUG
,
294 OPTION_KEEP_FILE_SYMBOLS
,
295 OPTION_READONLY_TEXT
,
296 OPTION_WRITABLE_TEXT
,
299 OPTION_EXTRACT_SYMBOL
,
300 OPTION_REVERSE_BYTES
,
301 OPTION_FILE_ALIGNMENT
,
304 OPTION_SECTION_ALIGNMENT
,
306 OPTION_INTERLEAVE_WIDTH
,
310 /* Options to handle if running as "strip". */
312 static struct option strip_options
[] =
314 {"discard-all", no_argument
, 0, 'x'},
315 {"discard-locals", no_argument
, 0, 'X'},
316 {"format", required_argument
, 0, 'F'}, /* Obsolete */
317 {"help", no_argument
, 0, 'h'},
318 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
319 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
320 {"input-target", required_argument
, 0, 'I'},
321 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
322 {"keep-symbol", required_argument
, 0, 'K'},
323 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
324 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
325 {"output-target", required_argument
, 0, 'O'},
326 {"output-file", required_argument
, 0, 'o'},
327 {"preserve-dates", no_argument
, 0, 'p'},
328 {"remove-section", required_argument
, 0, 'R'},
329 {"strip-all", no_argument
, 0, 's'},
330 {"strip-debug", no_argument
, 0, 'S'},
331 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
332 {"strip-symbol", required_argument
, 0, 'N'},
333 {"target", required_argument
, 0, 'F'},
334 {"verbose", no_argument
, 0, 'v'},
335 {"version", no_argument
, 0, 'V'},
336 {"wildcard", no_argument
, 0, 'w'},
337 {0, no_argument
, 0, 0}
340 /* Options to handle if running as "objcopy". */
342 static struct option copy_options
[] =
344 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
345 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
346 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
347 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
348 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
349 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
350 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
351 {"binary-architecture", required_argument
, 0, 'B'},
352 {"byte", required_argument
, 0, 'b'},
353 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
354 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
355 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
356 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
357 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
358 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
359 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
360 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
361 {"discard-all", no_argument
, 0, 'x'},
362 {"discard-locals", no_argument
, 0, 'X'},
363 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
364 {"format", required_argument
, 0, 'F'}, /* Obsolete */
365 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
366 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
367 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
368 {"help", no_argument
, 0, 'h'},
369 {"impure", no_argument
, 0, OPTION_IMPURE
},
370 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
371 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
372 {"input-target", required_argument
, 0, 'I'},
373 {"interleave", optional_argument
, 0, 'i'},
374 {"interleave-width", required_argument
, 0, OPTION_INTERLEAVE_WIDTH
},
375 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
376 {"keep-global-symbol", required_argument
, 0, 'G'},
377 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
378 {"keep-symbol", required_argument
, 0, 'K'},
379 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
380 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
381 {"localize-symbol", required_argument
, 0, 'L'},
382 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
383 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
384 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
385 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
386 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
387 {"only-section", required_argument
, 0, 'j'},
388 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
389 {"output-target", required_argument
, 0, 'O'},
390 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
391 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
392 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
393 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
394 {"preserve-dates", no_argument
, 0, 'p'},
395 {"pure", no_argument
, 0, OPTION_PURE
},
396 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
397 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
398 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
399 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
400 {"remove-section", required_argument
, 0, 'R'},
401 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
402 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
403 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
404 {"set-start", required_argument
, 0, OPTION_SET_START
},
405 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
406 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
407 {"strip-all", no_argument
, 0, 'S'},
408 {"strip-debug", no_argument
, 0, 'g'},
409 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
410 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
411 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
412 {"strip-symbol", required_argument
, 0, 'N'},
413 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
414 {"target", required_argument
, 0, 'F'},
415 {"verbose", no_argument
, 0, 'v'},
416 {"version", no_argument
, 0, 'V'},
417 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
418 {"weaken-symbol", required_argument
, 0, 'W'},
419 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
420 {"wildcard", no_argument
, 0, 'w'},
421 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
422 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
423 {"heap", required_argument
, 0, OPTION_HEAP
},
424 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
425 {"section-alignment", required_argument
, 0, OPTION_SECTION_ALIGNMENT
},
426 {"stack", required_argument
, 0, OPTION_STACK
},
427 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
428 {0, no_argument
, 0, 0}
432 extern char *program_name
;
434 /* This flag distinguishes between strip and objcopy:
435 1 means this is 'strip'; 0 means this is 'objcopy'.
436 -1 means if we should use argv[0] to decide. */
439 /* The maximum length of an S record. This variable is declared in srec.c
440 and can be modified by the --srec-len parameter. */
441 extern unsigned int Chunk
;
443 /* Restrict the generation of Srecords to type S3 only.
444 This variable is declare in bfd/srec.c and can be toggled
445 on by the --srec-forceS3 command line switch. */
446 extern bfd_boolean S3Forced
;
448 /* Forward declarations. */
449 static void setup_section (bfd
*, asection
*, void *);
450 static void setup_bfd_headers (bfd
*, bfd
*);
451 static void copy_section (bfd
*, asection
*, void *);
452 static void get_sections (bfd
*, asection
*, void *);
453 static int compare_section_lma (const void *, const void *);
454 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
455 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
456 static const char *lookup_sym_redefinition (const char *);
459 copy_usage (FILE *stream
, int exit_status
)
461 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
462 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
463 fprintf (stream
, _(" The options are:\n"));
464 fprintf (stream
, _("\
465 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
466 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
467 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
468 -F --target <bfdname> Set both input and output format to <bfdname>\n\
469 --debugging Convert debugging information, if possible\n\
470 -p --preserve-dates Copy modified/access timestamps to the output\n\
471 -j --only-section <name> Only copy section <name> into the output\n\
472 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
473 -R --remove-section <name> Remove section <name> from the output\n\
474 -S --strip-all Remove all symbol and relocation information\n\
475 -g --strip-debug Remove all debugging symbols & sections\n\
476 --strip-unneeded Remove all symbols not needed by relocations\n\
477 -N --strip-symbol <name> Do not copy symbol <name>\n\
478 --strip-unneeded-symbol <name>\n\
479 Do not copy symbol <name> unless needed by\n\
481 --only-keep-debug Strip everything but the debug information\n\
482 --extract-symbol Remove section contents but keep symbols\n\
483 -K --keep-symbol <name> Do not strip symbol <name>\n\
484 --keep-file-symbols Do not strip file symbol(s)\n\
485 --localize-hidden Turn all ELF hidden symbols into locals\n\
486 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
487 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
488 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
489 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
490 --weaken Force all global symbols to be marked as weak\n\
491 -w --wildcard Permit wildcard in symbol comparison\n\
492 -x --discard-all Remove all non-global symbols\n\
493 -X --discard-locals Remove any compiler-generated symbols\n\
494 -i --interleave [<number>] Only copy N out of every <number> bytes\n\
495 --interleave-width <number> Set N for --interleave\n\
496 -b --byte <num> Select byte <num> in every interleaved block\n\
497 --gap-fill <val> Fill gaps between sections with <val>\n\
498 --pad-to <addr> Pad the last section up to address <addr>\n\
499 --set-start <addr> Set the start address to <addr>\n\
500 {--change-start|--adjust-start} <incr>\n\
501 Add <incr> to the start address\n\
502 {--change-addresses|--adjust-vma} <incr>\n\
503 Add <incr> to LMA, VMA and start addresses\n\
504 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
505 Change LMA and VMA of section <name> by <val>\n\
506 --change-section-lma <name>{=|+|-}<val>\n\
507 Change the LMA of section <name> by <val>\n\
508 --change-section-vma <name>{=|+|-}<val>\n\
509 Change the VMA of section <name> by <val>\n\
510 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
511 Warn if a named section does not exist\n\
512 --set-section-flags <name>=<flags>\n\
513 Set section <name>'s properties to <flags>\n\
514 --add-section <name>=<file> Add section <name> found in <file> to output\n\
515 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
516 --long-section-names {enable|disable|keep}\n\
517 Handle long section names in Coff objects.\n\
518 --change-leading-char Force output format's leading character style\n\
519 --remove-leading-char Remove leading character from global symbols\n\
520 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
521 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
522 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
524 --srec-len <number> Restrict the length of generated Srecords\n\
525 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
526 --strip-symbols <file> -N for all symbols listed in <file>\n\
527 --strip-unneeded-symbols <file>\n\
528 --strip-unneeded-symbol for all symbols listed\n\
530 --keep-symbols <file> -K for all symbols listed in <file>\n\
531 --localize-symbols <file> -L for all symbols listed in <file>\n\
532 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
533 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
534 --weaken-symbols <file> -W for all symbols listed in <file>\n\
535 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
536 --writable-text Mark the output text as writable\n\
537 --readonly-text Make the output text write protected\n\
538 --pure Mark the output file as demand paged\n\
539 --impure Mark the output file as impure\n\
540 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
541 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
542 --prefix-alloc-sections <prefix>\n\
543 Add <prefix> to start of every allocatable\n\
545 --file-alignment <num> Set PE file alignment to <num>\n\
546 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
548 --image-base <address> Set PE image base to <address>\n\
549 --section-alignment <num> Set PE section alignment to <num>\n\
550 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
552 --subsystem <name>[:<version>]\n\
553 Set PE subsystem to <name> [& <version>]\n\
554 -v --verbose List all object files modified\n\
555 @<file> Read options from <file>\n\
556 -V --version Display this program's version number\n\
557 -h --help Display this output\n\
558 --info List object formats & architectures supported\n\
560 list_supported_targets (program_name
, stream
);
561 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
562 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
567 strip_usage (FILE *stream
, int exit_status
)
569 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
570 fprintf (stream
, _(" Removes symbols and sections from files\n"));
571 fprintf (stream
, _(" The options are:\n"));
572 fprintf (stream
, _("\
573 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
574 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
575 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
576 -p --preserve-dates Copy modified/access timestamps to the output\n\
577 -R --remove-section=<name> Remove section <name> from the output\n\
578 -s --strip-all Remove all symbol and relocation information\n\
579 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
580 --strip-unneeded Remove all symbols not needed by relocations\n\
581 --only-keep-debug Strip everything but the debug information\n\
582 -N --strip-symbol=<name> Do not copy symbol <name>\n\
583 -K --keep-symbol=<name> Do not strip symbol <name>\n\
584 --keep-file-symbols Do not strip file symbol(s)\n\
585 -w --wildcard Permit wildcard in symbol comparison\n\
586 -x --discard-all Remove all non-global symbols\n\
587 -X --discard-locals Remove any compiler-generated symbols\n\
588 -v --verbose List all object files modified\n\
589 -V --version Display this program's version number\n\
590 -h --help Display this output\n\
591 --info List object formats & architectures supported\n\
592 -o <file> Place stripped output into <file>\n\
595 list_supported_targets (program_name
, stream
);
596 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
597 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
601 /* Parse section flags into a flagword, with a fatal error if the
602 string can't be parsed. */
605 parse_flags (const char *s
)
615 snext
= strchr (s
, ',');
625 #define PARSE_FLAG(fname,fval) \
626 else if (strncasecmp (fname, s, len) == 0) ret |= fval
627 PARSE_FLAG ("alloc", SEC_ALLOC
);
628 PARSE_FLAG ("load", SEC_LOAD
);
629 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
630 PARSE_FLAG ("readonly", SEC_READONLY
);
631 PARSE_FLAG ("debug", SEC_DEBUGGING
);
632 PARSE_FLAG ("code", SEC_CODE
);
633 PARSE_FLAG ("data", SEC_DATA
);
634 PARSE_FLAG ("rom", SEC_ROM
);
635 PARSE_FLAG ("share", SEC_COFF_SHARED
);
636 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
642 copy
= (char *) xmalloc (len
+ 1);
643 strncpy (copy
, s
, len
);
645 non_fatal (_("unrecognized section flag `%s'"), copy
);
646 fatal (_("supported flags: %s"),
647 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
657 /* Find and optionally add an entry in the change_sections list. */
659 static struct section_list
*
660 find_section_list (const char *name
, bfd_boolean add
)
662 struct section_list
*p
;
664 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
665 if (strcmp (p
->name
, name
) == 0)
671 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
676 p
->change_vma
= CHANGE_IGNORE
;
677 p
->change_lma
= CHANGE_IGNORE
;
680 p
->set_flags
= FALSE
;
683 p
->next
= change_sections
;
689 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
692 eq_string (const void *s1
, const void *s2
)
694 return strcmp ((const char *) s1
, (const char *) s2
) == 0;
698 create_symbol_htab (void)
700 return htab_create_alloc (16, htab_hash_string
, eq_string
, NULL
, xcalloc
, free
);
704 create_symbol_htabs (void)
706 strip_specific_htab
= create_symbol_htab ();
707 strip_unneeded_htab
= create_symbol_htab ();
708 keep_specific_htab
= create_symbol_htab ();
709 localize_specific_htab
= create_symbol_htab ();
710 globalize_specific_htab
= create_symbol_htab ();
711 keepglobal_specific_htab
= create_symbol_htab ();
712 weaken_specific_htab
= create_symbol_htab ();
715 /* Add a symbol to strip_specific_list. */
718 add_specific_symbol (const char *name
, htab_t htab
)
720 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
723 /* Add symbols listed in `filename' to strip_specific_list. */
725 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
726 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
729 add_specific_symbols (const char *filename
, htab_t htab
)
735 unsigned int line_count
;
737 size
= get_file_size (filename
);
744 buffer
= (char *) xmalloc (size
+ 2);
745 f
= fopen (filename
, FOPEN_RT
);
747 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
749 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
750 fatal (_("%s: fread failed"), filename
);
753 buffer
[size
] = '\n';
754 buffer
[size
+ 1] = '\0';
758 for (line
= buffer
; * line
!= '\0'; line
++)
763 int finished
= FALSE
;
765 for (eol
= line
;; eol
++)
771 /* Cope with \n\r. */
779 /* Cope with \r\n. */
790 /* Line comment, Terminate the line here, in case a
791 name is present and then allow the rest of the
792 loop to find the real end of the line. */
804 /* A name may now exist somewhere between 'line' and 'eol'.
805 Strip off leading whitespace and trailing whitespace,
806 then add it to the list. */
807 for (name
= line
; IS_WHITESPACE (* name
); name
++)
809 for (name_end
= name
;
810 (! IS_WHITESPACE (* name_end
))
811 && (! IS_LINE_TERMINATOR (* name_end
));
815 if (! IS_LINE_TERMINATOR (* name_end
))
819 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
822 if (! IS_LINE_TERMINATOR (* extra
))
823 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
824 filename
, line_count
);
830 add_specific_symbol (name
, htab
);
832 /* Advance line pointer to end of line. The 'eol ++' in the for
833 loop above will then advance us to the start of the next line. */
839 /* See whether a symbol should be stripped or kept
840 based on strip_specific_list and keep_symbols. */
843 is_specified_symbol_predicate (void **slot
, void *data
)
845 struct is_specified_symbol_predicate_data
*d
=
846 (struct is_specified_symbol_predicate_data
*) data
;
847 const char *slot_name
= (char *) *slot
;
849 if (*slot_name
!= '!')
851 if (! fnmatch (slot_name
, d
->name
, 0))
854 /* Stop traversal. */
860 if (fnmatch (slot_name
+ 1, d
->name
, 0))
863 /* Stop traversal. */
868 /* Continue traversal. */
873 is_specified_symbol (const char *name
, htab_t htab
)
877 struct is_specified_symbol_predicate_data data
;
882 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
887 return htab_find (htab
, name
) != NULL
;
890 /* Return a pointer to the symbol used as a signature for GROUP. */
893 group_signature (asection
*group
)
895 bfd
*abfd
= group
->owner
;
896 Elf_Internal_Shdr
*ghdr
;
898 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
901 ghdr
= &elf_section_data (group
)->this_hdr
;
902 if (ghdr
->sh_link
< elf_numsections (abfd
))
904 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
905 Elf_Internal_Shdr
*symhdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
907 if (symhdr
->sh_type
== SHT_SYMTAB
908 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
909 return isympp
[ghdr
->sh_info
- 1];
914 /* See if a section is being removed. */
917 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
919 if (sections_removed
|| sections_copied
)
921 struct section_list
*p
;
923 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
925 if (sections_removed
&& p
!= NULL
&& p
->remove
)
927 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
931 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
933 if (strip_symbols
== STRIP_DEBUG
934 || strip_symbols
== STRIP_UNNEEDED
935 || strip_symbols
== STRIP_ALL
936 || discard_locals
== LOCALS_ALL
937 || convert_debugging
)
940 if (strip_symbols
== STRIP_NONDEBUG
)
944 if ((bfd_get_section_flags (abfd
, sec
) & SEC_GROUP
) != 0)
950 If we are going to strip the group signature symbol, then
951 strip the group section too. */
952 gsym
= group_signature (sec
);
957 if ((strip_symbols
== STRIP_ALL
958 && !is_specified_symbol (gname
, keep_specific_htab
))
959 || is_specified_symbol (gname
, strip_specific_htab
))
966 /* Return true if SYM is a hidden symbol. */
969 is_hidden_symbol (asymbol
*sym
)
971 elf_symbol_type
*elf_sym
;
973 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
975 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
984 /* Choose which symbol entries to copy; put the result in OSYMS.
985 We don't copy in place, because that confuses the relocs.
986 Return the number of symbols to print. */
989 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
990 asymbol
**isyms
, long symcount
)
992 asymbol
**from
= isyms
, **to
= osyms
;
993 long src_count
= 0, dst_count
= 0;
994 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
996 for (; src_count
< symcount
; src_count
++)
998 asymbol
*sym
= from
[src_count
];
999 flagword flags
= sym
->flags
;
1000 char *name
= (char *) bfd_asymbol_name (sym
);
1002 bfd_boolean used_in_reloc
= FALSE
;
1003 bfd_boolean undefined
;
1004 bfd_boolean rem_leading_char
;
1005 bfd_boolean add_leading_char
;
1007 undefined
= bfd_is_und_section (bfd_get_section (sym
));
1009 if (redefine_sym_list
)
1011 char *old_name
, *new_name
;
1013 old_name
= (char *) bfd_asymbol_name (sym
);
1014 new_name
= (char *) lookup_sym_redefinition (old_name
);
1015 bfd_asymbol_name (sym
) = new_name
;
1019 /* Check if we will remove the current leading character. */
1021 (name
[0] == bfd_get_symbol_leading_char (abfd
))
1022 && (change_leading_char
1023 || (remove_leading_char
1024 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1026 || bfd_is_com_section (bfd_get_section (sym
)))));
1028 /* Check if we will add a new leading character. */
1031 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1032 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1033 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1035 /* Short circuit for change_leading_char if we can do it in-place. */
1036 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1038 name
[0] = bfd_get_symbol_leading_char (obfd
);
1039 bfd_asymbol_name (sym
) = name
;
1040 rem_leading_char
= FALSE
;
1041 add_leading_char
= FALSE
;
1044 /* Remove leading char. */
1045 if (rem_leading_char
)
1046 bfd_asymbol_name (sym
) = ++name
;
1048 /* Add new leading char and/or prefix. */
1049 if (add_leading_char
|| prefix_symbols_string
)
1053 ptr
= n
= (char *) xmalloc (1 + strlen (prefix_symbols_string
)
1054 + strlen (name
) + 1);
1055 if (add_leading_char
)
1056 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1058 if (prefix_symbols_string
)
1060 strcpy (ptr
, prefix_symbols_string
);
1061 ptr
+= strlen (prefix_symbols_string
);
1065 bfd_asymbol_name (sym
) = n
;
1069 if (strip_symbols
== STRIP_ALL
)
1071 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1072 || ((flags
& BSF_SECTION_SYM
) != 0
1073 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
1077 used_in_reloc
= TRUE
;
1079 else if (relocatable
/* Relocatable file. */
1080 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1081 || bfd_is_com_section (bfd_get_section (sym
))))
1083 else if (bfd_decode_symclass (sym
) == 'I')
1084 /* Global symbols in $idata sections need to be retained
1085 even if relocatable is FALSE. External users of the
1086 library containing the $idata section may reference these
1089 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1090 || (flags
& BSF_WEAK
) != 0
1092 || bfd_is_com_section (bfd_get_section (sym
)))
1093 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1094 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1095 keep
= (strip_symbols
!= STRIP_DEBUG
1096 && strip_symbols
!= STRIP_UNNEEDED
1097 && ! convert_debugging
);
1098 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
1099 /* COMDAT sections store special information in local
1100 symbols, so we cannot risk stripping any of them. */
1102 else /* Local symbol. */
1103 keep
= (strip_symbols
!= STRIP_UNNEEDED
1104 && (discard_locals
!= LOCALS_ALL
1105 && (discard_locals
!= LOCALS_START_L
1106 || ! bfd_is_local_label (abfd
, sym
))));
1108 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1110 /* There are multiple ways to set 'keep' above, but if it
1111 was the relocatable symbol case, then that's an error. */
1114 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1122 && !(flags
& BSF_KEEP
)
1123 && is_specified_symbol (name
, strip_unneeded_htab
))
1127 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1128 || is_specified_symbol (name
, keep_specific_htab
)))
1131 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
1136 if ((flags
& BSF_GLOBAL
) != 0
1137 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1139 sym
->flags
&= ~ BSF_GLOBAL
;
1140 sym
->flags
|= BSF_WEAK
;
1144 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1145 && (is_specified_symbol (name
, localize_specific_htab
)
1146 || (htab_elements (keepglobal_specific_htab
) != 0
1147 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1148 || (localize_hidden
&& is_hidden_symbol (sym
))))
1150 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1151 sym
->flags
|= BSF_LOCAL
;
1155 && (flags
& BSF_LOCAL
)
1156 && is_specified_symbol (name
, globalize_specific_htab
))
1158 sym
->flags
&= ~ BSF_LOCAL
;
1159 sym
->flags
|= BSF_GLOBAL
;
1162 to
[dst_count
++] = sym
;
1166 to
[dst_count
] = NULL
;
1171 /* Find the redefined name of symbol SOURCE. */
1174 lookup_sym_redefinition (const char *source
)
1176 struct redefine_node
*list
;
1178 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1179 if (strcmp (source
, list
->source
) == 0)
1180 return list
->target
;
1185 /* Add a node to a symbol redefine list. */
1188 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1190 struct redefine_node
**p
;
1191 struct redefine_node
*list
;
1192 struct redefine_node
*new_node
;
1194 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1196 if (strcmp (source
, list
->source
) == 0)
1197 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1200 if (strcmp (target
, list
->target
) == 0)
1201 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1205 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
1207 new_node
->source
= strdup (source
);
1208 new_node
->target
= strdup (target
);
1209 new_node
->next
= NULL
;
1214 /* Handle the --redefine-syms option. Read lines containing "old new"
1215 from the file, and add them to the symbol redefine list. */
1218 add_redefine_syms_file (const char *filename
)
1227 file
= fopen (filename
, "r");
1229 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1230 filename
, strerror (errno
));
1233 buf
= (char *) xmalloc (bufsize
+ 1 /* For the terminating NUL. */);
1241 /* Collect the input symbol name. */
1242 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1250 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1258 /* Eat white space between the symbol names. */
1259 while (IS_WHITESPACE (c
))
1261 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1266 /* Collect the output symbol name. */
1268 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1276 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1284 /* Eat white space at end of line. */
1285 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1290 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1291 || c
== '\n' || c
== EOF
)
1294 /* Append the redefinition to the list. */
1296 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1307 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1309 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1310 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1313 /* Eat the rest of the line and finish it. */
1314 while (c
!= '\n' && c
!= EOF
)
1320 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1325 /* Copy unkown object file IBFD onto OBFD.
1326 Returns TRUE upon success, FALSE otherwise. */
1329 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1337 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1339 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1346 non_fatal (_("stat returns negative size for `%s'"),
1347 bfd_get_archive_filename (ibfd
));
1351 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1353 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1358 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1359 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1361 cbuf
= (char *) xmalloc (BUFSIZE
);
1363 while (ncopied
< size
)
1365 tocopy
= size
- ncopied
;
1366 if (tocopy
> BUFSIZE
)
1369 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1370 != (bfd_size_type
) tocopy
)
1372 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1377 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1378 != (bfd_size_type
) tocopy
)
1380 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1388 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1393 /* Copy object file IBFD onto OBFD.
1394 Returns TRUE upon success, FALSE otherwise. */
1397 copy_object (bfd
*ibfd
, bfd
*obfd
, const bfd_arch_info_type
*input_arch
)
1401 asection
**osections
= NULL
;
1402 asection
*gnu_debuglink_section
= NULL
;
1403 bfd_size_type
*gaps
= NULL
;
1404 bfd_size_type max_gap
= 0;
1407 enum bfd_architecture iarch
;
1410 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1411 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1412 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1413 fatal (_("Unable to change endianness of input file(s)"));
1415 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1417 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1422 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1423 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1424 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1433 start
= bfd_get_start_address (ibfd
);
1434 start
+= change_start
;
1437 /* Neither the start address nor the flags
1438 need to be set for a core file. */
1439 if (bfd_get_format (obfd
) != bfd_core
)
1443 flags
= bfd_get_file_flags (ibfd
);
1444 flags
|= bfd_flags_to_set
;
1445 flags
&= ~bfd_flags_to_clear
;
1446 flags
&= bfd_applicable_file_flags (obfd
);
1448 if (strip_symbols
== STRIP_ALL
)
1449 flags
&= ~HAS_RELOC
;
1451 if (!bfd_set_start_address (obfd
, start
)
1452 || !bfd_set_file_flags (obfd
, flags
))
1454 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1459 /* Copy architecture of input file to output file. */
1460 iarch
= bfd_get_arch (ibfd
);
1461 imach
= bfd_get_mach (ibfd
);
1464 if (bfd_get_arch_info (ibfd
) == NULL
1465 || bfd_get_arch_info (ibfd
)->arch
== bfd_arch_unknown
)
1467 iarch
= input_arch
->arch
;
1468 imach
= input_arch
->mach
;
1471 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1472 bfd_get_archive_filename (ibfd
));
1474 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1475 && (ibfd
->target_defaulted
1476 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1478 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1479 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1480 bfd_get_archive_filename (ibfd
));
1482 non_fatal (_("Output file cannot represent architecture `%s'"),
1483 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1484 bfd_get_mach (ibfd
)));
1488 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1490 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1494 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1495 && bfd_pei_p (obfd
))
1497 /* Set up PE parameters. */
1498 pe_data_type
*pe
= pe_data (obfd
);
1500 /* Copy PE parameters before changing them. */
1501 if (ibfd
->xvec
->flavour
== bfd_target_coff_flavour
1502 && bfd_pei_p (ibfd
))
1503 pe
->pe_opthdr
= pe_data (ibfd
)->pe_opthdr
;
1505 if (pe_file_alignment
!= (bfd_vma
) -1)
1506 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
1508 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
1510 if (pe_heap_commit
!= (bfd_vma
) -1)
1511 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
1513 if (pe_heap_reserve
!= (bfd_vma
) -1)
1514 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
1516 if (pe_image_base
!= (bfd_vma
) -1)
1517 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
1519 if (pe_section_alignment
!= (bfd_vma
) -1)
1520 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
1522 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
1524 if (pe_stack_commit
!= (bfd_vma
) -1)
1525 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
1527 if (pe_stack_reserve
!= (bfd_vma
) -1)
1528 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_reserve
;
1530 if (pe_subsystem
!= -1)
1531 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
1533 if (pe_major_subsystem_version
!= -1)
1534 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
1536 if (pe_minor_subsystem_version
!= -1)
1537 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
1539 if (pe_file_alignment
> pe_section_alignment
)
1541 char file_alignment
[20], section_alignment
[20];
1543 sprintf_vma (file_alignment
, pe_file_alignment
);
1544 sprintf_vma (section_alignment
, pe_section_alignment
);
1545 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1547 file_alignment
, section_alignment
);
1554 if (osympp
!= isympp
)
1560 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1563 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1567 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1568 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1571 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1575 /* BFD mandates that all output sections be created and sizes set before
1576 any output is done. Thus, we traverse all sections multiple times. */
1577 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1579 if (!extract_symbol
)
1580 setup_bfd_headers (ibfd
, obfd
);
1582 if (add_sections
!= NULL
)
1584 struct section_add
*padd
;
1585 struct section_list
*pset
;
1587 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1591 pset
= find_section_list (padd
->name
, FALSE
);
1595 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1596 if (pset
!= NULL
&& pset
->set_flags
)
1597 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1599 /* bfd_make_section_with_flags() does not return very helpful
1600 error codes, so check for the most likely user error first. */
1601 if (bfd_get_section_by_name (obfd
, padd
->name
))
1603 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1604 _("can't add section '%s'"), padd
->name
);
1609 /* We use LINKER_CREATED here so that the backend hooks
1610 will create any special section type information,
1611 instead of presuming we know what we're doing merely
1612 because we set the flags. */
1613 padd
->section
= bfd_make_section_with_flags
1614 (obfd
, padd
->name
, flags
| SEC_LINKER_CREATED
);
1615 if (padd
->section
== NULL
)
1617 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1618 _("can't create section `%s'"),
1624 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1626 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1632 if (pset
->change_vma
!= CHANGE_IGNORE
)
1633 if (! bfd_set_section_vma (obfd
, padd
->section
,
1636 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1640 if (pset
->change_lma
!= CHANGE_IGNORE
)
1642 padd
->section
->lma
= pset
->lma_val
;
1644 if (! bfd_set_section_alignment
1645 (obfd
, padd
->section
,
1646 bfd_section_alignment (obfd
, padd
->section
)))
1648 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1656 if (gnu_debuglink_filename
!= NULL
)
1658 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1659 (obfd
, gnu_debuglink_filename
);
1661 if (gnu_debuglink_section
== NULL
)
1663 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1664 _("cannot create debug link section `%s'"),
1665 gnu_debuglink_filename
);
1669 /* Special processing for PE format files. We
1670 have no way to distinguish PE from COFF here. */
1671 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1673 bfd_vma debuglink_vma
;
1674 asection
* highest_section
;
1677 /* The PE spec requires that all sections be adjacent and sorted
1678 in ascending order of VMA. It also specifies that debug
1679 sections should be last. This is despite the fact that debug
1680 sections are not loaded into memory and so in theory have no
1683 This means that the debuglink section must be given a non-zero
1684 VMA which makes it contiguous with other debug sections. So
1685 walk the current section list, find the section with the
1686 highest VMA and start the debuglink section after that one. */
1687 for (sec
= obfd
->sections
, highest_section
= NULL
;
1691 && (highest_section
== NULL
1692 || sec
->vma
> highest_section
->vma
))
1693 highest_section
= sec
;
1695 if (highest_section
)
1696 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1697 + highest_section
->size
,
1698 /* FIXME: We ought to be using
1699 COFF_PAGE_SIZE here or maybe
1700 bfd_get_section_alignment() (if it
1701 was set) but since this is for PE
1702 and we know the required alignment
1703 it is easier just to hard code it. */
1706 /* Umm, not sure what to do in this case. */
1707 debuglink_vma
= 0x1000;
1709 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1713 if (bfd_count_sections (obfd
) != 0
1714 && (gap_fill_set
|| pad_to_set
))
1719 /* We must fill in gaps between the sections and/or we must pad
1720 the last section to a specified address. We do this by
1721 grabbing a list of the sections, sorting them by VMA, and
1722 increasing the section sizes as required to fill the gaps.
1723 We write out the gap contents below. */
1725 c
= bfd_count_sections (obfd
);
1726 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
1728 bfd_map_over_sections (obfd
, get_sections
, &set
);
1730 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1732 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
1733 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1737 for (i
= 0; i
< c
- 1; i
++)
1741 bfd_vma gap_start
, gap_stop
;
1743 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1744 if ((flags
& SEC_HAS_CONTENTS
) == 0
1745 || (flags
& SEC_LOAD
) == 0)
1748 size
= bfd_section_size (obfd
, osections
[i
]);
1749 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1750 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1751 if (gap_start
< gap_stop
)
1753 if (! bfd_set_section_size (obfd
, osections
[i
],
1754 size
+ (gap_stop
- gap_start
)))
1756 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
1757 _("Can't fill gap after section"));
1761 gaps
[i
] = gap_stop
- gap_start
;
1762 if (max_gap
< gap_stop
- gap_start
)
1763 max_gap
= gap_stop
- gap_start
;
1773 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1774 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1775 if (lma
+ size
< pad_to
)
1777 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1780 bfd_nonfatal_message (NULL
, obfd
, osections
[c
- 1],
1781 _("can't add padding"));
1786 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1787 if (max_gap
< pad_to
- (lma
+ size
))
1788 max_gap
= pad_to
- (lma
+ size
);
1794 /* Symbol filtering must happen after the output sections
1795 have been created, but before their contents are set. */
1797 if (convert_debugging
)
1798 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, FALSE
);
1800 if (strip_symbols
== STRIP_DEBUG
1801 || strip_symbols
== STRIP_ALL
1802 || strip_symbols
== STRIP_UNNEEDED
1803 || strip_symbols
== STRIP_NONDEBUG
1804 || discard_locals
!= LOCALS_UNDEF
1806 || htab_elements (strip_specific_htab
) != 0
1807 || htab_elements (keep_specific_htab
) != 0
1808 || htab_elements (localize_specific_htab
) != 0
1809 || htab_elements (globalize_specific_htab
) != 0
1810 || htab_elements (keepglobal_specific_htab
) != 0
1811 || htab_elements (weaken_specific_htab
) != 0
1812 || prefix_symbols_string
1815 || convert_debugging
1816 || change_leading_char
1817 || remove_leading_char
1818 || redefine_sym_list
1821 /* Mark symbols used in output relocations so that they
1822 are kept, even if they are local labels or static symbols.
1824 Note we iterate over the input sections examining their
1825 relocations since the relocations for the output sections
1826 haven't been set yet. mark_symbols_used_in_relocations will
1827 ignore input sections which have no corresponding output
1829 if (strip_symbols
!= STRIP_ALL
)
1830 bfd_map_over_sections (ibfd
,
1831 mark_symbols_used_in_relocations
,
1833 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1834 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1837 if (convert_debugging
&& dhandle
!= NULL
)
1839 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1846 bfd_set_symtab (obfd
, osympp
, symcount
);
1848 /* This has to happen after the symbol table has been set. */
1849 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1851 if (add_sections
!= NULL
)
1853 struct section_add
*padd
;
1855 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1857 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1860 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1866 if (gnu_debuglink_filename
!= NULL
)
1868 if (! bfd_fill_in_gnu_debuglink_section
1869 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1871 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1872 _("cannot fill debug link section `%s'"),
1873 gnu_debuglink_filename
);
1878 if (gap_fill_set
|| pad_to_set
)
1883 /* Fill in the gaps. */
1886 buf
= (bfd_byte
*) xmalloc (max_gap
);
1887 memset (buf
, gap_fill
, max_gap
);
1889 c
= bfd_count_sections (obfd
);
1890 for (i
= 0; i
< c
; i
++)
1898 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1909 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1912 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
1923 /* Do not copy backend data if --extract-symbol is passed; anything
1924 that needs to look at the section contents will fail. */
1928 /* Allow the BFD backend to copy any private data it understands
1929 from the input BFD to the output BFD. This is done last to
1930 permit the routine to look at the filtered symbol table, which is
1931 important for the ECOFF code at least. */
1932 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1934 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1935 _("error copying private BFD data"));
1939 /* Switch to the alternate machine code. We have to do this at the
1940 very end, because we only initialize the header when we create
1941 the first section. */
1942 if (use_alt_mach_code
!= 0)
1944 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1946 non_fatal (_("this target does not support %lu alternative machine codes"),
1948 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1950 non_fatal (_("treating that number as an absolute e_machine value instead"));
1951 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
1954 non_fatal (_("ignoring the alternative value"));
1961 /* Read each archive element in turn from IBFD, copy the
1962 contents to temp file, and keep the temp file handle.
1963 If 'force_output_target' is TRUE then make sure that
1964 all elements in the new archive are of the type
1968 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
1969 bfd_boolean force_output_target
,
1970 const bfd_arch_info_type
*input_arch
)
1974 struct name_list
*next
;
1978 bfd
**ptr
= &obfd
->archive_head
;
1981 const char *filename
;
1983 /* Make a temp directory to hold the contents. */
1984 dir
= make_tempdir (bfd_get_filename (obfd
));
1986 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1989 obfd
->has_armap
= ibfd
->has_armap
;
1990 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
1994 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1996 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1999 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
2003 while (!status
&& this_element
!= NULL
)
2009 int stat_status
= 0;
2010 bfd_boolean del
= TRUE
;
2012 /* Create an output file for this member. */
2013 output_name
= concat (dir
, "/",
2014 bfd_get_filename (this_element
), (char *) 0);
2016 /* If the file already exists, make another temp dir. */
2017 if (stat (output_name
, &buf
) >= 0)
2019 output_name
= make_tempdir (output_name
);
2020 if (output_name
== NULL
)
2021 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2024 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
2025 l
->name
= output_name
;
2029 output_name
= concat (output_name
, "/",
2030 bfd_get_filename (this_element
), (char *) 0);
2035 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
2037 if (stat_status
!= 0)
2038 non_fatal (_("internal stat error on %s"),
2039 bfd_get_filename (this_element
));
2042 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
2043 l
->name
= output_name
;
2048 if (bfd_check_format (this_element
, bfd_object
))
2050 /* PR binutils/3110: Cope with archives
2051 containing multiple target types. */
2052 if (force_output_target
)
2053 output_bfd
= bfd_openw (output_name
, output_target
);
2055 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
2057 if (output_bfd
== NULL
)
2059 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2064 del
= ! copy_object (this_element
, output_bfd
, input_arch
);
2067 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
2069 if (!bfd_close (output_bfd
))
2071 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2072 /* Error in new object file. Don't change archive. */
2077 goto copy_unknown_element
;
2081 bfd_nonfatal_message (NULL
, this_element
, NULL
,
2082 _("Unable to recognise the format of file"));
2084 output_bfd
= bfd_openw (output_name
, output_target
);
2085 copy_unknown_element
:
2086 del
= !copy_unknown_object (this_element
, output_bfd
);
2087 if (!bfd_close_all_done (output_bfd
))
2089 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2090 /* Error in new object file. Don't change archive. */
2097 unlink (output_name
);
2102 if (preserve_dates
&& stat_status
== 0)
2103 set_times (output_name
, &buf
);
2105 /* Open the newly output file and attach to our list. */
2106 output_bfd
= bfd_openr (output_name
, output_target
);
2108 l
->obfd
= output_bfd
;
2111 ptr
= &output_bfd
->archive_next
;
2113 last_element
= this_element
;
2115 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
2117 bfd_close (last_element
);
2122 filename
= bfd_get_filename (obfd
);
2123 if (!bfd_close (obfd
))
2126 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2130 filename
= bfd_get_filename (ibfd
);
2131 if (!bfd_close (ibfd
))
2134 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2138 /* Delete all the files that we opened. */
2139 for (l
= list
; l
!= NULL
; l
= l
->next
)
2141 if (l
->obfd
== NULL
)
2145 bfd_close (l
->obfd
);
2153 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
2155 /* This is only relevant to Coff targets. */
2156 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
2159 && bfd_get_flavour (input_bfd
) == bfd_target_coff_flavour
)
2160 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
2161 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
2165 /* The top-level control. */
2168 copy_file (const char *input_filename
, const char *output_filename
,
2169 const char *input_target
, const char *output_target
,
2170 const bfd_arch_info_type
*input_arch
)
2173 char **obj_matching
;
2174 char **core_matching
;
2175 off_t size
= get_file_size (input_filename
);
2180 non_fatal (_("error: the input file '%s' is empty"),
2186 /* To allow us to do "strip *" without dying on the first
2187 non-object file, failures are nonfatal. */
2188 ibfd
= bfd_openr (input_filename
, input_target
);
2191 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2196 if (bfd_check_format (ibfd
, bfd_archive
))
2198 bfd_boolean force_output_target
;
2201 /* bfd_get_target does not return the correct value until
2202 bfd_check_format succeeds. */
2203 if (output_target
== NULL
)
2205 output_target
= bfd_get_target (ibfd
);
2206 force_output_target
= FALSE
;
2209 force_output_target
= TRUE
;
2211 obfd
= bfd_openw (output_filename
, output_target
);
2214 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2218 /* This is a no-op on non-Coff targets. */
2219 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2221 copy_archive (ibfd
, obfd
, output_target
, force_output_target
, input_arch
);
2223 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
2228 /* bfd_get_target does not return the correct value until
2229 bfd_check_format succeeds. */
2230 if (output_target
== NULL
)
2231 output_target
= bfd_get_target (ibfd
);
2233 obfd
= bfd_openw (output_filename
, output_target
);
2236 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2240 /* This is a no-op on non-Coff targets. */
2241 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2243 if (! copy_object (ibfd
, obfd
, input_arch
))
2246 if (!bfd_close (obfd
))
2249 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2253 if (!bfd_close (ibfd
))
2256 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2262 bfd_error_type obj_error
= bfd_get_error ();
2263 bfd_error_type core_error
;
2265 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
2267 /* This probably can't happen.. */
2268 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2269 free (obj_matching
);
2273 core_error
= bfd_get_error ();
2274 /* Report the object error in preference to the core error. */
2275 if (obj_error
!= core_error
)
2276 bfd_set_error (obj_error
);
2278 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2280 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2282 list_matching_formats (obj_matching
);
2283 free (obj_matching
);
2285 if (core_error
== bfd_error_file_ambiguously_recognized
)
2287 list_matching_formats (core_matching
);
2288 free (core_matching
);
2295 /* Add a name to the section renaming list. */
2298 add_section_rename (const char * old_name
, const char * new_name
,
2301 section_rename
* srename
;
2303 /* Check for conflicts first. */
2304 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
2305 if (strcmp (srename
->old_name
, old_name
) == 0)
2307 /* Silently ignore duplicate definitions. */
2308 if (strcmp (srename
->new_name
, new_name
) == 0
2309 && srename
->flags
== flags
)
2312 fatal (_("Multiple renames of section %s"), old_name
);
2315 srename
= (section_rename
*) xmalloc (sizeof (* srename
));
2317 srename
->old_name
= old_name
;
2318 srename
->new_name
= new_name
;
2319 srename
->flags
= flags
;
2320 srename
->next
= section_rename_list
;
2322 section_rename_list
= srename
;
2325 /* Check the section rename list for a new name of the input section
2326 ISECTION. Return the new name if one is found.
2327 Also set RETURNED_FLAGS to the flags to be used for this section. */
2330 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2331 flagword
* returned_flags
)
2333 const char * old_name
= bfd_section_name (ibfd
, isection
);
2334 section_rename
* srename
;
2336 /* Default to using the flags of the input section. */
2337 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2339 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
2340 if (strcmp (srename
->old_name
, old_name
) == 0)
2342 if (srename
->flags
!= (flagword
) -1)
2343 * returned_flags
= srename
->flags
;
2345 return srename
->new_name
;
2351 /* Once each of the sections is copied, we may still need to do some
2352 finalization work for private section headers. Do that here. */
2355 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2357 /* Allow the BFD backend to copy any private data it understands
2358 from the input section to the output section. */
2359 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2362 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
2363 _("error in private header data"));
2367 /* All went well. */
2371 /* Create a section in OBFD with the same
2372 name and attributes as ISECTION in IBFD. */
2375 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2377 bfd
*obfd
= (bfd
*) obfdarg
;
2378 struct section_list
*p
;
2386 char *prefix
= NULL
;
2387 bfd_boolean make_nobits
;
2389 if (is_strip_section (ibfd
, isection
))
2392 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2396 /* Get the, possibly new, name of the output section. */
2397 name
= find_section_rename (ibfd
, isection
, & flags
);
2399 /* Prefix sections. */
2400 if ((prefix_alloc_sections_string
)
2401 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2402 prefix
= prefix_alloc_sections_string
;
2403 else if (prefix_sections_string
)
2404 prefix
= prefix_sections_string
;
2410 n
= (char *) xmalloc (strlen (prefix
) + strlen (name
) + 1);
2416 make_nobits
= FALSE
;
2417 if (p
!= NULL
&& p
->set_flags
)
2418 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2419 else if (strip_symbols
== STRIP_NONDEBUG
2420 && (flags
& (SEC_ALLOC
| SEC_GROUP
)) != 0
2421 && !(ibfd
->xvec
->flavour
== bfd_target_elf_flavour
2422 && elf_section_type (isection
) == SHT_NOTE
))
2424 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
2425 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2429 /* Twiddle the input section flags so that it seems to
2430 elf.c:copy_private_bfd_data that section flags have not
2431 changed between input and output sections. This hack
2432 prevents wholesale rewriting of the program headers. */
2433 isection
->flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
2437 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2439 if (osection
== NULL
)
2441 err
= _("failed to create output section");
2446 elf_section_type (osection
) = SHT_NOBITS
;
2448 size
= bfd_section_size (ibfd
, isection
);
2450 size
= (size
+ interleave
- 1) / interleave
* copy_width
;
2451 else if (extract_symbol
)
2453 if (! bfd_set_section_size (obfd
, osection
, size
))
2455 err
= _("failed to set size");
2459 vma
= bfd_section_vma (ibfd
, isection
);
2460 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2462 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2465 vma
+= change_section_address
;
2467 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2469 err
= _("failed to set vma");
2473 lma
= isection
->lma
;
2474 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2476 if (p
->change_lma
== CHANGE_MODIFY
)
2478 else if (p
->change_lma
== CHANGE_SET
)
2484 lma
+= change_section_address
;
2486 osection
->lma
= lma
;
2488 /* FIXME: This is probably not enough. If we change the LMA we
2489 may have to recompute the header for the file as well. */
2490 if (!bfd_set_section_alignment (obfd
,
2492 bfd_section_alignment (ibfd
, isection
)))
2494 err
= _("failed to set alignment");
2498 /* Copy merge entity size. */
2499 osection
->entsize
= isection
->entsize
;
2501 /* This used to be mangle_section; we do here to avoid using
2502 bfd_get_section_by_name since some formats allow multiple
2503 sections with the same name. */
2504 isection
->output_section
= osection
;
2505 isection
->output_offset
= 0;
2507 /* Do not copy backend data if --extract-symbol is passed; anything
2508 that needs to look at the section contents will fail. */
2512 if ((isection
->flags
& SEC_GROUP
) != 0)
2514 asymbol
*gsym
= group_signature (isection
);
2518 gsym
->flags
|= BSF_KEEP
;
2519 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2520 elf_group_id (isection
) = gsym
;
2524 /* Allow the BFD backend to copy any private data it understands
2525 from the input section to the output section. */
2526 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2528 err
= _("failed to copy private data");
2532 /* All went well. */
2537 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
2540 /* Copy the data of input section ISECTION of IBFD
2541 to an output section with the same name in OBFD.
2542 If stripping then don't copy any relocation info. */
2545 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2547 bfd
*obfd
= (bfd
*) obfdarg
;
2548 struct section_list
*p
;
2556 /* If we have already failed earlier on,
2557 do not keep on generating complaints now. */
2561 if (is_strip_section (ibfd
, isection
))
2564 flags
= bfd_get_section_flags (ibfd
, isection
);
2565 if ((flags
& SEC_GROUP
) != 0)
2568 osection
= isection
->output_section
;
2569 size
= bfd_get_section_size (isection
);
2571 if (size
== 0 || osection
== 0)
2577 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2579 /* Core files do not need to be relocated. */
2580 if (bfd_get_format (obfd
) == bfd_core
)
2584 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2588 /* Do not complain if the target does not support relocations. */
2589 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2594 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2601 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2604 relpp
= (arelent
**) xmalloc (relsize
);
2605 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2609 bfd_nonfatal_message (NULL
, ibfd
, isection
,
2610 _("relocation count is negative"));
2614 if (strip_symbols
== STRIP_ALL
)
2616 /* Remove relocations which are not in
2617 keep_strip_specific_list. */
2618 arelent
**temp_relpp
;
2619 long temp_relcount
= 0;
2622 temp_relpp
= (arelent
**) xmalloc (relsize
);
2623 for (i
= 0; i
< relcount
; i
++)
2624 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2625 keep_specific_htab
))
2626 temp_relpp
[temp_relcount
++] = relpp
[i
];
2627 relcount
= temp_relcount
;
2632 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2637 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2638 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2640 void *memhunk
= xmalloc (size
);
2642 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2645 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
2651 /* We don't handle leftover bytes (too many possible behaviors,
2652 and we don't know what the user wants). The section length
2653 must be a multiple of the number of bytes to swap. */
2654 if ((size
% reverse_bytes
) == 0)
2659 for (i
= 0; i
< size
; i
+= reverse_bytes
)
2660 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
2662 bfd_byte
*m
= (bfd_byte
*) memhunk
;
2665 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
2666 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
2670 /* User must pad the section up in order to do this. */
2671 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2672 bfd_section_name (ibfd
, isection
), reverse_bytes
);
2677 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2678 char *from
= (char *) memhunk
+ copy_byte
;
2679 char *to
= (char *) memhunk
;
2680 char *end
= (char *) memhunk
+ size
;
2683 for (; from
< end
; from
+= interleave
)
2684 for (i
= 0; i
< copy_width
; i
++)
2687 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
* copy_width
;
2688 osection
->lma
/= interleave
;
2691 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2694 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2699 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2701 void *memhunk
= xmalloc (size
);
2703 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2704 flag--they can just remove the section entirely and add it
2705 back again. However, we do permit them to turn on the
2706 SEC_HAS_CONTENTS flag, and take it to mean that the section
2707 contents should be zeroed out. */
2709 memset (memhunk
, 0, size
);
2710 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2713 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
2720 /* Get all the sections. This is used when --gap-fill or --pad-to is
2724 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2726 asection
***secppp
= (asection
***) secppparg
;
2728 **secppp
= osection
;
2732 /* Sort sections by VMA. This is called via qsort, and is used when
2733 --gap-fill or --pad-to is used. We force non loadable or empty
2734 sections to the front, where they are easier to ignore. */
2737 compare_section_lma (const void *arg1
, const void *arg2
)
2739 const asection
*const *sec1
= (const asection
* const *) arg1
;
2740 const asection
*const *sec2
= (const asection
* const *) arg2
;
2741 flagword flags1
, flags2
;
2743 /* Sort non loadable sections to the front. */
2744 flags1
= (*sec1
)->flags
;
2745 flags2
= (*sec2
)->flags
;
2746 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2747 || (flags1
& SEC_LOAD
) == 0)
2749 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2750 && (flags2
& SEC_LOAD
) != 0)
2755 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2756 || (flags2
& SEC_LOAD
) == 0)
2760 /* Sort sections by LMA. */
2761 if ((*sec1
)->lma
> (*sec2
)->lma
)
2763 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2766 /* Sort sections with the same LMA by size. */
2767 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2769 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2775 /* Mark all the symbols which will be used in output relocations with
2776 the BSF_KEEP flag so that those symbols will not be stripped.
2778 Ignore relocations which will not appear in the output file. */
2781 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2783 asymbol
**symbols
= (asymbol
**) symbolsarg
;
2788 /* Ignore an input section with no corresponding output section. */
2789 if (isection
->output_section
== NULL
)
2792 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2795 /* Do not complain if the target does not support relocations. */
2796 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2798 bfd_fatal (bfd_get_filename (ibfd
));
2804 relpp
= (arelent
**) xmalloc (relsize
);
2805 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2807 bfd_fatal (bfd_get_filename (ibfd
));
2809 /* Examine each symbol used in a relocation. If it's not one of the
2810 special bfd section symbols, then mark it with BSF_KEEP. */
2811 for (i
= 0; i
< relcount
; i
++)
2813 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2814 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2815 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2816 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2823 /* Write out debugging information. */
2826 write_debugging_info (bfd
*obfd
, void *dhandle
,
2827 long *symcountp ATTRIBUTE_UNUSED
,
2828 asymbol
***symppp ATTRIBUTE_UNUSED
)
2830 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2831 return write_ieee_debugging_info (obfd
, dhandle
);
2833 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2834 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2836 bfd_byte
*syms
, *strings
;
2837 bfd_size_type symsize
, stringsize
;
2838 asection
*stabsec
, *stabstrsec
;
2841 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2846 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2847 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2848 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2850 || stabstrsec
== NULL
2851 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2852 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2853 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2854 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2856 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2857 _("can't create debugging section"));
2861 /* We can get away with setting the section contents now because
2862 the next thing the caller is going to do is copy over the
2863 real sections. We may someday have to split the contents
2864 setting out of this function. */
2865 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2866 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2869 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2870 _("can't set debugging section contents"));
2877 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2878 _("don't know how to write debugging information for %s"),
2879 bfd_get_target (obfd
));
2884 strip_main (int argc
, char *argv
[])
2886 char *input_target
= NULL
;
2887 char *output_target
= NULL
;
2888 bfd_boolean show_version
= FALSE
;
2889 bfd_boolean formats_info
= FALSE
;
2892 struct section_list
*p
;
2893 char *output_file
= NULL
;
2895 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2896 strip_options
, (int *) 0)) != EOF
)
2901 input_target
= optarg
;
2904 output_target
= optarg
;
2907 input_target
= output_target
= optarg
;
2910 p
= find_section_list (optarg
, TRUE
);
2912 sections_removed
= TRUE
;
2915 strip_symbols
= STRIP_ALL
;
2919 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2920 strip_symbols
= STRIP_DEBUG
;
2922 case OPTION_STRIP_UNNEEDED
:
2923 strip_symbols
= STRIP_UNNEEDED
;
2926 add_specific_symbol (optarg
, keep_specific_htab
);
2929 add_specific_symbol (optarg
, strip_specific_htab
);
2932 output_file
= optarg
;
2935 preserve_dates
= TRUE
;
2938 discard_locals
= LOCALS_ALL
;
2941 discard_locals
= LOCALS_START_L
;
2947 show_version
= TRUE
;
2949 case OPTION_FORMATS_INFO
:
2950 formats_info
= TRUE
;
2952 case OPTION_ONLY_KEEP_DEBUG
:
2953 strip_symbols
= STRIP_NONDEBUG
;
2955 case OPTION_KEEP_FILE_SYMBOLS
:
2956 keep_file_symbols
= 1;
2959 /* We've been given a long option. */
2966 strip_usage (stdout
, 0);
2968 strip_usage (stderr
, 1);
2979 print_version ("strip");
2981 /* Default is to strip all symbols. */
2982 if (strip_symbols
== STRIP_UNDEF
2983 && discard_locals
== LOCALS_UNDEF
2984 && htab_elements (strip_specific_htab
) == 0)
2985 strip_symbols
= STRIP_ALL
;
2987 if (output_target
== NULL
)
2988 output_target
= input_target
;
2992 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2993 strip_usage (stderr
, 1);
2995 for (; i
< argc
; i
++)
2997 int hold_status
= status
;
2998 struct stat statbuf
;
3001 if (get_file_size (argv
[i
]) < 1)
3008 /* No need to check the return value of stat().
3009 It has already been checked in get_file_size(). */
3010 stat (argv
[i
], &statbuf
);
3012 if (output_file
== NULL
|| strcmp (argv
[i
], output_file
) == 0)
3013 tmpname
= make_tempname (argv
[i
]);
3015 tmpname
= output_file
;
3017 if (tmpname
== NULL
)
3019 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
3020 _("could not create temporary file to hold stripped copy"));
3026 copy_file (argv
[i
], tmpname
, input_target
, output_target
, NULL
);
3030 set_times (tmpname
, &statbuf
);
3031 if (output_file
!= tmpname
)
3032 status
= (smart_rename (tmpname
,
3033 output_file
? output_file
: argv
[i
],
3034 preserve_dates
) != 0);
3036 status
= hold_status
;
3039 unlink_if_ordinary (tmpname
);
3040 if (output_file
!= tmpname
)
3047 /* Set up PE subsystem. */
3050 set_pe_subsystem (const char *s
)
3052 const char *version
, *subsystem
;
3062 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
3063 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
3064 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
3065 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
3066 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
3067 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
3068 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
3069 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
3070 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
},
3071 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
3077 /* Check for the presence of a version number. */
3078 version
= strchr (s
, ':');
3079 if (version
== NULL
)
3083 int len
= version
- s
;
3087 version
= copy
+ 1 + len
;
3088 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
3090 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
3092 non_fatal (_("%s: bad version in PE subsystem"), s
);
3095 /* Check for numeric subsystem. */
3096 value
= (short) strtol (subsystem
, ©
, 0);
3099 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3100 if (v
[i
].value
== value
)
3102 pe_subsystem
= value
;
3103 set_def
= v
[i
].set_def
;
3109 /* Search for subsystem by name. */
3110 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3111 if (strcmp (subsystem
, v
[i
].name
) == 0)
3113 pe_subsystem
= v
[i
].value
;
3114 set_def
= v
[i
].set_def
;
3122 fatal (_("unknown PE subsystem: %s"), s
);
3127 if (pe_file_alignment
== (bfd_vma
) -1)
3128 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
3129 if (pe_section_alignment
== (bfd_vma
) -1)
3130 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
3135 /* Convert EFI target to PEI target. */
3138 convert_efi_target (char *efi
)
3144 if (strcmp (efi
+ 4, "ia32") == 0)
3146 /* Change ia32 to i386. */
3151 else if (strcmp (efi
+ 4, "x86_64") == 0)
3153 /* Change x86_64 to x86-64. */
3159 copy_main (int argc
, char *argv
[])
3161 char *input_filename
= NULL
;
3162 char *output_filename
= NULL
;
3164 char *input_target
= NULL
;
3165 char *output_target
= NULL
;
3166 bfd_boolean show_version
= FALSE
;
3167 bfd_boolean change_warn
= TRUE
;
3168 bfd_boolean formats_info
= FALSE
;
3170 struct section_list
*p
;
3171 struct stat statbuf
;
3172 const bfd_arch_info_type
*input_arch
= NULL
;
3174 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
3175 copy_options
, (int *) 0)) != EOF
)
3180 copy_byte
= atoi (optarg
);
3182 fatal (_("byte number must be non-negative"));
3186 input_arch
= bfd_scan_arch (optarg
);
3187 if (input_arch
== NULL
)
3188 fatal (_("architecture %s unknown"), optarg
);
3194 interleave
= atoi (optarg
);
3196 fatal (_("interleave must be positive"));
3202 case OPTION_INTERLEAVE_WIDTH
:
3203 copy_width
= atoi (optarg
);
3205 fatal(_("interleave width must be positive"));
3209 case 's': /* "source" - 'I' is preferred */
3210 input_target
= optarg
;
3214 case 'd': /* "destination" - 'O' is preferred */
3215 output_target
= optarg
;
3219 input_target
= output_target
= optarg
;
3223 p
= find_section_list (optarg
, TRUE
);
3225 fatal (_("%s both copied and removed"), optarg
);
3227 sections_copied
= TRUE
;
3231 p
= find_section_list (optarg
, TRUE
);
3233 fatal (_("%s both copied and removed"), optarg
);
3235 sections_removed
= TRUE
;
3239 strip_symbols
= STRIP_ALL
;
3243 strip_symbols
= STRIP_DEBUG
;
3246 case OPTION_STRIP_UNNEEDED
:
3247 strip_symbols
= STRIP_UNNEEDED
;
3250 case OPTION_ONLY_KEEP_DEBUG
:
3251 strip_symbols
= STRIP_NONDEBUG
;
3254 case OPTION_KEEP_FILE_SYMBOLS
:
3255 keep_file_symbols
= 1;
3258 case OPTION_ADD_GNU_DEBUGLINK
:
3259 gnu_debuglink_filename
= optarg
;
3263 add_specific_symbol (optarg
, keep_specific_htab
);
3267 add_specific_symbol (optarg
, strip_specific_htab
);
3270 case OPTION_STRIP_UNNEEDED_SYMBOL
:
3271 add_specific_symbol (optarg
, strip_unneeded_htab
);
3275 add_specific_symbol (optarg
, localize_specific_htab
);
3278 case OPTION_GLOBALIZE_SYMBOL
:
3279 add_specific_symbol (optarg
, globalize_specific_htab
);
3283 add_specific_symbol (optarg
, keepglobal_specific_htab
);
3287 add_specific_symbol (optarg
, weaken_specific_htab
);
3291 preserve_dates
= TRUE
;
3299 discard_locals
= LOCALS_ALL
;
3303 discard_locals
= LOCALS_START_L
;
3311 show_version
= TRUE
;
3314 case OPTION_FORMATS_INFO
:
3315 formats_info
= TRUE
;
3322 case OPTION_ADD_SECTION
:
3326 struct section_add
*pa
;
3329 s
= strchr (optarg
, '=');
3332 fatal (_("bad format for %s"), "--add-section");
3334 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
3335 pa
->name
= xstrndup (optarg
, s
- optarg
);
3336 pa
->filename
= s
+ 1;
3338 /* We don't use get_file_size so that we can do
3339 --add-section .note.GNU_stack=/dev/null
3340 get_file_size doesn't work on /dev/null. */
3342 f
= fopen (pa
->filename
, FOPEN_RB
);
3344 fatal (_("cannot open: %s: %s"),
3345 pa
->filename
, strerror (errno
));
3349 pa
->contents
= (bfd_byte
*) xmalloc (alloc
);
3357 pa
->contents
= (bfd_byte
*) xrealloc (pa
->contents
, alloc
);
3360 got
= fread (pa
->contents
+ off
, 1, alloc
- off
, f
);
3362 fatal (_("%s: fread failed"), pa
->filename
);
3371 pa
->next
= add_sections
;
3376 case OPTION_CHANGE_START
:
3377 change_start
= parse_vma (optarg
, "--change-start");
3380 case OPTION_CHANGE_SECTION_ADDRESS
:
3381 case OPTION_CHANGE_SECTION_LMA
:
3382 case OPTION_CHANGE_SECTION_VMA
:
3387 char *option
= NULL
;
3389 enum change_action what
= CHANGE_IGNORE
;
3393 case OPTION_CHANGE_SECTION_ADDRESS
:
3394 option
= "--change-section-address";
3396 case OPTION_CHANGE_SECTION_LMA
:
3397 option
= "--change-section-lma";
3399 case OPTION_CHANGE_SECTION_VMA
:
3400 option
= "--change-section-vma";
3404 s
= strchr (optarg
, '=');
3407 s
= strchr (optarg
, '+');
3410 s
= strchr (optarg
, '-');
3412 fatal (_("bad format for %s"), option
);
3417 name
= (char *) xmalloc (len
+ 1);
3418 strncpy (name
, optarg
, len
);
3421 p
= find_section_list (name
, TRUE
);
3423 val
= parse_vma (s
+ 1, option
);
3427 case '=': what
= CHANGE_SET
; break;
3428 case '-': val
= - val
; /* Drop through. */
3429 case '+': what
= CHANGE_MODIFY
; break;
3434 case OPTION_CHANGE_SECTION_ADDRESS
:
3435 p
->change_vma
= what
;
3439 case OPTION_CHANGE_SECTION_LMA
:
3440 p
->change_lma
= what
;
3444 case OPTION_CHANGE_SECTION_VMA
:
3445 p
->change_vma
= what
;
3452 case OPTION_CHANGE_ADDRESSES
:
3453 change_section_address
= parse_vma (optarg
, "--change-addresses");
3454 change_start
= change_section_address
;
3457 case OPTION_CHANGE_WARNINGS
:
3461 case OPTION_CHANGE_LEADING_CHAR
:
3462 change_leading_char
= TRUE
;
3465 case OPTION_DEBUGGING
:
3466 convert_debugging
= TRUE
;
3469 case OPTION_GAP_FILL
:
3471 bfd_vma gap_fill_vma
;
3473 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
3474 gap_fill
= (bfd_byte
) gap_fill_vma
;
3475 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
3479 sprintf_vma (buff
, gap_fill_vma
);
3481 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3484 gap_fill_set
= TRUE
;
3488 case OPTION_NO_CHANGE_WARNINGS
:
3489 change_warn
= FALSE
;
3493 pad_to
= parse_vma (optarg
, "--pad-to");
3497 case OPTION_REMOVE_LEADING_CHAR
:
3498 remove_leading_char
= TRUE
;
3501 case OPTION_REDEFINE_SYM
:
3503 /* Push this redefinition onto redefine_symbol_list. */
3507 const char *nextarg
;
3508 char *source
, *target
;
3510 s
= strchr (optarg
, '=');
3512 fatal (_("bad format for %s"), "--redefine-sym");
3515 source
= (char *) xmalloc (len
+ 1);
3516 strncpy (source
, optarg
, len
);
3520 len
= strlen (nextarg
);
3521 target
= (char *) xmalloc (len
+ 1);
3522 strcpy (target
, nextarg
);
3524 redefine_list_append ("--redefine-sym", source
, target
);
3531 case OPTION_REDEFINE_SYMS
:
3532 add_redefine_syms_file (optarg
);
3535 case OPTION_SET_SECTION_FLAGS
:
3541 s
= strchr (optarg
, '=');
3543 fatal (_("bad format for %s"), "--set-section-flags");
3546 name
= (char *) xmalloc (len
+ 1);
3547 strncpy (name
, optarg
, len
);
3550 p
= find_section_list (name
, TRUE
);
3552 p
->set_flags
= TRUE
;
3553 p
->flags
= parse_flags (s
+ 1);
3557 case OPTION_RENAME_SECTION
:
3560 const char *eq
, *fl
;
3565 eq
= strchr (optarg
, '=');
3567 fatal (_("bad format for %s"), "--rename-section");
3571 fatal (_("bad format for %s"), "--rename-section");
3573 old_name
= (char *) xmalloc (len
+ 1);
3574 strncpy (old_name
, optarg
, len
);
3578 fl
= strchr (eq
, ',');
3581 flags
= parse_flags (fl
+ 1);
3591 fatal (_("bad format for %s"), "--rename-section");
3593 new_name
= (char *) xmalloc (len
+ 1);
3594 strncpy (new_name
, eq
, len
);
3597 add_section_rename (old_name
, new_name
, flags
);
3601 case OPTION_SET_START
:
3602 set_start
= parse_vma (optarg
, "--set-start");
3603 set_start_set
= TRUE
;
3606 case OPTION_SREC_LEN
:
3607 Chunk
= parse_vma (optarg
, "--srec-len");
3610 case OPTION_SREC_FORCES3
:
3614 case OPTION_STRIP_SYMBOLS
:
3615 add_specific_symbols (optarg
, strip_specific_htab
);
3618 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3619 add_specific_symbols (optarg
, strip_unneeded_htab
);
3622 case OPTION_KEEP_SYMBOLS
:
3623 add_specific_symbols (optarg
, keep_specific_htab
);
3626 case OPTION_LOCALIZE_HIDDEN
:
3627 localize_hidden
= TRUE
;
3630 case OPTION_LOCALIZE_SYMBOLS
:
3631 add_specific_symbols (optarg
, localize_specific_htab
);
3634 case OPTION_LONG_SECTION_NAMES
:
3635 if (!strcmp ("enable", optarg
))
3636 long_section_names
= ENABLE
;
3637 else if (!strcmp ("disable", optarg
))
3638 long_section_names
= DISABLE
;
3639 else if (!strcmp ("keep", optarg
))
3640 long_section_names
= KEEP
;
3642 fatal (_("unknown long section names option '%s'"), optarg
);
3645 case OPTION_GLOBALIZE_SYMBOLS
:
3646 add_specific_symbols (optarg
, globalize_specific_htab
);
3649 case OPTION_KEEPGLOBAL_SYMBOLS
:
3650 add_specific_symbols (optarg
, keepglobal_specific_htab
);
3653 case OPTION_WEAKEN_SYMBOLS
:
3654 add_specific_symbols (optarg
, weaken_specific_htab
);
3657 case OPTION_ALT_MACH_CODE
:
3658 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3659 if (use_alt_mach_code
== 0)
3660 fatal (_("unable to parse alternative machine code"));
3663 case OPTION_PREFIX_SYMBOLS
:
3664 prefix_symbols_string
= optarg
;
3667 case OPTION_PREFIX_SECTIONS
:
3668 prefix_sections_string
= optarg
;
3671 case OPTION_PREFIX_ALLOC_SECTIONS
:
3672 prefix_alloc_sections_string
= optarg
;
3675 case OPTION_READONLY_TEXT
:
3676 bfd_flags_to_set
|= WP_TEXT
;
3677 bfd_flags_to_clear
&= ~WP_TEXT
;
3680 case OPTION_WRITABLE_TEXT
:
3681 bfd_flags_to_clear
|= WP_TEXT
;
3682 bfd_flags_to_set
&= ~WP_TEXT
;
3686 bfd_flags_to_set
|= D_PAGED
;
3687 bfd_flags_to_clear
&= ~D_PAGED
;
3691 bfd_flags_to_clear
|= D_PAGED
;
3692 bfd_flags_to_set
&= ~D_PAGED
;
3695 case OPTION_EXTRACT_SYMBOL
:
3696 extract_symbol
= TRUE
;
3699 case OPTION_REVERSE_BYTES
:
3701 int prev
= reverse_bytes
;
3703 reverse_bytes
= atoi (optarg
);
3704 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
3705 fatal (_("number of bytes to reverse must be positive and even"));
3707 if (prev
&& prev
!= reverse_bytes
)
3708 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3713 case OPTION_FILE_ALIGNMENT
:
3714 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
3720 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
3722 || (*end
!= '.' && *end
!= '\0'))
3723 non_fatal (_("%s: invalid reserve value for --heap"),
3725 else if (*end
!= '\0')
3727 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
3729 non_fatal (_("%s: invalid commit value for --heap"),
3735 case OPTION_IMAGE_BASE
:
3736 pe_image_base
= parse_vma (optarg
, "--image-base");
3739 case OPTION_SECTION_ALIGNMENT
:
3740 pe_section_alignment
= parse_vma (optarg
,
3741 "--section-alignment");
3744 case OPTION_SUBSYSTEM
:
3745 set_pe_subsystem (optarg
);
3751 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
3753 || (*end
!= '.' && *end
!= '\0'))
3754 non_fatal (_("%s: invalid reserve value for --stack"),
3756 else if (*end
!= '\0')
3758 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
3760 non_fatal (_("%s: invalid commit value for --stack"),
3767 /* We've been given a long option. */
3772 copy_usage (stdout
, 0);
3775 copy_usage (stderr
, 1);
3786 print_version ("objcopy");
3788 if (interleave
&& copy_byte
== -1)
3789 fatal (_("interleave start byte must be set with --byte"));
3791 if (copy_byte
>= interleave
)
3792 fatal (_("byte number must be less than interleave"));
3794 if (copy_width
> interleave
- copy_byte
)
3795 fatal (_("interleave width must be less than or equal to interleave - byte`"));
3797 if (optind
== argc
|| optind
+ 2 < argc
)
3798 copy_usage (stderr
, 1);
3800 input_filename
= argv
[optind
];
3801 if (optind
+ 1 < argc
)
3802 output_filename
= argv
[optind
+ 1];
3804 /* Default is to strip no symbols. */
3805 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3806 strip_symbols
= STRIP_NONE
;
3808 if (output_target
== NULL
)
3809 output_target
= input_target
;
3811 /* Convert input EFI target to PEI target. */
3812 if (input_target
!= NULL
3813 && strncmp (input_target
, "efi-", 4) == 0)
3817 efi
= xstrdup (output_target
+ 4);
3818 if (strncmp (efi
, "bsdrv-", 6) == 0
3819 || strncmp (efi
, "rtdrv-", 6) == 0)
3821 else if (strncmp (efi
, "app-", 4) != 0)
3822 fatal (_("unknown input EFI target: %s"), input_target
);
3825 convert_efi_target (efi
);
3828 /* Convert output EFI target to PEI target. */
3829 if (output_target
!= NULL
3830 && strncmp (output_target
, "efi-", 4) == 0)
3834 efi
= xstrdup (output_target
+ 4);
3835 if (strncmp (efi
, "app-", 4) == 0)
3837 if (pe_subsystem
== -1)
3838 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
3840 else if (strncmp (efi
, "bsdrv-", 6) == 0)
3842 if (pe_subsystem
== -1)
3843 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
3846 else if (strncmp (efi
, "rtdrv-", 6) == 0)
3848 if (pe_subsystem
== -1)
3849 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
3853 fatal (_("unknown output EFI target: %s"), output_target
);
3855 if (pe_file_alignment
== (bfd_vma
) -1)
3856 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
3857 if (pe_section_alignment
== (bfd_vma
) -1)
3858 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
3860 output_target
= efi
;
3861 convert_efi_target (efi
);
3865 if (stat (input_filename
, & statbuf
) < 0)
3866 fatal (_("warning: could not locate '%s'. System error message: %s"),
3867 input_filename
, strerror (errno
));
3869 /* If there is no destination file, or the source and destination files
3870 are the same, then create a temp and rename the result into the input. */
3871 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3872 tmpname
= make_tempname (input_filename
);
3874 tmpname
= output_filename
;
3876 if (tmpname
== NULL
)
3877 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3878 input_filename
, strerror (errno
));
3880 copy_file (input_filename
, tmpname
, input_target
, output_target
, input_arch
);
3884 set_times (tmpname
, &statbuf
);
3885 if (tmpname
!= output_filename
)
3886 status
= (smart_rename (tmpname
, input_filename
,
3887 preserve_dates
) != 0);
3890 unlink_if_ordinary (tmpname
);
3894 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3898 if (p
->change_vma
!= CHANGE_IGNORE
)
3902 sprintf_vma (buff
, p
->vma_val
);
3904 /* xgettext:c-format */
3905 non_fatal (_("%s %s%c0x%s never used"),
3906 "--change-section-vma",
3908 p
->change_vma
== CHANGE_SET
? '=' : '+',
3912 if (p
->change_lma
!= CHANGE_IGNORE
)
3916 sprintf_vma (buff
, p
->lma_val
);
3918 /* xgettext:c-format */
3919 non_fatal (_("%s %s%c0x%s never used"),
3920 "--change-section-lma",
3922 p
->change_lma
== CHANGE_SET
? '=' : '+',
3933 main (int argc
, char *argv
[])
3935 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3936 setlocale (LC_MESSAGES
, "");
3938 #if defined (HAVE_SETLOCALE)
3939 setlocale (LC_CTYPE
, "");
3941 bindtextdomain (PACKAGE
, LOCALEDIR
);
3942 textdomain (PACKAGE
);
3944 program_name
= argv
[0];
3945 xmalloc_set_program_name (program_name
);
3947 START_PROGRESS (program_name
, 0);
3949 expandargv (&argc
, &argv
);
3951 strip_symbols
= STRIP_UNDEF
;
3952 discard_locals
= LOCALS_UNDEF
;
3955 set_default_bfd_target ();
3959 int i
= strlen (program_name
);
3960 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3961 /* Drop the .exe suffix, if any. */
3962 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3965 program_name
[i
] = '\0';
3968 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3971 create_symbol_htabs ();
3974 strip_main (argc
, argv
);
3976 copy_main (argc
, argv
);
3978 END_PROGRESS (program_name
);