1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
27 #include "libiberty.h"
30 #include "filenames.h"
35 #include "coff/internal.h"
38 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
39 header in generic PE code. */
40 #include "coff/i386.h"
43 static bfd_vma pe_file_alignment = (bfd_vma) -1;
44 static bfd_vma pe_heap_commit = (bfd_vma) -1;
45 static bfd_vma pe_heap_reserve = (bfd_vma) -1;
46 static bfd_vma pe_image_base = (bfd_vma) -1;
47 static bfd_vma pe_section_alignment = (bfd_vma) -1;
48 static bfd_vma pe_stack_commit = (bfd_vma) -1;
49 static bfd_vma pe_stack_reserve = (bfd_vma) -1;
50 static short pe_subsystem = -1;
51 static short pe_major_subsystem_version = -1;
52 static short pe_minor_subsystem_version = -1;
54 struct is_specified_symbol_predicate_data
60 /* A list to support redefine_sym. */
65 struct redefine_node *next;
68 typedef struct section_rename
70 const char * old_name;
71 const char * new_name;
73 struct section_rename * next;
77 /* List of sections to be renamed. */
78 static section_rename *section_rename_list;
80 static asymbol **isympp = NULL; /* Input symbols. */
81 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
83 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
84 static int copy_byte = -1;
85 static int interleave = 4;
87 static bfd_boolean verbose; /* Print file and target names. */
88 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
89 static int status = 0; /* Exit status. */
94 STRIP_NONE, /* Don't strip. */
95 STRIP_DEBUG, /* Strip all debugger symbols. */
96 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
97 STRIP_NONDEBUG, /* Strip everything but debug info. */
98 STRIP_ALL /* Strip all symbols. */
101 /* Which symbols to remove. */
102 static enum strip_action strip_symbols;
107 LOCALS_START_L, /* Discard locals starting with L. */
108 LOCALS_ALL /* Discard all locals. */
111 /* Which local symbols to remove. Overrides STRIP_ALL. */
112 static enum locals_action discard_locals;
114 /* What kind of change to perform. */
122 /* Structure used to hold lists of sections and actions to take. */
125 struct section_list * next; /* Next section to change. */
126 const char * name; /* Section name. */
127 bfd_boolean used; /* Whether this entry was used. */
128 bfd_boolean remove; /* Whether to remove this section. */
129 bfd_boolean copy; /* Whether to copy this section. */
130 enum change_action change_vma;/* Whether to change or set VMA. */
131 bfd_vma vma_val; /* Amount to change by or set to. */
132 enum change_action change_lma;/* Whether to change or set LMA. */
133 bfd_vma lma_val; /* Amount to change by or set to. */
134 bfd_boolean set_flags; /* Whether to set the section flags. */
135 flagword flags; /* What to set the section flags to. */
138 static struct section_list *change_sections;
140 /* TRUE if some sections are to be removed. */
141 static bfd_boolean sections_removed;
143 /* TRUE if only some sections are to be copied. */
144 static bfd_boolean sections_copied;
146 /* Changes to the start address. */
147 static bfd_vma change_start = 0;
148 static bfd_boolean set_start_set = FALSE;
149 static bfd_vma set_start;
151 /* Changes to section addresses. */
152 static bfd_vma change_section_address = 0;
154 /* Filling gaps between sections. */
155 static bfd_boolean gap_fill_set = FALSE;
156 static bfd_byte gap_fill = 0;
158 /* Pad to a given address. */
159 static bfd_boolean pad_to_set = FALSE;
160 static bfd_vma pad_to;
162 /* Use alternative machine code? */
163 static unsigned long use_alt_mach_code = 0;
165 /* Output BFD flags user wants to set or clear */
166 static flagword bfd_flags_to_set;
167 static flagword bfd_flags_to_clear;
169 /* List of sections to add. */
172 /* Next section to add. */
173 struct section_add *next;
174 /* Name of section to add. */
176 /* Name of file holding section contents. */
177 const char *filename;
180 /* Contents of file. */
182 /* BFD section, after it has been added. */
186 /* List of sections to add to the output BFD. */
187 static struct section_add *add_sections;
189 /* If non-NULL the argument to --add-gnu-debuglink.
190 This should be the filename to store in the .gnu_debuglink section. */
191 static const char * gnu_debuglink_filename = NULL;
193 /* Whether to convert debugging information. */
194 static bfd_boolean convert_debugging = FALSE;
196 /* Whether to change the leading character in symbol names. */
197 static bfd_boolean change_leading_char = FALSE;
199 /* Whether to remove the leading character from global symbol names. */
200 static bfd_boolean remove_leading_char = FALSE;
202 /* Whether to permit wildcard in symbol comparison. */
203 static bfd_boolean wildcard = FALSE;
205 /* True if --localize-hidden is in effect. */
206 static bfd_boolean localize_hidden = FALSE;
208 /* List of symbols to strip, keep, localize, keep-global, weaken,
210 static htab_t strip_specific_htab = NULL;
211 static htab_t strip_unneeded_htab = NULL;
212 static htab_t keep_specific_htab = NULL;
213 static htab_t localize_specific_htab = NULL;
214 static htab_t globalize_specific_htab = NULL;
215 static htab_t keepglobal_specific_htab = NULL;
216 static htab_t weaken_specific_htab = NULL;
217 static struct redefine_node *redefine_sym_list = NULL;
219 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
220 static bfd_boolean weaken = FALSE;
222 /* If this is TRUE, we retain BSF_FILE symbols. */
223 static bfd_boolean keep_file_symbols = FALSE;
225 /* Prefix symbols/sections. */
226 static char *prefix_symbols_string = 0;
227 static char *prefix_sections_string = 0;
228 static char *prefix_alloc_sections_string = 0;
230 /* True if --extract-symbol was passed on the command line. */
231 static bfd_boolean extract_symbol = FALSE;
233 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
234 of <reverse_bytes> bytes within each output section. */
235 static int reverse_bytes = 0;
237 /* For Coff objects, we may want to allow or disallow long section names,
238 or preserve them where found in the inputs. Debug info relies on them. */
239 enum long_section_name_handling
246 /* The default long section handling mode is to preserve them.
247 This is also the only behaviour for 'strip'. */
248 static enum long_section_name_handling long_section_names = KEEP;
250 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
251 enum command_line_switch
253 OPTION_ADD_SECTION=150,
254 OPTION_CHANGE_ADDRESSES,
255 OPTION_CHANGE_LEADING_CHAR,
257 OPTION_CHANGE_SECTION_ADDRESS,
258 OPTION_CHANGE_SECTION_LMA,
259 OPTION_CHANGE_SECTION_VMA,
260 OPTION_CHANGE_WARNINGS,
263 OPTION_NO_CHANGE_WARNINGS,
265 OPTION_REMOVE_LEADING_CHAR,
266 OPTION_SET_SECTION_FLAGS,
268 OPTION_STRIP_UNNEEDED,
271 OPTION_REDEFINE_SYMS,
274 OPTION_STRIP_SYMBOLS,
275 OPTION_STRIP_UNNEEDED_SYMBOL,
276 OPTION_STRIP_UNNEEDED_SYMBOLS,
278 OPTION_LOCALIZE_HIDDEN,
279 OPTION_LOCALIZE_SYMBOLS,
280 OPTION_LONG_SECTION_NAMES,
281 OPTION_GLOBALIZE_SYMBOL,
282 OPTION_GLOBALIZE_SYMBOLS,
283 OPTION_KEEPGLOBAL_SYMBOLS,
284 OPTION_WEAKEN_SYMBOLS,
285 OPTION_RENAME_SECTION,
286 OPTION_ALT_MACH_CODE,
287 OPTION_PREFIX_SYMBOLS,
288 OPTION_PREFIX_SECTIONS,
289 OPTION_PREFIX_ALLOC_SECTIONS,
291 OPTION_ADD_GNU_DEBUGLINK,
292 OPTION_ONLY_KEEP_DEBUG,
293 OPTION_KEEP_FILE_SYMBOLS,
294 OPTION_READONLY_TEXT,
295 OPTION_WRITABLE_TEXT,
298 OPTION_EXTRACT_SYMBOL,
299 OPTION_REVERSE_BYTES,
300 OPTION_FILE_ALIGNMENT,
303 OPTION_SECTION_ALIGNMENT,
308 /* Options to handle if running as "strip". */
310 static struct option strip_options[] =
312 {"discard-all", no_argument, 0, 'x'},
313 {"discard-locals", no_argument, 0, 'X'},
314 {"format", required_argument, 0, 'F'}, /* Obsolete */
315 {"help", no_argument, 0, 'h'},
316 {"info", no_argument, 0, OPTION_FORMATS_INFO},
317 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
318 {"input-target", required_argument, 0, 'I'},
319 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
320 {"keep-symbol", required_argument, 0, 'K'},
321 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
322 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
323 {"output-target", required_argument, 0, 'O'},
324 {"output-file", required_argument, 0, 'o'},
325 {"preserve-dates", no_argument, 0, 'p'},
326 {"remove-section", required_argument, 0, 'R'},
327 {"strip-all", no_argument, 0, 's'},
328 {"strip-debug", no_argument, 0, 'S'},
329 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
330 {"strip-symbol", required_argument, 0, 'N'},
331 {"target", required_argument, 0, 'F'},
332 {"verbose", no_argument, 0, 'v'},
333 {"version", no_argument, 0, 'V'},
334 {"wildcard", no_argument, 0, 'w'},
335 {0, no_argument, 0, 0}
338 /* Options to handle if running as "objcopy". */
340 static struct option copy_options[] =
342 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
343 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
344 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
345 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
346 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
347 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
348 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
349 {"binary-architecture", required_argument, 0, 'B'},
350 {"byte", required_argument, 0, 'b'},
351 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
352 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
353 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
354 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
355 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
356 {"change-start", required_argument, 0, OPTION_CHANGE_START},
357 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
358 {"debugging", no_argument, 0, OPTION_DEBUGGING},
359 {"discard-all", no_argument, 0, 'x'},
360 {"discard-locals", no_argument, 0, 'X'},
361 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
362 {"format", required_argument, 0, 'F'}, /* Obsolete */
363 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
364 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
365 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
366 {"help", no_argument, 0, 'h'},
367 {"impure", no_argument, 0, OPTION_IMPURE},
368 {"info", no_argument, 0, OPTION_FORMATS_INFO},
369 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
370 {"input-target", required_argument, 0, 'I'},
371 {"interleave", required_argument, 0, 'i'},
372 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
373 {"keep-global-symbol", required_argument, 0, 'G'},
374 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
375 {"keep-symbol", required_argument, 0, 'K'},
376 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
377 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
378 {"localize-symbol", required_argument, 0, 'L'},
379 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
380 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
381 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
382 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
383 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
384 {"only-section", required_argument, 0, 'j'},
385 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
386 {"output-target", required_argument, 0, 'O'},
387 {"pad-to", required_argument, 0, OPTION_PAD_TO},
388 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
389 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
390 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
391 {"preserve-dates", no_argument, 0, 'p'},
392 {"pure", no_argument, 0, OPTION_PURE},
393 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
394 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
395 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
396 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
397 {"remove-section", required_argument, 0, 'R'},
398 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
399 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
400 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
401 {"set-start", required_argument, 0, OPTION_SET_START},
402 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
403 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
404 {"strip-all", no_argument, 0, 'S'},
405 {"strip-debug", no_argument, 0, 'g'},
406 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
407 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
408 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
409 {"strip-symbol", required_argument, 0, 'N'},
410 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
411 {"target", required_argument, 0, 'F'},
412 {"verbose", no_argument, 0, 'v'},
413 {"version", no_argument, 0, 'V'},
414 {"weaken", no_argument, 0, OPTION_WEAKEN},
415 {"weaken-symbol", required_argument, 0, 'W'},
416 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
417 {"wildcard", no_argument, 0, 'w'},
418 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
419 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
420 {"heap", required_argument, 0, OPTION_HEAP},
421 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
422 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
423 {"stack", required_argument, 0, OPTION_STACK},
424 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
425 {0, no_argument, 0, 0}
429 extern char *program_name;
431 /* This flag distinguishes between strip and objcopy:
432 1 means this is 'strip'; 0 means this is 'objcopy'.
433 -1 means if we should use argv[0] to decide. */
436 /* The maximum length of an S record. This variable is declared in srec.c
437 and can be modified by the --srec-len parameter. */
438 extern unsigned int Chunk;
440 /* Restrict the generation of Srecords to type S3 only.
441 This variable is declare in bfd/srec.c and can be toggled
442 on by the --srec-forceS3 command line switch. */
443 extern bfd_boolean S3Forced;
445 /* Defined in bfd/binary.c. Used to set architecture and machine of input
447 extern enum bfd_architecture bfd_external_binary_architecture;
448 extern unsigned long bfd_external_machine;
450 /* Forward declarations. */
451 static void setup_section (bfd *, asection *, void *);
452 static void setup_bfd_headers (bfd *, bfd *);
453 static void copy_section (bfd *, asection *, void *);
454 static void get_sections (bfd *, asection *, void *);
455 static int compare_section_lma (const void *, const void *);
456 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
457 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
458 static const char *lookup_sym_redefinition (const char *);
461 copy_usage (FILE *stream, int exit_status)
463 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
464 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
465 fprintf (stream, _(" The options are:\n"));
466 fprintf (stream, _("\
467 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
468 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
469 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
470 -F --target <bfdname> Set both input and output format to <bfdname>\n\
471 --debugging Convert debugging information, if possible\n\
472 -p --preserve-dates Copy modified/access timestamps to the output\n\
473 -j --only-section <name> Only copy section <name> into the output\n\
474 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
475 -R --remove-section <name> Remove section <name> from the output\n\
476 -S --strip-all Remove all symbol and relocation information\n\
477 -g --strip-debug Remove all debugging symbols & sections\n\
478 --strip-unneeded Remove all symbols not needed by relocations\n\
479 -N --strip-symbol <name> Do not copy symbol <name>\n\
480 --strip-unneeded-symbol <name>\n\
481 Do not copy symbol <name> unless needed by\n\
483 --only-keep-debug Strip everything but the debug information\n\
484 --extract-symbol Remove section contents but keep symbols\n\
485 -K --keep-symbol <name> Do not strip symbol <name>\n\
486 --keep-file-symbols Do not strip file symbol(s)\n\
487 --localize-hidden Turn all ELF hidden symbols into locals\n\
488 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
489 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
490 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
491 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
492 --weaken Force all global symbols to be marked as weak\n\
493 -w --wildcard Permit wildcard in symbol comparison\n\
494 -x --discard-all Remove all non-global symbols\n\
495 -X --discard-locals Remove any compiler-generated symbols\n\
496 -i --interleave <number> Only copy one out of every <number> bytes\n\
497 -b --byte <num> Select byte <num> in every interleaved block\n\
498 --gap-fill <val> Fill gaps between sections with <val>\n\
499 --pad-to <addr> Pad the last section up to address <addr>\n\
500 --set-start <addr> Set the start address to <addr>\n\
501 {--change-start|--adjust-start} <incr>\n\
502 Add <incr> to the start address\n\
503 {--change-addresses|--adjust-vma} <incr>\n\
504 Add <incr> to LMA, VMA and start addresses\n\
505 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
506 Change LMA and VMA of section <name> by <val>\n\
507 --change-section-lma <name>{=|+|-}<val>\n\
508 Change the LMA of section <name> by <val>\n\
509 --change-section-vma <name>{=|+|-}<val>\n\
510 Change the VMA of section <name> by <val>\n\
511 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
512 Warn if a named section does not exist\n\
513 --set-section-flags <name>=<flags>\n\
514 Set section <name>'s properties to <flags>\n\
515 --add-section <name>=<file> Add section <name> found in <file> to output\n\
516 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
517 --long-section-names {enable|disable|keep}\n\
518 Handle long section names in Coff objects.\n\
519 --change-leading-char Force output format's leading character style\n\
520 --remove-leading-char Remove leading character from global symbols\n\
521 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
522 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
523 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
525 --srec-len <number> Restrict the length of generated Srecords\n\
526 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
527 --strip-symbols <file> -N for all symbols listed in <file>\n\
528 --strip-unneeded-symbols <file>\n\
529 --strip-unneeded-symbol for all symbols listed\n\
531 --keep-symbols <file> -K for all symbols listed in <file>\n\
532 --localize-symbols <file> -L for all symbols listed in <file>\n\
533 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
534 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
535 --weaken-symbols <file> -W for all symbols listed in <file>\n\
536 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
537 --writable-text Mark the output text as writable\n\
538 --readonly-text Make the output text write protected\n\
539 --pure Mark the output file as demand paged\n\
540 --impure Mark the output file as impure\n\
541 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
542 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
543 --prefix-alloc-sections <prefix>\n\
544 Add <prefix> to start of every allocatable\n\
546 --file-alignment <num> Set PE file alignment to <num>\n\
547 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
549 --image-base <address> Set PE image base to <address>\n\
550 --section-alignment <num> Set PE section alignment to <num>\n\
551 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
553 --subsystem <name>[:<version>]\n\
554 Set PE subsystem to <name> [& <version>]\n]\
555 -v --verbose List all object files modified\n\
556 @<file> Read options from <file>\n\
557 -V --version Display this program's version number\n\
558 -h --help Display this output\n\
559 --info List object formats & architectures supported\n\
561 list_supported_targets (program_name, stream);
562 if (REPORT_BUGS_TO[0] && exit_status == 0)
563 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
568 strip_usage (FILE *stream, int exit_status)
570 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
571 fprintf (stream, _(" Removes symbols and sections from files\n"));
572 fprintf (stream, _(" The options are:\n"));
573 fprintf (stream, _("\
574 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
575 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
576 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
577 -p --preserve-dates Copy modified/access timestamps to the output\n\
578 -R --remove-section=<name> Remove section <name> from the output\n\
579 -s --strip-all Remove all symbol and relocation information\n\
580 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
581 --strip-unneeded Remove all symbols not needed by relocations\n\
582 --only-keep-debug Strip everything but the debug information\n\
583 -N --strip-symbol=<name> Do not copy symbol <name>\n\
584 -K --keep-symbol=<name> Do not strip symbol <name>\n\
585 --keep-file-symbols Do not strip file symbol(s)\n\
586 -w --wildcard Permit wildcard in symbol comparison\n\
587 -x --discard-all Remove all non-global symbols\n\
588 -X --discard-locals Remove any compiler-generated symbols\n\
589 -v --verbose List all object files modified\n\
590 -V --version Display this program's version number\n\
591 -h --help Display this output\n\
592 --info List object formats & architectures supported\n\
593 -o <file> Place stripped output into <file>\n\
596 list_supported_targets (program_name, stream);
597 if (REPORT_BUGS_TO[0] && exit_status == 0)
598 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
602 /* Parse section flags into a flagword, with a fatal error if the
603 string can't be parsed. */
606 parse_flags (const char *s)
616 snext = strchr (s, ',');
626 #define PARSE_FLAG(fname,fval) \
627 else if (strncasecmp (fname, s, len) == 0) ret |= fval
628 PARSE_FLAG ("alloc", SEC_ALLOC);
629 PARSE_FLAG ("load", SEC_LOAD);
630 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
631 PARSE_FLAG ("readonly", SEC_READONLY);
632 PARSE_FLAG ("debug", SEC_DEBUGGING);
633 PARSE_FLAG ("code", SEC_CODE);
634 PARSE_FLAG ("data", SEC_DATA);
635 PARSE_FLAG ("rom", SEC_ROM);
636 PARSE_FLAG ("share", SEC_COFF_SHARED);
637 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
643 copy = (char *) xmalloc (len + 1);
644 strncpy (copy, s, len);
646 non_fatal (_("unrecognized section flag `%s'"), copy);
647 fatal (_("supported flags: %s"),
648 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
658 /* Find and optionally add an entry in the change_sections list. */
660 static struct section_list *
661 find_section_list (const char *name, bfd_boolean add)
663 struct section_list *p;
665 for (p = change_sections; p != NULL; p = p->next)
666 if (strcmp (p->name, name) == 0)
672 p = (struct section_list *) xmalloc (sizeof (struct section_list));
677 p->change_vma = CHANGE_IGNORE;
678 p->change_lma = CHANGE_IGNORE;
681 p->set_flags = FALSE;
684 p->next = change_sections;
690 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
693 eq_string (const void *s1, const void *s2)
695 return strcmp ((const char *) s1, (const char *) s2) == 0;
699 create_symbol_htab (void)
701 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
705 create_symbol_htabs (void)
707 strip_specific_htab = create_symbol_htab ();
708 strip_unneeded_htab = create_symbol_htab ();
709 keep_specific_htab = create_symbol_htab ();
710 localize_specific_htab = create_symbol_htab ();
711 globalize_specific_htab = create_symbol_htab ();
712 keepglobal_specific_htab = create_symbol_htab ();
713 weaken_specific_htab = create_symbol_htab ();
716 /* Add a symbol to strip_specific_list. */
719 add_specific_symbol (const char *name, htab_t htab)
721 *htab_find_slot (htab, name, INSERT) = (char *) name;
724 /* Add symbols listed in `filename' to strip_specific_list. */
726 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
727 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
730 add_specific_symbols (const char *filename, htab_t htab)
736 unsigned int line_count;
738 size = get_file_size (filename);
745 buffer = (char *) xmalloc (size + 2);
746 f = fopen (filename, FOPEN_RT);
748 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
750 if (fread (buffer, 1, size, f) == 0 || ferror (f))
751 fatal (_("%s: fread failed"), filename);
754 buffer [size] = '\n';
755 buffer [size + 1] = '\0';
759 for (line = buffer; * line != '\0'; line ++)
764 int finished = FALSE;
766 for (eol = line;; eol ++)
772 /* Cope with \n\r. */
780 /* Cope with \r\n. */
791 /* Line comment, Terminate the line here, in case a
792 name is present and then allow the rest of the
793 loop to find the real end of the line. */
805 /* A name may now exist somewhere between 'line' and 'eol'.
806 Strip off leading whitespace and trailing whitespace,
807 then add it to the list. */
808 for (name = line; IS_WHITESPACE (* name); name ++)
810 for (name_end = name;
811 (! IS_WHITESPACE (* name_end))
812 && (! IS_LINE_TERMINATOR (* name_end));
816 if (! IS_LINE_TERMINATOR (* name_end))
820 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
823 if (! IS_LINE_TERMINATOR (* extra))
824 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
825 filename, line_count);
831 add_specific_symbol (name, htab);
833 /* Advance line pointer to end of line. The 'eol ++' in the for
834 loop above will then advance us to the start of the next line. */
840 /* See whether a symbol should be stripped or kept
841 based on strip_specific_list and keep_symbols. */
844 is_specified_symbol_predicate (void **slot, void *data)
846 struct is_specified_symbol_predicate_data *d =
847 (struct is_specified_symbol_predicate_data *) data;
848 const char *slot_name = (char *) *slot;
850 if (*slot_name != '!')
852 if (! fnmatch (slot_name, d->name, 0))
855 /* Stop traversal. */
861 if (fnmatch (slot_name + 1, d->name, 0))
864 /* Stop traversal. */
869 /* Continue traversal. */
874 is_specified_symbol (const char *name, htab_t htab)
878 struct is_specified_symbol_predicate_data data;
883 htab_traverse (htab, is_specified_symbol_predicate, &data);
888 return htab_find (htab, name) != NULL;
891 /* Return a pointer to the symbol used as a signature for GROUP. */
894 group_signature (asection *group)
896 bfd *abfd = group->owner;
897 Elf_Internal_Shdr *ghdr;
899 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
902 ghdr = &elf_section_data (group)->this_hdr;
903 if (ghdr->sh_link < elf_numsections (abfd))
905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
906 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
908 if (symhdr->sh_type == SHT_SYMTAB
909 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
910 return isympp[ghdr->sh_info - 1];
915 /* See if a section is being removed. */
918 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
920 if (sections_removed || sections_copied)
922 struct section_list *p;
924 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
926 if (sections_removed && p != NULL && p->remove)
928 if (sections_copied && (p == NULL || ! p->copy))
932 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
934 if (strip_symbols == STRIP_DEBUG
935 || strip_symbols == STRIP_UNNEEDED
936 || strip_symbols == STRIP_ALL
937 || discard_locals == LOCALS_ALL
938 || convert_debugging)
941 if (strip_symbols == STRIP_NONDEBUG)
945 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
951 Group sections look like debugging sections but they are not.
952 (They have a non-zero size but they are not ALLOCated). */
953 if (strip_symbols == STRIP_NONDEBUG)
957 If we are going to strip the group signature symbol, then
958 strip the group section too. */
959 gsym = group_signature (sec);
964 if ((strip_symbols == STRIP_ALL
965 && !is_specified_symbol (gname, keep_specific_htab))
966 || is_specified_symbol (gname, strip_specific_htab))
973 /* Return true if SYM is a hidden symbol. */
976 is_hidden_symbol (asymbol *sym)
978 elf_symbol_type *elf_sym;
980 elf_sym = elf_symbol_from (sym->the_bfd, sym);
982 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
991 /* Choose which symbol entries to copy; put the result in OSYMS.
992 We don't copy in place, because that confuses the relocs.
993 Return the number of symbols to print. */
996 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
997 asymbol **isyms, long symcount)
999 asymbol **from = isyms, **to = osyms;
1000 long src_count = 0, dst_count = 0;
1001 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1003 for (; src_count < symcount; src_count++)
1005 asymbol *sym = from[src_count];
1006 flagword flags = sym->flags;
1007 char *name = (char *) bfd_asymbol_name (sym);
1009 bfd_boolean used_in_reloc = FALSE;
1010 bfd_boolean undefined;
1011 bfd_boolean rem_leading_char;
1012 bfd_boolean add_leading_char;
1014 undefined = bfd_is_und_section (bfd_get_section (sym));
1016 if (redefine_sym_list)
1018 char *old_name, *new_name;
1020 old_name = (char *) bfd_asymbol_name (sym);
1021 new_name = (char *) lookup_sym_redefinition (old_name);
1022 bfd_asymbol_name (sym) = new_name;
1026 /* Check if we will remove the current leading character. */
1028 (name[0] == bfd_get_symbol_leading_char (abfd))
1029 && (change_leading_char
1030 || (remove_leading_char
1031 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1033 || bfd_is_com_section (bfd_get_section (sym)))));
1035 /* Check if we will add a new leading character. */
1038 && (bfd_get_symbol_leading_char (obfd) != '\0')
1039 && (bfd_get_symbol_leading_char (abfd) == '\0'
1040 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1042 /* Short circuit for change_leading_char if we can do it in-place. */
1043 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1045 name[0] = bfd_get_symbol_leading_char (obfd);
1046 bfd_asymbol_name (sym) = name;
1047 rem_leading_char = FALSE;
1048 add_leading_char = FALSE;
1051 /* Remove leading char. */
1052 if (rem_leading_char)
1053 bfd_asymbol_name (sym) = ++name;
1055 /* Add new leading char and/or prefix. */
1056 if (add_leading_char || prefix_symbols_string)
1060 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1061 + strlen (name) + 1);
1062 if (add_leading_char)
1063 *ptr++ = bfd_get_symbol_leading_char (obfd);
1065 if (prefix_symbols_string)
1067 strcpy (ptr, prefix_symbols_string);
1068 ptr += strlen (prefix_symbols_string);
1072 bfd_asymbol_name (sym) = n;
1076 if (strip_symbols == STRIP_ALL)
1078 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1079 || ((flags & BSF_SECTION_SYM) != 0
1080 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1084 used_in_reloc = TRUE;
1086 else if (relocatable /* Relocatable file. */
1087 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1088 || bfd_is_com_section (bfd_get_section (sym))))
1090 else if (bfd_decode_symclass (sym) == 'I')
1091 /* Global symbols in $idata sections need to be retained
1092 even if relocatable is FALSE. External users of the
1093 library containing the $idata section may reference these
1096 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1097 || (flags & BSF_WEAK) != 0
1099 || bfd_is_com_section (bfd_get_section (sym)))
1100 keep = strip_symbols != STRIP_UNNEEDED;
1101 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1102 keep = (strip_symbols != STRIP_DEBUG
1103 && strip_symbols != STRIP_UNNEEDED
1104 && ! convert_debugging);
1105 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1106 /* COMDAT sections store special information in local
1107 symbols, so we cannot risk stripping any of them. */
1109 else /* Local symbol. */
1110 keep = (strip_symbols != STRIP_UNNEEDED
1111 && (discard_locals != LOCALS_ALL
1112 && (discard_locals != LOCALS_START_L
1113 || ! bfd_is_local_label (abfd, sym))));
1115 if (keep && is_specified_symbol (name, strip_specific_htab))
1117 /* There are multiple ways to set 'keep' above, but if it
1118 was the relocatable symbol case, then that's an error. */
1121 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1129 && !(flags & BSF_KEEP)
1130 && is_specified_symbol (name, strip_unneeded_htab))
1134 && ((keep_file_symbols && (flags & BSF_FILE))
1135 || is_specified_symbol (name, keep_specific_htab)))
1138 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1143 if ((flags & BSF_GLOBAL) != 0
1144 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1146 sym->flags &= ~ BSF_GLOBAL;
1147 sym->flags |= BSF_WEAK;
1151 && (flags & (BSF_GLOBAL | BSF_WEAK))
1152 && (is_specified_symbol (name, localize_specific_htab)
1153 || (htab_elements (keepglobal_specific_htab) != 0
1154 && ! is_specified_symbol (name, keepglobal_specific_htab))
1155 || (localize_hidden && is_hidden_symbol (sym))))
1157 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1158 sym->flags |= BSF_LOCAL;
1162 && (flags & BSF_LOCAL)
1163 && is_specified_symbol (name, globalize_specific_htab))
1165 sym->flags &= ~ BSF_LOCAL;
1166 sym->flags |= BSF_GLOBAL;
1169 to[dst_count++] = sym;
1173 to[dst_count] = NULL;
1178 /* Find the redefined name of symbol SOURCE. */
1181 lookup_sym_redefinition (const char *source)
1183 struct redefine_node *list;
1185 for (list = redefine_sym_list; list != NULL; list = list->next)
1186 if (strcmp (source, list->source) == 0)
1187 return list->target;
1192 /* Add a node to a symbol redefine list. */
1195 redefine_list_append (const char *cause, const char *source, const char *target)
1197 struct redefine_node **p;
1198 struct redefine_node *list;
1199 struct redefine_node *new_node;
1201 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1203 if (strcmp (source, list->source) == 0)
1204 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1207 if (strcmp (target, list->target) == 0)
1208 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1212 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1214 new_node->source = strdup (source);
1215 new_node->target = strdup (target);
1216 new_node->next = NULL;
1221 /* Handle the --redefine-syms option. Read lines containing "old new"
1222 from the file, and add them to the symbol redefine list. */
1225 add_redefine_syms_file (const char *filename)
1234 file = fopen (filename, "r");
1236 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1237 filename, strerror (errno));
1240 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
1248 /* Collect the input symbol name. */
1249 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1257 buf = (char *) xrealloc (buf, bufsize + 1);
1265 /* Eat white space between the symbol names. */
1266 while (IS_WHITESPACE (c))
1268 if (c == '#' || IS_LINE_TERMINATOR (c))
1273 /* Collect the output symbol name. */
1275 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1283 buf = (char *) xrealloc (buf, bufsize + 1);
1291 /* Eat white space at end of line. */
1292 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1297 if ((c == '\r' && (c = getc (file)) == '\n')
1298 || c == '\n' || c == EOF)
1301 /* Append the redefinition to the list. */
1303 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1314 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1316 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1317 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1320 /* Eat the rest of the line and finish it. */
1321 while (c != '\n' && c != EOF)
1327 fatal (_("%s:%d: premature end of file"), filename, lineno);
1332 /* Copy unkown object file IBFD onto OBFD.
1333 Returns TRUE upon success, FALSE otherwise. */
1336 copy_unknown_object (bfd *ibfd, bfd *obfd)
1344 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1346 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1353 non_fatal (_("stat returns negative size for `%s'"),
1354 bfd_get_archive_filename (ibfd));
1358 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1360 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1365 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1366 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1368 cbuf = (char *) xmalloc (BUFSIZE);
1370 while (ncopied < size)
1372 tocopy = size - ncopied;
1373 if (tocopy > BUFSIZE)
1376 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1377 != (bfd_size_type) tocopy)
1379 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1384 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1385 != (bfd_size_type) tocopy)
1387 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1395 chmod (bfd_get_filename (obfd), buf.st_mode);
1400 /* Copy object file IBFD onto OBFD.
1401 Returns TRUE upon success, FALSE otherwise. */
1404 copy_object (bfd *ibfd, bfd *obfd)
1408 asection **osections = NULL;
1409 asection *gnu_debuglink_section = NULL;
1410 bfd_size_type *gaps = NULL;
1411 bfd_size_type max_gap = 0;
1414 enum bfd_architecture iarch;
1417 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1418 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1419 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1420 fatal (_("Unable to change endianness of input file(s)"));
1422 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1424 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1429 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1430 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1431 bfd_get_filename (obfd), bfd_get_target (obfd));
1440 start = bfd_get_start_address (ibfd);
1441 start += change_start;
1444 /* Neither the start address nor the flags
1445 need to be set for a core file. */
1446 if (bfd_get_format (obfd) != bfd_core)
1450 flags = bfd_get_file_flags (ibfd);
1451 flags |= bfd_flags_to_set;
1452 flags &= ~bfd_flags_to_clear;
1453 flags &= bfd_applicable_file_flags (obfd);
1455 if (strip_symbols == STRIP_ALL)
1456 flags &= ~HAS_RELOC;
1458 if (!bfd_set_start_address (obfd, start)
1459 || !bfd_set_file_flags (obfd, flags))
1461 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1466 /* Copy architecture of input file to output file. */
1467 iarch = bfd_get_arch (ibfd);
1468 imach = bfd_get_mach (ibfd);
1469 if (!bfd_set_arch_mach (obfd, iarch, imach)
1470 && (ibfd->target_defaulted
1471 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1473 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1474 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1475 bfd_get_archive_filename (ibfd));
1477 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1478 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1479 bfd_get_mach (ibfd)));
1483 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1485 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1489 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1490 && bfd_pei_p (obfd))
1492 /* Set up PE parameters. */
1493 pe_data_type *pe = pe_data (obfd);
1495 /* Copy PE parameters before changing them. */
1496 if (ibfd->xvec->flavour == bfd_target_coff_flavour
1497 && bfd_pei_p (ibfd))
1498 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1500 if (pe_file_alignment != (bfd_vma) -1)
1501 pe->pe_opthdr.FileAlignment = pe_file_alignment;
1503 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
1505 if (pe_heap_commit != (bfd_vma) -1)
1506 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
1508 if (pe_heap_reserve != (bfd_vma) -1)
1509 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
1511 if (pe_image_base != (bfd_vma) -1)
1512 pe->pe_opthdr.ImageBase = pe_image_base;
1514 if (pe_section_alignment != (bfd_vma) -1)
1515 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
1517 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
1519 if (pe_stack_commit != (bfd_vma) -1)
1520 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
1522 if (pe_stack_reserve != (bfd_vma) -1)
1523 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
1525 if (pe_subsystem != -1)
1526 pe->pe_opthdr.Subsystem = pe_subsystem;
1528 if (pe_major_subsystem_version != -1)
1529 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
1531 if (pe_minor_subsystem_version != -1)
1532 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
1534 if (pe_file_alignment > pe_section_alignment)
1536 char file_alignment[20], section_alignment[20];
1538 sprintf_vma (file_alignment, pe_file_alignment);
1539 sprintf_vma (section_alignment, pe_section_alignment);
1540 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1542 file_alignment, section_alignment);
1549 if (osympp != isympp)
1555 symsize = bfd_get_symtab_upper_bound (ibfd);
1558 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1562 osympp = isympp = (asymbol **) xmalloc (symsize);
1563 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1566 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1570 /* BFD mandates that all output sections be created and sizes set before
1571 any output is done. Thus, we traverse all sections multiple times. */
1572 bfd_map_over_sections (ibfd, setup_section, obfd);
1574 if (!extract_symbol)
1575 setup_bfd_headers (ibfd, obfd);
1577 if (add_sections != NULL)
1579 struct section_add *padd;
1580 struct section_list *pset;
1582 for (padd = add_sections; padd != NULL; padd = padd->next)
1586 pset = find_section_list (padd->name, FALSE);
1590 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1591 if (pset != NULL && pset->set_flags)
1592 flags = pset->flags | SEC_HAS_CONTENTS;
1594 /* bfd_make_section_with_flags() does not return very helpful
1595 error codes, so check for the most likely user error first. */
1596 if (bfd_get_section_by_name (obfd, padd->name))
1598 bfd_nonfatal_message (NULL, obfd, NULL,
1599 _("can't add section '%s'"), padd->name);
1604 padd->section = bfd_make_section_with_flags (obfd, padd->name, flags);
1605 if (padd->section == NULL)
1607 bfd_nonfatal_message (NULL, obfd, NULL,
1608 _("can't create section `%s'"),
1614 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1616 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1622 if (pset->change_vma != CHANGE_IGNORE)
1623 if (! bfd_set_section_vma (obfd, padd->section,
1626 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1630 if (pset->change_lma != CHANGE_IGNORE)
1632 padd->section->lma = pset->lma_val;
1634 if (! bfd_set_section_alignment
1635 (obfd, padd->section,
1636 bfd_section_alignment (obfd, padd->section)))
1638 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1646 if (gnu_debuglink_filename != NULL)
1648 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1649 (obfd, gnu_debuglink_filename);
1651 if (gnu_debuglink_section == NULL)
1653 bfd_nonfatal_message (NULL, obfd, NULL,
1654 _("cannot create debug link section `%s'"),
1655 gnu_debuglink_filename);
1659 /* Special processing for PE format files. We
1660 have no way to distinguish PE from COFF here. */
1661 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1663 bfd_vma debuglink_vma;
1664 asection * highest_section;
1667 /* The PE spec requires that all sections be adjacent and sorted
1668 in ascending order of VMA. It also specifies that debug
1669 sections should be last. This is despite the fact that debug
1670 sections are not loaded into memory and so in theory have no
1673 This means that the debuglink section must be given a non-zero
1674 VMA which makes it contiguous with other debug sections. So
1675 walk the current section list, find the section with the
1676 highest VMA and start the debuglink section after that one. */
1677 for (sec = obfd->sections, highest_section = NULL;
1681 && (highest_section == NULL
1682 || sec->vma > highest_section->vma))
1683 highest_section = sec;
1685 if (highest_section)
1686 debuglink_vma = BFD_ALIGN (highest_section->vma
1687 + highest_section->size,
1688 /* FIXME: We ought to be using
1689 COFF_PAGE_SIZE here or maybe
1690 bfd_get_section_alignment() (if it
1691 was set) but since this is for PE
1692 and we know the required alignment
1693 it is easier just to hard code it. */
1696 /* Umm, not sure what to do in this case. */
1697 debuglink_vma = 0x1000;
1699 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1703 if (bfd_count_sections (obfd) != 0
1704 && (gap_fill_set || pad_to_set))
1709 /* We must fill in gaps between the sections and/or we must pad
1710 the last section to a specified address. We do this by
1711 grabbing a list of the sections, sorting them by VMA, and
1712 increasing the section sizes as required to fill the gaps.
1713 We write out the gap contents below. */
1715 c = bfd_count_sections (obfd);
1716 osections = (asection **) xmalloc (c * sizeof (asection *));
1718 bfd_map_over_sections (obfd, get_sections, &set);
1720 qsort (osections, c, sizeof (asection *), compare_section_lma);
1722 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
1723 memset (gaps, 0, c * sizeof (bfd_size_type));
1727 for (i = 0; i < c - 1; i++)
1731 bfd_vma gap_start, gap_stop;
1733 flags = bfd_get_section_flags (obfd, osections[i]);
1734 if ((flags & SEC_HAS_CONTENTS) == 0
1735 || (flags & SEC_LOAD) == 0)
1738 size = bfd_section_size (obfd, osections[i]);
1739 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1740 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1741 if (gap_start < gap_stop)
1743 if (! bfd_set_section_size (obfd, osections[i],
1744 size + (gap_stop - gap_start)))
1746 bfd_nonfatal_message (NULL, obfd, osections[i],
1747 _("Can't fill gap after section"));
1751 gaps[i] = gap_stop - gap_start;
1752 if (max_gap < gap_stop - gap_start)
1753 max_gap = gap_stop - gap_start;
1763 lma = bfd_section_lma (obfd, osections[c - 1]);
1764 size = bfd_section_size (obfd, osections[c - 1]);
1765 if (lma + size < pad_to)
1767 if (! bfd_set_section_size (obfd, osections[c - 1],
1770 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
1771 _("can't add padding"));
1776 gaps[c - 1] = pad_to - (lma + size);
1777 if (max_gap < pad_to - (lma + size))
1778 max_gap = pad_to - (lma + size);
1784 /* Symbol filtering must happen after the output sections
1785 have been created, but before their contents are set. */
1787 if (convert_debugging)
1788 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
1790 if (strip_symbols == STRIP_DEBUG
1791 || strip_symbols == STRIP_ALL
1792 || strip_symbols == STRIP_UNNEEDED
1793 || strip_symbols == STRIP_NONDEBUG
1794 || discard_locals != LOCALS_UNDEF
1796 || htab_elements (strip_specific_htab) != 0
1797 || htab_elements (keep_specific_htab) != 0
1798 || htab_elements (localize_specific_htab) != 0
1799 || htab_elements (globalize_specific_htab) != 0
1800 || htab_elements (keepglobal_specific_htab) != 0
1801 || htab_elements (weaken_specific_htab) != 0
1802 || prefix_symbols_string
1805 || convert_debugging
1806 || change_leading_char
1807 || remove_leading_char
1808 || redefine_sym_list
1811 /* Mark symbols used in output relocations so that they
1812 are kept, even if they are local labels or static symbols.
1814 Note we iterate over the input sections examining their
1815 relocations since the relocations for the output sections
1816 haven't been set yet. mark_symbols_used_in_relocations will
1817 ignore input sections which have no corresponding output
1819 if (strip_symbols != STRIP_ALL)
1820 bfd_map_over_sections (ibfd,
1821 mark_symbols_used_in_relocations,
1823 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1824 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1827 if (convert_debugging && dhandle != NULL)
1829 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1836 bfd_set_symtab (obfd, osympp, symcount);
1838 /* This has to happen after the symbol table has been set. */
1839 bfd_map_over_sections (ibfd, copy_section, obfd);
1841 if (add_sections != NULL)
1843 struct section_add *padd;
1845 for (padd = add_sections; padd != NULL; padd = padd->next)
1847 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1850 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1856 if (gnu_debuglink_filename != NULL)
1858 if (! bfd_fill_in_gnu_debuglink_section
1859 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1861 bfd_nonfatal_message (NULL, obfd, NULL,
1862 _("cannot fill debug link section `%s'"),
1863 gnu_debuglink_filename);
1868 if (gap_fill_set || pad_to_set)
1873 /* Fill in the gaps. */
1876 buf = (bfd_byte *) xmalloc (max_gap);
1877 memset (buf, gap_fill, max_gap);
1879 c = bfd_count_sections (obfd);
1880 for (i = 0; i < c; i++)
1888 off = bfd_section_size (obfd, osections[i]) - left;
1899 if (! bfd_set_section_contents (obfd, osections[i], buf,
1902 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
1913 /* Do not copy backend data if --extract-symbol is passed; anything
1914 that needs to look at the section contents will fail. */
1918 /* Allow the BFD backend to copy any private data it understands
1919 from the input BFD to the output BFD. This is done last to
1920 permit the routine to look at the filtered symbol table, which is
1921 important for the ECOFF code at least. */
1922 if (! bfd_copy_private_bfd_data (ibfd, obfd))
1924 bfd_nonfatal_message (NULL, obfd, NULL,
1925 _("error copying private BFD data"));
1929 /* Switch to the alternate machine code. We have to do this at the
1930 very end, because we only initialize the header when we create
1931 the first section. */
1932 if (use_alt_mach_code != 0)
1934 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
1936 non_fatal (_("this target does not support %lu alternative machine codes"),
1938 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1940 non_fatal (_("treating that number as an absolute e_machine value instead"));
1941 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
1944 non_fatal (_("ignoring the alternative value"));
1951 /* Read each archive element in turn from IBFD, copy the
1952 contents to temp file, and keep the temp file handle.
1953 If 'force_output_target' is TRUE then make sure that
1954 all elements in the new archive are of the type
1958 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
1959 bfd_boolean force_output_target)
1963 struct name_list *next;
1967 bfd **ptr = &obfd->archive_head;
1970 const char *filename;
1972 /* Make a temp directory to hold the contents. */
1973 dir = make_tempdir (bfd_get_filename (obfd));
1975 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1978 obfd->has_armap = ibfd->has_armap;
1979 obfd->is_thin_archive = ibfd->is_thin_archive;
1983 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1985 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1988 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1992 while (!status && this_element != NULL)
1998 int stat_status = 0;
1999 bfd_boolean del = TRUE;
2001 /* Create an output file for this member. */
2002 output_name = concat (dir, "/",
2003 bfd_get_filename (this_element), (char *) 0);
2005 /* If the file already exists, make another temp dir. */
2006 if (stat (output_name, &buf) >= 0)
2008 output_name = make_tempdir (output_name);
2009 if (output_name == NULL)
2010 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2013 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2014 l->name = output_name;
2018 output_name = concat (output_name, "/",
2019 bfd_get_filename (this_element), (char *) 0);
2024 stat_status = bfd_stat_arch_elt (this_element, &buf);
2026 if (stat_status != 0)
2027 non_fatal (_("internal stat error on %s"),
2028 bfd_get_filename (this_element));
2031 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2032 l->name = output_name;
2037 if (bfd_check_format (this_element, bfd_object))
2039 /* PR binutils/3110: Cope with archives
2040 containing multiple target types. */
2041 if (force_output_target)
2042 output_bfd = bfd_openw (output_name, output_target);
2044 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
2046 if (output_bfd == NULL)
2048 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2053 del = ! copy_object (this_element, output_bfd);
2056 || bfd_get_arch (this_element) != bfd_arch_unknown)
2058 if (!bfd_close (output_bfd))
2060 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2061 /* Error in new object file. Don't change archive. */
2066 goto copy_unknown_element;
2070 bfd_nonfatal_message (NULL, this_element, NULL,
2071 _("Unable to recognise the format of file"));
2073 output_bfd = bfd_openw (output_name, output_target);
2074 copy_unknown_element:
2075 del = !copy_unknown_object (this_element, output_bfd);
2076 if (!bfd_close_all_done (output_bfd))
2078 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2079 /* Error in new object file. Don't change archive. */
2086 unlink (output_name);
2091 if (preserve_dates && stat_status == 0)
2092 set_times (output_name, &buf);
2094 /* Open the newly output file and attach to our list. */
2095 output_bfd = bfd_openr (output_name, output_target);
2097 l->obfd = output_bfd;
2100 ptr = &output_bfd->archive_next;
2102 last_element = this_element;
2104 this_element = bfd_openr_next_archived_file (ibfd, last_element);
2106 bfd_close (last_element);
2111 filename = bfd_get_filename (obfd);
2112 if (!bfd_close (obfd))
2115 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2119 filename = bfd_get_filename (ibfd);
2120 if (!bfd_close (ibfd))
2123 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2127 /* Delete all the files that we opened. */
2128 for (l = list; l != NULL; l = l->next)
2130 if (l->obfd == NULL)
2134 bfd_close (l->obfd);
2142 set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
2144 /* This is only relevant to Coff targets. */
2145 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
2148 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
2149 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
2150 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
2154 /* The top-level control. */
2157 copy_file (const char *input_filename, const char *output_filename,
2158 const char *input_target, const char *output_target)
2161 char **obj_matching;
2162 char **core_matching;
2163 off_t size = get_file_size (input_filename);
2168 non_fatal (_("error: the input file '%s' is empty"),
2174 /* To allow us to do "strip *" without dying on the first
2175 non-object file, failures are nonfatal. */
2176 ibfd = bfd_openr (input_filename, input_target);
2179 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2184 if (bfd_check_format (ibfd, bfd_archive))
2186 bfd_boolean force_output_target;
2189 /* bfd_get_target does not return the correct value until
2190 bfd_check_format succeeds. */
2191 if (output_target == NULL)
2193 output_target = bfd_get_target (ibfd);
2194 force_output_target = FALSE;
2197 force_output_target = TRUE;
2199 obfd = bfd_openw (output_filename, output_target);
2202 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2206 /* This is a no-op on non-Coff targets. */
2207 set_long_section_mode (obfd, ibfd, long_section_names);
2209 copy_archive (ibfd, obfd, output_target, force_output_target);
2211 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2216 /* bfd_get_target does not return the correct value until
2217 bfd_check_format succeeds. */
2218 if (output_target == NULL)
2219 output_target = bfd_get_target (ibfd);
2221 obfd = bfd_openw (output_filename, output_target);
2224 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2228 /* This is a no-op on non-Coff targets. */
2229 set_long_section_mode (obfd, ibfd, long_section_names);
2231 if (! copy_object (ibfd, obfd))
2234 if (!bfd_close (obfd))
2237 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2241 if (!bfd_close (ibfd))
2244 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2250 bfd_error_type obj_error = bfd_get_error ();
2251 bfd_error_type core_error;
2253 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2255 /* This probably can't happen.. */
2256 if (obj_error == bfd_error_file_ambiguously_recognized)
2257 free (obj_matching);
2261 core_error = bfd_get_error ();
2262 /* Report the object error in preference to the core error. */
2263 if (obj_error != core_error)
2264 bfd_set_error (obj_error);
2266 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2268 if (obj_error == bfd_error_file_ambiguously_recognized)
2270 list_matching_formats (obj_matching);
2271 free (obj_matching);
2273 if (core_error == bfd_error_file_ambiguously_recognized)
2275 list_matching_formats (core_matching);
2276 free (core_matching);
2283 /* Add a name to the section renaming list. */
2286 add_section_rename (const char * old_name, const char * new_name,
2289 section_rename * rename;
2291 /* Check for conflicts first. */
2292 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2293 if (strcmp (rename->old_name, old_name) == 0)
2295 /* Silently ignore duplicate definitions. */
2296 if (strcmp (rename->new_name, new_name) == 0
2297 && rename->flags == flags)
2300 fatal (_("Multiple renames of section %s"), old_name);
2303 rename = (section_rename *) xmalloc (sizeof (* rename));
2305 rename->old_name = old_name;
2306 rename->new_name = new_name;
2307 rename->flags = flags;
2308 rename->next = section_rename_list;
2310 section_rename_list = rename;
2313 /* Check the section rename list for a new name of the input section
2314 ISECTION. Return the new name if one is found.
2315 Also set RETURNED_FLAGS to the flags to be used for this section. */
2318 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2319 flagword * returned_flags)
2321 const char * old_name = bfd_section_name (ibfd, isection);
2322 section_rename * rename;
2324 /* Default to using the flags of the input section. */
2325 * returned_flags = bfd_get_section_flags (ibfd, isection);
2327 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2328 if (strcmp (rename->old_name, old_name) == 0)
2330 if (rename->flags != (flagword) -1)
2331 * returned_flags = rename->flags;
2333 return rename->new_name;
2339 /* Once each of the sections is copied, we may still need to do some
2340 finalization work for private section headers. Do that here. */
2343 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2345 /* Allow the BFD backend to copy any private data it understands
2346 from the input section to the output section. */
2347 if (! bfd_copy_private_header_data (ibfd, obfd))
2350 bfd_nonfatal_message (NULL, ibfd, NULL,
2351 _("error in private header data"));
2355 /* All went well. */
2359 /* Create a section in OBFD with the same
2360 name and attributes as ISECTION in IBFD. */
2363 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2365 bfd *obfd = (bfd *) obfdarg;
2366 struct section_list *p;
2374 char *prefix = NULL;
2375 bfd_boolean make_nobits;
2377 if (is_strip_section (ibfd, isection))
2380 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2384 /* Get the, possibly new, name of the output section. */
2385 name = find_section_rename (ibfd, isection, & flags);
2387 /* Prefix sections. */
2388 if ((prefix_alloc_sections_string)
2389 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2390 prefix = prefix_alloc_sections_string;
2391 else if (prefix_sections_string)
2392 prefix = prefix_sections_string;
2398 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
2404 make_nobits = FALSE;
2405 if (p != NULL && p->set_flags)
2406 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2407 else if (strip_symbols == STRIP_NONDEBUG
2408 && (flags & SEC_ALLOC) != 0
2409 && (ibfd->xvec->flavour != bfd_target_elf_flavour
2410 || elf_section_type (isection) != SHT_NOTE))
2412 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2413 if (obfd->xvec->flavour == bfd_target_elf_flavour)
2417 /* Twiddle the input section flags so that it seems to
2418 elf.c:copy_private_bfd_data that section flags have not
2419 changed between input and output sections. This hack
2420 prevents wholesale rewriting of the program headers. */
2421 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2425 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2427 if (osection == NULL)
2429 err = _("failed to create output section");
2434 elf_section_type (osection) = SHT_NOBITS;
2436 size = bfd_section_size (ibfd, isection);
2438 size = (size + interleave - 1) / interleave;
2439 else if (extract_symbol)
2441 if (! bfd_set_section_size (obfd, osection, size))
2443 err = _("failed to set size");
2447 vma = bfd_section_vma (ibfd, isection);
2448 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2450 else if (p != NULL && p->change_vma == CHANGE_SET)
2453 vma += change_section_address;
2455 if (! bfd_set_section_vma (obfd, osection, vma))
2457 err = _("failed to set vma");
2461 lma = isection->lma;
2462 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2464 if (p->change_lma == CHANGE_MODIFY)
2466 else if (p->change_lma == CHANGE_SET)
2472 lma += change_section_address;
2474 osection->lma = lma;
2476 /* FIXME: This is probably not enough. If we change the LMA we
2477 may have to recompute the header for the file as well. */
2478 if (!bfd_set_section_alignment (obfd,
2480 bfd_section_alignment (ibfd, isection)))
2482 err = _("failed to set alignment");
2486 /* Copy merge entity size. */
2487 osection->entsize = isection->entsize;
2489 /* This used to be mangle_section; we do here to avoid using
2490 bfd_get_section_by_name since some formats allow multiple
2491 sections with the same name. */
2492 isection->output_section = osection;
2493 isection->output_offset = 0;
2495 /* Do not copy backend data if --extract-symbol is passed; anything
2496 that needs to look at the section contents will fail. */
2500 if ((isection->flags & SEC_GROUP) != 0)
2502 asymbol *gsym = group_signature (isection);
2506 gsym->flags |= BSF_KEEP;
2507 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
2508 elf_group_id (isection) = gsym;
2512 /* Allow the BFD backend to copy any private data it understands
2513 from the input section to the output section. */
2514 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2516 err = _("failed to copy private data");
2520 /* All went well. */
2525 bfd_nonfatal_message (NULL, obfd, osection, err);
2528 /* Copy the data of input section ISECTION of IBFD
2529 to an output section with the same name in OBFD.
2530 If stripping then don't copy any relocation info. */
2533 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2535 bfd *obfd = (bfd *) obfdarg;
2536 struct section_list *p;
2544 /* If we have already failed earlier on,
2545 do not keep on generating complaints now. */
2549 if (is_strip_section (ibfd, isection))
2552 flags = bfd_get_section_flags (ibfd, isection);
2553 if ((flags & SEC_GROUP) != 0)
2556 osection = isection->output_section;
2557 size = bfd_get_section_size (isection);
2559 if (size == 0 || osection == 0)
2565 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2567 /* Core files do not need to be relocated. */
2568 if (bfd_get_format (obfd) == bfd_core)
2572 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2576 /* Do not complain if the target does not support relocations. */
2577 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2582 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2589 bfd_set_reloc (obfd, osection, NULL, 0);
2592 relpp = (arelent **) xmalloc (relsize);
2593 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2597 bfd_nonfatal_message (NULL, ibfd, isection,
2598 _("relocation count is negative"));
2602 if (strip_symbols == STRIP_ALL)
2604 /* Remove relocations which are not in
2605 keep_strip_specific_list. */
2606 arelent **temp_relpp;
2607 long temp_relcount = 0;
2610 temp_relpp = (arelent **) xmalloc (relsize);
2611 for (i = 0; i < relcount; i++)
2612 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2613 keep_specific_htab))
2614 temp_relpp [temp_relcount++] = relpp [i];
2615 relcount = temp_relcount;
2620 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2625 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2626 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2628 void *memhunk = xmalloc (size);
2630 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2633 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2639 /* We don't handle leftover bytes (too many possible behaviors,
2640 and we don't know what the user wants). The section length
2641 must be a multiple of the number of bytes to swap. */
2642 if ((size % reverse_bytes) == 0)
2647 for (i = 0; i < size; i += reverse_bytes)
2648 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
2650 bfd_byte *m = (bfd_byte *) memhunk;
2653 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
2654 m[(i + reverse_bytes) - (j + 1)] = b;
2658 /* User must pad the section up in order to do this. */
2659 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2660 bfd_section_name (ibfd, isection), reverse_bytes);
2665 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2666 char *from = (char *) memhunk + copy_byte;
2667 char *to = (char *) memhunk;
2668 char *end = (char *) memhunk + size;
2670 for (; from < end; from += interleave)
2673 size = (size + interleave - 1 - copy_byte) / interleave;
2674 osection->lma /= interleave;
2677 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2680 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2685 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2687 void *memhunk = xmalloc (size);
2689 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2690 flag--they can just remove the section entirely and add it
2691 back again. However, we do permit them to turn on the
2692 SEC_HAS_CONTENTS flag, and take it to mean that the section
2693 contents should be zeroed out. */
2695 memset (memhunk, 0, size);
2696 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2699 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2706 /* Get all the sections. This is used when --gap-fill or --pad-to is
2710 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2712 asection ***secppp = (asection ***) secppparg;
2714 **secppp = osection;
2718 /* Sort sections by VMA. This is called via qsort, and is used when
2719 --gap-fill or --pad-to is used. We force non loadable or empty
2720 sections to the front, where they are easier to ignore. */
2723 compare_section_lma (const void *arg1, const void *arg2)
2725 const asection *const *sec1 = (const asection * const *) arg1;
2726 const asection *const *sec2 = (const asection * const *) arg2;
2727 flagword flags1, flags2;
2729 /* Sort non loadable sections to the front. */
2730 flags1 = (*sec1)->flags;
2731 flags2 = (*sec2)->flags;
2732 if ((flags1 & SEC_HAS_CONTENTS) == 0
2733 || (flags1 & SEC_LOAD) == 0)
2735 if ((flags2 & SEC_HAS_CONTENTS) != 0
2736 && (flags2 & SEC_LOAD) != 0)
2741 if ((flags2 & SEC_HAS_CONTENTS) == 0
2742 || (flags2 & SEC_LOAD) == 0)
2746 /* Sort sections by LMA. */
2747 if ((*sec1)->lma > (*sec2)->lma)
2749 else if ((*sec1)->lma < (*sec2)->lma)
2752 /* Sort sections with the same LMA by size. */
2753 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2755 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2761 /* Mark all the symbols which will be used in output relocations with
2762 the BSF_KEEP flag so that those symbols will not be stripped.
2764 Ignore relocations which will not appear in the output file. */
2767 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2769 asymbol **symbols = (asymbol **) symbolsarg;
2774 /* Ignore an input section with no corresponding output section. */
2775 if (isection->output_section == NULL)
2778 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2781 /* Do not complain if the target does not support relocations. */
2782 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2784 bfd_fatal (bfd_get_filename (ibfd));
2790 relpp = (arelent **) xmalloc (relsize);
2791 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2793 bfd_fatal (bfd_get_filename (ibfd));
2795 /* Examine each symbol used in a relocation. If it's not one of the
2796 special bfd section symbols, then mark it with BSF_KEEP. */
2797 for (i = 0; i < relcount; i++)
2799 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2800 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2801 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2802 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2809 /* Write out debugging information. */
2812 write_debugging_info (bfd *obfd, void *dhandle,
2813 long *symcountp ATTRIBUTE_UNUSED,
2814 asymbol ***symppp ATTRIBUTE_UNUSED)
2816 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2817 return write_ieee_debugging_info (obfd, dhandle);
2819 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2820 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2822 bfd_byte *syms, *strings;
2823 bfd_size_type symsize, stringsize;
2824 asection *stabsec, *stabstrsec;
2827 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2832 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2833 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2834 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2836 || stabstrsec == NULL
2837 || ! bfd_set_section_size (obfd, stabsec, symsize)
2838 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2839 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2840 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2842 bfd_nonfatal_message (NULL, obfd, NULL,
2843 _("can't create debugging section"));
2847 /* We can get away with setting the section contents now because
2848 the next thing the caller is going to do is copy over the
2849 real sections. We may someday have to split the contents
2850 setting out of this function. */
2851 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2852 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2855 bfd_nonfatal_message (NULL, obfd, NULL,
2856 _("can't set debugging section contents"));
2863 bfd_nonfatal_message (NULL, obfd, NULL,
2864 _("don't know how to write debugging information for %s"),
2865 bfd_get_target (obfd));
2870 strip_main (int argc, char *argv[])
2872 char *input_target = NULL;
2873 char *output_target = NULL;
2874 bfd_boolean show_version = FALSE;
2875 bfd_boolean formats_info = FALSE;
2878 struct section_list *p;
2879 char *output_file = NULL;
2881 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2882 strip_options, (int *) 0)) != EOF)
2887 input_target = optarg;
2890 output_target = optarg;
2893 input_target = output_target = optarg;
2896 p = find_section_list (optarg, TRUE);
2898 sections_removed = TRUE;
2901 strip_symbols = STRIP_ALL;
2905 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2906 strip_symbols = STRIP_DEBUG;
2908 case OPTION_STRIP_UNNEEDED:
2909 strip_symbols = STRIP_UNNEEDED;
2912 add_specific_symbol (optarg, keep_specific_htab);
2915 add_specific_symbol (optarg, strip_specific_htab);
2918 output_file = optarg;
2921 preserve_dates = TRUE;
2924 discard_locals = LOCALS_ALL;
2927 discard_locals = LOCALS_START_L;
2933 show_version = TRUE;
2935 case OPTION_FORMATS_INFO:
2936 formats_info = TRUE;
2938 case OPTION_ONLY_KEEP_DEBUG:
2939 strip_symbols = STRIP_NONDEBUG;
2941 case OPTION_KEEP_FILE_SYMBOLS:
2942 keep_file_symbols = 1;
2945 /* We've been given a long option. */
2952 strip_usage (stdout, 0);
2954 strip_usage (stderr, 1);
2965 print_version ("strip");
2967 /* Default is to strip all symbols. */
2968 if (strip_symbols == STRIP_UNDEF
2969 && discard_locals == LOCALS_UNDEF
2970 && htab_elements (strip_specific_htab) == 0)
2971 strip_symbols = STRIP_ALL;
2973 if (output_target == NULL)
2974 output_target = input_target;
2978 || (output_file != NULL && (i + 1) < argc))
2979 strip_usage (stderr, 1);
2981 for (; i < argc; i++)
2983 int hold_status = status;
2984 struct stat statbuf;
2987 if (get_file_size (argv[i]) < 1)
2994 /* No need to check the return value of stat().
2995 It has already been checked in get_file_size(). */
2996 stat (argv[i], &statbuf);
2998 if (output_file == NULL || strcmp (argv[i], output_file) == 0)
2999 tmpname = make_tempname (argv[i]);
3001 tmpname = output_file;
3003 if (tmpname == NULL)
3005 bfd_nonfatal_message (argv[i], NULL, NULL,
3006 _("could not create temporary file to hold stripped copy"));
3012 copy_file (argv[i], tmpname, input_target, output_target);
3016 set_times (tmpname, &statbuf);
3017 if (output_file != tmpname)
3018 status = (smart_rename (tmpname,
3019 output_file ? output_file : argv[i],
3020 preserve_dates) != 0);
3022 status = hold_status;
3025 unlink_if_ordinary (tmpname);
3026 if (output_file != tmpname)
3033 /* Set up PE subsystem. */
3036 set_pe_subsystem (const char *s)
3038 const char *version, *subsystem;
3048 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
3049 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
3050 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
3051 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
3052 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
3053 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
3054 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
3055 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
3056 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
3057 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
3063 /* Check for the presence of a version number. */
3064 version = strchr (s, ':');
3065 if (version == NULL)
3069 int len = version - s;
3073 version = copy + 1 + len;
3074 pe_major_subsystem_version = strtoul (version, ©, 0);
3076 pe_minor_subsystem_version = strtoul (copy + 1, ©, 0);
3078 non_fatal (_("%s: bad version in PE subsystem"), s);
3081 /* Check for numeric subsystem. */
3082 value = (short) strtol (subsystem, ©, 0);
3085 for (i = 0; i < ARRAY_SIZE (v); i++)
3086 if (v[i].value == value)
3088 pe_subsystem = value;
3089 set_def = v[i].set_def;
3095 /* Search for subsystem by name. */
3096 for (i = 0; i < ARRAY_SIZE (v); i++)
3097 if (strcmp (subsystem, v[i].name) == 0)
3099 pe_subsystem = v[i].value;
3100 set_def = v[i].set_def;
3108 fatal (_("unknown PE subsystem: %s"), s);
3113 if (pe_file_alignment == (bfd_vma) -1)
3114 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3115 if (pe_section_alignment == (bfd_vma) -1)
3116 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3121 /* Convert EFI target to PEI target. */
3124 convert_efi_target (char *efi)
3130 if (strcmp (efi + 4, "ia32") == 0)
3132 /* Change ia32 to i386. */
3137 else if (strcmp (efi + 4, "x86_64") == 0)
3139 /* Change x86_64 to x86-64. */
3145 copy_main (int argc, char *argv[])
3147 char * binary_architecture = NULL;
3148 char *input_filename = NULL;
3149 char *output_filename = NULL;
3151 char *input_target = NULL;
3152 char *output_target = NULL;
3153 bfd_boolean show_version = FALSE;
3154 bfd_boolean change_warn = TRUE;
3155 bfd_boolean formats_info = FALSE;
3157 struct section_list *p;
3158 struct stat statbuf;
3160 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
3161 copy_options, (int *) 0)) != EOF)
3166 copy_byte = atoi (optarg);
3168 fatal (_("byte number must be non-negative"));
3172 binary_architecture = optarg;
3176 interleave = atoi (optarg);
3178 fatal (_("interleave must be positive"));
3182 case 's': /* "source" - 'I' is preferred */
3183 input_target = optarg;
3187 case 'd': /* "destination" - 'O' is preferred */
3188 output_target = optarg;
3192 input_target = output_target = optarg;
3196 p = find_section_list (optarg, TRUE);
3198 fatal (_("%s both copied and removed"), optarg);
3200 sections_copied = TRUE;
3204 p = find_section_list (optarg, TRUE);
3206 fatal (_("%s both copied and removed"), optarg);
3208 sections_removed = TRUE;
3212 strip_symbols = STRIP_ALL;
3216 strip_symbols = STRIP_DEBUG;
3219 case OPTION_STRIP_UNNEEDED:
3220 strip_symbols = STRIP_UNNEEDED;
3223 case OPTION_ONLY_KEEP_DEBUG:
3224 strip_symbols = STRIP_NONDEBUG;
3227 case OPTION_KEEP_FILE_SYMBOLS:
3228 keep_file_symbols = 1;
3231 case OPTION_ADD_GNU_DEBUGLINK:
3232 gnu_debuglink_filename = optarg;
3236 add_specific_symbol (optarg, keep_specific_htab);
3240 add_specific_symbol (optarg, strip_specific_htab);
3243 case OPTION_STRIP_UNNEEDED_SYMBOL:
3244 add_specific_symbol (optarg, strip_unneeded_htab);
3248 add_specific_symbol (optarg, localize_specific_htab);
3251 case OPTION_GLOBALIZE_SYMBOL:
3252 add_specific_symbol (optarg, globalize_specific_htab);
3256 add_specific_symbol (optarg, keepglobal_specific_htab);
3260 add_specific_symbol (optarg, weaken_specific_htab);
3264 preserve_dates = TRUE;
3272 discard_locals = LOCALS_ALL;
3276 discard_locals = LOCALS_START_L;
3284 show_version = TRUE;
3287 case OPTION_FORMATS_INFO:
3288 formats_info = TRUE;
3295 case OPTION_ADD_SECTION:
3299 struct section_add *pa;
3304 s = strchr (optarg, '=');
3307 fatal (_("bad format for %s"), "--add-section");
3309 size = get_file_size (s + 1);
3316 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
3319 name = (char *) xmalloc (len + 1);
3320 strncpy (name, optarg, len);
3324 pa->filename = s + 1;
3326 pa->contents = (bfd_byte *) xmalloc (size);
3328 f = fopen (pa->filename, FOPEN_RB);
3331 fatal (_("cannot open: %s: %s"),
3332 pa->filename, strerror (errno));
3334 if (fread (pa->contents, 1, pa->size, f) == 0
3336 fatal (_("%s: fread failed"), pa->filename);
3340 pa->next = add_sections;
3345 case OPTION_CHANGE_START:
3346 change_start = parse_vma (optarg, "--change-start");
3349 case OPTION_CHANGE_SECTION_ADDRESS:
3350 case OPTION_CHANGE_SECTION_LMA:
3351 case OPTION_CHANGE_SECTION_VMA:
3356 char *option = NULL;
3358 enum change_action what = CHANGE_IGNORE;
3362 case OPTION_CHANGE_SECTION_ADDRESS:
3363 option = "--change-section-address";
3365 case OPTION_CHANGE_SECTION_LMA:
3366 option = "--change-section-lma";
3368 case OPTION_CHANGE_SECTION_VMA:
3369 option = "--change-section-vma";
3373 s = strchr (optarg, '=');
3376 s = strchr (optarg, '+');
3379 s = strchr (optarg, '-');
3381 fatal (_("bad format for %s"), option);
3386 name = (char *) xmalloc (len + 1);
3387 strncpy (name, optarg, len);
3390 p = find_section_list (name, TRUE);
3392 val = parse_vma (s + 1, option);
3396 case '=': what = CHANGE_SET; break;
3397 case '-': val = - val; /* Drop through. */
3398 case '+': what = CHANGE_MODIFY; break;
3403 case OPTION_CHANGE_SECTION_ADDRESS:
3404 p->change_vma = what;
3408 case OPTION_CHANGE_SECTION_LMA:
3409 p->change_lma = what;
3413 case OPTION_CHANGE_SECTION_VMA:
3414 p->change_vma = what;
3421 case OPTION_CHANGE_ADDRESSES:
3422 change_section_address = parse_vma (optarg, "--change-addresses");
3423 change_start = change_section_address;
3426 case OPTION_CHANGE_WARNINGS:
3430 case OPTION_CHANGE_LEADING_CHAR:
3431 change_leading_char = TRUE;
3434 case OPTION_DEBUGGING:
3435 convert_debugging = TRUE;
3438 case OPTION_GAP_FILL:
3440 bfd_vma gap_fill_vma;
3442 gap_fill_vma = parse_vma (optarg, "--gap-fill");
3443 gap_fill = (bfd_byte) gap_fill_vma;
3444 if ((bfd_vma) gap_fill != gap_fill_vma)
3448 sprintf_vma (buff, gap_fill_vma);
3450 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3453 gap_fill_set = TRUE;
3457 case OPTION_NO_CHANGE_WARNINGS:
3458 change_warn = FALSE;
3462 pad_to = parse_vma (optarg, "--pad-to");
3466 case OPTION_REMOVE_LEADING_CHAR:
3467 remove_leading_char = TRUE;
3470 case OPTION_REDEFINE_SYM:
3472 /* Push this redefinition onto redefine_symbol_list. */
3476 const char *nextarg;
3477 char *source, *target;
3479 s = strchr (optarg, '=');
3481 fatal (_("bad format for %s"), "--redefine-sym");
3484 source = (char *) xmalloc (len + 1);
3485 strncpy (source, optarg, len);
3489 len = strlen (nextarg);
3490 target = (char *) xmalloc (len + 1);
3491 strcpy (target, nextarg);
3493 redefine_list_append ("--redefine-sym", source, target);
3500 case OPTION_REDEFINE_SYMS:
3501 add_redefine_syms_file (optarg);
3504 case OPTION_SET_SECTION_FLAGS:
3510 s = strchr (optarg, '=');
3512 fatal (_("bad format for %s"), "--set-section-flags");
3515 name = (char *) xmalloc (len + 1);
3516 strncpy (name, optarg, len);
3519 p = find_section_list (name, TRUE);
3521 p->set_flags = TRUE;
3522 p->flags = parse_flags (s + 1);
3526 case OPTION_RENAME_SECTION:
3529 const char *eq, *fl;
3534 eq = strchr (optarg, '=');
3536 fatal (_("bad format for %s"), "--rename-section");
3540 fatal (_("bad format for %s"), "--rename-section");
3542 old_name = (char *) xmalloc (len + 1);
3543 strncpy (old_name, optarg, len);
3547 fl = strchr (eq, ',');
3550 flags = parse_flags (fl + 1);
3560 fatal (_("bad format for %s"), "--rename-section");
3562 new_name = (char *) xmalloc (len + 1);
3563 strncpy (new_name, eq, len);
3566 add_section_rename (old_name, new_name, flags);
3570 case OPTION_SET_START:
3571 set_start = parse_vma (optarg, "--set-start");
3572 set_start_set = TRUE;
3575 case OPTION_SREC_LEN:
3576 Chunk = parse_vma (optarg, "--srec-len");
3579 case OPTION_SREC_FORCES3:
3583 case OPTION_STRIP_SYMBOLS:
3584 add_specific_symbols (optarg, strip_specific_htab);
3587 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3588 add_specific_symbols (optarg, strip_unneeded_htab);
3591 case OPTION_KEEP_SYMBOLS:
3592 add_specific_symbols (optarg, keep_specific_htab);
3595 case OPTION_LOCALIZE_HIDDEN:
3596 localize_hidden = TRUE;
3599 case OPTION_LOCALIZE_SYMBOLS:
3600 add_specific_symbols (optarg, localize_specific_htab);
3603 case OPTION_LONG_SECTION_NAMES:
3604 if (!strcmp ("enable", optarg))
3605 long_section_names = ENABLE;
3606 else if (!strcmp ("disable", optarg))
3607 long_section_names = DISABLE;
3608 else if (!strcmp ("keep", optarg))
3609 long_section_names = KEEP;
3611 fatal (_("unknown long section names option '%s'"), optarg);
3614 case OPTION_GLOBALIZE_SYMBOLS:
3615 add_specific_symbols (optarg, globalize_specific_htab);
3618 case OPTION_KEEPGLOBAL_SYMBOLS:
3619 add_specific_symbols (optarg, keepglobal_specific_htab);
3622 case OPTION_WEAKEN_SYMBOLS:
3623 add_specific_symbols (optarg, weaken_specific_htab);
3626 case OPTION_ALT_MACH_CODE:
3627 use_alt_mach_code = strtoul (optarg, NULL, 0);
3628 if (use_alt_mach_code == 0)
3629 fatal (_("unable to parse alternative machine code"));
3632 case OPTION_PREFIX_SYMBOLS:
3633 prefix_symbols_string = optarg;
3636 case OPTION_PREFIX_SECTIONS:
3637 prefix_sections_string = optarg;
3640 case OPTION_PREFIX_ALLOC_SECTIONS:
3641 prefix_alloc_sections_string = optarg;
3644 case OPTION_READONLY_TEXT:
3645 bfd_flags_to_set |= WP_TEXT;
3646 bfd_flags_to_clear &= ~WP_TEXT;
3649 case OPTION_WRITABLE_TEXT:
3650 bfd_flags_to_clear |= WP_TEXT;
3651 bfd_flags_to_set &= ~WP_TEXT;
3655 bfd_flags_to_set |= D_PAGED;
3656 bfd_flags_to_clear &= ~D_PAGED;
3660 bfd_flags_to_clear |= D_PAGED;
3661 bfd_flags_to_set &= ~D_PAGED;
3664 case OPTION_EXTRACT_SYMBOL:
3665 extract_symbol = TRUE;
3668 case OPTION_REVERSE_BYTES:
3670 int prev = reverse_bytes;
3672 reverse_bytes = atoi (optarg);
3673 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
3674 fatal (_("number of bytes to reverse must be positive and even"));
3676 if (prev && prev != reverse_bytes)
3677 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3682 case OPTION_FILE_ALIGNMENT:
3683 pe_file_alignment = parse_vma (optarg, "--file-alignment");
3689 pe_heap_reserve = strtoul (optarg, &end, 0);
3691 || (*end != '.' && *end != '\0'))
3692 non_fatal (_("%s: invalid reserve value for --heap"),
3694 else if (*end != '\0')
3696 pe_heap_commit = strtoul (end + 1, &end, 0);
3698 non_fatal (_("%s: invalid commit value for --heap"),
3704 case OPTION_IMAGE_BASE:
3705 pe_image_base = parse_vma (optarg, "--image-base");
3708 case OPTION_SECTION_ALIGNMENT:
3709 pe_section_alignment = parse_vma (optarg,
3710 "--section-alignment");
3713 case OPTION_SUBSYSTEM:
3714 set_pe_subsystem (optarg);
3720 pe_stack_reserve = strtoul (optarg, &end, 0);
3722 || (*end != '.' && *end != '\0'))
3723 non_fatal (_("%s: invalid reserve value for --stack"),
3725 else if (*end != '\0')
3727 pe_stack_commit = strtoul (end + 1, &end, 0);
3729 non_fatal (_("%s: invalid commit value for --stack"),
3736 /* We've been given a long option. */
3741 copy_usage (stdout, 0);
3744 copy_usage (stderr, 1);
3755 print_version ("objcopy");
3757 if (copy_byte >= interleave)
3758 fatal (_("byte number must be less than interleave"));
3760 if (optind == argc || optind + 2 < argc)
3761 copy_usage (stderr, 1);
3763 input_filename = argv[optind];
3764 if (optind + 1 < argc)
3765 output_filename = argv[optind + 1];
3767 /* Default is to strip no symbols. */
3768 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3769 strip_symbols = STRIP_NONE;
3771 if (output_target == NULL)
3772 output_target = input_target;
3774 /* Convert input EFI target to PEI target. */
3775 if (input_target != NULL
3776 && strncmp (input_target, "efi-", 4) == 0)
3780 efi = xstrdup (output_target + 4);
3781 if (strncmp (efi, "bsdrv-", 6) == 0
3782 || strncmp (efi, "rtdrv-", 6) == 0)
3784 else if (strncmp (efi, "app-", 4) != 0)
3785 fatal (_("unknown input EFI target: %s"), input_target);
3788 convert_efi_target (efi);
3791 /* Convert output EFI target to PEI target. */
3792 if (output_target != NULL
3793 && strncmp (output_target, "efi-", 4) == 0)
3797 efi = xstrdup (output_target + 4);
3798 if (strncmp (efi, "app-", 4) == 0)
3800 if (pe_subsystem == -1)
3801 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
3803 else if (strncmp (efi, "bsdrv-", 6) == 0)
3805 if (pe_subsystem == -1)
3806 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
3809 else if (strncmp (efi, "rtdrv-", 6) == 0)
3811 if (pe_subsystem == -1)
3812 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
3816 fatal (_("unknown output EFI target: %s"), output_target);
3818 if (pe_file_alignment == (bfd_vma) -1)
3819 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3820 if (pe_section_alignment == (bfd_vma) -1)
3821 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3823 output_target = efi;
3824 convert_efi_target (efi);
3827 if (binary_architecture != NULL)
3829 if (input_target && strcmp (input_target, "binary") == 0)
3831 const bfd_arch_info_type * temp_arch_info;
3833 temp_arch_info = bfd_scan_arch (binary_architecture);
3835 if (temp_arch_info != NULL)
3837 bfd_external_binary_architecture = temp_arch_info->arch;
3838 bfd_external_machine = temp_arch_info->mach;
3841 fatal (_("architecture %s unknown"), binary_architecture);
3845 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3846 non_fatal (_(" Argument %s ignored"), binary_architecture);
3851 if (stat (input_filename, & statbuf) < 0)
3852 fatal (_("warning: could not locate '%s'. System error message: %s"),
3853 input_filename, strerror (errno));
3855 /* If there is no destination file, or the source and destination files
3856 are the same, then create a temp and rename the result into the input. */
3857 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3858 tmpname = make_tempname (input_filename);
3860 tmpname = output_filename;
3862 if (tmpname == NULL)
3863 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3864 input_filename, strerror (errno));
3866 copy_file (input_filename, tmpname, input_target, output_target);
3870 set_times (tmpname, &statbuf);
3871 if (tmpname != output_filename)
3872 status = (smart_rename (tmpname, input_filename,
3873 preserve_dates) != 0);
3876 unlink_if_ordinary (tmpname);
3880 for (p = change_sections; p != NULL; p = p->next)
3884 if (p->change_vma != CHANGE_IGNORE)
3888 sprintf_vma (buff, p->vma_val);
3890 /* xgettext:c-format */
3891 non_fatal (_("%s %s%c0x%s never used"),
3892 "--change-section-vma",
3894 p->change_vma == CHANGE_SET ? '=' : '+',
3898 if (p->change_lma != CHANGE_IGNORE)
3902 sprintf_vma (buff, p->lma_val);
3904 /* xgettext:c-format */
3905 non_fatal (_("%s %s%c0x%s never used"),
3906 "--change-section-lma",
3908 p->change_lma == CHANGE_SET ? '=' : '+',
3919 main (int argc, char *argv[])
3921 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3922 setlocale (LC_MESSAGES, "");
3924 #if defined (HAVE_SETLOCALE)
3925 setlocale (LC_CTYPE, "");
3927 bindtextdomain (PACKAGE, LOCALEDIR);
3928 textdomain (PACKAGE);
3930 program_name = argv[0];
3931 xmalloc_set_program_name (program_name);
3933 START_PROGRESS (program_name, 0);
3935 expandargv (&argc, &argv);
3937 strip_symbols = STRIP_UNDEF;
3938 discard_locals = LOCALS_UNDEF;
3941 set_default_bfd_target ();
3945 int i = strlen (program_name);
3946 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3947 /* Drop the .exe suffix, if any. */
3948 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3951 program_name[i] = '\0';
3954 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3957 create_symbol_htabs ();
3960 strip_main (argc, argv);
3962 copy_main (argc, argv);
3964 END_PROGRESS (program_name);