1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "libiberty.h"
28 #include "filenames.h"
32 #include "coff/internal.h"
35 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
36 header in generic PE code. */
37 #include "coff/i386.h"
40 static bfd_vma pe_file_alignment = (bfd_vma) -1;
41 static bfd_vma pe_heap_commit = (bfd_vma) -1;
42 static bfd_vma pe_heap_reserve = (bfd_vma) -1;
43 static bfd_vma pe_image_base = (bfd_vma) -1;
44 static bfd_vma pe_section_alignment = (bfd_vma) -1;
45 static bfd_vma pe_stack_commit = (bfd_vma) -1;
46 static bfd_vma pe_stack_reserve = (bfd_vma) -1;
47 static short pe_subsystem = -1;
48 static short pe_major_subsystem_version = -1;
49 static short pe_minor_subsystem_version = -1;
51 struct is_specified_symbol_predicate_data
57 /* A list to support redefine_sym. */
62 struct redefine_node *next;
67 struct addsym_node *next;
75 typedef struct section_rename
77 const char * old_name;
78 const char * new_name;
80 struct section_rename * next;
84 /* List of sections to be renamed. */
85 static section_rename *section_rename_list;
87 static asymbol **isympp = NULL; /* Input symbols. */
88 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
90 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
91 static int copy_byte = -1;
92 static int interleave = 0; /* Initialised to 4 in copy_main(). */
93 static int copy_width = 1;
95 static bfd_boolean verbose; /* Print file and target names. */
96 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
97 static int deterministic = -1; /* Enable deterministic archives. */
98 static int status = 0; /* Exit status. */
103 STRIP_NONE, /* Don't strip. */
104 STRIP_DEBUG, /* Strip all debugger symbols. */
105 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
106 STRIP_NONDEBUG, /* Strip everything but debug info. */
107 STRIP_DWO, /* Strip all DWO info. */
108 STRIP_NONDWO, /* Strip everything but DWO info. */
109 STRIP_ALL /* Strip all symbols. */
112 /* Which symbols to remove. */
113 static enum strip_action strip_symbols = STRIP_UNDEF;
118 LOCALS_START_L, /* Discard locals starting with L. */
119 LOCALS_ALL /* Discard all locals. */
122 /* Which local symbols to remove. Overrides STRIP_ALL. */
123 static enum locals_action discard_locals;
125 /* Structure used to hold lists of sections and actions to take. */
128 struct section_list * next; /* Next section to change. */
129 const char * pattern; /* Section name pattern. */
130 bfd_boolean used; /* Whether this entry was used. */
132 unsigned int context; /* What to do with matching sections. */
133 /* Flag bits used in the context field.
134 COPY and REMOVE are mutually exlusive. SET and ALTER are mutually exclusive. */
135 #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
136 #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
137 #define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */
138 #define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */
139 #define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */
140 #define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */
141 #define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */
143 bfd_vma vma_val; /* Amount to change by or set to. */
144 bfd_vma lma_val; /* Amount to change by or set to. */
145 flagword flags; /* What to set the section flags to. */
148 static struct section_list *change_sections;
150 /* TRUE if some sections are to be removed. */
151 static bfd_boolean sections_removed;
153 /* TRUE if only some sections are to be copied. */
154 static bfd_boolean sections_copied;
156 /* Changes to the start address. */
157 static bfd_vma change_start = 0;
158 static bfd_boolean set_start_set = FALSE;
159 static bfd_vma set_start;
161 /* Changes to section addresses. */
162 static bfd_vma change_section_address = 0;
164 /* Filling gaps between sections. */
165 static bfd_boolean gap_fill_set = FALSE;
166 static bfd_byte gap_fill = 0;
168 /* Pad to a given address. */
169 static bfd_boolean pad_to_set = FALSE;
170 static bfd_vma pad_to;
172 /* Use alternative machine code? */
173 static unsigned long use_alt_mach_code = 0;
175 /* Output BFD flags user wants to set or clear */
176 static flagword bfd_flags_to_set;
177 static flagword bfd_flags_to_clear;
179 /* List of sections to add. */
182 /* Next section to add. */
183 struct section_add *next;
184 /* Name of section to add. */
186 /* Name of file holding section contents. */
187 const char *filename;
190 /* Contents of file. */
192 /* BFD section, after it has been added. */
196 /* List of sections to add to the output BFD. */
197 static struct section_add *add_sections;
199 /* List of sections to update in the output BFD. */
200 static struct section_add *update_sections;
202 /* List of sections to dump from the output BFD. */
203 static struct section_add *dump_sections;
205 /* If non-NULL the argument to --add-gnu-debuglink.
206 This should be the filename to store in the .gnu_debuglink section. */
207 static const char * gnu_debuglink_filename = NULL;
209 /* Whether to convert debugging information. */
210 static bfd_boolean convert_debugging = FALSE;
212 /* Whether to compress/decompress DWARF debug sections. */
217 compress_zlib = compress | 1 << 1,
218 compress_gnu_zlib = compress | 1 << 2,
219 compress_gabi_zlib = compress | 1 << 3,
221 } do_debug_sections = nothing;
223 /* Whether to change the leading character in symbol names. */
224 static bfd_boolean change_leading_char = FALSE;
226 /* Whether to remove the leading character from global symbol names. */
227 static bfd_boolean remove_leading_char = FALSE;
229 /* Whether to permit wildcard in symbol comparison. */
230 static bfd_boolean wildcard = FALSE;
232 /* True if --localize-hidden is in effect. */
233 static bfd_boolean localize_hidden = FALSE;
235 /* List of symbols to strip, keep, localize, keep-global, weaken,
237 static htab_t strip_specific_htab = NULL;
238 static htab_t strip_unneeded_htab = NULL;
239 static htab_t keep_specific_htab = NULL;
240 static htab_t localize_specific_htab = NULL;
241 static htab_t globalize_specific_htab = NULL;
242 static htab_t keepglobal_specific_htab = NULL;
243 static htab_t weaken_specific_htab = NULL;
244 static struct redefine_node *redefine_sym_list = NULL;
245 static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
246 static int add_symbols = 0;
248 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
249 static bfd_boolean weaken = FALSE;
251 /* If this is TRUE, we retain BSF_FILE symbols. */
252 static bfd_boolean keep_file_symbols = FALSE;
254 /* Prefix symbols/sections. */
255 static char *prefix_symbols_string = 0;
256 static char *prefix_sections_string = 0;
257 static char *prefix_alloc_sections_string = 0;
259 /* True if --extract-symbol was passed on the command line. */
260 static bfd_boolean extract_symbol = FALSE;
262 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
263 of <reverse_bytes> bytes within each output section. */
264 static int reverse_bytes = 0;
266 /* For Coff objects, we may want to allow or disallow long section names,
267 or preserve them where found in the inputs. Debug info relies on them. */
268 enum long_section_name_handling
275 /* The default long section handling mode is to preserve them.
276 This is also the only behaviour for 'strip'. */
277 static enum long_section_name_handling long_section_names = KEEP;
279 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
280 enum command_line_switch
282 OPTION_ADD_SECTION=150,
283 OPTION_ADD_GNU_DEBUGLINK,
285 OPTION_ALT_MACH_CODE,
286 OPTION_CHANGE_ADDRESSES,
287 OPTION_CHANGE_LEADING_CHAR,
288 OPTION_CHANGE_SECTION_ADDRESS,
289 OPTION_CHANGE_SECTION_LMA,
290 OPTION_CHANGE_SECTION_VMA,
292 OPTION_CHANGE_WARNINGS,
293 OPTION_COMPRESS_DEBUG_SECTIONS,
295 OPTION_DECOMPRESS_DEBUG_SECTIONS,
298 OPTION_EXTRACT_SYMBOL,
299 OPTION_FILE_ALIGNMENT,
302 OPTION_GLOBALIZE_SYMBOL,
303 OPTION_GLOBALIZE_SYMBOLS,
307 OPTION_INTERLEAVE_WIDTH,
308 OPTION_KEEPGLOBAL_SYMBOLS,
309 OPTION_KEEP_FILE_SYMBOLS,
311 OPTION_LOCALIZE_HIDDEN,
312 OPTION_LOCALIZE_SYMBOLS,
313 OPTION_LONG_SECTION_NAMES,
314 OPTION_NO_CHANGE_WARNINGS,
315 OPTION_ONLY_KEEP_DEBUG,
317 OPTION_PREFIX_ALLOC_SECTIONS,
318 OPTION_PREFIX_SECTIONS,
319 OPTION_PREFIX_SYMBOLS,
321 OPTION_READONLY_TEXT,
323 OPTION_REDEFINE_SYMS,
324 OPTION_REMOVE_LEADING_CHAR,
325 OPTION_RENAME_SECTION,
326 OPTION_REVERSE_BYTES,
327 OPTION_SECTION_ALIGNMENT,
328 OPTION_SET_SECTION_FLAGS,
334 OPTION_STRIP_SYMBOLS,
335 OPTION_STRIP_UNNEEDED,
336 OPTION_STRIP_UNNEEDED_SYMBOL,
337 OPTION_STRIP_UNNEEDED_SYMBOLS,
339 OPTION_UPDATE_SECTION,
341 OPTION_WEAKEN_SYMBOLS,
345 /* Options to handle if running as "strip". */
347 static struct option strip_options[] =
349 {"disable-deterministic-archives", no_argument, 0, 'U'},
350 {"discard-all", no_argument, 0, 'x'},
351 {"discard-locals", no_argument, 0, 'X'},
352 {"enable-deterministic-archives", no_argument, 0, 'D'},
353 {"format", required_argument, 0, 'F'}, /* Obsolete */
354 {"help", no_argument, 0, 'h'},
355 {"info", no_argument, 0, OPTION_FORMATS_INFO},
356 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
357 {"input-target", required_argument, 0, 'I'},
358 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
359 {"keep-symbol", required_argument, 0, 'K'},
360 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
361 {"output-file", required_argument, 0, 'o'},
362 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
363 {"output-target", required_argument, 0, 'O'},
364 {"preserve-dates", no_argument, 0, 'p'},
365 {"remove-section", required_argument, 0, 'R'},
366 {"strip-all", no_argument, 0, 's'},
367 {"strip-debug", no_argument, 0, 'S'},
368 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
369 {"strip-symbol", required_argument, 0, 'N'},
370 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
371 {"target", required_argument, 0, 'F'},
372 {"verbose", no_argument, 0, 'v'},
373 {"version", no_argument, 0, 'V'},
374 {"wildcard", no_argument, 0, 'w'},
375 {0, no_argument, 0, 0}
378 /* Options to handle if running as "objcopy". */
380 static struct option copy_options[] =
382 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
383 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
384 {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
385 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
386 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
387 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
388 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
389 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
390 {"binary-architecture", required_argument, 0, 'B'},
391 {"byte", required_argument, 0, 'b'},
392 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
393 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
394 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
395 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
396 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
397 {"change-start", required_argument, 0, OPTION_CHANGE_START},
398 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
399 {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
400 {"debugging", no_argument, 0, OPTION_DEBUGGING},
401 {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
402 {"disable-deterministic-archives", no_argument, 0, 'U'},
403 {"discard-all", no_argument, 0, 'x'},
404 {"discard-locals", no_argument, 0, 'X'},
405 {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
406 {"enable-deterministic-archives", no_argument, 0, 'D'},
407 {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
408 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
409 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
410 {"format", required_argument, 0, 'F'}, /* Obsolete */
411 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
412 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
413 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
414 {"heap", required_argument, 0, OPTION_HEAP},
415 {"help", no_argument, 0, 'h'},
416 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
417 {"impure", no_argument, 0, OPTION_IMPURE},
418 {"info", no_argument, 0, OPTION_FORMATS_INFO},
419 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
420 {"input-target", required_argument, 0, 'I'},
421 {"interleave", optional_argument, 0, 'i'},
422 {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
423 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
424 {"keep-global-symbol", required_argument, 0, 'G'},
425 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
426 {"keep-symbol", required_argument, 0, 'K'},
427 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
428 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
429 {"localize-symbol", required_argument, 0, 'L'},
430 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
431 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
432 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
433 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
434 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
435 {"only-section", required_argument, 0, 'j'},
436 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
437 {"output-target", required_argument, 0, 'O'},
438 {"pad-to", required_argument, 0, OPTION_PAD_TO},
439 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
440 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
441 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
442 {"preserve-dates", no_argument, 0, 'p'},
443 {"pure", no_argument, 0, OPTION_PURE},
444 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
445 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
446 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
447 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
448 {"remove-section", required_argument, 0, 'R'},
449 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
450 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
451 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
452 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
453 {"set-start", required_argument, 0, OPTION_SET_START},
454 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
455 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
456 {"stack", required_argument, 0, OPTION_STACK},
457 {"strip-all", no_argument, 0, 'S'},
458 {"strip-debug", no_argument, 0, 'g'},
459 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
460 {"strip-symbol", required_argument, 0, 'N'},
461 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
462 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
463 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
464 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
465 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
466 {"target", required_argument, 0, 'F'},
467 {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
468 {"verbose", no_argument, 0, 'v'},
469 {"version", no_argument, 0, 'V'},
470 {"weaken", no_argument, 0, OPTION_WEAKEN},
471 {"weaken-symbol", required_argument, 0, 'W'},
472 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
473 {"wildcard", no_argument, 0, 'w'},
474 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
475 {0, no_argument, 0, 0}
479 extern char *program_name;
481 /* This flag distinguishes between strip and objcopy:
482 1 means this is 'strip'; 0 means this is 'objcopy'.
483 -1 means if we should use argv[0] to decide. */
486 /* The maximum length of an S record. This variable is declared in srec.c
487 and can be modified by the --srec-len parameter. */
488 extern unsigned int Chunk;
490 /* Restrict the generation of Srecords to type S3 only.
491 This variable is declare in bfd/srec.c and can be toggled
492 on by the --srec-forceS3 command line switch. */
493 extern bfd_boolean S3Forced;
495 /* Forward declarations. */
496 static void setup_section (bfd *, asection *, void *);
497 static void setup_bfd_headers (bfd *, bfd *);
498 static void copy_relocations_in_section (bfd *, asection *, void *);
499 static void copy_section (bfd *, asection *, void *);
500 static void get_sections (bfd *, asection *, void *);
501 static int compare_section_lma (const void *, const void *);
502 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
503 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
504 static const char *lookup_sym_redefinition (const char *);
507 copy_usage (FILE *stream, int exit_status)
509 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
510 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
511 fprintf (stream, _(" The options are:\n"));
512 fprintf (stream, _("\
513 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
514 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
515 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
516 -F --target <bfdname> Set both input and output format to <bfdname>\n\
517 --debugging Convert debugging information, if possible\n\
518 -p --preserve-dates Copy modified/access timestamps to the output\n"));
519 if (DEFAULT_AR_DETERMINISTIC)
520 fprintf (stream, _("\
521 -D --enable-deterministic-archives\n\
522 Produce deterministic output when stripping archives (default)\n\
523 -U --disable-deterministic-archives\n\
524 Disable -D behavior\n"));
526 fprintf (stream, _("\
527 -D --enable-deterministic-archives\n\
528 Produce deterministic output when stripping archives\n\
529 -U --disable-deterministic-archives\n\
530 Disable -D behavior (default)\n"));
531 fprintf (stream, _("\
532 -j --only-section <name> Only copy section <name> into the output\n\
533 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
534 -R --remove-section <name> Remove section <name> from the output\n\
535 -S --strip-all Remove all symbol and relocation information\n\
536 -g --strip-debug Remove all debugging symbols & sections\n\
537 --strip-dwo Remove all DWO sections\n\
538 --strip-unneeded Remove all symbols not needed by relocations\n\
539 -N --strip-symbol <name> Do not copy symbol <name>\n\
540 --strip-unneeded-symbol <name>\n\
541 Do not copy symbol <name> unless needed by\n\
543 --only-keep-debug Strip everything but the debug information\n\
544 --extract-dwo Copy only DWO sections\n\
545 --extract-symbol Remove section contents but keep symbols\n\
546 -K --keep-symbol <name> Do not strip symbol <name>\n\
547 --keep-file-symbols Do not strip file symbol(s)\n\
548 --localize-hidden Turn all ELF hidden symbols into locals\n\
549 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
550 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
551 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
552 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
553 --weaken Force all global symbols to be marked as weak\n\
554 -w --wildcard Permit wildcard in symbol comparison\n\
555 -x --discard-all Remove all non-global symbols\n\
556 -X --discard-locals Remove any compiler-generated symbols\n\
557 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
558 --interleave-width <number> Set N for --interleave\n\
559 -b --byte <num> Select byte <num> in every interleaved block\n\
560 --gap-fill <val> Fill gaps between sections with <val>\n\
561 --pad-to <addr> Pad the last section up to address <addr>\n\
562 --set-start <addr> Set the start address to <addr>\n\
563 {--change-start|--adjust-start} <incr>\n\
564 Add <incr> to the start address\n\
565 {--change-addresses|--adjust-vma} <incr>\n\
566 Add <incr> to LMA, VMA and start addresses\n\
567 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
568 Change LMA and VMA of section <name> by <val>\n\
569 --change-section-lma <name>{=|+|-}<val>\n\
570 Change the LMA of section <name> by <val>\n\
571 --change-section-vma <name>{=|+|-}<val>\n\
572 Change the VMA of section <name> by <val>\n\
573 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
574 Warn if a named section does not exist\n\
575 --set-section-flags <name>=<flags>\n\
576 Set section <name>'s properties to <flags>\n\
577 --add-section <name>=<file> Add section <name> found in <file> to output\n\
578 --update-section <name>=<file>\n\
579 Update contents of section <name> with\n\
580 contents found in <file>\n\
581 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
582 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
583 --long-section-names {enable|disable|keep}\n\
584 Handle long section names in Coff objects.\n\
585 --change-leading-char Force output format's leading character style\n\
586 --remove-leading-char Remove leading character from global symbols\n\
587 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
588 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
589 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
591 --srec-len <number> Restrict the length of generated Srecords\n\
592 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
593 --strip-symbols <file> -N for all symbols listed in <file>\n\
594 --strip-unneeded-symbols <file>\n\
595 --strip-unneeded-symbol for all symbols listed\n\
597 --keep-symbols <file> -K for all symbols listed in <file>\n\
598 --localize-symbols <file> -L for all symbols listed in <file>\n\
599 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
600 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
601 --weaken-symbols <file> -W for all symbols listed in <file>\n\
602 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
603 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
604 --writable-text Mark the output text as writable\n\
605 --readonly-text Make the output text write protected\n\
606 --pure Mark the output file as demand paged\n\
607 --impure Mark the output file as impure\n\
608 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
609 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
610 --prefix-alloc-sections <prefix>\n\
611 Add <prefix> to start of every allocatable\n\
613 --file-alignment <num> Set PE file alignment to <num>\n\
614 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
616 --image-base <address> Set PE image base to <address>\n\
617 --section-alignment <num> Set PE section alignment to <num>\n\
618 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
620 --subsystem <name>[:<version>]\n\
621 Set PE subsystem to <name> [& <version>]\n\
622 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
623 Compress DWARF debug sections using zlib\n\
624 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
625 -v --verbose List all object files modified\n\
626 @<file> Read options from <file>\n\
627 -V --version Display this program's version number\n\
628 -h --help Display this output\n\
629 --info List object formats & architectures supported\n\
631 list_supported_targets (program_name, stream);
632 if (REPORT_BUGS_TO[0] && exit_status == 0)
633 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
638 strip_usage (FILE *stream, int exit_status)
640 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
641 fprintf (stream, _(" Removes symbols and sections from files\n"));
642 fprintf (stream, _(" The options are:\n"));
643 fprintf (stream, _("\
644 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
645 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
646 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
647 -p --preserve-dates Copy modified/access timestamps to the output\n\
649 if (DEFAULT_AR_DETERMINISTIC)
650 fprintf (stream, _("\
651 -D --enable-deterministic-archives\n\
652 Produce deterministic output when stripping archives (default)\n\
653 -U --disable-deterministic-archives\n\
654 Disable -D behavior\n"));
656 fprintf (stream, _("\
657 -D --enable-deterministic-archives\n\
658 Produce deterministic output when stripping archives\n\
659 -U --disable-deterministic-archives\n\
660 Disable -D behavior (default)\n"));
661 fprintf (stream, _("\
662 -R --remove-section=<name> Also remove section <name> from the output\n\
663 -s --strip-all Remove all symbol and relocation information\n\
664 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
665 --strip-dwo Remove all DWO sections\n\
666 --strip-unneeded Remove all symbols not needed by relocations\n\
667 --only-keep-debug Strip everything but the debug information\n\
668 -N --strip-symbol=<name> Do not copy symbol <name>\n\
669 -K --keep-symbol=<name> Do not strip symbol <name>\n\
670 --keep-file-symbols Do not strip file symbol(s)\n\
671 -w --wildcard Permit wildcard in symbol comparison\n\
672 -x --discard-all Remove all non-global symbols\n\
673 -X --discard-locals Remove any compiler-generated symbols\n\
674 -v --verbose List all object files modified\n\
675 -V --version Display this program's version number\n\
676 -h --help Display this output\n\
677 --info List object formats & architectures supported\n\
678 -o <file> Place stripped output into <file>\n\
681 list_supported_targets (program_name, stream);
682 if (REPORT_BUGS_TO[0] && exit_status == 0)
683 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
687 /* Parse section flags into a flagword, with a fatal error if the
688 string can't be parsed. */
691 parse_flags (const char *s)
701 snext = strchr (s, ',');
711 #define PARSE_FLAG(fname,fval) \
712 else if (strncasecmp (fname, s, len) == 0) ret |= fval
713 PARSE_FLAG ("alloc", SEC_ALLOC);
714 PARSE_FLAG ("load", SEC_LOAD);
715 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
716 PARSE_FLAG ("readonly", SEC_READONLY);
717 PARSE_FLAG ("debug", SEC_DEBUGGING);
718 PARSE_FLAG ("code", SEC_CODE);
719 PARSE_FLAG ("data", SEC_DATA);
720 PARSE_FLAG ("rom", SEC_ROM);
721 PARSE_FLAG ("share", SEC_COFF_SHARED);
722 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
723 PARSE_FLAG ("merge", SEC_MERGE);
724 PARSE_FLAG ("strings", SEC_STRINGS);
730 copy = (char *) xmalloc (len + 1);
731 strncpy (copy, s, len);
733 non_fatal (_("unrecognized section flag `%s'"), copy);
734 fatal (_("supported flags: %s"),
735 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
745 /* Parse symbol flags into a flagword, with a fatal error if the
746 string can't be parsed. */
749 parse_symflags (const char *s, char **other)
759 snext = strchr (s, ',');
768 #define PARSE_FLAG(fname,fval) \
769 else if (len == (int) sizeof fname - 1 && strncasecmp (fname, s, len) == 0) \
772 #define PARSE_OTHER(fname,fval) \
773 else if (len >= (int) sizeof fname && strncasecmp (fname, s, sizeof fname - 1) == 0) \
774 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
777 PARSE_FLAG ("local", BSF_LOCAL);
778 PARSE_FLAG ("global", BSF_GLOBAL);
779 PARSE_FLAG ("export", BSF_EXPORT);
780 PARSE_FLAG ("debug", BSF_DEBUGGING);
781 PARSE_FLAG ("function", BSF_FUNCTION);
782 PARSE_FLAG ("weak", BSF_WEAK);
783 PARSE_FLAG ("section", BSF_SECTION_SYM);
784 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
785 PARSE_FLAG ("warning", BSF_WARNING);
786 PARSE_FLAG ("indirect", BSF_INDIRECT);
787 PARSE_FLAG ("file", BSF_FILE);
788 PARSE_FLAG ("object", BSF_OBJECT);
789 PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
790 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
791 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
792 PARSE_OTHER ("before=", *other);
800 copy = (char *) xmalloc (len + 1);
801 strncpy (copy, s, len);
803 non_fatal (_("unrecognized symbol flag `%s'"), copy);
804 fatal (_("supported flags: %s"),
805 "local, global, export, debug, function, weak, section, "
806 "constructor, warning, indirect, file, object, synthetic, "
807 "indirect-function, unique-object, before=<othersym>");
817 /* Find and optionally add an entry in the change_sections list.
819 We need to be careful in how we match section names because of the support
820 for wildcard characters. For example suppose that the user has invoked
823 --set-section-flags .debug_*=debug
824 --set-section-flags .debug_str=readonly,debug
825 --change-section-address .debug_*ranges=0x1000
827 With the idea that all debug sections will receive the DEBUG flag, the
828 .debug_str section will also receive the READONLY flag and the
829 .debug_ranges and .debug_aranges sections will have their address set to
830 0x1000. (This may not make much sense, but it is just an example).
832 When adding the section name patterns to the section list we need to make
833 sure that previous entries do not match with the new entry, unless the
834 match is exact. (In which case we assume that the user is overriding
835 the previous entry with the new context).
837 When matching real section names to the section list we make use of the
838 wildcard characters, but we must do so in context. Eg if we are setting
839 section addresses then we match for .debug_ranges but not for .debug_info.
841 Finally, if ADD is false and we do find a match, we mark the section list
844 static struct section_list *
845 find_section_list (const char *name, bfd_boolean add, unsigned int context)
847 struct section_list *p;
849 /* assert ((context & ((1 << 7) - 1)) != 0); */
851 for (p = change_sections; p != NULL; p = p->next)
855 if (strcmp (p->pattern, name) == 0)
857 /* Check for context conflicts. */
858 if (((p->context & SECTION_CONTEXT_REMOVE)
859 && (context & SECTION_CONTEXT_COPY))
860 || ((context & SECTION_CONTEXT_REMOVE)
861 && (p->context & SECTION_CONTEXT_COPY)))
862 fatal (_("error: %s both copied and removed"), name);
864 if (((p->context & SECTION_CONTEXT_SET_VMA)
865 && (context & SECTION_CONTEXT_ALTER_VMA))
866 || ((context & SECTION_CONTEXT_SET_VMA)
867 && (context & SECTION_CONTEXT_ALTER_VMA)))
868 fatal (_("error: %s both sets and alters VMA"), name);
870 if (((p->context & SECTION_CONTEXT_SET_LMA)
871 && (context & SECTION_CONTEXT_ALTER_LMA))
872 || ((context & SECTION_CONTEXT_SET_LMA)
873 && (context & SECTION_CONTEXT_ALTER_LMA)))
874 fatal (_("error: %s both sets and alters LMA"), name);
876 /* Extend the context. */
877 p->context |= context;
881 /* If we are not adding a new name/pattern then
882 only check for a match if the context applies. */
883 else if ((p->context & context)
884 /* We could check for the presence of wildchar characters
885 first and choose between calling strcmp and fnmatch,
886 but is that really worth it ? */
887 && fnmatch (p->pattern, name, 0) == 0)
897 p = (struct section_list *) xmalloc (sizeof (struct section_list));
900 p->context = context;
904 p->next = change_sections;
910 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
913 eq_string (const void *s1, const void *s2)
915 return strcmp ((const char *) s1, (const char *) s2) == 0;
919 create_symbol_htab (void)
921 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
925 create_symbol_htabs (void)
927 strip_specific_htab = create_symbol_htab ();
928 strip_unneeded_htab = create_symbol_htab ();
929 keep_specific_htab = create_symbol_htab ();
930 localize_specific_htab = create_symbol_htab ();
931 globalize_specific_htab = create_symbol_htab ();
932 keepglobal_specific_htab = create_symbol_htab ();
933 weaken_specific_htab = create_symbol_htab ();
936 /* Add a symbol to strip_specific_list. */
939 add_specific_symbol (const char *name, htab_t htab)
941 *htab_find_slot (htab, name, INSERT) = (char *) name;
944 /* Add symbols listed in `filename' to strip_specific_list. */
946 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
947 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
950 add_specific_symbols (const char *filename, htab_t htab)
956 unsigned int line_count;
958 size = get_file_size (filename);
965 buffer = (char *) xmalloc (size + 2);
966 f = fopen (filename, FOPEN_RT);
968 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
970 if (fread (buffer, 1, size, f) == 0 || ferror (f))
971 fatal (_("%s: fread failed"), filename);
974 buffer [size] = '\n';
975 buffer [size + 1] = '\0';
979 for (line = buffer; * line != '\0'; line ++)
984 int finished = FALSE;
986 for (eol = line;; eol ++)
992 /* Cope with \n\r. */
1000 /* Cope with \r\n. */
1011 /* Line comment, Terminate the line here, in case a
1012 name is present and then allow the rest of the
1013 loop to find the real end of the line. */
1025 /* A name may now exist somewhere between 'line' and 'eol'.
1026 Strip off leading whitespace and trailing whitespace,
1027 then add it to the list. */
1028 for (name = line; IS_WHITESPACE (* name); name ++)
1030 for (name_end = name;
1031 (! IS_WHITESPACE (* name_end))
1032 && (! IS_LINE_TERMINATOR (* name_end));
1036 if (! IS_LINE_TERMINATOR (* name_end))
1040 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
1043 if (! IS_LINE_TERMINATOR (* extra))
1044 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1045 filename, line_count);
1050 if (name_end > name)
1051 add_specific_symbol (name, htab);
1053 /* Advance line pointer to end of line. The 'eol ++' in the for
1054 loop above will then advance us to the start of the next line. */
1060 /* See whether a symbol should be stripped or kept
1061 based on strip_specific_list and keep_symbols. */
1064 is_specified_symbol_predicate (void **slot, void *data)
1066 struct is_specified_symbol_predicate_data *d =
1067 (struct is_specified_symbol_predicate_data *) data;
1068 const char *slot_name = (char *) *slot;
1070 if (*slot_name != '!')
1072 if (! fnmatch (slot_name, d->name, 0))
1075 /* Continue traversal, there might be a non-match rule. */
1081 if (! fnmatch (slot_name + 1, d->name, 0))
1084 /* Stop traversal. */
1089 /* Continue traversal. */
1094 is_specified_symbol (const char *name, htab_t htab)
1098 struct is_specified_symbol_predicate_data data;
1103 htab_traverse (htab, is_specified_symbol_predicate, &data);
1108 return htab_find (htab, name) != NULL;
1111 /* Return a pointer to the symbol used as a signature for GROUP. */
1114 group_signature (asection *group)
1116 bfd *abfd = group->owner;
1117 Elf_Internal_Shdr *ghdr;
1119 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1122 ghdr = &elf_section_data (group)->this_hdr;
1123 if (ghdr->sh_link < elf_numsections (abfd))
1125 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1126 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
1128 if (symhdr->sh_type == SHT_SYMTAB
1129 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
1130 return isympp[ghdr->sh_info - 1];
1135 /* Return TRUE if the section is a DWO section. */
1138 is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1140 const char *name = bfd_get_section_name (abfd, sec);
1141 int len = strlen (name);
1143 return strncmp (name + len - 4, ".dwo", 4) == 0;
1146 /* Return TRUE if section SEC is in the update list. */
1149 is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1151 if (update_sections != NULL)
1153 struct section_add *pupdate;
1155 for (pupdate = update_sections;
1157 pupdate = pupdate->next)
1159 if (strcmp (sec->name, pupdate->name) == 0)
1167 /* See if a non-group section is being removed. */
1170 is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1172 if (sections_removed || sections_copied)
1174 struct section_list *p;
1175 struct section_list *q;
1177 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1178 SECTION_CONTEXT_REMOVE);
1179 q = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1180 SECTION_CONTEXT_COPY);
1183 fatal (_("error: section %s matches both remove and copy options"),
1184 bfd_get_section_name (abfd, sec));
1185 if (p && is_update_section (abfd, sec))
1186 fatal (_("error: section %s matches both update and remove options"),
1187 bfd_get_section_name (abfd, sec));
1191 if (sections_copied && q == NULL)
1195 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
1197 if (strip_symbols == STRIP_DEBUG
1198 || strip_symbols == STRIP_UNNEEDED
1199 || strip_symbols == STRIP_ALL
1200 || discard_locals == LOCALS_ALL
1201 || convert_debugging)
1203 /* By default we don't want to strip .reloc section.
1204 This section has for pe-coff special meaning. See
1205 pe-dll.c file in ld, and peXXigen.c in bfd for details. */
1206 if (strcmp (bfd_get_section_name (abfd, sec), ".reloc") != 0)
1210 if (strip_symbols == STRIP_DWO)
1211 return is_dwo_section (abfd, sec);
1213 if (strip_symbols == STRIP_NONDEBUG)
1217 if (strip_symbols == STRIP_NONDWO)
1218 return !is_dwo_section (abfd, sec);
1223 /* See if a section is being removed. */
1226 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1228 if (is_strip_section_1 (abfd, sec))
1231 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
1235 asection *elt, *first;
1238 If we are going to strip the group signature symbol, then
1239 strip the group section too. */
1240 gsym = group_signature (sec);
1245 if ((strip_symbols == STRIP_ALL
1246 && !is_specified_symbol (gname, keep_specific_htab))
1247 || is_specified_symbol (gname, strip_specific_htab))
1250 /* Remove the group section if all members are removed. */
1251 first = elt = elf_next_in_group (sec);
1254 if (!is_strip_section_1 (abfd, elt))
1256 elt = elf_next_in_group (elt);
1268 is_nondebug_keep_contents_section (bfd *ibfd, asection *isection)
1270 /* Always keep ELF note sections. */
1271 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
1272 return (elf_section_type (isection) == SHT_NOTE);
1274 /* Always keep the .buildid section for PE/COFF.
1276 Strictly, this should be written "always keep the section storing the debug
1277 directory", but that may be the .text section for objects produced by some
1278 tools, which it is not sensible to keep. */
1279 if (ibfd->xvec->flavour == bfd_target_coff_flavour)
1280 return (strcmp (bfd_get_section_name (ibfd, isection), ".buildid") == 0);
1285 /* Return true if SYM is a hidden symbol. */
1288 is_hidden_symbol (asymbol *sym)
1290 elf_symbol_type *elf_sym;
1292 elf_sym = elf_symbol_from (sym->the_bfd, sym);
1293 if (elf_sym != NULL)
1294 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
1304 need_sym_before (struct addsym_node **node, const char *sym)
1307 struct addsym_node *ptr = add_sym_list;
1309 /* 'othersym' symbols are at the front of the list. */
1310 for (count = 0; count < add_symbols; count++)
1314 else if (strcmp (ptr->othersym, sym) == 0)
1316 free (ptr->othersym);
1317 ptr->othersym = ""; /* Empty name is hopefully never a valid symbol name. */
1327 create_new_symbol (struct addsym_node *ptr, bfd *obfd)
1329 asymbol *sym = bfd_make_empty_symbol(obfd);
1331 bfd_asymbol_name(sym) = ptr->symdef;
1332 sym->value = ptr->symval;
1333 sym->flags = ptr->flags;
1336 asection *sec = bfd_get_section_by_name (obfd, ptr->section);
1338 fatal (_("Section %s not found"), ptr->section);
1342 sym->section = bfd_abs_section_ptr;
1346 /* Choose which symbol entries to copy; put the result in OSYMS.
1347 We don't copy in place, because that confuses the relocs.
1348 Return the number of symbols to print. */
1351 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
1352 asymbol **isyms, long symcount)
1354 asymbol **from = isyms, **to = osyms;
1355 long src_count = 0, dst_count = 0;
1356 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1358 for (; src_count < symcount; src_count++)
1360 asymbol *sym = from[src_count];
1361 flagword flags = sym->flags;
1362 char *name = (char *) bfd_asymbol_name (sym);
1364 bfd_boolean used_in_reloc = FALSE;
1365 bfd_boolean undefined;
1366 bfd_boolean rem_leading_char;
1367 bfd_boolean add_leading_char;
1369 undefined = bfd_is_und_section (bfd_get_section (sym));
1373 struct addsym_node *ptr;
1375 if (need_sym_before (&ptr, name))
1376 to[dst_count++] = create_new_symbol (ptr, obfd);
1379 if (redefine_sym_list)
1381 char *old_name, *new_name;
1383 old_name = (char *) bfd_asymbol_name (sym);
1384 new_name = (char *) lookup_sym_redefinition (old_name);
1385 bfd_asymbol_name (sym) = new_name;
1389 /* Check if we will remove the current leading character. */
1391 (name[0] == bfd_get_symbol_leading_char (abfd))
1392 && (change_leading_char
1393 || (remove_leading_char
1394 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1396 || bfd_is_com_section (bfd_get_section (sym)))));
1398 /* Check if we will add a new leading character. */
1401 && (bfd_get_symbol_leading_char (obfd) != '\0')
1402 && (bfd_get_symbol_leading_char (abfd) == '\0'
1403 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1405 /* Short circuit for change_leading_char if we can do it in-place. */
1406 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1408 name[0] = bfd_get_symbol_leading_char (obfd);
1409 bfd_asymbol_name (sym) = name;
1410 rem_leading_char = FALSE;
1411 add_leading_char = FALSE;
1414 /* Remove leading char. */
1415 if (rem_leading_char)
1416 bfd_asymbol_name (sym) = ++name;
1418 /* Add new leading char and/or prefix. */
1419 if (add_leading_char || prefix_symbols_string)
1423 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1424 + strlen (name) + 1);
1425 if (add_leading_char)
1426 *ptr++ = bfd_get_symbol_leading_char (obfd);
1428 if (prefix_symbols_string)
1430 strcpy (ptr, prefix_symbols_string);
1431 ptr += strlen (prefix_symbols_string);
1435 bfd_asymbol_name (sym) = n;
1439 if (strip_symbols == STRIP_ALL)
1441 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1442 || ((flags & BSF_SECTION_SYM) != 0
1443 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1447 used_in_reloc = TRUE;
1449 else if (relocatable /* Relocatable file. */
1450 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1451 || bfd_is_com_section (bfd_get_section (sym))))
1453 else if (bfd_decode_symclass (sym) == 'I')
1454 /* Global symbols in $idata sections need to be retained
1455 even if relocatable is FALSE. External users of the
1456 library containing the $idata section may reference these
1459 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1460 || (flags & BSF_WEAK) != 0
1462 || bfd_is_com_section (bfd_get_section (sym)))
1463 keep = strip_symbols != STRIP_UNNEEDED;
1464 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1465 keep = (strip_symbols != STRIP_DEBUG
1466 && strip_symbols != STRIP_UNNEEDED
1467 && ! convert_debugging);
1468 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1469 /* COMDAT sections store special information in local
1470 symbols, so we cannot risk stripping any of them. */
1472 else /* Local symbol. */
1473 keep = (strip_symbols != STRIP_UNNEEDED
1474 && (discard_locals != LOCALS_ALL
1475 && (discard_locals != LOCALS_START_L
1476 || ! bfd_is_local_label (abfd, sym))));
1478 if (keep && is_specified_symbol (name, strip_specific_htab))
1480 /* There are multiple ways to set 'keep' above, but if it
1481 was the relocatable symbol case, then that's an error. */
1484 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1492 && !(flags & BSF_KEEP)
1493 && is_specified_symbol (name, strip_unneeded_htab))
1497 && ((keep_file_symbols && (flags & BSF_FILE))
1498 || is_specified_symbol (name, keep_specific_htab)))
1501 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1506 if ((flags & BSF_GLOBAL) != 0
1507 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1509 sym->flags &= ~ BSF_GLOBAL;
1510 sym->flags |= BSF_WEAK;
1514 && (flags & (BSF_GLOBAL | BSF_WEAK))
1515 && (is_specified_symbol (name, localize_specific_htab)
1516 || (htab_elements (keepglobal_specific_htab) != 0
1517 && ! is_specified_symbol (name, keepglobal_specific_htab))
1518 || (localize_hidden && is_hidden_symbol (sym))))
1520 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1521 sym->flags |= BSF_LOCAL;
1525 && (flags & BSF_LOCAL)
1526 && is_specified_symbol (name, globalize_specific_htab))
1528 sym->flags &= ~ BSF_LOCAL;
1529 sym->flags |= BSF_GLOBAL;
1532 to[dst_count++] = sym;
1537 struct addsym_node *ptr = add_sym_list;
1539 for (src_count = 0; src_count < add_symbols; src_count++)
1543 if (strcmp (ptr->othersym, ""))
1544 fatal (_("'before=%s' not found"), ptr->othersym);
1547 to[dst_count++] = create_new_symbol (ptr, obfd);
1553 to[dst_count] = NULL;
1558 /* Find the redefined name of symbol SOURCE. */
1561 lookup_sym_redefinition (const char *source)
1563 struct redefine_node *list;
1565 for (list = redefine_sym_list; list != NULL; list = list->next)
1566 if (strcmp (source, list->source) == 0)
1567 return list->target;
1572 /* Add a node to a symbol redefine list. */
1575 redefine_list_append (const char *cause, const char *source, const char *target)
1577 struct redefine_node **p;
1578 struct redefine_node *list;
1579 struct redefine_node *new_node;
1581 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1583 if (strcmp (source, list->source) == 0)
1584 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1587 if (strcmp (target, list->target) == 0)
1588 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1592 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1594 new_node->source = strdup (source);
1595 new_node->target = strdup (target);
1596 new_node->next = NULL;
1601 /* Handle the --redefine-syms option. Read lines containing "old new"
1602 from the file, and add them to the symbol redefine list. */
1605 add_redefine_syms_file (const char *filename)
1614 file = fopen (filename, "r");
1616 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1617 filename, strerror (errno));
1620 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
1628 /* Collect the input symbol name. */
1629 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1637 buf = (char *) xrealloc (buf, bufsize + 1);
1645 /* Eat white space between the symbol names. */
1646 while (IS_WHITESPACE (c))
1648 if (c == '#' || IS_LINE_TERMINATOR (c))
1653 /* Collect the output symbol name. */
1655 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1663 buf = (char *) xrealloc (buf, bufsize + 1);
1671 /* Eat white space at end of line. */
1672 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1677 if ((c == '\r' && (c = getc (file)) == '\n')
1678 || c == '\n' || c == EOF)
1681 /* Append the redefinition to the list. */
1683 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1694 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1696 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1697 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1700 /* Eat the rest of the line and finish it. */
1701 while (c != '\n' && c != EOF)
1707 fatal (_("%s:%d: premature end of file"), filename, lineno);
1712 /* Copy unkown object file IBFD onto OBFD.
1713 Returns TRUE upon success, FALSE otherwise. */
1716 copy_unknown_object (bfd *ibfd, bfd *obfd)
1724 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1726 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1733 non_fatal (_("stat returns negative size for `%s'"),
1734 bfd_get_archive_filename (ibfd));
1738 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1740 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1745 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1746 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1748 cbuf = (char *) xmalloc (BUFSIZE);
1750 while (ncopied < size)
1752 tocopy = size - ncopied;
1753 if (tocopy > BUFSIZE)
1756 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1757 != (bfd_size_type) tocopy)
1759 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1764 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1765 != (bfd_size_type) tocopy)
1767 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1775 /* We should at least to be able to read it back when copying an
1776 unknown object in an archive. */
1777 chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
1782 /* Copy object file IBFD onto OBFD.
1783 Returns TRUE upon success, FALSE otherwise. */
1786 copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
1790 asection **osections = NULL;
1791 asection *gnu_debuglink_section = NULL;
1792 bfd_size_type *gaps = NULL;
1793 bfd_size_type max_gap = 0;
1796 enum bfd_architecture iarch;
1800 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1801 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1802 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1804 /* PR 17636: Call non-fatal so that we return to our parent who
1805 may need to tidy temporary files. */
1806 non_fatal (_("Unable to change endianness of input file(s)"));
1810 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1812 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1816 if (ibfd->sections == NULL)
1818 non_fatal (_("error: the input file '%s' has no sections"),
1819 bfd_get_archive_filename (ibfd));
1823 if ((do_debug_sections & compress) != 0
1824 && do_debug_sections != compress
1825 && ibfd->xvec->flavour != bfd_target_elf_flavour)
1827 non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
1828 bfd_get_archive_filename (ibfd));
1833 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1834 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1835 bfd_get_filename (obfd), bfd_get_target (obfd));
1844 start = bfd_get_start_address (ibfd);
1845 start += change_start;
1848 /* Neither the start address nor the flags
1849 need to be set for a core file. */
1850 if (bfd_get_format (obfd) != bfd_core)
1854 flags = bfd_get_file_flags (ibfd);
1855 flags |= bfd_flags_to_set;
1856 flags &= ~bfd_flags_to_clear;
1857 flags &= bfd_applicable_file_flags (obfd);
1859 if (strip_symbols == STRIP_ALL)
1860 flags &= ~HAS_RELOC;
1862 if (!bfd_set_start_address (obfd, start)
1863 || !bfd_set_file_flags (obfd, flags))
1865 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1870 /* Copy architecture of input file to output file. */
1871 iarch = bfd_get_arch (ibfd);
1872 imach = bfd_get_mach (ibfd);
1875 if (bfd_get_arch_info (ibfd) == NULL
1876 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
1878 iarch = input_arch->arch;
1879 imach = input_arch->mach;
1882 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1883 bfd_get_archive_filename (ibfd));
1885 if (!bfd_set_arch_mach (obfd, iarch, imach)
1886 && (ibfd->target_defaulted
1887 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1889 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1890 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1891 bfd_get_archive_filename (ibfd));
1893 non_fatal (_("Output file cannot represent architecture `%s'"),
1894 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1895 bfd_get_mach (ibfd)));
1899 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1901 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1905 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1906 && bfd_pei_p (obfd))
1908 /* Set up PE parameters. */
1909 pe_data_type *pe = pe_data (obfd);
1911 /* Copy PE parameters before changing them. */
1912 if (ibfd->xvec->flavour == bfd_target_coff_flavour
1913 && bfd_pei_p (ibfd))
1914 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1916 if (pe_file_alignment != (bfd_vma) -1)
1917 pe->pe_opthdr.FileAlignment = pe_file_alignment;
1919 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
1921 if (pe_heap_commit != (bfd_vma) -1)
1922 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
1924 if (pe_heap_reserve != (bfd_vma) -1)
1925 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
1927 if (pe_image_base != (bfd_vma) -1)
1928 pe->pe_opthdr.ImageBase = pe_image_base;
1930 if (pe_section_alignment != (bfd_vma) -1)
1931 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
1933 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
1935 if (pe_stack_commit != (bfd_vma) -1)
1936 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
1938 if (pe_stack_reserve != (bfd_vma) -1)
1939 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
1941 if (pe_subsystem != -1)
1942 pe->pe_opthdr.Subsystem = pe_subsystem;
1944 if (pe_major_subsystem_version != -1)
1945 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
1947 if (pe_minor_subsystem_version != -1)
1948 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
1950 if (pe_file_alignment > pe_section_alignment)
1952 char file_alignment[20], section_alignment[20];
1954 sprintf_vma (file_alignment, pe_file_alignment);
1955 sprintf_vma (section_alignment, pe_section_alignment);
1956 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1958 file_alignment, section_alignment);
1965 if (osympp != isympp)
1971 symsize = bfd_get_symtab_upper_bound (ibfd);
1974 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1978 osympp = isympp = (asymbol **) xmalloc (symsize);
1979 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1982 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1985 /* PR 17512: file: d6323821
1986 If the symbol table could not be loaded do not pretend that we have
1987 any symbols. This trips us up later on when we load the relocs. */
1991 osympp = isympp = NULL;
1994 /* BFD mandates that all output sections be created and sizes set before
1995 any output is done. Thus, we traverse all sections multiple times. */
1996 bfd_map_over_sections (ibfd, setup_section, obfd);
1998 if (!extract_symbol)
1999 setup_bfd_headers (ibfd, obfd);
2001 if (add_sections != NULL)
2003 struct section_add *padd;
2004 struct section_list *pset;
2006 for (padd = add_sections; padd != NULL; padd = padd->next)
2010 pset = find_section_list (padd->name, FALSE,
2011 SECTION_CONTEXT_SET_FLAGS);
2013 flags = pset->flags | SEC_HAS_CONTENTS;
2015 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
2017 /* bfd_make_section_with_flags() does not return very helpful
2018 error codes, so check for the most likely user error first. */
2019 if (bfd_get_section_by_name (obfd, padd->name))
2021 bfd_nonfatal_message (NULL, obfd, NULL,
2022 _("can't add section '%s'"), padd->name);
2027 /* We use LINKER_CREATED here so that the backend hooks
2028 will create any special section type information,
2029 instead of presuming we know what we're doing merely
2030 because we set the flags. */
2031 padd->section = bfd_make_section_with_flags
2032 (obfd, padd->name, flags | SEC_LINKER_CREATED);
2033 if (padd->section == NULL)
2035 bfd_nonfatal_message (NULL, obfd, NULL,
2036 _("can't create section `%s'"),
2042 if (! bfd_set_section_size (obfd, padd->section, padd->size))
2044 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2048 pset = find_section_list (padd->name, FALSE,
2049 SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
2051 && ! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
2053 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2057 pset = find_section_list (padd->name, FALSE,
2058 SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
2061 padd->section->lma = pset->lma_val;
2063 if (! bfd_set_section_alignment
2064 (obfd, padd->section,
2065 bfd_section_alignment (obfd, padd->section)))
2067 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2074 if (update_sections != NULL)
2076 struct section_add *pupdate;
2078 for (pupdate = update_sections;
2080 pupdate = pupdate->next)
2084 pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name);
2085 if (pupdate->section == NULL)
2087 non_fatal (_("error: %s not found, can't be updated"), pupdate->name);
2091 osec = pupdate->section->output_section;
2092 if (! bfd_set_section_size (obfd, osec, pupdate->size))
2094 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2100 if (dump_sections != NULL)
2102 struct section_add * pdump;
2104 for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
2108 sec = bfd_get_section_by_name (ibfd, pdump->name);
2111 bfd_nonfatal_message (NULL, ibfd, NULL,
2112 _("can't dump section '%s' - it does not exist"),
2117 if ((bfd_get_section_flags (ibfd, sec) & SEC_HAS_CONTENTS) == 0)
2119 bfd_nonfatal_message (NULL, ibfd, sec,
2120 _("can't dump section - it has no contents"));
2124 bfd_size_type size = bfd_get_section_size (sec);
2127 bfd_nonfatal_message (NULL, ibfd, sec,
2128 _("can't dump section - it is empty"));
2133 f = fopen (pdump->filename, FOPEN_WB);
2136 bfd_nonfatal_message (pdump->filename, NULL, NULL,
2137 _("could not open section dump file"));
2141 bfd_byte * contents = xmalloc (size);
2142 if (bfd_get_section_contents (ibfd, sec, contents, 0, size))
2144 if (fwrite (contents, 1, size, f) != size)
2146 non_fatal (_("error writing section contents to %s (error: %s)"),
2153 bfd_nonfatal_message (NULL, ibfd, sec,
2154 _("could not retrieve section contents"));
2161 if (gnu_debuglink_filename != NULL)
2163 /* PR 15125: Give a helpful warning message if
2164 the debuglink section already exists, and
2165 allow the rest of the copy to complete. */
2166 if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
2168 non_fatal (_("%s: debuglink section already exists"),
2169 bfd_get_filename (obfd));
2170 gnu_debuglink_filename = NULL;
2174 gnu_debuglink_section = bfd_create_gnu_debuglink_section
2175 (obfd, gnu_debuglink_filename);
2177 if (gnu_debuglink_section == NULL)
2179 bfd_nonfatal_message (NULL, obfd, NULL,
2180 _("cannot create debug link section `%s'"),
2181 gnu_debuglink_filename);
2185 /* Special processing for PE format files. We
2186 have no way to distinguish PE from COFF here. */
2187 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
2189 bfd_vma debuglink_vma;
2190 asection * highest_section;
2193 /* The PE spec requires that all sections be adjacent and sorted
2194 in ascending order of VMA. It also specifies that debug
2195 sections should be last. This is despite the fact that debug
2196 sections are not loaded into memory and so in theory have no
2199 This means that the debuglink section must be given a non-zero
2200 VMA which makes it contiguous with other debug sections. So
2201 walk the current section list, find the section with the
2202 highest VMA and start the debuglink section after that one. */
2203 for (sec = obfd->sections, highest_section = NULL;
2207 && (highest_section == NULL
2208 || sec->vma > highest_section->vma))
2209 highest_section = sec;
2211 if (highest_section)
2212 debuglink_vma = BFD_ALIGN (highest_section->vma
2213 + highest_section->size,
2214 /* FIXME: We ought to be using
2215 COFF_PAGE_SIZE here or maybe
2216 bfd_get_section_alignment() (if it
2217 was set) but since this is for PE
2218 and we know the required alignment
2219 it is easier just to hard code it. */
2222 /* Umm, not sure what to do in this case. */
2223 debuglink_vma = 0x1000;
2225 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
2230 c = bfd_count_sections (obfd);
2232 && (gap_fill_set || pad_to_set))
2236 /* We must fill in gaps between the sections and/or we must pad
2237 the last section to a specified address. We do this by
2238 grabbing a list of the sections, sorting them by VMA, and
2239 increasing the section sizes as required to fill the gaps.
2240 We write out the gap contents below. */
2242 osections = (asection **) xmalloc (c * sizeof (asection *));
2244 bfd_map_over_sections (obfd, get_sections, &set);
2246 qsort (osections, c, sizeof (asection *), compare_section_lma);
2248 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
2249 memset (gaps, 0, c * sizeof (bfd_size_type));
2253 for (i = 0; i < c - 1; i++)
2257 bfd_vma gap_start, gap_stop;
2259 flags = bfd_get_section_flags (obfd, osections[i]);
2260 if ((flags & SEC_HAS_CONTENTS) == 0
2261 || (flags & SEC_LOAD) == 0)
2264 size = bfd_section_size (obfd, osections[i]);
2265 gap_start = bfd_section_lma (obfd, osections[i]) + size;
2266 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
2267 if (gap_start < gap_stop)
2269 if (! bfd_set_section_size (obfd, osections[i],
2270 size + (gap_stop - gap_start)))
2272 bfd_nonfatal_message (NULL, obfd, osections[i],
2273 _("Can't fill gap after section"));
2277 gaps[i] = gap_stop - gap_start;
2278 if (max_gap < gap_stop - gap_start)
2279 max_gap = gap_stop - gap_start;
2289 lma = bfd_section_lma (obfd, osections[c - 1]);
2290 size = bfd_section_size (obfd, osections[c - 1]);
2291 if (lma + size < pad_to)
2293 if (! bfd_set_section_size (obfd, osections[c - 1],
2296 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
2297 _("can't add padding"));
2302 gaps[c - 1] = pad_to - (lma + size);
2303 if (max_gap < pad_to - (lma + size))
2304 max_gap = pad_to - (lma + size);
2310 /* Symbol filtering must happen after the output sections
2311 have been created, but before their contents are set. */
2313 if (convert_debugging)
2314 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
2316 if (strip_symbols == STRIP_DEBUG
2317 || strip_symbols == STRIP_ALL
2318 || strip_symbols == STRIP_UNNEEDED
2319 || strip_symbols == STRIP_NONDEBUG
2320 || strip_symbols == STRIP_DWO
2321 || strip_symbols == STRIP_NONDWO
2322 || discard_locals != LOCALS_UNDEF
2324 || htab_elements (strip_specific_htab) != 0
2325 || htab_elements (keep_specific_htab) != 0
2326 || htab_elements (localize_specific_htab) != 0
2327 || htab_elements (globalize_specific_htab) != 0
2328 || htab_elements (keepglobal_specific_htab) != 0
2329 || htab_elements (weaken_specific_htab) != 0
2330 || prefix_symbols_string
2333 || convert_debugging
2334 || change_leading_char
2335 || remove_leading_char
2336 || redefine_sym_list
2340 /* Mark symbols used in output relocations so that they
2341 are kept, even if they are local labels or static symbols.
2343 Note we iterate over the input sections examining their
2344 relocations since the relocations for the output sections
2345 haven't been set yet. mark_symbols_used_in_relocations will
2346 ignore input sections which have no corresponding output
2348 if (strip_symbols != STRIP_ALL)
2349 bfd_map_over_sections (ibfd,
2350 mark_symbols_used_in_relocations,
2352 osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *));
2353 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
2356 if (convert_debugging && dhandle != NULL)
2358 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
2365 bfd_set_symtab (obfd, osympp, symcount);
2367 /* This has to happen before section positions are set. */
2368 bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
2370 /* This has to happen after the symbol table has been set. */
2371 bfd_map_over_sections (ibfd, copy_section, obfd);
2373 if (add_sections != NULL)
2375 struct section_add *padd;
2377 for (padd = add_sections; padd != NULL; padd = padd->next)
2379 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
2382 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2388 if (update_sections != NULL)
2390 struct section_add *pupdate;
2392 for (pupdate = update_sections;
2394 pupdate = pupdate->next)
2398 osec = pupdate->section->output_section;
2399 if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
2402 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2408 if (gnu_debuglink_filename != NULL)
2410 if (! bfd_fill_in_gnu_debuglink_section
2411 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
2413 bfd_nonfatal_message (NULL, obfd, NULL,
2414 _("cannot fill debug link section `%s'"),
2415 gnu_debuglink_filename);
2420 if (gap_fill_set || pad_to_set)
2424 /* Fill in the gaps. */
2427 buf = (bfd_byte *) xmalloc (max_gap);
2428 memset (buf, gap_fill, max_gap);
2430 c = bfd_count_sections (obfd);
2431 for (i = 0; i < c; i++)
2439 off = bfd_section_size (obfd, osections[i]) - left;
2450 if (! bfd_set_section_contents (obfd, osections[i], buf,
2453 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
2464 /* Allow the BFD backend to copy any private data it understands
2465 from the input BFD to the output BFD. This is done last to
2466 permit the routine to look at the filtered symbol table, which is
2467 important for the ECOFF code at least. */
2468 if (! bfd_copy_private_bfd_data (ibfd, obfd))
2470 bfd_nonfatal_message (NULL, obfd, NULL,
2471 _("error copying private BFD data"));
2475 /* Switch to the alternate machine code. We have to do this at the
2476 very end, because we only initialize the header when we create
2477 the first section. */
2478 if (use_alt_mach_code != 0)
2480 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
2482 non_fatal (_("this target does not support %lu alternative machine codes"),
2484 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2486 non_fatal (_("treating that number as an absolute e_machine value instead"));
2487 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
2490 non_fatal (_("ignoring the alternative value"));
2497 /* Read each archive element in turn from IBFD, copy the
2498 contents to temp file, and keep the temp file handle.
2499 If 'force_output_target' is TRUE then make sure that
2500 all elements in the new archive are of the type
2504 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
2505 bfd_boolean force_output_target,
2506 const bfd_arch_info_type *input_arch)
2510 struct name_list *next;
2514 bfd **ptr = &obfd->archive_head;
2517 const char *filename;
2519 /* Make a temp directory to hold the contents. */
2520 dir = make_tempdir (bfd_get_filename (obfd));
2522 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2525 if (strip_symbols == STRIP_ALL)
2526 obfd->has_armap = FALSE;
2528 obfd->has_armap = ibfd->has_armap;
2529 obfd->is_thin_archive = ibfd->is_thin_archive;
2532 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2536 this_element = bfd_openr_next_archived_file (ibfd, NULL);
2538 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2541 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
2542 goto cleanup_and_exit;
2545 while (!status && this_element != NULL)
2551 int stat_status = 0;
2552 bfd_boolean del = TRUE;
2553 bfd_boolean ok_object;
2555 /* PR binutils/17533: Do not allow directory traversal
2556 outside of the current directory tree by archive members. */
2557 if (! is_valid_archive_path (bfd_get_filename (this_element)))
2559 non_fatal (_("illegal pathname found in archive member: %s"),
2560 bfd_get_filename (this_element));
2562 goto cleanup_and_exit;
2565 /* Create an output file for this member. */
2566 output_name = concat (dir, "/",
2567 bfd_get_filename (this_element), (char *) 0);
2569 /* If the file already exists, make another temp dir. */
2570 if (stat (output_name, &buf) >= 0)
2572 output_name = make_tempdir (output_name);
2573 if (output_name == NULL)
2575 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
2578 goto cleanup_and_exit;
2581 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2582 l->name = output_name;
2586 output_name = concat (output_name, "/",
2587 bfd_get_filename (this_element), (char *) 0);
2592 stat_status = bfd_stat_arch_elt (this_element, &buf);
2594 if (stat_status != 0)
2595 non_fatal (_("internal stat error on %s"),
2596 bfd_get_filename (this_element));
2599 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2600 l->name = output_name;
2605 ok_object = bfd_check_format (this_element, bfd_object);
2607 bfd_nonfatal_message (NULL, this_element, NULL,
2608 _("Unable to recognise the format of file"));
2610 /* PR binutils/3110: Cope with archives
2611 containing multiple target types. */
2612 if (force_output_target || !ok_object)
2613 output_bfd = bfd_openw (output_name, output_target);
2615 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
2617 if (output_bfd == NULL)
2619 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2621 goto cleanup_and_exit;
2626 del = !copy_object (this_element, output_bfd, input_arch);
2628 if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
2629 /* Try again as an unknown object file. */
2631 else if (!bfd_close (output_bfd))
2633 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2634 /* Error in new object file. Don't change archive. */
2641 del = !copy_unknown_object (this_element, output_bfd);
2642 if (!bfd_close_all_done (output_bfd))
2644 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2645 /* Error in new object file. Don't change archive. */
2652 unlink (output_name);
2657 if (preserve_dates && stat_status == 0)
2658 set_times (output_name, &buf);
2660 /* Open the newly output file and attach to our list. */
2661 output_bfd = bfd_openr (output_name, output_target);
2663 l->obfd = output_bfd;
2666 ptr = &output_bfd->archive_next;
2668 last_element = this_element;
2670 this_element = bfd_openr_next_archived_file (ibfd, last_element);
2672 bfd_close (last_element);
2677 filename = bfd_get_filename (obfd);
2678 if (!bfd_close (obfd))
2681 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2684 filename = bfd_get_filename (ibfd);
2685 if (!bfd_close (ibfd))
2688 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2692 /* Delete all the files that we opened. */
2693 for (l = list; l != NULL; l = l->next)
2695 if (l->obfd == NULL)
2699 bfd_close (l->obfd);
2708 set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
2710 /* This is only relevant to Coff targets. */
2711 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
2714 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
2715 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
2716 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
2720 /* The top-level control. */
2723 copy_file (const char *input_filename, const char *output_filename,
2724 const char *input_target, const char *output_target,
2725 const bfd_arch_info_type *input_arch)
2728 char **obj_matching;
2729 char **core_matching;
2730 off_t size = get_file_size (input_filename);
2735 non_fatal (_("error: the input file '%s' is empty"),
2741 /* To allow us to do "strip *" without dying on the first
2742 non-object file, failures are nonfatal. */
2743 ibfd = bfd_openr (input_filename, input_target);
2746 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2751 switch (do_debug_sections)
2755 case compress_gnu_zlib:
2756 case compress_gabi_zlib:
2757 ibfd->flags |= BFD_COMPRESS;
2758 /* Don't check if input is ELF here since this information is
2759 only available after bfd_check_format_matches is called. */
2760 if (do_debug_sections != compress_gnu_zlib)
2761 ibfd->flags |= BFD_COMPRESS_GABI;
2764 ibfd->flags |= BFD_DECOMPRESS;
2770 if (bfd_check_format (ibfd, bfd_archive))
2772 bfd_boolean force_output_target;
2775 /* bfd_get_target does not return the correct value until
2776 bfd_check_format succeeds. */
2777 if (output_target == NULL)
2779 output_target = bfd_get_target (ibfd);
2780 force_output_target = FALSE;
2783 force_output_target = TRUE;
2785 obfd = bfd_openw (output_filename, output_target);
2788 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2792 /* This is a no-op on non-Coff targets. */
2793 set_long_section_mode (obfd, ibfd, long_section_names);
2795 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
2797 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2802 /* bfd_get_target does not return the correct value until
2803 bfd_check_format succeeds. */
2804 if (output_target == NULL)
2805 output_target = bfd_get_target (ibfd);
2807 obfd = bfd_openw (output_filename, output_target);
2810 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2814 /* This is a no-op on non-Coff targets. */
2815 set_long_section_mode (obfd, ibfd, long_section_names);
2817 if (! copy_object (ibfd, obfd, input_arch))
2820 /* PR 17512: file: 0f15796a.
2821 If the file could not be copied it may not be in a writeable
2822 state. So use bfd_close_all_done to avoid the possibility of
2823 writing uninitialised data into the file. */
2824 if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd)))
2827 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2831 if (!bfd_close (ibfd))
2834 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2840 bfd_error_type obj_error = bfd_get_error ();
2841 bfd_error_type core_error;
2843 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2845 /* This probably can't happen.. */
2846 if (obj_error == bfd_error_file_ambiguously_recognized)
2847 free (obj_matching);
2851 core_error = bfd_get_error ();
2852 /* Report the object error in preference to the core error. */
2853 if (obj_error != core_error)
2854 bfd_set_error (obj_error);
2856 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2858 if (obj_error == bfd_error_file_ambiguously_recognized)
2860 list_matching_formats (obj_matching);
2861 free (obj_matching);
2863 if (core_error == bfd_error_file_ambiguously_recognized)
2865 list_matching_formats (core_matching);
2866 free (core_matching);
2873 /* Add a name to the section renaming list. */
2876 add_section_rename (const char * old_name, const char * new_name,
2879 section_rename * srename;
2881 /* Check for conflicts first. */
2882 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2883 if (strcmp (srename->old_name, old_name) == 0)
2885 /* Silently ignore duplicate definitions. */
2886 if (strcmp (srename->new_name, new_name) == 0
2887 && srename->flags == flags)
2890 fatal (_("Multiple renames of section %s"), old_name);
2893 srename = (section_rename *) xmalloc (sizeof (* srename));
2895 srename->old_name = old_name;
2896 srename->new_name = new_name;
2897 srename->flags = flags;
2898 srename->next = section_rename_list;
2900 section_rename_list = srename;
2903 /* Check the section rename list for a new name of the input section
2904 ISECTION. Return the new name if one is found.
2905 Also set RETURNED_FLAGS to the flags to be used for this section. */
2908 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2909 flagword * returned_flags)
2911 const char * old_name = bfd_section_name (ibfd, isection);
2912 section_rename * srename;
2914 /* Default to using the flags of the input section. */
2915 * returned_flags = bfd_get_section_flags (ibfd, isection);
2917 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2918 if (strcmp (srename->old_name, old_name) == 0)
2920 if (srename->flags != (flagword) -1)
2921 * returned_flags = srename->flags;
2923 return srename->new_name;
2929 /* Once each of the sections is copied, we may still need to do some
2930 finalization work for private section headers. Do that here. */
2933 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2935 /* Allow the BFD backend to copy any private data it understands
2936 from the input section to the output section. */
2937 if (! bfd_copy_private_header_data (ibfd, obfd))
2940 bfd_nonfatal_message (NULL, ibfd, NULL,
2941 _("error in private header data"));
2945 /* All went well. */
2949 /* Create a section in OBFD with the same
2950 name and attributes as ISECTION in IBFD. */
2953 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2955 bfd *obfd = (bfd *) obfdarg;
2956 struct section_list *p;
2964 char *prefix = NULL;
2965 bfd_boolean make_nobits;
2967 if (is_strip_section (ibfd, isection))
2970 /* Get the, possibly new, name of the output section. */
2971 name = find_section_rename (ibfd, isection, & flags);
2973 /* Prefix sections. */
2974 if ((prefix_alloc_sections_string)
2975 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2976 prefix = prefix_alloc_sections_string;
2977 else if (prefix_sections_string)
2978 prefix = prefix_sections_string;
2984 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
2990 make_nobits = FALSE;
2992 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
2993 SECTION_CONTEXT_SET_FLAGS);
2995 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2996 else if (strip_symbols == STRIP_NONDEBUG
2997 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
2998 && !is_nondebug_keep_contents_section (ibfd, isection))
3000 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3001 if (obfd->xvec->flavour == bfd_target_elf_flavour)
3005 /* Twiddle the input section flags so that it seems to
3006 elf.c:copy_private_bfd_data that section flags have not
3007 changed between input and output sections. This hack
3008 prevents wholesale rewriting of the program headers. */
3009 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3013 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
3015 if (osection == NULL)
3017 err = _("failed to create output section");
3022 elf_section_type (osection) = SHT_NOBITS;
3024 size = bfd_section_size (ibfd, isection);
3025 size = bfd_convert_section_size (ibfd, isection, obfd, size);
3027 size = (size + interleave - 1) / interleave * copy_width;
3028 else if (extract_symbol)
3030 if (! bfd_set_section_size (obfd, osection, size))
3032 err = _("failed to set size");
3036 vma = bfd_section_vma (ibfd, isection);
3037 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3038 SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
3041 if (p->context & SECTION_CONTEXT_SET_VMA)
3047 vma += change_section_address;
3049 if (! bfd_set_section_vma (obfd, osection, vma))
3051 err = _("failed to set vma");
3055 lma = isection->lma;
3056 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3057 SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
3060 if (p->context & SECTION_CONTEXT_ALTER_LMA)
3066 lma += change_section_address;
3068 osection->lma = lma;
3070 /* FIXME: This is probably not enough. If we change the LMA we
3071 may have to recompute the header for the file as well. */
3072 if (!bfd_set_section_alignment (obfd,
3074 bfd_section_alignment (ibfd, isection)))
3076 err = _("failed to set alignment");
3080 /* Copy merge entity size. */
3081 osection->entsize = isection->entsize;
3083 /* Copy compress status. */
3084 osection->compress_status = isection->compress_status;
3086 /* This used to be mangle_section; we do here to avoid using
3087 bfd_get_section_by_name since some formats allow multiple
3088 sections with the same name. */
3089 isection->output_section = osection;
3090 isection->output_offset = 0;
3092 if ((isection->flags & SEC_GROUP) != 0)
3094 asymbol *gsym = group_signature (isection);
3098 gsym->flags |= BSF_KEEP;
3099 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
3100 elf_group_id (isection) = gsym;
3104 /* Allow the BFD backend to copy any private data it understands
3105 from the input section to the output section. */
3106 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
3108 err = _("failed to copy private data");
3112 /* All went well. */
3117 bfd_nonfatal_message (NULL, obfd, osection, err);
3120 /* Return TRUE if input section ISECTION should be skipped. */
3123 skip_section (bfd *ibfd, sec_ptr isection)
3129 /* If we have already failed earlier on,
3130 do not keep on generating complaints now. */
3137 if (is_strip_section (ibfd, isection))
3140 if (is_update_section (ibfd, isection))
3143 flags = bfd_get_section_flags (ibfd, isection);
3144 if ((flags & SEC_GROUP) != 0)
3147 osection = isection->output_section;
3148 size = bfd_get_section_size (isection);
3150 if (size == 0 || osection == 0)
3156 /* Copy relocations in input section ISECTION of IBFD to an output
3157 section with the same name in OBFDARG. If stripping then don't
3158 copy any relocation info. */
3161 copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3163 bfd *obfd = (bfd *) obfdarg;
3169 if (skip_section (ibfd, isection))
3172 osection = isection->output_section;
3174 /* Core files and DWO files do not need to be relocated. */
3175 if (bfd_get_format (obfd) == bfd_core || strip_symbols == STRIP_NONDWO)
3179 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
3183 /* Do not complain if the target does not support relocations. */
3184 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3189 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
3197 bfd_set_reloc (obfd, osection, NULL, 0);
3198 osection->flags &= ~SEC_RELOC;
3202 relpp = (arelent **) xmalloc (relsize);
3203 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
3207 bfd_nonfatal_message (NULL, ibfd, isection,
3208 _("relocation count is negative"));
3212 if (strip_symbols == STRIP_ALL)
3214 /* Remove relocations which are not in
3215 keep_strip_specific_list. */
3216 arelent **temp_relpp;
3217 long temp_relcount = 0;
3220 temp_relpp = (arelent **) xmalloc (relsize);
3221 for (i = 0; i < relcount; i++)
3223 /* PR 17512: file: 9e907e0c. */
3224 if (relpp[i]->sym_ptr_ptr)
3225 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
3226 keep_specific_htab))
3227 temp_relpp [temp_relcount++] = relpp [i];
3229 relcount = temp_relcount;
3234 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
3237 osection->flags &= ~SEC_RELOC;
3243 /* Copy the data of input section ISECTION of IBFD
3244 to an output section with the same name in OBFD. */
3247 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3249 bfd *obfd = (bfd *) obfdarg;
3250 struct section_list *p;
3254 if (skip_section (ibfd, isection))
3257 osection = isection->output_section;
3258 /* The output SHF_COMPRESSED section size is different from input if
3259 ELF classes of input and output aren't the same. We can't use
3260 the output section size since --interleave will shrink the output
3261 section. Size will be updated if the section is converted. */
3262 size = bfd_get_section_size (isection);
3264 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
3265 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
3267 bfd_byte *memhunk = NULL;
3269 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)
3270 || !bfd_convert_section_contents (ibfd, isection, obfd,
3274 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
3280 /* We don't handle leftover bytes (too many possible behaviors,
3281 and we don't know what the user wants). The section length
3282 must be a multiple of the number of bytes to swap. */
3283 if ((size % reverse_bytes) == 0)
3288 for (i = 0; i < size; i += reverse_bytes)
3289 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
3291 bfd_byte *m = (bfd_byte *) memhunk;
3294 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
3295 m[(i + reverse_bytes) - (j + 1)] = b;
3299 /* User must pad the section up in order to do this. */
3300 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
3301 bfd_section_name (ibfd, isection), reverse_bytes);
3306 /* Keep only every `copy_byte'th byte in MEMHUNK. */
3307 char *from = (char *) memhunk + copy_byte;
3308 char *to = (char *) memhunk;
3309 char *end = (char *) memhunk + size;
3312 for (; from < end; from += interleave)
3313 for (i = 0; i < copy_width; i++)
3315 if (&from[i] >= end)
3320 size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
3321 osection->lma /= interleave;
3324 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
3327 bfd_nonfatal_message (NULL, obfd, osection, NULL);
3332 else if ((p = find_section_list (bfd_get_section_name (ibfd, isection),
3333 FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL
3334 && (p->flags & SEC_HAS_CONTENTS) != 0)
3336 void *memhunk = xmalloc (size);
3338 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
3339 flag--they can just remove the section entirely and add it
3340 back again. However, we do permit them to turn on the
3341 SEC_HAS_CONTENTS flag, and take it to mean that the section
3342 contents should be zeroed out. */
3344 memset (memhunk, 0, size);
3345 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
3348 bfd_nonfatal_message (NULL, obfd, osection, NULL);
3355 /* Get all the sections. This is used when --gap-fill or --pad-to is
3359 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
3361 asection ***secppp = (asection ***) secppparg;
3363 **secppp = osection;
3367 /* Sort sections by VMA. This is called via qsort, and is used when
3368 --gap-fill or --pad-to is used. We force non loadable or empty
3369 sections to the front, where they are easier to ignore. */
3372 compare_section_lma (const void *arg1, const void *arg2)
3374 const asection *const *sec1 = (const asection * const *) arg1;
3375 const asection *const *sec2 = (const asection * const *) arg2;
3376 flagword flags1, flags2;
3378 /* Sort non loadable sections to the front. */
3379 flags1 = (*sec1)->flags;
3380 flags2 = (*sec2)->flags;
3381 if ((flags1 & SEC_HAS_CONTENTS) == 0
3382 || (flags1 & SEC_LOAD) == 0)
3384 if ((flags2 & SEC_HAS_CONTENTS) != 0
3385 && (flags2 & SEC_LOAD) != 0)
3390 if ((flags2 & SEC_HAS_CONTENTS) == 0
3391 || (flags2 & SEC_LOAD) == 0)
3395 /* Sort sections by LMA. */
3396 if ((*sec1)->lma > (*sec2)->lma)
3398 else if ((*sec1)->lma < (*sec2)->lma)
3401 /* Sort sections with the same LMA by size. */
3402 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
3404 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
3410 /* Mark all the symbols which will be used in output relocations with
3411 the BSF_KEEP flag so that those symbols will not be stripped.
3413 Ignore relocations which will not appear in the output file. */
3416 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
3418 asymbol **symbols = (asymbol **) symbolsarg;
3423 /* Ignore an input section with no corresponding output section. */
3424 if (isection->output_section == NULL)
3427 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
3430 /* Do not complain if the target does not support relocations. */
3431 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3433 bfd_fatal (bfd_get_filename (ibfd));
3439 relpp = (arelent **) xmalloc (relsize);
3440 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
3442 bfd_fatal (bfd_get_filename (ibfd));
3444 /* Examine each symbol used in a relocation. If it's not one of the
3445 special bfd section symbols, then mark it with BSF_KEEP. */
3446 for (i = 0; i < relcount; i++)
3448 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
3449 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
3450 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
3451 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
3458 /* Write out debugging information. */
3461 write_debugging_info (bfd *obfd, void *dhandle,
3462 long *symcountp ATTRIBUTE_UNUSED,
3463 asymbol ***symppp ATTRIBUTE_UNUSED)
3465 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
3466 return write_ieee_debugging_info (obfd, dhandle);
3468 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
3469 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
3471 bfd_byte *syms, *strings;
3472 bfd_size_type symsize, stringsize;
3473 asection *stabsec, *stabstrsec;
3476 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
3481 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
3482 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
3483 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
3485 || stabstrsec == NULL
3486 || ! bfd_set_section_size (obfd, stabsec, symsize)
3487 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
3488 || ! bfd_set_section_alignment (obfd, stabsec, 2)
3489 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
3491 bfd_nonfatal_message (NULL, obfd, NULL,
3492 _("can't create debugging section"));
3496 /* We can get away with setting the section contents now because
3497 the next thing the caller is going to do is copy over the
3498 real sections. We may someday have to split the contents
3499 setting out of this function. */
3500 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
3501 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
3504 bfd_nonfatal_message (NULL, obfd, NULL,
3505 _("can't set debugging section contents"));
3512 bfd_nonfatal_message (NULL, obfd, NULL,
3513 _("don't know how to write debugging information for %s"),
3514 bfd_get_target (obfd));
3518 /* If neither -D nor -U was specified explicitly,
3519 then use the configured default. */
3521 default_deterministic (void)
3523 if (deterministic < 0)
3524 deterministic = DEFAULT_AR_DETERMINISTIC;
3528 strip_main (int argc, char *argv[])
3530 char *input_target = NULL;
3531 char *output_target = NULL;
3532 bfd_boolean show_version = FALSE;
3533 bfd_boolean formats_info = FALSE;
3536 char *output_file = NULL;
3538 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvwDU",
3539 strip_options, (int *) 0)) != EOF)
3544 input_target = optarg;
3547 output_target = optarg;
3550 input_target = output_target = optarg;
3553 find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE);
3554 sections_removed = TRUE;
3557 strip_symbols = STRIP_ALL;
3561 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
3562 strip_symbols = STRIP_DEBUG;
3564 case OPTION_STRIP_DWO:
3565 strip_symbols = STRIP_DWO;
3567 case OPTION_STRIP_UNNEEDED:
3568 strip_symbols = STRIP_UNNEEDED;
3571 add_specific_symbol (optarg, keep_specific_htab);
3574 add_specific_symbol (optarg, strip_specific_htab);
3577 output_file = optarg;
3580 preserve_dates = TRUE;
3583 deterministic = TRUE;
3586 deterministic = FALSE;
3589 discard_locals = LOCALS_ALL;
3592 discard_locals = LOCALS_START_L;
3598 show_version = TRUE;
3600 case OPTION_FORMATS_INFO:
3601 formats_info = TRUE;
3603 case OPTION_ONLY_KEEP_DEBUG:
3604 strip_symbols = STRIP_NONDEBUG;
3606 case OPTION_KEEP_FILE_SYMBOLS:
3607 keep_file_symbols = 1;
3610 /* We've been given a long option. */
3617 strip_usage (stdout, 0);
3619 strip_usage (stderr, 1);
3630 print_version ("strip");
3632 default_deterministic ();
3634 /* Default is to strip all symbols. */
3635 if (strip_symbols == STRIP_UNDEF
3636 && discard_locals == LOCALS_UNDEF
3637 && htab_elements (strip_specific_htab) == 0)
3638 strip_symbols = STRIP_ALL;
3640 if (output_target == NULL)
3641 output_target = input_target;
3645 || (output_file != NULL && (i + 1) < argc))
3646 strip_usage (stderr, 1);
3648 for (; i < argc; i++)
3650 int hold_status = status;
3651 struct stat statbuf;
3654 if (get_file_size (argv[i]) < 1)
3661 /* No need to check the return value of stat().
3662 It has already been checked in get_file_size(). */
3663 stat (argv[i], &statbuf);
3665 if (output_file == NULL
3666 || filename_cmp (argv[i], output_file) == 0)
3667 tmpname = make_tempname (argv[i]);
3669 tmpname = output_file;
3671 if (tmpname == NULL)
3673 bfd_nonfatal_message (argv[i], NULL, NULL,
3674 _("could not create temporary file to hold stripped copy"));
3680 copy_file (argv[i], tmpname, input_target, output_target, NULL);
3684 set_times (tmpname, &statbuf);
3685 if (output_file != tmpname)
3686 status = (smart_rename (tmpname,
3687 output_file ? output_file : argv[i],
3688 preserve_dates) != 0);
3690 status = hold_status;
3693 unlink_if_ordinary (tmpname);
3694 if (output_file != tmpname)
3701 /* Set up PE subsystem. */
3704 set_pe_subsystem (const char *s)
3706 const char *version, *subsystem;
3716 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
3717 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
3718 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
3719 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
3720 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
3721 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
3722 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
3723 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
3724 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
3725 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
3731 /* Check for the presence of a version number. */
3732 version = strchr (s, ':');
3733 if (version == NULL)
3737 int len = version - s;
3741 version = copy + 1 + len;
3742 pe_major_subsystem_version = strtoul (version, ©, 0);
3744 pe_minor_subsystem_version = strtoul (copy + 1, ©, 0);
3746 non_fatal (_("%s: bad version in PE subsystem"), s);
3749 /* Check for numeric subsystem. */
3750 value = (short) strtol (subsystem, ©, 0);
3753 for (i = 0; i < ARRAY_SIZE (v); i++)
3754 if (v[i].value == value)
3756 pe_subsystem = value;
3757 set_def = v[i].set_def;
3763 /* Search for subsystem by name. */
3764 for (i = 0; i < ARRAY_SIZE (v); i++)
3765 if (strcmp (subsystem, v[i].name) == 0)
3767 pe_subsystem = v[i].value;
3768 set_def = v[i].set_def;
3776 fatal (_("unknown PE subsystem: %s"), s);
3781 if (pe_file_alignment == (bfd_vma) -1)
3782 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3783 if (pe_section_alignment == (bfd_vma) -1)
3784 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3788 free ((char *) subsystem);
3791 /* Convert EFI target to PEI target. */
3794 convert_efi_target (char *efi)
3800 if (strcmp (efi + 4, "ia32") == 0)
3802 /* Change ia32 to i386. */
3807 else if (strcmp (efi + 4, "x86_64") == 0)
3809 /* Change x86_64 to x86-64. */
3814 /* Allocate and return a pointer to a struct section_add, initializing the
3815 structure using ARG, a string in the format "sectionname=filename".
3816 The returned structure will have its next pointer set to NEXT. The
3817 OPTION field is the name of the command line option currently being
3818 parsed, and is only used if an error needs to be reported. */
3820 static struct section_add *
3821 init_section_add (const char *arg,
3822 struct section_add *next,
3825 struct section_add *pa;
3828 s = strchr (arg, '=');
3830 fatal (_("bad format for %s"), option);
3832 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
3833 pa->name = xstrndup (arg, s - arg);
3834 pa->filename = s + 1;
3836 pa->contents = NULL;
3842 /* Load the file specified in PA, allocating memory to hold the file
3843 contents, and store a pointer to the allocated memory in the contents
3844 field of PA. The size field of PA is also updated. All errors call
3848 section_add_load_file (struct section_add *pa)
3853 /* We don't use get_file_size so that we can do
3854 --add-section .note.GNU_stack=/dev/null
3855 get_file_size doesn't work on /dev/null. */
3857 f = fopen (pa->filename, FOPEN_RB);
3859 fatal (_("cannot open: %s: %s"),
3860 pa->filename, strerror (errno));
3864 pa->contents = (bfd_byte *) xmalloc (alloc);
3872 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
3875 got = fread (pa->contents + off, 1, alloc - off, f);
3877 fatal (_("%s: fread failed"), pa->filename);
3888 copy_main (int argc, char *argv[])
3890 char *input_filename = NULL;
3891 char *output_filename = NULL;
3893 char *input_target = NULL;
3894 char *output_target = NULL;
3895 bfd_boolean show_version = FALSE;
3896 bfd_boolean change_warn = TRUE;
3897 bfd_boolean formats_info = FALSE;
3899 struct stat statbuf;
3900 const bfd_arch_info_type *input_arch = NULL;
3902 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
3903 copy_options, (int *) 0)) != EOF)
3908 copy_byte = atoi (optarg);
3910 fatal (_("byte number must be non-negative"));
3914 input_arch = bfd_scan_arch (optarg);
3915 if (input_arch == NULL)
3916 fatal (_("architecture %s unknown"), optarg);
3922 interleave = atoi (optarg);
3924 fatal (_("interleave must be positive"));
3930 case OPTION_INTERLEAVE_WIDTH:
3931 copy_width = atoi (optarg);
3933 fatal(_("interleave width must be positive"));
3937 case 's': /* "source" - 'I' is preferred */
3938 input_target = optarg;
3942 case 'd': /* "destination" - 'O' is preferred */
3943 output_target = optarg;
3947 input_target = output_target = optarg;
3951 find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY);
3952 sections_copied = TRUE;
3956 find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE);
3957 sections_removed = TRUE;
3961 strip_symbols = STRIP_ALL;
3965 strip_symbols = STRIP_DEBUG;
3968 case OPTION_STRIP_DWO:
3969 strip_symbols = STRIP_DWO;
3972 case OPTION_STRIP_UNNEEDED:
3973 strip_symbols = STRIP_UNNEEDED;
3976 case OPTION_ONLY_KEEP_DEBUG:
3977 strip_symbols = STRIP_NONDEBUG;
3980 case OPTION_KEEP_FILE_SYMBOLS:
3981 keep_file_symbols = 1;
3984 case OPTION_ADD_GNU_DEBUGLINK:
3985 long_section_names = ENABLE ;
3986 gnu_debuglink_filename = optarg;
3990 add_specific_symbol (optarg, keep_specific_htab);
3994 add_specific_symbol (optarg, strip_specific_htab);
3997 case OPTION_STRIP_UNNEEDED_SYMBOL:
3998 add_specific_symbol (optarg, strip_unneeded_htab);
4002 add_specific_symbol (optarg, localize_specific_htab);
4005 case OPTION_GLOBALIZE_SYMBOL:
4006 add_specific_symbol (optarg, globalize_specific_htab);
4010 add_specific_symbol (optarg, keepglobal_specific_htab);
4014 add_specific_symbol (optarg, weaken_specific_htab);
4018 preserve_dates = TRUE;
4022 deterministic = TRUE;
4026 deterministic = FALSE;
4034 discard_locals = LOCALS_ALL;
4038 discard_locals = LOCALS_START_L;
4046 show_version = TRUE;
4049 case OPTION_FORMATS_INFO:
4050 formats_info = TRUE;
4057 case OPTION_ADD_SECTION:
4058 add_sections = init_section_add (optarg, add_sections,
4060 section_add_load_file (add_sections);
4063 case OPTION_UPDATE_SECTION:
4064 update_sections = init_section_add (optarg, update_sections,
4065 "--update-section");
4066 section_add_load_file (update_sections);
4069 case OPTION_DUMP_SECTION:
4070 dump_sections = init_section_add (optarg, dump_sections,
4074 case OPTION_ADD_SYMBOL:
4077 struct addsym_node *newsym = xmalloc (sizeof *newsym);
4079 newsym->next = NULL;
4080 s = strchr (optarg, '=');
4082 fatal (_("bad format for %s"), "--add-symbol");
4083 t = strchr (s + 1, ':');
4085 newsym->symdef = xstrndup (optarg, s - optarg);
4088 newsym->section = xstrndup (s + 1, t - (s + 1));
4089 newsym->symval = strtol (t + 1, NULL, 0);
4093 newsym->section = NULL;
4094 newsym->symval = strtol (s + 1, NULL, 0);
4098 t = strchr (t + 1, ',');
4100 newsym->flags = parse_symflags (t+1, &newsym->othersym);
4102 newsym->flags = BSF_GLOBAL;
4104 /* Keep 'othersym' symbols at the front of the list. */
4105 if (newsym->othersym)
4107 newsym->next = add_sym_list;
4109 add_sym_tail = &newsym->next;
4110 add_sym_list = newsym;
4114 *add_sym_tail = newsym;
4115 add_sym_tail = &newsym->next;
4121 case OPTION_CHANGE_START:
4122 change_start = parse_vma (optarg, "--change-start");
4125 case OPTION_CHANGE_SECTION_ADDRESS:
4126 case OPTION_CHANGE_SECTION_LMA:
4127 case OPTION_CHANGE_SECTION_VMA:
4129 struct section_list * p;
4130 unsigned int context = 0;
4134 char *option = NULL;
4139 case OPTION_CHANGE_SECTION_ADDRESS:
4140 option = "--change-section-address";
4141 context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
4143 case OPTION_CHANGE_SECTION_LMA:
4144 option = "--change-section-lma";
4145 context = SECTION_CONTEXT_ALTER_LMA;
4147 case OPTION_CHANGE_SECTION_VMA:
4148 option = "--change-section-vma";
4149 context = SECTION_CONTEXT_ALTER_VMA;
4153 s = strchr (optarg, '=');
4156 s = strchr (optarg, '+');
4159 s = strchr (optarg, '-');
4161 fatal (_("bad format for %s"), option);
4166 /* Correct the context. */
4169 case OPTION_CHANGE_SECTION_ADDRESS:
4170 context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
4172 case OPTION_CHANGE_SECTION_LMA:
4173 context = SECTION_CONTEXT_SET_LMA;
4175 case OPTION_CHANGE_SECTION_VMA:
4176 context = SECTION_CONTEXT_SET_VMA;
4182 name = (char *) xmalloc (len + 1);
4183 strncpy (name, optarg, len);
4186 p = find_section_list (name, TRUE, context);
4188 val = parse_vma (s + 1, option);
4194 case OPTION_CHANGE_SECTION_ADDRESS:
4198 case OPTION_CHANGE_SECTION_LMA:
4202 case OPTION_CHANGE_SECTION_VMA:
4209 case OPTION_CHANGE_ADDRESSES:
4210 change_section_address = parse_vma (optarg, "--change-addresses");
4211 change_start = change_section_address;
4214 case OPTION_CHANGE_WARNINGS:
4218 case OPTION_CHANGE_LEADING_CHAR:
4219 change_leading_char = TRUE;
4222 case OPTION_COMPRESS_DEBUG_SECTIONS:
4225 if (strcasecmp (optarg, "none") == 0)
4226 do_debug_sections = decompress;
4227 else if (strcasecmp (optarg, "zlib") == 0)
4228 do_debug_sections = compress_zlib;
4229 else if (strcasecmp (optarg, "zlib-gnu") == 0)
4230 do_debug_sections = compress_gnu_zlib;
4231 else if (strcasecmp (optarg, "zlib-gabi") == 0)
4232 do_debug_sections = compress_gabi_zlib;
4234 fatal (_("unrecognized --compress-debug-sections type `%s'"),
4238 do_debug_sections = compress;
4241 case OPTION_DEBUGGING:
4242 convert_debugging = TRUE;
4245 case OPTION_DECOMPRESS_DEBUG_SECTIONS:
4246 do_debug_sections = decompress;
4249 case OPTION_GAP_FILL:
4251 bfd_vma gap_fill_vma;
4253 gap_fill_vma = parse_vma (optarg, "--gap-fill");
4254 gap_fill = (bfd_byte) gap_fill_vma;
4255 if ((bfd_vma) gap_fill != gap_fill_vma)
4259 sprintf_vma (buff, gap_fill_vma);
4261 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
4264 gap_fill_set = TRUE;
4268 case OPTION_NO_CHANGE_WARNINGS:
4269 change_warn = FALSE;
4273 pad_to = parse_vma (optarg, "--pad-to");
4277 case OPTION_REMOVE_LEADING_CHAR:
4278 remove_leading_char = TRUE;
4281 case OPTION_REDEFINE_SYM:
4283 /* Push this redefinition onto redefine_symbol_list. */
4287 const char *nextarg;
4288 char *source, *target;
4290 s = strchr (optarg, '=');
4292 fatal (_("bad format for %s"), "--redefine-sym");
4295 source = (char *) xmalloc (len + 1);
4296 strncpy (source, optarg, len);
4300 len = strlen (nextarg);
4301 target = (char *) xmalloc (len + 1);
4302 strcpy (target, nextarg);
4304 redefine_list_append ("--redefine-sym", source, target);
4311 case OPTION_REDEFINE_SYMS:
4312 add_redefine_syms_file (optarg);
4315 case OPTION_SET_SECTION_FLAGS:
4317 struct section_list *p;
4322 s = strchr (optarg, '=');
4324 fatal (_("bad format for %s"), "--set-section-flags");
4327 name = (char *) xmalloc (len + 1);
4328 strncpy (name, optarg, len);
4331 p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS);
4333 p->flags = parse_flags (s + 1);
4337 case OPTION_RENAME_SECTION:
4340 const char *eq, *fl;
4345 eq = strchr (optarg, '=');
4347 fatal (_("bad format for %s"), "--rename-section");
4351 fatal (_("bad format for %s"), "--rename-section");
4353 old_name = (char *) xmalloc (len + 1);
4354 strncpy (old_name, optarg, len);
4358 fl = strchr (eq, ',');
4361 flags = parse_flags (fl + 1);
4371 fatal (_("bad format for %s"), "--rename-section");
4373 new_name = (char *) xmalloc (len + 1);
4374 strncpy (new_name, eq, len);
4377 add_section_rename (old_name, new_name, flags);
4381 case OPTION_SET_START:
4382 set_start = parse_vma (optarg, "--set-start");
4383 set_start_set = TRUE;
4386 case OPTION_SREC_LEN:
4387 Chunk = parse_vma (optarg, "--srec-len");
4390 case OPTION_SREC_FORCES3:
4394 case OPTION_STRIP_SYMBOLS:
4395 add_specific_symbols (optarg, strip_specific_htab);
4398 case OPTION_STRIP_UNNEEDED_SYMBOLS:
4399 add_specific_symbols (optarg, strip_unneeded_htab);
4402 case OPTION_KEEP_SYMBOLS:
4403 add_specific_symbols (optarg, keep_specific_htab);
4406 case OPTION_LOCALIZE_HIDDEN:
4407 localize_hidden = TRUE;
4410 case OPTION_LOCALIZE_SYMBOLS:
4411 add_specific_symbols (optarg, localize_specific_htab);
4414 case OPTION_LONG_SECTION_NAMES:
4415 if (!strcmp ("enable", optarg))
4416 long_section_names = ENABLE;
4417 else if (!strcmp ("disable", optarg))
4418 long_section_names = DISABLE;
4419 else if (!strcmp ("keep", optarg))
4420 long_section_names = KEEP;
4422 fatal (_("unknown long section names option '%s'"), optarg);
4425 case OPTION_GLOBALIZE_SYMBOLS:
4426 add_specific_symbols (optarg, globalize_specific_htab);
4429 case OPTION_KEEPGLOBAL_SYMBOLS:
4430 add_specific_symbols (optarg, keepglobal_specific_htab);
4433 case OPTION_WEAKEN_SYMBOLS:
4434 add_specific_symbols (optarg, weaken_specific_htab);
4437 case OPTION_ALT_MACH_CODE:
4438 use_alt_mach_code = strtoul (optarg, NULL, 0);
4439 if (use_alt_mach_code == 0)
4440 fatal (_("unable to parse alternative machine code"));
4443 case OPTION_PREFIX_SYMBOLS:
4444 prefix_symbols_string = optarg;
4447 case OPTION_PREFIX_SECTIONS:
4448 prefix_sections_string = optarg;
4451 case OPTION_PREFIX_ALLOC_SECTIONS:
4452 prefix_alloc_sections_string = optarg;
4455 case OPTION_READONLY_TEXT:
4456 bfd_flags_to_set |= WP_TEXT;
4457 bfd_flags_to_clear &= ~WP_TEXT;
4460 case OPTION_WRITABLE_TEXT:
4461 bfd_flags_to_clear |= WP_TEXT;
4462 bfd_flags_to_set &= ~WP_TEXT;
4466 bfd_flags_to_set |= D_PAGED;
4467 bfd_flags_to_clear &= ~D_PAGED;
4471 bfd_flags_to_clear |= D_PAGED;
4472 bfd_flags_to_set &= ~D_PAGED;
4475 case OPTION_EXTRACT_DWO:
4476 strip_symbols = STRIP_NONDWO;
4479 case OPTION_EXTRACT_SYMBOL:
4480 extract_symbol = TRUE;
4483 case OPTION_REVERSE_BYTES:
4485 int prev = reverse_bytes;
4487 reverse_bytes = atoi (optarg);
4488 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
4489 fatal (_("number of bytes to reverse must be positive and even"));
4491 if (prev && prev != reverse_bytes)
4492 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
4497 case OPTION_FILE_ALIGNMENT:
4498 pe_file_alignment = parse_vma (optarg, "--file-alignment");
4504 pe_heap_reserve = strtoul (optarg, &end, 0);
4506 || (*end != '.' && *end != '\0'))
4507 non_fatal (_("%s: invalid reserve value for --heap"),
4509 else if (*end != '\0')
4511 pe_heap_commit = strtoul (end + 1, &end, 0);
4513 non_fatal (_("%s: invalid commit value for --heap"),
4519 case OPTION_IMAGE_BASE:
4520 pe_image_base = parse_vma (optarg, "--image-base");
4523 case OPTION_SECTION_ALIGNMENT:
4524 pe_section_alignment = parse_vma (optarg,
4525 "--section-alignment");
4528 case OPTION_SUBSYSTEM:
4529 set_pe_subsystem (optarg);
4535 pe_stack_reserve = strtoul (optarg, &end, 0);
4537 || (*end != '.' && *end != '\0'))
4538 non_fatal (_("%s: invalid reserve value for --stack"),
4540 else if (*end != '\0')
4542 pe_stack_commit = strtoul (end + 1, &end, 0);
4544 non_fatal (_("%s: invalid commit value for --stack"),
4551 /* We've been given a long option. */
4556 copy_usage (stdout, 0);
4559 copy_usage (stderr, 1);
4570 print_version ("objcopy");
4572 if (interleave && copy_byte == -1)
4573 fatal (_("interleave start byte must be set with --byte"));
4575 if (copy_byte >= interleave)
4576 fatal (_("byte number must be less than interleave"));
4578 if (copy_width > interleave - copy_byte)
4579 fatal (_("interleave width must be less than or equal to interleave - byte`"));
4581 if (optind == argc || optind + 2 < argc)
4582 copy_usage (stderr, 1);
4584 input_filename = argv[optind];
4585 if (optind + 1 < argc)
4586 output_filename = argv[optind + 1];
4588 default_deterministic ();
4590 /* Default is to strip no symbols. */
4591 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
4592 strip_symbols = STRIP_NONE;
4594 if (output_target == NULL)
4595 output_target = input_target;
4597 /* Convert input EFI target to PEI target. */
4598 if (input_target != NULL
4599 && strncmp (input_target, "efi-", 4) == 0)
4603 efi = xstrdup (output_target + 4);
4604 if (strncmp (efi, "bsdrv-", 6) == 0
4605 || strncmp (efi, "rtdrv-", 6) == 0)
4607 else if (strncmp (efi, "app-", 4) != 0)
4608 fatal (_("unknown input EFI target: %s"), input_target);
4611 convert_efi_target (efi);
4614 /* Convert output EFI target to PEI target. */
4615 if (output_target != NULL
4616 && strncmp (output_target, "efi-", 4) == 0)
4620 efi = xstrdup (output_target + 4);
4621 if (strncmp (efi, "app-", 4) == 0)
4623 if (pe_subsystem == -1)
4624 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
4626 else if (strncmp (efi, "bsdrv-", 6) == 0)
4628 if (pe_subsystem == -1)
4629 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
4632 else if (strncmp (efi, "rtdrv-", 6) == 0)
4634 if (pe_subsystem == -1)
4635 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
4639 fatal (_("unknown output EFI target: %s"), output_target);
4641 if (pe_file_alignment == (bfd_vma) -1)
4642 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
4643 if (pe_section_alignment == (bfd_vma) -1)
4644 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
4646 output_target = efi;
4647 convert_efi_target (efi);
4651 if (stat (input_filename, & statbuf) < 0)
4652 fatal (_("warning: could not locate '%s'. System error message: %s"),
4653 input_filename, strerror (errno));
4655 /* If there is no destination file, or the source and destination files
4656 are the same, then create a temp and rename the result into the input. */
4657 if (output_filename == NULL
4658 || filename_cmp (input_filename, output_filename) == 0)
4659 tmpname = make_tempname (input_filename);
4661 tmpname = output_filename;
4663 if (tmpname == NULL)
4664 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
4665 input_filename, strerror (errno));
4667 copy_file (input_filename, tmpname, input_target, output_target, input_arch);
4671 set_times (tmpname, &statbuf);
4672 if (tmpname != output_filename)
4673 status = (smart_rename (tmpname, input_filename,
4674 preserve_dates) != 0);
4677 unlink_if_ordinary (tmpname);
4681 struct section_list *p;
4683 for (p = change_sections; p != NULL; p = p->next)
4687 if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
4691 sprintf_vma (buff, p->vma_val);
4693 /* xgettext:c-format */
4694 non_fatal (_("%s %s%c0x%s never used"),
4695 "--change-section-vma",
4697 p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
4701 if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
4705 sprintf_vma (buff, p->lma_val);
4707 /* xgettext:c-format */
4708 non_fatal (_("%s %s%c0x%s never used"),
4709 "--change-section-lma",
4711 p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
4722 main (int argc, char *argv[])
4724 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4725 setlocale (LC_MESSAGES, "");
4727 #if defined (HAVE_SETLOCALE)
4728 setlocale (LC_CTYPE, "");
4730 bindtextdomain (PACKAGE, LOCALEDIR);
4731 textdomain (PACKAGE);
4733 program_name = argv[0];
4734 xmalloc_set_program_name (program_name);
4736 START_PROGRESS (program_name, 0);
4738 expandargv (&argc, &argv);
4740 strip_symbols = STRIP_UNDEF;
4741 discard_locals = LOCALS_UNDEF;
4744 set_default_bfd_target ();
4748 int i = strlen (program_name);
4749 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4750 /* Drop the .exe suffix, if any. */
4751 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
4754 program_name[i] = '\0';
4757 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
4760 create_symbol_htabs ();
4763 bfd_set_error_program_name (argv[0]);
4766 strip_main (argc, argv);
4768 copy_main (argc, argv);
4770 END_PROGRESS (program_name);