1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2017 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"
31 #include "coff/internal.h"
34 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
35 header in generic PE code. */
36 #include "coff/i386.h"
39 static bfd_vma pe_file_alignment = (bfd_vma) -1;
40 static bfd_vma pe_heap_commit = (bfd_vma) -1;
41 static bfd_vma pe_heap_reserve = (bfd_vma) -1;
42 static bfd_vma pe_image_base = (bfd_vma) -1;
43 static bfd_vma pe_section_alignment = (bfd_vma) -1;
44 static bfd_vma pe_stack_commit = (bfd_vma) -1;
45 static bfd_vma pe_stack_reserve = (bfd_vma) -1;
46 static short pe_subsystem = -1;
47 static short pe_major_subsystem_version = -1;
48 static short pe_minor_subsystem_version = -1;
50 struct is_specified_symbol_predicate_data
56 /* A list to support redefine_sym. */
61 struct redefine_node *next;
66 struct addsym_node *next;
74 typedef struct section_rename
76 const char * old_name;
77 const char * new_name;
79 struct section_rename * next;
83 /* List of sections to be renamed. */
84 static section_rename *section_rename_list;
86 static asymbol **isympp = NULL; /* Input symbols. */
87 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
89 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
90 static int copy_byte = -1;
91 static int interleave = 0; /* Initialised to 4 in copy_main(). */
92 static int copy_width = 1;
94 static bfd_boolean verbose; /* Print file and target names. */
95 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
96 static int deterministic = -1; /* Enable deterministic archives. */
97 static int status = 0; /* Exit status. */
102 STRIP_NONE, /* Don't strip. */
103 STRIP_DEBUG, /* Strip all debugger symbols. */
104 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
105 STRIP_NONDEBUG, /* Strip everything but debug info. */
106 STRIP_DWO, /* Strip all DWO info. */
107 STRIP_NONDWO, /* Strip everything but DWO info. */
108 STRIP_ALL /* Strip all symbols. */
111 /* Which symbols to remove. */
112 static enum strip_action strip_symbols = STRIP_UNDEF;
117 LOCALS_START_L, /* Discard locals starting with L. */
118 LOCALS_ALL /* Discard all locals. */
121 /* Which local symbols to remove. Overrides STRIP_ALL. */
122 static enum locals_action discard_locals;
124 /* Structure used to hold lists of sections and actions to take. */
127 struct section_list * next; /* Next section to change. */
128 const char * pattern; /* Section name pattern. */
129 bfd_boolean used; /* Whether this entry was used. */
131 unsigned int context; /* What to do with matching sections. */
132 /* Flag bits used in the context field.
133 COPY and REMOVE are mutually exlusive. SET and ALTER are mutually exclusive. */
134 #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
135 #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
136 #define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */
137 #define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */
138 #define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */
139 #define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */
140 #define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */
141 #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 7) /* Remove relocations for this section. */
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_elf_stt_common 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_REMOVE_RELOCS,
330 OPTION_RENAME_SECTION,
331 OPTION_REVERSE_BYTES,
332 OPTION_SECTION_ALIGNMENT,
333 OPTION_SET_SECTION_FLAGS,
339 OPTION_STRIP_SYMBOLS,
340 OPTION_STRIP_UNNEEDED,
341 OPTION_STRIP_UNNEEDED_SYMBOL,
342 OPTION_STRIP_UNNEEDED_SYMBOLS,
344 OPTION_UPDATE_SECTION,
346 OPTION_WEAKEN_SYMBOLS,
350 /* Options to handle if running as "strip". */
352 static struct option strip_options[] =
354 {"disable-deterministic-archives", no_argument, 0, 'U'},
355 {"discard-all", no_argument, 0, 'x'},
356 {"discard-locals", no_argument, 0, 'X'},
357 {"enable-deterministic-archives", no_argument, 0, 'D'},
358 {"format", required_argument, 0, 'F'}, /* Obsolete */
359 {"help", no_argument, 0, 'h'},
360 {"info", no_argument, 0, OPTION_FORMATS_INFO},
361 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
362 {"input-target", required_argument, 0, 'I'},
363 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
364 {"keep-symbol", required_argument, 0, 'K'},
365 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
366 {"output-file", required_argument, 0, 'o'},
367 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
368 {"output-target", required_argument, 0, 'O'},
369 {"preserve-dates", no_argument, 0, 'p'},
370 {"remove-section", required_argument, 0, 'R'},
371 {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
372 {"strip-all", no_argument, 0, 's'},
373 {"strip-debug", no_argument, 0, 'S'},
374 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
375 {"strip-symbol", required_argument, 0, 'N'},
376 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
377 {"target", required_argument, 0, 'F'},
378 {"verbose", no_argument, 0, 'v'},
379 {"version", no_argument, 0, 'V'},
380 {"wildcard", no_argument, 0, 'w'},
381 {0, no_argument, 0, 0}
384 /* Options to handle if running as "objcopy". */
386 static struct option copy_options[] =
388 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
389 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
390 {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
391 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
392 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
393 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
394 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
395 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
396 {"binary-architecture", required_argument, 0, 'B'},
397 {"byte", required_argument, 0, 'b'},
398 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
399 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
400 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
401 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
402 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
403 {"change-start", required_argument, 0, OPTION_CHANGE_START},
404 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
405 {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
406 {"debugging", no_argument, 0, OPTION_DEBUGGING},
407 {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
408 {"disable-deterministic-archives", no_argument, 0, 'U'},
409 {"discard-all", no_argument, 0, 'x'},
410 {"discard-locals", no_argument, 0, 'X'},
411 {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
412 {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
413 {"enable-deterministic-archives", no_argument, 0, 'D'},
414 {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
415 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
416 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
417 {"format", required_argument, 0, 'F'}, /* Obsolete */
418 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
419 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
420 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
421 {"heap", required_argument, 0, OPTION_HEAP},
422 {"help", no_argument, 0, 'h'},
423 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
424 {"impure", no_argument, 0, OPTION_IMPURE},
425 {"info", no_argument, 0, OPTION_FORMATS_INFO},
426 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
427 {"input-target", required_argument, 0, 'I'},
428 {"interleave", optional_argument, 0, 'i'},
429 {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
430 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
431 {"keep-global-symbol", required_argument, 0, 'G'},
432 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
433 {"keep-symbol", required_argument, 0, 'K'},
434 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
435 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
436 {"localize-symbol", required_argument, 0, 'L'},
437 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
438 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
439 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
440 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
441 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
442 {"only-section", required_argument, 0, 'j'},
443 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
444 {"output-target", required_argument, 0, 'O'},
445 {"pad-to", required_argument, 0, OPTION_PAD_TO},
446 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
447 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
448 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
449 {"preserve-dates", no_argument, 0, 'p'},
450 {"pure", no_argument, 0, OPTION_PURE},
451 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
452 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
453 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
454 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
455 {"remove-section", required_argument, 0, 'R'},
456 {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
457 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
458 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
459 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
460 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
461 {"set-start", required_argument, 0, OPTION_SET_START},
462 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
463 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
464 {"stack", required_argument, 0, OPTION_STACK},
465 {"strip-all", no_argument, 0, 'S'},
466 {"strip-debug", no_argument, 0, 'g'},
467 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
468 {"strip-symbol", required_argument, 0, 'N'},
469 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
470 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
471 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
472 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
473 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
474 {"target", required_argument, 0, 'F'},
475 {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
476 {"verbose", no_argument, 0, 'v'},
477 {"version", no_argument, 0, 'V'},
478 {"weaken", no_argument, 0, OPTION_WEAKEN},
479 {"weaken-symbol", required_argument, 0, 'W'},
480 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
481 {"wildcard", no_argument, 0, 'w'},
482 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
483 {0, no_argument, 0, 0}
487 extern char *program_name;
489 /* This flag distinguishes between strip and objcopy:
490 1 means this is 'strip'; 0 means this is 'objcopy'.
491 -1 means if we should use argv[0] to decide. */
494 /* The maximum length of an S record. This variable is declared in srec.c
495 and can be modified by the --srec-len parameter. */
496 extern unsigned int Chunk;
498 /* Restrict the generation of Srecords to type S3 only.
499 This variable is declare in bfd/srec.c and can be toggled
500 on by the --srec-forceS3 command line switch. */
501 extern bfd_boolean S3Forced;
503 /* Forward declarations. */
504 static void setup_section (bfd *, asection *, void *);
505 static void setup_bfd_headers (bfd *, bfd *);
506 static void copy_relocations_in_section (bfd *, asection *, void *);
507 static void copy_section (bfd *, asection *, void *);
508 static void get_sections (bfd *, asection *, void *);
509 static int compare_section_lma (const void *, const void *);
510 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
511 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
512 static const char *lookup_sym_redefinition (const char *);
513 static const char *find_section_rename (const char *, flagword *);
515 ATTRIBUTE_NORETURN static void
516 copy_usage (FILE *stream, int exit_status)
518 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
519 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
520 fprintf (stream, _(" The options are:\n"));
521 fprintf (stream, _("\
522 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
523 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
524 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
525 -F --target <bfdname> Set both input and output format to <bfdname>\n\
526 --debugging Convert debugging information, if possible\n\
527 -p --preserve-dates Copy modified/access timestamps to the output\n"));
528 if (DEFAULT_AR_DETERMINISTIC)
529 fprintf (stream, _("\
530 -D --enable-deterministic-archives\n\
531 Produce deterministic output when stripping archives (default)\n\
532 -U --disable-deterministic-archives\n\
533 Disable -D behavior\n"));
535 fprintf (stream, _("\
536 -D --enable-deterministic-archives\n\
537 Produce deterministic output when stripping archives\n\
538 -U --disable-deterministic-archives\n\
539 Disable -D behavior (default)\n"));
540 fprintf (stream, _("\
541 -j --only-section <name> Only copy section <name> into the output\n\
542 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
543 -R --remove-section <name> Remove section <name> from the output\n\
544 --remove-relocations <name> Remove relocations from section <name>\n\
545 -S --strip-all Remove all symbol and relocation information\n\
546 -g --strip-debug Remove all debugging symbols & sections\n\
547 --strip-dwo Remove all DWO sections\n\
548 --strip-unneeded Remove all symbols not needed by relocations\n\
549 -N --strip-symbol <name> Do not copy symbol <name>\n\
550 --strip-unneeded-symbol <name>\n\
551 Do not copy symbol <name> unless needed by\n\
553 --only-keep-debug Strip everything but the debug information\n\
554 --extract-dwo Copy only DWO sections\n\
555 --extract-symbol Remove section contents but keep symbols\n\
556 -K --keep-symbol <name> Do not strip symbol <name>\n\
557 --keep-file-symbols Do not strip file symbol(s)\n\
558 --localize-hidden Turn all ELF hidden symbols into locals\n\
559 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
560 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
561 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
562 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
563 --weaken Force all global symbols to be marked as weak\n\
564 -w --wildcard Permit wildcard in symbol comparison\n\
565 -x --discard-all Remove all non-global symbols\n\
566 -X --discard-locals Remove any compiler-generated symbols\n\
567 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
568 --interleave-width <number> Set N for --interleave\n\
569 -b --byte <num> Select byte <num> in every interleaved block\n\
570 --gap-fill <val> Fill gaps between sections with <val>\n\
571 --pad-to <addr> Pad the last section up to address <addr>\n\
572 --set-start <addr> Set the start address to <addr>\n\
573 {--change-start|--adjust-start} <incr>\n\
574 Add <incr> to the start address\n\
575 {--change-addresses|--adjust-vma} <incr>\n\
576 Add <incr> to LMA, VMA and start addresses\n\
577 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
578 Change LMA and VMA of section <name> by <val>\n\
579 --change-section-lma <name>{=|+|-}<val>\n\
580 Change the LMA of section <name> by <val>\n\
581 --change-section-vma <name>{=|+|-}<val>\n\
582 Change the VMA of section <name> by <val>\n\
583 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
584 Warn if a named section does not exist\n\
585 --set-section-flags <name>=<flags>\n\
586 Set section <name>'s properties to <flags>\n\
587 --add-section <name>=<file> Add section <name> found in <file> to output\n\
588 --update-section <name>=<file>\n\
589 Update contents of section <name> with\n\
590 contents found in <file>\n\
591 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
592 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
593 --long-section-names {enable|disable|keep}\n\
594 Handle long section names in Coff objects.\n\
595 --change-leading-char Force output format's leading character style\n\
596 --remove-leading-char Remove leading character from global symbols\n\
597 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
598 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
599 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
601 --srec-len <number> Restrict the length of generated Srecords\n\
602 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
603 --strip-symbols <file> -N for all symbols listed in <file>\n\
604 --strip-unneeded-symbols <file>\n\
605 --strip-unneeded-symbol for all symbols listed\n\
607 --keep-symbols <file> -K for all symbols listed in <file>\n\
608 --localize-symbols <file> -L for all symbols listed in <file>\n\
609 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
610 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
611 --weaken-symbols <file> -W for all symbols listed in <file>\n\
612 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
613 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
614 --writable-text Mark the output text as writable\n\
615 --readonly-text Make the output text write protected\n\
616 --pure Mark the output file as demand paged\n\
617 --impure Mark the output file as impure\n\
618 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
619 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
620 --prefix-alloc-sections <prefix>\n\
621 Add <prefix> to start of every allocatable\n\
623 --file-alignment <num> Set PE file alignment to <num>\n\
624 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
626 --image-base <address> Set PE image base to <address>\n\
627 --section-alignment <num> Set PE section alignment to <num>\n\
628 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
630 --subsystem <name>[:<version>]\n\
631 Set PE subsystem to <name> [& <version>]\n\
632 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
633 Compress DWARF debug sections using zlib\n\
634 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
635 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
637 -v --verbose List all object files modified\n\
638 @<file> Read options from <file>\n\
639 -V --version Display this program's version number\n\
640 -h --help Display this output\n\
641 --info List object formats & architectures supported\n\
643 list_supported_targets (program_name, stream);
644 if (REPORT_BUGS_TO[0] && exit_status == 0)
645 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
649 ATTRIBUTE_NORETURN static void
650 strip_usage (FILE *stream, int exit_status)
652 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
653 fprintf (stream, _(" Removes symbols and sections from files\n"));
654 fprintf (stream, _(" The options are:\n"));
655 fprintf (stream, _("\
656 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
657 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
658 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
659 -p --preserve-dates Copy modified/access timestamps to the output\n\
661 if (DEFAULT_AR_DETERMINISTIC)
662 fprintf (stream, _("\
663 -D --enable-deterministic-archives\n\
664 Produce deterministic output when stripping archives (default)\n\
665 -U --disable-deterministic-archives\n\
666 Disable -D behavior\n"));
668 fprintf (stream, _("\
669 -D --enable-deterministic-archives\n\
670 Produce deterministic output when stripping archives\n\
671 -U --disable-deterministic-archives\n\
672 Disable -D behavior (default)\n"));
673 fprintf (stream, _("\
674 -R --remove-section=<name> Also remove section <name> from the output\n\
675 --remove-relocations <name> Remove relocations from section <name>\n\
676 -s --strip-all Remove all symbol and relocation information\n\
677 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
678 --strip-dwo Remove all DWO sections\n\
679 --strip-unneeded Remove all symbols not needed by relocations\n\
680 --only-keep-debug Strip everything but the debug information\n\
681 -N --strip-symbol=<name> Do not copy symbol <name>\n\
682 -K --keep-symbol=<name> Do not strip symbol <name>\n\
683 --keep-file-symbols Do not strip file symbol(s)\n\
684 -w --wildcard Permit wildcard in symbol comparison\n\
685 -x --discard-all Remove all non-global symbols\n\
686 -X --discard-locals Remove any compiler-generated symbols\n\
687 -v --verbose List all object files modified\n\
688 -V --version Display this program's version number\n\
689 -h --help Display this output\n\
690 --info List object formats & architectures supported\n\
691 -o <file> Place stripped output into <file>\n\
694 list_supported_targets (program_name, stream);
695 if (REPORT_BUGS_TO[0] && exit_status == 0)
696 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
700 /* Parse section flags into a flagword, with a fatal error if the
701 string can't be parsed. */
704 parse_flags (const char *s)
714 snext = strchr (s, ',');
724 #define PARSE_FLAG(fname,fval) \
725 else if (strncasecmp (fname, s, len) == 0) ret |= fval
726 PARSE_FLAG ("alloc", SEC_ALLOC);
727 PARSE_FLAG ("load", SEC_LOAD);
728 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
729 PARSE_FLAG ("readonly", SEC_READONLY);
730 PARSE_FLAG ("debug", SEC_DEBUGGING);
731 PARSE_FLAG ("code", SEC_CODE);
732 PARSE_FLAG ("data", SEC_DATA);
733 PARSE_FLAG ("rom", SEC_ROM);
734 PARSE_FLAG ("share", SEC_COFF_SHARED);
735 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
736 PARSE_FLAG ("merge", SEC_MERGE);
737 PARSE_FLAG ("strings", SEC_STRINGS);
743 copy = (char *) xmalloc (len + 1);
744 strncpy (copy, s, len);
746 non_fatal (_("unrecognized section flag `%s'"), copy);
747 fatal (_("supported flags: %s"),
748 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
758 /* Parse symbol flags into a flagword, with a fatal error if the
759 string can't be parsed. */
762 parse_symflags (const char *s, char **other)
772 snext = strchr (s, ',');
781 #define PARSE_FLAG(fname, fval) \
782 else if (len == sizeof fname - 1 \
783 && strncasecmp (fname, s, len) == 0) \
786 #define PARSE_OTHER(fname, fval) \
787 else if (len >= sizeof fname \
788 && strncasecmp (fname, s, sizeof fname - 1) == 0) \
789 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
792 PARSE_FLAG ("local", BSF_LOCAL);
793 PARSE_FLAG ("global", BSF_GLOBAL);
794 PARSE_FLAG ("export", BSF_EXPORT);
795 PARSE_FLAG ("debug", BSF_DEBUGGING);
796 PARSE_FLAG ("function", BSF_FUNCTION);
797 PARSE_FLAG ("weak", BSF_WEAK);
798 PARSE_FLAG ("section", BSF_SECTION_SYM);
799 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
800 PARSE_FLAG ("warning", BSF_WARNING);
801 PARSE_FLAG ("indirect", BSF_INDIRECT);
802 PARSE_FLAG ("file", BSF_FILE);
803 PARSE_FLAG ("object", BSF_OBJECT);
804 PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
805 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
806 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
807 PARSE_OTHER ("before=", *other);
815 copy = (char *) xmalloc (len + 1);
816 strncpy (copy, s, len);
818 non_fatal (_("unrecognized symbol flag `%s'"), copy);
819 fatal (_("supported flags: %s"),
820 "local, global, export, debug, function, weak, section, "
821 "constructor, warning, indirect, file, object, synthetic, "
822 "indirect-function, unique-object, before=<othersym>");
832 /* Find and optionally add an entry in the change_sections list.
834 We need to be careful in how we match section names because of the support
835 for wildcard characters. For example suppose that the user has invoked
838 --set-section-flags .debug_*=debug
839 --set-section-flags .debug_str=readonly,debug
840 --change-section-address .debug_*ranges=0x1000
842 With the idea that all debug sections will receive the DEBUG flag, the
843 .debug_str section will also receive the READONLY flag and the
844 .debug_ranges and .debug_aranges sections will have their address set to
845 0x1000. (This may not make much sense, but it is just an example).
847 When adding the section name patterns to the section list we need to make
848 sure that previous entries do not match with the new entry, unless the
849 match is exact. (In which case we assume that the user is overriding
850 the previous entry with the new context).
852 When matching real section names to the section list we make use of the
853 wildcard characters, but we must do so in context. Eg if we are setting
854 section addresses then we match for .debug_ranges but not for .debug_info.
856 Finally, if ADD is false and we do find a match, we mark the section list
859 static struct section_list *
860 find_section_list (const char *name, bfd_boolean add, unsigned int context)
862 struct section_list *p, *match = NULL;
864 /* assert ((context & ((1 << 7) - 1)) != 0); */
866 for (p = change_sections; p != NULL; p = p->next)
870 if (strcmp (p->pattern, name) == 0)
872 /* Check for context conflicts. */
873 if (((p->context & SECTION_CONTEXT_REMOVE)
874 && (context & SECTION_CONTEXT_COPY))
875 || ((context & SECTION_CONTEXT_REMOVE)
876 && (p->context & SECTION_CONTEXT_COPY)))
877 fatal (_("error: %s both copied and removed"), name);
879 if (((p->context & SECTION_CONTEXT_SET_VMA)
880 && (context & SECTION_CONTEXT_ALTER_VMA))
881 || ((context & SECTION_CONTEXT_SET_VMA)
882 && (context & SECTION_CONTEXT_ALTER_VMA)))
883 fatal (_("error: %s both sets and alters VMA"), name);
885 if (((p->context & SECTION_CONTEXT_SET_LMA)
886 && (context & SECTION_CONTEXT_ALTER_LMA))
887 || ((context & SECTION_CONTEXT_SET_LMA)
888 && (context & SECTION_CONTEXT_ALTER_LMA)))
889 fatal (_("error: %s both sets and alters LMA"), name);
891 /* Extend the context. */
892 p->context |= context;
896 /* If we are not adding a new name/pattern then
897 only check for a match if the context applies. */
898 else if (p->context & context)
900 /* We could check for the presence of wildchar characters
901 first and choose between calling strcmp and fnmatch,
902 but is that really worth it ? */
903 if (p->pattern [0] == '!')
905 if (fnmatch (p->pattern + 1, name, 0) == 0)
913 if (fnmatch (p->pattern, name, 0) == 0)
929 p = (struct section_list *) xmalloc (sizeof (struct section_list));
932 p->context = context;
936 p->next = change_sections;
942 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
945 eq_string (const void *s1, const void *s2)
947 return strcmp ((const char *) s1, (const char *) s2) == 0;
951 create_symbol_htab (void)
953 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
957 create_symbol_htabs (void)
959 strip_specific_htab = create_symbol_htab ();
960 strip_unneeded_htab = create_symbol_htab ();
961 keep_specific_htab = create_symbol_htab ();
962 localize_specific_htab = create_symbol_htab ();
963 globalize_specific_htab = create_symbol_htab ();
964 keepglobal_specific_htab = create_symbol_htab ();
965 weaken_specific_htab = create_symbol_htab ();
968 /* Add a symbol to strip_specific_list. */
971 add_specific_symbol (const char *name, htab_t htab)
973 *htab_find_slot (htab, name, INSERT) = (char *) name;
976 /* Add symbols listed in `filename' to strip_specific_list. */
978 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
979 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
982 add_specific_symbols (const char *filename, htab_t htab)
988 unsigned int line_count;
990 size = get_file_size (filename);
997 buffer = (char *) xmalloc (size + 2);
998 f = fopen (filename, FOPEN_RT);
1000 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
1002 if (fread (buffer, 1, size, f) == 0 || ferror (f))
1003 fatal (_("%s: fread failed"), filename);
1006 buffer [size] = '\n';
1007 buffer [size + 1] = '\0';
1011 for (line = buffer; * line != '\0'; line ++)
1016 int finished = FALSE;
1018 for (eol = line;; eol ++)
1024 /* Cope with \n\r. */
1032 /* Cope with \r\n. */
1043 /* Line comment, Terminate the line here, in case a
1044 name is present and then allow the rest of the
1045 loop to find the real end of the line. */
1057 /* A name may now exist somewhere between 'line' and 'eol'.
1058 Strip off leading whitespace and trailing whitespace,
1059 then add it to the list. */
1060 for (name = line; IS_WHITESPACE (* name); name ++)
1062 for (name_end = name;
1063 (! IS_WHITESPACE (* name_end))
1064 && (! IS_LINE_TERMINATOR (* name_end));
1068 if (! IS_LINE_TERMINATOR (* name_end))
1072 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
1075 if (! IS_LINE_TERMINATOR (* extra))
1076 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1077 filename, line_count);
1082 if (name_end > name)
1083 add_specific_symbol (name, htab);
1085 /* Advance line pointer to end of line. The 'eol ++' in the for
1086 loop above will then advance us to the start of the next line. */
1092 /* See whether a symbol should be stripped or kept
1093 based on strip_specific_list and keep_symbols. */
1096 is_specified_symbol_predicate (void **slot, void *data)
1098 struct is_specified_symbol_predicate_data *d =
1099 (struct is_specified_symbol_predicate_data *) data;
1100 const char *slot_name = (char *) *slot;
1102 if (*slot_name != '!')
1104 if (! fnmatch (slot_name, d->name, 0))
1107 /* Continue traversal, there might be a non-match rule. */
1113 if (! fnmatch (slot_name + 1, d->name, 0))
1116 /* Stop traversal. */
1121 /* Continue traversal. */
1126 is_specified_symbol (const char *name, htab_t htab)
1130 struct is_specified_symbol_predicate_data data;
1135 htab_traverse (htab, is_specified_symbol_predicate, &data);
1140 return htab_find (htab, name) != NULL;
1143 /* Return a pointer to the symbol used as a signature for GROUP. */
1146 group_signature (asection *group)
1148 bfd *abfd = group->owner;
1149 Elf_Internal_Shdr *ghdr;
1151 /* PR 20089: An earlier error may have prevented us from loading the symbol table. */
1155 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1158 ghdr = &elf_section_data (group)->this_hdr;
1159 if (ghdr->sh_link < elf_numsections (abfd))
1161 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1162 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
1164 if (symhdr->sh_type == SHT_SYMTAB
1165 && ghdr->sh_info > 0
1166 && ghdr->sh_info < (symhdr->sh_size / bed->s->sizeof_sym))
1167 return isympp[ghdr->sh_info - 1];
1172 /* Return TRUE if the section is a DWO section. */
1175 is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1177 const char *name = bfd_get_section_name (abfd, sec);
1178 int len = strlen (name);
1180 return strncmp (name + len - 4, ".dwo", 4) == 0;
1183 /* Return TRUE if section SEC is in the update list. */
1186 is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1188 if (update_sections != NULL)
1190 struct section_add *pupdate;
1192 for (pupdate = update_sections;
1194 pupdate = pupdate->next)
1196 if (strcmp (sec->name, pupdate->name) == 0)
1204 /* See if a non-group section is being removed. */
1207 is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1209 if (sections_removed || sections_copied)
1211 struct section_list *p;
1212 struct section_list *q;
1214 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1215 SECTION_CONTEXT_REMOVE);
1216 q = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1217 SECTION_CONTEXT_COPY);
1220 fatal (_("error: section %s matches both remove and copy options"),
1221 bfd_get_section_name (abfd, sec));
1222 if (p && is_update_section (abfd, sec))
1223 fatal (_("error: section %s matches both update and remove options"),
1224 bfd_get_section_name (abfd, sec));
1228 if (sections_copied && q == NULL)
1232 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
1234 if (strip_symbols == STRIP_DEBUG
1235 || strip_symbols == STRIP_UNNEEDED
1236 || strip_symbols == STRIP_ALL
1237 || discard_locals == LOCALS_ALL
1238 || convert_debugging)
1240 /* By default we don't want to strip .reloc section.
1241 This section has for pe-coff special meaning. See
1242 pe-dll.c file in ld, and peXXigen.c in bfd for details. */
1243 if (strcmp (bfd_get_section_name (abfd, sec), ".reloc") != 0)
1247 if (strip_symbols == STRIP_DWO)
1248 return is_dwo_section (abfd, sec);
1250 if (strip_symbols == STRIP_NONDEBUG)
1254 if (strip_symbols == STRIP_NONDWO)
1255 return !is_dwo_section (abfd, sec);
1260 /* See if a section is being removed. */
1263 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1265 if (is_strip_section_1 (abfd, sec))
1268 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
1272 asection *elt, *first;
1275 If we are going to strip the group signature symbol, then
1276 strip the group section too. */
1277 gsym = group_signature (sec);
1282 if ((strip_symbols == STRIP_ALL
1283 && !is_specified_symbol (gname, keep_specific_htab))
1284 || is_specified_symbol (gname, strip_specific_htab))
1287 /* Remove the group section if all members are removed. */
1288 first = elt = elf_next_in_group (sec);
1291 if (!is_strip_section_1 (abfd, elt))
1293 elt = elf_next_in_group (elt);
1305 is_nondebug_keep_contents_section (bfd *ibfd, asection *isection)
1307 /* Always keep ELF note sections. */
1308 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
1309 return (elf_section_type (isection) == SHT_NOTE);
1311 /* Always keep the .buildid section for PE/COFF.
1313 Strictly, this should be written "always keep the section storing the debug
1314 directory", but that may be the .text section for objects produced by some
1315 tools, which it is not sensible to keep. */
1316 if (ibfd->xvec->flavour == bfd_target_coff_flavour)
1317 return (strcmp (bfd_get_section_name (ibfd, isection), ".buildid") == 0);
1322 /* Return true if SYM is a hidden symbol. */
1325 is_hidden_symbol (asymbol *sym)
1327 elf_symbol_type *elf_sym;
1329 elf_sym = elf_symbol_from (sym->the_bfd, sym);
1330 if (elf_sym != NULL)
1331 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
1341 need_sym_before (struct addsym_node **node, const char *sym)
1344 struct addsym_node *ptr = add_sym_list;
1346 /* 'othersym' symbols are at the front of the list. */
1347 for (count = 0; count < add_symbols; count++)
1351 else if (strcmp (ptr->othersym, sym) == 0)
1353 free (ptr->othersym);
1354 ptr->othersym = ""; /* Empty name is hopefully never a valid symbol name. */
1364 create_new_symbol (struct addsym_node *ptr, bfd *obfd)
1366 asymbol *sym = bfd_make_empty_symbol (obfd);
1368 bfd_asymbol_name (sym) = ptr->symdef;
1369 sym->value = ptr->symval;
1370 sym->flags = ptr->flags;
1373 asection *sec = bfd_get_section_by_name (obfd, ptr->section);
1375 fatal (_("Section %s not found"), ptr->section);
1379 sym->section = bfd_abs_section_ptr;
1383 /* Choose which symbol entries to copy; put the result in OSYMS.
1384 We don't copy in place, because that confuses the relocs.
1385 Return the number of symbols to print. */
1388 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
1389 asymbol **isyms, long symcount)
1391 asymbol **from = isyms, **to = osyms;
1392 long src_count = 0, dst_count = 0;
1393 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1395 for (; src_count < symcount; src_count++)
1397 asymbol *sym = from[src_count];
1398 flagword flags = sym->flags;
1399 char *name = (char *) bfd_asymbol_name (sym);
1401 bfd_boolean used_in_reloc = FALSE;
1402 bfd_boolean undefined;
1403 bfd_boolean rem_leading_char;
1404 bfd_boolean add_leading_char;
1406 undefined = bfd_is_und_section (bfd_get_section (sym));
1410 struct addsym_node *ptr;
1412 if (need_sym_before (&ptr, name))
1413 to[dst_count++] = create_new_symbol (ptr, obfd);
1416 if (redefine_sym_list || section_rename_list)
1420 new_name = (char *) lookup_sym_redefinition (name);
1421 if (new_name == name
1422 && (flags & BSF_SECTION_SYM) != 0)
1423 new_name = (char *) find_section_rename (name, NULL);
1424 bfd_asymbol_name (sym) = new_name;
1428 /* Check if we will remove the current leading character. */
1430 (name[0] == bfd_get_symbol_leading_char (abfd))
1431 && (change_leading_char
1432 || (remove_leading_char
1433 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1435 || bfd_is_com_section (bfd_get_section (sym)))));
1437 /* Check if we will add a new leading character. */
1440 && (bfd_get_symbol_leading_char (obfd) != '\0')
1441 && (bfd_get_symbol_leading_char (abfd) == '\0'
1442 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1444 /* Short circuit for change_leading_char if we can do it in-place. */
1445 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1447 name[0] = bfd_get_symbol_leading_char (obfd);
1448 bfd_asymbol_name (sym) = name;
1449 rem_leading_char = FALSE;
1450 add_leading_char = FALSE;
1453 /* Remove leading char. */
1454 if (rem_leading_char)
1455 bfd_asymbol_name (sym) = ++name;
1457 /* Add new leading char and/or prefix. */
1458 if (add_leading_char || prefix_symbols_string)
1462 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1463 + strlen (name) + 1);
1464 if (add_leading_char)
1465 *ptr++ = bfd_get_symbol_leading_char (obfd);
1467 if (prefix_symbols_string)
1469 strcpy (ptr, prefix_symbols_string);
1470 ptr += strlen (prefix_symbols_string);
1474 bfd_asymbol_name (sym) = n;
1478 if (strip_symbols == STRIP_ALL)
1480 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1481 || ((flags & BSF_SECTION_SYM) != 0
1482 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1486 used_in_reloc = TRUE;
1488 else if (relocatable /* Relocatable file. */
1489 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1490 || bfd_is_com_section (bfd_get_section (sym))))
1492 else if (bfd_decode_symclass (sym) == 'I')
1493 /* Global symbols in $idata sections need to be retained
1494 even if relocatable is FALSE. External users of the
1495 library containing the $idata section may reference these
1498 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1499 || (flags & BSF_WEAK) != 0
1501 || bfd_is_com_section (bfd_get_section (sym)))
1502 keep = strip_symbols != STRIP_UNNEEDED;
1503 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1504 keep = (strip_symbols != STRIP_DEBUG
1505 && strip_symbols != STRIP_UNNEEDED
1506 && ! convert_debugging);
1507 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1508 /* COMDAT sections store special information in local
1509 symbols, so we cannot risk stripping any of them. */
1511 else /* Local symbol. */
1512 keep = (strip_symbols != STRIP_UNNEEDED
1513 && (discard_locals != LOCALS_ALL
1514 && (discard_locals != LOCALS_START_L
1515 || ! bfd_is_local_label (abfd, sym))));
1517 if (keep && is_specified_symbol (name, strip_specific_htab))
1519 /* There are multiple ways to set 'keep' above, but if it
1520 was the relocatable symbol case, then that's an error. */
1523 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1531 && !(flags & BSF_KEEP)
1532 && is_specified_symbol (name, strip_unneeded_htab))
1536 && ((keep_file_symbols && (flags & BSF_FILE))
1537 || is_specified_symbol (name, keep_specific_htab)))
1540 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1545 if ((flags & BSF_GLOBAL) != 0
1546 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1548 sym->flags &= ~ BSF_GLOBAL;
1549 sym->flags |= BSF_WEAK;
1553 && (flags & (BSF_GLOBAL | BSF_WEAK))
1554 && (is_specified_symbol (name, localize_specific_htab)
1555 || (htab_elements (keepglobal_specific_htab) != 0
1556 && ! is_specified_symbol (name, keepglobal_specific_htab))
1557 || (localize_hidden && is_hidden_symbol (sym))))
1559 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1560 sym->flags |= BSF_LOCAL;
1564 && (flags & BSF_LOCAL)
1565 && is_specified_symbol (name, globalize_specific_htab))
1567 sym->flags &= ~ BSF_LOCAL;
1568 sym->flags |= BSF_GLOBAL;
1571 to[dst_count++] = sym;
1576 struct addsym_node *ptr = add_sym_list;
1578 for (src_count = 0; src_count < add_symbols; src_count++)
1582 if (strcmp (ptr->othersym, ""))
1583 fatal (_("'before=%s' not found"), ptr->othersym);
1586 to[dst_count++] = create_new_symbol (ptr, obfd);
1592 to[dst_count] = NULL;
1597 /* Find the redefined name of symbol SOURCE. */
1600 lookup_sym_redefinition (const char *source)
1602 struct redefine_node *list;
1604 for (list = redefine_sym_list; list != NULL; list = list->next)
1605 if (strcmp (source, list->source) == 0)
1606 return list->target;
1611 /* Add a node to a symbol redefine list. */
1614 redefine_list_append (const char *cause, const char *source, const char *target)
1616 struct redefine_node **p;
1617 struct redefine_node *list;
1618 struct redefine_node *new_node;
1620 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1622 if (strcmp (source, list->source) == 0)
1623 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1626 if (strcmp (target, list->target) == 0)
1627 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1631 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1633 new_node->source = strdup (source);
1634 new_node->target = strdup (target);
1635 new_node->next = NULL;
1640 /* Handle the --redefine-syms option. Read lines containing "old new"
1641 from the file, and add them to the symbol redefine list. */
1644 add_redefine_syms_file (const char *filename)
1653 file = fopen (filename, "r");
1655 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1656 filename, strerror (errno));
1659 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
1667 /* Collect the input symbol name. */
1668 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1676 buf = (char *) xrealloc (buf, bufsize + 1);
1684 /* Eat white space between the symbol names. */
1685 while (IS_WHITESPACE (c))
1687 if (c == '#' || IS_LINE_TERMINATOR (c))
1692 /* Collect the output symbol name. */
1694 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1702 buf = (char *) xrealloc (buf, bufsize + 1);
1710 /* Eat white space at end of line. */
1711 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1716 if ((c == '\r' && (c = getc (file)) == '\n')
1717 || c == '\n' || c == EOF)
1720 /* Append the redefinition to the list. */
1722 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1733 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1735 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1736 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1739 /* Eat the rest of the line and finish it. */
1740 while (c != '\n' && c != EOF)
1746 fatal (_("%s:%d: premature end of file"), filename, lineno);
1751 /* Copy unknown object file IBFD onto OBFD.
1752 Returns TRUE upon success, FALSE otherwise. */
1755 copy_unknown_object (bfd *ibfd, bfd *obfd)
1763 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1765 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1772 non_fatal (_("stat returns negative size for `%s'"),
1773 bfd_get_archive_filename (ibfd));
1777 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1779 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1784 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1785 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1787 cbuf = (char *) xmalloc (BUFSIZE);
1789 while (ncopied < size)
1791 tocopy = size - ncopied;
1792 if (tocopy > BUFSIZE)
1795 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1796 != (bfd_size_type) tocopy)
1798 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1803 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1804 != (bfd_size_type) tocopy)
1806 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1814 /* We should at least to be able to read it back when copying an
1815 unknown object in an archive. */
1816 chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
1821 /* Copy object file IBFD onto OBFD.
1822 Returns TRUE upon success, FALSE otherwise. */
1825 copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
1829 asection **osections = NULL;
1830 asection *gnu_debuglink_section = NULL;
1831 bfd_size_type *gaps = NULL;
1832 bfd_size_type max_gap = 0;
1835 enum bfd_architecture iarch;
1839 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1840 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1841 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1843 /* PR 17636: Call non-fatal so that we return to our parent who
1844 may need to tidy temporary files. */
1845 non_fatal (_("Unable to change endianness of input file(s)"));
1849 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1851 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1855 if (ibfd->sections == NULL)
1857 non_fatal (_("error: the input file '%s' has no sections"),
1858 bfd_get_archive_filename (ibfd));
1862 if (ibfd->xvec->flavour != bfd_target_elf_flavour)
1864 if ((do_debug_sections & compress) != 0
1865 && do_debug_sections != compress)
1867 non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
1868 bfd_get_archive_filename (ibfd));
1872 if (do_elf_stt_common)
1874 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
1875 bfd_get_archive_filename (ibfd));
1881 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1882 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1883 bfd_get_filename (obfd), bfd_get_target (obfd));
1892 start = bfd_get_start_address (ibfd);
1893 start += change_start;
1896 /* Neither the start address nor the flags
1897 need to be set for a core file. */
1898 if (bfd_get_format (obfd) != bfd_core)
1902 flags = bfd_get_file_flags (ibfd);
1903 flags |= bfd_flags_to_set;
1904 flags &= ~bfd_flags_to_clear;
1905 flags &= bfd_applicable_file_flags (obfd);
1907 if (strip_symbols == STRIP_ALL)
1908 flags &= ~HAS_RELOC;
1910 if (!bfd_set_start_address (obfd, start)
1911 || !bfd_set_file_flags (obfd, flags))
1913 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1918 /* Copy architecture of input file to output file. */
1919 iarch = bfd_get_arch (ibfd);
1920 imach = bfd_get_mach (ibfd);
1923 if (bfd_get_arch_info (ibfd) == NULL
1924 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
1926 iarch = input_arch->arch;
1927 imach = input_arch->mach;
1930 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1931 bfd_get_archive_filename (ibfd));
1933 if (!bfd_set_arch_mach (obfd, iarch, imach)
1934 && (ibfd->target_defaulted
1935 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1937 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1938 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1939 bfd_get_archive_filename (ibfd));
1941 non_fatal (_("Output file cannot represent architecture `%s'"),
1942 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1943 bfd_get_mach (ibfd)));
1947 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1949 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1953 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1954 && bfd_pei_p (obfd))
1956 /* Set up PE parameters. */
1957 pe_data_type *pe = pe_data (obfd);
1959 /* Copy PE parameters before changing them. */
1960 if (ibfd->xvec->flavour == bfd_target_coff_flavour
1961 && bfd_pei_p (ibfd))
1962 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1964 if (pe_file_alignment != (bfd_vma) -1)
1965 pe->pe_opthdr.FileAlignment = pe_file_alignment;
1967 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
1969 if (pe_heap_commit != (bfd_vma) -1)
1970 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
1972 if (pe_heap_reserve != (bfd_vma) -1)
1973 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
1975 if (pe_image_base != (bfd_vma) -1)
1976 pe->pe_opthdr.ImageBase = pe_image_base;
1978 if (pe_section_alignment != (bfd_vma) -1)
1979 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
1981 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
1983 if (pe_stack_commit != (bfd_vma) -1)
1984 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
1986 if (pe_stack_reserve != (bfd_vma) -1)
1987 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
1989 if (pe_subsystem != -1)
1990 pe->pe_opthdr.Subsystem = pe_subsystem;
1992 if (pe_major_subsystem_version != -1)
1993 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
1995 if (pe_minor_subsystem_version != -1)
1996 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
1998 if (pe_file_alignment > pe_section_alignment)
2000 char file_alignment[20], section_alignment[20];
2002 sprintf_vma (file_alignment, pe_file_alignment);
2003 sprintf_vma (section_alignment, pe_section_alignment);
2004 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
2006 file_alignment, section_alignment);
2013 if (osympp != isympp)
2019 symsize = bfd_get_symtab_upper_bound (ibfd);
2022 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2026 osympp = isympp = (asymbol **) xmalloc (symsize);
2027 symcount = bfd_canonicalize_symtab (ibfd, isympp);
2030 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2033 /* PR 17512: file: d6323821
2034 If the symbol table could not be loaded do not pretend that we have
2035 any symbols. This trips us up later on when we load the relocs. */
2039 osympp = isympp = NULL;
2042 /* BFD mandates that all output sections be created and sizes set before
2043 any output is done. Thus, we traverse all sections multiple times. */
2044 bfd_map_over_sections (ibfd, setup_section, obfd);
2046 if (!extract_symbol)
2047 setup_bfd_headers (ibfd, obfd);
2049 if (add_sections != NULL)
2051 struct section_add *padd;
2052 struct section_list *pset;
2054 for (padd = add_sections; padd != NULL; padd = padd->next)
2058 pset = find_section_list (padd->name, FALSE,
2059 SECTION_CONTEXT_SET_FLAGS);
2061 flags = pset->flags | SEC_HAS_CONTENTS;
2063 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
2065 /* bfd_make_section_with_flags() does not return very helpful
2066 error codes, so check for the most likely user error first. */
2067 if (bfd_get_section_by_name (obfd, padd->name))
2069 bfd_nonfatal_message (NULL, obfd, NULL,
2070 _("can't add section '%s'"), padd->name);
2075 /* We use LINKER_CREATED here so that the backend hooks
2076 will create any special section type information,
2077 instead of presuming we know what we're doing merely
2078 because we set the flags. */
2079 padd->section = bfd_make_section_with_flags
2080 (obfd, padd->name, flags | SEC_LINKER_CREATED);
2081 if (padd->section == NULL)
2083 bfd_nonfatal_message (NULL, obfd, NULL,
2084 _("can't create section `%s'"),
2090 if (! bfd_set_section_size (obfd, padd->section, padd->size))
2092 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2096 pset = find_section_list (padd->name, FALSE,
2097 SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
2099 && ! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
2101 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2105 pset = find_section_list (padd->name, FALSE,
2106 SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
2109 padd->section->lma = pset->lma_val;
2111 if (! bfd_set_section_alignment
2112 (obfd, padd->section,
2113 bfd_section_alignment (obfd, padd->section)))
2115 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2122 if (update_sections != NULL)
2124 struct section_add *pupdate;
2126 for (pupdate = update_sections;
2128 pupdate = pupdate->next)
2132 pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name);
2133 if (pupdate->section == NULL)
2135 non_fatal (_("error: %s not found, can't be updated"), pupdate->name);
2139 osec = pupdate->section->output_section;
2140 if (! bfd_set_section_size (obfd, osec, pupdate->size))
2142 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2148 if (dump_sections != NULL)
2150 struct section_add * pdump;
2152 for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
2156 sec = bfd_get_section_by_name (ibfd, pdump->name);
2159 bfd_nonfatal_message (NULL, ibfd, NULL,
2160 _("can't dump section '%s' - it does not exist"),
2165 if ((bfd_get_section_flags (ibfd, sec) & SEC_HAS_CONTENTS) == 0)
2167 bfd_nonfatal_message (NULL, ibfd, sec,
2168 _("can't dump section - it has no contents"));
2172 bfd_size_type size = bfd_get_section_size (sec);
2175 bfd_nonfatal_message (NULL, ibfd, sec,
2176 _("can't dump section - it is empty"));
2181 f = fopen (pdump->filename, FOPEN_WB);
2184 bfd_nonfatal_message (pdump->filename, NULL, NULL,
2185 _("could not open section dump file"));
2189 bfd_byte * contents = xmalloc (size);
2190 if (bfd_get_section_contents (ibfd, sec, contents, 0, size))
2192 if (fwrite (contents, 1, size, f) != size)
2194 non_fatal (_("error writing section contents to %s (error: %s)"),
2201 bfd_nonfatal_message (NULL, ibfd, sec,
2202 _("could not retrieve section contents"));
2209 if (gnu_debuglink_filename != NULL)
2211 /* PR 15125: Give a helpful warning message if
2212 the debuglink section already exists, and
2213 allow the rest of the copy to complete. */
2214 if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
2216 non_fatal (_("%s: debuglink section already exists"),
2217 bfd_get_filename (obfd));
2218 gnu_debuglink_filename = NULL;
2222 gnu_debuglink_section = bfd_create_gnu_debuglink_section
2223 (obfd, gnu_debuglink_filename);
2225 if (gnu_debuglink_section == NULL)
2227 bfd_nonfatal_message (NULL, obfd, NULL,
2228 _("cannot create debug link section `%s'"),
2229 gnu_debuglink_filename);
2233 /* Special processing for PE format files. We
2234 have no way to distinguish PE from COFF here. */
2235 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
2237 bfd_vma debuglink_vma;
2238 asection * highest_section;
2241 /* The PE spec requires that all sections be adjacent and sorted
2242 in ascending order of VMA. It also specifies that debug
2243 sections should be last. This is despite the fact that debug
2244 sections are not loaded into memory and so in theory have no
2247 This means that the debuglink section must be given a non-zero
2248 VMA which makes it contiguous with other debug sections. So
2249 walk the current section list, find the section with the
2250 highest VMA and start the debuglink section after that one. */
2251 for (sec = obfd->sections, highest_section = NULL;
2255 && (highest_section == NULL
2256 || sec->vma > highest_section->vma))
2257 highest_section = sec;
2259 if (highest_section)
2260 debuglink_vma = BFD_ALIGN (highest_section->vma
2261 + highest_section->size,
2262 /* FIXME: We ought to be using
2263 COFF_PAGE_SIZE here or maybe
2264 bfd_get_section_alignment() (if it
2265 was set) but since this is for PE
2266 and we know the required alignment
2267 it is easier just to hard code it. */
2270 /* Umm, not sure what to do in this case. */
2271 debuglink_vma = 0x1000;
2273 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
2278 c = bfd_count_sections (obfd);
2280 && (gap_fill_set || pad_to_set))
2284 /* We must fill in gaps between the sections and/or we must pad
2285 the last section to a specified address. We do this by
2286 grabbing a list of the sections, sorting them by VMA, and
2287 increasing the section sizes as required to fill the gaps.
2288 We write out the gap contents below. */
2290 osections = (asection **) xmalloc (c * sizeof (asection *));
2292 bfd_map_over_sections (obfd, get_sections, &set);
2294 qsort (osections, c, sizeof (asection *), compare_section_lma);
2296 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
2297 memset (gaps, 0, c * sizeof (bfd_size_type));
2301 for (i = 0; i < c - 1; i++)
2305 bfd_vma gap_start, gap_stop;
2307 flags = bfd_get_section_flags (obfd, osections[i]);
2308 if ((flags & SEC_HAS_CONTENTS) == 0
2309 || (flags & SEC_LOAD) == 0)
2312 size = bfd_section_size (obfd, osections[i]);
2313 gap_start = bfd_section_lma (obfd, osections[i]) + size;
2314 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
2315 if (gap_start < gap_stop)
2317 if (! bfd_set_section_size (obfd, osections[i],
2318 size + (gap_stop - gap_start)))
2320 bfd_nonfatal_message (NULL, obfd, osections[i],
2321 _("Can't fill gap after section"));
2325 gaps[i] = gap_stop - gap_start;
2326 if (max_gap < gap_stop - gap_start)
2327 max_gap = gap_stop - gap_start;
2337 lma = bfd_section_lma (obfd, osections[c - 1]);
2338 size = bfd_section_size (obfd, osections[c - 1]);
2339 if (lma + size < pad_to)
2341 if (! bfd_set_section_size (obfd, osections[c - 1],
2344 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
2345 _("can't add padding"));
2350 gaps[c - 1] = pad_to - (lma + size);
2351 if (max_gap < pad_to - (lma + size))
2352 max_gap = pad_to - (lma + size);
2358 /* Symbol filtering must happen after the output sections
2359 have been created, but before their contents are set. */
2361 if (convert_debugging)
2362 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
2364 if (strip_symbols == STRIP_DEBUG
2365 || strip_symbols == STRIP_ALL
2366 || strip_symbols == STRIP_UNNEEDED
2367 || strip_symbols == STRIP_NONDEBUG
2368 || strip_symbols == STRIP_DWO
2369 || strip_symbols == STRIP_NONDWO
2370 || discard_locals != LOCALS_UNDEF
2372 || htab_elements (strip_specific_htab) != 0
2373 || htab_elements (keep_specific_htab) != 0
2374 || htab_elements (localize_specific_htab) != 0
2375 || htab_elements (globalize_specific_htab) != 0
2376 || htab_elements (keepglobal_specific_htab) != 0
2377 || htab_elements (weaken_specific_htab) != 0
2378 || prefix_symbols_string
2381 || convert_debugging
2382 || change_leading_char
2383 || remove_leading_char
2384 || redefine_sym_list
2385 || section_rename_list
2389 /* Mark symbols used in output relocations so that they
2390 are kept, even if they are local labels or static symbols.
2392 Note we iterate over the input sections examining their
2393 relocations since the relocations for the output sections
2394 haven't been set yet. mark_symbols_used_in_relocations will
2395 ignore input sections which have no corresponding output
2397 if (strip_symbols != STRIP_ALL)
2398 bfd_map_over_sections (ibfd,
2399 mark_symbols_used_in_relocations,
2401 osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *));
2402 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
2405 if (convert_debugging && dhandle != NULL)
2407 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
2414 bfd_set_symtab (obfd, osympp, symcount);
2416 /* This has to happen before section positions are set. */
2417 bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
2419 /* This has to happen after the symbol table has been set. */
2420 bfd_map_over_sections (ibfd, copy_section, obfd);
2422 if (add_sections != NULL)
2424 struct section_add *padd;
2426 for (padd = add_sections; padd != NULL; padd = padd->next)
2428 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
2431 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2437 if (update_sections != NULL)
2439 struct section_add *pupdate;
2441 for (pupdate = update_sections;
2443 pupdate = pupdate->next)
2447 osec = pupdate->section->output_section;
2448 if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
2451 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2457 if (gnu_debuglink_filename != NULL)
2459 if (! bfd_fill_in_gnu_debuglink_section
2460 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
2462 bfd_nonfatal_message (NULL, obfd, NULL,
2463 _("cannot fill debug link section `%s'"),
2464 gnu_debuglink_filename);
2469 if (gap_fill_set || pad_to_set)
2473 /* Fill in the gaps. */
2476 buf = (bfd_byte *) xmalloc (max_gap);
2477 memset (buf, gap_fill, max_gap);
2479 c = bfd_count_sections (obfd);
2480 for (i = 0; i < c; i++)
2488 off = bfd_section_size (obfd, osections[i]) - left;
2499 if (! bfd_set_section_contents (obfd, osections[i], buf,
2502 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
2513 /* Allow the BFD backend to copy any private data it understands
2514 from the input BFD to the output BFD. This is done last to
2515 permit the routine to look at the filtered symbol table, which is
2516 important for the ECOFF code at least. */
2517 if (! bfd_copy_private_bfd_data (ibfd, obfd))
2519 bfd_nonfatal_message (NULL, obfd, NULL,
2520 _("error copying private BFD data"));
2524 /* Switch to the alternate machine code. We have to do this at the
2525 very end, because we only initialize the header when we create
2526 the first section. */
2527 if (use_alt_mach_code != 0)
2529 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
2531 non_fatal (_("this target does not support %lu alternative machine codes"),
2533 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2535 non_fatal (_("treating that number as an absolute e_machine value instead"));
2536 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
2539 non_fatal (_("ignoring the alternative value"));
2546 /* Read each archive element in turn from IBFD, copy the
2547 contents to temp file, and keep the temp file handle.
2548 If 'force_output_target' is TRUE then make sure that
2549 all elements in the new archive are of the type
2553 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
2554 bfd_boolean force_output_target,
2555 const bfd_arch_info_type *input_arch)
2559 struct name_list *next;
2563 bfd **ptr = &obfd->archive_head;
2566 const char *filename;
2568 /* Make a temp directory to hold the contents. */
2569 dir = make_tempdir (bfd_get_filename (obfd));
2571 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2574 if (strip_symbols == STRIP_ALL)
2575 obfd->has_armap = FALSE;
2577 obfd->has_armap = ibfd->has_armap;
2578 obfd->is_thin_archive = ibfd->is_thin_archive;
2581 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2585 this_element = bfd_openr_next_archived_file (ibfd, NULL);
2587 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2590 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
2591 goto cleanup_and_exit;
2594 while (!status && this_element != NULL)
2600 int stat_status = 0;
2601 bfd_boolean del = TRUE;
2602 bfd_boolean ok_object;
2604 /* PR binutils/17533: Do not allow directory traversal
2605 outside of the current directory tree by archive members. */
2606 if (! is_valid_archive_path (bfd_get_filename (this_element)))
2608 non_fatal (_("illegal pathname found in archive member: %s"),
2609 bfd_get_filename (this_element));
2611 goto cleanup_and_exit;
2614 /* Create an output file for this member. */
2615 output_name = concat (dir, "/",
2616 bfd_get_filename (this_element), (char *) 0);
2618 /* If the file already exists, make another temp dir. */
2619 if (stat (output_name, &buf) >= 0)
2621 output_name = make_tempdir (output_name);
2622 if (output_name == NULL)
2624 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
2627 goto cleanup_and_exit;
2630 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2631 l->name = output_name;
2635 output_name = concat (output_name, "/",
2636 bfd_get_filename (this_element), (char *) 0);
2641 stat_status = bfd_stat_arch_elt (this_element, &buf);
2643 if (stat_status != 0)
2644 non_fatal (_("internal stat error on %s"),
2645 bfd_get_filename (this_element));
2648 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2649 l->name = output_name;
2654 ok_object = bfd_check_format (this_element, bfd_object);
2656 bfd_nonfatal_message (NULL, this_element, NULL,
2657 _("Unable to recognise the format of file"));
2659 /* PR binutils/3110: Cope with archives
2660 containing multiple target types. */
2661 if (force_output_target || !ok_object)
2662 output_bfd = bfd_openw (output_name, output_target);
2664 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
2666 if (output_bfd == NULL)
2668 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2670 goto cleanup_and_exit;
2675 del = !copy_object (this_element, output_bfd, input_arch);
2677 if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
2678 /* Try again as an unknown object file. */
2680 else if (!bfd_close (output_bfd))
2682 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2683 /* Error in new object file. Don't change archive. */
2690 del = !copy_unknown_object (this_element, output_bfd);
2691 if (!bfd_close_all_done (output_bfd))
2693 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2694 /* Error in new object file. Don't change archive. */
2701 unlink (output_name);
2706 if (preserve_dates && stat_status == 0)
2707 set_times (output_name, &buf);
2709 /* Open the newly output file and attach to our list. */
2710 output_bfd = bfd_openr (output_name, output_target);
2712 l->obfd = output_bfd;
2715 ptr = &output_bfd->archive_next;
2717 last_element = this_element;
2719 this_element = bfd_openr_next_archived_file (ibfd, last_element);
2721 bfd_close (last_element);
2726 filename = bfd_get_filename (obfd);
2727 if (!bfd_close (obfd))
2730 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2733 filename = bfd_get_filename (ibfd);
2734 if (!bfd_close (ibfd))
2737 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2741 /* Delete all the files that we opened. */
2742 for (l = list; l != NULL; l = l->next)
2744 if (l->obfd == NULL)
2748 bfd_close (l->obfd);
2757 set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
2759 /* This is only relevant to Coff targets. */
2760 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
2763 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
2764 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
2765 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
2769 /* The top-level control. */
2772 copy_file (const char *input_filename, const char *output_filename,
2773 const char *input_target, const char *output_target,
2774 const bfd_arch_info_type *input_arch)
2777 char **obj_matching;
2778 char **core_matching;
2779 off_t size = get_file_size (input_filename);
2784 non_fatal (_("error: the input file '%s' is empty"),
2790 /* To allow us to do "strip *" without dying on the first
2791 non-object file, failures are nonfatal. */
2792 ibfd = bfd_openr (input_filename, input_target);
2795 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2800 switch (do_debug_sections)
2804 case compress_gnu_zlib:
2805 case compress_gabi_zlib:
2806 ibfd->flags |= BFD_COMPRESS;
2807 /* Don't check if input is ELF here since this information is
2808 only available after bfd_check_format_matches is called. */
2809 if (do_debug_sections != compress_gnu_zlib)
2810 ibfd->flags |= BFD_COMPRESS_GABI;
2813 ibfd->flags |= BFD_DECOMPRESS;
2819 switch (do_elf_stt_common)
2821 case elf_stt_common:
2822 ibfd->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
2825 case no_elf_stt_common:
2826 ibfd->flags |= BFD_CONVERT_ELF_COMMON;
2832 if (bfd_check_format (ibfd, bfd_archive))
2834 bfd_boolean force_output_target;
2837 /* bfd_get_target does not return the correct value until
2838 bfd_check_format succeeds. */
2839 if (output_target == NULL)
2841 output_target = bfd_get_target (ibfd);
2842 force_output_target = FALSE;
2845 force_output_target = TRUE;
2847 obfd = bfd_openw (output_filename, output_target);
2850 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2854 /* This is a no-op on non-Coff targets. */
2855 set_long_section_mode (obfd, ibfd, long_section_names);
2857 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
2859 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2864 /* bfd_get_target does not return the correct value until
2865 bfd_check_format succeeds. */
2866 if (output_target == NULL)
2867 output_target = bfd_get_target (ibfd);
2869 obfd = bfd_openw (output_filename, output_target);
2872 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2876 /* This is a no-op on non-Coff targets. */
2877 set_long_section_mode (obfd, ibfd, long_section_names);
2879 if (! copy_object (ibfd, obfd, input_arch))
2882 /* PR 17512: file: 0f15796a.
2883 If the file could not be copied it may not be in a writeable
2884 state. So use bfd_close_all_done to avoid the possibility of
2885 writing uninitialised data into the file. */
2886 if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd)))
2889 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2893 if (!bfd_close (ibfd))
2896 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2902 bfd_error_type obj_error = bfd_get_error ();
2903 bfd_error_type core_error;
2905 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2907 /* This probably can't happen.. */
2908 if (obj_error == bfd_error_file_ambiguously_recognized)
2909 free (obj_matching);
2913 core_error = bfd_get_error ();
2914 /* Report the object error in preference to the core error. */
2915 if (obj_error != core_error)
2916 bfd_set_error (obj_error);
2918 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2920 if (obj_error == bfd_error_file_ambiguously_recognized)
2922 list_matching_formats (obj_matching);
2923 free (obj_matching);
2925 if (core_error == bfd_error_file_ambiguously_recognized)
2927 list_matching_formats (core_matching);
2928 free (core_matching);
2935 /* Add a name to the section renaming list. */
2938 add_section_rename (const char * old_name, const char * new_name,
2941 section_rename * srename;
2943 /* Check for conflicts first. */
2944 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2945 if (strcmp (srename->old_name, old_name) == 0)
2947 /* Silently ignore duplicate definitions. */
2948 if (strcmp (srename->new_name, new_name) == 0
2949 && srename->flags == flags)
2952 fatal (_("Multiple renames of section %s"), old_name);
2955 srename = (section_rename *) xmalloc (sizeof (* srename));
2957 srename->old_name = old_name;
2958 srename->new_name = new_name;
2959 srename->flags = flags;
2960 srename->next = section_rename_list;
2962 section_rename_list = srename;
2965 /* Check the section rename list for a new name of the input section
2966 called OLD_NAME. Returns the new name if one is found and sets
2967 RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
2970 find_section_rename (const char *old_name, flagword *returned_flags)
2972 const section_rename *srename;
2974 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2975 if (strcmp (srename->old_name, old_name) == 0)
2977 if (returned_flags != NULL && srename->flags != (flagword) -1)
2978 *returned_flags = srename->flags;
2980 return srename->new_name;
2986 /* Once each of the sections is copied, we may still need to do some
2987 finalization work for private section headers. Do that here. */
2990 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2992 /* Allow the BFD backend to copy any private data it understands
2993 from the input section to the output section. */
2994 if (! bfd_copy_private_header_data (ibfd, obfd))
2997 bfd_nonfatal_message (NULL, ibfd, NULL,
2998 _("error in private header data"));
3002 /* All went well. */
3006 /* Create a section in OBFD with the same
3007 name and attributes as ISECTION in IBFD. */
3010 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3012 bfd *obfd = (bfd *) obfdarg;
3013 struct section_list *p;
3021 char *prefix = NULL;
3022 bfd_boolean make_nobits;
3024 if (is_strip_section (ibfd, isection))
3027 /* Get the, possibly new, name of the output section. */
3028 name = bfd_section_name (ibfd, isection);
3029 flags = bfd_get_section_flags (ibfd, isection);
3030 name = find_section_rename (name, &flags);
3032 /* Prefix sections. */
3033 if ((prefix_alloc_sections_string)
3034 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
3035 prefix = prefix_alloc_sections_string;
3036 else if (prefix_sections_string)
3037 prefix = prefix_sections_string;
3043 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
3049 make_nobits = FALSE;
3051 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3052 SECTION_CONTEXT_SET_FLAGS);
3054 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
3055 else if (strip_symbols == STRIP_NONDEBUG
3056 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
3057 && !is_nondebug_keep_contents_section (ibfd, isection))
3059 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3060 if (obfd->xvec->flavour == bfd_target_elf_flavour)
3064 /* Twiddle the input section flags so that it seems to
3065 elf.c:copy_private_bfd_data that section flags have not
3066 changed between input and output sections. This hack
3067 prevents wholesale rewriting of the program headers. */
3068 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3072 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
3074 if (osection == NULL)
3076 err = _("failed to create output section");
3081 elf_section_type (osection) = SHT_NOBITS;
3083 size = bfd_section_size (ibfd, isection);
3084 size = bfd_convert_section_size (ibfd, isection, obfd, size);
3086 size = (size + interleave - 1) / interleave * copy_width;
3087 else if (extract_symbol)
3089 if (! bfd_set_section_size (obfd, osection, size))
3091 err = _("failed to set size");
3095 vma = bfd_section_vma (ibfd, isection);
3096 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3097 SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
3100 if (p->context & SECTION_CONTEXT_SET_VMA)
3106 vma += change_section_address;
3108 if (! bfd_set_section_vma (obfd, osection, vma))
3110 err = _("failed to set vma");
3114 lma = isection->lma;
3115 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3116 SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
3119 if (p->context & SECTION_CONTEXT_ALTER_LMA)
3125 lma += change_section_address;
3127 osection->lma = lma;
3129 /* FIXME: This is probably not enough. If we change the LMA we
3130 may have to recompute the header for the file as well. */
3131 if (!bfd_set_section_alignment (obfd,
3133 bfd_section_alignment (ibfd, isection)))
3135 err = _("failed to set alignment");
3139 /* Copy merge entity size. */
3140 osection->entsize = isection->entsize;
3142 /* Copy compress status. */
3143 osection->compress_status = isection->compress_status;
3145 /* This used to be mangle_section; we do here to avoid using
3146 bfd_get_section_by_name since some formats allow multiple
3147 sections with the same name. */
3148 isection->output_section = osection;
3149 isection->output_offset = 0;
3151 if ((isection->flags & SEC_GROUP) != 0)
3153 asymbol *gsym = group_signature (isection);
3157 gsym->flags |= BSF_KEEP;
3158 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
3159 elf_group_id (isection) = gsym;
3163 /* Allow the BFD backend to copy any private data it understands
3164 from the input section to the output section. */
3165 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
3167 err = _("failed to copy private data");
3171 /* All went well. */
3176 bfd_nonfatal_message (NULL, obfd, osection, err);
3179 /* Return TRUE if input section ISECTION should be skipped. */
3182 skip_section (bfd *ibfd, sec_ptr isection)
3188 /* If we have already failed earlier on,
3189 do not keep on generating complaints now. */
3196 if (is_strip_section (ibfd, isection))
3199 if (is_update_section (ibfd, isection))
3202 flags = bfd_get_section_flags (ibfd, isection);
3203 if ((flags & SEC_GROUP) != 0)
3206 osection = isection->output_section;
3207 size = bfd_get_section_size (isection);
3209 if (size == 0 || osection == 0)
3215 /* Add section SECTION_PATTERN to the list of sections that will have their
3216 relocations removed. */
3219 handle_remove_relocations_option (const char *section_pattern)
3221 find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE_RELOCS);
3224 /* Return TRUE if ISECTION from IBFD should have its relocations removed,
3225 otherwise return FALSE. If the user has requested that relocations be
3226 removed from a section that does not have relocations then this
3227 function will still return TRUE. */
3230 discard_relocations (bfd *ibfd ATTRIBUTE_UNUSED, asection *isection)
3232 return (find_section_list (bfd_section_name (ibfd, isection), FALSE,
3233 SECTION_CONTEXT_REMOVE_RELOCS) != NULL);
3236 /* Wrapper for dealing with --remove-section (-R) command line arguments.
3237 A special case is detected here, if the user asks to remove a relocation
3238 section (one starting with ".rela." or ".rel.") then this removal must
3239 be done using a different technique. */
3242 handle_remove_section_option (const char *section_pattern)
3244 if (strncmp (section_pattern, ".rela.", 6) == 0)
3245 handle_remove_relocations_option (section_pattern + 5);
3246 else if (strncmp (section_pattern, ".rel.", 5) == 0)
3247 handle_remove_relocations_option (section_pattern + 4);
3250 find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE);
3251 sections_removed = TRUE;
3255 /* Copy relocations in input section ISECTION of IBFD to an output
3256 section with the same name in OBFDARG. If stripping then don't
3257 copy any relocation info. */
3260 copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3262 bfd *obfd = (bfd *) obfdarg;
3268 if (skip_section (ibfd, isection))
3271 osection = isection->output_section;
3273 /* Core files and DWO files do not need to be relocated. */
3274 if (bfd_get_format (obfd) == bfd_core
3275 || strip_symbols == STRIP_NONDWO
3276 || discard_relocations (ibfd, isection))
3280 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
3284 /* Do not complain if the target does not support relocations. */
3285 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3290 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
3298 bfd_set_reloc (obfd, osection, NULL, 0);
3299 osection->flags &= ~SEC_RELOC;
3303 relpp = (arelent **) xmalloc (relsize);
3304 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
3308 bfd_nonfatal_message (NULL, ibfd, isection,
3309 _("relocation count is negative"));
3313 if (strip_symbols == STRIP_ALL)
3315 /* Remove relocations which are not in
3316 keep_strip_specific_list. */
3317 arelent **temp_relpp;
3318 long temp_relcount = 0;
3321 temp_relpp = (arelent **) xmalloc (relsize);
3322 for (i = 0; i < relcount; i++)
3324 /* PR 17512: file: 9e907e0c. */
3325 if (relpp[i]->sym_ptr_ptr
3327 && * relpp[i]->sym_ptr_ptr)
3328 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
3329 keep_specific_htab))
3330 temp_relpp [temp_relcount++] = relpp [i];
3332 relcount = temp_relcount;
3337 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
3340 osection->flags &= ~SEC_RELOC;
3346 /* Copy the data of input section ISECTION of IBFD
3347 to an output section with the same name in OBFD. */
3350 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3352 bfd *obfd = (bfd *) obfdarg;
3353 struct section_list *p;
3357 if (skip_section (ibfd, isection))
3360 osection = isection->output_section;
3361 /* The output SHF_COMPRESSED section size is different from input if
3362 ELF classes of input and output aren't the same. We can't use
3363 the output section size since --interleave will shrink the output
3364 section. Size will be updated if the section is converted. */
3365 size = bfd_get_section_size (isection);
3367 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
3368 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
3370 bfd_byte *memhunk = NULL;
3372 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)
3373 || !bfd_convert_section_contents (ibfd, isection, obfd,
3377 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
3384 /* We don't handle leftover bytes (too many possible behaviors,
3385 and we don't know what the user wants). The section length
3386 must be a multiple of the number of bytes to swap. */
3387 if ((size % reverse_bytes) == 0)
3392 for (i = 0; i < size; i += reverse_bytes)
3393 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
3395 bfd_byte *m = (bfd_byte *) memhunk;
3398 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
3399 m[(i + reverse_bytes) - (j + 1)] = b;
3403 /* User must pad the section up in order to do this. */
3404 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
3405 bfd_section_name (ibfd, isection), reverse_bytes);
3410 /* Keep only every `copy_byte'th byte in MEMHUNK. */
3411 char *from = (char *) memhunk + copy_byte;
3412 char *to = (char *) memhunk;
3413 char *end = (char *) memhunk + size;
3416 for (; from < end; from += interleave)
3417 for (i = 0; i < copy_width; i++)
3419 if (&from[i] >= end)
3424 size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
3425 osection->lma /= interleave;
3428 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
3431 bfd_nonfatal_message (NULL, obfd, osection, NULL);
3437 else if ((p = find_section_list (bfd_get_section_name (ibfd, isection),
3438 FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL
3439 && (p->flags & SEC_HAS_CONTENTS) != 0)
3441 void *memhunk = xmalloc (size);
3443 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
3444 flag--they can just remove the section entirely and add it
3445 back again. However, we do permit them to turn on the
3446 SEC_HAS_CONTENTS flag, and take it to mean that the section
3447 contents should be zeroed out. */
3449 memset (memhunk, 0, size);
3450 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
3453 bfd_nonfatal_message (NULL, obfd, osection, NULL);
3461 /* Get all the sections. This is used when --gap-fill or --pad-to is
3465 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
3467 asection ***secppp = (asection ***) secppparg;
3469 **secppp = osection;
3473 /* Sort sections by VMA. This is called via qsort, and is used when
3474 --gap-fill or --pad-to is used. We force non loadable or empty
3475 sections to the front, where they are easier to ignore. */
3478 compare_section_lma (const void *arg1, const void *arg2)
3480 const asection *const *sec1 = (const asection * const *) arg1;
3481 const asection *const *sec2 = (const asection * const *) arg2;
3482 flagword flags1, flags2;
3484 /* Sort non loadable sections to the front. */
3485 flags1 = (*sec1)->flags;
3486 flags2 = (*sec2)->flags;
3487 if ((flags1 & SEC_HAS_CONTENTS) == 0
3488 || (flags1 & SEC_LOAD) == 0)
3490 if ((flags2 & SEC_HAS_CONTENTS) != 0
3491 && (flags2 & SEC_LOAD) != 0)
3496 if ((flags2 & SEC_HAS_CONTENTS) == 0
3497 || (flags2 & SEC_LOAD) == 0)
3501 /* Sort sections by LMA. */
3502 if ((*sec1)->lma > (*sec2)->lma)
3504 else if ((*sec1)->lma < (*sec2)->lma)
3507 /* Sort sections with the same LMA by size. */
3508 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
3510 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
3516 /* Mark all the symbols which will be used in output relocations with
3517 the BSF_KEEP flag so that those symbols will not be stripped.
3519 Ignore relocations which will not appear in the output file. */
3522 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
3524 asymbol **symbols = (asymbol **) symbolsarg;
3529 /* Ignore an input section with no corresponding output section. */
3530 if (isection->output_section == NULL)
3533 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
3536 /* Do not complain if the target does not support relocations. */
3537 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3539 bfd_fatal (bfd_get_filename (ibfd));
3545 relpp = (arelent **) xmalloc (relsize);
3546 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
3548 bfd_fatal (bfd_get_filename (ibfd));
3550 /* Examine each symbol used in a relocation. If it's not one of the
3551 special bfd section symbols, then mark it with BSF_KEEP. */
3552 for (i = 0; i < relcount; i++)
3554 /* See PRs 20923 and 20930 for reproducers for the NULL tests. */
3555 if (relpp[i]->sym_ptr_ptr != NULL
3556 && * relpp[i]->sym_ptr_ptr != NULL
3557 && *relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
3558 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
3559 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
3560 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
3567 /* Write out debugging information. */
3570 write_debugging_info (bfd *obfd, void *dhandle,
3571 long *symcountp ATTRIBUTE_UNUSED,
3572 asymbol ***symppp ATTRIBUTE_UNUSED)
3574 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
3575 return write_ieee_debugging_info (obfd, dhandle);
3577 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
3578 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
3580 bfd_byte *syms, *strings;
3581 bfd_size_type symsize, stringsize;
3582 asection *stabsec, *stabstrsec;
3585 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
3590 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
3591 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
3592 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
3594 || stabstrsec == NULL
3595 || ! bfd_set_section_size (obfd, stabsec, symsize)
3596 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
3597 || ! bfd_set_section_alignment (obfd, stabsec, 2)
3598 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
3600 bfd_nonfatal_message (NULL, obfd, NULL,
3601 _("can't create debugging section"));
3605 /* We can get away with setting the section contents now because
3606 the next thing the caller is going to do is copy over the
3607 real sections. We may someday have to split the contents
3608 setting out of this function. */
3609 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
3610 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
3613 bfd_nonfatal_message (NULL, obfd, NULL,
3614 _("can't set debugging section contents"));
3621 bfd_nonfatal_message (NULL, obfd, NULL,
3622 _("don't know how to write debugging information for %s"),
3623 bfd_get_target (obfd));
3627 /* If neither -D nor -U was specified explicitly,
3628 then use the configured default. */
3630 default_deterministic (void)
3632 if (deterministic < 0)
3633 deterministic = DEFAULT_AR_DETERMINISTIC;
3637 strip_main (int argc, char *argv[])
3639 char *input_target = NULL;
3640 char *output_target = NULL;
3641 bfd_boolean show_version = FALSE;
3642 bfd_boolean formats_info = FALSE;
3645 char *output_file = NULL;
3647 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvwDU",
3648 strip_options, (int *) 0)) != EOF)
3653 input_target = optarg;
3656 output_target = optarg;
3659 input_target = output_target = optarg;
3662 handle_remove_section_option (optarg);
3664 case OPTION_REMOVE_RELOCS:
3665 handle_remove_relocations_option (optarg);
3668 strip_symbols = STRIP_ALL;
3672 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
3673 strip_symbols = STRIP_DEBUG;
3675 case OPTION_STRIP_DWO:
3676 strip_symbols = STRIP_DWO;
3678 case OPTION_STRIP_UNNEEDED:
3679 strip_symbols = STRIP_UNNEEDED;
3682 add_specific_symbol (optarg, keep_specific_htab);
3685 add_specific_symbol (optarg, strip_specific_htab);
3688 output_file = optarg;
3691 preserve_dates = TRUE;
3694 deterministic = TRUE;
3697 deterministic = FALSE;
3700 discard_locals = LOCALS_ALL;
3703 discard_locals = LOCALS_START_L;
3709 show_version = TRUE;
3711 case OPTION_FORMATS_INFO:
3712 formats_info = TRUE;
3714 case OPTION_ONLY_KEEP_DEBUG:
3715 strip_symbols = STRIP_NONDEBUG;
3717 case OPTION_KEEP_FILE_SYMBOLS:
3718 keep_file_symbols = 1;
3721 /* We've been given a long option. */
3728 strip_usage (stdout, 0);
3730 strip_usage (stderr, 1);
3741 print_version ("strip");
3743 default_deterministic ();
3745 /* Default is to strip all symbols. */
3746 if (strip_symbols == STRIP_UNDEF
3747 && discard_locals == LOCALS_UNDEF
3748 && htab_elements (strip_specific_htab) == 0)
3749 strip_symbols = STRIP_ALL;
3751 if (output_target == NULL)
3752 output_target = input_target;
3756 || (output_file != NULL && (i + 1) < argc))
3757 strip_usage (stderr, 1);
3759 for (; i < argc; i++)
3761 int hold_status = status;
3762 struct stat statbuf;
3765 if (get_file_size (argv[i]) < 1)
3772 /* No need to check the return value of stat().
3773 It has already been checked in get_file_size(). */
3774 stat (argv[i], &statbuf);
3776 if (output_file == NULL
3777 || filename_cmp (argv[i], output_file) == 0)
3778 tmpname = make_tempname (argv[i]);
3780 tmpname = output_file;
3782 if (tmpname == NULL)
3784 bfd_nonfatal_message (argv[i], NULL, NULL,
3785 _("could not create temporary file to hold stripped copy"));
3791 copy_file (argv[i], tmpname, input_target, output_target, NULL);
3795 set_times (tmpname, &statbuf);
3796 if (output_file != tmpname)
3797 status = (smart_rename (tmpname,
3798 output_file ? output_file : argv[i],
3799 preserve_dates) != 0);
3801 status = hold_status;
3804 unlink_if_ordinary (tmpname);
3805 if (output_file != tmpname)
3812 /* Set up PE subsystem. */
3815 set_pe_subsystem (const char *s)
3817 const char *version, *subsystem;
3827 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
3828 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
3829 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
3830 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
3831 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
3832 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
3833 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
3834 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
3835 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
3836 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
3842 /* Check for the presence of a version number. */
3843 version = strchr (s, ':');
3844 if (version == NULL)
3848 int len = version - s;
3852 version = copy + 1 + len;
3853 pe_major_subsystem_version = strtoul (version, ©, 0);
3855 pe_minor_subsystem_version = strtoul (copy + 1, ©, 0);
3857 non_fatal (_("%s: bad version in PE subsystem"), s);
3860 /* Check for numeric subsystem. */
3861 value = (short) strtol (subsystem, ©, 0);
3864 for (i = 0; i < ARRAY_SIZE (v); i++)
3865 if (v[i].value == value)
3867 pe_subsystem = value;
3868 set_def = v[i].set_def;
3874 /* Search for subsystem by name. */
3875 for (i = 0; i < ARRAY_SIZE (v); i++)
3876 if (strcmp (subsystem, v[i].name) == 0)
3878 pe_subsystem = v[i].value;
3879 set_def = v[i].set_def;
3887 fatal (_("unknown PE subsystem: %s"), s);
3892 if (pe_file_alignment == (bfd_vma) -1)
3893 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3894 if (pe_section_alignment == (bfd_vma) -1)
3895 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3899 free ((char *) subsystem);
3902 /* Convert EFI target to PEI target. */
3905 convert_efi_target (char *efi)
3911 if (strcmp (efi + 4, "ia32") == 0)
3913 /* Change ia32 to i386. */
3918 else if (strcmp (efi + 4, "x86_64") == 0)
3920 /* Change x86_64 to x86-64. */
3925 /* Allocate and return a pointer to a struct section_add, initializing the
3926 structure using ARG, a string in the format "sectionname=filename".
3927 The returned structure will have its next pointer set to NEXT. The
3928 OPTION field is the name of the command line option currently being
3929 parsed, and is only used if an error needs to be reported. */
3931 static struct section_add *
3932 init_section_add (const char *arg,
3933 struct section_add *next,
3936 struct section_add *pa;
3939 s = strchr (arg, '=');
3941 fatal (_("bad format for %s"), option);
3943 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
3944 pa->name = xstrndup (arg, s - arg);
3945 pa->filename = s + 1;
3947 pa->contents = NULL;
3953 /* Load the file specified in PA, allocating memory to hold the file
3954 contents, and store a pointer to the allocated memory in the contents
3955 field of PA. The size field of PA is also updated. All errors call
3959 section_add_load_file (struct section_add *pa)
3964 /* We don't use get_file_size so that we can do
3965 --add-section .note.GNU_stack=/dev/null
3966 get_file_size doesn't work on /dev/null. */
3968 f = fopen (pa->filename, FOPEN_RB);
3970 fatal (_("cannot open: %s: %s"),
3971 pa->filename, strerror (errno));
3975 pa->contents = (bfd_byte *) xmalloc (alloc);
3983 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
3986 got = fread (pa->contents + off, 1, alloc - off, f);
3988 fatal (_("%s: fread failed"), pa->filename);
3999 copy_main (int argc, char *argv[])
4001 char *input_filename = NULL;
4002 char *output_filename = NULL;
4004 char *input_target = NULL;
4005 char *output_target = NULL;
4006 bfd_boolean show_version = FALSE;
4007 bfd_boolean change_warn = TRUE;
4008 bfd_boolean formats_info = FALSE;
4010 struct stat statbuf;
4011 const bfd_arch_info_type *input_arch = NULL;
4013 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
4014 copy_options, (int *) 0)) != EOF)
4019 copy_byte = atoi (optarg);
4021 fatal (_("byte number must be non-negative"));
4025 input_arch = bfd_scan_arch (optarg);
4026 if (input_arch == NULL)
4027 fatal (_("architecture %s unknown"), optarg);
4033 interleave = atoi (optarg);
4035 fatal (_("interleave must be positive"));
4041 case OPTION_INTERLEAVE_WIDTH:
4042 copy_width = atoi (optarg);
4044 fatal(_("interleave width must be positive"));
4048 case 's': /* "source" - 'I' is preferred */
4049 input_target = optarg;
4053 case 'd': /* "destination" - 'O' is preferred */
4054 output_target = optarg;
4058 input_target = output_target = optarg;
4062 find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY);
4063 sections_copied = TRUE;
4067 handle_remove_section_option (optarg);
4070 case OPTION_REMOVE_RELOCS:
4071 handle_remove_relocations_option (optarg);
4075 strip_symbols = STRIP_ALL;
4079 strip_symbols = STRIP_DEBUG;
4082 case OPTION_STRIP_DWO:
4083 strip_symbols = STRIP_DWO;
4086 case OPTION_STRIP_UNNEEDED:
4087 strip_symbols = STRIP_UNNEEDED;
4090 case OPTION_ONLY_KEEP_DEBUG:
4091 strip_symbols = STRIP_NONDEBUG;
4094 case OPTION_KEEP_FILE_SYMBOLS:
4095 keep_file_symbols = 1;
4098 case OPTION_ADD_GNU_DEBUGLINK:
4099 long_section_names = ENABLE ;
4100 gnu_debuglink_filename = optarg;
4104 add_specific_symbol (optarg, keep_specific_htab);
4108 add_specific_symbol (optarg, strip_specific_htab);
4111 case OPTION_STRIP_UNNEEDED_SYMBOL:
4112 add_specific_symbol (optarg, strip_unneeded_htab);
4116 add_specific_symbol (optarg, localize_specific_htab);
4119 case OPTION_GLOBALIZE_SYMBOL:
4120 add_specific_symbol (optarg, globalize_specific_htab);
4124 add_specific_symbol (optarg, keepglobal_specific_htab);
4128 add_specific_symbol (optarg, weaken_specific_htab);
4132 preserve_dates = TRUE;
4136 deterministic = TRUE;
4140 deterministic = FALSE;
4148 discard_locals = LOCALS_ALL;
4152 discard_locals = LOCALS_START_L;
4160 show_version = TRUE;
4163 case OPTION_FORMATS_INFO:
4164 formats_info = TRUE;
4171 case OPTION_ADD_SECTION:
4172 add_sections = init_section_add (optarg, add_sections,
4174 section_add_load_file (add_sections);
4177 case OPTION_UPDATE_SECTION:
4178 update_sections = init_section_add (optarg, update_sections,
4179 "--update-section");
4180 section_add_load_file (update_sections);
4183 case OPTION_DUMP_SECTION:
4184 dump_sections = init_section_add (optarg, dump_sections,
4188 case OPTION_ADD_SYMBOL:
4191 struct addsym_node *newsym = xmalloc (sizeof *newsym);
4193 newsym->next = NULL;
4194 s = strchr (optarg, '=');
4196 fatal (_("bad format for %s"), "--add-symbol");
4197 t = strchr (s + 1, ':');
4199 newsym->symdef = xstrndup (optarg, s - optarg);
4202 newsym->section = xstrndup (s + 1, t - (s + 1));
4203 newsym->symval = strtol (t + 1, NULL, 0);
4207 newsym->section = NULL;
4208 newsym->symval = strtol (s + 1, NULL, 0);
4212 t = strchr (t + 1, ',');
4213 newsym->othersym = NULL;
4215 newsym->flags = parse_symflags (t+1, &newsym->othersym);
4217 newsym->flags = BSF_GLOBAL;
4219 /* Keep 'othersym' symbols at the front of the list. */
4220 if (newsym->othersym)
4222 newsym->next = add_sym_list;
4224 add_sym_tail = &newsym->next;
4225 add_sym_list = newsym;
4229 *add_sym_tail = newsym;
4230 add_sym_tail = &newsym->next;
4236 case OPTION_CHANGE_START:
4237 change_start = parse_vma (optarg, "--change-start");
4240 case OPTION_CHANGE_SECTION_ADDRESS:
4241 case OPTION_CHANGE_SECTION_LMA:
4242 case OPTION_CHANGE_SECTION_VMA:
4244 struct section_list * p;
4245 unsigned int context = 0;
4249 char *option = NULL;
4254 case OPTION_CHANGE_SECTION_ADDRESS:
4255 option = "--change-section-address";
4256 context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
4258 case OPTION_CHANGE_SECTION_LMA:
4259 option = "--change-section-lma";
4260 context = SECTION_CONTEXT_ALTER_LMA;
4262 case OPTION_CHANGE_SECTION_VMA:
4263 option = "--change-section-vma";
4264 context = SECTION_CONTEXT_ALTER_VMA;
4268 s = strchr (optarg, '=');
4271 s = strchr (optarg, '+');
4274 s = strchr (optarg, '-');
4276 fatal (_("bad format for %s"), option);
4281 /* Correct the context. */
4284 case OPTION_CHANGE_SECTION_ADDRESS:
4285 context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
4287 case OPTION_CHANGE_SECTION_LMA:
4288 context = SECTION_CONTEXT_SET_LMA;
4290 case OPTION_CHANGE_SECTION_VMA:
4291 context = SECTION_CONTEXT_SET_VMA;
4297 name = (char *) xmalloc (len + 1);
4298 strncpy (name, optarg, len);
4301 p = find_section_list (name, TRUE, context);
4303 val = parse_vma (s + 1, option);
4309 case OPTION_CHANGE_SECTION_ADDRESS:
4313 case OPTION_CHANGE_SECTION_LMA:
4317 case OPTION_CHANGE_SECTION_VMA:
4324 case OPTION_CHANGE_ADDRESSES:
4325 change_section_address = parse_vma (optarg, "--change-addresses");
4326 change_start = change_section_address;
4329 case OPTION_CHANGE_WARNINGS:
4333 case OPTION_CHANGE_LEADING_CHAR:
4334 change_leading_char = TRUE;
4337 case OPTION_COMPRESS_DEBUG_SECTIONS:
4340 if (strcasecmp (optarg, "none") == 0)
4341 do_debug_sections = decompress;
4342 else if (strcasecmp (optarg, "zlib") == 0)
4343 do_debug_sections = compress_zlib;
4344 else if (strcasecmp (optarg, "zlib-gnu") == 0)
4345 do_debug_sections = compress_gnu_zlib;
4346 else if (strcasecmp (optarg, "zlib-gabi") == 0)
4347 do_debug_sections = compress_gabi_zlib;
4349 fatal (_("unrecognized --compress-debug-sections type `%s'"),
4353 do_debug_sections = compress;
4356 case OPTION_DEBUGGING:
4357 convert_debugging = TRUE;
4360 case OPTION_DECOMPRESS_DEBUG_SECTIONS:
4361 do_debug_sections = decompress;
4364 case OPTION_ELF_STT_COMMON:
4365 if (strcasecmp (optarg, "yes") == 0)
4366 do_elf_stt_common = elf_stt_common;
4367 else if (strcasecmp (optarg, "no") == 0)
4368 do_elf_stt_common = no_elf_stt_common;
4370 fatal (_("unrecognized --elf-stt-common= option `%s'"),
4374 case OPTION_GAP_FILL:
4376 bfd_vma gap_fill_vma;
4378 gap_fill_vma = parse_vma (optarg, "--gap-fill");
4379 gap_fill = (bfd_byte) gap_fill_vma;
4380 if ((bfd_vma) gap_fill != gap_fill_vma)
4384 sprintf_vma (buff, gap_fill_vma);
4386 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
4389 gap_fill_set = TRUE;
4393 case OPTION_NO_CHANGE_WARNINGS:
4394 change_warn = FALSE;
4398 pad_to = parse_vma (optarg, "--pad-to");
4402 case OPTION_REMOVE_LEADING_CHAR:
4403 remove_leading_char = TRUE;
4406 case OPTION_REDEFINE_SYM:
4408 /* Push this redefinition onto redefine_symbol_list. */
4412 const char *nextarg;
4413 char *source, *target;
4415 s = strchr (optarg, '=');
4417 fatal (_("bad format for %s"), "--redefine-sym");
4420 source = (char *) xmalloc (len + 1);
4421 strncpy (source, optarg, len);
4425 len = strlen (nextarg);
4426 target = (char *) xmalloc (len + 1);
4427 strcpy (target, nextarg);
4429 redefine_list_append ("--redefine-sym", source, target);
4436 case OPTION_REDEFINE_SYMS:
4437 add_redefine_syms_file (optarg);
4440 case OPTION_SET_SECTION_FLAGS:
4442 struct section_list *p;
4447 s = strchr (optarg, '=');
4449 fatal (_("bad format for %s"), "--set-section-flags");
4452 name = (char *) xmalloc (len + 1);
4453 strncpy (name, optarg, len);
4456 p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS);
4458 p->flags = parse_flags (s + 1);
4462 case OPTION_RENAME_SECTION:
4465 const char *eq, *fl;
4470 eq = strchr (optarg, '=');
4472 fatal (_("bad format for %s"), "--rename-section");
4476 fatal (_("bad format for %s"), "--rename-section");
4478 old_name = (char *) xmalloc (len + 1);
4479 strncpy (old_name, optarg, len);
4483 fl = strchr (eq, ',');
4486 flags = parse_flags (fl + 1);
4496 fatal (_("bad format for %s"), "--rename-section");
4498 new_name = (char *) xmalloc (len + 1);
4499 strncpy (new_name, eq, len);
4502 add_section_rename (old_name, new_name, flags);
4506 case OPTION_SET_START:
4507 set_start = parse_vma (optarg, "--set-start");
4508 set_start_set = TRUE;
4511 case OPTION_SREC_LEN:
4512 Chunk = parse_vma (optarg, "--srec-len");
4515 case OPTION_SREC_FORCES3:
4519 case OPTION_STRIP_SYMBOLS:
4520 add_specific_symbols (optarg, strip_specific_htab);
4523 case OPTION_STRIP_UNNEEDED_SYMBOLS:
4524 add_specific_symbols (optarg, strip_unneeded_htab);
4527 case OPTION_KEEP_SYMBOLS:
4528 add_specific_symbols (optarg, keep_specific_htab);
4531 case OPTION_LOCALIZE_HIDDEN:
4532 localize_hidden = TRUE;
4535 case OPTION_LOCALIZE_SYMBOLS:
4536 add_specific_symbols (optarg, localize_specific_htab);
4539 case OPTION_LONG_SECTION_NAMES:
4540 if (!strcmp ("enable", optarg))
4541 long_section_names = ENABLE;
4542 else if (!strcmp ("disable", optarg))
4543 long_section_names = DISABLE;
4544 else if (!strcmp ("keep", optarg))
4545 long_section_names = KEEP;
4547 fatal (_("unknown long section names option '%s'"), optarg);
4550 case OPTION_GLOBALIZE_SYMBOLS:
4551 add_specific_symbols (optarg, globalize_specific_htab);
4554 case OPTION_KEEPGLOBAL_SYMBOLS:
4555 add_specific_symbols (optarg, keepglobal_specific_htab);
4558 case OPTION_WEAKEN_SYMBOLS:
4559 add_specific_symbols (optarg, weaken_specific_htab);
4562 case OPTION_ALT_MACH_CODE:
4563 use_alt_mach_code = strtoul (optarg, NULL, 0);
4564 if (use_alt_mach_code == 0)
4565 fatal (_("unable to parse alternative machine code"));
4568 case OPTION_PREFIX_SYMBOLS:
4569 prefix_symbols_string = optarg;
4572 case OPTION_PREFIX_SECTIONS:
4573 prefix_sections_string = optarg;
4576 case OPTION_PREFIX_ALLOC_SECTIONS:
4577 prefix_alloc_sections_string = optarg;
4580 case OPTION_READONLY_TEXT:
4581 bfd_flags_to_set |= WP_TEXT;
4582 bfd_flags_to_clear &= ~WP_TEXT;
4585 case OPTION_WRITABLE_TEXT:
4586 bfd_flags_to_clear |= WP_TEXT;
4587 bfd_flags_to_set &= ~WP_TEXT;
4591 bfd_flags_to_set |= D_PAGED;
4592 bfd_flags_to_clear &= ~D_PAGED;
4596 bfd_flags_to_clear |= D_PAGED;
4597 bfd_flags_to_set &= ~D_PAGED;
4600 case OPTION_EXTRACT_DWO:
4601 strip_symbols = STRIP_NONDWO;
4604 case OPTION_EXTRACT_SYMBOL:
4605 extract_symbol = TRUE;
4608 case OPTION_REVERSE_BYTES:
4610 int prev = reverse_bytes;
4612 reverse_bytes = atoi (optarg);
4613 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
4614 fatal (_("number of bytes to reverse must be positive and even"));
4616 if (prev && prev != reverse_bytes)
4617 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
4622 case OPTION_FILE_ALIGNMENT:
4623 pe_file_alignment = parse_vma (optarg, "--file-alignment");
4629 pe_heap_reserve = strtoul (optarg, &end, 0);
4631 || (*end != '.' && *end != '\0'))
4632 non_fatal (_("%s: invalid reserve value for --heap"),
4634 else if (*end != '\0')
4636 pe_heap_commit = strtoul (end + 1, &end, 0);
4638 non_fatal (_("%s: invalid commit value for --heap"),
4644 case OPTION_IMAGE_BASE:
4645 pe_image_base = parse_vma (optarg, "--image-base");
4648 case OPTION_SECTION_ALIGNMENT:
4649 pe_section_alignment = parse_vma (optarg,
4650 "--section-alignment");
4653 case OPTION_SUBSYSTEM:
4654 set_pe_subsystem (optarg);
4660 pe_stack_reserve = strtoul (optarg, &end, 0);
4662 || (*end != '.' && *end != '\0'))
4663 non_fatal (_("%s: invalid reserve value for --stack"),
4665 else if (*end != '\0')
4667 pe_stack_commit = strtoul (end + 1, &end, 0);
4669 non_fatal (_("%s: invalid commit value for --stack"),
4676 /* We've been given a long option. */
4681 copy_usage (stdout, 0);
4684 copy_usage (stderr, 1);
4695 print_version ("objcopy");
4697 if (interleave && copy_byte == -1)
4698 fatal (_("interleave start byte must be set with --byte"));
4700 if (copy_byte >= interleave)
4701 fatal (_("byte number must be less than interleave"));
4703 if (copy_width > interleave - copy_byte)
4704 fatal (_("interleave width must be less than or equal to interleave - byte`"));
4706 if (optind == argc || optind + 2 < argc)
4707 copy_usage (stderr, 1);
4709 input_filename = argv[optind];
4710 if (optind + 1 < argc)
4711 output_filename = argv[optind + 1];
4713 default_deterministic ();
4715 /* Default is to strip no symbols. */
4716 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
4717 strip_symbols = STRIP_NONE;
4719 if (output_target == NULL)
4720 output_target = input_target;
4722 /* Convert input EFI target to PEI target. */
4723 if (input_target != NULL
4724 && strncmp (input_target, "efi-", 4) == 0)
4728 efi = xstrdup (output_target + 4);
4729 if (strncmp (efi, "bsdrv-", 6) == 0
4730 || strncmp (efi, "rtdrv-", 6) == 0)
4732 else if (strncmp (efi, "app-", 4) != 0)
4733 fatal (_("unknown input EFI target: %s"), input_target);
4736 convert_efi_target (efi);
4739 /* Convert output EFI target to PEI target. */
4740 if (output_target != NULL
4741 && strncmp (output_target, "efi-", 4) == 0)
4745 efi = xstrdup (output_target + 4);
4746 if (strncmp (efi, "app-", 4) == 0)
4748 if (pe_subsystem == -1)
4749 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
4751 else if (strncmp (efi, "bsdrv-", 6) == 0)
4753 if (pe_subsystem == -1)
4754 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
4757 else if (strncmp (efi, "rtdrv-", 6) == 0)
4759 if (pe_subsystem == -1)
4760 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
4764 fatal (_("unknown output EFI target: %s"), output_target);
4766 if (pe_file_alignment == (bfd_vma) -1)
4767 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
4768 if (pe_section_alignment == (bfd_vma) -1)
4769 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
4771 output_target = efi;
4772 convert_efi_target (efi);
4776 if (stat (input_filename, & statbuf) < 0)
4777 fatal (_("warning: could not locate '%s'. System error message: %s"),
4778 input_filename, strerror (errno));
4780 /* If there is no destination file, or the source and destination files
4781 are the same, then create a temp and rename the result into the input. */
4782 if (output_filename == NULL
4783 || filename_cmp (input_filename, output_filename) == 0)
4784 tmpname = make_tempname (input_filename);
4786 tmpname = output_filename;
4788 if (tmpname == NULL)
4789 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
4790 input_filename, strerror (errno));
4792 copy_file (input_filename, tmpname, input_target, output_target, input_arch);
4796 set_times (tmpname, &statbuf);
4797 if (tmpname != output_filename)
4798 status = (smart_rename (tmpname, input_filename,
4799 preserve_dates) != 0);
4802 unlink_if_ordinary (tmpname);
4806 struct section_list *p;
4808 for (p = change_sections; p != NULL; p = p->next)
4812 if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
4816 sprintf_vma (buff, p->vma_val);
4818 /* xgettext:c-format */
4819 non_fatal (_("%s %s%c0x%s never used"),
4820 "--change-section-vma",
4822 p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
4826 if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
4830 sprintf_vma (buff, p->lma_val);
4832 /* xgettext:c-format */
4833 non_fatal (_("%s %s%c0x%s never used"),
4834 "--change-section-lma",
4836 p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
4847 main (int argc, char *argv[])
4849 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4850 setlocale (LC_MESSAGES, "");
4852 #if defined (HAVE_SETLOCALE)
4853 setlocale (LC_CTYPE, "");
4855 bindtextdomain (PACKAGE, LOCALEDIR);
4856 textdomain (PACKAGE);
4858 program_name = argv[0];
4859 xmalloc_set_program_name (program_name);
4861 START_PROGRESS (program_name, 0);
4863 expandargv (&argc, &argv);
4865 strip_symbols = STRIP_UNDEF;
4866 discard_locals = LOCALS_UNDEF;
4869 set_default_bfd_target ();
4873 int i = strlen (program_name);
4874 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4875 /* Drop the .exe suffix, if any. */
4876 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
4879 program_name[i] = '\0';
4882 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
4885 create_symbol_htabs ();
4888 bfd_set_error_program_name (argv[0]);
4891 strip_main (argc, argv);
4893 copy_main (argc, argv);
4895 END_PROGRESS (program_name);