1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "libiberty.h"
28 #include "filenames.h"
31 #include "coff/internal.h"
33 #include "safe-ctype.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 node includes symbol name mapping to support redefine_sym. */
66 struct addsym_node
*next
;
74 typedef struct section_rename
76 const char * old_name
;
77 const char * new_name
;
79 struct section_rename
* next
;
83 /* List of sections to be renamed. */
84 static section_rename
*section_rename_list
;
86 static asymbol
**isympp
= NULL
; /* Input symbols. */
87 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
89 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
90 static int copy_byte
= -1;
91 static int interleave
= 0; /* Initialised to 4 in copy_main(). */
92 static int copy_width
= 1;
94 static bfd_boolean verbose
; /* Print file and target names. */
95 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
96 static int deterministic
= -1; /* Enable deterministic archives. */
97 static int status
= 0; /* Exit status. */
99 static bfd_boolean merge_notes
= FALSE
; /* Merge note sections. */
101 typedef struct merged_note_section
103 asection
* sec
; /* The section that is being merged. */
104 bfd_byte
* contents
;/* New contents of the section. */
105 bfd_size_type size
; /* New size of the section. */
106 struct merged_note_section
* next
; /* Link to next merged note section. */
107 } merged_note_section
;
112 STRIP_NONE
, /* Don't strip. */
113 STRIP_DEBUG
, /* Strip all debugger symbols. */
114 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
115 STRIP_NONDEBUG
, /* Strip everything but debug info. */
116 STRIP_DWO
, /* Strip all DWO info. */
117 STRIP_NONDWO
, /* Strip everything but DWO info. */
118 STRIP_ALL
/* Strip all symbols. */
121 /* Which symbols to remove. */
122 static enum strip_action strip_symbols
= STRIP_UNDEF
;
127 LOCALS_START_L
, /* Discard locals starting with L. */
128 LOCALS_ALL
/* Discard all locals. */
131 /* Which local symbols to remove. Overrides STRIP_ALL. */
132 static enum locals_action discard_locals
;
134 /* Structure used to hold lists of sections and actions to take. */
137 struct section_list
* next
; /* Next section to change. */
138 const char * pattern
; /* Section name pattern. */
139 bfd_boolean used
; /* Whether this entry was used. */
141 unsigned int context
; /* What to do with matching sections. */
142 /* Flag bits used in the context field.
143 COPY and REMOVE are mutually exlusive. SET and ALTER are mutually exclusive. */
144 #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
145 #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
146 #define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */
147 #define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */
148 #define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */
149 #define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */
150 #define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */
151 #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 7) /* Remove relocations for this section. */
152 #define SECTION_CONTEXT_SET_ALIGNMENT (1 << 8) /* Set alignment for section. */
154 bfd_vma vma_val
; /* Amount to change by or set to. */
155 bfd_vma lma_val
; /* Amount to change by or set to. */
156 flagword flags
; /* What to set the section flags to. */
157 unsigned int alignment
; /* Alignment of output section. */
160 static struct section_list
*change_sections
;
162 /* TRUE if some sections are to be removed. */
163 static bfd_boolean sections_removed
;
165 /* TRUE if only some sections are to be copied. */
166 static bfd_boolean sections_copied
;
168 /* Changes to the start address. */
169 static bfd_vma change_start
= 0;
170 static bfd_boolean set_start_set
= FALSE
;
171 static bfd_vma set_start
;
173 /* Changes to section addresses. */
174 static bfd_vma change_section_address
= 0;
176 /* Filling gaps between sections. */
177 static bfd_boolean gap_fill_set
= FALSE
;
178 static bfd_byte gap_fill
= 0;
180 /* Pad to a given address. */
181 static bfd_boolean pad_to_set
= FALSE
;
182 static bfd_vma pad_to
;
184 /* Use alternative machine code? */
185 static unsigned long use_alt_mach_code
= 0;
187 /* Output BFD flags user wants to set or clear */
188 static flagword bfd_flags_to_set
;
189 static flagword bfd_flags_to_clear
;
191 /* List of sections to add. */
194 /* Next section to add. */
195 struct section_add
*next
;
196 /* Name of section to add. */
198 /* Name of file holding section contents. */
199 const char *filename
;
202 /* Contents of file. */
204 /* BFD section, after it has been added. */
208 /* List of sections to add to the output BFD. */
209 static struct section_add
*add_sections
;
211 /* List of sections to update in the output BFD. */
212 static struct section_add
*update_sections
;
214 /* List of sections to dump from the output BFD. */
215 static struct section_add
*dump_sections
;
217 /* If non-NULL the argument to --add-gnu-debuglink.
218 This should be the filename to store in the .gnu_debuglink section. */
219 static const char * gnu_debuglink_filename
= NULL
;
221 /* Whether to convert debugging information. */
222 static bfd_boolean convert_debugging
= FALSE
;
224 /* Whether to compress/decompress DWARF debug sections. */
229 compress_zlib
= compress
| 1 << 1,
230 compress_gnu_zlib
= compress
| 1 << 2,
231 compress_gabi_zlib
= compress
| 1 << 3,
233 } do_debug_sections
= nothing
;
235 /* Whether to generate ELF common symbols with the STT_COMMON type. */
236 static enum bfd_link_elf_stt_common do_elf_stt_common
= unchanged
;
238 /* Whether to change the leading character in symbol names. */
239 static bfd_boolean change_leading_char
= FALSE
;
241 /* Whether to remove the leading character from global symbol names. */
242 static bfd_boolean remove_leading_char
= FALSE
;
244 /* Whether to permit wildcard in symbol comparison. */
245 static bfd_boolean wildcard
= FALSE
;
247 /* True if --localize-hidden is in effect. */
248 static bfd_boolean localize_hidden
= FALSE
;
250 /* List of symbols to strip, keep, localize, keep-global, weaken,
252 static htab_t strip_specific_htab
= NULL
;
253 static htab_t strip_unneeded_htab
= NULL
;
254 static htab_t keep_specific_htab
= NULL
;
255 static htab_t localize_specific_htab
= NULL
;
256 static htab_t globalize_specific_htab
= NULL
;
257 static htab_t keepglobal_specific_htab
= NULL
;
258 static htab_t weaken_specific_htab
= NULL
;
259 static htab_t redefine_specific_htab
= NULL
;
260 static htab_t redefine_specific_reverse_htab
= NULL
;
261 static struct addsym_node
*add_sym_list
= NULL
, **add_sym_tail
= &add_sym_list
;
262 static int add_symbols
= 0;
264 static char *strip_specific_buffer
= NULL
;
265 static char *strip_unneeded_buffer
= NULL
;
266 static char *keep_specific_buffer
= NULL
;
267 static char *localize_specific_buffer
= NULL
;
268 static char *globalize_specific_buffer
= NULL
;
269 static char *keepglobal_specific_buffer
= NULL
;
270 static char *weaken_specific_buffer
= NULL
;
272 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
273 static bfd_boolean weaken
= FALSE
;
275 /* If this is TRUE, we retain BSF_FILE symbols. */
276 static bfd_boolean keep_file_symbols
= FALSE
;
278 /* Prefix symbols/sections. */
279 static char *prefix_symbols_string
= 0;
280 static char *prefix_sections_string
= 0;
281 static char *prefix_alloc_sections_string
= 0;
283 /* True if --extract-symbol was passed on the command line. */
284 static bfd_boolean extract_symbol
= FALSE
;
286 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
287 of <reverse_bytes> bytes within each output section. */
288 static int reverse_bytes
= 0;
290 /* For Coff objects, we may want to allow or disallow long section names,
291 or preserve them where found in the inputs. Debug info relies on them. */
292 enum long_section_name_handling
299 /* The default long section handling mode is to preserve them.
300 This is also the only behaviour for 'strip'. */
301 static enum long_section_name_handling long_section_names
= KEEP
;
303 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
304 enum command_line_switch
306 OPTION_ADD_SECTION
=150,
307 OPTION_ADD_GNU_DEBUGLINK
,
309 OPTION_ALT_MACH_CODE
,
310 OPTION_CHANGE_ADDRESSES
,
311 OPTION_CHANGE_LEADING_CHAR
,
312 OPTION_CHANGE_SECTION_ADDRESS
,
313 OPTION_CHANGE_SECTION_LMA
,
314 OPTION_CHANGE_SECTION_VMA
,
316 OPTION_CHANGE_WARNINGS
,
317 OPTION_COMPRESS_DEBUG_SECTIONS
,
319 OPTION_DECOMPRESS_DEBUG_SECTIONS
,
321 OPTION_ELF_STT_COMMON
,
323 OPTION_EXTRACT_SYMBOL
,
324 OPTION_FILE_ALIGNMENT
,
327 OPTION_GLOBALIZE_SYMBOL
,
328 OPTION_GLOBALIZE_SYMBOLS
,
332 OPTION_INTERLEAVE_WIDTH
,
333 OPTION_KEEPGLOBAL_SYMBOLS
,
334 OPTION_KEEP_FILE_SYMBOLS
,
336 OPTION_LOCALIZE_HIDDEN
,
337 OPTION_LOCALIZE_SYMBOLS
,
338 OPTION_LONG_SECTION_NAMES
,
340 OPTION_NO_MERGE_NOTES
,
341 OPTION_NO_CHANGE_WARNINGS
,
342 OPTION_ONLY_KEEP_DEBUG
,
344 OPTION_PREFIX_ALLOC_SECTIONS
,
345 OPTION_PREFIX_SECTIONS
,
346 OPTION_PREFIX_SYMBOLS
,
348 OPTION_READONLY_TEXT
,
350 OPTION_REDEFINE_SYMS
,
351 OPTION_REMOVE_LEADING_CHAR
,
352 OPTION_REMOVE_RELOCS
,
353 OPTION_RENAME_SECTION
,
354 OPTION_REVERSE_BYTES
,
355 OPTION_PE_SECTION_ALIGNMENT
,
356 OPTION_SET_SECTION_FLAGS
,
357 OPTION_SET_SECTION_ALIGNMENT
,
363 OPTION_STRIP_SYMBOLS
,
364 OPTION_STRIP_UNNEEDED
,
365 OPTION_STRIP_UNNEEDED_SYMBOL
,
366 OPTION_STRIP_UNNEEDED_SYMBOLS
,
368 OPTION_UPDATE_SECTION
,
369 OPTION_VERILOG_DATA_WIDTH
,
371 OPTION_WEAKEN_SYMBOLS
,
375 /* Options to handle if running as "strip". */
377 static struct option strip_options
[] =
379 {"disable-deterministic-archives", no_argument
, 0, 'U'},
380 {"discard-all", no_argument
, 0, 'x'},
381 {"discard-locals", no_argument
, 0, 'X'},
382 {"enable-deterministic-archives", no_argument
, 0, 'D'},
383 {"format", required_argument
, 0, 'F'}, /* Obsolete */
384 {"help", no_argument
, 0, 'h'},
385 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
386 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
387 {"input-target", required_argument
, 0, 'I'},
388 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
389 {"keep-symbol", required_argument
, 0, 'K'},
390 {"merge-notes", no_argument
, 0, 'M'},
391 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
392 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
393 {"output-file", required_argument
, 0, 'o'},
394 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
395 {"output-target", required_argument
, 0, 'O'},
396 {"preserve-dates", no_argument
, 0, 'p'},
397 {"remove-section", required_argument
, 0, 'R'},
398 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
399 {"strip-all", no_argument
, 0, 's'},
400 {"strip-debug", no_argument
, 0, 'S'},
401 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
402 {"strip-symbol", required_argument
, 0, 'N'},
403 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
404 {"target", required_argument
, 0, 'F'},
405 {"verbose", no_argument
, 0, 'v'},
406 {"version", no_argument
, 0, 'V'},
407 {"wildcard", no_argument
, 0, 'w'},
408 {0, no_argument
, 0, 0}
411 /* Options to handle if running as "objcopy". */
413 static struct option copy_options
[] =
415 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
416 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
417 {"add-symbol", required_argument
, 0, OPTION_ADD_SYMBOL
},
418 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
419 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
420 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
421 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
422 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
423 {"binary-architecture", required_argument
, 0, 'B'},
424 {"byte", required_argument
, 0, 'b'},
425 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
426 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
427 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
428 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
429 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
430 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
431 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
432 {"compress-debug-sections", optional_argument
, 0, OPTION_COMPRESS_DEBUG_SECTIONS
},
433 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
434 {"decompress-debug-sections", no_argument
, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS
},
435 {"disable-deterministic-archives", no_argument
, 0, 'U'},
436 {"discard-all", no_argument
, 0, 'x'},
437 {"discard-locals", no_argument
, 0, 'X'},
438 {"dump-section", required_argument
, 0, OPTION_DUMP_SECTION
},
439 {"elf-stt-common", required_argument
, 0, OPTION_ELF_STT_COMMON
},
440 {"enable-deterministic-archives", no_argument
, 0, 'D'},
441 {"extract-dwo", no_argument
, 0, OPTION_EXTRACT_DWO
},
442 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
443 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
444 {"format", required_argument
, 0, 'F'}, /* Obsolete */
445 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
446 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
447 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
448 {"heap", required_argument
, 0, OPTION_HEAP
},
449 {"help", no_argument
, 0, 'h'},
450 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
451 {"impure", no_argument
, 0, OPTION_IMPURE
},
452 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
453 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
454 {"input-target", required_argument
, 0, 'I'},
455 {"interleave", optional_argument
, 0, 'i'},
456 {"interleave-width", required_argument
, 0, OPTION_INTERLEAVE_WIDTH
},
457 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
458 {"keep-global-symbol", required_argument
, 0, 'G'},
459 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
460 {"keep-symbol", required_argument
, 0, 'K'},
461 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
462 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
463 {"localize-symbol", required_argument
, 0, 'L'},
464 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
465 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
466 {"merge-notes", no_argument
, 0, 'M'},
467 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
468 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
469 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
470 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
471 {"only-section", required_argument
, 0, 'j'},
472 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
473 {"output-target", required_argument
, 0, 'O'},
474 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
475 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
476 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
477 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
478 {"preserve-dates", no_argument
, 0, 'p'},
479 {"pure", no_argument
, 0, OPTION_PURE
},
480 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
481 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
482 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
483 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
484 {"remove-section", required_argument
, 0, 'R'},
485 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
486 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
487 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
488 {"section-alignment", required_argument
, 0, OPTION_PE_SECTION_ALIGNMENT
},
489 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
490 {"set-section-alignment", required_argument
, 0, OPTION_SET_SECTION_ALIGNMENT
},
491 {"set-start", required_argument
, 0, OPTION_SET_START
},
492 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
493 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
494 {"stack", required_argument
, 0, OPTION_STACK
},
495 {"strip-all", no_argument
, 0, 'S'},
496 {"strip-debug", no_argument
, 0, 'g'},
497 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
498 {"strip-symbol", required_argument
, 0, 'N'},
499 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
500 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
501 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
502 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
503 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
504 {"target", required_argument
, 0, 'F'},
505 {"update-section", required_argument
, 0, OPTION_UPDATE_SECTION
},
506 {"verbose", no_argument
, 0, 'v'},
507 {"verilog-data-width", required_argument
, 0, OPTION_VERILOG_DATA_WIDTH
},
508 {"version", no_argument
, 0, 'V'},
509 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
510 {"weaken-symbol", required_argument
, 0, 'W'},
511 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
512 {"wildcard", no_argument
, 0, 'w'},
513 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
514 {0, no_argument
, 0, 0}
518 extern char *program_name
;
520 /* This flag distinguishes between strip and objcopy:
521 1 means this is 'strip'; 0 means this is 'objcopy'.
522 -1 means if we should use argv[0] to decide. */
525 /* The maximum length of an S record. This variable is defined in srec.c
526 and can be modified by the --srec-len parameter. */
527 extern unsigned int _bfd_srec_len
;
529 /* Restrict the generation of Srecords to type S3 only.
530 This variable is defined in bfd/srec.c and can be toggled
531 on by the --srec-forceS3 command line switch. */
532 extern bfd_boolean _bfd_srec_forceS3
;
534 /* Width of data in bytes for verilog output.
535 This variable is declared in bfd/verilog.c and can be modified by
536 the --verilog-data-width parameter. */
537 extern unsigned int VerilogDataWidth
;
539 /* Forward declarations. */
540 static void setup_section (bfd
*, asection
*, void *);
541 static void setup_bfd_headers (bfd
*, bfd
*);
542 static void copy_relocations_in_section (bfd
*, asection
*, void *);
543 static void copy_section (bfd
*, asection
*, void *);
544 static void get_sections (bfd
*, asection
*, void *);
545 static int compare_section_lma (const void *, const void *);
546 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
547 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
548 static const char *lookup_sym_redefinition (const char *);
549 static const char *find_section_rename (const char *, flagword
*);
551 ATTRIBUTE_NORETURN
static void
552 copy_usage (FILE *stream
, int exit_status
)
554 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
555 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
556 fprintf (stream
, _(" The options are:\n"));
557 fprintf (stream
, _("\
558 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
559 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
560 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
561 -F --target <bfdname> Set both input and output format to <bfdname>\n\
562 --debugging Convert debugging information, if possible\n\
563 -p --preserve-dates Copy modified/access timestamps to the output\n"));
564 if (DEFAULT_AR_DETERMINISTIC
)
565 fprintf (stream
, _("\
566 -D --enable-deterministic-archives\n\
567 Produce deterministic output when stripping archives (default)\n\
568 -U --disable-deterministic-archives\n\
569 Disable -D behavior\n"));
571 fprintf (stream
, _("\
572 -D --enable-deterministic-archives\n\
573 Produce deterministic output when stripping archives\n\
574 -U --disable-deterministic-archives\n\
575 Disable -D behavior (default)\n"));
576 fprintf (stream
, _("\
577 -j --only-section <name> Only copy section <name> into the output\n\
578 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
579 -R --remove-section <name> Remove section <name> from the output\n\
580 --remove-relocations <name> Remove relocations from section <name>\n\
581 -S --strip-all Remove all symbol and relocation information\n\
582 -g --strip-debug Remove all debugging symbols & sections\n\
583 --strip-dwo Remove all DWO sections\n\
584 --strip-unneeded Remove all symbols not needed by relocations\n\
585 -N --strip-symbol <name> Do not copy symbol <name>\n\
586 --strip-unneeded-symbol <name>\n\
587 Do not copy symbol <name> unless needed by\n\
589 --only-keep-debug Strip everything but the debug information\n\
590 --extract-dwo Copy only DWO sections\n\
591 --extract-symbol Remove section contents but keep symbols\n\
592 -K --keep-symbol <name> Do not strip symbol <name>\n\
593 --keep-file-symbols Do not strip file symbol(s)\n\
594 --localize-hidden Turn all ELF hidden symbols into locals\n\
595 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
596 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
597 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
598 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
599 --weaken Force all global symbols to be marked as weak\n\
600 -w --wildcard Permit wildcard in symbol comparison\n\
601 -x --discard-all Remove all non-global symbols\n\
602 -X --discard-locals Remove any compiler-generated symbols\n\
603 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
604 --interleave-width <number> Set N for --interleave\n\
605 -b --byte <num> Select byte <num> in every interleaved block\n\
606 --gap-fill <val> Fill gaps between sections with <val>\n\
607 --pad-to <addr> Pad the last section up to address <addr>\n\
608 --set-start <addr> Set the start address to <addr>\n\
609 {--change-start|--adjust-start} <incr>\n\
610 Add <incr> to the start address\n\
611 {--change-addresses|--adjust-vma} <incr>\n\
612 Add <incr> to LMA, VMA and start addresses\n\
613 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
614 Change LMA and VMA of section <name> by <val>\n\
615 --change-section-lma <name>{=|+|-}<val>\n\
616 Change the LMA of section <name> by <val>\n\
617 --change-section-vma <name>{=|+|-}<val>\n\
618 Change the VMA of section <name> by <val>\n\
619 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
620 Warn if a named section does not exist\n\
621 --set-section-flags <name>=<flags>\n\
622 Set section <name>'s properties to <flags>\n\
623 --set-section-alignment <name>=<align>\n\
624 Set section <name>'s alignment to <align> bytes\n\
625 --add-section <name>=<file> Add section <name> found in <file> to output\n\
626 --update-section <name>=<file>\n\
627 Update contents of section <name> with\n\
628 contents found in <file>\n\
629 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
630 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
631 --long-section-names {enable|disable|keep}\n\
632 Handle long section names in Coff objects.\n\
633 --change-leading-char Force output format's leading character style\n\
634 --remove-leading-char Remove leading character from global symbols\n\
635 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
636 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
637 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
639 --srec-len <number> Restrict the length of generated Srecords\n\
640 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
641 --strip-symbols <file> -N for all symbols listed in <file>\n\
642 --strip-unneeded-symbols <file>\n\
643 --strip-unneeded-symbol for all symbols listed\n\
645 --keep-symbols <file> -K for all symbols listed in <file>\n\
646 --localize-symbols <file> -L for all symbols listed in <file>\n\
647 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
648 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
649 --weaken-symbols <file> -W for all symbols listed in <file>\n\
650 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
651 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
652 --writable-text Mark the output text as writable\n\
653 --readonly-text Make the output text write protected\n\
654 --pure Mark the output file as demand paged\n\
655 --impure Mark the output file as impure\n\
656 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
657 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
658 --prefix-alloc-sections <prefix>\n\
659 Add <prefix> to start of every allocatable\n\
661 --file-alignment <num> Set PE file alignment to <num>\n\
662 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
664 --image-base <address> Set PE image base to <address>\n\
665 --section-alignment <num> Set PE section alignment to <num>\n\
666 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
668 --subsystem <name>[:<version>]\n\
669 Set PE subsystem to <name> [& <version>]\n\
670 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
671 Compress DWARF debug sections using zlib\n\
672 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
673 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
675 --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
676 -M --merge-notes Remove redundant entries in note sections\n\
677 --no-merge-notes Do not attempt to remove redundant notes (default)\n\
678 -v --verbose List all object files modified\n\
679 @<file> Read options from <file>\n\
680 -V --version Display this program's version number\n\
681 -h --help Display this output\n\
682 --info List object formats & architectures supported\n\
684 list_supported_targets (program_name
, stream
);
685 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
686 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
690 ATTRIBUTE_NORETURN
static void
691 strip_usage (FILE *stream
, int exit_status
)
693 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
694 fprintf (stream
, _(" Removes symbols and sections from files\n"));
695 fprintf (stream
, _(" The options are:\n"));
696 fprintf (stream
, _("\
697 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
698 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
699 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
700 -p --preserve-dates Copy modified/access timestamps to the output\n\
702 if (DEFAULT_AR_DETERMINISTIC
)
703 fprintf (stream
, _("\
704 -D --enable-deterministic-archives\n\
705 Produce deterministic output when stripping archives (default)\n\
706 -U --disable-deterministic-archives\n\
707 Disable -D behavior\n"));
709 fprintf (stream
, _("\
710 -D --enable-deterministic-archives\n\
711 Produce deterministic output when stripping archives\n\
712 -U --disable-deterministic-archives\n\
713 Disable -D behavior (default)\n"));
714 fprintf (stream
, _("\
715 -R --remove-section=<name> Also remove section <name> from the output\n\
716 --remove-relocations <name> Remove relocations from section <name>\n\
717 -s --strip-all Remove all symbol and relocation information\n\
718 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
719 --strip-dwo Remove all DWO sections\n\
720 --strip-unneeded Remove all symbols not needed by relocations\n\
721 --only-keep-debug Strip everything but the debug information\n\
722 -M --merge-notes Remove redundant entries in note sections (default)\n\
723 --no-merge-notes Do not attempt to remove redundant notes\n\
724 -N --strip-symbol=<name> Do not copy symbol <name>\n\
725 -K --keep-symbol=<name> Do not strip symbol <name>\n\
726 --keep-file-symbols Do not strip file symbol(s)\n\
727 -w --wildcard Permit wildcard in symbol comparison\n\
728 -x --discard-all Remove all non-global symbols\n\
729 -X --discard-locals Remove any compiler-generated symbols\n\
730 -v --verbose List all object files modified\n\
731 -V --version Display this program's version number\n\
732 -h --help Display this output\n\
733 --info List object formats & architectures supported\n\
734 -o <file> Place stripped output into <file>\n\
737 list_supported_targets (program_name
, stream
);
738 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
739 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
743 /* Parse section flags into a flagword, with a fatal error if the
744 string can't be parsed. */
747 parse_flags (const char *s
)
757 snext
= strchr (s
, ',');
767 #define PARSE_FLAG(fname,fval) \
768 else if (strncasecmp (fname, s, len) == 0) ret |= fval
769 PARSE_FLAG ("alloc", SEC_ALLOC
);
770 PARSE_FLAG ("load", SEC_LOAD
);
771 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
772 PARSE_FLAG ("readonly", SEC_READONLY
);
773 PARSE_FLAG ("debug", SEC_DEBUGGING
);
774 PARSE_FLAG ("code", SEC_CODE
);
775 PARSE_FLAG ("data", SEC_DATA
);
776 PARSE_FLAG ("rom", SEC_ROM
);
777 PARSE_FLAG ("share", SEC_COFF_SHARED
);
778 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
779 PARSE_FLAG ("merge", SEC_MERGE
);
780 PARSE_FLAG ("strings", SEC_STRINGS
);
786 copy
= (char *) xmalloc (len
+ 1);
787 strncpy (copy
, s
, len
);
789 non_fatal (_("unrecognized section flag `%s'"), copy
);
790 fatal (_("supported flags: %s"),
791 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
801 /* Parse symbol flags into a flagword, with a fatal error if the
802 string can't be parsed. */
805 parse_symflags (const char *s
, char **other
)
815 snext
= strchr (s
, ',');
824 #define PARSE_FLAG(fname, fval) \
825 else if (len == sizeof fname - 1 \
826 && strncasecmp (fname, s, len) == 0) \
829 #define PARSE_OTHER(fname, fval) \
830 else if (len >= sizeof fname \
831 && strncasecmp (fname, s, sizeof fname - 1) == 0) \
832 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
835 PARSE_FLAG ("local", BSF_LOCAL
);
836 PARSE_FLAG ("global", BSF_GLOBAL
);
837 PARSE_FLAG ("export", BSF_EXPORT
);
838 PARSE_FLAG ("debug", BSF_DEBUGGING
);
839 PARSE_FLAG ("function", BSF_FUNCTION
);
840 PARSE_FLAG ("weak", BSF_WEAK
);
841 PARSE_FLAG ("section", BSF_SECTION_SYM
);
842 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR
);
843 PARSE_FLAG ("warning", BSF_WARNING
);
844 PARSE_FLAG ("indirect", BSF_INDIRECT
);
845 PARSE_FLAG ("file", BSF_FILE
);
846 PARSE_FLAG ("object", BSF_OBJECT
);
847 PARSE_FLAG ("synthetic", BSF_SYNTHETIC
);
848 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION
| BSF_FUNCTION
);
849 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE
| BSF_OBJECT
);
850 PARSE_OTHER ("before=", *other
);
858 copy
= (char *) xmalloc (len
+ 1);
859 strncpy (copy
, s
, len
);
861 non_fatal (_("unrecognized symbol flag `%s'"), copy
);
862 fatal (_("supported flags: %s"),
863 "local, global, export, debug, function, weak, section, "
864 "constructor, warning, indirect, file, object, synthetic, "
865 "indirect-function, unique-object, before=<othersym>");
875 /* Find and optionally add an entry in the change_sections list.
877 We need to be careful in how we match section names because of the support
878 for wildcard characters. For example suppose that the user has invoked
881 --set-section-flags .debug_*=debug
882 --set-section-flags .debug_str=readonly,debug
883 --change-section-address .debug_*ranges=0x1000
885 With the idea that all debug sections will receive the DEBUG flag, the
886 .debug_str section will also receive the READONLY flag and the
887 .debug_ranges and .debug_aranges sections will have their address set to
888 0x1000. (This may not make much sense, but it is just an example).
890 When adding the section name patterns to the section list we need to make
891 sure that previous entries do not match with the new entry, unless the
892 match is exact. (In which case we assume that the user is overriding
893 the previous entry with the new context).
895 When matching real section names to the section list we make use of the
896 wildcard characters, but we must do so in context. Eg if we are setting
897 section addresses then we match for .debug_ranges but not for .debug_info.
899 Finally, if ADD is false and we do find a match, we mark the section list
902 static struct section_list
*
903 find_section_list (const char *name
, bfd_boolean add
, unsigned int context
)
905 struct section_list
*p
, *match
= NULL
;
907 /* assert ((context & ((1 << 7) - 1)) != 0); */
909 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
913 if (strcmp (p
->pattern
, name
) == 0)
915 /* Check for context conflicts. */
916 if (((p
->context
& SECTION_CONTEXT_REMOVE
)
917 && (context
& SECTION_CONTEXT_COPY
))
918 || ((context
& SECTION_CONTEXT_REMOVE
)
919 && (p
->context
& SECTION_CONTEXT_COPY
)))
920 fatal (_("error: %s both copied and removed"), name
);
922 if (((p
->context
& SECTION_CONTEXT_SET_VMA
)
923 && (context
& SECTION_CONTEXT_ALTER_VMA
))
924 || ((context
& SECTION_CONTEXT_SET_VMA
)
925 && (context
& SECTION_CONTEXT_ALTER_VMA
)))
926 fatal (_("error: %s both sets and alters VMA"), name
);
928 if (((p
->context
& SECTION_CONTEXT_SET_LMA
)
929 && (context
& SECTION_CONTEXT_ALTER_LMA
))
930 || ((context
& SECTION_CONTEXT_SET_LMA
)
931 && (context
& SECTION_CONTEXT_ALTER_LMA
)))
932 fatal (_("error: %s both sets and alters LMA"), name
);
934 /* Extend the context. */
935 p
->context
|= context
;
939 /* If we are not adding a new name/pattern then
940 only check for a match if the context applies. */
941 else if (p
->context
& context
)
943 /* We could check for the presence of wildchar characters
944 first and choose between calling strcmp and fnmatch,
945 but is that really worth it ? */
946 if (p
->pattern
[0] == '!')
948 if (fnmatch (p
->pattern
+ 1, name
, 0) == 0)
956 if (fnmatch (p
->pattern
, name
, 0) == 0)
972 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
975 p
->context
= context
;
980 p
->next
= change_sections
;
986 /* S1 is the entry node already in the table, S2 is the key node. */
989 eq_string_redefnode (const void *s1
, const void *s2
)
991 struct redefine_node
*node1
= (struct redefine_node
*) s1
;
992 struct redefine_node
*node2
= (struct redefine_node
*) s2
;
993 return !strcmp ((const char *) node1
->source
, (const char *) node2
->source
);
996 /* P is redefine node. Hash value is generated from its "source" filed. */
999 htab_hash_redefnode (const void *p
)
1001 struct redefine_node
*redefnode
= (struct redefine_node
*) p
;
1002 return htab_hash_string (redefnode
->source
);
1005 /* Create hashtab used for redefine node. */
1008 create_symbol2redef_htab (void)
1010 return htab_create_alloc (16, htab_hash_redefnode
, eq_string_redefnode
, NULL
,
1014 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
1017 eq_string (const void *s1
, const void *s2
)
1019 return strcmp ((const char *) s1
, (const char *) s2
) == 0;
1023 create_symbol_htab (void)
1025 return htab_create_alloc (16, htab_hash_string
, eq_string
, NULL
, xcalloc
, free
);
1029 create_symbol_htabs (void)
1031 strip_specific_htab
= create_symbol_htab ();
1032 strip_unneeded_htab
= create_symbol_htab ();
1033 keep_specific_htab
= create_symbol_htab ();
1034 localize_specific_htab
= create_symbol_htab ();
1035 globalize_specific_htab
= create_symbol_htab ();
1036 keepglobal_specific_htab
= create_symbol_htab ();
1037 weaken_specific_htab
= create_symbol_htab ();
1038 redefine_specific_htab
= create_symbol2redef_htab ();
1039 /* As there is no bidirectional hash table in libiberty, need a reverse table
1040 to check duplicated target string. */
1041 redefine_specific_reverse_htab
= create_symbol_htab ();
1044 /* Add a symbol to strip_specific_list. */
1047 add_specific_symbol (const char *name
, htab_t htab
)
1049 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
1052 /* Like add_specific_symbol, but the element type is void *. */
1055 add_specific_symbol_node (const void *node
, htab_t htab
)
1057 *htab_find_slot (htab
, node
, INSERT
) = (void *) node
;
1060 /* Add symbols listed in `filename' to strip_specific_list. */
1062 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
1063 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
1066 add_specific_symbols (const char *filename
, htab_t htab
, char **buffer_p
)
1072 unsigned int line_count
;
1074 size
= get_file_size (filename
);
1081 buffer
= (char *) xmalloc (size
+ 2);
1082 f
= fopen (filename
, FOPEN_RT
);
1084 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
1086 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
1087 fatal (_("%s: fread failed"), filename
);
1090 buffer
[size
] = '\n';
1091 buffer
[size
+ 1] = '\0';
1095 for (line
= buffer
; * line
!= '\0'; line
++)
1100 int finished
= FALSE
;
1102 for (eol
= line
;; eol
++)
1108 /* Cope with \n\r. */
1116 /* Cope with \r\n. */
1127 /* Line comment, Terminate the line here, in case a
1128 name is present and then allow the rest of the
1129 loop to find the real end of the line. */
1141 /* A name may now exist somewhere between 'line' and 'eol'.
1142 Strip off leading whitespace and trailing whitespace,
1143 then add it to the list. */
1144 for (name
= line
; IS_WHITESPACE (* name
); name
++)
1146 for (name_end
= name
;
1147 (! IS_WHITESPACE (* name_end
))
1148 && (! IS_LINE_TERMINATOR (* name_end
));
1152 if (! IS_LINE_TERMINATOR (* name_end
))
1156 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
1159 if (! IS_LINE_TERMINATOR (* extra
))
1160 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1161 filename
, line_count
);
1166 if (name_end
> name
)
1167 add_specific_symbol (name
, htab
);
1169 /* Advance line pointer to end of line. The 'eol ++' in the for
1170 loop above will then advance us to the start of the next line. */
1175 /* Do not free the buffer. Parts of it will have been referenced
1176 in the calls to add_specific_symbol. */
1180 /* See whether a symbol should be stripped or kept
1181 based on strip_specific_list and keep_symbols. */
1184 is_specified_symbol_predicate (void **slot
, void *data
)
1186 struct is_specified_symbol_predicate_data
*d
=
1187 (struct is_specified_symbol_predicate_data
*) data
;
1188 const char *slot_name
= (char *) *slot
;
1190 if (*slot_name
!= '!')
1192 if (! fnmatch (slot_name
, d
->name
, 0))
1195 /* Continue traversal, there might be a non-match rule. */
1201 if (! fnmatch (slot_name
+ 1, d
->name
, 0))
1204 /* Stop traversal. */
1209 /* Continue traversal. */
1214 is_specified_symbol (const char *name
, htab_t htab
)
1218 struct is_specified_symbol_predicate_data data
;
1223 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
1228 return htab_find (htab
, name
) != NULL
;
1231 /* Return a pointer to the symbol used as a signature for GROUP. */
1234 group_signature (asection
*group
)
1236 bfd
*abfd
= group
->owner
;
1237 Elf_Internal_Shdr
*ghdr
;
1239 /* PR 20089: An earlier error may have prevented us from loading the symbol table. */
1243 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
1246 ghdr
= &elf_section_data (group
)->this_hdr
;
1247 if (ghdr
->sh_link
== elf_onesymtab (abfd
))
1249 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1250 Elf_Internal_Shdr
*symhdr
= &elf_symtab_hdr (abfd
);
1252 if (ghdr
->sh_info
> 0
1253 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
1254 return isympp
[ghdr
->sh_info
- 1];
1259 /* Return TRUE if the section is a DWO section. */
1262 is_dwo_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1264 const char *name
= bfd_section_name (sec
);
1265 int len
= strlen (name
);
1267 return strncmp (name
+ len
- 4, ".dwo", 4) == 0;
1270 /* Return TRUE if section SEC is in the update list. */
1273 is_update_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1275 if (update_sections
!= NULL
)
1277 struct section_add
*pupdate
;
1279 for (pupdate
= update_sections
;
1281 pupdate
= pupdate
->next
)
1283 if (strcmp (sec
->name
, pupdate
->name
) == 0)
1292 is_mergeable_note_section (bfd
* abfd
, asection
* sec
)
1295 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1296 && elf_section_data (sec
)->this_hdr
.sh_type
== SHT_NOTE
1297 /* FIXME: We currently only support merging GNU_BUILD_NOTEs.
1298 We should add support for more note types. */
1299 && ((elf_section_data (sec
)->this_hdr
.sh_flags
& SHF_GNU_BUILD_NOTE
) != 0
1300 /* Old versions of GAS (prior to 2.27) could not set the section
1301 flags to OS-specific values, so we also accept sections that
1302 start with the expected name. */
1303 || (CONST_STRNEQ (sec
->name
, GNU_BUILD_ATTRS_SECTION_NAME
))))
1309 /* See if a non-group section is being removed. */
1312 is_strip_section_1 (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1314 if (sections_removed
|| sections_copied
)
1316 struct section_list
*p
;
1317 struct section_list
*q
;
1319 p
= find_section_list (bfd_section_name (sec
), FALSE
,
1320 SECTION_CONTEXT_REMOVE
);
1321 q
= find_section_list (bfd_section_name (sec
), FALSE
,
1322 SECTION_CONTEXT_COPY
);
1325 fatal (_("error: section %s matches both remove and copy options"),
1326 bfd_section_name (sec
));
1327 if (p
&& is_update_section (abfd
, sec
))
1328 fatal (_("error: section %s matches both update and remove options"),
1329 bfd_section_name (sec
));
1333 if (sections_copied
&& q
== NULL
)
1337 if ((bfd_section_flags (sec
) & SEC_DEBUGGING
) != 0)
1339 if (strip_symbols
== STRIP_DEBUG
1340 || strip_symbols
== STRIP_UNNEEDED
1341 || strip_symbols
== STRIP_ALL
1342 || discard_locals
== LOCALS_ALL
1343 || convert_debugging
)
1345 /* By default we don't want to strip .reloc section.
1346 This section has for pe-coff special meaning. See
1347 pe-dll.c file in ld, and peXXigen.c in bfd for details. */
1348 if (strcmp (bfd_section_name (sec
), ".reloc") != 0)
1352 if (strip_symbols
== STRIP_DWO
)
1353 return is_dwo_section (abfd
, sec
);
1355 if (strip_symbols
== STRIP_NONDEBUG
)
1359 if (strip_symbols
== STRIP_NONDWO
)
1360 return !is_dwo_section (abfd
, sec
);
1365 /* See if a section is being removed. */
1368 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1370 if (is_strip_section_1 (abfd
, sec
))
1373 if ((bfd_section_flags (sec
) & SEC_GROUP
) != 0)
1377 asection
*elt
, *first
;
1379 gsym
= group_signature (sec
);
1380 /* Strip groups without a valid signature. */
1385 If we are going to strip the group signature symbol, then
1386 strip the group section too. */
1388 if ((strip_symbols
== STRIP_ALL
1389 && !is_specified_symbol (gname
, keep_specific_htab
))
1390 || is_specified_symbol (gname
, strip_specific_htab
))
1393 /* Remove the group section if all members are removed. */
1394 first
= elt
= elf_next_in_group (sec
);
1397 if (!is_strip_section_1 (abfd
, elt
))
1399 elt
= elf_next_in_group (elt
);
1411 is_nondebug_keep_contents_section (bfd
*ibfd
, asection
*isection
)
1413 /* Always keep ELF note sections. */
1414 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
1415 return (elf_section_type (isection
) == SHT_NOTE
);
1417 /* Always keep the .buildid section for PE/COFF.
1419 Strictly, this should be written "always keep the section storing the debug
1420 directory", but that may be the .text section for objects produced by some
1421 tools, which it is not sensible to keep. */
1422 if (ibfd
->xvec
->flavour
== bfd_target_coff_flavour
)
1423 return (strcmp (bfd_section_name (isection
), ".buildid") == 0);
1428 /* Return true if SYM is a hidden symbol. */
1431 is_hidden_symbol (asymbol
*sym
)
1433 elf_symbol_type
*elf_sym
;
1435 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
1436 if (elf_sym
!= NULL
)
1437 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
1447 need_sym_before (struct addsym_node
**node
, const char *sym
)
1450 struct addsym_node
*ptr
= add_sym_list
;
1452 /* 'othersym' symbols are at the front of the list. */
1453 for (count
= 0; count
< add_symbols
; count
++)
1457 else if (strcmp (ptr
->othersym
, sym
) == 0)
1459 free (ptr
->othersym
);
1460 ptr
->othersym
= ""; /* Empty name is hopefully never a valid symbol name. */
1470 create_new_symbol (struct addsym_node
*ptr
, bfd
*obfd
)
1472 asymbol
*sym
= bfd_make_empty_symbol (obfd
);
1474 bfd_set_asymbol_name (sym
, ptr
->symdef
);
1475 sym
->value
= ptr
->symval
;
1476 sym
->flags
= ptr
->flags
;
1479 asection
*sec
= bfd_get_section_by_name (obfd
, ptr
->section
);
1481 fatal (_("Section %s not found"), ptr
->section
);
1485 sym
->section
= bfd_abs_section_ptr
;
1489 /* Choose which symbol entries to copy; put the result in OSYMS.
1490 We don't copy in place, because that confuses the relocs.
1491 Return the number of symbols to print. */
1494 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
1495 asymbol
**isyms
, long symcount
)
1497 asymbol
**from
= isyms
, **to
= osyms
;
1498 long src_count
= 0, dst_count
= 0;
1499 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
1501 for (; src_count
< symcount
; src_count
++)
1503 asymbol
*sym
= from
[src_count
];
1504 flagword flags
= sym
->flags
;
1505 char *name
= (char *) bfd_asymbol_name (sym
);
1507 bfd_boolean used_in_reloc
= FALSE
;
1508 bfd_boolean undefined
;
1509 bfd_boolean rem_leading_char
;
1510 bfd_boolean add_leading_char
;
1512 undefined
= bfd_is_und_section (bfd_asymbol_section (sym
));
1516 struct addsym_node
*ptr
;
1518 if (need_sym_before (&ptr
, name
))
1519 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1522 if (htab_elements (redefine_specific_htab
) || section_rename_list
)
1526 new_name
= (char *) lookup_sym_redefinition (name
);
1527 if (new_name
== name
1528 && (flags
& BSF_SECTION_SYM
) != 0)
1529 new_name
= (char *) find_section_rename (name
, NULL
);
1530 bfd_set_asymbol_name (sym
, new_name
);
1534 /* Check if we will remove the current leading character. */
1536 (name
[0] == bfd_get_symbol_leading_char (abfd
))
1537 && (change_leading_char
1538 || (remove_leading_char
1539 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1541 || bfd_is_com_section (bfd_asymbol_section (sym
)))));
1543 /* Check if we will add a new leading character. */
1546 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1547 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1548 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1550 /* Short circuit for change_leading_char if we can do it in-place. */
1551 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1553 name
[0] = bfd_get_symbol_leading_char (obfd
);
1554 bfd_set_asymbol_name (sym
, name
);
1555 rem_leading_char
= FALSE
;
1556 add_leading_char
= FALSE
;
1559 /* Remove leading char. */
1560 if (rem_leading_char
)
1561 bfd_set_asymbol_name (sym
, ++name
);
1563 /* Add new leading char and/or prefix. */
1564 if (add_leading_char
|| prefix_symbols_string
)
1568 ptr
= n
= (char *) xmalloc (1 + strlen (prefix_symbols_string
)
1569 + strlen (name
) + 1);
1570 if (add_leading_char
)
1571 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1573 if (prefix_symbols_string
)
1575 strcpy (ptr
, prefix_symbols_string
);
1576 ptr
+= strlen (prefix_symbols_string
);
1580 bfd_set_asymbol_name (sym
, n
);
1584 if (strip_symbols
== STRIP_ALL
)
1586 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1587 || ((flags
& BSF_SECTION_SYM
) != 0
1588 && ((*bfd_asymbol_section (sym
)->symbol_ptr_ptr
)->flags
1592 used_in_reloc
= TRUE
;
1594 else if (relocatable
/* Relocatable file. */
1595 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1596 || bfd_is_com_section (bfd_asymbol_section (sym
))))
1598 else if (bfd_decode_symclass (sym
) == 'I')
1599 /* Global symbols in $idata sections need to be retained
1600 even if relocatable is FALSE. External users of the
1601 library containing the $idata section may reference these
1604 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1605 || (flags
& BSF_WEAK
) != 0
1607 || bfd_is_com_section (bfd_asymbol_section (sym
)))
1608 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1609 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1610 keep
= (strip_symbols
!= STRIP_DEBUG
1611 && strip_symbols
!= STRIP_UNNEEDED
1612 && ! convert_debugging
);
1613 else if (bfd_coff_get_comdat_section (abfd
, bfd_asymbol_section (sym
)))
1614 /* COMDAT sections store special information in local
1615 symbols, so we cannot risk stripping any of them. */
1617 else /* Local symbol. */
1618 keep
= (strip_symbols
!= STRIP_UNNEEDED
1619 && (discard_locals
!= LOCALS_ALL
1620 && (discard_locals
!= LOCALS_START_L
1621 || ! bfd_is_local_label (abfd
, sym
))));
1623 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1625 /* There are multiple ways to set 'keep' above, but if it
1626 was the relocatable symbol case, then that's an error. */
1629 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1637 && !(flags
& BSF_KEEP
)
1638 && is_specified_symbol (name
, strip_unneeded_htab
))
1642 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1643 || is_specified_symbol (name
, keep_specific_htab
)))
1646 if (keep
&& is_strip_section (abfd
, bfd_asymbol_section (sym
)))
1651 if ((flags
& BSF_GLOBAL
) != 0
1652 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1654 sym
->flags
&= ~ BSF_GLOBAL
;
1655 sym
->flags
|= BSF_WEAK
;
1659 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1660 && (is_specified_symbol (name
, localize_specific_htab
)
1661 || (htab_elements (keepglobal_specific_htab
) != 0
1662 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1663 || (localize_hidden
&& is_hidden_symbol (sym
))))
1665 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1666 sym
->flags
|= BSF_LOCAL
;
1670 && (flags
& BSF_LOCAL
)
1671 && is_specified_symbol (name
, globalize_specific_htab
))
1673 sym
->flags
&= ~ BSF_LOCAL
;
1674 sym
->flags
|= BSF_GLOBAL
;
1677 to
[dst_count
++] = sym
;
1682 struct addsym_node
*ptr
= add_sym_list
;
1684 for (src_count
= 0; src_count
< add_symbols
; src_count
++)
1688 if (strcmp (ptr
->othersym
, ""))
1689 fatal (_("'before=%s' not found"), ptr
->othersym
);
1692 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1698 to
[dst_count
] = NULL
;
1703 /* Find the redefined name of symbol SOURCE. */
1706 lookup_sym_redefinition (const char *source
)
1708 struct redefine_node key_node
= {(char *) source
, NULL
};
1709 struct redefine_node
*redef_node
1710 = (struct redefine_node
*) htab_find (redefine_specific_htab
, &key_node
);
1712 return redef_node
== NULL
? source
: redef_node
->target
;
1715 /* Insert a node into symbol redefine hash tabel. */
1718 add_redefine_and_check (const char *cause
, const char *source
,
1721 struct redefine_node
*new_node
1722 = (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
1724 new_node
->source
= strdup (source
);
1725 new_node
->target
= strdup (target
);
1727 if (htab_find (redefine_specific_htab
, new_node
) != HTAB_EMPTY_ENTRY
)
1728 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1731 if (htab_find (redefine_specific_reverse_htab
, target
) != HTAB_EMPTY_ENTRY
)
1732 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1735 /* Insert the NEW_NODE into hash table for quick search. */
1736 add_specific_symbol_node (new_node
, redefine_specific_htab
);
1738 /* Insert the target string into the reverse hash table, this is needed for
1739 duplicated target string check. */
1740 add_specific_symbol (new_node
->target
, redefine_specific_reverse_htab
);
1744 /* Handle the --redefine-syms option. Read lines containing "old new"
1745 from the file, and add them to the symbol redefine list. */
1748 add_redefine_syms_file (const char *filename
)
1757 file
= fopen (filename
, "r");
1759 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1760 filename
, strerror (errno
));
1763 buf
= (char *) xmalloc (bufsize
+ 1 /* For the terminating NUL. */);
1771 /* Collect the input symbol name. */
1772 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1780 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1788 /* Eat white space between the symbol names. */
1789 while (IS_WHITESPACE (c
))
1791 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1796 /* Collect the output symbol name. */
1798 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1806 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1814 /* Eat white space at end of line. */
1815 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1820 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1821 || c
== '\n' || c
== EOF
)
1824 /* Append the redefinition to the list. */
1826 add_redefine_and_check (filename
, &buf
[0], &buf
[outsym_off
]);
1837 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1839 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1840 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1843 /* Eat the rest of the line and finish it. */
1844 while (c
!= '\n' && c
!= EOF
)
1850 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1856 /* Copy unknown object file IBFD onto OBFD.
1857 Returns TRUE upon success, FALSE otherwise. */
1860 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1868 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1870 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1877 non_fatal (_("stat returns negative size for `%s'"),
1878 bfd_get_archive_filename (ibfd
));
1882 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1884 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1889 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1890 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1892 cbuf
= (char *) xmalloc (BUFSIZE
);
1894 while (ncopied
< size
)
1896 tocopy
= size
- ncopied
;
1897 if (tocopy
> BUFSIZE
)
1900 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1901 != (bfd_size_type
) tocopy
)
1903 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1908 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1909 != (bfd_size_type
) tocopy
)
1911 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1919 /* We should at least to be able to read it back when copying an
1920 unknown object in an archive. */
1921 chmod (bfd_get_filename (obfd
), buf
.st_mode
| S_IRUSR
);
1926 typedef struct objcopy_internal_note
1928 Elf_Internal_Note note
;
1929 unsigned long padded_namesz
;
1932 } objcopy_internal_note
;
1934 #define DEBUG_MERGE 0
1937 #define merge_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
1939 #define merge_debug(format, ...)
1942 /* Returns TRUE iff PNOTE1 overlaps or adjoins PNOTE2. */
1945 overlaps_or_adjoins (objcopy_internal_note
* pnote1
,
1946 objcopy_internal_note
* pnote2
)
1948 if (pnote1
->end
< pnote2
->start
)
1949 /* FIXME: Alignment of 16 bytes taken from x86_64 binaries.
1950 Really we should extract the alignment of the section
1951 covered by the notes. */
1952 return BFD_ALIGN (pnote1
->end
, 16) < pnote2
->start
;
1954 if (pnote2
->end
< pnote2
->start
)
1955 return BFD_ALIGN (pnote2
->end
, 16) < pnote1
->start
;
1957 if (pnote1
->end
< pnote2
->end
)
1960 if (pnote2
->end
< pnote1
->end
)
1966 /* Returns TRUE iff NEEDLE is fully contained by HAYSTACK. */
1969 contained_by (objcopy_internal_note
* needle
,
1970 objcopy_internal_note
* haystack
)
1972 return needle
->start
>= haystack
->start
&& needle
->end
<= haystack
->end
;
1976 is_open_note (objcopy_internal_note
* pnote
)
1978 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_OPEN
;
1982 is_func_note (objcopy_internal_note
* pnote
)
1984 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_FUNC
;
1988 is_deleted_note (objcopy_internal_note
* pnote
)
1990 return pnote
->note
.type
== 0;
1994 is_version_note (objcopy_internal_note
* pnote
)
1996 return (pnote
->note
.namesz
> 4
1997 && pnote
->note
.namedata
[0] == 'G'
1998 && pnote
->note
.namedata
[1] == 'A'
1999 && pnote
->note
.namedata
[2] == '$'
2000 && pnote
->note
.namedata
[3] == GNU_BUILD_ATTRIBUTE_VERSION
);
2004 is_64bit (bfd
* abfd
)
2006 /* Should never happen, but let's be paranoid. */
2007 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
2010 return elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
;
2013 /* This sorting function is used to get the notes into an order
2014 that makes merging easy. */
2017 compare_gnu_build_notes (const void * data1
, const void * data2
)
2019 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2020 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2022 /* Sort notes based upon the attribute they record. */
2023 int cmp
= memcmp (pnote1
->note
.namedata
+ 3,
2024 pnote2
->note
.namedata
+ 3,
2025 pnote1
->note
.namesz
< pnote2
->note
.namesz
?
2026 pnote1
->note
.namesz
- 3 : pnote2
->note
.namesz
- 3);
2030 if (pnote1
->end
< pnote2
->start
)
2032 if (pnote1
->start
> pnote2
->end
)
2035 /* Overlaps - we should merge the two ranges. */
2036 if (pnote1
->start
< pnote2
->start
)
2038 if (pnote1
->end
> pnote2
->end
)
2041 /* Put OPEN notes before function notes. */
2042 if (is_open_note (pnote1
) && ! is_open_note (pnote2
))
2044 if (! is_open_note (pnote1
) && is_open_note (pnote2
))
2050 /* This sorting function is used to get the notes into an order
2051 that makes eliminating address ranges easier. */
2054 sort_gnu_build_notes (const void * data1
, const void * data2
)
2056 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2057 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2059 if (pnote1
->note
.type
!= pnote2
->note
.type
)
2061 /* Move deleted notes to the end. */
2062 if (is_deleted_note (pnote1
)) /* 1: OFD 2: OFD */
2065 /* Move OPEN notes to the start. */
2066 if (is_open_note (pnote1
)) /* 1: OF 2: OFD */
2069 if (is_deleted_note (pnote2
)) /* 1: F 2: O D */
2072 return 1; /* 1: F 2: O */
2075 /* Sort by starting address. */
2076 if (pnote1
->start
< pnote2
->start
)
2078 if (pnote1
->start
> pnote2
->start
)
2081 /* Then by end address (bigger range first). */
2082 if (pnote1
->end
> pnote2
->end
)
2084 if (pnote1
->end
< pnote2
->end
)
2087 /* Then by attribute type. */
2088 if (pnote1
->note
.namesz
> 4
2089 && pnote2
->note
.namesz
> 4
2090 && pnote1
->note
.namedata
[3] != pnote2
->note
.namedata
[3])
2091 return pnote1
->note
.namedata
[3] - pnote2
->note
.namedata
[3];
2096 /* Merge the notes on SEC, removing redundant entries.
2097 Returns the new, smaller size of the section upon success. */
2099 static bfd_size_type
2100 merge_gnu_build_notes (bfd
* abfd
,
2103 bfd_byte
* contents
)
2105 objcopy_internal_note
* pnotes_end
;
2106 objcopy_internal_note
* pnotes
= NULL
;
2107 objcopy_internal_note
* pnote
;
2108 bfd_size_type remain
= size
;
2109 unsigned version_1_seen
= 0;
2110 unsigned version_2_seen
= 0;
2111 unsigned version_3_seen
= 0;
2112 const char * err
= NULL
;
2113 bfd_byte
* in
= contents
;
2114 unsigned long previous_func_start
= 0;
2115 unsigned long previous_open_start
= 0;
2116 unsigned long previous_func_end
= 0;
2117 unsigned long previous_open_end
= 0;
2120 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
2126 /* If there are relocs associated with this section then we
2127 cannot safely merge it. */
2128 relpp
= (arelent
**) xmalloc (relsize
);
2129 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, isympp
);
2134 non_fatal (_("%s[%s]: Cannot merge - there are relocations against this section"),
2135 bfd_get_filename (abfd
), bfd_section_name (sec
));
2140 /* Make a copy of the notes and convert to our internal format.
2141 Minimum size of a note is 12 bytes. Also locate the version
2142 notes and check them. */
2143 pnote
= pnotes
= (objcopy_internal_note
*)
2144 xcalloc ((size
/ 12), sizeof (* pnote
));
2145 while (remain
>= 12)
2149 pnote
->note
.namesz
= bfd_get_32 (abfd
, in
);
2150 pnote
->note
.descsz
= bfd_get_32 (abfd
, in
+ 4);
2151 pnote
->note
.type
= bfd_get_32 (abfd
, in
+ 8);
2152 pnote
->padded_namesz
= (pnote
->note
.namesz
+ 3) & ~3;
2154 if (((pnote
->note
.descsz
+ 3) & ~3) != pnote
->note
.descsz
)
2156 err
= _("corrupt GNU build attribute note: description size not a factor of 4");
2160 if (pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_OPEN
2161 && pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_FUNC
)
2163 err
= _("corrupt GNU build attribute note: wrong note type");
2167 if (pnote
->padded_namesz
+ pnote
->note
.descsz
+ 12 > remain
)
2169 err
= _("corrupt GNU build attribute note: note too big");
2173 if (pnote
->note
.namesz
< 2)
2175 err
= _("corrupt GNU build attribute note: name too small");
2179 pnote
->note
.namedata
= (char *)(in
+ 12);
2180 pnote
->note
.descdata
= (char *)(in
+ 12 + pnote
->padded_namesz
);
2182 remain
-= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2183 in
+= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2185 if (pnote
->note
.namesz
> 2
2186 && pnote
->note
.namedata
[0] == '$'
2187 && pnote
->note
.namedata
[1] == GNU_BUILD_ATTRIBUTE_VERSION
2188 && pnote
->note
.namedata
[2] == '1')
2190 else if (is_version_note (pnote
))
2192 if (pnote
->note
.namedata
[4] == '2')
2194 else if (pnote
->note
.namedata
[4] == '3')
2198 err
= _("corrupt GNU build attribute note: unsupported version");
2203 switch (pnote
->note
.descsz
)
2210 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2211 /* FIXME: For version 1 and 2 notes we should try to
2212 calculate the end address by finding a symbol whose
2213 value is START, and then adding in its size.
2215 For now though, since v1 and v2 was not intended to
2216 handle gaps, we chose an artificially large end
2222 if (! is_64bit (abfd
))
2224 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2225 end
= bfd_get_32 (abfd
, pnote
->note
.descdata
+ 4);
2229 start
= bfd_get_64 (abfd
, pnote
->note
.descdata
);
2230 /* FIXME: For version 1 and 2 notes we should try to
2231 calculate the end address by finding a symbol whose
2232 value is START, and then adding in its size.
2234 For now though, since v1 and v2 was not intended to
2235 handle gaps, we chose an artificially large end
2242 start
= bfd_get_64 (abfd
, pnote
->note
.descdata
);
2243 end
= bfd_get_64 (abfd
, pnote
->note
.descdata
+ 8);
2247 err
= _("corrupt GNU build attribute note: bad description size");
2251 if (is_open_note (pnote
))
2254 previous_open_start
= start
;
2256 pnote
->start
= previous_open_start
;
2259 previous_open_end
= end
;
2261 pnote
->end
= previous_open_end
;
2266 previous_func_start
= start
;
2268 pnote
->start
= previous_func_start
;
2271 previous_func_end
= end
;
2273 pnote
->end
= previous_func_end
;
2276 if (pnote
->note
.namedata
[pnote
->note
.namesz
- 1] != 0)
2278 err
= _("corrupt GNU build attribute note: name not NUL terminated");
2287 /* Check that the notes are valid. */
2290 err
= _("corrupt GNU build attribute notes: excess data at end");
2294 if (version_1_seen
== 0 && version_2_seen
== 0 && version_3_seen
== 0)
2297 err
= _("bad GNU build attribute notes: no known versions detected");
2300 /* This happens with glibc. No idea why. */
2301 non_fatal (_("%s[%s]: Warning: version note missing - assuming version 3"),
2302 bfd_get_filename (abfd
), bfd_section_name (sec
));
2307 if ( (version_1_seen
> 0 && version_2_seen
> 0)
2308 || (version_1_seen
> 0 && version_3_seen
> 0)
2309 || (version_2_seen
> 0 && version_3_seen
> 0))
2311 err
= _("bad GNU build attribute notes: multiple different versions");
2315 /* We are now only supporting the merging v3+ notes
2316 - it makes things much simpler. */
2317 if (version_3_seen
== 0)
2319 merge_debug ("%s: skipping merge - not using v3 notes", bfd_section_name (sec
));
2323 merge_debug ("Merging section %s which contains %ld notes\n",
2324 sec
->name
, pnotes_end
- pnotes
);
2326 /* Sort the notes. */
2327 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
),
2328 compare_gnu_build_notes
);
2331 merge_debug ("Results of initial sort:\n");
2332 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2333 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2334 (pnote
->note
.namedata
- (char *) contents
) - 12,
2335 pnote
->start
, pnote
->end
,
2337 pnote
->note
.namedata
[3],
2342 /* Now merge the notes. The rules are:
2343 1. If a note has a zero range, it can be eliminated.
2344 2. If two notes have the same namedata then:
2345 2a. If one note's range is fully covered by the other note
2346 then it can be deleted.
2347 2b. If one note's range partially overlaps or adjoins the
2348 other note then if they are both of the same type (open
2349 or func) then they can be merged and one deleted. If
2350 they are of different types then they cannot be merged. */
2351 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2353 /* Skip already deleted notes.
2354 FIXME: Can this happen ? We are scanning forwards and
2355 deleting backwards after all. */
2356 if (is_deleted_note (pnote
))
2359 /* Rule 1 - delete 0-range notes. */
2360 if (pnote
->start
== pnote
->end
)
2362 merge_debug ("Delete note at offset %#08lx - empty range\n",
2363 (pnote
->note
.namedata
- (char *) contents
) - 12);
2364 pnote
->note
.type
= 0;
2369 objcopy_internal_note
* back
;
2371 /* Rule 2: Check to see if there is an identical previous note. */
2372 for (iter
= 0, back
= pnote
- 1; back
>= pnotes
; back
--)
2374 if (is_deleted_note (back
))
2377 /* Our sorting function should have placed all identically
2378 attributed notes together, so if we see a note of a different
2379 attribute type stop searching. */
2380 if (back
->note
.namesz
!= pnote
->note
.namesz
2381 || memcmp (back
->note
.namedata
,
2382 pnote
->note
.namedata
, pnote
->note
.namesz
) != 0)
2385 if (back
->start
== pnote
->start
2386 && back
->end
== pnote
->end
)
2388 merge_debug ("Delete note at offset %#08lx - duplicate of note at offset %#08lx\n",
2389 (pnote
->note
.namedata
- (char *) contents
) - 12,
2390 (back
->note
.namedata
- (char *) contents
) - 12);
2391 pnote
->note
.type
= 0;
2396 if (contained_by (pnote
, back
))
2398 merge_debug ("Delete note at offset %#08lx - fully contained by note at %#08lx\n",
2399 (pnote
->note
.namedata
- (char *) contents
) - 12,
2400 (back
->note
.namedata
- (char *) contents
) - 12);
2401 pnote
->note
.type
= 0;
2406 /* This should not happen as we have sorted the
2407 notes with earlier starting addresses first. */
2408 if (contained_by (back
, pnote
))
2409 merge_debug ("ERROR: UNEXPECTED CONTAINMENT\n");
2413 if (overlaps_or_adjoins (back
, pnote
)
2414 && is_func_note (back
) == is_func_note (pnote
))
2416 merge_debug ("Delete note at offset %#08lx - merge into note at %#08lx\n",
2417 (pnote
->note
.namedata
- (char *) contents
) - 12,
2418 (back
->note
.namedata
- (char *) contents
) - 12);
2420 back
->end
= back
->end
> pnote
->end
? back
->end
: pnote
->end
;
2421 back
->start
= back
->start
< pnote
->start
? back
->start
: pnote
->start
;
2422 pnote
->note
.type
= 0;
2426 /* Don't scan too far back however. */
2429 /* FIXME: Not sure if this can ever be triggered. */
2430 merge_debug ("ITERATION LIMIT REACHED\n");
2435 if (! is_deleted_note (pnote
))
2436 merge_debug ("Unable to do anything with note at %#08lx\n",
2437 (pnote
->note
.namedata
- (char *) contents
) - 12);
2441 /* Resort the notes. */
2442 merge_debug ("Final sorting of notes\n");
2443 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
), sort_gnu_build_notes
);
2445 /* Reconstruct the ELF notes. */
2446 bfd_byte
* new_contents
;
2449 bfd_size_type new_size
;
2450 bfd_vma prev_start
= 0;
2451 bfd_vma prev_end
= 0;
2453 new = new_contents
= xmalloc (size
);
2454 for (pnote
= pnotes
, old
= contents
;
2458 bfd_size_type note_size
= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2460 if (! is_deleted_note (pnote
))
2462 /* Create the note, potentially using the
2463 address range of the previous note. */
2464 if (pnote
->start
== prev_start
&& pnote
->end
== prev_end
)
2466 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2467 bfd_put_32 (abfd
, 0, new + 4);
2468 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2470 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2471 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2472 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2473 new += pnote
->padded_namesz
;
2477 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2478 bfd_put_32 (abfd
, is_64bit (abfd
) ? 16 : 8, new + 4);
2479 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2481 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2482 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2483 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2484 new += pnote
->padded_namesz
;
2485 if (is_64bit (abfd
))
2487 bfd_put_64 (abfd
, pnote
->start
, new);
2488 bfd_put_64 (abfd
, pnote
->end
, new + 8);
2493 bfd_put_32 (abfd
, pnote
->start
, new);
2494 bfd_put_32 (abfd
, pnote
->end
, new + 4);
2498 prev_start
= pnote
->start
;
2499 prev_end
= pnote
->end
;
2507 merge_debug ("Results of merge:\n");
2508 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2509 if (! is_deleted_note (pnote
))
2510 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2511 (pnote
->note
.namedata
- (char *) contents
) - 12,
2512 pnote
->start
, pnote
->end
,
2514 pnote
->note
.namedata
[3],
2519 new_size
= new - new_contents
;
2520 memcpy (contents
, new_contents
, new_size
);
2522 free (new_contents
);
2527 bfd_set_error (bfd_error_bad_value
);
2528 bfd_nonfatal_message (NULL
, abfd
, sec
, err
);
2536 /* Copy object file IBFD onto OBFD.
2537 Returns TRUE upon success, FALSE otherwise. */
2540 copy_object (bfd
*ibfd
, bfd
*obfd
, const bfd_arch_info_type
*input_arch
)
2544 asection
**osections
= NULL
;
2546 asection
*gnu_debuglink_section
= NULL
;
2547 bfd_size_type
*gaps
= NULL
;
2548 bfd_size_type max_gap
= 0;
2551 enum bfd_architecture iarch
;
2555 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
2556 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
2557 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
2559 /* PR 17636: Call non-fatal so that we return to our parent who
2560 may need to tidy temporary files. */
2561 non_fatal (_("Unable to change endianness of input file(s)"));
2565 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2567 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
2571 if (ibfd
->sections
== NULL
)
2573 non_fatal (_("error: the input file '%s' has no sections"),
2574 bfd_get_archive_filename (ibfd
));
2578 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
2580 if ((do_debug_sections
& compress
) != 0
2581 && do_debug_sections
!= compress
)
2583 non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
2584 bfd_get_archive_filename (ibfd
));
2588 if (do_elf_stt_common
)
2590 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
2591 bfd_get_archive_filename (ibfd
));
2597 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
2598 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
2599 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2608 start
= bfd_get_start_address (ibfd
);
2609 start
+= change_start
;
2612 /* Neither the start address nor the flags
2613 need to be set for a core file. */
2614 if (bfd_get_format (obfd
) != bfd_core
)
2618 flags
= bfd_get_file_flags (ibfd
);
2619 flags
|= bfd_flags_to_set
;
2620 flags
&= ~bfd_flags_to_clear
;
2621 flags
&= bfd_applicable_file_flags (obfd
);
2623 if (strip_symbols
== STRIP_ALL
)
2624 flags
&= ~HAS_RELOC
;
2626 if (!bfd_set_start_address (obfd
, start
)
2627 || !bfd_set_file_flags (obfd
, flags
))
2629 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2634 /* Copy architecture of input file to output file. */
2635 iarch
= bfd_get_arch (ibfd
);
2636 imach
= bfd_get_mach (ibfd
);
2639 if (bfd_get_arch_info (ibfd
) == NULL
2640 || bfd_get_arch_info (ibfd
)->arch
== bfd_arch_unknown
)
2642 iarch
= input_arch
->arch
;
2643 imach
= input_arch
->mach
;
2646 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
2647 bfd_get_archive_filename (ibfd
));
2649 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
2650 && (ibfd
->target_defaulted
2651 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
2653 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
2654 non_fatal (_("Unable to recognise the format of the input file `%s'"),
2655 bfd_get_archive_filename (ibfd
));
2657 non_fatal (_("Output file cannot represent architecture `%s'"),
2658 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
2659 bfd_get_mach (ibfd
)));
2663 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2665 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2669 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2670 && bfd_pei_p (obfd
))
2672 /* Set up PE parameters. */
2673 pe_data_type
*pe
= pe_data (obfd
);
2675 /* Copy PE parameters before changing them. */
2676 if (ibfd
->xvec
->flavour
== bfd_target_coff_flavour
2677 && bfd_pei_p (ibfd
))
2678 pe
->pe_opthdr
= pe_data (ibfd
)->pe_opthdr
;
2680 if (pe_file_alignment
!= (bfd_vma
) -1)
2681 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
2683 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
2685 if (pe_heap_commit
!= (bfd_vma
) -1)
2686 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
2688 if (pe_heap_reserve
!= (bfd_vma
) -1)
2689 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
2691 if (pe_image_base
!= (bfd_vma
) -1)
2692 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
2694 if (pe_section_alignment
!= (bfd_vma
) -1)
2695 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
2697 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
2699 if (pe_stack_commit
!= (bfd_vma
) -1)
2700 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
2702 if (pe_stack_reserve
!= (bfd_vma
) -1)
2703 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_reserve
;
2705 if (pe_subsystem
!= -1)
2706 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
2708 if (pe_major_subsystem_version
!= -1)
2709 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
2711 if (pe_minor_subsystem_version
!= -1)
2712 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
2714 if (pe_file_alignment
> pe_section_alignment
)
2716 char file_alignment
[20], section_alignment
[20];
2718 sprintf_vma (file_alignment
, pe_file_alignment
);
2719 sprintf_vma (section_alignment
, pe_section_alignment
);
2720 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
2722 file_alignment
, section_alignment
);
2729 if (osympp
!= isympp
)
2735 symsize
= bfd_get_symtab_upper_bound (ibfd
);
2738 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2742 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
2743 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
2746 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2749 /* PR 17512: file: d6323821
2750 If the symbol table could not be loaded do not pretend that we have
2751 any symbols. This trips us up later on when we load the relocs. */
2755 osympp
= isympp
= NULL
;
2758 /* BFD mandates that all output sections be created and sizes set before
2759 any output is done. Thus, we traverse all sections multiple times. */
2760 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
2762 if (!extract_symbol
)
2763 setup_bfd_headers (ibfd
, obfd
);
2765 if (add_sections
!= NULL
)
2767 struct section_add
*padd
;
2768 struct section_list
*pset
;
2770 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
2774 pset
= find_section_list (padd
->name
, FALSE
,
2775 SECTION_CONTEXT_SET_FLAGS
);
2777 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
2779 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
2781 /* bfd_make_section_with_flags() does not return very helpful
2782 error codes, so check for the most likely user error first. */
2783 if (bfd_get_section_by_name (obfd
, padd
->name
))
2785 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2786 _("can't add section '%s'"), padd
->name
);
2791 /* We use LINKER_CREATED here so that the backend hooks
2792 will create any special section type information,
2793 instead of presuming we know what we're doing merely
2794 because we set the flags. */
2795 padd
->section
= bfd_make_section_with_flags
2796 (obfd
, padd
->name
, flags
| SEC_LINKER_CREATED
);
2797 if (padd
->section
== NULL
)
2799 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2800 _("can't create section `%s'"),
2806 if (!bfd_set_section_size (padd
->section
, padd
->size
))
2808 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2812 pset
= find_section_list (padd
->name
, FALSE
,
2813 SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
);
2815 && !bfd_set_section_vma (padd
->section
, pset
->vma_val
))
2817 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2821 pset
= find_section_list (padd
->name
, FALSE
,
2822 SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
);
2825 padd
->section
->lma
= pset
->lma_val
;
2827 if (!bfd_set_section_alignment
2828 (padd
->section
, bfd_section_alignment (padd
->section
)))
2830 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2837 if (update_sections
!= NULL
)
2839 struct section_add
*pupdate
;
2841 for (pupdate
= update_sections
;
2843 pupdate
= pupdate
->next
)
2845 pupdate
->section
= bfd_get_section_by_name (ibfd
, pupdate
->name
);
2846 if (pupdate
->section
== NULL
)
2848 non_fatal (_("error: %s not found, can't be updated"), pupdate
->name
);
2852 osec
= pupdate
->section
->output_section
;
2853 if (!bfd_set_section_size (osec
, pupdate
->size
))
2855 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
2861 merged_note_section
* merged_note_sections
= NULL
;
2864 /* This palaver is necessary because we must set the output
2865 section size first, before its contents are ready. */
2866 for (osec
= ibfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
2868 if (! is_mergeable_note_section (ibfd
, osec
))
2871 bfd_size_type size
= bfd_section_size (osec
);
2875 bfd_nonfatal_message (NULL
, ibfd
, osec
,
2876 _("warning: note section is empty"));
2880 merged_note_section
* merged
= xmalloc (sizeof * merged
);
2881 merged
->contents
= NULL
;
2882 if (! bfd_get_full_section_contents (ibfd
, osec
, & merged
->contents
))
2884 bfd_nonfatal_message (NULL
, ibfd
, osec
,
2885 _("warning: could not load note section"));
2886 free (merged
->contents
);
2891 merged
->size
= merge_gnu_build_notes (ibfd
, osec
, size
,
2893 if (merged
->size
== size
)
2895 /* Merging achieves nothing. */
2896 merge_debug ("Merge of section %s achieved nothing - skipping\n",
2897 bfd_section_name (osec
));
2898 free (merged
->contents
);
2903 if (osec
->output_section
== NULL
2904 || !bfd_set_section_size (osec
->output_section
, merged
->size
))
2906 bfd_nonfatal_message (NULL
, obfd
, osec
,
2907 _("warning: failed to set merged notes size"));
2908 free (merged
->contents
);
2913 /* Add section to list of merged sections. */
2915 merged
->next
= merged_note_sections
;
2916 merged_note_sections
= merged
;
2920 if (dump_sections
!= NULL
)
2922 struct section_add
* pdump
;
2924 for (pdump
= dump_sections
; pdump
!= NULL
; pdump
= pdump
->next
)
2926 osec
= bfd_get_section_by_name (ibfd
, pdump
->name
);
2929 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
2930 _("can't dump section '%s' - it does not exist"),
2935 if ((bfd_section_flags (osec
) & SEC_HAS_CONTENTS
) == 0)
2937 bfd_nonfatal_message (NULL
, ibfd
, osec
,
2938 _("can't dump section - it has no contents"));
2942 bfd_size_type size
= bfd_section_size (osec
);
2945 bfd_nonfatal_message (NULL
, ibfd
, osec
,
2946 _("can't dump section - it is empty"));
2951 f
= fopen (pdump
->filename
, FOPEN_WB
);
2954 bfd_nonfatal_message (pdump
->filename
, NULL
, NULL
,
2955 _("could not open section dump file"));
2960 if (bfd_malloc_and_get_section (ibfd
, osec
, &contents
))
2962 if (fwrite (contents
, 1, size
, f
) != size
)
2964 non_fatal (_("error writing section contents to %s (error: %s)"),
2973 bfd_nonfatal_message (NULL
, ibfd
, osec
,
2974 _("could not retrieve section contents"));
2981 if (gnu_debuglink_filename
!= NULL
)
2983 /* PR 15125: Give a helpful warning message if
2984 the debuglink section already exists, and
2985 allow the rest of the copy to complete. */
2986 if (bfd_get_section_by_name (obfd
, ".gnu_debuglink"))
2988 non_fatal (_("%s: debuglink section already exists"),
2989 bfd_get_filename (obfd
));
2990 gnu_debuglink_filename
= NULL
;
2994 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
2995 (obfd
, gnu_debuglink_filename
);
2997 if (gnu_debuglink_section
== NULL
)
2999 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3000 _("cannot create debug link section `%s'"),
3001 gnu_debuglink_filename
);
3005 /* Special processing for PE format files. We
3006 have no way to distinguish PE from COFF here. */
3007 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
3009 bfd_vma debuglink_vma
;
3010 asection
* highest_section
;
3012 /* The PE spec requires that all sections be adjacent and sorted
3013 in ascending order of VMA. It also specifies that debug
3014 sections should be last. This is despite the fact that debug
3015 sections are not loaded into memory and so in theory have no
3018 This means that the debuglink section must be given a non-zero
3019 VMA which makes it contiguous with other debug sections. So
3020 walk the current section list, find the section with the
3021 highest VMA and start the debuglink section after that one. */
3022 for (osec
= obfd
->sections
, highest_section
= NULL
;
3026 && (highest_section
== NULL
3027 || osec
->vma
> highest_section
->vma
))
3028 highest_section
= osec
;
3030 if (highest_section
)
3031 debuglink_vma
= BFD_ALIGN (highest_section
->vma
3032 + highest_section
->size
,
3033 /* FIXME: We ought to be using
3034 COFF_PAGE_SIZE here or maybe
3035 bfd_section_alignment() (if it
3036 was set) but since this is for PE
3037 and we know the required alignment
3038 it is easier just to hard code it. */
3041 /* Umm, not sure what to do in this case. */
3042 debuglink_vma
= 0x1000;
3044 bfd_set_section_vma (gnu_debuglink_section
, debuglink_vma
);
3049 c
= bfd_count_sections (obfd
);
3051 && (gap_fill_set
|| pad_to_set
))
3055 /* We must fill in gaps between the sections and/or we must pad
3056 the last section to a specified address. We do this by
3057 grabbing a list of the sections, sorting them by VMA, and
3058 increasing the section sizes as required to fill the gaps.
3059 We write out the gap contents below. */
3061 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
3063 bfd_map_over_sections (obfd
, get_sections
, &set
);
3065 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
3067 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
3068 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
3072 for (i
= 0; i
< c
- 1; i
++)
3076 bfd_vma gap_start
, gap_stop
;
3078 flags
= bfd_section_flags (osections
[i
]);
3079 if ((flags
& SEC_HAS_CONTENTS
) == 0
3080 || (flags
& SEC_LOAD
) == 0)
3083 size
= bfd_section_size (osections
[i
]);
3084 gap_start
= bfd_section_lma (osections
[i
]) + size
;
3085 gap_stop
= bfd_section_lma (osections
[i
+ 1]);
3086 if (gap_start
< gap_stop
)
3088 if (!bfd_set_section_size (osections
[i
],
3089 size
+ (gap_stop
- gap_start
)))
3091 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
3092 _("Can't fill gap after section"));
3096 gaps
[i
] = gap_stop
- gap_start
;
3097 if (max_gap
< gap_stop
- gap_start
)
3098 max_gap
= gap_stop
- gap_start
;
3108 lma
= bfd_section_lma (osections
[c
- 1]);
3109 size
= bfd_section_size (osections
[c
- 1]);
3110 if (lma
+ size
< pad_to
)
3112 if (!bfd_set_section_size (osections
[c
- 1], pad_to
- lma
))
3114 bfd_nonfatal_message (NULL
, obfd
, osections
[c
- 1],
3115 _("can't add padding"));
3120 gaps
[c
- 1] = pad_to
- (lma
+ size
);
3121 if (max_gap
< pad_to
- (lma
+ size
))
3122 max_gap
= pad_to
- (lma
+ size
);
3128 /* Symbol filtering must happen after the output sections
3129 have been created, but before their contents are set. */
3131 if (convert_debugging
)
3132 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, FALSE
);
3134 if (strip_symbols
== STRIP_DEBUG
3135 || strip_symbols
== STRIP_ALL
3136 || strip_symbols
== STRIP_UNNEEDED
3137 || strip_symbols
== STRIP_NONDEBUG
3138 || strip_symbols
== STRIP_DWO
3139 || strip_symbols
== STRIP_NONDWO
3140 || discard_locals
!= LOCALS_UNDEF
3142 || htab_elements (strip_specific_htab
) != 0
3143 || htab_elements (keep_specific_htab
) != 0
3144 || htab_elements (localize_specific_htab
) != 0
3145 || htab_elements (globalize_specific_htab
) != 0
3146 || htab_elements (keepglobal_specific_htab
) != 0
3147 || htab_elements (weaken_specific_htab
) != 0
3148 || htab_elements (redefine_specific_htab
) != 0
3149 || prefix_symbols_string
3152 || convert_debugging
3153 || change_leading_char
3154 || remove_leading_char
3155 || section_rename_list
3159 /* Mark symbols used in output relocations so that they
3160 are kept, even if they are local labels or static symbols.
3162 Note we iterate over the input sections examining their
3163 relocations since the relocations for the output sections
3164 haven't been set yet. mark_symbols_used_in_relocations will
3165 ignore input sections which have no corresponding output
3167 if (strip_symbols
!= STRIP_ALL
)
3169 bfd_set_error (bfd_error_no_error
);
3170 bfd_map_over_sections (ibfd
,
3171 mark_symbols_used_in_relocations
,
3173 if (bfd_get_error () != bfd_error_no_error
)
3180 osympp
= (asymbol
**) xmalloc ((symcount
+ add_symbols
+ 1) * sizeof (asymbol
*));
3181 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
3184 if (convert_debugging
&& dhandle
!= NULL
)
3188 res
= write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
);
3191 dhandle
= NULL
; /* Paranoia... */
3200 bfd_set_symtab (obfd
, osympp
, symcount
);
3202 /* This has to happen before section positions are set. */
3203 bfd_map_over_sections (ibfd
, copy_relocations_in_section
, obfd
);
3205 /* This has to happen after the symbol table has been set. */
3206 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
3208 if (add_sections
!= NULL
)
3210 struct section_add
*padd
;
3212 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
3214 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
3217 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3223 if (update_sections
!= NULL
)
3225 struct section_add
*pupdate
;
3227 for (pupdate
= update_sections
;
3229 pupdate
= pupdate
->next
)
3231 osec
= pupdate
->section
->output_section
;
3232 if (! bfd_set_section_contents (obfd
, osec
, pupdate
->contents
,
3235 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
3241 if (merged_note_sections
!= NULL
)
3243 merged_note_section
* merged
= NULL
;
3245 for (osec
= obfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
3247 if (! is_mergeable_note_section (obfd
, osec
))
3251 merged
= merged_note_sections
;
3253 /* It is likely that output sections are in the same order
3254 as the input sections, but do not assume that this is
3256 if (strcmp (bfd_section_name (merged
->sec
),
3257 bfd_section_name (osec
)) != 0)
3259 for (merged
= merged_note_sections
;
3261 merged
= merged
->next
)
3262 if (strcmp (bfd_section_name (merged
->sec
),
3263 bfd_section_name (osec
)) == 0)
3268 bfd_nonfatal_message
3270 _("error: failed to copy merged notes into output"));
3275 if (! is_mergeable_note_section (obfd
, osec
))
3277 bfd_nonfatal_message
3279 _("error: failed to copy merged notes into output"));
3283 if (! bfd_set_section_contents (obfd
, osec
, merged
->contents
, 0,
3286 bfd_nonfatal_message
3288 _("error: failed to copy merged notes into output"));
3292 merged
= merged
->next
;
3295 /* Free the memory. */
3296 merged_note_section
* next
;
3297 for (merged
= merged_note_sections
; merged
!= NULL
; merged
= next
)
3299 next
= merged
->next
;
3300 free (merged
->contents
);
3304 else if (merge_notes
&& ! is_strip
)
3305 non_fatal (_("%s: Could not find any mergeable note sections"),
3306 bfd_get_filename (ibfd
));
3308 if (gnu_debuglink_filename
!= NULL
)
3310 if (! bfd_fill_in_gnu_debuglink_section
3311 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
3313 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3314 _("cannot fill debug link section `%s'"),
3315 gnu_debuglink_filename
);
3320 if (gap_fill_set
|| pad_to_set
)
3324 /* Fill in the gaps. */
3327 buf
= (bfd_byte
*) xmalloc (max_gap
);
3328 memset (buf
, gap_fill
, max_gap
);
3330 c
= bfd_count_sections (obfd
);
3331 for (i
= 0; i
< c
; i
++)
3339 off
= bfd_section_size (osections
[i
]) - left
;
3350 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
3353 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
3369 /* Allow the BFD backend to copy any private data it understands
3370 from the input BFD to the output BFD. This is done last to
3371 permit the routine to look at the filtered symbol table, which is
3372 important for the ECOFF code at least. */
3373 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
3375 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3376 _("error copying private BFD data"));
3380 /* Switch to the alternate machine code. We have to do this at the
3381 very end, because we only initialize the header when we create
3382 the first section. */
3383 if (use_alt_mach_code
!= 0)
3385 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
3387 non_fatal (_("this target does not support %lu alternative machine codes"),
3389 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
3391 non_fatal (_("treating that number as an absolute e_machine value instead"));
3392 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
3395 non_fatal (_("ignoring the alternative value"));
3402 /* Read each archive element in turn from IBFD, copy the
3403 contents to temp file, and keep the temp file handle.
3404 If 'force_output_target' is TRUE then make sure that
3405 all elements in the new archive are of the type
3409 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
3410 bfd_boolean force_output_target
,
3411 const bfd_arch_info_type
*input_arch
)
3415 struct name_list
*next
;
3419 bfd
**ptr
= &obfd
->archive_head
;
3422 const char *filename
;
3424 /* PR 24281: It is not clear what should happen when copying a thin archive.
3425 One part is straight forward - if the output archive is in a different
3426 directory from the input archive then any relative paths in the library
3427 should be adjusted to the new location. But if any transformation
3428 options are active (eg strip, rename, add, etc) then the implication is
3429 that these should be applied to the files pointed to by the archive.
3430 But since objcopy is not destructive, this means that new files must be
3431 created, and there is no guidance for the names of the new files. (Plus
3432 this conflicts with one of the goals of thin libraries - only taking up
3433 a minimal amount of space in the file system).
3435 So for now we fail if an attempt is made to copy such libraries. */
3436 if (ibfd
->is_thin_archive
)
3439 bfd_set_error (bfd_error_invalid_operation
);
3440 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3441 _("sorry: copying thin archives is not currently supported"));
3445 /* Make a temp directory to hold the contents. */
3446 dir
= make_tempdir (bfd_get_filename (obfd
));
3448 fatal (_("cannot create tempdir for archive copying (error: %s)"),
3451 if (strip_symbols
== STRIP_ALL
)
3452 obfd
->has_armap
= FALSE
;
3454 obfd
->has_armap
= ibfd
->has_armap
;
3455 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
3458 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
3462 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
3464 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
3467 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
3468 goto cleanup_and_exit
;
3471 while (!status
&& this_element
!= NULL
)
3477 int stat_status
= 0;
3478 bfd_boolean del
= TRUE
;
3479 bfd_boolean ok_object
;
3481 /* PR binutils/17533: Do not allow directory traversal
3482 outside of the current directory tree by archive members. */
3483 if (! is_valid_archive_path (bfd_get_filename (this_element
)))
3485 non_fatal (_("illegal pathname found in archive member: %s"),
3486 bfd_get_filename (this_element
));
3488 goto cleanup_and_exit
;
3491 /* Create an output file for this member. */
3492 output_name
= concat (dir
, "/",
3493 bfd_get_filename (this_element
), (char *) 0);
3495 /* If the file already exists, make another temp dir. */
3496 if (stat (output_name
, &buf
) >= 0)
3498 char * tmpdir
= make_tempdir (output_name
);
3503 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
3506 goto cleanup_and_exit
;
3509 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
3514 output_name
= concat (tmpdir
, "/",
3515 bfd_get_filename (this_element
), (char *) 0);
3520 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
3522 if (stat_status
!= 0)
3523 non_fatal (_("internal stat error on %s"),
3524 bfd_get_filename (this_element
));
3527 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
3528 l
->name
= output_name
;
3533 ok_object
= bfd_check_format (this_element
, bfd_object
);
3535 bfd_nonfatal_message (NULL
, this_element
, NULL
,
3536 _("Unable to recognise the format of file"));
3538 /* PR binutils/3110: Cope with archives
3539 containing multiple target types. */
3540 if (force_output_target
|| !ok_object
)
3541 output_bfd
= bfd_openw (output_name
, output_target
);
3543 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
3545 if (output_bfd
== NULL
)
3547 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3549 goto cleanup_and_exit
;
3554 del
= !copy_object (this_element
, output_bfd
, input_arch
);
3556 if (del
&& bfd_get_arch (this_element
) == bfd_arch_unknown
)
3557 /* Try again as an unknown object file. */
3559 else if (!bfd_close (output_bfd
))
3561 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3562 /* Error in new object file. Don't change archive. */
3569 del
= !copy_unknown_object (this_element
, output_bfd
);
3570 if (!bfd_close_all_done (output_bfd
))
3572 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3573 /* Error in new object file. Don't change archive. */
3580 unlink (output_name
);
3585 if (preserve_dates
&& stat_status
== 0)
3586 set_times (output_name
, &buf
);
3588 /* Open the newly output file and attach to our list. */
3589 output_bfd
= bfd_openr (output_name
, output_target
);
3591 l
->obfd
= output_bfd
;
3594 ptr
= &output_bfd
->archive_next
;
3596 last_element
= this_element
;
3598 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
3600 bfd_close (last_element
);
3605 filename
= bfd_get_filename (obfd
);
3606 if (!bfd_close (obfd
))
3609 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3612 filename
= bfd_get_filename (ibfd
);
3613 if (!bfd_close (ibfd
))
3616 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3620 /* Delete all the files that we opened. */
3622 struct name_list
* next
;
3624 for (l
= list
; l
!= NULL
; l
= next
)
3626 if (l
->obfd
== NULL
)
3630 bfd_close (l
->obfd
);
3642 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
3644 /* This is only relevant to Coff targets. */
3645 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
3648 && bfd_get_flavour (input_bfd
) == bfd_target_coff_flavour
)
3649 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
3650 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
3654 /* The top-level control. */
3657 copy_file (const char *input_filename
, const char *output_filename
,
3658 const char *input_target
, const char *output_target
,
3659 const bfd_arch_info_type
*input_arch
)
3662 char **obj_matching
;
3663 char **core_matching
;
3664 off_t size
= get_file_size (input_filename
);
3669 non_fatal (_("error: the input file '%s' is empty"),
3675 /* To allow us to do "strip *" without dying on the first
3676 non-object file, failures are nonfatal. */
3677 ibfd
= bfd_openr (input_filename
, input_target
);
3680 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3685 switch (do_debug_sections
)
3689 case compress_gnu_zlib
:
3690 case compress_gabi_zlib
:
3691 ibfd
->flags
|= BFD_COMPRESS
;
3692 /* Don't check if input is ELF here since this information is
3693 only available after bfd_check_format_matches is called. */
3694 if (do_debug_sections
!= compress_gnu_zlib
)
3695 ibfd
->flags
|= BFD_COMPRESS_GABI
;
3698 ibfd
->flags
|= BFD_DECOMPRESS
;
3704 switch (do_elf_stt_common
)
3706 case elf_stt_common
:
3707 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
| BFD_USE_ELF_STT_COMMON
;
3710 case no_elf_stt_common
:
3711 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
;
3717 if (bfd_check_format (ibfd
, bfd_archive
))
3719 bfd_boolean force_output_target
;
3722 /* bfd_get_target does not return the correct value until
3723 bfd_check_format succeeds. */
3724 if (output_target
== NULL
)
3726 output_target
= bfd_get_target (ibfd
);
3727 force_output_target
= FALSE
;
3730 force_output_target
= TRUE
;
3732 obfd
= bfd_openw (output_filename
, output_target
);
3735 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3739 /* This is a no-op on non-Coff targets. */
3740 set_long_section_mode (obfd
, ibfd
, long_section_names
);
3742 copy_archive (ibfd
, obfd
, output_target
, force_output_target
, input_arch
);
3744 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
3749 /* bfd_get_target does not return the correct value until
3750 bfd_check_format succeeds. */
3751 if (output_target
== NULL
)
3752 output_target
= bfd_get_target (ibfd
);
3754 obfd
= bfd_openw (output_filename
, output_target
);
3757 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3761 /* This is a no-op on non-Coff targets. */
3762 set_long_section_mode (obfd
, ibfd
, long_section_names
);
3764 if (! copy_object (ibfd
, obfd
, input_arch
))
3767 /* PR 17512: file: 0f15796a.
3768 If the file could not be copied it may not be in a writeable
3769 state. So use bfd_close_all_done to avoid the possibility of
3770 writing uninitialised data into the file. */
3771 if (! (status
? bfd_close_all_done (obfd
) : bfd_close (obfd
)))
3774 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3778 if (!bfd_close (ibfd
))
3781 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3787 bfd_error_type obj_error
= bfd_get_error ();
3788 bfd_error_type core_error
;
3790 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
3792 /* This probably can't happen.. */
3793 if (obj_error
== bfd_error_file_ambiguously_recognized
)
3794 free (obj_matching
);
3798 core_error
= bfd_get_error ();
3799 /* Report the object error in preference to the core error. */
3800 if (obj_error
!= core_error
)
3801 bfd_set_error (obj_error
);
3803 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3805 if (obj_error
== bfd_error_file_ambiguously_recognized
)
3807 list_matching_formats (obj_matching
);
3808 free (obj_matching
);
3810 if (core_error
== bfd_error_file_ambiguously_recognized
)
3812 list_matching_formats (core_matching
);
3813 free (core_matching
);
3820 /* Add a name to the section renaming list. */
3823 add_section_rename (const char * old_name
, const char * new_name
,
3826 section_rename
* srename
;
3828 /* Check for conflicts first. */
3829 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
3830 if (strcmp (srename
->old_name
, old_name
) == 0)
3832 /* Silently ignore duplicate definitions. */
3833 if (strcmp (srename
->new_name
, new_name
) == 0
3834 && srename
->flags
== flags
)
3837 fatal (_("Multiple renames of section %s"), old_name
);
3840 srename
= (section_rename
*) xmalloc (sizeof (* srename
));
3842 srename
->old_name
= old_name
;
3843 srename
->new_name
= new_name
;
3844 srename
->flags
= flags
;
3845 srename
->next
= section_rename_list
;
3847 section_rename_list
= srename
;
3850 /* Check the section rename list for a new name of the input section
3851 called OLD_NAME. Returns the new name if one is found and sets
3852 RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
3855 find_section_rename (const char *old_name
, flagword
*returned_flags
)
3857 const section_rename
*srename
;
3859 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
3860 if (strcmp (srename
->old_name
, old_name
) == 0)
3862 if (returned_flags
!= NULL
&& srename
->flags
!= (flagword
) -1)
3863 *returned_flags
= srename
->flags
;
3865 return srename
->new_name
;
3871 /* Once each of the sections is copied, we may still need to do some
3872 finalization work for private section headers. Do that here. */
3875 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
3877 /* Allow the BFD backend to copy any private data it understands
3878 from the input section to the output section. */
3879 if (! bfd_copy_private_header_data (ibfd
, obfd
))
3882 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3883 _("error in private header data"));
3887 /* All went well. */
3891 /* Create a section in OBFD with the same
3892 name and attributes as ISECTION in IBFD. */
3895 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
3897 bfd
*obfd
= (bfd
*) obfdarg
;
3898 struct section_list
*p
;
3906 char *prefix
= NULL
;
3907 bfd_boolean make_nobits
;
3908 unsigned int alignment
;
3910 if (is_strip_section (ibfd
, isection
))
3913 /* Get the, possibly new, name of the output section. */
3914 name
= bfd_section_name (isection
);
3915 flags
= bfd_section_flags (isection
);
3916 name
= find_section_rename (name
, &flags
);
3918 /* Prefix sections. */
3919 if ((prefix_alloc_sections_string
)
3920 && (bfd_section_flags (isection
) & SEC_ALLOC
))
3921 prefix
= prefix_alloc_sections_string
;
3922 else if (prefix_sections_string
)
3923 prefix
= prefix_sections_string
;
3929 n
= (char *) xmalloc (strlen (prefix
) + strlen (name
) + 1);
3935 make_nobits
= FALSE
;
3937 p
= find_section_list (bfd_section_name (isection
), FALSE
,
3938 SECTION_CONTEXT_SET_FLAGS
);
3940 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
3941 else if (strip_symbols
== STRIP_NONDEBUG
3942 && (flags
& (SEC_ALLOC
| SEC_GROUP
)) != 0
3943 && !is_nondebug_keep_contents_section (ibfd
, isection
))
3945 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
3946 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
3950 /* Twiddle the input section flags so that it seems to
3951 elf.c:copy_private_bfd_data that section flags have not
3952 changed between input and output sections. This hack
3953 prevents wholesale rewriting of the program headers. */
3954 isection
->flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
);
3958 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
3960 if (osection
== NULL
)
3962 err
= _("failed to create output section");
3967 elf_section_type (osection
) = SHT_NOBITS
;
3969 size
= bfd_section_size (isection
);
3970 size
= bfd_convert_section_size (ibfd
, isection
, obfd
, size
);
3972 size
= (size
+ interleave
- 1) / interleave
* copy_width
;
3973 else if (extract_symbol
)
3975 if (!bfd_set_section_size (osection
, size
))
3977 err
= _("failed to set size");
3981 vma
= bfd_section_vma (isection
);
3982 p
= find_section_list (bfd_section_name (isection
), FALSE
,
3983 SECTION_CONTEXT_ALTER_VMA
| SECTION_CONTEXT_SET_VMA
);
3986 if (p
->context
& SECTION_CONTEXT_SET_VMA
)
3992 vma
+= change_section_address
;
3994 if (!bfd_set_section_vma (osection
, vma
))
3996 err
= _("failed to set vma");
4000 lma
= isection
->lma
;
4001 p
= find_section_list (bfd_section_name (isection
), FALSE
,
4002 SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_SET_LMA
);
4005 if (p
->context
& SECTION_CONTEXT_ALTER_LMA
)
4011 lma
+= change_section_address
;
4013 osection
->lma
= lma
;
4015 p
= find_section_list (bfd_section_name (isection
), FALSE
,
4016 SECTION_CONTEXT_SET_ALIGNMENT
);
4018 alignment
= p
->alignment
;
4020 alignment
= bfd_section_alignment (isection
);
4022 /* FIXME: This is probably not enough. If we change the LMA we
4023 may have to recompute the header for the file as well. */
4024 if (!bfd_set_section_alignment (osection
, alignment
))
4026 err
= _("failed to set alignment");
4030 /* Copy merge entity size. */
4031 osection
->entsize
= isection
->entsize
;
4033 /* Copy compress status. */
4034 osection
->compress_status
= isection
->compress_status
;
4036 /* This used to be mangle_section; we do here to avoid using
4037 bfd_get_section_by_name since some formats allow multiple
4038 sections with the same name. */
4039 isection
->output_section
= osection
;
4040 isection
->output_offset
= 0;
4042 if ((isection
->flags
& SEC_GROUP
) != 0)
4044 asymbol
*gsym
= group_signature (isection
);
4048 gsym
->flags
|= BSF_KEEP
;
4049 if (ibfd
->xvec
->flavour
== bfd_target_elf_flavour
)
4050 elf_group_id (isection
) = gsym
;
4054 /* Allow the BFD backend to copy any private data it understands
4055 from the input section to the output section. */
4056 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
4058 err
= _("failed to copy private data");
4062 /* All went well. */
4067 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
4070 /* Return TRUE if input section ISECTION should be skipped. */
4073 skip_section (bfd
*ibfd
, sec_ptr isection
, bfd_boolean skip_copy
)
4079 /* If we have already failed earlier on,
4080 do not keep on generating complaints now. */
4087 if (is_strip_section (ibfd
, isection
))
4090 if (is_update_section (ibfd
, isection
))
4093 /* When merging a note section we skip the copying of the contents,
4094 but not the copying of the relocs associated with the contents. */
4095 if (skip_copy
&& is_mergeable_note_section (ibfd
, isection
))
4098 flags
= bfd_section_flags (isection
);
4099 if ((flags
& SEC_GROUP
) != 0)
4102 osection
= isection
->output_section
;
4103 size
= bfd_section_size (isection
);
4105 if (size
== 0 || osection
== 0)
4111 /* Add section SECTION_PATTERN to the list of sections that will have their
4112 relocations removed. */
4115 handle_remove_relocations_option (const char *section_pattern
)
4117 find_section_list (section_pattern
, TRUE
, SECTION_CONTEXT_REMOVE_RELOCS
);
4120 /* Return TRUE if ISECTION from IBFD should have its relocations removed,
4121 otherwise return FALSE. If the user has requested that relocations be
4122 removed from a section that does not have relocations then this
4123 function will still return TRUE. */
4126 discard_relocations (bfd
*ibfd ATTRIBUTE_UNUSED
, asection
*isection
)
4128 return (find_section_list (bfd_section_name (isection
), FALSE
,
4129 SECTION_CONTEXT_REMOVE_RELOCS
) != NULL
);
4132 /* Wrapper for dealing with --remove-section (-R) command line arguments.
4133 A special case is detected here, if the user asks to remove a relocation
4134 section (one starting with ".rela" or ".rel") then this removal must
4135 be done using a different technique in a relocatable object. */
4138 handle_remove_section_option (const char *section_pattern
)
4140 find_section_list (section_pattern
, TRUE
, SECTION_CONTEXT_REMOVE
);
4141 if (strncmp (section_pattern
, ".rel", 4) == 0)
4143 section_pattern
+= 4;
4144 if (*section_pattern
== 'a')
4146 if (*section_pattern
)
4147 handle_remove_relocations_option (section_pattern
);
4149 sections_removed
= TRUE
;
4152 /* Copy relocations in input section ISECTION of IBFD to an output
4153 section with the same name in OBFDARG. If stripping then don't
4154 copy any relocation info. */
4157 copy_relocations_in_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4159 bfd
*obfd
= (bfd
*) obfdarg
;
4165 if (skip_section (ibfd
, isection
, FALSE
))
4168 osection
= isection
->output_section
;
4170 /* Core files and DWO files do not need to be relocated. */
4171 if (bfd_get_format (obfd
) == bfd_core
4172 || strip_symbols
== STRIP_NONDWO
4173 || discard_relocations (ibfd
, isection
))
4177 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4181 /* Do not complain if the target does not support relocations. */
4182 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4187 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4195 bfd_set_reloc (obfd
, osection
, NULL
, 0);
4196 osection
->flags
&= ~SEC_RELOC
;
4200 if (isection
->orelocation
!= NULL
)
4202 /* Some other function has already set up the output relocs
4203 for us, so scan those instead of the default relocs. */
4204 relcount
= isection
->reloc_count
;
4205 relpp
= isection
->orelocation
;
4209 relpp
= (arelent
**) xmalloc (relsize
);
4210 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
4214 bfd_nonfatal_message (NULL
, ibfd
, isection
,
4215 _("relocation count is negative"));
4220 if (strip_symbols
== STRIP_ALL
)
4222 /* Remove relocations which are not in
4223 keep_strip_specific_list. */
4224 arelent
**temp_relpp
;
4225 long temp_relcount
= 0;
4228 temp_relpp
= (arelent
**) xmalloc (relsize
);
4229 for (i
= 0; i
< relcount
; i
++)
4231 /* PR 17512: file: 9e907e0c. */
4232 if (relpp
[i
]->sym_ptr_ptr
4234 && * relpp
[i
]->sym_ptr_ptr
)
4235 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
4236 keep_specific_htab
))
4237 temp_relpp
[temp_relcount
++] = relpp
[i
];
4239 relcount
= temp_relcount
;
4240 if (isection
->orelocation
== NULL
)
4245 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
4248 osection
->flags
&= ~SEC_RELOC
;
4254 /* Copy the data of input section ISECTION of IBFD
4255 to an output section with the same name in OBFD. */
4258 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4260 bfd
*obfd
= (bfd
*) obfdarg
;
4261 struct section_list
*p
;
4265 if (skip_section (ibfd
, isection
, TRUE
))
4268 osection
= isection
->output_section
;
4269 /* The output SHF_COMPRESSED section size is different from input if
4270 ELF classes of input and output aren't the same. We can't use
4271 the output section size since --interleave will shrink the output
4272 section. Size will be updated if the section is converted. */
4273 size
= bfd_section_size (isection
);
4275 if (bfd_section_flags (isection
) & SEC_HAS_CONTENTS
4276 && bfd_section_flags (osection
) & SEC_HAS_CONTENTS
)
4278 bfd_byte
*memhunk
= NULL
;
4280 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
)
4281 || !bfd_convert_section_contents (ibfd
, isection
, obfd
,
4285 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4292 /* We don't handle leftover bytes (too many possible behaviors,
4293 and we don't know what the user wants). The section length
4294 must be a multiple of the number of bytes to swap. */
4295 if ((size
% reverse_bytes
) == 0)
4300 for (i
= 0; i
< size
; i
+= reverse_bytes
)
4301 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
4303 bfd_byte
*m
= (bfd_byte
*) memhunk
;
4306 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
4307 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
4311 /* User must pad the section up in order to do this. */
4312 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
4313 bfd_section_name (isection
), reverse_bytes
);
4318 /* Keep only every `copy_byte'th byte in MEMHUNK. */
4319 char *from
= (char *) memhunk
+ copy_byte
;
4320 char *to
= (char *) memhunk
;
4321 char *end
= (char *) memhunk
+ size
;
4324 /* If the section address is not exactly divisible by the interleave,
4325 then we must bias the from address. If the copy_byte is less than
4326 the bias, then we must skip forward one interleave, and increment
4328 int extra
= isection
->lma
% interleave
;
4330 if (copy_byte
< extra
)
4333 for (; from
< end
; from
+= interleave
)
4334 for (i
= 0; i
< copy_width
; i
++)
4336 if (&from
[i
] >= end
)
4341 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
* copy_width
;
4342 osection
->lma
/= interleave
;
4343 if (copy_byte
< extra
)
4347 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4350 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4356 else if ((p
= find_section_list (bfd_section_name (isection
),
4357 FALSE
, SECTION_CONTEXT_SET_FLAGS
)) != NULL
4358 && (p
->flags
& SEC_HAS_CONTENTS
) != 0)
4360 void *memhunk
= xmalloc (size
);
4362 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
4363 flag--they can just remove the section entirely and add it
4364 back again. However, we do permit them to turn on the
4365 SEC_HAS_CONTENTS flag, and take it to mean that the section
4366 contents should be zeroed out. */
4368 memset (memhunk
, 0, size
);
4369 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4372 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4380 /* Get all the sections. This is used when --gap-fill or --pad-to is
4384 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
4386 asection
***secppp
= (asection
***) secppparg
;
4388 **secppp
= osection
;
4392 /* Sort sections by LMA. This is called via qsort, and is used when
4393 --gap-fill or --pad-to is used. We force non loadable or empty
4394 sections to the front, where they are easier to ignore. */
4397 compare_section_lma (const void *arg1
, const void *arg2
)
4399 const asection
*sec1
= *(const asection
**) arg1
;
4400 const asection
*sec2
= *(const asection
**) arg2
;
4401 flagword flags1
, flags2
;
4403 /* Sort non loadable sections to the front. */
4404 flags1
= sec1
->flags
;
4405 flags2
= sec2
->flags
;
4406 if ((flags1
& SEC_HAS_CONTENTS
) == 0
4407 || (flags1
& SEC_LOAD
) == 0)
4409 if ((flags2
& SEC_HAS_CONTENTS
) != 0
4410 && (flags2
& SEC_LOAD
) != 0)
4415 if ((flags2
& SEC_HAS_CONTENTS
) == 0
4416 || (flags2
& SEC_LOAD
) == 0)
4420 /* Sort sections by LMA. */
4421 if (sec1
->lma
> sec2
->lma
)
4423 if (sec1
->lma
< sec2
->lma
)
4426 /* Sort sections with the same LMA by size. */
4427 if (bfd_section_size (sec1
) > bfd_section_size (sec2
))
4429 if (bfd_section_size (sec1
) < bfd_section_size (sec2
))
4432 if (sec1
->id
> sec2
->id
)
4434 if (sec1
->id
< sec2
->id
)
4439 /* Mark all the symbols which will be used in output relocations with
4440 the BSF_KEEP flag so that those symbols will not be stripped.
4442 Ignore relocations which will not appear in the output file. */
4445 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
4447 asymbol
**symbols
= (asymbol
**) symbolsarg
;
4452 /* Ignore an input section with no corresponding output section. */
4453 if (isection
->output_section
== NULL
)
4456 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4459 /* Do not complain if the target does not support relocations. */
4460 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4462 bfd_fatal (bfd_get_filename (ibfd
));
4468 relpp
= (arelent
**) xmalloc (relsize
);
4469 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
4471 bfd_fatal (bfd_get_filename (ibfd
));
4473 /* Examine each symbol used in a relocation. If it's not one of the
4474 special bfd section symbols, then mark it with BSF_KEEP. */
4475 for (i
= 0; i
< relcount
; i
++)
4477 /* See PRs 20923 and 20930 for reproducers for the NULL tests. */
4478 if (relpp
[i
]->sym_ptr_ptr
!= NULL
4479 && * relpp
[i
]->sym_ptr_ptr
!= NULL
4480 && *relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
4481 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
4482 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
4483 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
4490 /* Write out debugging information. */
4493 write_debugging_info (bfd
*obfd
, void *dhandle
,
4494 long *symcountp ATTRIBUTE_UNUSED
,
4495 asymbol
***symppp ATTRIBUTE_UNUSED
)
4497 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
4498 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
4500 bfd_byte
*syms
, *strings
= NULL
;
4501 bfd_size_type symsize
, stringsize
;
4502 asection
*stabsec
, *stabstrsec
;
4505 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
4510 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
4511 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
4512 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
4514 || stabstrsec
== NULL
4515 || !bfd_set_section_size (stabsec
, symsize
)
4516 || !bfd_set_section_size (stabstrsec
, stringsize
)
4517 || !bfd_set_section_alignment (stabsec
, 2)
4518 || !bfd_set_section_alignment (stabstrsec
, 0))
4520 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4521 _("can't create debugging section"));
4526 /* We can get away with setting the section contents now because
4527 the next thing the caller is going to do is copy over the
4528 real sections. We may someday have to split the contents
4529 setting out of this function. */
4530 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
4531 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
4534 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4535 _("can't set debugging section contents"));
4543 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4544 _("don't know how to write debugging information for %s"),
4545 bfd_get_target (obfd
));
4549 /* If neither -D nor -U was specified explicitly,
4550 then use the configured default. */
4552 default_deterministic (void)
4554 if (deterministic
< 0)
4555 deterministic
= DEFAULT_AR_DETERMINISTIC
;
4559 strip_main (int argc
, char *argv
[])
4561 char *input_target
= NULL
;
4562 char *output_target
= NULL
;
4563 bfd_boolean show_version
= FALSE
;
4564 bfd_boolean formats_info
= FALSE
;
4567 char *output_file
= NULL
;
4568 bfd_boolean merge_notes_set
= FALSE
;
4570 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU",
4571 strip_options
, (int *) 0)) != EOF
)
4576 input_target
= optarg
;
4579 output_target
= optarg
;
4582 input_target
= output_target
= optarg
;
4585 handle_remove_section_option (optarg
);
4587 case OPTION_REMOVE_RELOCS
:
4588 handle_remove_relocations_option (optarg
);
4591 strip_symbols
= STRIP_ALL
;
4595 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
4596 strip_symbols
= STRIP_DEBUG
;
4598 case OPTION_STRIP_DWO
:
4599 strip_symbols
= STRIP_DWO
;
4601 case OPTION_STRIP_UNNEEDED
:
4602 strip_symbols
= STRIP_UNNEEDED
;
4605 add_specific_symbol (optarg
, keep_specific_htab
);
4609 merge_notes_set
= TRUE
;
4611 case OPTION_NO_MERGE_NOTES
:
4612 merge_notes
= FALSE
;
4613 merge_notes_set
= TRUE
;
4616 add_specific_symbol (optarg
, strip_specific_htab
);
4619 output_file
= optarg
;
4622 preserve_dates
= TRUE
;
4625 deterministic
= TRUE
;
4628 deterministic
= FALSE
;
4631 discard_locals
= LOCALS_ALL
;
4634 discard_locals
= LOCALS_START_L
;
4640 show_version
= TRUE
;
4642 case OPTION_FORMATS_INFO
:
4643 formats_info
= TRUE
;
4645 case OPTION_ONLY_KEEP_DEBUG
:
4646 strip_symbols
= STRIP_NONDEBUG
;
4648 case OPTION_KEEP_FILE_SYMBOLS
:
4649 keep_file_symbols
= 1;
4652 /* We've been given a long option. */
4659 strip_usage (stdout
, 0);
4661 strip_usage (stderr
, 1);
4665 /* If the user has not expressly chosen to merge/not-merge ELF notes
4666 then enable the merging unless we are stripping debug or dwo info. */
4667 if (! merge_notes_set
4668 && (strip_symbols
== STRIP_UNDEF
4669 || strip_symbols
== STRIP_ALL
4670 || strip_symbols
== STRIP_UNNEEDED
4671 || strip_symbols
== STRIP_NONDEBUG
4672 || strip_symbols
== STRIP_NONDWO
))
4682 print_version ("strip");
4684 default_deterministic ();
4686 /* Default is to strip all symbols. */
4687 if (strip_symbols
== STRIP_UNDEF
4688 && discard_locals
== LOCALS_UNDEF
4689 && htab_elements (strip_specific_htab
) == 0)
4690 strip_symbols
= STRIP_ALL
;
4692 if (output_target
== NULL
)
4693 output_target
= input_target
;
4697 || (output_file
!= NULL
&& (i
+ 1) < argc
))
4698 strip_usage (stderr
, 1);
4700 for (; i
< argc
; i
++)
4702 int hold_status
= status
;
4703 struct stat statbuf
;
4706 if (get_file_size (argv
[i
]) < 1)
4713 /* No need to check the return value of stat().
4714 It has already been checked in get_file_size(). */
4715 stat (argv
[i
], &statbuf
);
4717 if (output_file
== NULL
4718 || filename_cmp (argv
[i
], output_file
) == 0)
4719 tmpname
= make_tempname (argv
[i
]);
4721 tmpname
= output_file
;
4723 if (tmpname
== NULL
)
4725 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
4726 _("could not create temporary file to hold stripped copy"));
4732 copy_file (argv
[i
], tmpname
, input_target
, output_target
, NULL
);
4736 set_times (tmpname
, &statbuf
);
4737 if (output_file
!= tmpname
)
4738 status
= (smart_rename (tmpname
,
4739 output_file
? output_file
: argv
[i
],
4740 preserve_dates
) != 0);
4742 status
= hold_status
;
4745 unlink_if_ordinary (tmpname
);
4746 if (output_file
!= tmpname
)
4753 /* Set up PE subsystem. */
4756 set_pe_subsystem (const char *s
)
4758 const char *version
, *subsystem
;
4768 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
4769 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
4770 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
4771 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
4772 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
4773 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
4774 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
4775 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
4776 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
},
4777 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
4783 /* Check for the presence of a version number. */
4784 version
= strchr (s
, ':');
4785 if (version
== NULL
)
4789 int len
= version
- s
;
4793 version
= copy
+ 1 + len
;
4794 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
4796 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
4798 non_fatal (_("%s: bad version in PE subsystem"), s
);
4801 /* Check for numeric subsystem. */
4802 value
= (short) strtol (subsystem
, ©
, 0);
4805 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
4806 if (v
[i
].value
== value
)
4808 pe_subsystem
= value
;
4809 set_def
= v
[i
].set_def
;
4815 /* Search for subsystem by name. */
4816 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
4817 if (strcmp (subsystem
, v
[i
].name
) == 0)
4819 pe_subsystem
= v
[i
].value
;
4820 set_def
= v
[i
].set_def
;
4828 fatal (_("unknown PE subsystem: %s"), s
);
4833 if (pe_file_alignment
== (bfd_vma
) -1)
4834 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
4835 if (pe_section_alignment
== (bfd_vma
) -1)
4836 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
4840 free ((char *) subsystem
);
4843 /* Convert EFI target to PEI target. */
4846 convert_efi_target (char *efi
)
4852 if (strcmp (efi
+ 4, "ia32") == 0)
4854 /* Change ia32 to i386. */
4859 else if (strcmp (efi
+ 4, "x86_64") == 0)
4861 /* Change x86_64 to x86-64. */
4866 /* Allocate and return a pointer to a struct section_add, initializing the
4867 structure using ARG, a string in the format "sectionname=filename".
4868 The returned structure will have its next pointer set to NEXT. The
4869 OPTION field is the name of the command line option currently being
4870 parsed, and is only used if an error needs to be reported. */
4872 static struct section_add
*
4873 init_section_add (const char *arg
,
4874 struct section_add
*next
,
4877 struct section_add
*pa
;
4880 s
= strchr (arg
, '=');
4882 fatal (_("bad format for %s"), option
);
4884 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
4885 pa
->name
= xstrndup (arg
, s
- arg
);
4886 pa
->filename
= s
+ 1;
4888 pa
->contents
= NULL
;
4894 /* Load the file specified in PA, allocating memory to hold the file
4895 contents, and store a pointer to the allocated memory in the contents
4896 field of PA. The size field of PA is also updated. All errors call
4900 section_add_load_file (struct section_add
*pa
)
4905 /* We don't use get_file_size so that we can do
4906 --add-section .note.GNU_stack=/dev/null
4907 get_file_size doesn't work on /dev/null. */
4909 f
= fopen (pa
->filename
, FOPEN_RB
);
4911 fatal (_("cannot open: %s: %s"),
4912 pa
->filename
, strerror (errno
));
4916 pa
->contents
= (bfd_byte
*) xmalloc (alloc
);
4924 pa
->contents
= (bfd_byte
*) xrealloc (pa
->contents
, alloc
);
4927 got
= fread (pa
->contents
+ off
, 1, alloc
- off
, f
);
4929 fatal (_("%s: fread failed"), pa
->filename
);
4940 copy_main (int argc
, char *argv
[])
4942 char *input_filename
= NULL
;
4943 char *output_filename
= NULL
;
4945 char *input_target
= NULL
;
4946 char *output_target
= NULL
;
4947 bfd_boolean show_version
= FALSE
;
4948 bfd_boolean change_warn
= TRUE
;
4949 bfd_boolean formats_info
= FALSE
;
4950 bfd_boolean use_globalize
= FALSE
;
4951 bfd_boolean use_keep_global
= FALSE
;
4953 struct stat statbuf
;
4954 const bfd_arch_info_type
*input_arch
= NULL
;
4956 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
4957 copy_options
, (int *) 0)) != EOF
)
4962 copy_byte
= atoi (optarg
);
4964 fatal (_("byte number must be non-negative"));
4968 input_arch
= bfd_scan_arch (optarg
);
4969 if (input_arch
== NULL
)
4970 fatal (_("architecture %s unknown"), optarg
);
4976 interleave
= atoi (optarg
);
4978 fatal (_("interleave must be positive"));
4984 case OPTION_INTERLEAVE_WIDTH
:
4985 copy_width
= atoi (optarg
);
4987 fatal(_("interleave width must be positive"));
4991 case 's': /* "source" - 'I' is preferred */
4992 input_target
= optarg
;
4996 case 'd': /* "destination" - 'O' is preferred */
4997 output_target
= optarg
;
5001 input_target
= output_target
= optarg
;
5005 find_section_list (optarg
, TRUE
, SECTION_CONTEXT_COPY
);
5006 sections_copied
= TRUE
;
5010 handle_remove_section_option (optarg
);
5013 case OPTION_REMOVE_RELOCS
:
5014 handle_remove_relocations_option (optarg
);
5018 strip_symbols
= STRIP_ALL
;
5022 strip_symbols
= STRIP_DEBUG
;
5025 case OPTION_STRIP_DWO
:
5026 strip_symbols
= STRIP_DWO
;
5029 case OPTION_STRIP_UNNEEDED
:
5030 strip_symbols
= STRIP_UNNEEDED
;
5033 case OPTION_ONLY_KEEP_DEBUG
:
5034 strip_symbols
= STRIP_NONDEBUG
;
5037 case OPTION_KEEP_FILE_SYMBOLS
:
5038 keep_file_symbols
= 1;
5041 case OPTION_ADD_GNU_DEBUGLINK
:
5042 long_section_names
= ENABLE
;
5043 gnu_debuglink_filename
= optarg
;
5047 add_specific_symbol (optarg
, keep_specific_htab
);
5053 case OPTION_NO_MERGE_NOTES
:
5054 merge_notes
= FALSE
;
5058 add_specific_symbol (optarg
, strip_specific_htab
);
5061 case OPTION_STRIP_UNNEEDED_SYMBOL
:
5062 add_specific_symbol (optarg
, strip_unneeded_htab
);
5066 add_specific_symbol (optarg
, localize_specific_htab
);
5069 case OPTION_GLOBALIZE_SYMBOL
:
5070 use_globalize
= TRUE
;
5071 add_specific_symbol (optarg
, globalize_specific_htab
);
5075 use_keep_global
= TRUE
;
5076 add_specific_symbol (optarg
, keepglobal_specific_htab
);
5080 add_specific_symbol (optarg
, weaken_specific_htab
);
5084 preserve_dates
= TRUE
;
5088 deterministic
= TRUE
;
5092 deterministic
= FALSE
;
5100 discard_locals
= LOCALS_ALL
;
5104 discard_locals
= LOCALS_START_L
;
5112 show_version
= TRUE
;
5115 case OPTION_FORMATS_INFO
:
5116 formats_info
= TRUE
;
5123 case OPTION_ADD_SECTION
:
5124 add_sections
= init_section_add (optarg
, add_sections
,
5126 section_add_load_file (add_sections
);
5129 case OPTION_UPDATE_SECTION
:
5130 update_sections
= init_section_add (optarg
, update_sections
,
5131 "--update-section");
5132 section_add_load_file (update_sections
);
5135 case OPTION_DUMP_SECTION
:
5136 dump_sections
= init_section_add (optarg
, dump_sections
,
5140 case OPTION_ADD_SYMBOL
:
5143 struct addsym_node
*newsym
= xmalloc (sizeof *newsym
);
5145 newsym
->next
= NULL
;
5146 s
= strchr (optarg
, '=');
5148 fatal (_("bad format for %s"), "--add-symbol");
5149 t
= strchr (s
+ 1, ':');
5151 newsym
->symdef
= xstrndup (optarg
, s
- optarg
);
5154 newsym
->section
= xstrndup (s
+ 1, t
- (s
+ 1));
5155 newsym
->symval
= strtol (t
+ 1, NULL
, 0);
5159 newsym
->section
= NULL
;
5160 newsym
->symval
= strtol (s
+ 1, NULL
, 0);
5164 t
= strchr (t
+ 1, ',');
5165 newsym
->othersym
= NULL
;
5167 newsym
->flags
= parse_symflags (t
+1, &newsym
->othersym
);
5169 newsym
->flags
= BSF_GLOBAL
;
5171 /* Keep 'othersym' symbols at the front of the list. */
5172 if (newsym
->othersym
)
5174 newsym
->next
= add_sym_list
;
5176 add_sym_tail
= &newsym
->next
;
5177 add_sym_list
= newsym
;
5181 *add_sym_tail
= newsym
;
5182 add_sym_tail
= &newsym
->next
;
5188 case OPTION_CHANGE_START
:
5189 change_start
= parse_vma (optarg
, "--change-start");
5192 case OPTION_CHANGE_SECTION_ADDRESS
:
5193 case OPTION_CHANGE_SECTION_LMA
:
5194 case OPTION_CHANGE_SECTION_VMA
:
5196 struct section_list
* p
;
5197 unsigned int context
= 0;
5201 char *option
= NULL
;
5206 case OPTION_CHANGE_SECTION_ADDRESS
:
5207 option
= "--change-section-address";
5208 context
= SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_ALTER_VMA
;
5210 case OPTION_CHANGE_SECTION_LMA
:
5211 option
= "--change-section-lma";
5212 context
= SECTION_CONTEXT_ALTER_LMA
;
5214 case OPTION_CHANGE_SECTION_VMA
:
5215 option
= "--change-section-vma";
5216 context
= SECTION_CONTEXT_ALTER_VMA
;
5220 s
= strchr (optarg
, '=');
5223 s
= strchr (optarg
, '+');
5226 s
= strchr (optarg
, '-');
5228 fatal (_("bad format for %s"), option
);
5233 /* Correct the context. */
5236 case OPTION_CHANGE_SECTION_ADDRESS
:
5237 context
= SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_SET_VMA
;
5239 case OPTION_CHANGE_SECTION_LMA
:
5240 context
= SECTION_CONTEXT_SET_LMA
;
5242 case OPTION_CHANGE_SECTION_VMA
:
5243 context
= SECTION_CONTEXT_SET_VMA
;
5249 name
= (char *) xmalloc (len
+ 1);
5250 strncpy (name
, optarg
, len
);
5253 p
= find_section_list (name
, TRUE
, context
);
5255 val
= parse_vma (s
+ 1, option
);
5261 case OPTION_CHANGE_SECTION_ADDRESS
:
5265 case OPTION_CHANGE_SECTION_LMA
:
5269 case OPTION_CHANGE_SECTION_VMA
:
5276 case OPTION_CHANGE_ADDRESSES
:
5277 change_section_address
= parse_vma (optarg
, "--change-addresses");
5278 change_start
= change_section_address
;
5281 case OPTION_CHANGE_WARNINGS
:
5285 case OPTION_CHANGE_LEADING_CHAR
:
5286 change_leading_char
= TRUE
;
5289 case OPTION_COMPRESS_DEBUG_SECTIONS
:
5292 if (strcasecmp (optarg
, "none") == 0)
5293 do_debug_sections
= decompress
;
5294 else if (strcasecmp (optarg
, "zlib") == 0)
5295 do_debug_sections
= compress_zlib
;
5296 else if (strcasecmp (optarg
, "zlib-gnu") == 0)
5297 do_debug_sections
= compress_gnu_zlib
;
5298 else if (strcasecmp (optarg
, "zlib-gabi") == 0)
5299 do_debug_sections
= compress_gabi_zlib
;
5301 fatal (_("unrecognized --compress-debug-sections type `%s'"),
5305 do_debug_sections
= compress
;
5308 case OPTION_DEBUGGING
:
5309 convert_debugging
= TRUE
;
5312 case OPTION_DECOMPRESS_DEBUG_SECTIONS
:
5313 do_debug_sections
= decompress
;
5316 case OPTION_ELF_STT_COMMON
:
5317 if (strcasecmp (optarg
, "yes") == 0)
5318 do_elf_stt_common
= elf_stt_common
;
5319 else if (strcasecmp (optarg
, "no") == 0)
5320 do_elf_stt_common
= no_elf_stt_common
;
5322 fatal (_("unrecognized --elf-stt-common= option `%s'"),
5326 case OPTION_GAP_FILL
:
5328 bfd_vma gap_fill_vma
;
5330 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
5331 gap_fill
= (bfd_byte
) gap_fill_vma
;
5332 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
5336 sprintf_vma (buff
, gap_fill_vma
);
5338 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
5341 gap_fill_set
= TRUE
;
5345 case OPTION_NO_CHANGE_WARNINGS
:
5346 change_warn
= FALSE
;
5350 pad_to
= parse_vma (optarg
, "--pad-to");
5354 case OPTION_REMOVE_LEADING_CHAR
:
5355 remove_leading_char
= TRUE
;
5358 case OPTION_REDEFINE_SYM
:
5360 /* Insert this redefinition onto redefine_specific_htab. */
5364 const char *nextarg
;
5365 char *source
, *target
;
5367 s
= strchr (optarg
, '=');
5369 fatal (_("bad format for %s"), "--redefine-sym");
5372 source
= (char *) xmalloc (len
+ 1);
5373 strncpy (source
, optarg
, len
);
5377 len
= strlen (nextarg
);
5378 target
= (char *) xmalloc (len
+ 1);
5379 strcpy (target
, nextarg
);
5381 add_redefine_and_check ("--redefine-sym", source
, target
);
5388 case OPTION_REDEFINE_SYMS
:
5389 add_redefine_syms_file (optarg
);
5392 case OPTION_SET_SECTION_FLAGS
:
5394 struct section_list
*p
;
5399 s
= strchr (optarg
, '=');
5401 fatal (_("bad format for %s"), "--set-section-flags");
5404 name
= (char *) xmalloc (len
+ 1);
5405 strncpy (name
, optarg
, len
);
5408 p
= find_section_list (name
, TRUE
, SECTION_CONTEXT_SET_FLAGS
);
5410 p
->flags
= parse_flags (s
+ 1);
5414 case OPTION_SET_SECTION_ALIGNMENT
:
5416 struct section_list
*p
;
5422 s
= strchr (optarg
, '=');
5424 fatal (_("bad format for --set-section-alignment: argument needed"));
5426 align
= atoi (s
+ 1);
5428 fatal (_("bad format for --set-section-alignment: numeric argument needed"));
5430 /* Convert integer alignment into a power-of-two alignment. */
5432 while ((align
& 1) == 0)
5439 /* Number has more than on 1, i.e. wasn't a power of 2. */
5440 fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
5442 /* Add the alignment setting to the section list. */
5444 name
= (char *) xmalloc (len
+ 1);
5445 strncpy (name
, optarg
, len
);
5448 p
= find_section_list (name
, TRUE
, SECTION_CONTEXT_SET_ALIGNMENT
);
5450 p
->alignment
= palign
;
5454 case OPTION_RENAME_SECTION
:
5457 const char *eq
, *fl
;
5462 eq
= strchr (optarg
, '=');
5464 fatal (_("bad format for %s"), "--rename-section");
5468 fatal (_("bad format for %s"), "--rename-section");
5470 old_name
= (char *) xmalloc (len
+ 1);
5471 strncpy (old_name
, optarg
, len
);
5475 fl
= strchr (eq
, ',');
5478 flags
= parse_flags (fl
+ 1);
5488 fatal (_("bad format for %s"), "--rename-section");
5490 new_name
= (char *) xmalloc (len
+ 1);
5491 strncpy (new_name
, eq
, len
);
5494 add_section_rename (old_name
, new_name
, flags
);
5498 case OPTION_SET_START
:
5499 set_start
= parse_vma (optarg
, "--set-start");
5500 set_start_set
= TRUE
;
5503 case OPTION_SREC_LEN
:
5504 _bfd_srec_len
= parse_vma (optarg
, "--srec-len");
5507 case OPTION_SREC_FORCES3
:
5508 _bfd_srec_forceS3
= TRUE
;
5511 case OPTION_STRIP_SYMBOLS
:
5512 add_specific_symbols (optarg
, strip_specific_htab
,
5513 &strip_specific_buffer
);
5516 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
5517 add_specific_symbols (optarg
, strip_unneeded_htab
,
5518 &strip_unneeded_buffer
);
5521 case OPTION_KEEP_SYMBOLS
:
5522 add_specific_symbols (optarg
, keep_specific_htab
,
5523 &keep_specific_buffer
);
5526 case OPTION_LOCALIZE_HIDDEN
:
5527 localize_hidden
= TRUE
;
5530 case OPTION_LOCALIZE_SYMBOLS
:
5531 add_specific_symbols (optarg
, localize_specific_htab
,
5532 &localize_specific_buffer
);
5535 case OPTION_LONG_SECTION_NAMES
:
5536 if (!strcmp ("enable", optarg
))
5537 long_section_names
= ENABLE
;
5538 else if (!strcmp ("disable", optarg
))
5539 long_section_names
= DISABLE
;
5540 else if (!strcmp ("keep", optarg
))
5541 long_section_names
= KEEP
;
5543 fatal (_("unknown long section names option '%s'"), optarg
);
5546 case OPTION_GLOBALIZE_SYMBOLS
:
5547 use_globalize
= TRUE
;
5548 add_specific_symbols (optarg
, globalize_specific_htab
,
5549 &globalize_specific_buffer
);
5552 case OPTION_KEEPGLOBAL_SYMBOLS
:
5553 use_keep_global
= TRUE
;
5554 add_specific_symbols (optarg
, keepglobal_specific_htab
,
5555 &keepglobal_specific_buffer
);
5558 case OPTION_WEAKEN_SYMBOLS
:
5559 add_specific_symbols (optarg
, weaken_specific_htab
,
5560 &weaken_specific_buffer
);
5563 case OPTION_ALT_MACH_CODE
:
5564 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
5565 if (use_alt_mach_code
== 0)
5566 fatal (_("unable to parse alternative machine code"));
5569 case OPTION_PREFIX_SYMBOLS
:
5570 prefix_symbols_string
= optarg
;
5573 case OPTION_PREFIX_SECTIONS
:
5574 prefix_sections_string
= optarg
;
5577 case OPTION_PREFIX_ALLOC_SECTIONS
:
5578 prefix_alloc_sections_string
= optarg
;
5581 case OPTION_READONLY_TEXT
:
5582 bfd_flags_to_set
|= WP_TEXT
;
5583 bfd_flags_to_clear
&= ~WP_TEXT
;
5586 case OPTION_WRITABLE_TEXT
:
5587 bfd_flags_to_clear
|= WP_TEXT
;
5588 bfd_flags_to_set
&= ~WP_TEXT
;
5592 bfd_flags_to_set
|= D_PAGED
;
5593 bfd_flags_to_clear
&= ~D_PAGED
;
5597 bfd_flags_to_clear
|= D_PAGED
;
5598 bfd_flags_to_set
&= ~D_PAGED
;
5601 case OPTION_EXTRACT_DWO
:
5602 strip_symbols
= STRIP_NONDWO
;
5605 case OPTION_EXTRACT_SYMBOL
:
5606 extract_symbol
= TRUE
;
5609 case OPTION_REVERSE_BYTES
:
5611 int prev
= reverse_bytes
;
5613 reverse_bytes
= atoi (optarg
);
5614 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
5615 fatal (_("number of bytes to reverse must be positive and even"));
5617 if (prev
&& prev
!= reverse_bytes
)
5618 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
5623 case OPTION_FILE_ALIGNMENT
:
5624 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
5630 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
5632 || (*end
!= '.' && *end
!= '\0'))
5633 non_fatal (_("%s: invalid reserve value for --heap"),
5635 else if (*end
!= '\0')
5637 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
5639 non_fatal (_("%s: invalid commit value for --heap"),
5645 case OPTION_IMAGE_BASE
:
5646 pe_image_base
= parse_vma (optarg
, "--image-base");
5649 case OPTION_PE_SECTION_ALIGNMENT
:
5650 pe_section_alignment
= parse_vma (optarg
,
5651 "--section-alignment");
5654 case OPTION_SUBSYSTEM
:
5655 set_pe_subsystem (optarg
);
5661 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
5663 || (*end
!= '.' && *end
!= '\0'))
5664 non_fatal (_("%s: invalid reserve value for --stack"),
5666 else if (*end
!= '\0')
5668 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
5670 non_fatal (_("%s: invalid commit value for --stack"),
5676 case OPTION_VERILOG_DATA_WIDTH
:
5677 VerilogDataWidth
= parse_vma (optarg
, "--verilog-data-width");
5678 if (VerilogDataWidth
< 1)
5679 fatal (_("verilog data width must be at least 1 byte"));
5683 /* We've been given a long option. */
5688 copy_usage (stdout
, 0);
5691 copy_usage (stderr
, 1);
5695 if (use_globalize
&& use_keep_global
)
5696 fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)"));
5705 print_version ("objcopy");
5707 if (interleave
&& copy_byte
== -1)
5708 fatal (_("interleave start byte must be set with --byte"));
5710 if (copy_byte
>= interleave
)
5711 fatal (_("byte number must be less than interleave"));
5713 if (copy_width
> interleave
- copy_byte
)
5714 fatal (_("interleave width must be less than or equal to interleave - byte`"));
5716 if (optind
== argc
|| optind
+ 2 < argc
)
5717 copy_usage (stderr
, 1);
5719 input_filename
= argv
[optind
];
5720 if (optind
+ 1 < argc
)
5721 output_filename
= argv
[optind
+ 1];
5723 default_deterministic ();
5725 /* Default is to strip no symbols. */
5726 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
5727 strip_symbols
= STRIP_NONE
;
5729 if (output_target
== NULL
)
5730 output_target
= input_target
;
5732 /* Convert input EFI target to PEI target. */
5733 if (input_target
!= NULL
5734 && strncmp (input_target
, "efi-", 4) == 0)
5738 efi
= xstrdup (output_target
+ 4);
5739 if (strncmp (efi
, "bsdrv-", 6) == 0
5740 || strncmp (efi
, "rtdrv-", 6) == 0)
5742 else if (strncmp (efi
, "app-", 4) != 0)
5743 fatal (_("unknown input EFI target: %s"), input_target
);
5746 convert_efi_target (efi
);
5749 /* Convert output EFI target to PEI target. */
5750 if (output_target
!= NULL
5751 && strncmp (output_target
, "efi-", 4) == 0)
5755 efi
= xstrdup (output_target
+ 4);
5756 if (strncmp (efi
, "app-", 4) == 0)
5758 if (pe_subsystem
== -1)
5759 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
5761 else if (strncmp (efi
, "bsdrv-", 6) == 0)
5763 if (pe_subsystem
== -1)
5764 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
5767 else if (strncmp (efi
, "rtdrv-", 6) == 0)
5769 if (pe_subsystem
== -1)
5770 pe_subsystem
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
5774 fatal (_("unknown output EFI target: %s"), output_target
);
5776 if (pe_file_alignment
== (bfd_vma
) -1)
5777 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
5778 if (pe_section_alignment
== (bfd_vma
) -1)
5779 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
5781 output_target
= efi
;
5782 convert_efi_target (efi
);
5786 if (stat (input_filename
, & statbuf
) < 0)
5787 fatal (_("warning: could not locate '%s'. System error message: %s"),
5788 input_filename
, strerror (errno
));
5790 /* If there is no destination file, or the source and destination files
5791 are the same, then create a temp and rename the result into the input. */
5792 if (output_filename
== NULL
5793 || filename_cmp (input_filename
, output_filename
) == 0)
5794 tmpname
= make_tempname (input_filename
);
5796 tmpname
= output_filename
;
5798 if (tmpname
== NULL
)
5799 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
5800 input_filename
, strerror (errno
));
5802 copy_file (input_filename
, tmpname
, input_target
, output_target
, input_arch
);
5806 set_times (tmpname
, &statbuf
);
5807 if (tmpname
!= output_filename
)
5808 status
= (smart_rename (tmpname
, input_filename
,
5809 preserve_dates
) != 0);
5812 unlink_if_ordinary (tmpname
);
5814 if (tmpname
!= output_filename
)
5819 struct section_list
*p
;
5821 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
5825 if (p
->context
& (SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
))
5829 sprintf_vma (buff
, p
->vma_val
);
5831 /* xgettext:c-format */
5832 non_fatal (_("%s %s%c0x%s never used"),
5833 "--change-section-vma",
5835 p
->context
& SECTION_CONTEXT_SET_VMA
? '=' : '+',
5839 if (p
->context
& (SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
))
5843 sprintf_vma (buff
, p
->lma_val
);
5845 /* xgettext:c-format */
5846 non_fatal (_("%s %s%c0x%s never used"),
5847 "--change-section-lma",
5849 p
->context
& SECTION_CONTEXT_SET_LMA
? '=' : '+',
5856 if (strip_specific_buffer
)
5857 free (strip_specific_buffer
);
5859 if (strip_unneeded_buffer
)
5860 free (strip_unneeded_buffer
);
5862 if (keep_specific_buffer
)
5863 free (keep_specific_buffer
);
5865 if (localize_specific_buffer
)
5866 free (globalize_specific_buffer
);
5868 if (globalize_specific_buffer
)
5869 free (globalize_specific_buffer
);
5871 if (keepglobal_specific_buffer
)
5872 free (keepglobal_specific_buffer
);
5874 if (weaken_specific_buffer
)
5875 free (weaken_specific_buffer
);
5881 main (int argc
, char *argv
[])
5883 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
5884 setlocale (LC_MESSAGES
, "");
5886 #if defined (HAVE_SETLOCALE)
5887 setlocale (LC_CTYPE
, "");
5889 bindtextdomain (PACKAGE
, LOCALEDIR
);
5890 textdomain (PACKAGE
);
5892 program_name
= argv
[0];
5893 xmalloc_set_program_name (program_name
);
5895 START_PROGRESS (program_name
, 0);
5897 expandargv (&argc
, &argv
);
5899 strip_symbols
= STRIP_UNDEF
;
5900 discard_locals
= LOCALS_UNDEF
;
5902 if (bfd_init () != BFD_INIT_MAGIC
)
5903 fatal (_("fatal error: libbfd ABI mismatch"));
5904 set_default_bfd_target ();
5908 int i
= strlen (program_name
);
5909 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5910 /* Drop the .exe suffix, if any. */
5911 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
5914 program_name
[i
] = '\0';
5917 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
5920 create_symbol_htabs ();
5923 bfd_set_error_program_name (argv
[0]);
5926 strip_main (argc
, argv
);
5928 copy_main (argc
, argv
);
5930 END_PROGRESS (program_name
);