1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "libiberty.h"
28 #include "filenames.h"
32 #include "coff/internal.h"
35 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
36 header in generic PE code. */
37 #include "coff/i386.h"
40 static bfd_vma pe_file_alignment
= (bfd_vma
) -1;
41 static bfd_vma pe_heap_commit
= (bfd_vma
) -1;
42 static bfd_vma pe_heap_reserve
= (bfd_vma
) -1;
43 static bfd_vma pe_image_base
= (bfd_vma
) -1;
44 static bfd_vma pe_section_alignment
= (bfd_vma
) -1;
45 static bfd_vma pe_stack_commit
= (bfd_vma
) -1;
46 static bfd_vma pe_stack_reserve
= (bfd_vma
) -1;
47 static short pe_subsystem
= -1;
48 static short pe_major_subsystem_version
= -1;
49 static short pe_minor_subsystem_version
= -1;
51 struct is_specified_symbol_predicate_data
57 /* A list to support redefine_sym. */
62 struct redefine_node
*next
;
65 typedef struct section_rename
67 const char * old_name
;
68 const char * new_name
;
70 struct section_rename
* next
;
74 /* List of sections to be renamed. */
75 static section_rename
*section_rename_list
;
77 static asymbol
**isympp
= NULL
; /* Input symbols. */
78 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
80 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
81 static int copy_byte
= -1;
82 static int interleave
= 0; /* Initialised to 4 in copy_main(). */
83 static int copy_width
= 1;
85 static bfd_boolean verbose
; /* Print file and target names. */
86 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
87 static int deterministic
= -1; /* Enable deterministic archives. */
88 static int status
= 0; /* Exit status. */
93 STRIP_NONE
, /* Don't strip. */
94 STRIP_DEBUG
, /* Strip all debugger symbols. */
95 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
96 STRIP_NONDEBUG
, /* Strip everything but debug info. */
97 STRIP_DWO
, /* Strip all DWO info. */
98 STRIP_NONDWO
, /* Strip everything but DWO info. */
99 STRIP_ALL
/* Strip all symbols. */
102 /* Which symbols to remove. */
103 static enum strip_action strip_symbols
= STRIP_UNDEF
;
108 LOCALS_START_L
, /* Discard locals starting with L. */
109 LOCALS_ALL
/* Discard all locals. */
112 /* Which local symbols to remove. Overrides STRIP_ALL. */
113 static enum locals_action discard_locals
;
115 /* Structure used to hold lists of sections and actions to take. */
118 struct section_list
* next
; /* Next section to change. */
119 const char * pattern
; /* Section name pattern. */
120 bfd_boolean used
; /* Whether this entry was used. */
122 unsigned int context
; /* What to do with matching sections. */
123 /* Flag bits used in the context field.
124 COPY and REMOVE are mutually exlusive. SET and ALTER are mutually exclusive. */
125 #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
126 #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
127 #define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */
128 #define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */
129 #define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */
130 #define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */
131 #define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */
133 bfd_vma vma_val
; /* Amount to change by or set to. */
134 bfd_vma lma_val
; /* Amount to change by or set to. */
135 flagword flags
; /* What to set the section flags to. */
138 static struct section_list
*change_sections
;
140 /* TRUE if some sections are to be removed. */
141 static bfd_boolean sections_removed
;
143 /* TRUE if only some sections are to be copied. */
144 static bfd_boolean sections_copied
;
146 /* Changes to the start address. */
147 static bfd_vma change_start
= 0;
148 static bfd_boolean set_start_set
= FALSE
;
149 static bfd_vma set_start
;
151 /* Changes to section addresses. */
152 static bfd_vma change_section_address
= 0;
154 /* Filling gaps between sections. */
155 static bfd_boolean gap_fill_set
= FALSE
;
156 static bfd_byte gap_fill
= 0;
158 /* Pad to a given address. */
159 static bfd_boolean pad_to_set
= FALSE
;
160 static bfd_vma pad_to
;
162 /* Use alternative machine code? */
163 static unsigned long use_alt_mach_code
= 0;
165 /* Output BFD flags user wants to set or clear */
166 static flagword bfd_flags_to_set
;
167 static flagword bfd_flags_to_clear
;
169 /* List of sections to add. */
172 /* Next section to add. */
173 struct section_add
*next
;
174 /* Name of section to add. */
176 /* Name of file holding section contents. */
177 const char *filename
;
180 /* Contents of file. */
182 /* BFD section, after it has been added. */
186 /* List of sections to add to the output BFD. */
187 static struct section_add
*add_sections
;
189 /* List of sections to update in the output BFD. */
190 static struct section_add
*update_sections
;
192 /* List of sections to dump from the output BFD. */
193 static struct section_add
*dump_sections
;
195 /* If non-NULL the argument to --add-gnu-debuglink.
196 This should be the filename to store in the .gnu_debuglink section. */
197 static const char * gnu_debuglink_filename
= NULL
;
199 /* Whether to convert debugging information. */
200 static bfd_boolean convert_debugging
= FALSE
;
202 /* Whether to compress/decompress DWARF debug sections. */
211 } do_debug_sections
= nothing
;
213 /* Whether to change the leading character in symbol names. */
214 static bfd_boolean change_leading_char
= FALSE
;
216 /* Whether to remove the leading character from global symbol names. */
217 static bfd_boolean remove_leading_char
= FALSE
;
219 /* Whether to permit wildcard in symbol comparison. */
220 static bfd_boolean wildcard
= FALSE
;
222 /* True if --localize-hidden is in effect. */
223 static bfd_boolean localize_hidden
= FALSE
;
225 /* List of symbols to strip, keep, localize, keep-global, weaken,
227 static htab_t strip_specific_htab
= NULL
;
228 static htab_t strip_unneeded_htab
= NULL
;
229 static htab_t keep_specific_htab
= NULL
;
230 static htab_t localize_specific_htab
= NULL
;
231 static htab_t globalize_specific_htab
= NULL
;
232 static htab_t keepglobal_specific_htab
= NULL
;
233 static htab_t weaken_specific_htab
= NULL
;
234 static struct redefine_node
*redefine_sym_list
= NULL
;
236 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
237 static bfd_boolean weaken
= FALSE
;
239 /* If this is TRUE, we retain BSF_FILE symbols. */
240 static bfd_boolean keep_file_symbols
= FALSE
;
242 /* Prefix symbols/sections. */
243 static char *prefix_symbols_string
= 0;
244 static char *prefix_sections_string
= 0;
245 static char *prefix_alloc_sections_string
= 0;
247 /* True if --extract-symbol was passed on the command line. */
248 static bfd_boolean extract_symbol
= FALSE
;
250 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
251 of <reverse_bytes> bytes within each output section. */
252 static int reverse_bytes
= 0;
254 /* For Coff objects, we may want to allow or disallow long section names,
255 or preserve them where found in the inputs. Debug info relies on them. */
256 enum long_section_name_handling
263 /* The default long section handling mode is to preserve them.
264 This is also the only behaviour for 'strip'. */
265 static enum long_section_name_handling long_section_names
= KEEP
;
267 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
268 enum command_line_switch
270 OPTION_ADD_SECTION
=150,
271 OPTION_UPDATE_SECTION
,
273 OPTION_CHANGE_ADDRESSES
,
274 OPTION_CHANGE_LEADING_CHAR
,
276 OPTION_CHANGE_SECTION_ADDRESS
,
277 OPTION_CHANGE_SECTION_LMA
,
278 OPTION_CHANGE_SECTION_VMA
,
279 OPTION_CHANGE_WARNINGS
,
280 OPTION_COMPRESS_DEBUG_SECTIONS
,
282 OPTION_DECOMPRESS_DEBUG_SECTIONS
,
284 OPTION_NO_CHANGE_WARNINGS
,
286 OPTION_REMOVE_LEADING_CHAR
,
287 OPTION_SET_SECTION_FLAGS
,
289 OPTION_STRIP_UNNEEDED
,
292 OPTION_REDEFINE_SYMS
,
295 OPTION_STRIP_SYMBOLS
,
296 OPTION_STRIP_UNNEEDED_SYMBOL
,
297 OPTION_STRIP_UNNEEDED_SYMBOLS
,
299 OPTION_LOCALIZE_HIDDEN
,
300 OPTION_LOCALIZE_SYMBOLS
,
301 OPTION_LONG_SECTION_NAMES
,
302 OPTION_GLOBALIZE_SYMBOL
,
303 OPTION_GLOBALIZE_SYMBOLS
,
304 OPTION_KEEPGLOBAL_SYMBOLS
,
305 OPTION_WEAKEN_SYMBOLS
,
306 OPTION_RENAME_SECTION
,
307 OPTION_ALT_MACH_CODE
,
308 OPTION_PREFIX_SYMBOLS
,
309 OPTION_PREFIX_SECTIONS
,
310 OPTION_PREFIX_ALLOC_SECTIONS
,
312 OPTION_ADD_GNU_DEBUGLINK
,
313 OPTION_ONLY_KEEP_DEBUG
,
314 OPTION_KEEP_FILE_SYMBOLS
,
315 OPTION_READONLY_TEXT
,
316 OPTION_WRITABLE_TEXT
,
319 OPTION_EXTRACT_SYMBOL
,
320 OPTION_REVERSE_BYTES
,
321 OPTION_FILE_ALIGNMENT
,
324 OPTION_SECTION_ALIGNMENT
,
326 OPTION_INTERLEAVE_WIDTH
,
332 /* Options to handle if running as "strip". */
334 static struct option strip_options
[] =
336 {"disable-deterministic-archives", no_argument
, 0, 'U'},
337 {"discard-all", no_argument
, 0, 'x'},
338 {"discard-locals", no_argument
, 0, 'X'},
339 {"enable-deterministic-archives", no_argument
, 0, 'D'},
340 {"format", required_argument
, 0, 'F'}, /* Obsolete */
341 {"help", no_argument
, 0, 'h'},
342 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
343 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
344 {"input-target", required_argument
, 0, 'I'},
345 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
346 {"keep-symbol", required_argument
, 0, 'K'},
347 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
348 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
349 {"output-target", required_argument
, 0, 'O'},
350 {"output-file", required_argument
, 0, 'o'},
351 {"preserve-dates", no_argument
, 0, 'p'},
352 {"remove-section", required_argument
, 0, 'R'},
353 {"strip-all", no_argument
, 0, 's'},
354 {"strip-debug", no_argument
, 0, 'S'},
355 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
356 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
357 {"strip-symbol", required_argument
, 0, 'N'},
358 {"target", required_argument
, 0, 'F'},
359 {"verbose", no_argument
, 0, 'v'},
360 {"version", no_argument
, 0, 'V'},
361 {"wildcard", no_argument
, 0, 'w'},
362 {0, no_argument
, 0, 0}
365 /* Options to handle if running as "objcopy". */
367 static struct option copy_options
[] =
369 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
370 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
371 {"update-section", required_argument
, 0, OPTION_UPDATE_SECTION
},
372 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
373 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
374 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
375 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
376 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
377 {"binary-architecture", required_argument
, 0, 'B'},
378 {"byte", required_argument
, 0, 'b'},
379 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
380 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
381 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
382 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
383 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
384 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
385 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
386 {"compress-debug-sections", optional_argument
, 0, OPTION_COMPRESS_DEBUG_SECTIONS
},
387 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
388 {"decompress-debug-sections", no_argument
, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS
},
389 {"disable-deterministic-archives", no_argument
, 0, 'U'},
390 {"discard-all", no_argument
, 0, 'x'},
391 {"discard-locals", no_argument
, 0, 'X'},
392 {"dump-section", required_argument
, 0, OPTION_DUMP_SECTION
},
393 {"enable-deterministic-archives", no_argument
, 0, 'D'},
394 {"extract-dwo", no_argument
, 0, OPTION_EXTRACT_DWO
},
395 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
396 {"format", required_argument
, 0, 'F'}, /* Obsolete */
397 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
398 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
399 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
400 {"help", no_argument
, 0, 'h'},
401 {"impure", no_argument
, 0, OPTION_IMPURE
},
402 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
403 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
404 {"input-target", required_argument
, 0, 'I'},
405 {"interleave", optional_argument
, 0, 'i'},
406 {"interleave-width", required_argument
, 0, OPTION_INTERLEAVE_WIDTH
},
407 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
408 {"keep-global-symbol", required_argument
, 0, 'G'},
409 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
410 {"keep-symbol", required_argument
, 0, 'K'},
411 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
412 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
413 {"localize-symbol", required_argument
, 0, 'L'},
414 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
415 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
416 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
417 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
418 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
419 {"only-section", required_argument
, 0, 'j'},
420 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
421 {"output-target", required_argument
, 0, 'O'},
422 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
423 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
424 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
425 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
426 {"preserve-dates", no_argument
, 0, 'p'},
427 {"pure", no_argument
, 0, OPTION_PURE
},
428 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
429 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
430 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
431 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
432 {"remove-section", required_argument
, 0, 'R'},
433 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
434 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
435 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
436 {"set-start", required_argument
, 0, OPTION_SET_START
},
437 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
438 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
439 {"strip-all", no_argument
, 0, 'S'},
440 {"strip-debug", no_argument
, 0, 'g'},
441 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
442 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
443 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
444 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
445 {"strip-symbol", required_argument
, 0, 'N'},
446 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
447 {"target", required_argument
, 0, 'F'},
448 {"verbose", no_argument
, 0, 'v'},
449 {"version", no_argument
, 0, 'V'},
450 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
451 {"weaken-symbol", required_argument
, 0, 'W'},
452 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
453 {"wildcard", no_argument
, 0, 'w'},
454 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
455 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
456 {"heap", required_argument
, 0, OPTION_HEAP
},
457 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
458 {"section-alignment", required_argument
, 0, OPTION_SECTION_ALIGNMENT
},
459 {"stack", required_argument
, 0, OPTION_STACK
},
460 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
461 {0, no_argument
, 0, 0}
465 extern char *program_name
;
467 /* This flag distinguishes between strip and objcopy:
468 1 means this is 'strip'; 0 means this is 'objcopy'.
469 -1 means if we should use argv[0] to decide. */
472 /* The maximum length of an S record. This variable is declared in srec.c
473 and can be modified by the --srec-len parameter. */
474 extern unsigned int Chunk
;
476 /* Restrict the generation of Srecords to type S3 only.
477 This variable is declare in bfd/srec.c and can be toggled
478 on by the --srec-forceS3 command line switch. */
479 extern bfd_boolean S3Forced
;
481 /* Forward declarations. */
482 static void setup_section (bfd
*, asection
*, void *);
483 static void setup_bfd_headers (bfd
*, bfd
*);
484 static void copy_relocations_in_section (bfd
*, asection
*, void *);
485 static void copy_section (bfd
*, asection
*, void *);
486 static void get_sections (bfd
*, asection
*, void *);
487 static int compare_section_lma (const void *, const void *);
488 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
489 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
490 static const char *lookup_sym_redefinition (const char *);
493 copy_usage (FILE *stream
, int exit_status
)
495 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
496 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
497 fprintf (stream
, _(" The options are:\n"));
498 fprintf (stream
, _("\
499 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
500 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
501 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
502 -F --target <bfdname> Set both input and output format to <bfdname>\n\
503 --debugging Convert debugging information, if possible\n\
504 -p --preserve-dates Copy modified/access timestamps to the output\n"));
505 if (DEFAULT_AR_DETERMINISTIC
)
506 fprintf (stream
, _("\
507 -D --enable-deterministic-archives\n\
508 Produce deterministic output when stripping archives (default)\n\
509 -U --disable-deterministic-archives\n\
510 Disable -D behavior\n"));
512 fprintf (stream
, _("\
513 -D --enable-deterministic-archives\n\
514 Produce deterministic output when stripping archives\n\
515 -U --disable-deterministic-archives\n\
516 Disable -D behavior (default)\n"));
517 fprintf (stream
, _("\
518 -j --only-section <name> Only copy section <name> into the output\n\
519 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
520 -R --remove-section <name> Remove section <name> from the output\n\
521 -S --strip-all Remove all symbol and relocation information\n\
522 -g --strip-debug Remove all debugging symbols & sections\n\
523 --strip-dwo Remove all DWO sections\n\
524 --strip-unneeded Remove all symbols not needed by relocations\n\
525 -N --strip-symbol <name> Do not copy symbol <name>\n\
526 --strip-unneeded-symbol <name>\n\
527 Do not copy symbol <name> unless needed by\n\
529 --only-keep-debug Strip everything but the debug information\n\
530 --extract-dwo Copy only DWO sections\n\
531 --extract-symbol Remove section contents but keep symbols\n\
532 -K --keep-symbol <name> Do not strip symbol <name>\n\
533 --keep-file-symbols Do not strip file symbol(s)\n\
534 --localize-hidden Turn all ELF hidden symbols into locals\n\
535 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
536 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
537 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
538 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
539 --weaken Force all global symbols to be marked as weak\n\
540 -w --wildcard Permit wildcard in symbol comparison\n\
541 -x --discard-all Remove all non-global symbols\n\
542 -X --discard-locals Remove any compiler-generated symbols\n\
543 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
544 --interleave-width <number> Set N for --interleave\n\
545 -b --byte <num> Select byte <num> in every interleaved block\n\
546 --gap-fill <val> Fill gaps between sections with <val>\n\
547 --pad-to <addr> Pad the last section up to address <addr>\n\
548 --set-start <addr> Set the start address to <addr>\n\
549 {--change-start|--adjust-start} <incr>\n\
550 Add <incr> to the start address\n\
551 {--change-addresses|--adjust-vma} <incr>\n\
552 Add <incr> to LMA, VMA and start addresses\n\
553 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
554 Change LMA and VMA of section <name> by <val>\n\
555 --change-section-lma <name>{=|+|-}<val>\n\
556 Change the LMA of section <name> by <val>\n\
557 --change-section-vma <name>{=|+|-}<val>\n\
558 Change the VMA of section <name> by <val>\n\
559 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
560 Warn if a named section does not exist\n\
561 --set-section-flags <name>=<flags>\n\
562 Set section <name>'s properties to <flags>\n\
563 --add-section <name>=<file> Add section <name> found in <file> to output\n\
564 --update-section <name>=<file>\n\
565 Update contents of section <name> with\n\
566 contents found in <file>\n\
567 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
568 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
569 --long-section-names {enable|disable|keep}\n\
570 Handle long section names in Coff objects.\n\
571 --change-leading-char Force output format's leading character style\n\
572 --remove-leading-char Remove leading character from global symbols\n\
573 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
574 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
575 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
577 --srec-len <number> Restrict the length of generated Srecords\n\
578 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
579 --strip-symbols <file> -N for all symbols listed in <file>\n\
580 --strip-unneeded-symbols <file>\n\
581 --strip-unneeded-symbol for all symbols listed\n\
583 --keep-symbols <file> -K for all symbols listed in <file>\n\
584 --localize-symbols <file> -L for all symbols listed in <file>\n\
585 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
586 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
587 --weaken-symbols <file> -W for all symbols listed in <file>\n\
588 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
589 --writable-text Mark the output text as writable\n\
590 --readonly-text Make the output text write protected\n\
591 --pure Mark the output file as demand paged\n\
592 --impure Mark the output file as impure\n\
593 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
594 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
595 --prefix-alloc-sections <prefix>\n\
596 Add <prefix> to start of every allocatable\n\
598 --file-alignment <num> Set PE file alignment to <num>\n\
599 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
601 --image-base <address> Set PE image base to <address>\n\
602 --section-alignment <num> Set PE section alignment to <num>\n\
603 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
605 --subsystem <name>[:<version>]\n\
606 Set PE subsystem to <name> [& <version>]\n\
607 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
608 Compress DWARF debug sections using zlib\n\
609 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
610 -v --verbose List all object files modified\n\
611 @<file> Read options from <file>\n\
612 -V --version Display this program's version number\n\
613 -h --help Display this output\n\
614 --info List object formats & architectures supported\n\
616 list_supported_targets (program_name
, stream
);
617 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
618 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
623 strip_usage (FILE *stream
, int exit_status
)
625 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
626 fprintf (stream
, _(" Removes symbols and sections from files\n"));
627 fprintf (stream
, _(" The options are:\n"));
628 fprintf (stream
, _("\
629 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
630 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
631 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
632 -p --preserve-dates Copy modified/access timestamps to the output\n\
634 if (DEFAULT_AR_DETERMINISTIC
)
635 fprintf (stream
, _("\
636 -D --enable-deterministic-archives\n\
637 Produce deterministic output when stripping archives (default)\n\
638 -U --disable-deterministic-archives\n\
639 Disable -D behavior\n"));
641 fprintf (stream
, _("\
642 -D --enable-deterministic-archives\n\
643 Produce deterministic output when stripping archives\n\
644 -U --disable-deterministic-archives\n\
645 Disable -D behavior (default)\n"));
646 fprintf (stream
, _("\
647 -R --remove-section=<name> Also remove section <name> from the output\n\
648 -s --strip-all Remove all symbol and relocation information\n\
649 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
650 --strip-dwo Remove all DWO sections\n\
651 --strip-unneeded Remove all symbols not needed by relocations\n\
652 --only-keep-debug Strip everything but the debug information\n\
653 -N --strip-symbol=<name> Do not copy symbol <name>\n\
654 -K --keep-symbol=<name> Do not strip symbol <name>\n\
655 --keep-file-symbols Do not strip file symbol(s)\n\
656 -w --wildcard Permit wildcard in symbol comparison\n\
657 -x --discard-all Remove all non-global symbols\n\
658 -X --discard-locals Remove any compiler-generated symbols\n\
659 -v --verbose List all object files modified\n\
660 -V --version Display this program's version number\n\
661 -h --help Display this output\n\
662 --info List object formats & architectures supported\n\
663 -o <file> Place stripped output into <file>\n\
666 list_supported_targets (program_name
, stream
);
667 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
668 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
672 /* Parse section flags into a flagword, with a fatal error if the
673 string can't be parsed. */
676 parse_flags (const char *s
)
686 snext
= strchr (s
, ',');
696 #define PARSE_FLAG(fname,fval) \
697 else if (strncasecmp (fname, s, len) == 0) ret |= fval
698 PARSE_FLAG ("alloc", SEC_ALLOC
);
699 PARSE_FLAG ("load", SEC_LOAD
);
700 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
701 PARSE_FLAG ("readonly", SEC_READONLY
);
702 PARSE_FLAG ("debug", SEC_DEBUGGING
);
703 PARSE_FLAG ("code", SEC_CODE
);
704 PARSE_FLAG ("data", SEC_DATA
);
705 PARSE_FLAG ("rom", SEC_ROM
);
706 PARSE_FLAG ("share", SEC_COFF_SHARED
);
707 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
708 PARSE_FLAG ("merge", SEC_MERGE
);
709 PARSE_FLAG ("strings", SEC_STRINGS
);
715 copy
= (char *) xmalloc (len
+ 1);
716 strncpy (copy
, s
, len
);
718 non_fatal (_("unrecognized section flag `%s'"), copy
);
719 fatal (_("supported flags: %s"),
720 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
730 /* Find and optionally add an entry in the change_sections list.
732 We need to be careful in how we match section names because of the support
733 for wildcard characters. For example suppose that the user has invoked
736 --set-section-flags .debug_*=debug
737 --set-section-flags .debug_str=readonly,debug
738 --change-section-address .debug_*ranges=0x1000
740 With the idea that all debug sections will receive the DEBUG flag, the
741 .debug_str section will also receive the READONLY flag and the
742 .debug_ranges and .debug_aranges sections will have their address set to
743 0x1000. (This may not make much sense, but it is just an example).
745 When adding the section name patterns to the section list we need to make
746 sure that previous entries do not match with the new entry, unless the
747 match is exact. (In which case we assume that the user is overriding
748 the previous entry with the new context).
750 When matching real section names to the section list we make use of the
751 wildcard characters, but we must do so in context. Eg if we are setting
752 section addresses then we match for .debug_ranges but not for .debug_info.
754 Finally, if ADD is false and we do find a match, we mark the section list
757 static struct section_list
*
758 find_section_list (const char *name
, bfd_boolean add
, unsigned int context
)
760 struct section_list
*p
;
762 /* assert ((context & ((1 << 7) - 1)) != 0); */
764 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
768 if (strcmp (p
->pattern
, name
) == 0)
770 /* Check for context conflicts. */
771 if (((p
->context
& SECTION_CONTEXT_REMOVE
)
772 && (context
& SECTION_CONTEXT_COPY
))
773 || ((context
& SECTION_CONTEXT_REMOVE
)
774 && (p
->context
& SECTION_CONTEXT_COPY
)))
775 fatal (_("error: %s both copied and removed"), name
);
777 if (((p
->context
& SECTION_CONTEXT_SET_VMA
)
778 && (context
& SECTION_CONTEXT_ALTER_VMA
))
779 || ((context
& SECTION_CONTEXT_SET_VMA
)
780 && (context
& SECTION_CONTEXT_ALTER_VMA
)))
781 fatal (_("error: %s both sets and alters VMA"), name
);
783 if (((p
->context
& SECTION_CONTEXT_SET_LMA
)
784 && (context
& SECTION_CONTEXT_ALTER_LMA
))
785 || ((context
& SECTION_CONTEXT_SET_LMA
)
786 && (context
& SECTION_CONTEXT_ALTER_LMA
)))
787 fatal (_("error: %s both sets and alters LMA"), name
);
789 /* Extend the context. */
790 p
->context
|= context
;
794 /* If we are not adding a new name/pattern then
795 only check for a match if the context applies. */
796 else if ((p
->context
& context
)
797 /* We could check for the presence of wildchar characters
798 first and choose between calling strcmp and fnmatch,
799 but is that really worth it ? */
800 && fnmatch (p
->pattern
, name
, 0) == 0)
810 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
813 p
->context
= context
;
817 p
->next
= change_sections
;
823 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
826 eq_string (const void *s1
, const void *s2
)
828 return strcmp ((const char *) s1
, (const char *) s2
) == 0;
832 create_symbol_htab (void)
834 return htab_create_alloc (16, htab_hash_string
, eq_string
, NULL
, xcalloc
, free
);
838 create_symbol_htabs (void)
840 strip_specific_htab
= create_symbol_htab ();
841 strip_unneeded_htab
= create_symbol_htab ();
842 keep_specific_htab
= create_symbol_htab ();
843 localize_specific_htab
= create_symbol_htab ();
844 globalize_specific_htab
= create_symbol_htab ();
845 keepglobal_specific_htab
= create_symbol_htab ();
846 weaken_specific_htab
= create_symbol_htab ();
849 /* Add a symbol to strip_specific_list. */
852 add_specific_symbol (const char *name
, htab_t htab
)
854 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
857 /* Add symbols listed in `filename' to strip_specific_list. */
859 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
860 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
863 add_specific_symbols (const char *filename
, htab_t htab
)
869 unsigned int line_count
;
871 size
= get_file_size (filename
);
878 buffer
= (char *) xmalloc (size
+ 2);
879 f
= fopen (filename
, FOPEN_RT
);
881 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
883 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
884 fatal (_("%s: fread failed"), filename
);
887 buffer
[size
] = '\n';
888 buffer
[size
+ 1] = '\0';
892 for (line
= buffer
; * line
!= '\0'; line
++)
897 int finished
= FALSE
;
899 for (eol
= line
;; eol
++)
905 /* Cope with \n\r. */
913 /* Cope with \r\n. */
924 /* Line comment, Terminate the line here, in case a
925 name is present and then allow the rest of the
926 loop to find the real end of the line. */
938 /* A name may now exist somewhere between 'line' and 'eol'.
939 Strip off leading whitespace and trailing whitespace,
940 then add it to the list. */
941 for (name
= line
; IS_WHITESPACE (* name
); name
++)
943 for (name_end
= name
;
944 (! IS_WHITESPACE (* name_end
))
945 && (! IS_LINE_TERMINATOR (* name_end
));
949 if (! IS_LINE_TERMINATOR (* name_end
))
953 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
956 if (! IS_LINE_TERMINATOR (* extra
))
957 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
958 filename
, line_count
);
964 add_specific_symbol (name
, htab
);
966 /* Advance line pointer to end of line. The 'eol ++' in the for
967 loop above will then advance us to the start of the next line. */
973 /* See whether a symbol should be stripped or kept
974 based on strip_specific_list and keep_symbols. */
977 is_specified_symbol_predicate (void **slot
, void *data
)
979 struct is_specified_symbol_predicate_data
*d
=
980 (struct is_specified_symbol_predicate_data
*) data
;
981 const char *slot_name
= (char *) *slot
;
983 if (*slot_name
!= '!')
985 if (! fnmatch (slot_name
, d
->name
, 0))
988 /* Stop traversal. */
994 if (fnmatch (slot_name
+ 1, d
->name
, 0))
997 /* Stop traversal. */
1002 /* Continue traversal. */
1007 is_specified_symbol (const char *name
, htab_t htab
)
1011 struct is_specified_symbol_predicate_data data
;
1016 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
1021 return htab_find (htab
, name
) != NULL
;
1024 /* Return a pointer to the symbol used as a signature for GROUP. */
1027 group_signature (asection
*group
)
1029 bfd
*abfd
= group
->owner
;
1030 Elf_Internal_Shdr
*ghdr
;
1032 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
1035 ghdr
= &elf_section_data (group
)->this_hdr
;
1036 if (ghdr
->sh_link
< elf_numsections (abfd
))
1038 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1039 Elf_Internal_Shdr
*symhdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
1041 if (symhdr
->sh_type
== SHT_SYMTAB
1042 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
1043 return isympp
[ghdr
->sh_info
- 1];
1048 /* Return TRUE if the section is a DWO section. */
1051 is_dwo_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1053 const char *name
= bfd_get_section_name (abfd
, sec
);
1054 int len
= strlen (name
);
1056 return strncmp (name
+ len
- 4, ".dwo", 4) == 0;
1059 /* Return TRUE if section SEC is in the update list. */
1062 is_update_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1064 if (update_sections
!= NULL
)
1066 struct section_add
*pupdate
;
1068 for (pupdate
= update_sections
;
1070 pupdate
= pupdate
->next
)
1072 if (strcmp (sec
->name
, pupdate
->name
) == 0)
1080 /* See if a non-group section is being removed. */
1083 is_strip_section_1 (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1085 if (sections_removed
|| sections_copied
)
1087 struct section_list
*p
;
1088 struct section_list
*q
;
1090 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
,
1091 SECTION_CONTEXT_REMOVE
);
1092 q
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
,
1093 SECTION_CONTEXT_COPY
);
1096 fatal (_("error: section %s matches both remove and copy options"),
1097 bfd_get_section_name (abfd
, sec
));
1098 if (p
&& is_update_section (abfd
, sec
))
1099 fatal (_("error: section %s matches both update and remove options"),
1100 bfd_get_section_name (abfd
, sec
));
1104 if (sections_copied
&& q
== NULL
)
1108 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
1110 if (strip_symbols
== STRIP_DEBUG
1111 || strip_symbols
== STRIP_UNNEEDED
1112 || strip_symbols
== STRIP_ALL
1113 || discard_locals
== LOCALS_ALL
1114 || convert_debugging
)
1116 /* By default we don't want to strip .reloc section.
1117 This section has for pe-coff special meaning. See
1118 pe-dll.c file in ld, and peXXigen.c in bfd for details. */
1119 if (strcmp (bfd_get_section_name (abfd
, sec
), ".reloc") != 0)
1123 if (strip_symbols
== STRIP_DWO
)
1124 return is_dwo_section (abfd
, sec
);
1126 if (strip_symbols
== STRIP_NONDEBUG
)
1130 if (strip_symbols
== STRIP_NONDWO
)
1131 return !is_dwo_section (abfd
, sec
);
1136 /* See if a section is being removed. */
1139 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1141 if (is_strip_section_1 (abfd
, sec
))
1144 if ((bfd_get_section_flags (abfd
, sec
) & SEC_GROUP
) != 0)
1148 asection
*elt
, *first
;
1151 If we are going to strip the group signature symbol, then
1152 strip the group section too. */
1153 gsym
= group_signature (sec
);
1158 if ((strip_symbols
== STRIP_ALL
1159 && !is_specified_symbol (gname
, keep_specific_htab
))
1160 || is_specified_symbol (gname
, strip_specific_htab
))
1163 /* Remove the group section if all members are removed. */
1164 first
= elt
= elf_next_in_group (sec
);
1167 if (!is_strip_section_1 (abfd
, elt
))
1169 elt
= elf_next_in_group (elt
);
1181 is_nondebug_keep_contents_section (bfd
*ibfd
, asection
*isection
)
1183 /* Always keep ELF note sections. */
1184 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
1185 return (elf_section_type (isection
) == SHT_NOTE
);
1187 /* Always keep the .buildid section for PE/COFF.
1189 Strictly, this should be written "always keep the section storing the debug
1190 directory", but that may be the .text section for objects produced by some
1191 tools, which it is not sensible to keep. */
1192 if (ibfd
->xvec
->flavour
== bfd_target_coff_flavour
)
1193 return (strcmp (bfd_get_section_name (ibfd
, isection
), ".buildid") == 0);
1198 /* Return true if SYM is a hidden symbol. */
1201 is_hidden_symbol (asymbol
*sym
)
1203 elf_symbol_type
*elf_sym
;
1205 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
1206 if (elf_sym
!= NULL
)
1207 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
1216 /* Choose which symbol entries to copy; put the result in OSYMS.
1217 We don't copy in place, because that confuses the relocs.
1218 Return the number of symbols to print. */
1221 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
1222 asymbol
**isyms
, long symcount
)
1224 asymbol
**from
= isyms
, **to
= osyms
;
1225 long src_count
= 0, dst_count
= 0;
1226 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
1228 for (; src_count
< symcount
; src_count
++)
1230 asymbol
*sym
= from
[src_count
];
1231 flagword flags
= sym
->flags
;
1232 char *name
= (char *) bfd_asymbol_name (sym
);
1234 bfd_boolean used_in_reloc
= FALSE
;
1235 bfd_boolean undefined
;
1236 bfd_boolean rem_leading_char
;
1237 bfd_boolean add_leading_char
;
1239 undefined
= bfd_is_und_section (bfd_get_section (sym
));
1241 if (redefine_sym_list
)
1243 char *old_name
, *new_name
;
1245 old_name
= (char *) bfd_asymbol_name (sym
);
1246 new_name
= (char *) lookup_sym_redefinition (old_name
);
1247 bfd_asymbol_name (sym
) = new_name
;
1251 /* Check if we will remove the current leading character. */
1253 (name
[0] == bfd_get_symbol_leading_char (abfd
))
1254 && (change_leading_char
1255 || (remove_leading_char
1256 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1258 || bfd_is_com_section (bfd_get_section (sym
)))));
1260 /* Check if we will add a new leading character. */
1263 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1264 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1265 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1267 /* Short circuit for change_leading_char if we can do it in-place. */
1268 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1270 name
[0] = bfd_get_symbol_leading_char (obfd
);
1271 bfd_asymbol_name (sym
) = name
;
1272 rem_leading_char
= FALSE
;
1273 add_leading_char
= FALSE
;
1276 /* Remove leading char. */
1277 if (rem_leading_char
)
1278 bfd_asymbol_name (sym
) = ++name
;
1280 /* Add new leading char and/or prefix. */
1281 if (add_leading_char
|| prefix_symbols_string
)
1285 ptr
= n
= (char *) xmalloc (1 + strlen (prefix_symbols_string
)
1286 + strlen (name
) + 1);
1287 if (add_leading_char
)
1288 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1290 if (prefix_symbols_string
)
1292 strcpy (ptr
, prefix_symbols_string
);
1293 ptr
+= strlen (prefix_symbols_string
);
1297 bfd_asymbol_name (sym
) = n
;
1301 if (strip_symbols
== STRIP_ALL
)
1303 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1304 || ((flags
& BSF_SECTION_SYM
) != 0
1305 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
1309 used_in_reloc
= TRUE
;
1311 else if (relocatable
/* Relocatable file. */
1312 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1313 || bfd_is_com_section (bfd_get_section (sym
))))
1315 else if (bfd_decode_symclass (sym
) == 'I')
1316 /* Global symbols in $idata sections need to be retained
1317 even if relocatable is FALSE. External users of the
1318 library containing the $idata section may reference these
1321 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1322 || (flags
& BSF_WEAK
) != 0
1324 || bfd_is_com_section (bfd_get_section (sym
)))
1325 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1326 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1327 keep
= (strip_symbols
!= STRIP_DEBUG
1328 && strip_symbols
!= STRIP_UNNEEDED
1329 && ! convert_debugging
);
1330 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
1331 /* COMDAT sections store special information in local
1332 symbols, so we cannot risk stripping any of them. */
1334 else /* Local symbol. */
1335 keep
= (strip_symbols
!= STRIP_UNNEEDED
1336 && (discard_locals
!= LOCALS_ALL
1337 && (discard_locals
!= LOCALS_START_L
1338 || ! bfd_is_local_label (abfd
, sym
))));
1340 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1342 /* There are multiple ways to set 'keep' above, but if it
1343 was the relocatable symbol case, then that's an error. */
1346 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1354 && !(flags
& BSF_KEEP
)
1355 && is_specified_symbol (name
, strip_unneeded_htab
))
1359 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1360 || is_specified_symbol (name
, keep_specific_htab
)))
1363 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
1368 if ((flags
& BSF_GLOBAL
) != 0
1369 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1371 sym
->flags
&= ~ BSF_GLOBAL
;
1372 sym
->flags
|= BSF_WEAK
;
1376 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1377 && (is_specified_symbol (name
, localize_specific_htab
)
1378 || (htab_elements (keepglobal_specific_htab
) != 0
1379 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1380 || (localize_hidden
&& is_hidden_symbol (sym
))))
1382 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1383 sym
->flags
|= BSF_LOCAL
;
1387 && (flags
& BSF_LOCAL
)
1388 && is_specified_symbol (name
, globalize_specific_htab
))
1390 sym
->flags
&= ~ BSF_LOCAL
;
1391 sym
->flags
|= BSF_GLOBAL
;
1394 to
[dst_count
++] = sym
;
1398 to
[dst_count
] = NULL
;
1403 /* Find the redefined name of symbol SOURCE. */
1406 lookup_sym_redefinition (const char *source
)
1408 struct redefine_node
*list
;
1410 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1411 if (strcmp (source
, list
->source
) == 0)
1412 return list
->target
;
1417 /* Add a node to a symbol redefine list. */
1420 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1422 struct redefine_node
**p
;
1423 struct redefine_node
*list
;
1424 struct redefine_node
*new_node
;
1426 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1428 if (strcmp (source
, list
->source
) == 0)
1429 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1432 if (strcmp (target
, list
->target
) == 0)
1433 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1437 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
1439 new_node
->source
= strdup (source
);
1440 new_node
->target
= strdup (target
);
1441 new_node
->next
= NULL
;
1446 /* Handle the --redefine-syms option. Read lines containing "old new"
1447 from the file, and add them to the symbol redefine list. */
1450 add_redefine_syms_file (const char *filename
)
1459 file
= fopen (filename
, "r");
1461 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1462 filename
, strerror (errno
));
1465 buf
= (char *) xmalloc (bufsize
+ 1 /* For the terminating NUL. */);
1473 /* Collect the input symbol name. */
1474 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1482 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1490 /* Eat white space between the symbol names. */
1491 while (IS_WHITESPACE (c
))
1493 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1498 /* Collect the output symbol name. */
1500 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1508 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1516 /* Eat white space at end of line. */
1517 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1522 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1523 || c
== '\n' || c
== EOF
)
1526 /* Append the redefinition to the list. */
1528 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1539 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1541 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1542 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1545 /* Eat the rest of the line and finish it. */
1546 while (c
!= '\n' && c
!= EOF
)
1552 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1557 /* Copy unkown object file IBFD onto OBFD.
1558 Returns TRUE upon success, FALSE otherwise. */
1561 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1569 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1571 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1578 non_fatal (_("stat returns negative size for `%s'"),
1579 bfd_get_archive_filename (ibfd
));
1583 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1585 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1590 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1591 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1593 cbuf
= (char *) xmalloc (BUFSIZE
);
1595 while (ncopied
< size
)
1597 tocopy
= size
- ncopied
;
1598 if (tocopy
> BUFSIZE
)
1601 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1602 != (bfd_size_type
) tocopy
)
1604 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1609 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1610 != (bfd_size_type
) tocopy
)
1612 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1620 /* We should at least to be able to read it back when copying an
1621 unknown object in an archive. */
1622 chmod (bfd_get_filename (obfd
), buf
.st_mode
| S_IRUSR
);
1627 /* Copy object file IBFD onto OBFD.
1628 Returns TRUE upon success, FALSE otherwise. */
1631 copy_object (bfd
*ibfd
, bfd
*obfd
, const bfd_arch_info_type
*input_arch
)
1635 asection
**osections
= NULL
;
1636 asection
*gnu_debuglink_section
= NULL
;
1637 bfd_size_type
*gaps
= NULL
;
1638 bfd_size_type max_gap
= 0;
1641 enum bfd_architecture iarch
;
1644 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1645 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1646 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1648 /* PR 17636: Call non-fatal so that we return to our parent who
1649 may need to tidy temporary files. */
1650 non_fatal (_("Unable to change endianness of input file(s)"));
1654 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1656 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1660 if (ibfd
->sections
== NULL
)
1662 non_fatal (_("error: the input file '%s' has no sections"),
1663 bfd_get_archive_filename (ibfd
));
1668 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1669 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1670 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1679 start
= bfd_get_start_address (ibfd
);
1680 start
+= change_start
;
1683 /* Neither the start address nor the flags
1684 need to be set for a core file. */
1685 if (bfd_get_format (obfd
) != bfd_core
)
1689 flags
= bfd_get_file_flags (ibfd
);
1690 flags
|= bfd_flags_to_set
;
1691 flags
&= ~bfd_flags_to_clear
;
1692 flags
&= bfd_applicable_file_flags (obfd
);
1694 if (strip_symbols
== STRIP_ALL
)
1695 flags
&= ~HAS_RELOC
;
1697 if (!bfd_set_start_address (obfd
, start
)
1698 || !bfd_set_file_flags (obfd
, flags
))
1700 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1705 /* Copy architecture of input file to output file. */
1706 iarch
= bfd_get_arch (ibfd
);
1707 imach
= bfd_get_mach (ibfd
);
1710 if (bfd_get_arch_info (ibfd
) == NULL
1711 || bfd_get_arch_info (ibfd
)->arch
== bfd_arch_unknown
)
1713 iarch
= input_arch
->arch
;
1714 imach
= input_arch
->mach
;
1717 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1718 bfd_get_archive_filename (ibfd
));
1720 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1721 && (ibfd
->target_defaulted
1722 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1724 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1725 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1726 bfd_get_archive_filename (ibfd
));
1728 non_fatal (_("Output file cannot represent architecture `%s'"),
1729 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1730 bfd_get_mach (ibfd
)));
1734 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1736 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1740 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1741 && bfd_pei_p (obfd
))
1743 /* Set up PE parameters. */
1744 pe_data_type
*pe
= pe_data (obfd
);
1746 /* Copy PE parameters before changing them. */
1747 if (ibfd
->xvec
->flavour
== bfd_target_coff_flavour
1748 && bfd_pei_p (ibfd
))
1749 pe
->pe_opthdr
= pe_data (ibfd
)->pe_opthdr
;
1751 if (pe_file_alignment
!= (bfd_vma
) -1)
1752 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
1754 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
1756 if (pe_heap_commit
!= (bfd_vma
) -1)
1757 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
1759 if (pe_heap_reserve
!= (bfd_vma
) -1)
1760 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
1762 if (pe_image_base
!= (bfd_vma
) -1)
1763 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
1765 if (pe_section_alignment
!= (bfd_vma
) -1)
1766 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
1768 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
1770 if (pe_stack_commit
!= (bfd_vma
) -1)
1771 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
1773 if (pe_stack_reserve
!= (bfd_vma
) -1)
1774 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_reserve
;
1776 if (pe_subsystem
!= -1)
1777 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
1779 if (pe_major_subsystem_version
!= -1)
1780 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
1782 if (pe_minor_subsystem_version
!= -1)
1783 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
1785 if (pe_file_alignment
> pe_section_alignment
)
1787 char file_alignment
[20], section_alignment
[20];
1789 sprintf_vma (file_alignment
, pe_file_alignment
);
1790 sprintf_vma (section_alignment
, pe_section_alignment
);
1791 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1793 file_alignment
, section_alignment
);
1800 if (osympp
!= isympp
)
1806 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1809 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1813 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1814 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1817 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1820 /* PR 17512: file: d6323821
1821 If the symbol table could not be loaded do not pretend that we have
1822 any symbols. This trips us up later on when we load the relocs. */
1826 osympp
= isympp
= NULL
;
1829 /* BFD mandates that all output sections be created and sizes set before
1830 any output is done. Thus, we traverse all sections multiple times. */
1831 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1833 if (!extract_symbol
)
1834 setup_bfd_headers (ibfd
, obfd
);
1836 if (add_sections
!= NULL
)
1838 struct section_add
*padd
;
1839 struct section_list
*pset
;
1841 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1845 pset
= find_section_list (padd
->name
, FALSE
,
1846 SECTION_CONTEXT_SET_FLAGS
);
1848 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1850 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1852 /* bfd_make_section_with_flags() does not return very helpful
1853 error codes, so check for the most likely user error first. */
1854 if (bfd_get_section_by_name (obfd
, padd
->name
))
1856 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1857 _("can't add section '%s'"), padd
->name
);
1862 /* We use LINKER_CREATED here so that the backend hooks
1863 will create any special section type information,
1864 instead of presuming we know what we're doing merely
1865 because we set the flags. */
1866 padd
->section
= bfd_make_section_with_flags
1867 (obfd
, padd
->name
, flags
| SEC_LINKER_CREATED
);
1868 if (padd
->section
== NULL
)
1870 bfd_nonfatal_message (NULL
, obfd
, NULL
,
1871 _("can't create section `%s'"),
1877 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1879 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1883 pset
= find_section_list (padd
->name
, FALSE
,
1884 SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
);
1886 && ! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
1888 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1892 pset
= find_section_list (padd
->name
, FALSE
,
1893 SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
);
1896 padd
->section
->lma
= pset
->lma_val
;
1898 if (! bfd_set_section_alignment
1899 (obfd
, padd
->section
,
1900 bfd_section_alignment (obfd
, padd
->section
)))
1902 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
1909 if (update_sections
!= NULL
)
1911 struct section_add
*pupdate
;
1913 for (pupdate
= update_sections
;
1915 pupdate
= pupdate
->next
)
1919 pupdate
->section
= bfd_get_section_by_name (ibfd
, pupdate
->name
);
1920 if (pupdate
->section
== NULL
)
1922 non_fatal (_("error: %s not found, can't be updated"), pupdate
->name
);
1926 osec
= pupdate
->section
->output_section
;
1927 if (! bfd_set_section_size (obfd
, osec
, pupdate
->size
))
1929 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
1935 if (dump_sections
!= NULL
)
1937 struct section_add
* pdump
;
1939 for (pdump
= dump_sections
; pdump
!= NULL
; pdump
= pdump
->next
)
1943 sec
= bfd_get_section_by_name (ibfd
, pdump
->name
);
1946 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
1947 _("can't dump section '%s' - it does not exist"),
1952 if ((bfd_get_section_flags (ibfd
, sec
) & SEC_HAS_CONTENTS
) == 0)
1954 bfd_nonfatal_message (NULL
, ibfd
, sec
,
1955 _("can't dump section - it has no contents"));
1959 bfd_size_type size
= bfd_get_section_size (sec
);
1962 bfd_nonfatal_message (NULL
, ibfd
, sec
,
1963 _("can't dump section - it is empty"));
1968 f
= fopen (pdump
->filename
, FOPEN_WB
);
1971 bfd_nonfatal_message (pdump
->filename
, NULL
, NULL
,
1972 _("could not open section dump file"));
1976 bfd_byte
* contents
= xmalloc (size
);
1977 if (bfd_get_section_contents (ibfd
, sec
, contents
, 0, size
))
1979 if (fwrite (contents
, 1, size
, f
) != size
)
1981 non_fatal (_("error writing section contents to %s (error: %s)"),
1988 bfd_nonfatal_message (NULL
, ibfd
, sec
,
1989 _("could not retrieve section contents"));
1996 if (gnu_debuglink_filename
!= NULL
)
1998 /* PR 15125: Give a helpful warning message if
1999 the debuglink section already exists, and
2000 allow the rest of the copy to complete. */
2001 if (bfd_get_section_by_name (obfd
, ".gnu_debuglink"))
2003 non_fatal (_("%s: debuglink section already exists"),
2004 bfd_get_filename (obfd
));
2005 gnu_debuglink_filename
= NULL
;
2009 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
2010 (obfd
, gnu_debuglink_filename
);
2012 if (gnu_debuglink_section
== NULL
)
2014 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2015 _("cannot create debug link section `%s'"),
2016 gnu_debuglink_filename
);
2020 /* Special processing for PE format files. We
2021 have no way to distinguish PE from COFF here. */
2022 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
2024 bfd_vma debuglink_vma
;
2025 asection
* highest_section
;
2028 /* The PE spec requires that all sections be adjacent and sorted
2029 in ascending order of VMA. It also specifies that debug
2030 sections should be last. This is despite the fact that debug
2031 sections are not loaded into memory and so in theory have no
2034 This means that the debuglink section must be given a non-zero
2035 VMA which makes it contiguous with other debug sections. So
2036 walk the current section list, find the section with the
2037 highest VMA and start the debuglink section after that one. */
2038 for (sec
= obfd
->sections
, highest_section
= NULL
;
2042 && (highest_section
== NULL
2043 || sec
->vma
> highest_section
->vma
))
2044 highest_section
= sec
;
2046 if (highest_section
)
2047 debuglink_vma
= BFD_ALIGN (highest_section
->vma
2048 + highest_section
->size
,
2049 /* FIXME: We ought to be using
2050 COFF_PAGE_SIZE here or maybe
2051 bfd_get_section_alignment() (if it
2052 was set) but since this is for PE
2053 and we know the required alignment
2054 it is easier just to hard code it. */
2057 /* Umm, not sure what to do in this case. */
2058 debuglink_vma
= 0x1000;
2060 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
2065 if (bfd_count_sections (obfd
) != 0
2066 && (gap_fill_set
|| pad_to_set
))
2071 /* We must fill in gaps between the sections and/or we must pad
2072 the last section to a specified address. We do this by
2073 grabbing a list of the sections, sorting them by VMA, and
2074 increasing the section sizes as required to fill the gaps.
2075 We write out the gap contents below. */
2077 c
= bfd_count_sections (obfd
);
2078 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
2080 bfd_map_over_sections (obfd
, get_sections
, &set
);
2082 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
2084 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
2085 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
2089 for (i
= 0; i
< c
- 1; i
++)
2093 bfd_vma gap_start
, gap_stop
;
2095 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
2096 if ((flags
& SEC_HAS_CONTENTS
) == 0
2097 || (flags
& SEC_LOAD
) == 0)
2100 size
= bfd_section_size (obfd
, osections
[i
]);
2101 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
2102 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
2103 if (gap_start
< gap_stop
)
2105 if (! bfd_set_section_size (obfd
, osections
[i
],
2106 size
+ (gap_stop
- gap_start
)))
2108 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
2109 _("Can't fill gap after section"));
2113 gaps
[i
] = gap_stop
- gap_start
;
2114 if (max_gap
< gap_stop
- gap_start
)
2115 max_gap
= gap_stop
- gap_start
;
2125 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
2126 size
= bfd_section_size (obfd
, osections
[c
- 1]);
2127 if (lma
+ size
< pad_to
)
2129 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
2132 bfd_nonfatal_message (NULL
, obfd
, osections
[c
- 1],
2133 _("can't add padding"));
2138 gaps
[c
- 1] = pad_to
- (lma
+ size
);
2139 if (max_gap
< pad_to
- (lma
+ size
))
2140 max_gap
= pad_to
- (lma
+ size
);
2146 /* Symbol filtering must happen after the output sections
2147 have been created, but before their contents are set. */
2149 if (convert_debugging
)
2150 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, FALSE
);
2152 if (strip_symbols
== STRIP_DEBUG
2153 || strip_symbols
== STRIP_ALL
2154 || strip_symbols
== STRIP_UNNEEDED
2155 || strip_symbols
== STRIP_NONDEBUG
2156 || strip_symbols
== STRIP_DWO
2157 || strip_symbols
== STRIP_NONDWO
2158 || discard_locals
!= LOCALS_UNDEF
2160 || htab_elements (strip_specific_htab
) != 0
2161 || htab_elements (keep_specific_htab
) != 0
2162 || htab_elements (localize_specific_htab
) != 0
2163 || htab_elements (globalize_specific_htab
) != 0
2164 || htab_elements (keepglobal_specific_htab
) != 0
2165 || htab_elements (weaken_specific_htab
) != 0
2166 || prefix_symbols_string
2169 || convert_debugging
2170 || change_leading_char
2171 || remove_leading_char
2172 || redefine_sym_list
2175 /* Mark symbols used in output relocations so that they
2176 are kept, even if they are local labels or static symbols.
2178 Note we iterate over the input sections examining their
2179 relocations since the relocations for the output sections
2180 haven't been set yet. mark_symbols_used_in_relocations will
2181 ignore input sections which have no corresponding output
2183 if (strip_symbols
!= STRIP_ALL
)
2184 bfd_map_over_sections (ibfd
,
2185 mark_symbols_used_in_relocations
,
2187 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
2188 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
2191 if (convert_debugging
&& dhandle
!= NULL
)
2193 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
2200 bfd_set_symtab (obfd
, osympp
, symcount
);
2202 /* This has to happen before section positions are set. */
2203 bfd_map_over_sections (ibfd
, copy_relocations_in_section
, obfd
);
2205 /* This has to happen after the symbol table has been set. */
2206 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
2208 if (add_sections
!= NULL
)
2210 struct section_add
*padd
;
2212 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
2214 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
2217 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2223 if (update_sections
!= NULL
)
2225 struct section_add
*pupdate
;
2227 for (pupdate
= update_sections
;
2229 pupdate
= pupdate
->next
)
2233 osec
= pupdate
->section
->output_section
;
2234 if (! bfd_set_section_contents (obfd
, osec
, pupdate
->contents
,
2237 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
2243 if (gnu_debuglink_filename
!= NULL
)
2245 if (! bfd_fill_in_gnu_debuglink_section
2246 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
2248 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2249 _("cannot fill debug link section `%s'"),
2250 gnu_debuglink_filename
);
2255 if (gap_fill_set
|| pad_to_set
)
2260 /* Fill in the gaps. */
2263 buf
= (bfd_byte
*) xmalloc (max_gap
);
2264 memset (buf
, gap_fill
, max_gap
);
2266 c
= bfd_count_sections (obfd
);
2267 for (i
= 0; i
< c
; i
++)
2275 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
2286 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
2289 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
2300 /* Do not copy backend data if --extract-symbol is passed; anything
2301 that needs to look at the section contents will fail. */
2305 /* Allow the BFD backend to copy any private data it understands
2306 from the input BFD to the output BFD. This is done last to
2307 permit the routine to look at the filtered symbol table, which is
2308 important for the ECOFF code at least. */
2309 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
2311 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2312 _("error copying private BFD data"));
2316 /* Switch to the alternate machine code. We have to do this at the
2317 very end, because we only initialize the header when we create
2318 the first section. */
2319 if (use_alt_mach_code
!= 0)
2321 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
2323 non_fatal (_("this target does not support %lu alternative machine codes"),
2325 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2327 non_fatal (_("treating that number as an absolute e_machine value instead"));
2328 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
2331 non_fatal (_("ignoring the alternative value"));
2338 /* Read each archive element in turn from IBFD, copy the
2339 contents to temp file, and keep the temp file handle.
2340 If 'force_output_target' is TRUE then make sure that
2341 all elements in the new archive are of the type
2345 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
2346 bfd_boolean force_output_target
,
2347 const bfd_arch_info_type
*input_arch
)
2351 struct name_list
*next
;
2355 bfd
**ptr
= &obfd
->archive_head
;
2358 const char *filename
;
2360 /* Make a temp directory to hold the contents. */
2361 dir
= make_tempdir (bfd_get_filename (obfd
));
2363 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2366 if (strip_symbols
== STRIP_ALL
)
2367 obfd
->has_armap
= FALSE
;
2369 obfd
->has_armap
= ibfd
->has_armap
;
2370 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
2373 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
2377 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
2379 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2382 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
2383 goto cleanup_and_exit
;
2386 while (!status
&& this_element
!= NULL
)
2392 int stat_status
= 0;
2393 bfd_boolean del
= TRUE
;
2394 bfd_boolean ok_object
;
2396 /* PR binutils/17533: Do not allow directory traversal
2397 outside of the current directory tree by archive members. */
2398 if (! is_valid_archive_path (bfd_get_filename (this_element
)))
2400 non_fatal (_("illegal pathname found in archive member: %s"),
2401 bfd_get_filename (this_element
));
2403 goto cleanup_and_exit
;
2406 /* Create an output file for this member. */
2407 output_name
= concat (dir
, "/",
2408 bfd_get_filename (this_element
), (char *) 0);
2410 /* If the file already exists, make another temp dir. */
2411 if (stat (output_name
, &buf
) >= 0)
2413 output_name
= make_tempdir (output_name
);
2414 if (output_name
== NULL
)
2416 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
2419 goto cleanup_and_exit
;
2422 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
2423 l
->name
= output_name
;
2427 output_name
= concat (output_name
, "/",
2428 bfd_get_filename (this_element
), (char *) 0);
2433 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
2435 if (stat_status
!= 0)
2436 non_fatal (_("internal stat error on %s"),
2437 bfd_get_filename (this_element
));
2440 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
2441 l
->name
= output_name
;
2446 ok_object
= bfd_check_format (this_element
, bfd_object
);
2448 bfd_nonfatal_message (NULL
, this_element
, NULL
,
2449 _("Unable to recognise the format of file"));
2451 /* PR binutils/3110: Cope with archives
2452 containing multiple target types. */
2453 if (force_output_target
|| !ok_object
)
2454 output_bfd
= bfd_openw (output_name
, output_target
);
2456 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
2458 if (output_bfd
== NULL
)
2460 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2462 goto cleanup_and_exit
;
2467 del
= !copy_object (this_element
, output_bfd
, input_arch
);
2469 if (del
&& bfd_get_arch (this_element
) == bfd_arch_unknown
)
2470 /* Try again as an unknown object file. */
2472 else if (!bfd_close (output_bfd
))
2474 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2475 /* Error in new object file. Don't change archive. */
2482 del
= !copy_unknown_object (this_element
, output_bfd
);
2483 if (!bfd_close_all_done (output_bfd
))
2485 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
2486 /* Error in new object file. Don't change archive. */
2493 unlink (output_name
);
2498 if (preserve_dates
&& stat_status
== 0)
2499 set_times (output_name
, &buf
);
2501 /* Open the newly output file and attach to our list. */
2502 output_bfd
= bfd_openr (output_name
, output_target
);
2504 l
->obfd
= output_bfd
;
2507 ptr
= &output_bfd
->archive_next
;
2509 last_element
= this_element
;
2511 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
2513 bfd_close (last_element
);
2518 filename
= bfd_get_filename (obfd
);
2519 if (!bfd_close (obfd
))
2522 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2525 filename
= bfd_get_filename (ibfd
);
2526 if (!bfd_close (ibfd
))
2529 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
2533 /* Delete all the files that we opened. */
2534 for (l
= list
; l
!= NULL
; l
= l
->next
)
2536 if (l
->obfd
== NULL
)
2540 bfd_close (l
->obfd
);
2549 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
2551 /* This is only relevant to Coff targets. */
2552 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
2555 && bfd_get_flavour (input_bfd
) == bfd_target_coff_flavour
)
2556 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
2557 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
2561 /* The top-level control. */
2564 copy_file (const char *input_filename
, const char *output_filename
,
2565 const char *input_target
, const char *output_target
,
2566 const bfd_arch_info_type
*input_arch
)
2569 char **obj_matching
;
2570 char **core_matching
;
2571 off_t size
= get_file_size (input_filename
);
2576 non_fatal (_("error: the input file '%s' is empty"),
2582 /* To allow us to do "strip *" without dying on the first
2583 non-object file, failures are nonfatal. */
2584 ibfd
= bfd_openr (input_filename
, input_target
);
2587 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2592 switch (do_debug_sections
)
2596 case compress_gnu_zlib
:
2597 case compress_gabi_zlib
:
2598 ibfd
->flags
|= BFD_COMPRESS
;
2599 if (do_debug_sections
!= compress
)
2601 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
2602 fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported for `%s'"),
2603 bfd_get_target (ibfd
));
2604 if (do_debug_sections
== compress_gabi_zlib
)
2605 ibfd
->flags
|= BFD_COMPRESS_GABI
;
2609 ibfd
->flags
|= BFD_DECOMPRESS
;
2615 if (bfd_check_format (ibfd
, bfd_archive
))
2617 bfd_boolean force_output_target
;
2620 /* bfd_get_target does not return the correct value until
2621 bfd_check_format succeeds. */
2622 if (output_target
== NULL
)
2624 output_target
= bfd_get_target (ibfd
);
2625 force_output_target
= FALSE
;
2628 force_output_target
= TRUE
;
2630 obfd
= bfd_openw (output_filename
, output_target
);
2633 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2637 /* This is a no-op on non-Coff targets. */
2638 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2640 copy_archive (ibfd
, obfd
, output_target
, force_output_target
, input_arch
);
2642 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
2647 /* bfd_get_target does not return the correct value until
2648 bfd_check_format succeeds. */
2649 if (output_target
== NULL
)
2650 output_target
= bfd_get_target (ibfd
);
2652 obfd
= bfd_openw (output_filename
, output_target
);
2655 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2659 /* This is a no-op on non-Coff targets. */
2660 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2662 if (! copy_object (ibfd
, obfd
, input_arch
))
2665 /* PR 17512: file: 0f15796a.
2666 If the file could not be copied it may not be in a writeable
2667 state. So use bfd_close_all_done to avoid the possibility of
2668 writing uninitialised data into the file. */
2669 if (! (status
? bfd_close_all_done (obfd
) : bfd_close (obfd
)))
2672 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
2676 if (!bfd_close (ibfd
))
2679 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2685 bfd_error_type obj_error
= bfd_get_error ();
2686 bfd_error_type core_error
;
2688 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
2690 /* This probably can't happen.. */
2691 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2692 free (obj_matching
);
2696 core_error
= bfd_get_error ();
2697 /* Report the object error in preference to the core error. */
2698 if (obj_error
!= core_error
)
2699 bfd_set_error (obj_error
);
2701 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
2703 if (obj_error
== bfd_error_file_ambiguously_recognized
)
2705 list_matching_formats (obj_matching
);
2706 free (obj_matching
);
2708 if (core_error
== bfd_error_file_ambiguously_recognized
)
2710 list_matching_formats (core_matching
);
2711 free (core_matching
);
2718 /* Add a name to the section renaming list. */
2721 add_section_rename (const char * old_name
, const char * new_name
,
2724 section_rename
* srename
;
2726 /* Check for conflicts first. */
2727 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
2728 if (strcmp (srename
->old_name
, old_name
) == 0)
2730 /* Silently ignore duplicate definitions. */
2731 if (strcmp (srename
->new_name
, new_name
) == 0
2732 && srename
->flags
== flags
)
2735 fatal (_("Multiple renames of section %s"), old_name
);
2738 srename
= (section_rename
*) xmalloc (sizeof (* srename
));
2740 srename
->old_name
= old_name
;
2741 srename
->new_name
= new_name
;
2742 srename
->flags
= flags
;
2743 srename
->next
= section_rename_list
;
2745 section_rename_list
= srename
;
2748 /* Check the section rename list for a new name of the input section
2749 ISECTION. Return the new name if one is found.
2750 Also set RETURNED_FLAGS to the flags to be used for this section. */
2753 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2754 flagword
* returned_flags
)
2756 const char * old_name
= bfd_section_name (ibfd
, isection
);
2757 section_rename
* srename
;
2759 /* Default to using the flags of the input section. */
2760 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2762 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
2763 if (strcmp (srename
->old_name
, old_name
) == 0)
2765 if (srename
->flags
!= (flagword
) -1)
2766 * returned_flags
= srename
->flags
;
2768 return srename
->new_name
;
2774 /* Once each of the sections is copied, we may still need to do some
2775 finalization work for private section headers. Do that here. */
2778 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2780 /* Allow the BFD backend to copy any private data it understands
2781 from the input section to the output section. */
2782 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2785 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
2786 _("error in private header data"));
2790 /* All went well. */
2794 /* Create a section in OBFD with the same
2795 name and attributes as ISECTION in IBFD. */
2798 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2800 bfd
*obfd
= (bfd
*) obfdarg
;
2801 struct section_list
*p
;
2809 char *prefix
= NULL
;
2810 bfd_boolean make_nobits
;
2812 if (is_strip_section (ibfd
, isection
))
2815 /* Get the, possibly new, name of the output section. */
2816 name
= find_section_rename (ibfd
, isection
, & flags
);
2818 /* Prefix sections. */
2819 if ((prefix_alloc_sections_string
)
2820 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2821 prefix
= prefix_alloc_sections_string
;
2822 else if (prefix_sections_string
)
2823 prefix
= prefix_sections_string
;
2829 n
= (char *) xmalloc (strlen (prefix
) + strlen (name
) + 1);
2835 make_nobits
= FALSE
;
2837 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
,
2838 SECTION_CONTEXT_SET_FLAGS
);
2840 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2841 else if (strip_symbols
== STRIP_NONDEBUG
2842 && (flags
& (SEC_ALLOC
| SEC_GROUP
)) != 0
2843 && !is_nondebug_keep_contents_section (ibfd
, isection
))
2845 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
2846 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2850 /* Twiddle the input section flags so that it seems to
2851 elf.c:copy_private_bfd_data that section flags have not
2852 changed between input and output sections. This hack
2853 prevents wholesale rewriting of the program headers. */
2854 isection
->flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
2858 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2860 if (osection
== NULL
)
2862 err
= _("failed to create output section");
2867 elf_section_type (osection
) = SHT_NOBITS
;
2869 size
= bfd_section_size (ibfd
, isection
);
2871 size
= (size
+ interleave
- 1) / interleave
* copy_width
;
2872 else if (extract_symbol
)
2874 if (! bfd_set_section_size (obfd
, osection
, size
))
2876 err
= _("failed to set size");
2880 vma
= bfd_section_vma (ibfd
, isection
);
2881 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
,
2882 SECTION_CONTEXT_ALTER_VMA
| SECTION_CONTEXT_SET_VMA
);
2885 if (p
->context
& SECTION_CONTEXT_SET_VMA
)
2891 vma
+= change_section_address
;
2893 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2895 err
= _("failed to set vma");
2899 lma
= isection
->lma
;
2900 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
,
2901 SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_SET_LMA
);
2904 if (p
->context
& SECTION_CONTEXT_ALTER_LMA
)
2910 lma
+= change_section_address
;
2912 osection
->lma
= lma
;
2914 /* FIXME: This is probably not enough. If we change the LMA we
2915 may have to recompute the header for the file as well. */
2916 if (!bfd_set_section_alignment (obfd
,
2918 bfd_section_alignment (ibfd
, isection
)))
2920 err
= _("failed to set alignment");
2924 /* Copy merge entity size. */
2925 osection
->entsize
= isection
->entsize
;
2927 /* This used to be mangle_section; we do here to avoid using
2928 bfd_get_section_by_name since some formats allow multiple
2929 sections with the same name. */
2930 isection
->output_section
= osection
;
2931 isection
->output_offset
= 0;
2933 /* Do not copy backend data if --extract-symbol is passed; anything
2934 that needs to look at the section contents will fail. */
2938 if ((isection
->flags
& SEC_GROUP
) != 0)
2940 asymbol
*gsym
= group_signature (isection
);
2944 gsym
->flags
|= BSF_KEEP
;
2945 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
2946 elf_group_id (isection
) = gsym
;
2950 /* Allow the BFD backend to copy any private data it understands
2951 from the input section to the output section. */
2952 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2954 err
= _("failed to copy private data");
2958 /* All went well. */
2963 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
2966 /* Return TRUE if input section ISECTION should be skipped. */
2969 skip_section (bfd
*ibfd
, sec_ptr isection
)
2975 /* If we have already failed earlier on,
2976 do not keep on generating complaints now. */
2983 if (is_strip_section (ibfd
, isection
))
2986 if (is_update_section (ibfd
, isection
))
2989 flags
= bfd_get_section_flags (ibfd
, isection
);
2990 if ((flags
& SEC_GROUP
) != 0)
2993 osection
= isection
->output_section
;
2994 size
= bfd_get_section_size (isection
);
2996 if (size
== 0 || osection
== 0)
3002 /* Copy relocations in input section ISECTION of IBFD to an output
3003 section with the same name in OBFDARG. If stripping then don't
3004 copy any relocation info. */
3007 copy_relocations_in_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
3009 bfd
*obfd
= (bfd
*) obfdarg
;
3015 if (skip_section (ibfd
, isection
))
3018 osection
= isection
->output_section
;
3020 /* Core files and DWO files do not need to be relocated. */
3021 if (bfd_get_format (obfd
) == bfd_core
|| strip_symbols
== STRIP_NONDWO
)
3025 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
3029 /* Do not complain if the target does not support relocations. */
3030 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
3035 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
3043 bfd_set_reloc (obfd
, osection
, NULL
, 0);
3044 osection
->flags
&= ~SEC_RELOC
;
3048 relpp
= (arelent
**) xmalloc (relsize
);
3049 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
3053 bfd_nonfatal_message (NULL
, ibfd
, isection
,
3054 _("relocation count is negative"));
3058 if (strip_symbols
== STRIP_ALL
)
3060 /* Remove relocations which are not in
3061 keep_strip_specific_list. */
3062 arelent
**temp_relpp
;
3063 long temp_relcount
= 0;
3066 temp_relpp
= (arelent
**) xmalloc (relsize
);
3067 for (i
= 0; i
< relcount
; i
++)
3069 /* PR 17512: file: 9e907e0c. */
3070 if (relpp
[i
]->sym_ptr_ptr
)
3071 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
3072 keep_specific_htab
))
3073 temp_relpp
[temp_relcount
++] = relpp
[i
];
3075 relcount
= temp_relcount
;
3080 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
3083 osection
->flags
&= ~SEC_RELOC
;
3089 /* Copy the data of input section ISECTION of IBFD
3090 to an output section with the same name in OBFD. */
3093 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
3095 bfd
*obfd
= (bfd
*) obfdarg
;
3096 struct section_list
*p
;
3100 if (skip_section (ibfd
, isection
))
3103 osection
= isection
->output_section
;
3104 size
= bfd_get_section_size (isection
);
3106 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
3107 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
3109 bfd_byte
*memhunk
= NULL
;
3111 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
))
3114 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
3120 /* We don't handle leftover bytes (too many possible behaviors,
3121 and we don't know what the user wants). The section length
3122 must be a multiple of the number of bytes to swap. */
3123 if ((size
% reverse_bytes
) == 0)
3128 for (i
= 0; i
< size
; i
+= reverse_bytes
)
3129 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
3131 bfd_byte
*m
= (bfd_byte
*) memhunk
;
3134 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
3135 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
3139 /* User must pad the section up in order to do this. */
3140 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
3141 bfd_section_name (ibfd
, isection
), reverse_bytes
);
3146 /* Keep only every `copy_byte'th byte in MEMHUNK. */
3147 char *from
= (char *) memhunk
+ copy_byte
;
3148 char *to
= (char *) memhunk
;
3149 char *end
= (char *) memhunk
+ size
;
3152 for (; from
< end
; from
+= interleave
)
3153 for (i
= 0; i
< copy_width
; i
++)
3155 if (&from
[i
] >= end
)
3160 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
* copy_width
;
3161 osection
->lma
/= interleave
;
3164 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
3167 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
3172 else if ((p
= find_section_list (bfd_get_section_name (ibfd
, isection
),
3173 FALSE
, SECTION_CONTEXT_SET_FLAGS
)) != NULL
3174 && (p
->flags
& SEC_HAS_CONTENTS
) != 0)
3176 void *memhunk
= xmalloc (size
);
3178 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
3179 flag--they can just remove the section entirely and add it
3180 back again. However, we do permit them to turn on the
3181 SEC_HAS_CONTENTS flag, and take it to mean that the section
3182 contents should be zeroed out. */
3184 memset (memhunk
, 0, size
);
3185 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
3188 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
3195 /* Get all the sections. This is used when --gap-fill or --pad-to is
3199 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
3201 asection
***secppp
= (asection
***) secppparg
;
3203 **secppp
= osection
;
3207 /* Sort sections by VMA. This is called via qsort, and is used when
3208 --gap-fill or --pad-to is used. We force non loadable or empty
3209 sections to the front, where they are easier to ignore. */
3212 compare_section_lma (const void *arg1
, const void *arg2
)
3214 const asection
*const *sec1
= (const asection
* const *) arg1
;
3215 const asection
*const *sec2
= (const asection
* const *) arg2
;
3216 flagword flags1
, flags2
;
3218 /* Sort non loadable sections to the front. */
3219 flags1
= (*sec1
)->flags
;
3220 flags2
= (*sec2
)->flags
;
3221 if ((flags1
& SEC_HAS_CONTENTS
) == 0
3222 || (flags1
& SEC_LOAD
) == 0)
3224 if ((flags2
& SEC_HAS_CONTENTS
) != 0
3225 && (flags2
& SEC_LOAD
) != 0)
3230 if ((flags2
& SEC_HAS_CONTENTS
) == 0
3231 || (flags2
& SEC_LOAD
) == 0)
3235 /* Sort sections by LMA. */
3236 if ((*sec1
)->lma
> (*sec2
)->lma
)
3238 else if ((*sec1
)->lma
< (*sec2
)->lma
)
3241 /* Sort sections with the same LMA by size. */
3242 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
3244 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
3250 /* Mark all the symbols which will be used in output relocations with
3251 the BSF_KEEP flag so that those symbols will not be stripped.
3253 Ignore relocations which will not appear in the output file. */
3256 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
3258 asymbol
**symbols
= (asymbol
**) symbolsarg
;
3263 /* Ignore an input section with no corresponding output section. */
3264 if (isection
->output_section
== NULL
)
3267 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
3270 /* Do not complain if the target does not support relocations. */
3271 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
3273 bfd_fatal (bfd_get_filename (ibfd
));
3279 relpp
= (arelent
**) xmalloc (relsize
);
3280 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
3282 bfd_fatal (bfd_get_filename (ibfd
));
3284 /* Examine each symbol used in a relocation. If it's not one of the
3285 special bfd section symbols, then mark it with BSF_KEEP. */
3286 for (i
= 0; i
< relcount
; i
++)
3288 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
3289 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
3290 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
3291 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
3298 /* Write out debugging information. */
3301 write_debugging_info (bfd
*obfd
, void *dhandle
,
3302 long *symcountp ATTRIBUTE_UNUSED
,
3303 asymbol
***symppp ATTRIBUTE_UNUSED
)
3305 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
3306 return write_ieee_debugging_info (obfd
, dhandle
);
3308 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
3309 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
3311 bfd_byte
*syms
, *strings
;
3312 bfd_size_type symsize
, stringsize
;
3313 asection
*stabsec
, *stabstrsec
;
3316 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
3321 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
3322 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
3323 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
3325 || stabstrsec
== NULL
3326 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
3327 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
3328 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
3329 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
3331 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3332 _("can't create debugging section"));
3336 /* We can get away with setting the section contents now because
3337 the next thing the caller is going to do is copy over the
3338 real sections. We may someday have to split the contents
3339 setting out of this function. */
3340 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
3341 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
3344 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3345 _("can't set debugging section contents"));
3352 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3353 _("don't know how to write debugging information for %s"),
3354 bfd_get_target (obfd
));
3358 /* If neither -D nor -U was specified explicitly,
3359 then use the configured default. */
3361 default_deterministic (void)
3363 if (deterministic
< 0)
3364 deterministic
= DEFAULT_AR_DETERMINISTIC
;
3368 strip_main (int argc
, char *argv
[])
3370 char *input_target
= NULL
;
3371 char *output_target
= NULL
;
3372 bfd_boolean show_version
= FALSE
;
3373 bfd_boolean formats_info
= FALSE
;
3376 char *output_file
= NULL
;
3378 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvwDU",
3379 strip_options
, (int *) 0)) != EOF
)
3384 input_target
= optarg
;
3387 output_target
= optarg
;
3390 input_target
= output_target
= optarg
;
3393 find_section_list (optarg
, TRUE
, SECTION_CONTEXT_REMOVE
);
3394 sections_removed
= TRUE
;
3397 strip_symbols
= STRIP_ALL
;
3401 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
3402 strip_symbols
= STRIP_DEBUG
;
3404 case OPTION_STRIP_DWO
:
3405 strip_symbols
= STRIP_DWO
;
3407 case OPTION_STRIP_UNNEEDED
:
3408 strip_symbols
= STRIP_UNNEEDED
;
3411 add_specific_symbol (optarg
, keep_specific_htab
);
3414 add_specific_symbol (optarg
, strip_specific_htab
);
3417 output_file
= optarg
;
3420 preserve_dates
= TRUE
;
3423 deterministic
= TRUE
;
3426 deterministic
= FALSE
;
3429 discard_locals
= LOCALS_ALL
;
3432 discard_locals
= LOCALS_START_L
;
3438 show_version
= TRUE
;
3440 case OPTION_FORMATS_INFO
:
3441 formats_info
= TRUE
;
3443 case OPTION_ONLY_KEEP_DEBUG
:
3444 strip_symbols
= STRIP_NONDEBUG
;
3446 case OPTION_KEEP_FILE_SYMBOLS
:
3447 keep_file_symbols
= 1;
3450 /* We've been given a long option. */
3457 strip_usage (stdout
, 0);
3459 strip_usage (stderr
, 1);
3470 print_version ("strip");
3472 default_deterministic ();
3474 /* Default is to strip all symbols. */
3475 if (strip_symbols
== STRIP_UNDEF
3476 && discard_locals
== LOCALS_UNDEF
3477 && htab_elements (strip_specific_htab
) == 0)
3478 strip_symbols
= STRIP_ALL
;
3480 if (output_target
== NULL
)
3481 output_target
= input_target
;
3485 || (output_file
!= NULL
&& (i
+ 1) < argc
))
3486 strip_usage (stderr
, 1);
3488 for (; i
< argc
; i
++)
3490 int hold_status
= status
;
3491 struct stat statbuf
;
3494 if (get_file_size (argv
[i
]) < 1)
3501 /* No need to check the return value of stat().
3502 It has already been checked in get_file_size(). */
3503 stat (argv
[i
], &statbuf
);
3505 if (output_file
== NULL
3506 || filename_cmp (argv
[i
], output_file
) == 0)
3507 tmpname
= make_tempname (argv
[i
]);
3509 tmpname
= output_file
;
3511 if (tmpname
== NULL
)
3513 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
3514 _("could not create temporary file to hold stripped copy"));
3520 copy_file (argv
[i
], tmpname
, input_target
, output_target
, NULL
);
3524 set_times (tmpname
, &statbuf
);
3525 if (output_file
!= tmpname
)
3526 status
= (smart_rename (tmpname
,
3527 output_file
? output_file
: argv
[i
],
3528 preserve_dates
) != 0);
3530 status
= hold_status
;
3533 unlink_if_ordinary (tmpname
);
3534 if (output_file
!= tmpname
)
3541 /* Set up PE subsystem. */
3544 set_pe_subsystem (const char *s
)
3546 const char *version
, *subsystem
;
3556 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
3557 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
3558 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
3559 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
3560 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
3561 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
3562 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
3563 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
3564 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
},
3565 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
3571 /* Check for the presence of a version number. */
3572 version
= strchr (s
, ':');
3573 if (version
== NULL
)
3577 int len
= version
- s
;
3581 version
= copy
+ 1 + len
;
3582 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
3584 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
3586 non_fatal (_("%s: bad version in PE subsystem"), s
);
3589 /* Check for numeric subsystem. */
3590 value
= (short) strtol (subsystem
, ©
, 0);
3593 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3594 if (v
[i
].value
== value
)
3596 pe_subsystem
= value
;
3597 set_def
= v
[i
].set_def
;
3603 /* Search for subsystem by name. */
3604 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
3605 if (strcmp (subsystem
, v
[i
].name
) == 0)
3607 pe_subsystem
= v
[i
].value
;
3608 set_def
= v
[i
].set_def
;
3616 fatal (_("unknown PE subsystem: %s"), s
);
3621 if (pe_file_alignment
== (bfd_vma
) -1)
3622 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
3623 if (pe_section_alignment
== (bfd_vma
) -1)
3624 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
3628 free ((char *) subsystem
);
3631 /* Convert EFI target to PEI target. */
3634 convert_efi_target (char *efi
)
3640 if (strcmp (efi
+ 4, "ia32") == 0)
3642 /* Change ia32 to i386. */
3647 else if (strcmp (efi
+ 4, "x86_64") == 0)
3649 /* Change x86_64 to x86-64. */
3654 /* Allocate and return a pointer to a struct section_add, initializing the
3655 structure using ARG, a string in the format "sectionname=filename".
3656 The returned structure will have its next pointer set to NEXT. The
3657 OPTION field is the name of the command line option currently being
3658 parsed, and is only used if an error needs to be reported. */
3660 static struct section_add
*
3661 init_section_add (const char *arg
,
3662 struct section_add
*next
,
3665 struct section_add
*pa
;
3668 s
= strchr (arg
, '=');
3670 fatal (_("bad format for %s"), option
);
3672 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
3673 pa
->name
= xstrndup (arg
, s
- arg
);
3674 pa
->filename
= s
+ 1;
3676 pa
->contents
= NULL
;
3682 /* Load the file specified in PA, allocating memory to hold the file
3683 contents, and store a pointer to the allocated memory in the contents
3684 field of PA. The size field of PA is also updated. All errors call
3688 section_add_load_file (struct section_add
*pa
)
3693 /* We don't use get_file_size so that we can do
3694 --add-section .note.GNU_stack=/dev/null
3695 get_file_size doesn't work on /dev/null. */
3697 f
= fopen (pa
->filename
, FOPEN_RB
);
3699 fatal (_("cannot open: %s: %s"),
3700 pa
->filename
, strerror (errno
));
3704 pa
->contents
= (bfd_byte
*) xmalloc (alloc
);
3712 pa
->contents
= (bfd_byte
*) xrealloc (pa
->contents
, alloc
);
3715 got
= fread (pa
->contents
+ off
, 1, alloc
- off
, f
);
3717 fatal (_("%s: fread failed"), pa
->filename
);
3728 copy_main (int argc
, char *argv
[])
3730 char *input_filename
= NULL
;
3731 char *output_filename
= NULL
;
3733 char *input_target
= NULL
;
3734 char *output_target
= NULL
;
3735 bfd_boolean show_version
= FALSE
;
3736 bfd_boolean change_warn
= TRUE
;
3737 bfd_boolean formats_info
= FALSE
;
3739 struct stat statbuf
;
3740 const bfd_arch_info_type
*input_arch
= NULL
;
3742 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
3743 copy_options
, (int *) 0)) != EOF
)
3748 copy_byte
= atoi (optarg
);
3750 fatal (_("byte number must be non-negative"));
3754 input_arch
= bfd_scan_arch (optarg
);
3755 if (input_arch
== NULL
)
3756 fatal (_("architecture %s unknown"), optarg
);
3762 interleave
= atoi (optarg
);
3764 fatal (_("interleave must be positive"));
3770 case OPTION_INTERLEAVE_WIDTH
:
3771 copy_width
= atoi (optarg
);
3773 fatal(_("interleave width must be positive"));
3777 case 's': /* "source" - 'I' is preferred */
3778 input_target
= optarg
;
3782 case 'd': /* "destination" - 'O' is preferred */
3783 output_target
= optarg
;
3787 input_target
= output_target
= optarg
;
3791 find_section_list (optarg
, TRUE
, SECTION_CONTEXT_COPY
);
3792 sections_copied
= TRUE
;
3796 find_section_list (optarg
, TRUE
, SECTION_CONTEXT_REMOVE
);
3797 sections_removed
= TRUE
;
3801 strip_symbols
= STRIP_ALL
;
3805 strip_symbols
= STRIP_DEBUG
;
3808 case OPTION_STRIP_DWO
:
3809 strip_symbols
= STRIP_DWO
;
3812 case OPTION_STRIP_UNNEEDED
:
3813 strip_symbols
= STRIP_UNNEEDED
;
3816 case OPTION_ONLY_KEEP_DEBUG
:
3817 strip_symbols
= STRIP_NONDEBUG
;
3820 case OPTION_KEEP_FILE_SYMBOLS
:
3821 keep_file_symbols
= 1;
3824 case OPTION_ADD_GNU_DEBUGLINK
:
3825 long_section_names
= ENABLE
;
3826 gnu_debuglink_filename
= optarg
;
3830 add_specific_symbol (optarg
, keep_specific_htab
);
3834 add_specific_symbol (optarg
, strip_specific_htab
);
3837 case OPTION_STRIP_UNNEEDED_SYMBOL
:
3838 add_specific_symbol (optarg
, strip_unneeded_htab
);
3842 add_specific_symbol (optarg
, localize_specific_htab
);
3845 case OPTION_GLOBALIZE_SYMBOL
:
3846 add_specific_symbol (optarg
, globalize_specific_htab
);
3850 add_specific_symbol (optarg
, keepglobal_specific_htab
);
3854 add_specific_symbol (optarg
, weaken_specific_htab
);
3858 preserve_dates
= TRUE
;
3862 deterministic
= TRUE
;
3866 deterministic
= FALSE
;
3874 discard_locals
= LOCALS_ALL
;
3878 discard_locals
= LOCALS_START_L
;
3886 show_version
= TRUE
;
3889 case OPTION_FORMATS_INFO
:
3890 formats_info
= TRUE
;
3897 case OPTION_ADD_SECTION
:
3898 add_sections
= init_section_add (optarg
, add_sections
,
3900 section_add_load_file (add_sections
);
3903 case OPTION_UPDATE_SECTION
:
3904 update_sections
= init_section_add (optarg
, update_sections
,
3905 "--update-section");
3906 section_add_load_file (update_sections
);
3909 case OPTION_DUMP_SECTION
:
3910 dump_sections
= init_section_add (optarg
, dump_sections
,
3914 case OPTION_CHANGE_START
:
3915 change_start
= parse_vma (optarg
, "--change-start");
3918 case OPTION_CHANGE_SECTION_ADDRESS
:
3919 case OPTION_CHANGE_SECTION_LMA
:
3920 case OPTION_CHANGE_SECTION_VMA
:
3922 struct section_list
* p
;
3923 unsigned int context
= 0;
3927 char *option
= NULL
;
3932 case OPTION_CHANGE_SECTION_ADDRESS
:
3933 option
= "--change-section-address";
3934 context
= SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_ALTER_VMA
;
3936 case OPTION_CHANGE_SECTION_LMA
:
3937 option
= "--change-section-lma";
3938 context
= SECTION_CONTEXT_ALTER_LMA
;
3940 case OPTION_CHANGE_SECTION_VMA
:
3941 option
= "--change-section-vma";
3942 context
= SECTION_CONTEXT_ALTER_VMA
;
3946 s
= strchr (optarg
, '=');
3949 s
= strchr (optarg
, '+');
3952 s
= strchr (optarg
, '-');
3954 fatal (_("bad format for %s"), option
);
3959 /* Correct the context. */
3962 case OPTION_CHANGE_SECTION_ADDRESS
:
3963 context
= SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_SET_VMA
;
3965 case OPTION_CHANGE_SECTION_LMA
:
3966 context
= SECTION_CONTEXT_SET_LMA
;
3968 case OPTION_CHANGE_SECTION_VMA
:
3969 context
= SECTION_CONTEXT_SET_VMA
;
3975 name
= (char *) xmalloc (len
+ 1);
3976 strncpy (name
, optarg
, len
);
3979 p
= find_section_list (name
, TRUE
, context
);
3981 val
= parse_vma (s
+ 1, option
);
3987 case OPTION_CHANGE_SECTION_ADDRESS
:
3991 case OPTION_CHANGE_SECTION_LMA
:
3995 case OPTION_CHANGE_SECTION_VMA
:
4002 case OPTION_CHANGE_ADDRESSES
:
4003 change_section_address
= parse_vma (optarg
, "--change-addresses");
4004 change_start
= change_section_address
;
4007 case OPTION_CHANGE_WARNINGS
:
4011 case OPTION_CHANGE_LEADING_CHAR
:
4012 change_leading_char
= TRUE
;
4015 case OPTION_COMPRESS_DEBUG_SECTIONS
:
4018 if (strcasecmp (optarg
, "none") == 0)
4019 do_debug_sections
= decompress
;
4020 else if (strcasecmp (optarg
, "zlib") == 0)
4021 do_debug_sections
= compress_zlib
;
4022 else if (strcasecmp (optarg
, "zlib-gnu") == 0)
4023 do_debug_sections
= compress_gnu_zlib
;
4024 else if (strcasecmp (optarg
, "zlib-gabi") == 0)
4025 do_debug_sections
= compress_gabi_zlib
;
4027 fatal (_("unrecognized --compress-debug-sections type `%s'"),
4031 do_debug_sections
= compress
;
4034 case OPTION_DEBUGGING
:
4035 convert_debugging
= TRUE
;
4038 case OPTION_DECOMPRESS_DEBUG_SECTIONS
:
4039 do_debug_sections
= decompress
;
4042 case OPTION_GAP_FILL
:
4044 bfd_vma gap_fill_vma
;
4046 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
4047 gap_fill
= (bfd_byte
) gap_fill_vma
;
4048 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
4052 sprintf_vma (buff
, gap_fill_vma
);
4054 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
4057 gap_fill_set
= TRUE
;
4061 case OPTION_NO_CHANGE_WARNINGS
:
4062 change_warn
= FALSE
;
4066 pad_to
= parse_vma (optarg
, "--pad-to");
4070 case OPTION_REMOVE_LEADING_CHAR
:
4071 remove_leading_char
= TRUE
;
4074 case OPTION_REDEFINE_SYM
:
4076 /* Push this redefinition onto redefine_symbol_list. */
4080 const char *nextarg
;
4081 char *source
, *target
;
4083 s
= strchr (optarg
, '=');
4085 fatal (_("bad format for %s"), "--redefine-sym");
4088 source
= (char *) xmalloc (len
+ 1);
4089 strncpy (source
, optarg
, len
);
4093 len
= strlen (nextarg
);
4094 target
= (char *) xmalloc (len
+ 1);
4095 strcpy (target
, nextarg
);
4097 redefine_list_append ("--redefine-sym", source
, target
);
4104 case OPTION_REDEFINE_SYMS
:
4105 add_redefine_syms_file (optarg
);
4108 case OPTION_SET_SECTION_FLAGS
:
4110 struct section_list
*p
;
4115 s
= strchr (optarg
, '=');
4117 fatal (_("bad format for %s"), "--set-section-flags");
4120 name
= (char *) xmalloc (len
+ 1);
4121 strncpy (name
, optarg
, len
);
4124 p
= find_section_list (name
, TRUE
, SECTION_CONTEXT_SET_FLAGS
);
4126 p
->flags
= parse_flags (s
+ 1);
4130 case OPTION_RENAME_SECTION
:
4133 const char *eq
, *fl
;
4138 eq
= strchr (optarg
, '=');
4140 fatal (_("bad format for %s"), "--rename-section");
4144 fatal (_("bad format for %s"), "--rename-section");
4146 old_name
= (char *) xmalloc (len
+ 1);
4147 strncpy (old_name
, optarg
, len
);
4151 fl
= strchr (eq
, ',');
4154 flags
= parse_flags (fl
+ 1);
4164 fatal (_("bad format for %s"), "--rename-section");
4166 new_name
= (char *) xmalloc (len
+ 1);
4167 strncpy (new_name
, eq
, len
);
4170 add_section_rename (old_name
, new_name
, flags
);
4174 case OPTION_SET_START
:
4175 set_start
= parse_vma (optarg
, "--set-start");
4176 set_start_set
= TRUE
;
4179 case OPTION_SREC_LEN
:
4180 Chunk
= parse_vma (optarg
, "--srec-len");
4183 case OPTION_SREC_FORCES3
:
4187 case OPTION_STRIP_SYMBOLS
:
4188 add_specific_symbols (optarg
, strip_specific_htab
);
4191 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
4192 add_specific_symbols (optarg
, strip_unneeded_htab
);
4195 case OPTION_KEEP_SYMBOLS
:
4196 add_specific_symbols (optarg
, keep_specific_htab
);
4199 case OPTION_LOCALIZE_HIDDEN
:
4200 localize_hidden
= TRUE
;
4203 case OPTION_LOCALIZE_SYMBOLS
:
4204 add_specific_symbols (optarg
, localize_specific_htab
);
4207 case OPTION_LONG_SECTION_NAMES
:
4208 if (!strcmp ("enable", optarg
))
4209 long_section_names
= ENABLE
;
4210 else if (!strcmp ("disable", optarg
))
4211 long_section_names
= DISABLE
;
4212 else if (!strcmp ("keep", optarg
))
4213 long_section_names
= KEEP
;
4215 fatal (_("unknown long section names option '%s'"), optarg
);
4218 case OPTION_GLOBALIZE_SYMBOLS
:
4219 add_specific_symbols (optarg
, globalize_specific_htab
);
4222 case OPTION_KEEPGLOBAL_SYMBOLS
:
4223 add_specific_symbols (optarg
, keepglobal_specific_htab
);
4226 case OPTION_WEAKEN_SYMBOLS
:
4227 add_specific_symbols (optarg
, weaken_specific_htab
);
4230 case OPTION_ALT_MACH_CODE
:
4231 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
4232 if (use_alt_mach_code
== 0)
4233 fatal (_("unable to parse alternative machine code"));
4236 case OPTION_PREFIX_SYMBOLS
:
4237 prefix_symbols_string
= optarg
;
4240 case OPTION_PREFIX_SECTIONS
:
4241 prefix_sections_string
= optarg
;
4244 case OPTION_PREFIX_ALLOC_SECTIONS
:
4245 prefix_alloc_sections_string
= optarg
;
4248 case OPTION_READONLY_TEXT
:
4249 bfd_flags_to_set
|= WP_TEXT
;
4250 bfd_flags_to_clear
&= ~WP_TEXT
;
4253 case OPTION_WRITABLE_TEXT
:
4254 bfd_flags_to_clear
|= WP_TEXT
;
4255 bfd_flags_to_set
&= ~WP_TEXT
;
4259 bfd_flags_to_set
|= D_PAGED
;
4260 bfd_flags_to_clear
&= ~D_PAGED
;
4264 bfd_flags_to_clear
|= D_PAGED
;
4265 bfd_flags_to_set
&= ~D_PAGED
;
4268 case OPTION_EXTRACT_DWO
:
4269 strip_symbols
= STRIP_NONDWO
;
4272 case OPTION_EXTRACT_SYMBOL
:
4273 extract_symbol
= TRUE
;
4276 case OPTION_REVERSE_BYTES
:
4278 int prev
= reverse_bytes
;
4280 reverse_bytes
= atoi (optarg
);
4281 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
4282 fatal (_("number of bytes to reverse must be positive and even"));
4284 if (prev
&& prev
!= reverse_bytes
)
4285 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
4290 case OPTION_FILE_ALIGNMENT
:
4291 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
4297 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
4299 || (*end
!= '.' && *end
!= '\0'))
4300 non_fatal (_("%s: invalid reserve value for --heap"),
4302 else if (*end
!= '\0')
4304 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
4306 non_fatal (_("%s: invalid commit value for --heap"),
4312 case OPTION_IMAGE_BASE
:
4313 pe_image_base
= parse_vma (optarg
, "--image-base");
4316 case OPTION_SECTION_ALIGNMENT
:
4317 pe_section_alignment
= parse_vma (optarg
,
4318 "--section-alignment");
4321 case OPTION_SUBSYSTEM
:
4322 set_pe_subsystem (optarg
);
4328 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
4330 || (*end
!= '.' && *end
!= '\0'))
4331 non_fatal (_("%s: invalid reserve value for --stack"),
4333 else if (*end
!= '\0')
4335 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
4337 non_fatal (_("%s: invalid commit value for --stack"),
4344 /* We've been given a long option. */
4349 copy_usage (stdout
, 0);
4352 copy_usage (stderr
, 1);
4363 print_version ("objcopy");
4365 if (interleave
&& copy_byte
== -1)
4366 fatal (_("interleave start byte must be set with --byte"));
4368 if (copy_byte
>= interleave
)
4369 fatal (_("byte number must be less than interleave"));
4371 if (copy_width
> interleave
- copy_byte
)
4372 fatal (_("interleave width must be less than or equal to interleave - byte`"));
4374 if (optind
== argc
|| optind
+ 2 < argc
)
4375 copy_usage (stderr
, 1);
4377 input_filename
= argv
[optind
];
4378 if (optind
+ 1 < argc
)
4379 output_filename
= argv
[optind
+ 1];
4381 default_deterministic ();
4383 /* Default is to strip no symbols. */
4384 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
4385 strip_symbols
= STRIP_NONE
;
4387 if (output_target
== NULL
)
4388 output_target
= input_target
;
4390 /* Convert input EFI target to PEI target. */
4391 if (input_target
!= NULL
4392 && strncmp (input_target
, "efi-", 4) == 0)
4396 efi
= xstrdup (output_target
+ 4);
4397 if (strncmp (efi
, "bsdrv-", 6) == 0
4398 || strncmp (efi
, "rtdrv-", 6) == 0)
4400 else if (strncmp (efi
, "app-", 4) != 0)
4401 fatal (_("unknown input EFI target: %s"), input_target
);
4404 convert_efi_target (efi
);
4407 /* Convert output EFI target to PEI target. */
4408 if (output_target
!= NULL
4409 && strncmp (output_target
, "efi-", 4) == 0)
4413 efi
= xstrdup (output_target
+ 4);
4414 if (strncmp (efi
, "app-", 4) == 0)
4416 if (pe_subsystem
== -1)
4417 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
4419 else if (strncmp (efi
, "bsdrv-", 6) == 0)
4421 if (pe_subsystem
== -1)
4422 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
4425 else if (strncmp (efi
, "rtdrv-", 6) == 0)
4427 if (pe_subsystem
== -1)
4428 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
4432 fatal (_("unknown output EFI target: %s"), output_target
);
4434 if (pe_file_alignment
== (bfd_vma
) -1)
4435 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
4436 if (pe_section_alignment
== (bfd_vma
) -1)
4437 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
4439 output_target
= efi
;
4440 convert_efi_target (efi
);
4444 if (stat (input_filename
, & statbuf
) < 0)
4445 fatal (_("warning: could not locate '%s'. System error message: %s"),
4446 input_filename
, strerror (errno
));
4448 /* If there is no destination file, or the source and destination files
4449 are the same, then create a temp and rename the result into the input. */
4450 if (output_filename
== NULL
4451 || filename_cmp (input_filename
, output_filename
) == 0)
4452 tmpname
= make_tempname (input_filename
);
4454 tmpname
= output_filename
;
4456 if (tmpname
== NULL
)
4457 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
4458 input_filename
, strerror (errno
));
4460 copy_file (input_filename
, tmpname
, input_target
, output_target
, input_arch
);
4464 set_times (tmpname
, &statbuf
);
4465 if (tmpname
!= output_filename
)
4466 status
= (smart_rename (tmpname
, input_filename
,
4467 preserve_dates
) != 0);
4470 unlink_if_ordinary (tmpname
);
4474 struct section_list
*p
;
4476 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
4480 if (p
->context
& (SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
))
4484 sprintf_vma (buff
, p
->vma_val
);
4486 /* xgettext:c-format */
4487 non_fatal (_("%s %s%c0x%s never used"),
4488 "--change-section-vma",
4490 p
->context
& SECTION_CONTEXT_SET_VMA
? '=' : '+',
4494 if (p
->context
& (SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
))
4498 sprintf_vma (buff
, p
->lma_val
);
4500 /* xgettext:c-format */
4501 non_fatal (_("%s %s%c0x%s never used"),
4502 "--change-section-lma",
4504 p
->context
& SECTION_CONTEXT_SET_LMA
? '=' : '+',
4515 main (int argc
, char *argv
[])
4517 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4518 setlocale (LC_MESSAGES
, "");
4520 #if defined (HAVE_SETLOCALE)
4521 setlocale (LC_CTYPE
, "");
4523 bindtextdomain (PACKAGE
, LOCALEDIR
);
4524 textdomain (PACKAGE
);
4526 program_name
= argv
[0];
4527 xmalloc_set_program_name (program_name
);
4529 START_PROGRESS (program_name
, 0);
4531 expandargv (&argc
, &argv
);
4533 strip_symbols
= STRIP_UNDEF
;
4534 discard_locals
= LOCALS_UNDEF
;
4537 set_default_bfd_target ();
4541 int i
= strlen (program_name
);
4542 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4543 /* Drop the .exe suffix, if any. */
4544 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
4547 program_name
[i
] = '\0';
4550 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
4553 create_symbol_htabs ();
4556 bfd_set_error_program_name (argv
[0]);
4559 strip_main (argc
, argv
);
4561 copy_main (argc
, argv
);
4563 END_PROGRESS (program_name
);