1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2016 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 generate ELF common symbols with the STT_COMMON type. */
224 static enum bfd_link_discard do_elf_stt_common = unchanged;
226 /* Whether to change the leading character in symbol names. */
227 static bfd_boolean change_leading_char = FALSE;
229 /* Whether to remove the leading character from global symbol names. */
230 static bfd_boolean remove_leading_char = FALSE;
232 /* Whether to permit wildcard in symbol comparison. */
233 static bfd_boolean wildcard = FALSE;
235 /* True if --localize-hidden is in effect. */
236 static bfd_boolean localize_hidden = FALSE;
238 /* List of symbols to strip, keep, localize, keep-global, weaken,
240 static htab_t strip_specific_htab = NULL;
241 static htab_t strip_unneeded_htab = NULL;
242 static htab_t keep_specific_htab = NULL;
243 static htab_t localize_specific_htab = NULL;
244 static htab_t globalize_specific_htab = NULL;
245 static htab_t keepglobal_specific_htab = NULL;
246 static htab_t weaken_specific_htab = NULL;
247 static struct redefine_node *redefine_sym_list = NULL;
248 static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
249 static int add_symbols = 0;
251 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
252 static bfd_boolean weaken = FALSE;
254 /* If this is TRUE, we retain BSF_FILE symbols. */
255 static bfd_boolean keep_file_symbols = FALSE;
257 /* Prefix symbols/sections. */
258 static char *prefix_symbols_string = 0;
259 static char *prefix_sections_string = 0;
260 static char *prefix_alloc_sections_string = 0;
262 /* True if --extract-symbol was passed on the command line. */
263 static bfd_boolean extract_symbol = FALSE;
265 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
266 of <reverse_bytes> bytes within each output section. */
267 static int reverse_bytes = 0;
269 /* For Coff objects, we may want to allow or disallow long section names,
270 or preserve them where found in the inputs. Debug info relies on them. */
271 enum long_section_name_handling
278 /* The default long section handling mode is to preserve them.
279 This is also the only behaviour for 'strip'. */
280 static enum long_section_name_handling long_section_names = KEEP;
282 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
283 enum command_line_switch
285 OPTION_ADD_SECTION=150,
286 OPTION_ADD_GNU_DEBUGLINK,
288 OPTION_ALT_MACH_CODE,
289 OPTION_CHANGE_ADDRESSES,
290 OPTION_CHANGE_LEADING_CHAR,
291 OPTION_CHANGE_SECTION_ADDRESS,
292 OPTION_CHANGE_SECTION_LMA,
293 OPTION_CHANGE_SECTION_VMA,
295 OPTION_CHANGE_WARNINGS,
296 OPTION_COMPRESS_DEBUG_SECTIONS,
298 OPTION_DECOMPRESS_DEBUG_SECTIONS,
300 OPTION_ELF_STT_COMMON,
302 OPTION_EXTRACT_SYMBOL,
303 OPTION_FILE_ALIGNMENT,
306 OPTION_GLOBALIZE_SYMBOL,
307 OPTION_GLOBALIZE_SYMBOLS,
311 OPTION_INTERLEAVE_WIDTH,
312 OPTION_KEEPGLOBAL_SYMBOLS,
313 OPTION_KEEP_FILE_SYMBOLS,
315 OPTION_LOCALIZE_HIDDEN,
316 OPTION_LOCALIZE_SYMBOLS,
317 OPTION_LONG_SECTION_NAMES,
318 OPTION_NO_CHANGE_WARNINGS,
319 OPTION_ONLY_KEEP_DEBUG,
321 OPTION_PREFIX_ALLOC_SECTIONS,
322 OPTION_PREFIX_SECTIONS,
323 OPTION_PREFIX_SYMBOLS,
325 OPTION_READONLY_TEXT,
327 OPTION_REDEFINE_SYMS,
328 OPTION_REMOVE_LEADING_CHAR,
329 OPTION_RENAME_SECTION,
330 OPTION_REVERSE_BYTES,
331 OPTION_SECTION_ALIGNMENT,
332 OPTION_SET_SECTION_FLAGS,
338 OPTION_STRIP_SYMBOLS,
339 OPTION_STRIP_UNNEEDED,
340 OPTION_STRIP_UNNEEDED_SYMBOL,
341 OPTION_STRIP_UNNEEDED_SYMBOLS,
343 OPTION_UPDATE_SECTION,
345 OPTION_WEAKEN_SYMBOLS,
349 /* Options to handle if running as "strip". */
351 static struct option strip_options[] =
353 {"disable-deterministic-archives", no_argument, 0, 'U'},
354 {"discard-all", no_argument, 0, 'x'},
355 {"discard-locals", no_argument, 0, 'X'},
356 {"enable-deterministic-archives", no_argument, 0, 'D'},
357 {"format", required_argument, 0, 'F'}, /* Obsolete */
358 {"help", no_argument, 0, 'h'},
359 {"info", no_argument, 0, OPTION_FORMATS_INFO},
360 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
361 {"input-target", required_argument, 0, 'I'},
362 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
363 {"keep-symbol", required_argument, 0, 'K'},
364 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
365 {"output-file", required_argument, 0, 'o'},
366 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
367 {"output-target", required_argument, 0, 'O'},
368 {"preserve-dates", no_argument, 0, 'p'},
369 {"remove-section", required_argument, 0, 'R'},
370 {"strip-all", no_argument, 0, 's'},
371 {"strip-debug", no_argument, 0, 'S'},
372 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
373 {"strip-symbol", required_argument, 0, 'N'},
374 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
375 {"target", required_argument, 0, 'F'},
376 {"verbose", no_argument, 0, 'v'},
377 {"version", no_argument, 0, 'V'},
378 {"wildcard", no_argument, 0, 'w'},
379 {0, no_argument, 0, 0}
382 /* Options to handle if running as "objcopy". */
384 static struct option copy_options[] =
386 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
387 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
388 {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
389 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
390 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
391 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
392 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
393 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
394 {"binary-architecture", required_argument, 0, 'B'},
395 {"byte", required_argument, 0, 'b'},
396 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
397 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
398 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
399 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
400 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
401 {"change-start", required_argument, 0, OPTION_CHANGE_START},
402 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
403 {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
404 {"debugging", no_argument, 0, OPTION_DEBUGGING},
405 {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
406 {"disable-deterministic-archives", no_argument, 0, 'U'},
407 {"discard-all", no_argument, 0, 'x'},
408 {"discard-locals", no_argument, 0, 'X'},
409 {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
410 {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
411 {"enable-deterministic-archives", no_argument, 0, 'D'},
412 {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
413 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
414 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
415 {"format", required_argument, 0, 'F'}, /* Obsolete */
416 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
417 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
418 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
419 {"heap", required_argument, 0, OPTION_HEAP},
420 {"help", no_argument, 0, 'h'},
421 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
422 {"impure", no_argument, 0, OPTION_IMPURE},
423 {"info", no_argument, 0, OPTION_FORMATS_INFO},
424 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
425 {"input-target", required_argument, 0, 'I'},
426 {"interleave", optional_argument, 0, 'i'},
427 {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
428 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
429 {"keep-global-symbol", required_argument, 0, 'G'},
430 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
431 {"keep-symbol", required_argument, 0, 'K'},
432 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
433 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
434 {"localize-symbol", required_argument, 0, 'L'},
435 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
436 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
437 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
438 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
439 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
440 {"only-section", required_argument, 0, 'j'},
441 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
442 {"output-target", required_argument, 0, 'O'},
443 {"pad-to", required_argument, 0, OPTION_PAD_TO},
444 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
445 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
446 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
447 {"preserve-dates", no_argument, 0, 'p'},
448 {"pure", no_argument, 0, OPTION_PURE},
449 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
450 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
451 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
452 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
453 {"remove-section", required_argument, 0, 'R'},
454 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
455 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
456 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
457 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
458 {"set-start", required_argument, 0, OPTION_SET_START},
459 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
460 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
461 {"stack", required_argument, 0, OPTION_STACK},
462 {"strip-all", no_argument, 0, 'S'},
463 {"strip-debug", no_argument, 0, 'g'},
464 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
465 {"strip-symbol", required_argument, 0, 'N'},
466 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
467 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
468 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
469 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
470 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
471 {"target", required_argument, 0, 'F'},
472 {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
473 {"verbose", no_argument, 0, 'v'},
474 {"version", no_argument, 0, 'V'},
475 {"weaken", no_argument, 0, OPTION_WEAKEN},
476 {"weaken-symbol", required_argument, 0, 'W'},
477 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
478 {"wildcard", no_argument, 0, 'w'},
479 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
480 {0, no_argument, 0, 0}
484 extern char *program_name;
486 /* This flag distinguishes between strip and objcopy:
487 1 means this is 'strip'; 0 means this is 'objcopy'.
488 -1 means if we should use argv[0] to decide. */
491 /* The maximum length of an S record. This variable is declared in srec.c
492 and can be modified by the --srec-len parameter. */
493 extern unsigned int Chunk;
495 /* Restrict the generation of Srecords to type S3 only.
496 This variable is declare in bfd/srec.c and can be toggled
497 on by the --srec-forceS3 command line switch. */
498 extern bfd_boolean S3Forced;
500 /* Forward declarations. */
501 static void setup_section (bfd *, asection *, void *);
502 static void setup_bfd_headers (bfd *, bfd *);
503 static void copy_relocations_in_section (bfd *, asection *, void *);
504 static void copy_section (bfd *, asection *, void *);
505 static void get_sections (bfd *, asection *, void *);
506 static int compare_section_lma (const void *, const void *);
507 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
508 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
509 static const char *lookup_sym_redefinition (const char *);
512 copy_usage (FILE *stream, int exit_status)
514 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
515 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
516 fprintf (stream, _(" The options are:\n"));
517 fprintf (stream, _("\
518 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
519 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
520 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
521 -F --target <bfdname> Set both input and output format to <bfdname>\n\
522 --debugging Convert debugging information, if possible\n\
523 -p --preserve-dates Copy modified/access timestamps to the output\n"));
524 if (DEFAULT_AR_DETERMINISTIC)
525 fprintf (stream, _("\
526 -D --enable-deterministic-archives\n\
527 Produce deterministic output when stripping archives (default)\n\
528 -U --disable-deterministic-archives\n\
529 Disable -D behavior\n"));
531 fprintf (stream, _("\
532 -D --enable-deterministic-archives\n\
533 Produce deterministic output when stripping archives\n\
534 -U --disable-deterministic-archives\n\
535 Disable -D behavior (default)\n"));
536 fprintf (stream, _("\
537 -j --only-section <name> Only copy section <name> into the output\n\
538 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
539 -R --remove-section <name> Remove section <name> from the output\n\
540 -S --strip-all Remove all symbol and relocation information\n\
541 -g --strip-debug Remove all debugging symbols & sections\n\
542 --strip-dwo Remove all DWO sections\n\
543 --strip-unneeded Remove all symbols not needed by relocations\n\
544 -N --strip-symbol <name> Do not copy symbol <name>\n\
545 --strip-unneeded-symbol <name>\n\
546 Do not copy symbol <name> unless needed by\n\
548 --only-keep-debug Strip everything but the debug information\n\
549 --extract-dwo Copy only DWO sections\n\
550 --extract-symbol Remove section contents but keep symbols\n\
551 -K --keep-symbol <name> Do not strip symbol <name>\n\
552 --keep-file-symbols Do not strip file symbol(s)\n\
553 --localize-hidden Turn all ELF hidden symbols into locals\n\
554 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
555 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
556 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
557 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
558 --weaken Force all global symbols to be marked as weak\n\
559 -w --wildcard Permit wildcard in symbol comparison\n\
560 -x --discard-all Remove all non-global symbols\n\
561 -X --discard-locals Remove any compiler-generated symbols\n\
562 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
563 --interleave-width <number> Set N for --interleave\n\
564 -b --byte <num> Select byte <num> in every interleaved block\n\
565 --gap-fill <val> Fill gaps between sections with <val>\n\
566 --pad-to <addr> Pad the last section up to address <addr>\n\
567 --set-start <addr> Set the start address to <addr>\n\
568 {--change-start|--adjust-start} <incr>\n\
569 Add <incr> to the start address\n\
570 {--change-addresses|--adjust-vma} <incr>\n\
571 Add <incr> to LMA, VMA and start addresses\n\
572 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
573 Change LMA and VMA of section <name> by <val>\n\
574 --change-section-lma <name>{=|+|-}<val>\n\
575 Change the LMA of section <name> by <val>\n\
576 --change-section-vma <name>{=|+|-}<val>\n\
577 Change the VMA of section <name> by <val>\n\
578 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
579 Warn if a named section does not exist\n\
580 --set-section-flags <name>=<flags>\n\
581 Set section <name>'s properties to <flags>\n\
582 --add-section <name>=<file> Add section <name> found in <file> to output\n\
583 --update-section <name>=<file>\n\
584 Update contents of section <name> with\n\
585 contents found in <file>\n\
586 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
587 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
588 --long-section-names {enable|disable|keep}\n\
589 Handle long section names in Coff objects.\n\
590 --change-leading-char Force output format's leading character style\n\
591 --remove-leading-char Remove leading character from global symbols\n\
592 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
593 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
594 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
596 --srec-len <number> Restrict the length of generated Srecords\n\
597 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
598 --strip-symbols <file> -N for all symbols listed in <file>\n\
599 --strip-unneeded-symbols <file>\n\
600 --strip-unneeded-symbol for all symbols listed\n\
602 --keep-symbols <file> -K for all symbols listed in <file>\n\
603 --localize-symbols <file> -L for all symbols listed in <file>\n\
604 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
605 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
606 --weaken-symbols <file> -W for all symbols listed in <file>\n\
607 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
608 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
609 --writable-text Mark the output text as writable\n\
610 --readonly-text Make the output text write protected\n\
611 --pure Mark the output file as demand paged\n\
612 --impure Mark the output file as impure\n\
613 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
614 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
615 --prefix-alloc-sections <prefix>\n\
616 Add <prefix> to start of every allocatable\n\
618 --file-alignment <num> Set PE file alignment to <num>\n\
619 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
621 --image-base <address> Set PE image base to <address>\n\
622 --section-alignment <num> Set PE section alignment to <num>\n\
623 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
625 --subsystem <name>[:<version>]\n\
626 Set PE subsystem to <name> [& <version>]\n\
627 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
628 Compress DWARF debug sections using zlib\n\
629 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
630 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
632 -v --verbose List all object files modified\n\
633 @<file> Read options from <file>\n\
634 -V --version Display this program's version number\n\
635 -h --help Display this output\n\
636 --info List object formats & architectures supported\n\
638 list_supported_targets (program_name, stream);
639 if (REPORT_BUGS_TO[0] && exit_status == 0)
640 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
645 strip_usage (FILE *stream, int exit_status)
647 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
648 fprintf (stream, _(" Removes symbols and sections from files\n"));
649 fprintf (stream, _(" The options are:\n"));
650 fprintf (stream, _("\
651 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
652 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
653 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
654 -p --preserve-dates Copy modified/access timestamps to the output\n\
656 if (DEFAULT_AR_DETERMINISTIC)
657 fprintf (stream, _("\
658 -D --enable-deterministic-archives\n\
659 Produce deterministic output when stripping archives (default)\n\
660 -U --disable-deterministic-archives\n\
661 Disable -D behavior\n"));
663 fprintf (stream, _("\
664 -D --enable-deterministic-archives\n\
665 Produce deterministic output when stripping archives\n\
666 -U --disable-deterministic-archives\n\
667 Disable -D behavior (default)\n"));
668 fprintf (stream, _("\
669 -R --remove-section=<name> Also remove section <name> from the output\n\
670 -s --strip-all Remove all symbol and relocation information\n\
671 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
672 --strip-dwo Remove all DWO sections\n\
673 --strip-unneeded Remove all symbols not needed by relocations\n\
674 --only-keep-debug Strip everything but the debug information\n\
675 -N --strip-symbol=<name> Do not copy symbol <name>\n\
676 -K --keep-symbol=<name> Do not strip symbol <name>\n\
677 --keep-file-symbols Do not strip file symbol(s)\n\
678 -w --wildcard Permit wildcard in symbol comparison\n\
679 -x --discard-all Remove all non-global symbols\n\
680 -X --discard-locals Remove any compiler-generated symbols\n\
681 -v --verbose List all object files modified\n\
682 -V --version Display this program's version number\n\
683 -h --help Display this output\n\
684 --info List object formats & architectures supported\n\
685 -o <file> Place stripped output into <file>\n\
688 list_supported_targets (program_name, stream);
689 if (REPORT_BUGS_TO[0] && exit_status == 0)
690 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
694 /* Parse section flags into a flagword, with a fatal error if the
695 string can't be parsed. */
698 parse_flags (const char *s)
708 snext = strchr (s, ',');
718 #define PARSE_FLAG(fname,fval) \
719 else if (strncasecmp (fname, s, len) == 0) ret |= fval
720 PARSE_FLAG ("alloc", SEC_ALLOC);
721 PARSE_FLAG ("load", SEC_LOAD);
722 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
723 PARSE_FLAG ("readonly", SEC_READONLY);
724 PARSE_FLAG ("debug", SEC_DEBUGGING);
725 PARSE_FLAG ("code", SEC_CODE);
726 PARSE_FLAG ("data", SEC_DATA);
727 PARSE_FLAG ("rom", SEC_ROM);
728 PARSE_FLAG ("share", SEC_COFF_SHARED);
729 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
730 PARSE_FLAG ("merge", SEC_MERGE);
731 PARSE_FLAG ("strings", SEC_STRINGS);
737 copy = (char *) xmalloc (len + 1);
738 strncpy (copy, s, len);
740 non_fatal (_("unrecognized section flag `%s'"), copy);
741 fatal (_("supported flags: %s"),
742 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
752 /* Parse symbol flags into a flagword, with a fatal error if the
753 string can't be parsed. */
756 parse_symflags (const char *s, char **other)
766 snext = strchr (s, ',');
775 #define PARSE_FLAG(fname,fval) \
776 else if (len == (int) sizeof fname - 1 && strncasecmp (fname, s, len) == 0) \
779 #define PARSE_OTHER(fname,fval) \
780 else if (len >= (int) sizeof fname && strncasecmp (fname, s, sizeof fname - 1) == 0) \
781 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
784 PARSE_FLAG ("local", BSF_LOCAL);
785 PARSE_FLAG ("global", BSF_GLOBAL);
786 PARSE_FLAG ("export", BSF_EXPORT);
787 PARSE_FLAG ("debug", BSF_DEBUGGING);
788 PARSE_FLAG ("function", BSF_FUNCTION);
789 PARSE_FLAG ("weak", BSF_WEAK);
790 PARSE_FLAG ("section", BSF_SECTION_SYM);
791 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
792 PARSE_FLAG ("warning", BSF_WARNING);
793 PARSE_FLAG ("indirect", BSF_INDIRECT);
794 PARSE_FLAG ("file", BSF_FILE);
795 PARSE_FLAG ("object", BSF_OBJECT);
796 PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
797 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
798 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
799 PARSE_OTHER ("before=", *other);
807 copy = (char *) xmalloc (len + 1);
808 strncpy (copy, s, len);
810 non_fatal (_("unrecognized symbol flag `%s'"), copy);
811 fatal (_("supported flags: %s"),
812 "local, global, export, debug, function, weak, section, "
813 "constructor, warning, indirect, file, object, synthetic, "
814 "indirect-function, unique-object, before=<othersym>");
824 /* Find and optionally add an entry in the change_sections list.
826 We need to be careful in how we match section names because of the support
827 for wildcard characters. For example suppose that the user has invoked
830 --set-section-flags .debug_*=debug
831 --set-section-flags .debug_str=readonly,debug
832 --change-section-address .debug_*ranges=0x1000
834 With the idea that all debug sections will receive the DEBUG flag, the
835 .debug_str section will also receive the READONLY flag and the
836 .debug_ranges and .debug_aranges sections will have their address set to
837 0x1000. (This may not make much sense, but it is just an example).
839 When adding the section name patterns to the section list we need to make
840 sure that previous entries do not match with the new entry, unless the
841 match is exact. (In which case we assume that the user is overriding
842 the previous entry with the new context).
844 When matching real section names to the section list we make use of the
845 wildcard characters, but we must do so in context. Eg if we are setting
846 section addresses then we match for .debug_ranges but not for .debug_info.
848 Finally, if ADD is false and we do find a match, we mark the section list
851 static struct section_list *
852 find_section_list (const char *name, bfd_boolean add, unsigned int context)
854 struct section_list *p;
856 /* assert ((context & ((1 << 7) - 1)) != 0); */
858 for (p = change_sections; p != NULL; p = p->next)
862 if (strcmp (p->pattern, name) == 0)
864 /* Check for context conflicts. */
865 if (((p->context & SECTION_CONTEXT_REMOVE)
866 && (context & SECTION_CONTEXT_COPY))
867 || ((context & SECTION_CONTEXT_REMOVE)
868 && (p->context & SECTION_CONTEXT_COPY)))
869 fatal (_("error: %s both copied and removed"), name);
871 if (((p->context & SECTION_CONTEXT_SET_VMA)
872 && (context & SECTION_CONTEXT_ALTER_VMA))
873 || ((context & SECTION_CONTEXT_SET_VMA)
874 && (context & SECTION_CONTEXT_ALTER_VMA)))
875 fatal (_("error: %s both sets and alters VMA"), name);
877 if (((p->context & SECTION_CONTEXT_SET_LMA)
878 && (context & SECTION_CONTEXT_ALTER_LMA))
879 || ((context & SECTION_CONTEXT_SET_LMA)
880 && (context & SECTION_CONTEXT_ALTER_LMA)))
881 fatal (_("error: %s both sets and alters LMA"), name);
883 /* Extend the context. */
884 p->context |= context;
888 /* If we are not adding a new name/pattern then
889 only check for a match if the context applies. */
890 else if ((p->context & context)
891 /* We could check for the presence of wildchar characters
892 first and choose between calling strcmp and fnmatch,
893 but is that really worth it ? */
894 && fnmatch (p->pattern, name, 0) == 0)
904 p = (struct section_list *) xmalloc (sizeof (struct section_list));
907 p->context = context;
911 p->next = change_sections;
917 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
920 eq_string (const void *s1, const void *s2)
922 return strcmp ((const char *) s1, (const char *) s2) == 0;
926 create_symbol_htab (void)
928 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
932 create_symbol_htabs (void)
934 strip_specific_htab = create_symbol_htab ();
935 strip_unneeded_htab = create_symbol_htab ();
936 keep_specific_htab = create_symbol_htab ();
937 localize_specific_htab = create_symbol_htab ();
938 globalize_specific_htab = create_symbol_htab ();
939 keepglobal_specific_htab = create_symbol_htab ();
940 weaken_specific_htab = create_symbol_htab ();
943 /* Add a symbol to strip_specific_list. */
946 add_specific_symbol (const char *name, htab_t htab)
948 *htab_find_slot (htab, name, INSERT) = (char *) name;
951 /* Add symbols listed in `filename' to strip_specific_list. */
953 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
954 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
957 add_specific_symbols (const char *filename, htab_t htab)
963 unsigned int line_count;
965 size = get_file_size (filename);
972 buffer = (char *) xmalloc (size + 2);
973 f = fopen (filename, FOPEN_RT);
975 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
977 if (fread (buffer, 1, size, f) == 0 || ferror (f))
978 fatal (_("%s: fread failed"), filename);
981 buffer [size] = '\n';
982 buffer [size + 1] = '\0';
986 for (line = buffer; * line != '\0'; line ++)
991 int finished = FALSE;
993 for (eol = line;; eol ++)
999 /* Cope with \n\r. */
1007 /* Cope with \r\n. */
1018 /* Line comment, Terminate the line here, in case a
1019 name is present and then allow the rest of the
1020 loop to find the real end of the line. */
1032 /* A name may now exist somewhere between 'line' and 'eol'.
1033 Strip off leading whitespace and trailing whitespace,
1034 then add it to the list. */
1035 for (name = line; IS_WHITESPACE (* name); name ++)
1037 for (name_end = name;
1038 (! IS_WHITESPACE (* name_end))
1039 && (! IS_LINE_TERMINATOR (* name_end));
1043 if (! IS_LINE_TERMINATOR (* name_end))
1047 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
1050 if (! IS_LINE_TERMINATOR (* extra))
1051 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1052 filename, line_count);
1057 if (name_end > name)
1058 add_specific_symbol (name, htab);
1060 /* Advance line pointer to end of line. The 'eol ++' in the for
1061 loop above will then advance us to the start of the next line. */
1067 /* See whether a symbol should be stripped or kept
1068 based on strip_specific_list and keep_symbols. */
1071 is_specified_symbol_predicate (void **slot, void *data)
1073 struct is_specified_symbol_predicate_data *d =
1074 (struct is_specified_symbol_predicate_data *) data;
1075 const char *slot_name = (char *) *slot;
1077 if (*slot_name != '!')
1079 if (! fnmatch (slot_name, d->name, 0))
1082 /* Continue traversal, there might be a non-match rule. */
1088 if (! fnmatch (slot_name + 1, d->name, 0))
1091 /* Stop traversal. */
1096 /* Continue traversal. */
1101 is_specified_symbol (const char *name, htab_t htab)
1105 struct is_specified_symbol_predicate_data data;
1110 htab_traverse (htab, is_specified_symbol_predicate, &data);
1115 return htab_find (htab, name) != NULL;
1118 /* Return a pointer to the symbol used as a signature for GROUP. */
1121 group_signature (asection *group)
1123 bfd *abfd = group->owner;
1124 Elf_Internal_Shdr *ghdr;
1126 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1129 ghdr = &elf_section_data (group)->this_hdr;
1130 if (ghdr->sh_link < elf_numsections (abfd))
1132 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1133 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
1135 if (symhdr->sh_type == SHT_SYMTAB
1136 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
1137 return isympp[ghdr->sh_info - 1];
1142 /* Return TRUE if the section is a DWO section. */
1145 is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1147 const char *name = bfd_get_section_name (abfd, sec);
1148 int len = strlen (name);
1150 return strncmp (name + len - 4, ".dwo", 4) == 0;
1153 /* Return TRUE if section SEC is in the update list. */
1156 is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1158 if (update_sections != NULL)
1160 struct section_add *pupdate;
1162 for (pupdate = update_sections;
1164 pupdate = pupdate->next)
1166 if (strcmp (sec->name, pupdate->name) == 0)
1174 /* See if a non-group section is being removed. */
1177 is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1179 if (sections_removed || sections_copied)
1181 struct section_list *p;
1182 struct section_list *q;
1184 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1185 SECTION_CONTEXT_REMOVE);
1186 q = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1187 SECTION_CONTEXT_COPY);
1190 fatal (_("error: section %s matches both remove and copy options"),
1191 bfd_get_section_name (abfd, sec));
1192 if (p && is_update_section (abfd, sec))
1193 fatal (_("error: section %s matches both update and remove options"),
1194 bfd_get_section_name (abfd, sec));
1198 if (sections_copied && q == NULL)
1202 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
1204 if (strip_symbols == STRIP_DEBUG
1205 || strip_symbols == STRIP_UNNEEDED
1206 || strip_symbols == STRIP_ALL
1207 || discard_locals == LOCALS_ALL
1208 || convert_debugging)
1210 /* By default we don't want to strip .reloc section.
1211 This section has for pe-coff special meaning. See
1212 pe-dll.c file in ld, and peXXigen.c in bfd for details. */
1213 if (strcmp (bfd_get_section_name (abfd, sec), ".reloc") != 0)
1217 if (strip_symbols == STRIP_DWO)
1218 return is_dwo_section (abfd, sec);
1220 if (strip_symbols == STRIP_NONDEBUG)
1224 if (strip_symbols == STRIP_NONDWO)
1225 return !is_dwo_section (abfd, sec);
1230 /* See if a section is being removed. */
1233 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1235 if (is_strip_section_1 (abfd, sec))
1238 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
1242 asection *elt, *first;
1245 If we are going to strip the group signature symbol, then
1246 strip the group section too. */
1247 gsym = group_signature (sec);
1252 if ((strip_symbols == STRIP_ALL
1253 && !is_specified_symbol (gname, keep_specific_htab))
1254 || is_specified_symbol (gname, strip_specific_htab))
1257 /* Remove the group section if all members are removed. */
1258 first = elt = elf_next_in_group (sec);
1261 if (!is_strip_section_1 (abfd, elt))
1263 elt = elf_next_in_group (elt);
1275 is_nondebug_keep_contents_section (bfd *ibfd, asection *isection)
1277 /* Always keep ELF note sections. */
1278 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
1279 return (elf_section_type (isection) == SHT_NOTE);
1281 /* Always keep the .buildid section for PE/COFF.
1283 Strictly, this should be written "always keep the section storing the debug
1284 directory", but that may be the .text section for objects produced by some
1285 tools, which it is not sensible to keep. */
1286 if (ibfd->xvec->flavour == bfd_target_coff_flavour)
1287 return (strcmp (bfd_get_section_name (ibfd, isection), ".buildid") == 0);
1292 /* Return true if SYM is a hidden symbol. */
1295 is_hidden_symbol (asymbol *sym)
1297 elf_symbol_type *elf_sym;
1299 elf_sym = elf_symbol_from (sym->the_bfd, sym);
1300 if (elf_sym != NULL)
1301 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
1311 need_sym_before (struct addsym_node **node, const char *sym)
1314 struct addsym_node *ptr = add_sym_list;
1316 /* 'othersym' symbols are at the front of the list. */
1317 for (count = 0; count < add_symbols; count++)
1321 else if (strcmp (ptr->othersym, sym) == 0)
1323 free (ptr->othersym);
1324 ptr->othersym = ""; /* Empty name is hopefully never a valid symbol name. */
1334 create_new_symbol (struct addsym_node *ptr, bfd *obfd)
1336 asymbol *sym = bfd_make_empty_symbol(obfd);
1338 bfd_asymbol_name(sym) = ptr->symdef;
1339 sym->value = ptr->symval;
1340 sym->flags = ptr->flags;
1343 asection *sec = bfd_get_section_by_name (obfd, ptr->section);
1345 fatal (_("Section %s not found"), ptr->section);
1349 sym->section = bfd_abs_section_ptr;
1353 /* Choose which symbol entries to copy; put the result in OSYMS.
1354 We don't copy in place, because that confuses the relocs.
1355 Return the number of symbols to print. */
1358 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
1359 asymbol **isyms, long symcount)
1361 asymbol **from = isyms, **to = osyms;
1362 long src_count = 0, dst_count = 0;
1363 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1365 for (; src_count < symcount; src_count++)
1367 asymbol *sym = from[src_count];
1368 flagword flags = sym->flags;
1369 char *name = (char *) bfd_asymbol_name (sym);
1371 bfd_boolean used_in_reloc = FALSE;
1372 bfd_boolean undefined;
1373 bfd_boolean rem_leading_char;
1374 bfd_boolean add_leading_char;
1376 undefined = bfd_is_und_section (bfd_get_section (sym));
1380 struct addsym_node *ptr;
1382 if (need_sym_before (&ptr, name))
1383 to[dst_count++] = create_new_symbol (ptr, obfd);
1386 if (redefine_sym_list)
1388 char *old_name, *new_name;
1390 old_name = (char *) bfd_asymbol_name (sym);
1391 new_name = (char *) lookup_sym_redefinition (old_name);
1392 bfd_asymbol_name (sym) = new_name;
1396 /* Check if we will remove the current leading character. */
1398 (name[0] == bfd_get_symbol_leading_char (abfd))
1399 && (change_leading_char
1400 || (remove_leading_char
1401 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1403 || bfd_is_com_section (bfd_get_section (sym)))));
1405 /* Check if we will add a new leading character. */
1408 && (bfd_get_symbol_leading_char (obfd) != '\0')
1409 && (bfd_get_symbol_leading_char (abfd) == '\0'
1410 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1412 /* Short circuit for change_leading_char if we can do it in-place. */
1413 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1415 name[0] = bfd_get_symbol_leading_char (obfd);
1416 bfd_asymbol_name (sym) = name;
1417 rem_leading_char = FALSE;
1418 add_leading_char = FALSE;
1421 /* Remove leading char. */
1422 if (rem_leading_char)
1423 bfd_asymbol_name (sym) = ++name;
1425 /* Add new leading char and/or prefix. */
1426 if (add_leading_char || prefix_symbols_string)
1430 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1431 + strlen (name) + 1);
1432 if (add_leading_char)
1433 *ptr++ = bfd_get_symbol_leading_char (obfd);
1435 if (prefix_symbols_string)
1437 strcpy (ptr, prefix_symbols_string);
1438 ptr += strlen (prefix_symbols_string);
1442 bfd_asymbol_name (sym) = n;
1446 if (strip_symbols == STRIP_ALL)
1448 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1449 || ((flags & BSF_SECTION_SYM) != 0
1450 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1454 used_in_reloc = TRUE;
1456 else if (relocatable /* Relocatable file. */
1457 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1458 || bfd_is_com_section (bfd_get_section (sym))))
1460 else if (bfd_decode_symclass (sym) == 'I')
1461 /* Global symbols in $idata sections need to be retained
1462 even if relocatable is FALSE. External users of the
1463 library containing the $idata section may reference these
1466 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1467 || (flags & BSF_WEAK) != 0
1469 || bfd_is_com_section (bfd_get_section (sym)))
1470 keep = strip_symbols != STRIP_UNNEEDED;
1471 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1472 keep = (strip_symbols != STRIP_DEBUG
1473 && strip_symbols != STRIP_UNNEEDED
1474 && ! convert_debugging);
1475 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1476 /* COMDAT sections store special information in local
1477 symbols, so we cannot risk stripping any of them. */
1479 else /* Local symbol. */
1480 keep = (strip_symbols != STRIP_UNNEEDED
1481 && (discard_locals != LOCALS_ALL
1482 && (discard_locals != LOCALS_START_L
1483 || ! bfd_is_local_label (abfd, sym))));
1485 if (keep && is_specified_symbol (name, strip_specific_htab))
1487 /* There are multiple ways to set 'keep' above, but if it
1488 was the relocatable symbol case, then that's an error. */
1491 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1499 && !(flags & BSF_KEEP)
1500 && is_specified_symbol (name, strip_unneeded_htab))
1504 && ((keep_file_symbols && (flags & BSF_FILE))
1505 || is_specified_symbol (name, keep_specific_htab)))
1508 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1513 if ((flags & BSF_GLOBAL) != 0
1514 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1516 sym->flags &= ~ BSF_GLOBAL;
1517 sym->flags |= BSF_WEAK;
1521 && (flags & (BSF_GLOBAL | BSF_WEAK))
1522 && (is_specified_symbol (name, localize_specific_htab)
1523 || (htab_elements (keepglobal_specific_htab) != 0
1524 && ! is_specified_symbol (name, keepglobal_specific_htab))
1525 || (localize_hidden && is_hidden_symbol (sym))))
1527 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1528 sym->flags |= BSF_LOCAL;
1532 && (flags & BSF_LOCAL)
1533 && is_specified_symbol (name, globalize_specific_htab))
1535 sym->flags &= ~ BSF_LOCAL;
1536 sym->flags |= BSF_GLOBAL;
1539 to[dst_count++] = sym;
1544 struct addsym_node *ptr = add_sym_list;
1546 for (src_count = 0; src_count < add_symbols; src_count++)
1550 if (strcmp (ptr->othersym, ""))
1551 fatal (_("'before=%s' not found"), ptr->othersym);
1554 to[dst_count++] = create_new_symbol (ptr, obfd);
1560 to[dst_count] = NULL;
1565 /* Find the redefined name of symbol SOURCE. */
1568 lookup_sym_redefinition (const char *source)
1570 struct redefine_node *list;
1572 for (list = redefine_sym_list; list != NULL; list = list->next)
1573 if (strcmp (source, list->source) == 0)
1574 return list->target;
1579 /* Add a node to a symbol redefine list. */
1582 redefine_list_append (const char *cause, const char *source, const char *target)
1584 struct redefine_node **p;
1585 struct redefine_node *list;
1586 struct redefine_node *new_node;
1588 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1590 if (strcmp (source, list->source) == 0)
1591 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1594 if (strcmp (target, list->target) == 0)
1595 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1599 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1601 new_node->source = strdup (source);
1602 new_node->target = strdup (target);
1603 new_node->next = NULL;
1608 /* Handle the --redefine-syms option. Read lines containing "old new"
1609 from the file, and add them to the symbol redefine list. */
1612 add_redefine_syms_file (const char *filename)
1621 file = fopen (filename, "r");
1623 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1624 filename, strerror (errno));
1627 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
1635 /* Collect the input symbol name. */
1636 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1644 buf = (char *) xrealloc (buf, bufsize + 1);
1652 /* Eat white space between the symbol names. */
1653 while (IS_WHITESPACE (c))
1655 if (c == '#' || IS_LINE_TERMINATOR (c))
1660 /* Collect the output symbol name. */
1662 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1670 buf = (char *) xrealloc (buf, bufsize + 1);
1678 /* Eat white space at end of line. */
1679 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1684 if ((c == '\r' && (c = getc (file)) == '\n')
1685 || c == '\n' || c == EOF)
1688 /* Append the redefinition to the list. */
1690 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1701 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1703 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1704 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1707 /* Eat the rest of the line and finish it. */
1708 while (c != '\n' && c != EOF)
1714 fatal (_("%s:%d: premature end of file"), filename, lineno);
1719 /* Copy unkown object file IBFD onto OBFD.
1720 Returns TRUE upon success, FALSE otherwise. */
1723 copy_unknown_object (bfd *ibfd, bfd *obfd)
1731 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1733 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1740 non_fatal (_("stat returns negative size for `%s'"),
1741 bfd_get_archive_filename (ibfd));
1745 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1747 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1752 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1753 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1755 cbuf = (char *) xmalloc (BUFSIZE);
1757 while (ncopied < size)
1759 tocopy = size - ncopied;
1760 if (tocopy > BUFSIZE)
1763 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1764 != (bfd_size_type) tocopy)
1766 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1771 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1772 != (bfd_size_type) tocopy)
1774 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1782 /* We should at least to be able to read it back when copying an
1783 unknown object in an archive. */
1784 chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
1789 /* Copy object file IBFD onto OBFD.
1790 Returns TRUE upon success, FALSE otherwise. */
1793 copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
1797 asection **osections = NULL;
1798 asection *gnu_debuglink_section = NULL;
1799 bfd_size_type *gaps = NULL;
1800 bfd_size_type max_gap = 0;
1803 enum bfd_architecture iarch;
1807 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1808 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1809 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1811 /* PR 17636: Call non-fatal so that we return to our parent who
1812 may need to tidy temporary files. */
1813 non_fatal (_("Unable to change endianness of input file(s)"));
1817 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1819 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1823 if (ibfd->sections == NULL)
1825 non_fatal (_("error: the input file '%s' has no sections"),
1826 bfd_get_archive_filename (ibfd));
1830 if (ibfd->xvec->flavour != bfd_target_elf_flavour)
1832 if ((do_debug_sections & compress) != 0
1833 && do_debug_sections != compress)
1835 non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
1836 bfd_get_archive_filename (ibfd));
1840 if (do_elf_stt_common)
1842 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
1843 bfd_get_archive_filename (ibfd));
1849 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1850 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1851 bfd_get_filename (obfd), bfd_get_target (obfd));
1860 start = bfd_get_start_address (ibfd);
1861 start += change_start;
1864 /* Neither the start address nor the flags
1865 need to be set for a core file. */
1866 if (bfd_get_format (obfd) != bfd_core)
1870 flags = bfd_get_file_flags (ibfd);
1871 flags |= bfd_flags_to_set;
1872 flags &= ~bfd_flags_to_clear;
1873 flags &= bfd_applicable_file_flags (obfd);
1875 if (strip_symbols == STRIP_ALL)
1876 flags &= ~HAS_RELOC;
1878 if (!bfd_set_start_address (obfd, start)
1879 || !bfd_set_file_flags (obfd, flags))
1881 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1886 /* Copy architecture of input file to output file. */
1887 iarch = bfd_get_arch (ibfd);
1888 imach = bfd_get_mach (ibfd);
1891 if (bfd_get_arch_info (ibfd) == NULL
1892 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
1894 iarch = input_arch->arch;
1895 imach = input_arch->mach;
1898 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1899 bfd_get_archive_filename (ibfd));
1901 if (!bfd_set_arch_mach (obfd, iarch, imach)
1902 && (ibfd->target_defaulted
1903 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1905 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1906 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1907 bfd_get_archive_filename (ibfd));
1909 non_fatal (_("Output file cannot represent architecture `%s'"),
1910 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1911 bfd_get_mach (ibfd)));
1915 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1917 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1921 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1922 && bfd_pei_p (obfd))
1924 /* Set up PE parameters. */
1925 pe_data_type *pe = pe_data (obfd);
1927 /* Copy PE parameters before changing them. */
1928 if (ibfd->xvec->flavour == bfd_target_coff_flavour
1929 && bfd_pei_p (ibfd))
1930 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1932 if (pe_file_alignment != (bfd_vma) -1)
1933 pe->pe_opthdr.FileAlignment = pe_file_alignment;
1935 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
1937 if (pe_heap_commit != (bfd_vma) -1)
1938 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
1940 if (pe_heap_reserve != (bfd_vma) -1)
1941 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
1943 if (pe_image_base != (bfd_vma) -1)
1944 pe->pe_opthdr.ImageBase = pe_image_base;
1946 if (pe_section_alignment != (bfd_vma) -1)
1947 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
1949 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
1951 if (pe_stack_commit != (bfd_vma) -1)
1952 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
1954 if (pe_stack_reserve != (bfd_vma) -1)
1955 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
1957 if (pe_subsystem != -1)
1958 pe->pe_opthdr.Subsystem = pe_subsystem;
1960 if (pe_major_subsystem_version != -1)
1961 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
1963 if (pe_minor_subsystem_version != -1)
1964 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
1966 if (pe_file_alignment > pe_section_alignment)
1968 char file_alignment[20], section_alignment[20];
1970 sprintf_vma (file_alignment, pe_file_alignment);
1971 sprintf_vma (section_alignment, pe_section_alignment);
1972 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1974 file_alignment, section_alignment);
1981 if (osympp != isympp)
1987 symsize = bfd_get_symtab_upper_bound (ibfd);
1990 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1994 osympp = isympp = (asymbol **) xmalloc (symsize);
1995 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1998 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2001 /* PR 17512: file: d6323821
2002 If the symbol table could not be loaded do not pretend that we have
2003 any symbols. This trips us up later on when we load the relocs. */
2007 osympp = isympp = NULL;
2010 /* BFD mandates that all output sections be created and sizes set before
2011 any output is done. Thus, we traverse all sections multiple times. */
2012 bfd_map_over_sections (ibfd, setup_section, obfd);
2014 if (!extract_symbol)
2015 setup_bfd_headers (ibfd, obfd);
2017 if (add_sections != NULL)
2019 struct section_add *padd;
2020 struct section_list *pset;
2022 for (padd = add_sections; padd != NULL; padd = padd->next)
2026 pset = find_section_list (padd->name, FALSE,
2027 SECTION_CONTEXT_SET_FLAGS);
2029 flags = pset->flags | SEC_HAS_CONTENTS;
2031 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
2033 /* bfd_make_section_with_flags() does not return very helpful
2034 error codes, so check for the most likely user error first. */
2035 if (bfd_get_section_by_name (obfd, padd->name))
2037 bfd_nonfatal_message (NULL, obfd, NULL,
2038 _("can't add section '%s'"), padd->name);
2043 /* We use LINKER_CREATED here so that the backend hooks
2044 will create any special section type information,
2045 instead of presuming we know what we're doing merely
2046 because we set the flags. */
2047 padd->section = bfd_make_section_with_flags
2048 (obfd, padd->name, flags | SEC_LINKER_CREATED);
2049 if (padd->section == NULL)
2051 bfd_nonfatal_message (NULL, obfd, NULL,
2052 _("can't create section `%s'"),
2058 if (! bfd_set_section_size (obfd, padd->section, padd->size))
2060 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2064 pset = find_section_list (padd->name, FALSE,
2065 SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
2067 && ! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
2069 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2073 pset = find_section_list (padd->name, FALSE,
2074 SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
2077 padd->section->lma = pset->lma_val;
2079 if (! bfd_set_section_alignment
2080 (obfd, padd->section,
2081 bfd_section_alignment (obfd, padd->section)))
2083 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2090 if (update_sections != NULL)
2092 struct section_add *pupdate;
2094 for (pupdate = update_sections;
2096 pupdate = pupdate->next)
2100 pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name);
2101 if (pupdate->section == NULL)
2103 non_fatal (_("error: %s not found, can't be updated"), pupdate->name);
2107 osec = pupdate->section->output_section;
2108 if (! bfd_set_section_size (obfd, osec, pupdate->size))
2110 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2116 if (dump_sections != NULL)
2118 struct section_add * pdump;
2120 for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
2124 sec = bfd_get_section_by_name (ibfd, pdump->name);
2127 bfd_nonfatal_message (NULL, ibfd, NULL,
2128 _("can't dump section '%s' - it does not exist"),
2133 if ((bfd_get_section_flags (ibfd, sec) & SEC_HAS_CONTENTS) == 0)
2135 bfd_nonfatal_message (NULL, ibfd, sec,
2136 _("can't dump section - it has no contents"));
2140 bfd_size_type size = bfd_get_section_size (sec);
2143 bfd_nonfatal_message (NULL, ibfd, sec,
2144 _("can't dump section - it is empty"));
2149 f = fopen (pdump->filename, FOPEN_WB);
2152 bfd_nonfatal_message (pdump->filename, NULL, NULL,
2153 _("could not open section dump file"));
2157 bfd_byte * contents = xmalloc (size);
2158 if (bfd_get_section_contents (ibfd, sec, contents, 0, size))
2160 if (fwrite (contents, 1, size, f) != size)
2162 non_fatal (_("error writing section contents to %s (error: %s)"),
2169 bfd_nonfatal_message (NULL, ibfd, sec,
2170 _("could not retrieve section contents"));
2177 if (gnu_debuglink_filename != NULL)
2179 /* PR 15125: Give a helpful warning message if
2180 the debuglink section already exists, and
2181 allow the rest of the copy to complete. */
2182 if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
2184 non_fatal (_("%s: debuglink section already exists"),
2185 bfd_get_filename (obfd));
2186 gnu_debuglink_filename = NULL;
2190 gnu_debuglink_section = bfd_create_gnu_debuglink_section
2191 (obfd, gnu_debuglink_filename);
2193 if (gnu_debuglink_section == NULL)
2195 bfd_nonfatal_message (NULL, obfd, NULL,
2196 _("cannot create debug link section `%s'"),
2197 gnu_debuglink_filename);
2201 /* Special processing for PE format files. We
2202 have no way to distinguish PE from COFF here. */
2203 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
2205 bfd_vma debuglink_vma;
2206 asection * highest_section;
2209 /* The PE spec requires that all sections be adjacent and sorted
2210 in ascending order of VMA. It also specifies that debug
2211 sections should be last. This is despite the fact that debug
2212 sections are not loaded into memory and so in theory have no
2215 This means that the debuglink section must be given a non-zero
2216 VMA which makes it contiguous with other debug sections. So
2217 walk the current section list, find the section with the
2218 highest VMA and start the debuglink section after that one. */
2219 for (sec = obfd->sections, highest_section = NULL;
2223 && (highest_section == NULL
2224 || sec->vma > highest_section->vma))
2225 highest_section = sec;
2227 if (highest_section)
2228 debuglink_vma = BFD_ALIGN (highest_section->vma
2229 + highest_section->size,
2230 /* FIXME: We ought to be using
2231 COFF_PAGE_SIZE here or maybe
2232 bfd_get_section_alignment() (if it
2233 was set) but since this is for PE
2234 and we know the required alignment
2235 it is easier just to hard code it. */
2238 /* Umm, not sure what to do in this case. */
2239 debuglink_vma = 0x1000;
2241 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
2246 c = bfd_count_sections (obfd);
2248 && (gap_fill_set || pad_to_set))
2252 /* We must fill in gaps between the sections and/or we must pad
2253 the last section to a specified address. We do this by
2254 grabbing a list of the sections, sorting them by VMA, and
2255 increasing the section sizes as required to fill the gaps.
2256 We write out the gap contents below. */
2258 osections = (asection **) xmalloc (c * sizeof (asection *));
2260 bfd_map_over_sections (obfd, get_sections, &set);
2262 qsort (osections, c, sizeof (asection *), compare_section_lma);
2264 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
2265 memset (gaps, 0, c * sizeof (bfd_size_type));
2269 for (i = 0; i < c - 1; i++)
2273 bfd_vma gap_start, gap_stop;
2275 flags = bfd_get_section_flags (obfd, osections[i]);
2276 if ((flags & SEC_HAS_CONTENTS) == 0
2277 || (flags & SEC_LOAD) == 0)
2280 size = bfd_section_size (obfd, osections[i]);
2281 gap_start = bfd_section_lma (obfd, osections[i]) + size;
2282 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
2283 if (gap_start < gap_stop)
2285 if (! bfd_set_section_size (obfd, osections[i],
2286 size + (gap_stop - gap_start)))
2288 bfd_nonfatal_message (NULL, obfd, osections[i],
2289 _("Can't fill gap after section"));
2293 gaps[i] = gap_stop - gap_start;
2294 if (max_gap < gap_stop - gap_start)
2295 max_gap = gap_stop - gap_start;
2305 lma = bfd_section_lma (obfd, osections[c - 1]);
2306 size = bfd_section_size (obfd, osections[c - 1]);
2307 if (lma + size < pad_to)
2309 if (! bfd_set_section_size (obfd, osections[c - 1],
2312 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
2313 _("can't add padding"));
2318 gaps[c - 1] = pad_to - (lma + size);
2319 if (max_gap < pad_to - (lma + size))
2320 max_gap = pad_to - (lma + size);
2326 /* Symbol filtering must happen after the output sections
2327 have been created, but before their contents are set. */
2329 if (convert_debugging)
2330 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
2332 if (strip_symbols == STRIP_DEBUG
2333 || strip_symbols == STRIP_ALL
2334 || strip_symbols == STRIP_UNNEEDED
2335 || strip_symbols == STRIP_NONDEBUG
2336 || strip_symbols == STRIP_DWO
2337 || strip_symbols == STRIP_NONDWO
2338 || discard_locals != LOCALS_UNDEF
2340 || htab_elements (strip_specific_htab) != 0
2341 || htab_elements (keep_specific_htab) != 0
2342 || htab_elements (localize_specific_htab) != 0
2343 || htab_elements (globalize_specific_htab) != 0
2344 || htab_elements (keepglobal_specific_htab) != 0
2345 || htab_elements (weaken_specific_htab) != 0
2346 || prefix_symbols_string
2349 || convert_debugging
2350 || change_leading_char
2351 || remove_leading_char
2352 || redefine_sym_list
2356 /* Mark symbols used in output relocations so that they
2357 are kept, even if they are local labels or static symbols.
2359 Note we iterate over the input sections examining their
2360 relocations since the relocations for the output sections
2361 haven't been set yet. mark_symbols_used_in_relocations will
2362 ignore input sections which have no corresponding output
2364 if (strip_symbols != STRIP_ALL)
2365 bfd_map_over_sections (ibfd,
2366 mark_symbols_used_in_relocations,
2368 osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *));
2369 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
2372 if (convert_debugging && dhandle != NULL)
2374 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
2381 bfd_set_symtab (obfd, osympp, symcount);
2383 /* This has to happen before section positions are set. */
2384 bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
2386 /* This has to happen after the symbol table has been set. */
2387 bfd_map_over_sections (ibfd, copy_section, obfd);
2389 if (add_sections != NULL)
2391 struct section_add *padd;
2393 for (padd = add_sections; padd != NULL; padd = padd->next)
2395 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
2398 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2404 if (update_sections != NULL)
2406 struct section_add *pupdate;
2408 for (pupdate = update_sections;
2410 pupdate = pupdate->next)
2414 osec = pupdate->section->output_section;
2415 if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
2418 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2424 if (gnu_debuglink_filename != NULL)
2426 if (! bfd_fill_in_gnu_debuglink_section
2427 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
2429 bfd_nonfatal_message (NULL, obfd, NULL,
2430 _("cannot fill debug link section `%s'"),
2431 gnu_debuglink_filename);
2436 if (gap_fill_set || pad_to_set)
2440 /* Fill in the gaps. */
2443 buf = (bfd_byte *) xmalloc (max_gap);
2444 memset (buf, gap_fill, max_gap);
2446 c = bfd_count_sections (obfd);
2447 for (i = 0; i < c; i++)
2455 off = bfd_section_size (obfd, osections[i]) - left;
2466 if (! bfd_set_section_contents (obfd, osections[i], buf,
2469 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
2480 /* Allow the BFD backend to copy any private data it understands
2481 from the input BFD to the output BFD. This is done last to
2482 permit the routine to look at the filtered symbol table, which is
2483 important for the ECOFF code at least. */
2484 if (! bfd_copy_private_bfd_data (ibfd, obfd))
2486 bfd_nonfatal_message (NULL, obfd, NULL,
2487 _("error copying private BFD data"));
2491 /* Switch to the alternate machine code. We have to do this at the
2492 very end, because we only initialize the header when we create
2493 the first section. */
2494 if (use_alt_mach_code != 0)
2496 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
2498 non_fatal (_("this target does not support %lu alternative machine codes"),
2500 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2502 non_fatal (_("treating that number as an absolute e_machine value instead"));
2503 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
2506 non_fatal (_("ignoring the alternative value"));
2513 /* Read each archive element in turn from IBFD, copy the
2514 contents to temp file, and keep the temp file handle.
2515 If 'force_output_target' is TRUE then make sure that
2516 all elements in the new archive are of the type
2520 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
2521 bfd_boolean force_output_target,
2522 const bfd_arch_info_type *input_arch)
2526 struct name_list *next;
2530 bfd **ptr = &obfd->archive_head;
2533 const char *filename;
2535 /* Make a temp directory to hold the contents. */
2536 dir = make_tempdir (bfd_get_filename (obfd));
2538 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2541 if (strip_symbols == STRIP_ALL)
2542 obfd->has_armap = FALSE;
2544 obfd->has_armap = ibfd->has_armap;
2545 obfd->is_thin_archive = ibfd->is_thin_archive;
2548 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2552 this_element = bfd_openr_next_archived_file (ibfd, NULL);
2554 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2557 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
2558 goto cleanup_and_exit;
2561 while (!status && this_element != NULL)
2567 int stat_status = 0;
2568 bfd_boolean del = TRUE;
2569 bfd_boolean ok_object;
2571 /* PR binutils/17533: Do not allow directory traversal
2572 outside of the current directory tree by archive members. */
2573 if (! is_valid_archive_path (bfd_get_filename (this_element)))
2575 non_fatal (_("illegal pathname found in archive member: %s"),
2576 bfd_get_filename (this_element));
2578 goto cleanup_and_exit;
2581 /* Create an output file for this member. */
2582 output_name = concat (dir, "/",
2583 bfd_get_filename (this_element), (char *) 0);
2585 /* If the file already exists, make another temp dir. */
2586 if (stat (output_name, &buf) >= 0)
2588 output_name = make_tempdir (output_name);
2589 if (output_name == NULL)
2591 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
2594 goto cleanup_and_exit;
2597 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2598 l->name = output_name;
2602 output_name = concat (output_name, "/",
2603 bfd_get_filename (this_element), (char *) 0);
2608 stat_status = bfd_stat_arch_elt (this_element, &buf);
2610 if (stat_status != 0)
2611 non_fatal (_("internal stat error on %s"),
2612 bfd_get_filename (this_element));
2615 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2616 l->name = output_name;
2621 ok_object = bfd_check_format (this_element, bfd_object);
2623 bfd_nonfatal_message (NULL, this_element, NULL,
2624 _("Unable to recognise the format of file"));
2626 /* PR binutils/3110: Cope with archives
2627 containing multiple target types. */
2628 if (force_output_target || !ok_object)
2629 output_bfd = bfd_openw (output_name, output_target);
2631 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
2633 if (output_bfd == NULL)
2635 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2637 goto cleanup_and_exit;
2642 del = !copy_object (this_element, output_bfd, input_arch);
2644 if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
2645 /* Try again as an unknown object file. */
2647 else if (!bfd_close (output_bfd))
2649 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2650 /* Error in new object file. Don't change archive. */
2657 del = !copy_unknown_object (this_element, output_bfd);
2658 if (!bfd_close_all_done (output_bfd))
2660 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2661 /* Error in new object file. Don't change archive. */
2668 unlink (output_name);
2673 if (preserve_dates && stat_status == 0)
2674 set_times (output_name, &buf);
2676 /* Open the newly output file and attach to our list. */
2677 output_bfd = bfd_openr (output_name, output_target);
2679 l->obfd = output_bfd;
2682 ptr = &output_bfd->archive_next;
2684 last_element = this_element;
2686 this_element = bfd_openr_next_archived_file (ibfd, last_element);
2688 bfd_close (last_element);
2693 filename = bfd_get_filename (obfd);
2694 if (!bfd_close (obfd))
2697 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2700 filename = bfd_get_filename (ibfd);
2701 if (!bfd_close (ibfd))
2704 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2708 /* Delete all the files that we opened. */
2709 for (l = list; l != NULL; l = l->next)
2711 if (l->obfd == NULL)
2715 bfd_close (l->obfd);
2724 set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
2726 /* This is only relevant to Coff targets. */
2727 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
2730 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
2731 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
2732 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
2736 /* The top-level control. */
2739 copy_file (const char *input_filename, const char *output_filename,
2740 const char *input_target, const char *output_target,
2741 const bfd_arch_info_type *input_arch)
2744 char **obj_matching;
2745 char **core_matching;
2746 off_t size = get_file_size (input_filename);
2751 non_fatal (_("error: the input file '%s' is empty"),
2757 /* To allow us to do "strip *" without dying on the first
2758 non-object file, failures are nonfatal. */
2759 ibfd = bfd_openr (input_filename, input_target);
2762 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2767 switch (do_debug_sections)
2771 case compress_gnu_zlib:
2772 case compress_gabi_zlib:
2773 ibfd->flags |= BFD_COMPRESS;
2774 /* Don't check if input is ELF here since this information is
2775 only available after bfd_check_format_matches is called. */
2776 if (do_debug_sections != compress_gnu_zlib)
2777 ibfd->flags |= BFD_COMPRESS_GABI;
2780 ibfd->flags |= BFD_DECOMPRESS;
2786 switch (do_elf_stt_common)
2788 case elf_stt_common:
2789 ibfd->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
2792 case no_elf_stt_common:
2793 ibfd->flags |= BFD_CONVERT_ELF_COMMON;
2799 if (bfd_check_format (ibfd, bfd_archive))
2801 bfd_boolean force_output_target;
2804 /* bfd_get_target does not return the correct value until
2805 bfd_check_format succeeds. */
2806 if (output_target == NULL)
2808 output_target = bfd_get_target (ibfd);
2809 force_output_target = FALSE;
2812 force_output_target = TRUE;
2814 obfd = bfd_openw (output_filename, output_target);
2817 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2821 /* This is a no-op on non-Coff targets. */
2822 set_long_section_mode (obfd, ibfd, long_section_names);
2824 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
2826 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2831 /* bfd_get_target does not return the correct value until
2832 bfd_check_format succeeds. */
2833 if (output_target == NULL)
2834 output_target = bfd_get_target (ibfd);
2836 obfd = bfd_openw (output_filename, output_target);
2839 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2843 /* This is a no-op on non-Coff targets. */
2844 set_long_section_mode (obfd, ibfd, long_section_names);
2846 if (! copy_object (ibfd, obfd, input_arch))
2849 /* PR 17512: file: 0f15796a.
2850 If the file could not be copied it may not be in a writeable
2851 state. So use bfd_close_all_done to avoid the possibility of
2852 writing uninitialised data into the file. */
2853 if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd)))
2856 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2860 if (!bfd_close (ibfd))
2863 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2869 bfd_error_type obj_error = bfd_get_error ();
2870 bfd_error_type core_error;
2872 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2874 /* This probably can't happen.. */
2875 if (obj_error == bfd_error_file_ambiguously_recognized)
2876 free (obj_matching);
2880 core_error = bfd_get_error ();
2881 /* Report the object error in preference to the core error. */
2882 if (obj_error != core_error)
2883 bfd_set_error (obj_error);
2885 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2887 if (obj_error == bfd_error_file_ambiguously_recognized)
2889 list_matching_formats (obj_matching);
2890 free (obj_matching);
2892 if (core_error == bfd_error_file_ambiguously_recognized)
2894 list_matching_formats (core_matching);
2895 free (core_matching);
2902 /* Add a name to the section renaming list. */
2905 add_section_rename (const char * old_name, const char * new_name,
2908 section_rename * srename;
2910 /* Check for conflicts first. */
2911 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2912 if (strcmp (srename->old_name, old_name) == 0)
2914 /* Silently ignore duplicate definitions. */
2915 if (strcmp (srename->new_name, new_name) == 0
2916 && srename->flags == flags)
2919 fatal (_("Multiple renames of section %s"), old_name);
2922 srename = (section_rename *) xmalloc (sizeof (* srename));
2924 srename->old_name = old_name;
2925 srename->new_name = new_name;
2926 srename->flags = flags;
2927 srename->next = section_rename_list;
2929 section_rename_list = srename;
2932 /* Check the section rename list for a new name of the input section
2933 ISECTION. Return the new name if one is found.
2934 Also set RETURNED_FLAGS to the flags to be used for this section. */
2937 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2938 flagword * returned_flags)
2940 const char * old_name = bfd_section_name (ibfd, isection);
2941 section_rename * srename;
2943 /* Default to using the flags of the input section. */
2944 * returned_flags = bfd_get_section_flags (ibfd, isection);
2946 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2947 if (strcmp (srename->old_name, old_name) == 0)
2949 if (srename->flags != (flagword) -1)
2950 * returned_flags = srename->flags;
2952 return srename->new_name;
2958 /* Once each of the sections is copied, we may still need to do some
2959 finalization work for private section headers. Do that here. */
2962 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2964 /* Allow the BFD backend to copy any private data it understands
2965 from the input section to the output section. */
2966 if (! bfd_copy_private_header_data (ibfd, obfd))
2969 bfd_nonfatal_message (NULL, ibfd, NULL,
2970 _("error in private header data"));
2974 /* All went well. */
2978 /* Create a section in OBFD with the same
2979 name and attributes as ISECTION in IBFD. */
2982 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2984 bfd *obfd = (bfd *) obfdarg;
2985 struct section_list *p;
2993 char *prefix = NULL;
2994 bfd_boolean make_nobits;
2996 if (is_strip_section (ibfd, isection))
2999 /* Get the, possibly new, name of the output section. */
3000 name = find_section_rename (ibfd, isection, & flags);
3002 /* Prefix sections. */
3003 if ((prefix_alloc_sections_string)
3004 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
3005 prefix = prefix_alloc_sections_string;
3006 else if (prefix_sections_string)
3007 prefix = prefix_sections_string;
3013 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
3019 make_nobits = FALSE;
3021 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3022 SECTION_CONTEXT_SET_FLAGS);
3024 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
3025 else if (strip_symbols == STRIP_NONDEBUG
3026 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
3027 && !is_nondebug_keep_contents_section (ibfd, isection))
3029 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3030 if (obfd->xvec->flavour == bfd_target_elf_flavour)
3034 /* Twiddle the input section flags so that it seems to
3035 elf.c:copy_private_bfd_data that section flags have not
3036 changed between input and output sections. This hack
3037 prevents wholesale rewriting of the program headers. */
3038 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3042 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
3044 if (osection == NULL)
3046 err = _("failed to create output section");
3051 elf_section_type (osection) = SHT_NOBITS;
3053 size = bfd_section_size (ibfd, isection);
3054 size = bfd_convert_section_size (ibfd, isection, obfd, size);
3056 size = (size + interleave - 1) / interleave * copy_width;
3057 else if (extract_symbol)
3059 if (! bfd_set_section_size (obfd, osection, size))
3061 err = _("failed to set size");
3065 vma = bfd_section_vma (ibfd, isection);
3066 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3067 SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
3070 if (p->context & SECTION_CONTEXT_SET_VMA)
3076 vma += change_section_address;
3078 if (! bfd_set_section_vma (obfd, osection, vma))
3080 err = _("failed to set vma");
3084 lma = isection->lma;
3085 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3086 SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
3089 if (p->context & SECTION_CONTEXT_ALTER_LMA)
3095 lma += change_section_address;
3097 osection->lma = lma;
3099 /* FIXME: This is probably not enough. If we change the LMA we
3100 may have to recompute the header for the file as well. */
3101 if (!bfd_set_section_alignment (obfd,
3103 bfd_section_alignment (ibfd, isection)))
3105 err = _("failed to set alignment");
3109 /* Copy merge entity size. */
3110 osection->entsize = isection->entsize;
3112 /* Copy compress status. */
3113 osection->compress_status = isection->compress_status;
3115 /* This used to be mangle_section; we do here to avoid using
3116 bfd_get_section_by_name since some formats allow multiple
3117 sections with the same name. */
3118 isection->output_section = osection;
3119 isection->output_offset = 0;
3121 if ((isection->flags & SEC_GROUP) != 0)
3123 asymbol *gsym = group_signature (isection);
3127 gsym->flags |= BSF_KEEP;
3128 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
3129 elf_group_id (isection) = gsym;
3133 /* Allow the BFD backend to copy any private data it understands
3134 from the input section to the output section. */
3135 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
3137 err = _("failed to copy private data");
3141 /* All went well. */
3146 bfd_nonfatal_message (NULL, obfd, osection, err);
3149 /* Return TRUE if input section ISECTION should be skipped. */
3152 skip_section (bfd *ibfd, sec_ptr isection)
3158 /* If we have already failed earlier on,
3159 do not keep on generating complaints now. */
3166 if (is_strip_section (ibfd, isection))
3169 if (is_update_section (ibfd, isection))
3172 flags = bfd_get_section_flags (ibfd, isection);
3173 if ((flags & SEC_GROUP) != 0)
3176 osection = isection->output_section;
3177 size = bfd_get_section_size (isection);
3179 if (size == 0 || osection == 0)
3185 /* Copy relocations in input section ISECTION of IBFD to an output
3186 section with the same name in OBFDARG. If stripping then don't
3187 copy any relocation info. */
3190 copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3192 bfd *obfd = (bfd *) obfdarg;
3198 if (skip_section (ibfd, isection))
3201 osection = isection->output_section;
3203 /* Core files and DWO files do not need to be relocated. */
3204 if (bfd_get_format (obfd) == bfd_core || strip_symbols == STRIP_NONDWO)
3208 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
3212 /* Do not complain if the target does not support relocations. */
3213 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3218 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
3226 bfd_set_reloc (obfd, osection, NULL, 0);
3227 osection->flags &= ~SEC_RELOC;
3231 relpp = (arelent **) xmalloc (relsize);
3232 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
3236 bfd_nonfatal_message (NULL, ibfd, isection,
3237 _("relocation count is negative"));
3241 if (strip_symbols == STRIP_ALL)
3243 /* Remove relocations which are not in
3244 keep_strip_specific_list. */
3245 arelent **temp_relpp;
3246 long temp_relcount = 0;
3249 temp_relpp = (arelent **) xmalloc (relsize);
3250 for (i = 0; i < relcount; i++)
3252 /* PR 17512: file: 9e907e0c. */
3253 if (relpp[i]->sym_ptr_ptr
3255 && * relpp[i]->sym_ptr_ptr)
3256 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
3257 keep_specific_htab))
3258 temp_relpp [temp_relcount++] = relpp [i];
3260 relcount = temp_relcount;
3265 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
3268 osection->flags &= ~SEC_RELOC;
3274 /* Copy the data of input section ISECTION of IBFD
3275 to an output section with the same name in OBFD. */
3278 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3280 bfd *obfd = (bfd *) obfdarg;
3281 struct section_list *p;
3285 if (skip_section (ibfd, isection))
3288 osection = isection->output_section;
3289 /* The output SHF_COMPRESSED section size is different from input if
3290 ELF classes of input and output aren't the same. We can't use
3291 the output section size since --interleave will shrink the output
3292 section. Size will be updated if the section is converted. */
3293 size = bfd_get_section_size (isection);
3295 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
3296 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
3298 bfd_byte *memhunk = NULL;
3300 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)
3301 || !bfd_convert_section_contents (ibfd, isection, obfd,
3305 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
3311 /* We don't handle leftover bytes (too many possible behaviors,
3312 and we don't know what the user wants). The section length
3313 must be a multiple of the number of bytes to swap. */
3314 if ((size % reverse_bytes) == 0)
3319 for (i = 0; i < size; i += reverse_bytes)
3320 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
3322 bfd_byte *m = (bfd_byte *) memhunk;
3325 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
3326 m[(i + reverse_bytes) - (j + 1)] = b;
3330 /* User must pad the section up in order to do this. */
3331 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
3332 bfd_section_name (ibfd, isection), reverse_bytes);
3337 /* Keep only every `copy_byte'th byte in MEMHUNK. */
3338 char *from = (char *) memhunk + copy_byte;
3339 char *to = (char *) memhunk;
3340 char *end = (char *) memhunk + size;
3343 for (; from < end; from += interleave)
3344 for (i = 0; i < copy_width; i++)
3346 if (&from[i] >= end)
3351 size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
3352 osection->lma /= interleave;
3355 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
3358 bfd_nonfatal_message (NULL, obfd, osection, NULL);
3363 else if ((p = find_section_list (bfd_get_section_name (ibfd, isection),
3364 FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL
3365 && (p->flags & SEC_HAS_CONTENTS) != 0)
3367 void *memhunk = xmalloc (size);
3369 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
3370 flag--they can just remove the section entirely and add it
3371 back again. However, we do permit them to turn on the
3372 SEC_HAS_CONTENTS flag, and take it to mean that the section
3373 contents should be zeroed out. */
3375 memset (memhunk, 0, size);
3376 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
3379 bfd_nonfatal_message (NULL, obfd, osection, NULL);
3386 /* Get all the sections. This is used when --gap-fill or --pad-to is
3390 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
3392 asection ***secppp = (asection ***) secppparg;
3394 **secppp = osection;
3398 /* Sort sections by VMA. This is called via qsort, and is used when
3399 --gap-fill or --pad-to is used. We force non loadable or empty
3400 sections to the front, where they are easier to ignore. */
3403 compare_section_lma (const void *arg1, const void *arg2)
3405 const asection *const *sec1 = (const asection * const *) arg1;
3406 const asection *const *sec2 = (const asection * const *) arg2;
3407 flagword flags1, flags2;
3409 /* Sort non loadable sections to the front. */
3410 flags1 = (*sec1)->flags;
3411 flags2 = (*sec2)->flags;
3412 if ((flags1 & SEC_HAS_CONTENTS) == 0
3413 || (flags1 & SEC_LOAD) == 0)
3415 if ((flags2 & SEC_HAS_CONTENTS) != 0
3416 && (flags2 & SEC_LOAD) != 0)
3421 if ((flags2 & SEC_HAS_CONTENTS) == 0
3422 || (flags2 & SEC_LOAD) == 0)
3426 /* Sort sections by LMA. */
3427 if ((*sec1)->lma > (*sec2)->lma)
3429 else if ((*sec1)->lma < (*sec2)->lma)
3432 /* Sort sections with the same LMA by size. */
3433 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
3435 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
3441 /* Mark all the symbols which will be used in output relocations with
3442 the BSF_KEEP flag so that those symbols will not be stripped.
3444 Ignore relocations which will not appear in the output file. */
3447 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
3449 asymbol **symbols = (asymbol **) symbolsarg;
3454 /* Ignore an input section with no corresponding output section. */
3455 if (isection->output_section == NULL)
3458 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
3461 /* Do not complain if the target does not support relocations. */
3462 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3464 bfd_fatal (bfd_get_filename (ibfd));
3470 relpp = (arelent **) xmalloc (relsize);
3471 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
3473 bfd_fatal (bfd_get_filename (ibfd));
3475 /* Examine each symbol used in a relocation. If it's not one of the
3476 special bfd section symbols, then mark it with BSF_KEEP. */
3477 for (i = 0; i < relcount; i++)
3479 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
3480 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
3481 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
3482 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
3489 /* Write out debugging information. */
3492 write_debugging_info (bfd *obfd, void *dhandle,
3493 long *symcountp ATTRIBUTE_UNUSED,
3494 asymbol ***symppp ATTRIBUTE_UNUSED)
3496 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
3497 return write_ieee_debugging_info (obfd, dhandle);
3499 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
3500 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
3502 bfd_byte *syms, *strings;
3503 bfd_size_type symsize, stringsize;
3504 asection *stabsec, *stabstrsec;
3507 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
3512 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
3513 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
3514 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
3516 || stabstrsec == NULL
3517 || ! bfd_set_section_size (obfd, stabsec, symsize)
3518 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
3519 || ! bfd_set_section_alignment (obfd, stabsec, 2)
3520 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
3522 bfd_nonfatal_message (NULL, obfd, NULL,
3523 _("can't create debugging section"));
3527 /* We can get away with setting the section contents now because
3528 the next thing the caller is going to do is copy over the
3529 real sections. We may someday have to split the contents
3530 setting out of this function. */
3531 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
3532 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
3535 bfd_nonfatal_message (NULL, obfd, NULL,
3536 _("can't set debugging section contents"));
3543 bfd_nonfatal_message (NULL, obfd, NULL,
3544 _("don't know how to write debugging information for %s"),
3545 bfd_get_target (obfd));
3549 /* If neither -D nor -U was specified explicitly,
3550 then use the configured default. */
3552 default_deterministic (void)
3554 if (deterministic < 0)
3555 deterministic = DEFAULT_AR_DETERMINISTIC;
3559 strip_main (int argc, char *argv[])
3561 char *input_target = NULL;
3562 char *output_target = NULL;
3563 bfd_boolean show_version = FALSE;
3564 bfd_boolean formats_info = FALSE;
3567 char *output_file = NULL;
3569 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvwDU",
3570 strip_options, (int *) 0)) != EOF)
3575 input_target = optarg;
3578 output_target = optarg;
3581 input_target = output_target = optarg;
3584 find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE);
3585 sections_removed = TRUE;
3588 strip_symbols = STRIP_ALL;
3592 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
3593 strip_symbols = STRIP_DEBUG;
3595 case OPTION_STRIP_DWO:
3596 strip_symbols = STRIP_DWO;
3598 case OPTION_STRIP_UNNEEDED:
3599 strip_symbols = STRIP_UNNEEDED;
3602 add_specific_symbol (optarg, keep_specific_htab);
3605 add_specific_symbol (optarg, strip_specific_htab);
3608 output_file = optarg;
3611 preserve_dates = TRUE;
3614 deterministic = TRUE;
3617 deterministic = FALSE;
3620 discard_locals = LOCALS_ALL;
3623 discard_locals = LOCALS_START_L;
3629 show_version = TRUE;
3631 case OPTION_FORMATS_INFO:
3632 formats_info = TRUE;
3634 case OPTION_ONLY_KEEP_DEBUG:
3635 strip_symbols = STRIP_NONDEBUG;
3637 case OPTION_KEEP_FILE_SYMBOLS:
3638 keep_file_symbols = 1;
3641 /* We've been given a long option. */
3648 strip_usage (stdout, 0);
3650 strip_usage (stderr, 1);
3661 print_version ("strip");
3663 default_deterministic ();
3665 /* Default is to strip all symbols. */
3666 if (strip_symbols == STRIP_UNDEF
3667 && discard_locals == LOCALS_UNDEF
3668 && htab_elements (strip_specific_htab) == 0)
3669 strip_symbols = STRIP_ALL;
3671 if (output_target == NULL)
3672 output_target = input_target;
3676 || (output_file != NULL && (i + 1) < argc))
3677 strip_usage (stderr, 1);
3679 for (; i < argc; i++)
3681 int hold_status = status;
3682 struct stat statbuf;
3685 if (get_file_size (argv[i]) < 1)
3692 /* No need to check the return value of stat().
3693 It has already been checked in get_file_size(). */
3694 stat (argv[i], &statbuf);
3696 if (output_file == NULL
3697 || filename_cmp (argv[i], output_file) == 0)
3698 tmpname = make_tempname (argv[i]);
3700 tmpname = output_file;
3702 if (tmpname == NULL)
3704 bfd_nonfatal_message (argv[i], NULL, NULL,
3705 _("could not create temporary file to hold stripped copy"));
3711 copy_file (argv[i], tmpname, input_target, output_target, NULL);
3715 set_times (tmpname, &statbuf);
3716 if (output_file != tmpname)
3717 status = (smart_rename (tmpname,
3718 output_file ? output_file : argv[i],
3719 preserve_dates) != 0);
3721 status = hold_status;
3724 unlink_if_ordinary (tmpname);
3725 if (output_file != tmpname)
3732 /* Set up PE subsystem. */
3735 set_pe_subsystem (const char *s)
3737 const char *version, *subsystem;
3747 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
3748 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
3749 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
3750 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
3751 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
3752 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
3753 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
3754 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
3755 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
3756 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
3762 /* Check for the presence of a version number. */
3763 version = strchr (s, ':');
3764 if (version == NULL)
3768 int len = version - s;
3772 version = copy + 1 + len;
3773 pe_major_subsystem_version = strtoul (version, ©, 0);
3775 pe_minor_subsystem_version = strtoul (copy + 1, ©, 0);
3777 non_fatal (_("%s: bad version in PE subsystem"), s);
3780 /* Check for numeric subsystem. */
3781 value = (short) strtol (subsystem, ©, 0);
3784 for (i = 0; i < ARRAY_SIZE (v); i++)
3785 if (v[i].value == value)
3787 pe_subsystem = value;
3788 set_def = v[i].set_def;
3794 /* Search for subsystem by name. */
3795 for (i = 0; i < ARRAY_SIZE (v); i++)
3796 if (strcmp (subsystem, v[i].name) == 0)
3798 pe_subsystem = v[i].value;
3799 set_def = v[i].set_def;
3807 fatal (_("unknown PE subsystem: %s"), s);
3812 if (pe_file_alignment == (bfd_vma) -1)
3813 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3814 if (pe_section_alignment == (bfd_vma) -1)
3815 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3819 free ((char *) subsystem);
3822 /* Convert EFI target to PEI target. */
3825 convert_efi_target (char *efi)
3831 if (strcmp (efi + 4, "ia32") == 0)
3833 /* Change ia32 to i386. */
3838 else if (strcmp (efi + 4, "x86_64") == 0)
3840 /* Change x86_64 to x86-64. */
3845 /* Allocate and return a pointer to a struct section_add, initializing the
3846 structure using ARG, a string in the format "sectionname=filename".
3847 The returned structure will have its next pointer set to NEXT. The
3848 OPTION field is the name of the command line option currently being
3849 parsed, and is only used if an error needs to be reported. */
3851 static struct section_add *
3852 init_section_add (const char *arg,
3853 struct section_add *next,
3856 struct section_add *pa;
3859 s = strchr (arg, '=');
3861 fatal (_("bad format for %s"), option);
3863 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
3864 pa->name = xstrndup (arg, s - arg);
3865 pa->filename = s + 1;
3867 pa->contents = NULL;
3873 /* Load the file specified in PA, allocating memory to hold the file
3874 contents, and store a pointer to the allocated memory in the contents
3875 field of PA. The size field of PA is also updated. All errors call
3879 section_add_load_file (struct section_add *pa)
3884 /* We don't use get_file_size so that we can do
3885 --add-section .note.GNU_stack=/dev/null
3886 get_file_size doesn't work on /dev/null. */
3888 f = fopen (pa->filename, FOPEN_RB);
3890 fatal (_("cannot open: %s: %s"),
3891 pa->filename, strerror (errno));
3895 pa->contents = (bfd_byte *) xmalloc (alloc);
3903 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
3906 got = fread (pa->contents + off, 1, alloc - off, f);
3908 fatal (_("%s: fread failed"), pa->filename);
3919 copy_main (int argc, char *argv[])
3921 char *input_filename = NULL;
3922 char *output_filename = NULL;
3924 char *input_target = NULL;
3925 char *output_target = NULL;
3926 bfd_boolean show_version = FALSE;
3927 bfd_boolean change_warn = TRUE;
3928 bfd_boolean formats_info = FALSE;
3930 struct stat statbuf;
3931 const bfd_arch_info_type *input_arch = NULL;
3933 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
3934 copy_options, (int *) 0)) != EOF)
3939 copy_byte = atoi (optarg);
3941 fatal (_("byte number must be non-negative"));
3945 input_arch = bfd_scan_arch (optarg);
3946 if (input_arch == NULL)
3947 fatal (_("architecture %s unknown"), optarg);
3953 interleave = atoi (optarg);
3955 fatal (_("interleave must be positive"));
3961 case OPTION_INTERLEAVE_WIDTH:
3962 copy_width = atoi (optarg);
3964 fatal(_("interleave width must be positive"));
3968 case 's': /* "source" - 'I' is preferred */
3969 input_target = optarg;
3973 case 'd': /* "destination" - 'O' is preferred */
3974 output_target = optarg;
3978 input_target = output_target = optarg;
3982 find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY);
3983 sections_copied = TRUE;
3987 find_section_list (optarg, TRUE, SECTION_CONTEXT_REMOVE);
3988 sections_removed = TRUE;
3992 strip_symbols = STRIP_ALL;
3996 strip_symbols = STRIP_DEBUG;
3999 case OPTION_STRIP_DWO:
4000 strip_symbols = STRIP_DWO;
4003 case OPTION_STRIP_UNNEEDED:
4004 strip_symbols = STRIP_UNNEEDED;
4007 case OPTION_ONLY_KEEP_DEBUG:
4008 strip_symbols = STRIP_NONDEBUG;
4011 case OPTION_KEEP_FILE_SYMBOLS:
4012 keep_file_symbols = 1;
4015 case OPTION_ADD_GNU_DEBUGLINK:
4016 long_section_names = ENABLE ;
4017 gnu_debuglink_filename = optarg;
4021 add_specific_symbol (optarg, keep_specific_htab);
4025 add_specific_symbol (optarg, strip_specific_htab);
4028 case OPTION_STRIP_UNNEEDED_SYMBOL:
4029 add_specific_symbol (optarg, strip_unneeded_htab);
4033 add_specific_symbol (optarg, localize_specific_htab);
4036 case OPTION_GLOBALIZE_SYMBOL:
4037 add_specific_symbol (optarg, globalize_specific_htab);
4041 add_specific_symbol (optarg, keepglobal_specific_htab);
4045 add_specific_symbol (optarg, weaken_specific_htab);
4049 preserve_dates = TRUE;
4053 deterministic = TRUE;
4057 deterministic = FALSE;
4065 discard_locals = LOCALS_ALL;
4069 discard_locals = LOCALS_START_L;
4077 show_version = TRUE;
4080 case OPTION_FORMATS_INFO:
4081 formats_info = TRUE;
4088 case OPTION_ADD_SECTION:
4089 add_sections = init_section_add (optarg, add_sections,
4091 section_add_load_file (add_sections);
4094 case OPTION_UPDATE_SECTION:
4095 update_sections = init_section_add (optarg, update_sections,
4096 "--update-section");
4097 section_add_load_file (update_sections);
4100 case OPTION_DUMP_SECTION:
4101 dump_sections = init_section_add (optarg, dump_sections,
4105 case OPTION_ADD_SYMBOL:
4108 struct addsym_node *newsym = xmalloc (sizeof *newsym);
4110 newsym->next = NULL;
4111 s = strchr (optarg, '=');
4113 fatal (_("bad format for %s"), "--add-symbol");
4114 t = strchr (s + 1, ':');
4116 newsym->symdef = xstrndup (optarg, s - optarg);
4119 newsym->section = xstrndup (s + 1, t - (s + 1));
4120 newsym->symval = strtol (t + 1, NULL, 0);
4124 newsym->section = NULL;
4125 newsym->symval = strtol (s + 1, NULL, 0);
4129 t = strchr (t + 1, ',');
4131 newsym->flags = parse_symflags (t+1, &newsym->othersym);
4133 newsym->flags = BSF_GLOBAL;
4135 /* Keep 'othersym' symbols at the front of the list. */
4136 if (newsym->othersym)
4138 newsym->next = add_sym_list;
4140 add_sym_tail = &newsym->next;
4141 add_sym_list = newsym;
4145 *add_sym_tail = newsym;
4146 add_sym_tail = &newsym->next;
4152 case OPTION_CHANGE_START:
4153 change_start = parse_vma (optarg, "--change-start");
4156 case OPTION_CHANGE_SECTION_ADDRESS:
4157 case OPTION_CHANGE_SECTION_LMA:
4158 case OPTION_CHANGE_SECTION_VMA:
4160 struct section_list * p;
4161 unsigned int context = 0;
4165 char *option = NULL;
4170 case OPTION_CHANGE_SECTION_ADDRESS:
4171 option = "--change-section-address";
4172 context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
4174 case OPTION_CHANGE_SECTION_LMA:
4175 option = "--change-section-lma";
4176 context = SECTION_CONTEXT_ALTER_LMA;
4178 case OPTION_CHANGE_SECTION_VMA:
4179 option = "--change-section-vma";
4180 context = SECTION_CONTEXT_ALTER_VMA;
4184 s = strchr (optarg, '=');
4187 s = strchr (optarg, '+');
4190 s = strchr (optarg, '-');
4192 fatal (_("bad format for %s"), option);
4197 /* Correct the context. */
4200 case OPTION_CHANGE_SECTION_ADDRESS:
4201 context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
4203 case OPTION_CHANGE_SECTION_LMA:
4204 context = SECTION_CONTEXT_SET_LMA;
4206 case OPTION_CHANGE_SECTION_VMA:
4207 context = SECTION_CONTEXT_SET_VMA;
4213 name = (char *) xmalloc (len + 1);
4214 strncpy (name, optarg, len);
4217 p = find_section_list (name, TRUE, context);
4219 val = parse_vma (s + 1, option);
4225 case OPTION_CHANGE_SECTION_ADDRESS:
4229 case OPTION_CHANGE_SECTION_LMA:
4233 case OPTION_CHANGE_SECTION_VMA:
4240 case OPTION_CHANGE_ADDRESSES:
4241 change_section_address = parse_vma (optarg, "--change-addresses");
4242 change_start = change_section_address;
4245 case OPTION_CHANGE_WARNINGS:
4249 case OPTION_CHANGE_LEADING_CHAR:
4250 change_leading_char = TRUE;
4253 case OPTION_COMPRESS_DEBUG_SECTIONS:
4256 if (strcasecmp (optarg, "none") == 0)
4257 do_debug_sections = decompress;
4258 else if (strcasecmp (optarg, "zlib") == 0)
4259 do_debug_sections = compress_zlib;
4260 else if (strcasecmp (optarg, "zlib-gnu") == 0)
4261 do_debug_sections = compress_gnu_zlib;
4262 else if (strcasecmp (optarg, "zlib-gabi") == 0)
4263 do_debug_sections = compress_gabi_zlib;
4265 fatal (_("unrecognized --compress-debug-sections type `%s'"),
4269 do_debug_sections = compress;
4272 case OPTION_DEBUGGING:
4273 convert_debugging = TRUE;
4276 case OPTION_DECOMPRESS_DEBUG_SECTIONS:
4277 do_debug_sections = decompress;
4280 case OPTION_ELF_STT_COMMON:
4281 if (strcasecmp (optarg, "yes") == 0)
4282 do_elf_stt_common = elf_stt_common;
4283 else if (strcasecmp (optarg, "no") == 0)
4284 do_elf_stt_common = no_elf_stt_common;
4286 fatal (_("unrecognized --elf-stt-common= option `%s'"),
4290 case OPTION_GAP_FILL:
4292 bfd_vma gap_fill_vma;
4294 gap_fill_vma = parse_vma (optarg, "--gap-fill");
4295 gap_fill = (bfd_byte) gap_fill_vma;
4296 if ((bfd_vma) gap_fill != gap_fill_vma)
4300 sprintf_vma (buff, gap_fill_vma);
4302 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
4305 gap_fill_set = TRUE;
4309 case OPTION_NO_CHANGE_WARNINGS:
4310 change_warn = FALSE;
4314 pad_to = parse_vma (optarg, "--pad-to");
4318 case OPTION_REMOVE_LEADING_CHAR:
4319 remove_leading_char = TRUE;
4322 case OPTION_REDEFINE_SYM:
4324 /* Push this redefinition onto redefine_symbol_list. */
4328 const char *nextarg;
4329 char *source, *target;
4331 s = strchr (optarg, '=');
4333 fatal (_("bad format for %s"), "--redefine-sym");
4336 source = (char *) xmalloc (len + 1);
4337 strncpy (source, optarg, len);
4341 len = strlen (nextarg);
4342 target = (char *) xmalloc (len + 1);
4343 strcpy (target, nextarg);
4345 redefine_list_append ("--redefine-sym", source, target);
4352 case OPTION_REDEFINE_SYMS:
4353 add_redefine_syms_file (optarg);
4356 case OPTION_SET_SECTION_FLAGS:
4358 struct section_list *p;
4363 s = strchr (optarg, '=');
4365 fatal (_("bad format for %s"), "--set-section-flags");
4368 name = (char *) xmalloc (len + 1);
4369 strncpy (name, optarg, len);
4372 p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS);
4374 p->flags = parse_flags (s + 1);
4378 case OPTION_RENAME_SECTION:
4381 const char *eq, *fl;
4386 eq = strchr (optarg, '=');
4388 fatal (_("bad format for %s"), "--rename-section");
4392 fatal (_("bad format for %s"), "--rename-section");
4394 old_name = (char *) xmalloc (len + 1);
4395 strncpy (old_name, optarg, len);
4399 fl = strchr (eq, ',');
4402 flags = parse_flags (fl + 1);
4412 fatal (_("bad format for %s"), "--rename-section");
4414 new_name = (char *) xmalloc (len + 1);
4415 strncpy (new_name, eq, len);
4418 add_section_rename (old_name, new_name, flags);
4422 case OPTION_SET_START:
4423 set_start = parse_vma (optarg, "--set-start");
4424 set_start_set = TRUE;
4427 case OPTION_SREC_LEN:
4428 Chunk = parse_vma (optarg, "--srec-len");
4431 case OPTION_SREC_FORCES3:
4435 case OPTION_STRIP_SYMBOLS:
4436 add_specific_symbols (optarg, strip_specific_htab);
4439 case OPTION_STRIP_UNNEEDED_SYMBOLS:
4440 add_specific_symbols (optarg, strip_unneeded_htab);
4443 case OPTION_KEEP_SYMBOLS:
4444 add_specific_symbols (optarg, keep_specific_htab);
4447 case OPTION_LOCALIZE_HIDDEN:
4448 localize_hidden = TRUE;
4451 case OPTION_LOCALIZE_SYMBOLS:
4452 add_specific_symbols (optarg, localize_specific_htab);
4455 case OPTION_LONG_SECTION_NAMES:
4456 if (!strcmp ("enable", optarg))
4457 long_section_names = ENABLE;
4458 else if (!strcmp ("disable", optarg))
4459 long_section_names = DISABLE;
4460 else if (!strcmp ("keep", optarg))
4461 long_section_names = KEEP;
4463 fatal (_("unknown long section names option '%s'"), optarg);
4466 case OPTION_GLOBALIZE_SYMBOLS:
4467 add_specific_symbols (optarg, globalize_specific_htab);
4470 case OPTION_KEEPGLOBAL_SYMBOLS:
4471 add_specific_symbols (optarg, keepglobal_specific_htab);
4474 case OPTION_WEAKEN_SYMBOLS:
4475 add_specific_symbols (optarg, weaken_specific_htab);
4478 case OPTION_ALT_MACH_CODE:
4479 use_alt_mach_code = strtoul (optarg, NULL, 0);
4480 if (use_alt_mach_code == 0)
4481 fatal (_("unable to parse alternative machine code"));
4484 case OPTION_PREFIX_SYMBOLS:
4485 prefix_symbols_string = optarg;
4488 case OPTION_PREFIX_SECTIONS:
4489 prefix_sections_string = optarg;
4492 case OPTION_PREFIX_ALLOC_SECTIONS:
4493 prefix_alloc_sections_string = optarg;
4496 case OPTION_READONLY_TEXT:
4497 bfd_flags_to_set |= WP_TEXT;
4498 bfd_flags_to_clear &= ~WP_TEXT;
4501 case OPTION_WRITABLE_TEXT:
4502 bfd_flags_to_clear |= WP_TEXT;
4503 bfd_flags_to_set &= ~WP_TEXT;
4507 bfd_flags_to_set |= D_PAGED;
4508 bfd_flags_to_clear &= ~D_PAGED;
4512 bfd_flags_to_clear |= D_PAGED;
4513 bfd_flags_to_set &= ~D_PAGED;
4516 case OPTION_EXTRACT_DWO:
4517 strip_symbols = STRIP_NONDWO;
4520 case OPTION_EXTRACT_SYMBOL:
4521 extract_symbol = TRUE;
4524 case OPTION_REVERSE_BYTES:
4526 int prev = reverse_bytes;
4528 reverse_bytes = atoi (optarg);
4529 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
4530 fatal (_("number of bytes to reverse must be positive and even"));
4532 if (prev && prev != reverse_bytes)
4533 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
4538 case OPTION_FILE_ALIGNMENT:
4539 pe_file_alignment = parse_vma (optarg, "--file-alignment");
4545 pe_heap_reserve = strtoul (optarg, &end, 0);
4547 || (*end != '.' && *end != '\0'))
4548 non_fatal (_("%s: invalid reserve value for --heap"),
4550 else if (*end != '\0')
4552 pe_heap_commit = strtoul (end + 1, &end, 0);
4554 non_fatal (_("%s: invalid commit value for --heap"),
4560 case OPTION_IMAGE_BASE:
4561 pe_image_base = parse_vma (optarg, "--image-base");
4564 case OPTION_SECTION_ALIGNMENT:
4565 pe_section_alignment = parse_vma (optarg,
4566 "--section-alignment");
4569 case OPTION_SUBSYSTEM:
4570 set_pe_subsystem (optarg);
4576 pe_stack_reserve = strtoul (optarg, &end, 0);
4578 || (*end != '.' && *end != '\0'))
4579 non_fatal (_("%s: invalid reserve value for --stack"),
4581 else if (*end != '\0')
4583 pe_stack_commit = strtoul (end + 1, &end, 0);
4585 non_fatal (_("%s: invalid commit value for --stack"),
4592 /* We've been given a long option. */
4597 copy_usage (stdout, 0);
4600 copy_usage (stderr, 1);
4611 print_version ("objcopy");
4613 if (interleave && copy_byte == -1)
4614 fatal (_("interleave start byte must be set with --byte"));
4616 if (copy_byte >= interleave)
4617 fatal (_("byte number must be less than interleave"));
4619 if (copy_width > interleave - copy_byte)
4620 fatal (_("interleave width must be less than or equal to interleave - byte`"));
4622 if (optind == argc || optind + 2 < argc)
4623 copy_usage (stderr, 1);
4625 input_filename = argv[optind];
4626 if (optind + 1 < argc)
4627 output_filename = argv[optind + 1];
4629 default_deterministic ();
4631 /* Default is to strip no symbols. */
4632 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
4633 strip_symbols = STRIP_NONE;
4635 if (output_target == NULL)
4636 output_target = input_target;
4638 /* Convert input EFI target to PEI target. */
4639 if (input_target != NULL
4640 && strncmp (input_target, "efi-", 4) == 0)
4644 efi = xstrdup (output_target + 4);
4645 if (strncmp (efi, "bsdrv-", 6) == 0
4646 || strncmp (efi, "rtdrv-", 6) == 0)
4648 else if (strncmp (efi, "app-", 4) != 0)
4649 fatal (_("unknown input EFI target: %s"), input_target);
4652 convert_efi_target (efi);
4655 /* Convert output EFI target to PEI target. */
4656 if (output_target != NULL
4657 && strncmp (output_target, "efi-", 4) == 0)
4661 efi = xstrdup (output_target + 4);
4662 if (strncmp (efi, "app-", 4) == 0)
4664 if (pe_subsystem == -1)
4665 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
4667 else if (strncmp (efi, "bsdrv-", 6) == 0)
4669 if (pe_subsystem == -1)
4670 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
4673 else if (strncmp (efi, "rtdrv-", 6) == 0)
4675 if (pe_subsystem == -1)
4676 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
4680 fatal (_("unknown output EFI target: %s"), output_target);
4682 if (pe_file_alignment == (bfd_vma) -1)
4683 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
4684 if (pe_section_alignment == (bfd_vma) -1)
4685 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
4687 output_target = efi;
4688 convert_efi_target (efi);
4692 if (stat (input_filename, & statbuf) < 0)
4693 fatal (_("warning: could not locate '%s'. System error message: %s"),
4694 input_filename, strerror (errno));
4696 /* If there is no destination file, or the source and destination files
4697 are the same, then create a temp and rename the result into the input. */
4698 if (output_filename == NULL
4699 || filename_cmp (input_filename, output_filename) == 0)
4700 tmpname = make_tempname (input_filename);
4702 tmpname = output_filename;
4704 if (tmpname == NULL)
4705 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
4706 input_filename, strerror (errno));
4708 copy_file (input_filename, tmpname, input_target, output_target, input_arch);
4712 set_times (tmpname, &statbuf);
4713 if (tmpname != output_filename)
4714 status = (smart_rename (tmpname, input_filename,
4715 preserve_dates) != 0);
4718 unlink_if_ordinary (tmpname);
4722 struct section_list *p;
4724 for (p = change_sections; p != NULL; p = p->next)
4728 if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
4732 sprintf_vma (buff, p->vma_val);
4734 /* xgettext:c-format */
4735 non_fatal (_("%s %s%c0x%s never used"),
4736 "--change-section-vma",
4738 p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
4742 if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
4746 sprintf_vma (buff, p->lma_val);
4748 /* xgettext:c-format */
4749 non_fatal (_("%s %s%c0x%s never used"),
4750 "--change-section-lma",
4752 p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
4763 main (int argc, char *argv[])
4765 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4766 setlocale (LC_MESSAGES, "");
4768 #if defined (HAVE_SETLOCALE)
4769 setlocale (LC_CTYPE, "");
4771 bindtextdomain (PACKAGE, LOCALEDIR);
4772 textdomain (PACKAGE);
4774 program_name = argv[0];
4775 xmalloc_set_program_name (program_name);
4777 START_PROGRESS (program_name, 0);
4779 expandargv (&argc, &argv);
4781 strip_symbols = STRIP_UNDEF;
4782 discard_locals = LOCALS_UNDEF;
4785 set_default_bfd_target ();
4789 int i = strlen (program_name);
4790 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4791 /* Drop the .exe suffix, if any. */
4792 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
4795 program_name[i] = '\0';
4798 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
4801 create_symbol_htabs ();
4804 bfd_set_error_program_name (argv[0]);
4807 strip_main (argc, argv);
4809 copy_main (argc, argv);
4811 END_PROGRESS (program_name);