1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2025 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
24 #include "libiberty.h"
27 #include "filenames.h"
30 #include "coff/internal.h"
32 #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
;
71 const char * othersym
;
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 bool keep_section_symbols
= false ;/* True if section symbols should be retained. */
95 static bool verbose
; /* Print file and target names. */
96 static bool preserve_dates
; /* Preserve input file timestamp. */
97 static int deterministic
= -1; /* Enable deterministic archives. */
98 static int status
= 0; /* Exit status. */
100 static bool merge_notes
= false; /* Merge note sections. */
101 static bool strip_section_headers
= false;/* Strip section headers. */
103 typedef struct merged_note_section
105 asection
* sec
; /* The section that is being merged. */
106 bfd_byte
* contents
;/* New contents of the section. */
107 bfd_size_type size
; /* New size of the section. */
108 struct merged_note_section
* next
; /* Link to next merged note section. */
109 } merged_note_section
;
114 STRIP_NONE
, /* Don't strip. */
115 STRIP_DEBUG
, /* Strip all debugger symbols. */
116 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
117 STRIP_NONDEBUG
, /* Strip everything but debug info. */
118 STRIP_DWO
, /* Strip all DWO info. */
119 STRIP_NONDWO
, /* Strip everything but DWO info. */
120 STRIP_ALL
/* Strip all symbols. */
123 /* Which symbols to remove. */
124 static enum strip_action strip_symbols
= STRIP_UNDEF
;
129 LOCALS_START_L
, /* Discard locals starting with L. */
130 LOCALS_ALL
/* Discard all locals. */
133 /* Which local symbols to remove. Overrides STRIP_ALL. */
134 static enum locals_action discard_locals
;
136 /* Structure used to hold lists of sections and actions to take. */
139 struct section_list
*next
; /* Next section to change. */
140 const char *pattern
; /* Section name pattern. */
141 bool used
; /* Whether this entry was used. */
143 unsigned int context
; /* What to do with matching sections. */
144 /* Flag bits used in the context field.
145 COPY and REMOVE are mutually exlusive.
146 SET and ALTER are mutually exclusive. */
147 #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
148 #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
149 #define SECTION_CONTEXT_KEEP (1 << 2) /* Keep this section. */
150 #define SECTION_CONTEXT_SET_VMA (1 << 3) /* Set the sections' VMA address. */
151 #define SECTION_CONTEXT_ALTER_VMA (1 << 4) /* Increment or decrement the section's VMA address. */
152 #define SECTION_CONTEXT_SET_LMA (1 << 5) /* Set the sections' LMA address. */
153 #define SECTION_CONTEXT_ALTER_LMA (1 << 6) /* Increment or decrement the section's LMA address. */
154 #define SECTION_CONTEXT_SET_FLAGS (1 << 7) /* Set the section's flags. */
155 #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 8) /* Remove relocations for this section. */
156 #define SECTION_CONTEXT_SET_ALIGNMENT (1 << 9) /* Set alignment for section. */
158 bfd_vma vma_val
; /* Amount to change by or set to. */
159 bfd_vma lma_val
; /* Amount to change by or set to. */
160 flagword flags
; /* What to set the section flags to. */
161 unsigned int alignment
; /* Alignment of output section. */
164 static struct section_list
*change_sections
;
166 /* TRUE if some sections are to be removed. */
167 static bool sections_removed
;
169 /* TRUE if only some sections are to be copied. */
170 static bool sections_copied
;
172 /* Changes to the start address. */
173 static bfd_vma change_start
= 0;
174 static bool set_start_set
= false;
175 static bfd_vma set_start
;
177 /* Changes to section addresses. */
178 static bfd_vma change_section_address
= 0;
180 /* Filling gaps between sections. */
181 static bool gap_fill_set
= false;
182 static bfd_byte gap_fill
= 0;
184 /* Pad to a given address. */
185 static bool pad_to_set
= false;
186 static bfd_vma pad_to
;
188 /* Use alternative machine code? */
189 static unsigned long use_alt_mach_code
= 0;
191 /* Output BFD flags user wants to set or clear */
192 static flagword bfd_flags_to_set
;
193 static flagword bfd_flags_to_clear
;
195 /* List of sections to add. */
198 /* Next section to add. */
199 struct section_add
*next
;
200 /* Name of section to add. */
202 /* Name of file holding section contents. */
203 const char *filename
;
206 /* Contents of file. */
208 /* BFD section, after it has been added. */
212 /* List of sections to add to the output BFD. */
213 static struct section_add
*add_sections
;
215 /* List of sections to update in the output BFD. */
216 static struct section_add
*update_sections
;
218 /* List of sections to dump from the output BFD. */
219 static struct section_add
*dump_sections
;
221 /* If non-NULL the argument to --add-gnu-debuglink.
222 This should be the filename to store in the .gnu_debuglink section. */
223 static const char * gnu_debuglink_filename
= NULL
;
225 /* Whether to convert debugging information. */
226 static bool convert_debugging
= false;
228 /* Whether to compress/decompress DWARF debug sections. */
233 compress_zlib
= compress
| 1 << 1,
234 compress_gnu_zlib
= compress
| 1 << 2,
235 compress_gabi_zlib
= compress
| 1 << 3,
236 compress_zstd
= compress
| 1 << 4,
238 } do_debug_sections
= nothing
;
240 /* Whether to generate ELF common symbols with the STT_COMMON type. */
241 static enum bfd_link_elf_stt_common do_elf_stt_common
= unchanged
;
243 /* Whether to change the leading character in symbol names. */
244 static bool change_leading_char
= false;
246 /* Whether to remove the leading character from global symbol names. */
247 static bool remove_leading_char
= false;
249 /* Whether to permit wildcard in symbol comparison. */
250 static bool wildcard
= false;
252 /* True if --localize-hidden is in effect. */
253 static bool localize_hidden
= false;
255 /* List of symbols to strip, keep, localize, keep-global, weaken,
257 static htab_t strip_specific_htab
= NULL
;
258 static htab_t strip_unneeded_htab
= NULL
;
259 static htab_t keep_specific_htab
= NULL
;
260 static htab_t localize_specific_htab
= NULL
;
261 static htab_t globalize_specific_htab
= NULL
;
262 static htab_t keepglobal_specific_htab
= NULL
;
263 static htab_t weaken_specific_htab
= NULL
;
264 static htab_t redefine_specific_htab
= NULL
;
265 static htab_t redefine_specific_reverse_htab
= NULL
;
266 static struct addsym_node
*add_sym_list
= NULL
, **add_sym_tail
= &add_sym_list
;
267 static int add_symbols
= 0;
269 static char *strip_specific_buffer
= NULL
;
270 static char *strip_unneeded_buffer
= NULL
;
271 static char *keep_specific_buffer
= NULL
;
272 static char *localize_specific_buffer
= NULL
;
273 static char *globalize_specific_buffer
= NULL
;
274 static char *keepglobal_specific_buffer
= NULL
;
275 static char *weaken_specific_buffer
= NULL
;
277 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
278 static bool weaken
= false;
280 /* If this is TRUE, we retain BSF_FILE symbols. */
281 static bool keep_file_symbols
= false;
283 /* Prefix symbols/sections. */
284 static char *prefix_symbols_string
= 0;
285 static char *prefix_sections_string
= 0;
286 static char *prefix_alloc_sections_string
= 0;
288 /* True if --extract-symbol was passed on the command line. */
289 static bool extract_symbol
= false;
291 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
292 of <reverse_bytes> bytes within each output section. */
293 static int reverse_bytes
= 0;
295 /* For Coff objects, we may want to allow or disallow long section names,
296 or preserve them where found in the inputs. Debug info relies on them. */
297 enum long_section_name_handling
304 /* The default long section handling mode is to preserve them.
305 This is also the only behaviour for 'strip'. */
306 static enum long_section_name_handling long_section_names
= KEEP
;
308 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
309 enum command_line_switch
311 OPTION_ADD_SECTION
=150,
312 OPTION_ADD_GNU_DEBUGLINK
,
314 OPTION_ALT_MACH_CODE
,
315 OPTION_CHANGE_ADDRESSES
,
316 OPTION_CHANGE_LEADING_CHAR
,
317 OPTION_CHANGE_SECTION_ADDRESS
,
318 OPTION_CHANGE_SECTION_LMA
,
319 OPTION_CHANGE_SECTION_VMA
,
321 OPTION_CHANGE_WARNINGS
,
322 OPTION_COMPRESS_DEBUG_SECTIONS
,
324 OPTION_DECOMPRESS_DEBUG_SECTIONS
,
326 OPTION_ELF_STT_COMMON
,
328 OPTION_EXTRACT_SYMBOL
,
329 OPTION_FILE_ALIGNMENT
,
332 OPTION_GLOBALIZE_SYMBOL
,
333 OPTION_GLOBALIZE_SYMBOLS
,
337 OPTION_BINARY_SYMBOL_PREFIX
,
338 OPTION_INTERLEAVE_WIDTH
,
339 OPTION_KEEPGLOBAL_SYMBOLS
,
340 OPTION_KEEP_FILE_SYMBOLS
,
343 OPTION_KEEP_SECTION_SYMBOLS
,
344 OPTION_LOCALIZE_HIDDEN
,
345 OPTION_LOCALIZE_SYMBOLS
,
346 OPTION_LONG_SECTION_NAMES
,
348 OPTION_NO_MERGE_NOTES
,
349 OPTION_NO_CHANGE_WARNINGS
,
350 OPTION_ONLY_KEEP_DEBUG
,
352 OPTION_PREFIX_ALLOC_SECTIONS
,
353 OPTION_PREFIX_SECTIONS
,
354 OPTION_PREFIX_SYMBOLS
,
356 OPTION_READONLY_TEXT
,
358 OPTION_REDEFINE_SYMS
,
359 OPTION_REMOVE_LEADING_CHAR
,
360 OPTION_REMOVE_RELOCS
,
361 OPTION_RENAME_SECTION
,
362 OPTION_REVERSE_BYTES
,
363 OPTION_PE_SECTION_ALIGNMENT
,
365 OPTION_SET_SECTION_FLAGS
,
366 OPTION_SET_SECTION_ALIGNMENT
,
372 OPTION_STRIP_SECTION_HEADERS
,
373 OPTION_STRIP_SYMBOLS
,
374 OPTION_STRIP_UNNEEDED
,
375 OPTION_STRIP_UNNEEDED_SYMBOL
,
376 OPTION_STRIP_UNNEEDED_SYMBOLS
,
378 OPTION_UPDATE_SECTION
,
379 OPTION_VERILOG_DATA_WIDTH
,
381 OPTION_WEAKEN_SYMBOLS
,
385 /* Options to handle if running as "strip". */
387 static struct option strip_options
[] =
389 {"disable-deterministic-archives", no_argument
, 0, 'U'},
390 {"discard-all", no_argument
, 0, 'x'},
391 {"discard-locals", no_argument
, 0, 'X'},
392 {"enable-deterministic-archives", no_argument
, 0, 'D'},
393 {"format", required_argument
, 0, 'F'}, /* Obsolete */
394 {"help", no_argument
, 0, 'h'},
395 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
396 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
397 {"input-target", required_argument
, 0, 'I'},
398 {"keep-section-symbols", no_argument
, 0, OPTION_KEEP_SECTION_SYMBOLS
},
399 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
400 {"keep-section", required_argument
, 0, OPTION_KEEP_SECTION
},
401 {"keep-symbol", required_argument
, 0, 'K'},
402 {"merge-notes", no_argument
, 0, 'M'},
403 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
404 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
405 {"output-file", required_argument
, 0, 'o'},
406 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
407 {"output-target", required_argument
, 0, 'O'},
408 {"plugin", required_argument
, 0, OPTION_PLUGIN
},
409 {"preserve-dates", no_argument
, 0, 'p'},
410 {"remove-section", required_argument
, 0, 'R'},
411 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
412 {"strip-section-headers", no_argument
, 0, OPTION_STRIP_SECTION_HEADERS
},
413 {"strip-all", no_argument
, 0, 's'},
414 {"strip-debug", no_argument
, 0, 'S'},
415 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
416 {"strip-symbol", required_argument
, 0, 'N'},
417 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
418 {"target", required_argument
, 0, 'F'},
419 {"verbose", no_argument
, 0, 'v'},
420 {"version", no_argument
, 0, 'V'},
421 {"wildcard", no_argument
, 0, 'w'},
422 {0, no_argument
, 0, 0}
425 /* Options to handle if running as "objcopy". */
427 static struct option copy_options
[] =
429 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
430 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
431 {"add-symbol", required_argument
, 0, OPTION_ADD_SYMBOL
},
432 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
433 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
434 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
435 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
436 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
437 {"binary-architecture", required_argument
, 0, 'B'},
438 {"byte", required_argument
, 0, 'b'},
439 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
440 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
441 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
442 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
443 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
444 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
445 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
446 {"compress-debug-sections", optional_argument
, 0, OPTION_COMPRESS_DEBUG_SECTIONS
},
447 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
448 {"decompress-debug-sections", no_argument
, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS
},
449 {"disable-deterministic-archives", no_argument
, 0, 'U'},
450 {"discard-all", no_argument
, 0, 'x'},
451 {"discard-locals", no_argument
, 0, 'X'},
452 {"dump-section", required_argument
, 0, OPTION_DUMP_SECTION
},
453 {"elf-stt-common", required_argument
, 0, OPTION_ELF_STT_COMMON
},
454 {"enable-deterministic-archives", no_argument
, 0, 'D'},
455 {"extract-dwo", no_argument
, 0, OPTION_EXTRACT_DWO
},
456 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
457 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
458 {"format", required_argument
, 0, 'F'}, /* Obsolete */
459 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
460 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
461 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
462 {"heap", required_argument
, 0, OPTION_HEAP
},
463 {"help", no_argument
, 0, 'h'},
464 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
465 {"impure", no_argument
, 0, OPTION_IMPURE
},
466 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
467 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
468 {"input-target", required_argument
, 0, 'I'},
469 {"binary-symbol-prefix", required_argument
, 0, OPTION_BINARY_SYMBOL_PREFIX
},
470 {"interleave", optional_argument
, 0, 'i'},
471 {"interleave-width", required_argument
, 0, OPTION_INTERLEAVE_WIDTH
},
472 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
473 {"keep-global-symbol", required_argument
, 0, 'G'},
474 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
475 {"keep-section", required_argument
, 0, OPTION_KEEP_SECTION
},
476 {"keep-symbol", required_argument
, 0, 'K'},
477 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
478 {"keep-section-symbols", required_argument
, 0, OPTION_KEEP_SECTION_SYMBOLS
},
479 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
480 {"localize-symbol", required_argument
, 0, 'L'},
481 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
482 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
483 {"merge-notes", no_argument
, 0, 'M'},
484 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
485 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
486 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
487 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
488 {"only-section", required_argument
, 0, 'j'},
489 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
490 {"output-target", required_argument
, 0, 'O'},
491 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
492 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
493 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
494 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
495 {"preserve-dates", no_argument
, 0, 'p'},
496 {"pure", no_argument
, 0, OPTION_PURE
},
497 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
498 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
499 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
500 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
501 {"remove-section", required_argument
, 0, 'R'},
502 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
503 {"strip-section-headers", no_argument
, 0, OPTION_STRIP_SECTION_HEADERS
},
504 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
505 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
506 {"section-alignment", required_argument
, 0, OPTION_PE_SECTION_ALIGNMENT
},
507 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
508 {"set-section-alignment", required_argument
, 0, OPTION_SET_SECTION_ALIGNMENT
},
509 {"set-start", required_argument
, 0, OPTION_SET_START
},
510 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
511 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
512 {"stack", required_argument
, 0, OPTION_STACK
},
513 {"strip-all", no_argument
, 0, 'S'},
514 {"strip-debug", no_argument
, 0, 'g'},
515 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
516 {"strip-symbol", required_argument
, 0, 'N'},
517 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
518 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
519 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
520 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
521 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
522 {"target", required_argument
, 0, 'F'},
523 {"update-section", required_argument
, 0, OPTION_UPDATE_SECTION
},
524 {"verbose", no_argument
, 0, 'v'},
525 {"verilog-data-width", required_argument
, 0, OPTION_VERILOG_DATA_WIDTH
},
526 {"version", no_argument
, 0, 'V'},
527 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
528 {"weaken-symbol", required_argument
, 0, 'W'},
529 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
530 {"wildcard", no_argument
, 0, 'w'},
531 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
532 {0, no_argument
, 0, 0}
536 extern char *program_name
;
538 /* This flag distinguishes between strip and objcopy:
539 1 means this is 'strip'; 0 means this is 'objcopy'.
540 -1 means if we should use argv[0] to decide. */
545 /* The symbol prefix of a binary input blob.
546 * <p>_start, <p>_end, <p>_size
548 extern char *bfd_binary_symbol_prefix
;
550 /* The maximum length of an S record. This variable is defined in srec.c
551 and can be modified by the --srec-len parameter. */
552 extern unsigned int _bfd_srec_len
;
554 /* Restrict the generation of Srecords to type S3 only.
555 This variable is defined in bfd/srec.c and can be toggled
556 on by the --srec-forceS3 command line switch. */
557 extern bool _bfd_srec_forceS3
;
559 /* Width of data in bytes for verilog output.
560 This variable is declared in bfd/verilog.c and can be modified by
561 the --verilog-data-width parameter. */
562 extern unsigned int VerilogDataWidth
;
564 /* Endianness of data for verilog output.
565 This variable is declared in bfd/verilog.c and is set in the
566 copy_object() function. */
567 extern enum bfd_endian VerilogDataEndianness
;
569 /* Forward declarations. */
570 static bool setup_section (bfd
*, asection
*, bfd
*);
571 static bool setup_bfd_headers (bfd
*, bfd
*);
572 static bool copy_relocations_in_section (bfd
*, asection
*, bfd
*);
573 static bool copy_section (bfd
*, asection
*, bfd
*);
574 static int compare_section_lma (const void *, const void *);
575 static bool mark_symbols_used_in_relocations (bfd
*, asection
*, asymbol
**);
576 static bool write_debugging_info (bfd
*, void *, long *, asymbol
***);
577 static const char *lookup_sym_redefinition (const char *);
578 static const char *find_section_rename (const char *, flagword
*);
581 #define ZSTD_OPT "|zstd"
586 ATTRIBUTE_NORETURN
static void
587 copy_usage (FILE *stream
, int exit_status
)
589 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
590 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
591 fprintf (stream
, _(" The options are:\n"));
592 fprintf (stream
, _("\
593 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
594 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
595 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
596 -F --target <bfdname> Set both input and output format to <bfdname>\n\
597 --debugging Convert debugging information, if possible\n\
598 -p --preserve-dates Copy modified/access timestamps to the output\n"));
599 if (DEFAULT_AR_DETERMINISTIC
)
600 fprintf (stream
, _("\
601 -D --enable-deterministic-archives\n\
602 Produce deterministic output when stripping archives (default)\n\
603 -U --disable-deterministic-archives\n\
604 Disable -D behavior\n"));
606 fprintf (stream
, _("\
607 -D --enable-deterministic-archives\n\
608 Produce deterministic output when stripping archives\n\
609 -U --disable-deterministic-archives\n\
610 Disable -D behavior (default)\n"));
611 fprintf (stream
, _("\
612 -j --only-section <name> Only copy section <name> into the output\n\
613 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
614 -R --remove-section <name> Remove section <name> from the output\n\
615 --remove-relocations <name> Remove relocations from section <name>\n\
616 --strip-section-headers Strip section header from the output\n\
617 -S --strip-all Remove all symbol and relocation information\n\
618 -g --strip-debug Remove all debugging symbols & sections\n\
619 --strip-dwo Remove all DWO sections\n\
620 --strip-unneeded Remove all symbols not needed by relocations\n\
621 -N --strip-symbol <name> Do not copy symbol <name>\n\
622 --strip-unneeded-symbol <name>\n\
623 Do not copy symbol <name> unless needed by\n\
625 --only-keep-debug Strip everything but the debug information\n\
626 --extract-dwo Copy only DWO sections\n\
627 --extract-symbol Remove section contents but keep symbols\n\
628 --keep-section <name> Do not strip section <name>\n\
629 -K --keep-symbol <name> Do not strip symbol <name>\n\
630 --keep-section-symbols Do not strip section symbols\n\
631 --keep-file-symbols Do not strip file symbol(s)\n\
632 --localize-hidden Turn all ELF hidden symbols into locals\n\
633 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
634 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
635 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
636 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
637 --weaken Force all global symbols to be marked as weak\n\
638 -w --wildcard Permit wildcard in symbol comparison\n\
639 -x --discard-all Remove all non-global symbols\n\
640 -X --discard-locals Remove any compiler-generated symbols\n\
641 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
642 --interleave-width <number> Set N for --interleave\n\
643 -b --byte <num> Select byte <num> in every interleaved block\n\
644 --gap-fill <val> Fill gaps between sections with <val>\n\
645 --pad-to <addr> Pad the last section up to address <addr>\n\
646 --set-start <addr> Set the start address to <addr>\n\
647 {--change-start|--adjust-start} <incr>\n\
648 Add <incr> to the start address\n\
649 {--change-addresses|--adjust-vma} <incr>\n\
650 Add <incr> to LMA, VMA and start addresses\n\
651 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
652 Change LMA and VMA of section <name> by <val>\n\
653 --change-section-lma <name>{=|+|-}<val>\n\
654 Change the LMA of section <name> by <val>\n\
655 --change-section-vma <name>{=|+|-}<val>\n\
656 Change the VMA of section <name> by <val>\n\
657 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
658 Warn if a named section does not exist\n\
659 --set-section-flags <name>=<flags>\n\
660 Set section <name>'s properties to <flags>\n\
661 --set-section-alignment <name>=<align>\n\
662 Set section <name>'s alignment to <align> bytes\n\
663 --add-section <name>=<file> Add section <name> found in <file> to output\n\
664 --update-section <name>=<file>\n\
665 Update contents of section <name> with\n\
666 contents found in <file>\n\
667 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
668 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
669 --long-section-names {enable|disable|keep}\n\
670 Handle long section names in Coff objects.\n\
671 --change-leading-char Force output format's leading character style\n\
672 --remove-leading-char Remove leading character from global symbols\n\
673 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
674 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
675 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
677 --srec-len <number> Restrict the length of generated Srecords\n\
678 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
679 --strip-symbols <file> -N for all symbols listed in <file>\n\
680 --strip-unneeded-symbols <file>\n\
681 --strip-unneeded-symbol for all symbols listed\n\
683 --keep-symbols <file> -K for all symbols listed in <file>\n\
684 --localize-symbols <file> -L for all symbols listed in <file>\n\
685 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
686 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
687 --weaken-symbols <file> -W for all symbols listed in <file>\n\
688 --binary-symbol-prefix <prefix>\n\
689 Use <prefix> as the base symbol name for the input file\n\
690 (default: derived from file name)\n\
691 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
692 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
693 --writable-text Mark the output text as writable\n\
694 --readonly-text Make the output text write protected\n\
695 --pure Mark the output file as demand paged\n\
696 --impure Mark the output file as impure\n\
697 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
698 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
699 --prefix-alloc-sections <prefix>\n\
700 Add <prefix> to start of every allocatable\n\
702 --file-alignment <num> Set PE file alignment to <num>\n\
703 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
705 --image-base <address> Set PE image base to <address>\n\
706 --section-alignment <num> Set PE section alignment to <num>\n\
707 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
709 --subsystem <name>[:<version>]\n\
710 Set PE subsystem to <name> [& <version>]\n\
711 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi" ZSTD_OPT
"}]\n\
712 Compress DWARF debug sections\n\
713 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
714 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
716 --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
717 -M --merge-notes Remove redundant entries in note sections\n\
718 --no-merge-notes Do not attempt to remove redundant notes (default)\n\
719 -v --verbose List all object files modified\n\
720 @<file> Read options from <file>\n\
721 -V --version Display this program's version number\n\
722 -h --help Display this output\n\
723 --info List object formats & architectures supported\n\
725 list_supported_targets (program_name
, stream
);
726 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
727 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
731 ATTRIBUTE_NORETURN
static void
732 strip_usage (FILE *stream
, int exit_status
)
734 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
735 fprintf (stream
, _(" Removes symbols and sections from files\n"));
736 fprintf (stream
, _(" The options are:\n"));
737 fprintf (stream
, _("\
738 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
739 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
740 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
741 -p --preserve-dates Copy modified/access timestamps to the output\n\
743 if (DEFAULT_AR_DETERMINISTIC
)
744 fprintf (stream
, _("\
745 -D --enable-deterministic-archives\n\
746 Produce deterministic output when stripping archives (default)\n\
747 -U --disable-deterministic-archives\n\
748 Disable -D behavior\n"));
750 fprintf (stream
, _("\
751 -D --enable-deterministic-archives\n\
752 Produce deterministic output when stripping archives\n\
753 -U --disable-deterministic-archives\n\
754 Disable -D behavior (default)\n"));
755 fprintf (stream
, _("\
756 -R --remove-section=<name> Also remove section <name> from the output\n\
757 --remove-relocations <name> Remove relocations from section <name>\n\
758 --strip-section-headers Strip section headers from the output\n\
759 -s --strip-all Remove all symbol and relocation information\n\
760 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
761 --strip-dwo Remove all DWO sections\n\
762 --strip-unneeded Remove all symbols not needed by relocations\n\
763 --only-keep-debug Strip everything but the debug information\n\
764 -M --merge-notes Remove redundant entries in note sections (default)\n\
765 --no-merge-notes Do not attempt to remove redundant notes\n\
766 -N --strip-symbol=<name> Do not copy symbol <name>\n\
767 --keep-section=<name> Do not strip section <name>\n\
768 -K --keep-symbol=<name> Do not strip symbol <name>\n\
769 --keep-section-symbols Do not strip section symbols\n\
770 --keep-file-symbols Do not strip file symbol(s)\n\
771 -w --wildcard Permit wildcard in symbol comparison\n\
772 -x --discard-all Remove all non-global symbols\n\
773 -X --discard-locals Remove any compiler-generated symbols\n\
774 -v --verbose List all object files modified\n\
775 -V --version Display this program's version number\n\
776 -h --help Display this output\n\
777 --info List object formats & architectures supported\n\
778 -o <file> Place stripped output into <file>\n\
780 if (bfd_plugin_enabled ())
781 fprintf (stream
, _("\
782 --plugin NAME Load the specified plugin\n"));
784 list_supported_targets (program_name
, stream
);
785 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
786 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
790 /* Parse section flags into a flagword, with a fatal error if the
791 string can't be parsed. */
794 parse_flags (const char *s
)
804 snext
= strchr (s
, ',');
814 #define PARSE_FLAG(fname,fval) \
815 else if (strncasecmp (fname, s, len) == 0) ret |= fval
816 PARSE_FLAG ("alloc", SEC_ALLOC
);
817 PARSE_FLAG ("load", SEC_LOAD
);
818 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
819 PARSE_FLAG ("readonly", SEC_READONLY
);
820 PARSE_FLAG ("debug", SEC_DEBUGGING
);
821 PARSE_FLAG ("code", SEC_CODE
);
822 PARSE_FLAG ("data", SEC_DATA
);
823 PARSE_FLAG ("rom", SEC_ROM
);
824 PARSE_FLAG ("exclude", SEC_EXCLUDE
);
825 PARSE_FLAG ("share", SEC_COFF_SHARED
);
826 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
827 PARSE_FLAG ("merge", SEC_MERGE
);
828 PARSE_FLAG ("strings", SEC_STRINGS
);
829 PARSE_FLAG ("large", SEC_ELF_LARGE
);
835 copy
= (char *) xmalloc (len
+ 1);
836 strncpy (copy
, s
, len
);
838 non_fatal (_("unrecognized section flag `%s'"), copy
);
839 fatal (_ ("supported flags: %s"),
840 "alloc, load, noload, readonly, debug, code, data, rom, "
841 "exclude, contents, merge, strings, (COFF specific) share, "
842 "(ELF x86-64 specific) large");
852 /* Parse symbol flags into a flagword, with a fatal error if the
853 string can't be parsed. */
856 parse_symflags (const char *s
, const char **other
)
866 snext
= strchr (s
, ',');
875 #define PARSE_FLAG(fname, fval) \
876 else if (len == sizeof fname - 1 \
877 && strncasecmp (fname, s, len) == 0) \
880 #define PARSE_OTHER(fname, fval) \
881 else if (len >= sizeof fname \
882 && strncasecmp (fname, s, sizeof fname - 1) == 0) \
883 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
886 PARSE_FLAG ("local", BSF_LOCAL
);
887 PARSE_FLAG ("global", BSF_GLOBAL
);
888 PARSE_FLAG ("export", BSF_EXPORT
);
889 PARSE_FLAG ("debug", BSF_DEBUGGING
);
890 PARSE_FLAG ("function", BSF_FUNCTION
);
891 PARSE_FLAG ("weak", BSF_WEAK
);
892 PARSE_FLAG ("section", BSF_SECTION_SYM
);
893 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR
);
894 PARSE_FLAG ("warning", BSF_WARNING
);
895 PARSE_FLAG ("indirect", BSF_INDIRECT
);
896 PARSE_FLAG ("file", BSF_FILE
);
897 PARSE_FLAG ("object", BSF_OBJECT
);
898 PARSE_FLAG ("synthetic", BSF_SYNTHETIC
);
899 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION
| BSF_FUNCTION
);
900 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE
| BSF_OBJECT
);
901 PARSE_OTHER ("before=", *other
);
909 copy
= (char *) xmalloc (len
+ 1);
910 strncpy (copy
, s
, len
);
912 non_fatal (_("unrecognized symbol flag `%s'"), copy
);
913 fatal (_("supported flags: %s"),
914 "local, global, export, debug, function, weak, section, "
915 "constructor, warning, indirect, file, object, synthetic, "
916 "indirect-function, unique-object, before=<othersym>");
926 /* Find and optionally add an entry in the change_sections list.
928 We need to be careful in how we match section names because of the support
929 for wildcard characters. For example suppose that the user has invoked
932 --set-section-flags .debug_*=debug
933 --set-section-flags .debug_str=readonly,debug
934 --change-section-address .debug_*ranges=0x1000
936 With the idea that all debug sections will receive the DEBUG flag, the
937 .debug_str section will also receive the READONLY flag and the
938 .debug_ranges and .debug_aranges sections will have their address set to
939 0x1000. (This may not make much sense, but it is just an example).
941 When adding the section name patterns to the section list we need to make
942 sure that previous entries do not match with the new entry, unless the
943 match is exact. (In which case we assume that the user is overriding
944 the previous entry with the new context).
946 When matching real section names to the section list we make use of the
947 wildcard characters, but we must do so in context. Eg if we are setting
948 section addresses then we match for .debug_ranges but not for .debug_info.
950 Finally, if ADD is false and we do find a match, we mark the section list
953 static struct section_list
*
954 find_section_list (const char *name
, bool add
, unsigned int context
)
956 struct section_list
*p
, *match
= NULL
;
958 /* assert ((context & ((1 << 7) - 1)) != 0); */
960 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
964 if (strcmp (p
->pattern
, name
) == 0)
966 /* Check for context conflicts. */
967 if (((p
->context
& SECTION_CONTEXT_REMOVE
)
968 && (context
& SECTION_CONTEXT_COPY
))
969 || ((context
& SECTION_CONTEXT_REMOVE
)
970 && (p
->context
& SECTION_CONTEXT_COPY
)))
971 fatal (_("error: %s both copied and removed"), name
);
973 if (((p
->context
& SECTION_CONTEXT_SET_VMA
)
974 && (context
& SECTION_CONTEXT_ALTER_VMA
))
975 || ((context
& SECTION_CONTEXT_SET_VMA
)
976 && (context
& SECTION_CONTEXT_ALTER_VMA
)))
977 fatal (_("error: %s both sets and alters VMA"), name
);
979 if (((p
->context
& SECTION_CONTEXT_SET_LMA
)
980 && (context
& SECTION_CONTEXT_ALTER_LMA
))
981 || ((context
& SECTION_CONTEXT_SET_LMA
)
982 && (context
& SECTION_CONTEXT_ALTER_LMA
)))
983 fatal (_("error: %s both sets and alters LMA"), name
);
985 /* Extend the context. */
986 p
->context
|= context
;
990 /* If we are not adding a new name/pattern then
991 only check for a match if the context applies. */
992 else if (p
->context
& context
)
994 /* We could check for the presence of wildchar characters
995 first and choose between calling strcmp and fnmatch,
996 but is that really worth it ? */
997 if (p
->pattern
[0] == '!')
999 if (fnmatch (p
->pattern
+ 1, name
, 0) == 0)
1007 if (fnmatch (p
->pattern
, name
, 0) == 0)
1023 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
1026 p
->context
= context
;
1031 p
->next
= change_sections
;
1032 change_sections
= p
;
1037 /* S1 is the entry node already in the table, S2 is the key node. */
1040 eq_string_redefnode (const void *s1
, const void *s2
)
1042 struct redefine_node
*node1
= (struct redefine_node
*) s1
;
1043 struct redefine_node
*node2
= (struct redefine_node
*) s2
;
1044 return !strcmp ((const char *) node1
->source
, (const char *) node2
->source
);
1047 /* P is redefine node. Hash value is generated from its "source" filed. */
1050 htab_hash_redefnode (const void *p
)
1052 struct redefine_node
*redefnode
= (struct redefine_node
*) p
;
1053 return htab_hash_string (redefnode
->source
);
1056 /* Create hashtab used for redefine node. */
1059 create_symbol2redef_htab (void)
1061 return htab_create_alloc (16, htab_hash_redefnode
, eq_string_redefnode
, NULL
,
1066 create_symbol_htab (void)
1068 return htab_create_alloc (16, htab_hash_string
, htab_eq_string
, NULL
,
1073 create_symbol_htabs (void)
1075 strip_specific_htab
= create_symbol_htab ();
1076 strip_unneeded_htab
= create_symbol_htab ();
1077 keep_specific_htab
= create_symbol_htab ();
1078 localize_specific_htab
= create_symbol_htab ();
1079 globalize_specific_htab
= create_symbol_htab ();
1080 keepglobal_specific_htab
= create_symbol_htab ();
1081 weaken_specific_htab
= create_symbol_htab ();
1082 redefine_specific_htab
= create_symbol2redef_htab ();
1083 /* As there is no bidirectional hash table in libiberty, need a reverse table
1084 to check duplicated target string. */
1085 redefine_specific_reverse_htab
= create_symbol_htab ();
1089 delete_symbol_htabs (void)
1091 htab_delete (strip_specific_htab
);
1092 htab_delete (strip_unneeded_htab
);
1093 htab_delete (keep_specific_htab
);
1094 htab_delete (localize_specific_htab
);
1095 htab_delete (globalize_specific_htab
);
1096 htab_delete (keepglobal_specific_htab
);
1097 htab_delete (weaken_specific_htab
);
1098 htab_delete (redefine_specific_htab
);
1099 htab_delete (redefine_specific_reverse_htab
);
1102 if (osympp
!= isympp
)
1106 /* Add a symbol to strip_specific_list. */
1109 add_specific_symbol (const char *name
, htab_t htab
)
1111 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
1114 /* Like add_specific_symbol, but the element type is void *. */
1117 add_specific_symbol_node (const void *node
, htab_t htab
)
1119 *htab_find_slot (htab
, node
, INSERT
) = (void *) node
;
1122 /* Add symbols listed in `filename' to strip_specific_list. */
1124 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
1125 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
1128 add_specific_symbols (const char *filename
, htab_t htab
, char **buffer_p
)
1134 unsigned int line_count
;
1136 size
= get_file_size (filename
);
1143 buffer
= (char *) xmalloc (size
+ 2);
1144 f
= fopen (filename
, FOPEN_RT
);
1146 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
1148 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
1149 fatal (_("%s: fread failed"), filename
);
1152 buffer
[size
] = '\n';
1153 buffer
[size
+ 1] = '\0';
1157 for (line
= buffer
; * line
!= '\0'; line
++)
1162 int finished
= false;
1164 for (eol
= line
;; eol
++)
1170 /* Cope with \n\r. */
1178 /* Cope with \r\n. */
1189 /* Line comment, Terminate the line here, in case a
1190 name is present and then allow the rest of the
1191 loop to find the real end of the line. */
1203 /* A name may now exist somewhere between 'line' and 'eol'.
1204 Strip off leading whitespace and trailing whitespace,
1205 then add it to the list. */
1206 for (name
= line
; IS_WHITESPACE (* name
); name
++)
1208 for (name_end
= name
;
1209 (! IS_WHITESPACE (* name_end
))
1210 && (! IS_LINE_TERMINATOR (* name_end
));
1214 if (! IS_LINE_TERMINATOR (* name_end
))
1218 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
1221 if (! IS_LINE_TERMINATOR (* extra
))
1222 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1223 filename
, line_count
);
1228 if (name_end
> name
)
1229 add_specific_symbol (name
, htab
);
1231 /* Advance line pointer to end of line. The 'eol ++' in the for
1232 loop above will then advance us to the start of the next line. */
1237 /* Do not free the buffer. Parts of it will have been referenced
1238 in the calls to add_specific_symbol. */
1242 /* See whether a symbol should be stripped or kept
1243 based on strip_specific_list and keep_symbols. */
1246 is_specified_symbol_predicate (void **slot
, void *data
)
1248 struct is_specified_symbol_predicate_data
*d
=
1249 (struct is_specified_symbol_predicate_data
*) data
;
1250 const char *slot_name
= (char *) *slot
;
1252 if (*slot_name
!= '!')
1254 if (! fnmatch (slot_name
, d
->name
, 0))
1257 /* Continue traversal, there might be a non-match rule. */
1263 if (! fnmatch (slot_name
+ 1, d
->name
, 0))
1266 /* Stop traversal. */
1271 /* Continue traversal. */
1276 is_specified_symbol (const char *name
, htab_t htab
)
1280 struct is_specified_symbol_predicate_data data
;
1285 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
1290 return htab_find (htab
, name
) != NULL
;
1293 /* Return TRUE if the section is a DWO section. */
1296 is_dwo_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1301 if (sec
== NULL
|| (name
= bfd_section_name (sec
)) == NULL
)
1304 len
= strlen (name
);
1308 return startswith (name
+ len
- 4, ".dwo");
1311 /* Return TRUE if section SEC is in the update list. */
1314 is_update_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1316 if (update_sections
!= NULL
)
1318 struct section_add
*pupdate
;
1320 for (pupdate
= update_sections
;
1322 pupdate
= pupdate
->next
)
1324 if (strcmp (sec
->name
, pupdate
->name
) == 0)
1333 is_mergeable_note_section (bfd
* abfd
, asection
* sec
)
1336 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1337 && elf_section_data (sec
)->this_hdr
.sh_type
== SHT_NOTE
1338 /* FIXME: We currently only support merging GNU_BUILD_NOTEs.
1339 We should add support for more note types. */
1340 && (startswith (sec
->name
, GNU_BUILD_ATTRS_SECTION_NAME
)))
1346 /* See if a non-group section is being removed. */
1349 is_strip_section_1 (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1351 if (find_section_list (bfd_section_name (sec
), false, SECTION_CONTEXT_KEEP
)
1355 if (sections_removed
|| sections_copied
)
1357 struct section_list
*p
;
1358 struct section_list
*q
;
1360 p
= find_section_list (bfd_section_name (sec
), false,
1361 SECTION_CONTEXT_REMOVE
);
1362 q
= find_section_list (bfd_section_name (sec
), false,
1363 SECTION_CONTEXT_COPY
);
1366 fatal (_("error: section %s matches both remove and copy options"),
1367 bfd_section_name (sec
));
1368 if (p
&& is_update_section (abfd
, sec
))
1369 fatal (_("error: section %s matches both update and remove options"),
1370 bfd_section_name (sec
));
1374 if (sections_copied
&& q
== NULL
)
1378 /* Remove non-alloc sections for --strip-section-headers. */
1379 if (strip_section_headers
1380 && (bfd_section_flags (sec
) & SEC_ALLOC
) == 0)
1383 if ((bfd_section_flags (sec
) & SEC_DEBUGGING
) != 0)
1385 if (strip_symbols
== STRIP_DEBUG
1386 || strip_symbols
== STRIP_UNNEEDED
1387 || strip_symbols
== STRIP_ALL
1388 || discard_locals
== LOCALS_ALL
1389 || convert_debugging
)
1391 /* By default we don't want to strip .reloc section.
1392 This section has for pe-coff special meaning. See
1393 pe-dll.c file in ld, and peXXigen.c in bfd for details.
1394 Similarly we do not want to strip debuglink sections. */
1395 const char * kept_sections
[] =
1403 for (i
= ARRAY_SIZE (kept_sections
);i
--;)
1404 if (strcmp (bfd_section_name (sec
), kept_sections
[i
]) == 0)
1410 if (strip_symbols
== STRIP_DWO
)
1411 return is_dwo_section (abfd
, sec
);
1413 if (strip_symbols
== STRIP_NONDEBUG
)
1417 if (strip_symbols
== STRIP_NONDWO
)
1418 return !is_dwo_section (abfd
, sec
);
1423 /* See if a section is being removed. */
1426 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1428 if (is_strip_section_1 (abfd
, sec
))
1431 if ((bfd_section_flags (sec
) & SEC_GROUP
) != 0)
1435 asection
*elt
, *first
;
1437 gsym
= bfd_group_signature (sec
, isympp
);
1438 /* Strip groups without a valid signature. */
1443 If we are going to strip the group signature symbol, then
1444 strip the group section too. */
1446 if ((strip_symbols
== STRIP_ALL
1447 && !is_specified_symbol (gname
, keep_specific_htab
))
1448 || is_specified_symbol (gname
, strip_specific_htab
))
1451 /* Remove the group section if all members are removed. */
1452 first
= elt
= elf_next_in_group (sec
);
1455 if (!is_strip_section_1 (abfd
, elt
))
1457 elt
= elf_next_in_group (elt
);
1469 is_nondebug_keep_contents_section (bfd
*ibfd
, asection
*isection
)
1471 /* Always keep ELF note sections. */
1472 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
1473 return elf_section_type (isection
) == SHT_NOTE
;
1475 /* Always keep the .buildid section for PE/COFF.
1477 Strictly, this should be written "always keep the section storing the debug
1478 directory", but that may be the .text section for objects produced by some
1479 tools, which it is not sensible to keep. */
1480 if (bfd_get_flavour (ibfd
) == bfd_target_coff_flavour
)
1481 return strcmp (bfd_section_name (isection
), ".buildid") == 0;
1486 /* Return true if SYM is a hidden symbol. */
1489 is_hidden_symbol (asymbol
*sym
)
1491 elf_symbol_type
*elf_sym
;
1493 elf_sym
= elf_symbol_from (sym
);
1494 if (elf_sym
!= NULL
)
1495 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
1504 /* Empty name is hopefully never a valid symbol name. */
1505 static const char * empty_name
= "";
1508 need_sym_before (struct addsym_node
**node
, const char *sym
)
1511 struct addsym_node
*ptr
= add_sym_list
;
1513 /* 'othersym' symbols are at the front of the list. */
1514 for (count
= 0; count
< add_symbols
; count
++)
1518 if (ptr
->othersym
== empty_name
)
1520 else if (strcmp (ptr
->othersym
, sym
) == 0)
1522 free ((char *) ptr
->othersym
);
1523 ptr
->othersym
= empty_name
;
1533 create_new_symbol (struct addsym_node
*ptr
, bfd
*obfd
)
1535 asymbol
*sym
= bfd_make_empty_symbol (obfd
);
1537 bfd_set_asymbol_name (sym
, ptr
->symdef
);
1538 sym
->value
= ptr
->symval
;
1539 sym
->flags
= ptr
->flags
;
1542 asection
*sec
= bfd_get_section_by_name (obfd
, ptr
->section
);
1544 fatal (_("Section %s not found"), ptr
->section
);
1548 sym
->section
= bfd_abs_section_ptr
;
1552 /* Choose which symbol entries to copy; put the result in OSYMS.
1553 We don't copy in place, because that confuses the relocs.
1554 Update the number of symbols to print. */
1557 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
1558 asymbol
**isyms
, long *symcount
)
1560 asymbol
**from
= isyms
, **to
= osyms
;
1561 long src_count
= 0, dst_count
= 0;
1562 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
1564 for (; src_count
< *symcount
; src_count
++)
1566 asymbol
*sym
= from
[src_count
];
1567 flagword flags
= sym
->flags
;
1568 char *name
= (char *) bfd_asymbol_name (sym
);
1570 bool used_in_reloc
= false;
1572 bool rem_leading_char
;
1573 bool add_leading_char
;
1575 undefined
= bfd_is_und_section (bfd_asymbol_section (sym
));
1579 struct addsym_node
*ptr
;
1581 if (need_sym_before (&ptr
, name
))
1582 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1585 if (htab_elements (redefine_specific_htab
) || section_rename_list
)
1591 && strcmp (name
+ (name
[2] == '_'), "__gnu_lto_slim") == 0)
1593 fatal (_("redefining symbols does not work"
1594 " on LTO-compiled object files"));
1597 new_name
= (char *) lookup_sym_redefinition (name
);
1598 if (new_name
== name
1599 && (flags
& BSF_SECTION_SYM
) != 0)
1600 new_name
= (char *) find_section_rename (name
, NULL
);
1601 bfd_set_asymbol_name (sym
, new_name
);
1605 /* Check if we will remove the current leading character. */
1608 && name
[0] == bfd_get_symbol_leading_char (abfd
)
1609 && (change_leading_char
1610 || (remove_leading_char
1611 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1613 || bfd_is_com_section (bfd_asymbol_section (sym
))))));
1615 /* Check if we will add a new leading character. */
1618 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1619 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1620 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1622 /* Short circuit for change_leading_char if we can do it in-place. */
1623 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1625 name
[0] = bfd_get_symbol_leading_char (obfd
);
1626 bfd_set_asymbol_name (sym
, name
);
1627 rem_leading_char
= false;
1628 add_leading_char
= false;
1631 /* Remove leading char. */
1632 if (rem_leading_char
)
1633 bfd_set_asymbol_name (sym
, ++name
);
1635 /* Add new leading char and/or prefix. */
1636 if (add_leading_char
|| prefix_symbols_string
)
1639 size_t len
= strlen (name
) + 1;
1641 if (add_leading_char
)
1643 if (prefix_symbols_string
)
1644 len
+= strlen (prefix_symbols_string
);
1646 ptr
= n
= (char *) xmalloc (len
);
1647 if (add_leading_char
)
1648 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1650 if (prefix_symbols_string
)
1652 strcpy (ptr
, prefix_symbols_string
);
1653 ptr
+= strlen (prefix_symbols_string
);
1657 bfd_set_asymbol_name (sym
, n
);
1661 if (strip_symbols
== STRIP_ALL
)
1663 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1664 || ((flags
& BSF_SECTION_SYM
) != 0
1665 && (bfd_asymbol_section (sym
)->symbol
->flags
1669 used_in_reloc
= true;
1671 else if (relocatable
/* Relocatable file. */
1672 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1673 || bfd_is_com_section (bfd_asymbol_section (sym
))))
1675 else if (bfd_decode_symclass (sym
) == 'I')
1676 /* Global symbols in $idata sections need to be retained
1677 even if relocatable is FALSE. External users of the
1678 library containing the $idata section may reference these
1681 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1682 || (flags
& BSF_WEAK
) != 0
1684 || bfd_is_com_section (bfd_asymbol_section (sym
)))
1685 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1686 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1687 keep
= (strip_symbols
!= STRIP_DEBUG
1688 && strip_symbols
!= STRIP_UNNEEDED
1689 && ! convert_debugging
);
1690 else if (bfd_coff_get_comdat_section (abfd
, bfd_asymbol_section (sym
)))
1691 /* COMDAT sections store special information in local
1692 symbols, so we cannot risk stripping any of them. */
1694 else /* Local symbol. */
1695 keep
= (strip_symbols
!= STRIP_UNNEEDED
1696 && (discard_locals
!= LOCALS_ALL
1697 && (discard_locals
!= LOCALS_START_L
1698 || ! bfd_is_local_label (abfd
, sym
))));
1700 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1702 /* There are multiple ways to set 'keep' above, but if it
1703 was the relocatable symbol case, then that's an error. */
1706 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1714 && !(flags
& BSF_KEEP
)
1715 && is_specified_symbol (name
, strip_unneeded_htab
))
1719 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1720 || is_specified_symbol (name
, keep_specific_htab
)))
1723 if (keep
&& is_strip_section (abfd
, bfd_asymbol_section (sym
)))
1729 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1730 && (is_specified_symbol (name
, localize_specific_htab
)
1731 || (htab_elements (keepglobal_specific_htab
) != 0
1732 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1733 || (localize_hidden
&& is_hidden_symbol (sym
))))
1735 flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
1740 && (flags
& BSF_LOCAL
)
1741 && !(flags
& BSF_FILE
)
1742 && is_specified_symbol (name
, globalize_specific_htab
))
1744 flags
&= ~BSF_LOCAL
;
1745 flags
|= BSF_GLOBAL
;
1748 if (((flags
& (BSF_GLOBAL
| BSF_GNU_UNIQUE
))
1750 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1752 flags
&= ~(BSF_GLOBAL
| BSF_GNU_UNIQUE
);
1757 to
[dst_count
++] = sym
;
1762 struct addsym_node
*ptr
= add_sym_list
;
1764 for (src_count
= 0; src_count
< add_symbols
; src_count
++)
1768 if (ptr
->othersym
!= empty_name
)
1769 fatal (_("'before=%s' not found"), ptr
->othersym
);
1772 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1778 to
[dst_count
] = NULL
;
1779 *symcount
= dst_count
;
1784 /* Find the redefined name of symbol SOURCE. */
1787 lookup_sym_redefinition (const char *source
)
1789 struct redefine_node key_node
= {(char *) source
, NULL
};
1790 struct redefine_node
*redef_node
1791 = (struct redefine_node
*) htab_find (redefine_specific_htab
, &key_node
);
1793 return redef_node
== NULL
? source
: redef_node
->target
;
1796 /* Insert a node into symbol redefine hash tabel. */
1799 add_redefine_and_check (const char *cause
, const char *source
,
1802 struct redefine_node
*new_node
1803 = (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
1805 new_node
->source
= strdup (source
);
1806 new_node
->target
= strdup (target
);
1808 if (htab_find (redefine_specific_htab
, new_node
) != HTAB_EMPTY_ENTRY
)
1809 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1812 if (htab_find (redefine_specific_reverse_htab
, target
) != HTAB_EMPTY_ENTRY
)
1813 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1816 /* Insert the NEW_NODE into hash table for quick search. */
1817 add_specific_symbol_node (new_node
, redefine_specific_htab
);
1819 /* Insert the target string into the reverse hash table, this is needed for
1820 duplicated target string check. */
1821 add_specific_symbol (new_node
->target
, redefine_specific_reverse_htab
);
1825 /* Handle the --redefine-syms option. Read lines containing "old new"
1826 from the file, and add them to the symbol redefine list. */
1829 add_redefine_syms_file (const char *filename
)
1838 file
= fopen (filename
, "r");
1840 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1841 filename
, strerror (errno
));
1844 buf
= (char *) xmalloc (bufsize
+ 1 /* For the terminating NUL. */);
1852 /* Collect the input symbol name. */
1853 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1861 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1869 /* Eat white space between the symbol names. */
1870 while (IS_WHITESPACE (c
))
1872 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1877 /* Collect the output symbol name. */
1879 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1887 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1895 /* Eat white space at end of line. */
1896 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1901 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1902 || c
== '\n' || c
== EOF
)
1905 /* Append the redefinition to the list. */
1907 add_redefine_and_check (filename
, &buf
[0], &buf
[outsym_off
]);
1918 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1920 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1921 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1924 /* Eat the rest of the line and finish it. */
1925 while (c
!= '\n' && c
!= EOF
)
1931 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1937 /* Copy unknown object file IBFD onto OBFD.
1938 Returns TRUE upon success, FALSE otherwise. */
1941 copy_unknown_file (bfd
*ibfd
, bfd
*obfd
, off_t size
, unsigned int mode
)
1944 bfd_size_type tocopy
;
1948 non_fatal (_("stat returns negative size for `%s'"),
1949 bfd_get_archive_filename (ibfd
));
1953 if (bfd_seek (ibfd
, 0, SEEK_SET
) != 0)
1955 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1960 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1961 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1963 cbuf
= (char *) xmalloc (BUFSIZE
);
1971 if (bfd_read (cbuf
, tocopy
, ibfd
) != tocopy
)
1973 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1978 if (bfd_write (cbuf
, tocopy
, obfd
) != tocopy
)
1980 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1988 /* We should at least to be able to read it back when copying an
1989 unknown object in an archive. */
1990 chmod (bfd_get_filename (obfd
), mode
| S_IRUSR
);
1995 /* Copy unknown object file archive member IBFD onto OBFD.
1996 Returns TRUE upon success, FALSE otherwise. */
1999 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
2003 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
2005 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2009 if (!copy_unknown_file (ibfd
, obfd
, buf
.st_size
, buf
.st_mode
))
2015 typedef struct objcopy_internal_note
2017 Elf_Internal_Note note
;
2018 unsigned long padded_namesz
;
2021 } objcopy_internal_note
;
2023 #define DEBUG_MERGE 0
2026 #define merge_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
2028 #define merge_debug(format, ...)
2031 /* Returns TRUE iff PNOTE1 overlaps or adjoins PNOTE2. */
2034 overlaps_or_adjoins (objcopy_internal_note
* pnote1
,
2035 objcopy_internal_note
* pnote2
)
2037 if (pnote1
->end
< pnote2
->start
)
2038 /* FIXME: Alignment of 16 bytes taken from x86_64 binaries.
2039 Really we should extract the alignment of the section
2040 covered by the notes. */
2041 return BFD_ALIGN (pnote1
->end
, 16) < pnote2
->start
;
2043 if (pnote2
->end
< pnote2
->start
)
2044 return BFD_ALIGN (pnote2
->end
, 16) < pnote1
->start
;
2046 if (pnote1
->end
< pnote2
->end
)
2049 if (pnote2
->end
< pnote1
->end
)
2055 /* Returns TRUE iff NEEDLE is fully contained by HAYSTACK. */
2058 contained_by (objcopy_internal_note
* needle
,
2059 objcopy_internal_note
* haystack
)
2061 return needle
->start
>= haystack
->start
&& needle
->end
<= haystack
->end
;
2065 is_open_note (objcopy_internal_note
* pnote
)
2067 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_OPEN
;
2071 is_func_note (objcopy_internal_note
* pnote
)
2073 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_FUNC
;
2077 is_deleted_note (objcopy_internal_note
* pnote
)
2079 return pnote
->note
.type
== 0;
2083 is_version_note (objcopy_internal_note
* pnote
)
2085 return (pnote
->note
.namesz
> 4
2086 && pnote
->note
.namedata
[0] == 'G'
2087 && pnote
->note
.namedata
[1] == 'A'
2088 && pnote
->note
.namedata
[2] == '$'
2089 && pnote
->note
.namedata
[3] == GNU_BUILD_ATTRIBUTE_VERSION
);
2093 is_64bit (bfd
* abfd
)
2095 /* Should never happen, but let's be paranoid. */
2096 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
2099 return elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
;
2102 /* This sorting function is used to get the notes into an order
2103 that makes merging easy. */
2106 compare_gnu_build_notes (const void * data1
, const void * data2
)
2108 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2109 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2111 /* Sort notes based upon the attribute they record. */
2112 int cmp
= memcmp (pnote1
->note
.namedata
+ 3,
2113 pnote2
->note
.namedata
+ 3,
2114 pnote1
->note
.namesz
< pnote2
->note
.namesz
?
2115 pnote1
->note
.namesz
- 3 : pnote2
->note
.namesz
- 3);
2119 if (pnote1
->end
< pnote2
->start
)
2121 if (pnote1
->start
> pnote2
->end
)
2124 /* Overlaps - we should merge the two ranges. */
2125 if (pnote1
->start
< pnote2
->start
)
2127 if (pnote1
->end
> pnote2
->end
)
2129 if (pnote1
->end
< pnote2
->end
)
2132 /* Put OPEN notes before function notes. */
2133 if (is_open_note (pnote1
) && ! is_open_note (pnote2
))
2135 if (! is_open_note (pnote1
) && is_open_note (pnote2
))
2141 /* This sorting function is used to get the notes into an order
2142 that makes eliminating address ranges easier. */
2145 sort_gnu_build_notes (const void * data1
, const void * data2
)
2147 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2148 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2150 if (pnote1
->note
.type
!= pnote2
->note
.type
)
2152 /* Move deleted notes to the end. */
2153 if (is_deleted_note (pnote1
)) /* 1: OFD 2: OFD */
2156 /* Move OPEN notes to the start. */
2157 if (is_open_note (pnote1
)) /* 1: OF 2: OFD */
2160 if (is_deleted_note (pnote2
)) /* 1: F 2: O D */
2163 return 1; /* 1: F 2: O */
2166 /* Sort by starting address. */
2167 if (pnote1
->start
< pnote2
->start
)
2169 if (pnote1
->start
> pnote2
->start
)
2172 /* Then by end address (bigger range first). */
2173 if (pnote1
->end
> pnote2
->end
)
2175 if (pnote1
->end
< pnote2
->end
)
2178 /* Then by attribute type. */
2179 if (pnote1
->note
.namesz
> 4
2180 && pnote2
->note
.namesz
> 4
2181 && pnote1
->note
.namedata
[3] != pnote2
->note
.namedata
[3])
2182 return pnote1
->note
.namedata
[3] - pnote2
->note
.namedata
[3];
2187 /* Merge the notes on SEC, removing redundant entries. NEW_SIZE is
2188 set to the new, smaller size of the section. Returns true on
2189 success, false on errors that result in objcopy failing. */
2192 merge_gnu_build_notes (bfd
* abfd
,
2195 bfd_byte
* contents
,
2196 bfd_size_type
*new_size
)
2198 objcopy_internal_note
* pnotes_end
;
2199 objcopy_internal_note
* pnotes
= NULL
;
2200 objcopy_internal_note
* pnote
;
2201 bfd_size_type remain
= size
;
2202 unsigned version_1_seen
= 0;
2203 unsigned version_2_seen
= 0;
2204 unsigned version_3_seen
= 0;
2205 const char * err
= NULL
;
2206 bfd_byte
* in
= contents
;
2207 unsigned long previous_func_start
= 0;
2208 unsigned long previous_open_start
= 0;
2209 unsigned long previous_func_end
= 0;
2210 unsigned long previous_open_end
= 0;
2214 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
2220 /* If there are relocs associated with this section then we
2221 cannot safely merge it. */
2222 relpp
= (arelent
**) xmalloc (relsize
);
2223 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, isympp
);
2228 non_fatal (_("%s[%s]: Cannot merge - there are relocations against this section"),
2229 bfd_get_filename (abfd
), bfd_section_name (sec
));
2234 /* Make a copy of the notes and convert to our internal format.
2235 Minimum size of a note is 12 bytes. Also locate the version
2236 notes and check them. */
2237 pnote
= pnotes
= (objcopy_internal_note
*)
2238 xcalloc ((size
/ 12), sizeof (* pnote
));
2239 while (remain
>= 12)
2243 pnote
->note
.namesz
= bfd_get_32 (abfd
, in
);
2244 pnote
->note
.descsz
= bfd_get_32 (abfd
, in
+ 4);
2245 pnote
->note
.type
= bfd_get_32 (abfd
, in
+ 8);
2246 pnote
->padded_namesz
= (pnote
->note
.namesz
+ 3) & ~3;
2248 if (((pnote
->note
.descsz
+ 3) & ~3) != pnote
->note
.descsz
)
2250 err
= _("corrupt GNU build attribute note: description size not a factor of 4");
2254 if (pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_OPEN
2255 && pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_FUNC
)
2257 err
= _("corrupt GNU build attribute note: wrong note type");
2261 if (pnote
->padded_namesz
+ pnote
->note
.descsz
+ 12 > remain
)
2263 err
= _("corrupt GNU build attribute note: note too big");
2267 if (pnote
->note
.namesz
< 2)
2269 err
= _("corrupt GNU build attribute note: name too small");
2273 pnote
->note
.namedata
= (char *)(in
+ 12);
2274 pnote
->note
.descdata
= (char *)(in
+ 12 + pnote
->padded_namesz
);
2276 remain
-= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2277 in
+= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2279 if (pnote
->note
.namesz
> 2
2280 && pnote
->note
.namedata
[0] == '$'
2281 && pnote
->note
.namedata
[1] == GNU_BUILD_ATTRIBUTE_VERSION
2282 && pnote
->note
.namedata
[2] == '1')
2284 else if (is_version_note (pnote
))
2286 if (pnote
->note
.namedata
[4] == '2')
2288 else if (pnote
->note
.namedata
[4] == '3')
2292 err
= _("corrupt GNU build attribute note: unsupported version");
2297 switch (pnote
->note
.descsz
)
2304 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2305 /* FIXME: For version 1 and 2 notes we should try to
2306 calculate the end address by finding a symbol whose
2307 value is START, and then adding in its size.
2309 For now though, since v1 and v2 was not intended to
2310 handle gaps, we chose an artificially large end
2316 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2317 end
= bfd_get_32 (abfd
, pnote
->note
.descdata
+ 4);
2321 start
= bfd_get_64 (abfd
, pnote
->note
.descdata
);
2322 end
= bfd_get_64 (abfd
, pnote
->note
.descdata
+ 8);
2326 err
= _("corrupt GNU build attribute note: bad description size");
2331 /* This can happen with PPC64LE binaries where empty notes are
2332 encoded as start = end + 4. */
2335 if (is_open_note (pnote
))
2338 previous_open_start
= start
;
2340 pnote
->start
= previous_open_start
;
2343 previous_open_end
= end
;
2345 pnote
->end
= previous_open_end
;
2350 previous_func_start
= start
;
2352 pnote
->start
= previous_func_start
;
2355 previous_func_end
= end
;
2357 pnote
->end
= previous_func_end
;
2360 if (pnote
->note
.namedata
[pnote
->note
.namesz
- 1] != 0)
2362 err
= _("corrupt GNU build attribute note: name not NUL terminated");
2371 /* Check that the notes are valid. */
2374 err
= _("corrupt GNU build attribute notes: excess data at end");
2378 if (version_1_seen
== 0 && version_2_seen
== 0 && version_3_seen
== 0)
2381 err
= _("bad GNU build attribute notes: no known versions detected");
2384 /* This happens with glibc. No idea why. */
2385 non_fatal (_("%s[%s]: Warning: version note missing - assuming version 3"),
2386 bfd_get_filename (abfd
), bfd_section_name (sec
));
2391 if ( (version_1_seen
> 0 && version_2_seen
> 0)
2392 || (version_1_seen
> 0 && version_3_seen
> 0)
2393 || (version_2_seen
> 0 && version_3_seen
> 0))
2395 err
= _("bad GNU build attribute notes: multiple different versions");
2399 /* We are now only supporting the merging v3+ notes
2400 - it makes things much simpler. */
2401 if (version_3_seen
== 0)
2403 merge_debug ("%s: skipping merge - not using v3 notes", bfd_section_name (sec
));
2407 merge_debug ("Merging section %s which contains %ld notes\n",
2408 sec
->name
, pnotes_end
- pnotes
);
2410 /* Sort the notes. */
2411 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
),
2412 compare_gnu_build_notes
);
2415 merge_debug ("Results of initial sort:\n");
2416 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2417 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2418 (pnote
->note
.namedata
- (char *) contents
) - 12,
2419 pnote
->start
, pnote
->end
,
2421 pnote
->note
.namedata
[3],
2426 /* Now merge the notes. The rules are:
2427 1. If a note has a zero range, it can be eliminated.
2428 2. If two notes have the same namedata then:
2429 2a. If one note's range is fully covered by the other note
2430 then it can be deleted.
2431 2b. If one note's range partially overlaps or adjoins the
2432 other note then if they are both of the same type (open
2433 or func) then they can be merged and one deleted. If
2434 they are of different types then they cannot be merged. */
2435 objcopy_internal_note
* prev_note
= NULL
;
2437 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2439 /* Skip already deleted notes.
2440 FIXME: Can this happen ? We are scanning forwards and
2441 deleting backwards after all. */
2442 if (is_deleted_note (pnote
))
2445 /* Rule 1 - delete 0-range notes. */
2446 if (pnote
->start
== pnote
->end
)
2448 merge_debug ("Delete note at offset %#08lx - empty range\n",
2449 (pnote
->note
.namedata
- (char *) contents
) - 12);
2450 pnote
->note
.type
= 0;
2455 objcopy_internal_note
* back
;
2457 /* Rule 2: Check to see if there is an identical previous note. */
2458 for (iter
= 0, back
= prev_note
? prev_note
: pnote
- 1;
2462 if (is_deleted_note (back
))
2465 /* Our sorting function should have placed all identically
2466 attributed notes together, so if we see a note of a different
2467 attribute type stop searching. */
2468 if (back
->note
.namesz
!= pnote
->note
.namesz
2469 || memcmp (back
->note
.namedata
,
2470 pnote
->note
.namedata
, pnote
->note
.namesz
) != 0)
2473 if (back
->start
== pnote
->start
2474 && back
->end
== pnote
->end
)
2476 merge_debug ("Delete note at offset %#08lx - duplicate of note at offset %#08lx\n",
2477 (pnote
->note
.namedata
- (char *) contents
) - 12,
2478 (back
->note
.namedata
- (char *) contents
) - 12);
2479 pnote
->note
.type
= 0;
2484 if (contained_by (pnote
, back
))
2486 merge_debug ("Delete note at offset %#08lx - fully contained by note at %#08lx\n",
2487 (pnote
->note
.namedata
- (char *) contents
) - 12,
2488 (back
->note
.namedata
- (char *) contents
) - 12);
2489 pnote
->note
.type
= 0;
2494 /* This should not happen as we have sorted the
2495 notes with earlier starting addresses first. */
2496 if (contained_by (back
, pnote
))
2497 merge_debug ("ERROR: UNEXPECTED CONTAINMENT\n");
2501 if (overlaps_or_adjoins (back
, pnote
)
2502 && is_func_note (back
) == is_func_note (pnote
))
2504 merge_debug ("Delete note at offset %#08lx - merge into note at %#08lx\n",
2505 (pnote
->note
.namedata
- (char *) contents
) - 12,
2506 (back
->note
.namedata
- (char *) contents
) - 12);
2508 back
->end
= back
->end
> pnote
->end
? back
->end
: pnote
->end
;
2509 back
->start
= back
->start
< pnote
->start
? back
->start
: pnote
->start
;
2510 pnote
->note
.type
= 0;
2514 /* Don't scan too far back however. */
2517 /* FIXME: Not sure if this can ever be triggered. */
2518 merge_debug ("ITERATION LIMIT REACHED\n");
2523 if (! is_deleted_note (pnote
))
2525 /* Keep a pointer to this note, so that we can
2526 start the next search for rule 2 matches here. */
2529 merge_debug ("Unable to do anything with note at %#08lx\n",
2530 (pnote
->note
.namedata
- (char *) contents
) - 12);
2535 /* Resort the notes. */
2536 merge_debug ("Final sorting of notes\n");
2537 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
), sort_gnu_build_notes
);
2539 /* Reconstruct the ELF notes. */
2540 bfd_byte
* new_contents
;
2542 bfd_vma prev_start
= 0;
2543 bfd_vma prev_end
= 0;
2545 /* Not sure how, but the notes might grow in size.
2546 (eg see PR 1774507). Allow for this here. */
2547 new = new_contents
= xmalloc (size
* 2);
2548 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2550 if (! is_deleted_note (pnote
))
2552 /* Create the note, potentially using the
2553 address range of the previous note. */
2554 if (pnote
->start
== prev_start
&& pnote
->end
== prev_end
)
2556 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2557 bfd_put_32 (abfd
, 0, new + 4);
2558 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2560 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2561 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2562 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2563 new += pnote
->padded_namesz
;
2567 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2568 bfd_put_32 (abfd
, is_64bit (abfd
) ? 16 : 8, new + 4);
2569 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2571 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2572 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2573 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2574 new += pnote
->padded_namesz
;
2575 if (is_64bit (abfd
))
2577 bfd_put_64 (abfd
, pnote
->start
, new);
2578 bfd_put_64 (abfd
, pnote
->end
, new + 8);
2583 bfd_put_32 (abfd
, pnote
->start
, new);
2584 bfd_put_32 (abfd
, pnote
->end
, new + 4);
2588 prev_start
= pnote
->start
;
2589 prev_end
= pnote
->end
;
2595 merge_debug ("Results of merge:\n");
2596 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2597 if (! is_deleted_note (pnote
))
2598 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2599 (pnote
->note
.namedata
- (char *) contents
) - 12,
2600 pnote
->start
, pnote
->end
,
2602 pnote
->note
.namedata
[3],
2607 size_t nsize
= new - new_contents
;
2611 memcpy (contents
, new_contents
, nsize
);
2613 free (new_contents
);
2618 bfd_set_error (bfd_error_bad_value
);
2619 bfd_nonfatal_message (NULL
, abfd
, sec
, err
);
2627 check_new_section_flags (flagword flags
, bfd
*abfd
, const char * secname
)
2629 /* Only set the SEC_COFF_SHARED flag on COFF files.
2630 The same bit value is used by ELF targets to indicate
2631 compressed sections, and setting that flag here breaks
2633 if ((flags
& SEC_COFF_SHARED
)
2634 && bfd_get_flavour (abfd
) != bfd_target_coff_flavour
)
2636 non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"),
2637 bfd_get_filename (abfd
), secname
);
2638 flags
&= ~ SEC_COFF_SHARED
;
2641 /* Report a fatal error if 'large' is used with a non-x86-64 ELF target.
2642 Suppress the error for non-ELF targets to allow -O binary and formats that
2643 use the bit value SEC_ELF_LARGE for other purposes. */
2644 if ((flags
& SEC_ELF_LARGE
) != 0
2645 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
2646 && get_elf_backend_data (abfd
)->elf_machine_code
!= EM_X86_64
)
2648 fatal (_ ("%s[%s]: 'large' flag is ELF x86-64 specific"),
2649 bfd_get_filename (abfd
), secname
);
2650 flags
&= ~SEC_ELF_LARGE
;
2657 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
2659 /* This is only relevant to Coff targets. */
2660 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
2663 && bfd_get_flavour (input_bfd
) == bfd_target_coff_flavour
)
2664 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
2665 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
2669 /* Copy object file IBFD onto OBFD.
2670 Returns TRUE upon success, FALSE otherwise. */
2673 copy_object (bfd
*ibfd
, bfd
*obfd
, const bfd_arch_info_type
*input_arch
)
2677 asection
**osections
= NULL
;
2679 asection
*gnu_debuglink_section
= NULL
;
2680 bfd_size_type
*gaps
= NULL
;
2681 bfd_size_type max_gap
= 0;
2684 enum bfd_architecture iarch
;
2686 unsigned int num_sec
, i
;
2688 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
2689 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
2690 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
2692 /* PR 17636: Call non-fatal so that we return to our parent who
2693 may need to tidy temporary files. */
2694 non_fatal (_("unable to change endianness of '%s'"),
2695 bfd_get_archive_filename (ibfd
));
2699 if (ibfd
->read_only
)
2701 non_fatal (_("unable to modify '%s' due to errors"),
2702 bfd_get_archive_filename (ibfd
));
2706 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2708 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
2712 if (ibfd
->sections
== NULL
)
2714 non_fatal (_("error: the input file '%s' has no sections"),
2715 bfd_get_archive_filename (ibfd
));
2719 /* This is a no-op on non-Coff targets. */
2720 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2722 /* Set the Verilog output endianness based upon the input file's
2723 endianness. We may not be producing verilog format output,
2724 but testing this just adds extra code this is not really
2726 VerilogDataEndianness
= ibfd
->xvec
->byteorder
;
2728 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
2730 if (strip_section_headers
)
2732 ibfd
->flags
|= BFD_NO_SECTION_HEADER
;
2733 strip_symbols
= STRIP_ALL
;
2739 if ((do_debug_sections
& compress
) != 0
2740 && do_debug_sections
!= compress
)
2742 non_fatal (_ ("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi|"
2743 "zstd] is unsupported on `%s'"),
2744 bfd_get_archive_filename (ibfd
));
2748 if (do_elf_stt_common
)
2750 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
2751 bfd_get_archive_filename (ibfd
));
2755 if (strip_section_headers
)
2757 non_fatal (_("--strip-section-headers is unsupported on `%s'"),
2758 bfd_get_archive_filename (ibfd
));
2764 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
2765 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
2766 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2775 start
= bfd_get_start_address (ibfd
);
2776 start
+= change_start
;
2779 /* Neither the start address nor the flags
2780 need to be set for a core file. */
2781 if (bfd_get_format (obfd
) != bfd_core
)
2785 flags
= bfd_get_file_flags (ibfd
);
2786 flags
|= bfd_flags_to_set
;
2787 flags
&= ~bfd_flags_to_clear
;
2788 flags
&= bfd_applicable_file_flags (obfd
);
2790 if (strip_symbols
== STRIP_ALL
)
2791 flags
&= ~HAS_RELOC
;
2793 if (!bfd_set_start_address (obfd
, start
)
2794 || !bfd_set_file_flags (obfd
, flags
))
2796 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2801 /* Copy architecture of input file to output file. */
2802 iarch
= bfd_get_arch (ibfd
);
2803 imach
= bfd_get_mach (ibfd
);
2806 if (iarch
== bfd_arch_unknown
)
2808 iarch
= input_arch
->arch
;
2809 imach
= input_arch
->mach
;
2812 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
2813 bfd_get_archive_filename (ibfd
));
2815 if (iarch
== bfd_arch_unknown
2816 && bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2817 && bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2819 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
2823 if (iarch
== bfd_arch_unknown
2824 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
2825 && ibfd
->target_defaulted
)
2827 non_fatal (_("Unable to recognise the architecture of the input file `%s'"),
2828 bfd_get_archive_filename (ibfd
));
2831 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
2832 && iarch
!= bfd_arch_unknown
)
2834 non_fatal (_("Output file cannot represent architecture `%s'"),
2835 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
2836 bfd_get_mach (ibfd
)));
2840 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2842 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2846 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2847 && bfd_pei_p (obfd
))
2849 /* Set up PE parameters. */
2850 pe_data_type
*pe
= pe_data (obfd
);
2852 /* Copy PE parameters before changing them. */
2853 if (bfd_get_flavour (ibfd
) == bfd_target_coff_flavour
2854 && bfd_pei_p (ibfd
))
2856 pe
->pe_opthdr
= pe_data (ibfd
)->pe_opthdr
;
2859 pe
->timestamp
= pe_data (ibfd
)->coff
.timestamp
;
2864 if (pe_file_alignment
!= (bfd_vma
) -1)
2865 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
2867 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
2869 if (pe_heap_commit
!= (bfd_vma
) -1)
2870 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
2872 if (pe_heap_reserve
!= (bfd_vma
) -1)
2873 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
2875 if (pe_image_base
!= (bfd_vma
) -1)
2876 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
2878 if (pe_section_alignment
!= (bfd_vma
) -1)
2879 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
2881 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
2883 if (pe_stack_commit
!= (bfd_vma
) -1)
2884 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
2886 if (pe_stack_reserve
!= (bfd_vma
) -1)
2887 pe
->pe_opthdr
.SizeOfStackReserve
= pe_stack_reserve
;
2889 if (pe_subsystem
!= -1)
2890 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
2892 if (pe_major_subsystem_version
!= -1)
2893 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
2895 if (pe_minor_subsystem_version
!= -1)
2896 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
2898 if (pe_file_alignment
> pe_section_alignment
)
2900 non_fatal (_("warning: file alignment (0x%" PRIx64
2901 ") > section alignment (0x%" PRIx64
")"),
2902 (uint64_t) pe_file_alignment
,
2903 (uint64_t) pe_section_alignment
);
2909 if (osympp
!= isympp
)
2915 symsize
= bfd_get_symtab_upper_bound (ibfd
);
2918 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2922 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
2923 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
2926 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2929 /* PR 17512: file: d6323821
2930 If the symbol table could not be loaded do not pretend that we have
2931 any symbols. This trips us up later on when we load the relocs. */
2935 osympp
= isympp
= NULL
;
2938 /* BFD mandates that all output sections be created and sizes set before
2939 any output is done. Thus, we traverse all sections multiple times. */
2940 for (asection
*s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2941 if (!setup_section (ibfd
, s
, obfd
))
2944 if (!extract_symbol
)
2946 if (!setup_bfd_headers (ibfd
, obfd
))
2950 if (add_sections
!= NULL
)
2952 struct section_add
*padd
;
2953 struct section_list
*pset
;
2955 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
2959 pset
= find_section_list (padd
->name
, false,
2960 SECTION_CONTEXT_SET_FLAGS
);
2963 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
2964 flags
= check_new_section_flags (flags
, obfd
, padd
->name
);
2967 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
2969 /* bfd_make_section_with_flags() does not return very helpful
2970 error codes, so check for the most likely user error first. */
2971 if (bfd_get_section_by_name (obfd
, padd
->name
))
2973 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2974 _("can't add section '%s'"), padd
->name
);
2979 /* We use LINKER_CREATED here so that the backend hooks
2980 will create any special section type information,
2981 instead of presuming we know what we're doing merely
2982 because we set the flags. */
2983 padd
->section
= bfd_make_section_with_flags
2984 (obfd
, padd
->name
, flags
| SEC_LINKER_CREATED
);
2985 if (padd
->section
== NULL
)
2987 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2988 _("can't create section `%s'"),
2994 if (!bfd_set_section_size (padd
->section
, padd
->size
))
2996 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3000 pset
= find_section_list (padd
->name
, false,
3001 SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
);
3003 && !bfd_set_section_vma (padd
->section
, pset
->vma_val
))
3005 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3009 pset
= find_section_list (padd
->name
, false,
3010 SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
);
3013 padd
->section
->lma
= pset
->lma_val
;
3015 if (!bfd_set_section_alignment
3016 (padd
->section
, bfd_section_alignment (padd
->section
)))
3018 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3025 if (update_sections
!= NULL
)
3027 struct section_add
*pupdate
;
3029 for (pupdate
= update_sections
;
3031 pupdate
= pupdate
->next
)
3033 pupdate
->section
= bfd_get_section_by_name (ibfd
, pupdate
->name
);
3034 if (pupdate
->section
== NULL
)
3036 non_fatal (_("error: %s not found, can't be updated"), pupdate
->name
);
3040 osec
= pupdate
->section
->output_section
;
3041 if (!bfd_set_section_size (osec
, pupdate
->size
))
3043 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
3049 merged_note_section
* merged_note_sections
= NULL
;
3052 /* This palaver is necessary because we must set the output
3053 section size first, before its contents are ready. */
3054 for (osec
= ibfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
3056 if (! is_mergeable_note_section (ibfd
, osec
))
3059 /* If the section is going to be completly deleted then
3060 do not bother to merge it. */
3061 if (osec
->output_section
== NULL
)
3064 bfd_size_type size
= bfd_section_size (osec
);
3067 /* This can happen, eg when stripping a binary for a second
3068 time. See BZ 2121365 for an example. */
3071 merged_note_section
* merged
= xmalloc (sizeof * merged
);
3072 merged
->contents
= NULL
;
3073 if (! bfd_get_full_section_contents (ibfd
, osec
, & merged
->contents
))
3075 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3076 _("warning: could not load note section"));
3081 if (!merge_gnu_build_notes (ibfd
, osec
, size
,
3082 merged
->contents
, &merged
->size
))
3084 free (merged
->contents
);
3089 /* FIXME: Once we have read the contents in, we must write
3090 them out again. So even if the merging has achieved
3091 nothing we still add this entry to the merge list. */
3093 if (size
!= merged
->size
3094 && !bfd_set_section_size (osec
->output_section
, merged
->size
))
3096 bfd_nonfatal_message (NULL
, obfd
, osec
,
3097 _("warning: failed to set merged notes size"));
3098 free (merged
->contents
);
3103 /* Add section to list of merged sections. */
3105 merged
->next
= merged_note_sections
;
3106 merged_note_sections
= merged
;
3110 if (dump_sections
!= NULL
)
3112 struct section_add
* pdump
;
3114 for (pdump
= dump_sections
; pdump
!= NULL
; pdump
= pdump
->next
)
3119 osec
= bfd_get_section_by_name (ibfd
, pdump
->name
);
3122 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3123 _("can't dump section '%s' - it does not exist"),
3128 if ((bfd_section_flags (osec
) & SEC_HAS_CONTENTS
) == 0)
3130 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3131 _("can't dump section - it has no contents"));
3135 bfd_size_type size
= bfd_section_size (osec
);
3136 /* Note - we allow the dumping of zero-sized sections,
3137 creating an empty file. */
3139 f
= fopen (pdump
->filename
, FOPEN_WB
);
3142 bfd_nonfatal_message (pdump
->filename
, NULL
, NULL
,
3143 _("could not open section dump file"));
3147 if (bfd_malloc_and_get_section (ibfd
, osec
, &contents
))
3149 if (size
!= 0 && fwrite (contents
, 1, size
, f
) != size
)
3151 non_fatal (_("error writing section contents to %s (error: %s)"),
3160 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3161 _("could not retrieve section contents"));
3168 if (gnu_debuglink_filename
!= NULL
)
3170 /* PR 15125: Give a helpful warning message if
3171 the debuglink section already exists, and
3172 allow the rest of the copy to complete. */
3173 if (bfd_get_section_by_name (obfd
, ".gnu_debuglink"))
3175 non_fatal (_("%s: debuglink section already exists"),
3176 bfd_get_filename (ibfd
));
3177 gnu_debuglink_filename
= NULL
;
3181 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
3182 (obfd
, gnu_debuglink_filename
);
3184 if (gnu_debuglink_section
== NULL
)
3186 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3187 _("cannot create debug link section `%s'"),
3188 gnu_debuglink_filename
);
3192 /* Special processing for PE format files. We
3193 have no way to distinguish PE from COFF here. */
3194 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
3196 bfd_vma debuglink_vma
;
3197 asection
* highest_section
;
3199 /* The PE spec requires that all sections be adjacent and sorted
3200 in ascending order of VMA. It also specifies that debug
3201 sections should be last. This is despite the fact that debug
3202 sections are not loaded into memory and so in theory have no
3205 This means that the debuglink section must be given a non-zero
3206 VMA which makes it contiguous with other debug sections. So
3207 walk the current section list, find the section with the
3208 highest VMA and start the debuglink section after that one. */
3209 for (osec
= obfd
->sections
, highest_section
= NULL
;
3213 && (highest_section
== NULL
3214 || osec
->vma
> highest_section
->vma
))
3215 highest_section
= osec
;
3217 if (highest_section
)
3218 debuglink_vma
= BFD_ALIGN (highest_section
->vma
3219 + highest_section
->size
,
3220 /* FIXME: We ought to be using
3221 COFF_PAGE_SIZE here or maybe
3222 bfd_section_alignment() (if it
3223 was set) but since this is for PE
3224 and we know the required alignment
3225 it is easier just to hard code it. */
3228 /* Umm, not sure what to do in this case. */
3229 debuglink_vma
= 0x1000;
3231 bfd_set_section_vma (gnu_debuglink_section
, debuglink_vma
);
3236 num_sec
= bfd_count_sections (obfd
);
3238 && (gap_fill_set
|| pad_to_set
))
3240 /* We must fill in gaps between the sections and/or we must pad
3241 the last section to a specified address. We do this by
3242 grabbing a list of the sections, sorting them by VMA, and
3243 increasing the section sizes as required to fill the gaps.
3244 We write out the gap contents below. */
3246 osections
= xmalloc (num_sec
* sizeof (*osections
));
3247 asection
**set
= osections
;
3248 for (asection
*s
= obfd
->sections
; s
!= NULL
; s
= s
->next
)
3251 qsort (osections
, num_sec
, sizeof (*osections
), compare_section_lma
);
3253 gaps
= xmalloc (num_sec
* sizeof (*gaps
));
3254 memset (gaps
, 0, num_sec
* sizeof (*gaps
));
3258 for (i
= 0; i
< num_sec
- 1; i
++)
3261 bfd_size_type size
; /* Octets. */
3262 bfd_vma gap_start
, gap_stop
; /* Octets. */
3263 unsigned int opb1
= bfd_octets_per_byte (obfd
, osections
[i
]);
3264 unsigned int opb2
= bfd_octets_per_byte (obfd
, osections
[i
+1]);
3266 flags
= bfd_section_flags (osections
[i
]);
3267 if ((flags
& SEC_HAS_CONTENTS
) == 0
3268 || (flags
& SEC_LOAD
) == 0)
3271 size
= bfd_section_size (osections
[i
]);
3272 gap_start
= bfd_section_lma (osections
[i
]) * opb1
+ size
;
3273 gap_stop
= bfd_section_lma (osections
[i
+ 1]) * opb2
;
3274 if (gap_start
< gap_stop
)
3276 if (!bfd_set_section_size (osections
[i
],
3277 size
+ (gap_stop
- gap_start
)))
3279 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
3280 _("Can't fill gap after section"));
3283 gaps
[i
] = gap_stop
- gap_start
;
3284 if (max_gap
< gap_stop
- gap_start
)
3285 max_gap
= gap_stop
- gap_start
;
3292 bfd_vma lma
; /* Octets. */
3293 bfd_size_type size
; /* Octets. */
3294 unsigned int opb
= bfd_octets_per_byte (obfd
, osections
[num_sec
- 1]);
3295 bfd_vma _pad_to
= pad_to
* opb
;
3297 lma
= bfd_section_lma (osections
[num_sec
- 1]) * opb
;
3298 size
= bfd_section_size (osections
[num_sec
- 1]);
3299 if (lma
+ size
< _pad_to
)
3301 if (!bfd_set_section_size (osections
[num_sec
- 1], _pad_to
- lma
))
3303 bfd_nonfatal_message (NULL
, obfd
, osections
[num_sec
- 1],
3304 _("can't add padding"));
3309 gaps
[num_sec
- 1] = _pad_to
- (lma
+ size
);
3310 if (max_gap
< _pad_to
- (lma
+ size
))
3311 max_gap
= _pad_to
- (lma
+ size
);
3317 /* Symbol filtering must happen after the output sections
3318 have been created, but before their contents are set. */
3320 if (convert_debugging
)
3321 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, false);
3323 if ((obfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0
3324 && (obfd
->flags
& HAS_RELOC
) == 0)
3326 if (bfd_keep_unused_section_symbols (obfd
) || keep_section_symbols
)
3328 /* Non-relocatable inputs may not have the unused section
3329 symbols. Mark all section symbols as used to generate
3332 for (asect
= obfd
->sections
; asect
!= NULL
; asect
= asect
->next
)
3334 asect
->symbol
->flags
|= BSF_SECTION_SYM_USED
;
3338 /* Non-relocatable inputs may have the unused section symbols.
3339 Mark all section symbols as unused to excluded them. */
3341 for (s
= 0; s
< symcount
; s
++)
3342 if ((isympp
[s
]->flags
& BSF_SECTION_SYM_USED
))
3343 isympp
[s
]->flags
&= ~BSF_SECTION_SYM_USED
;
3347 if (strip_symbols
== STRIP_DEBUG
3348 || strip_symbols
== STRIP_ALL
3349 || strip_symbols
== STRIP_UNNEEDED
3350 || strip_symbols
== STRIP_NONDEBUG
3351 || strip_symbols
== STRIP_DWO
3352 || strip_symbols
== STRIP_NONDWO
3353 || discard_locals
!= LOCALS_UNDEF
3355 || htab_elements (strip_specific_htab
) != 0
3356 || htab_elements (keep_specific_htab
) != 0
3357 || htab_elements (localize_specific_htab
) != 0
3358 || htab_elements (globalize_specific_htab
) != 0
3359 || htab_elements (keepglobal_specific_htab
) != 0
3360 || htab_elements (weaken_specific_htab
) != 0
3361 || htab_elements (redefine_specific_htab
) != 0
3362 || prefix_symbols_string
3365 || convert_debugging
3366 || change_leading_char
3367 || remove_leading_char
3368 || section_rename_list
3372 /* Mark symbols used in output relocations so that they
3373 are kept, even if they are local labels or static symbols.
3375 Note we iterate over the input sections examining their
3376 relocations since the relocations for the output sections
3377 haven't been set yet. mark_symbols_used_in_relocations will
3378 ignore input sections which have no corresponding output
3380 if (strip_symbols
!= STRIP_ALL
)
3382 bfd_set_error (bfd_error_no_error
);
3383 for (asection
*s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3384 if (!mark_symbols_used_in_relocations (ibfd
, s
, isympp
)
3385 || bfd_get_error () != bfd_error_no_error
)
3389 osympp
= (asymbol
**) xmalloc ((symcount
+ add_symbols
+ 1) * sizeof (asymbol
*));
3390 if (!filter_symbols (ibfd
, obfd
, osympp
, isympp
, &symcount
))
3394 for (long s
= 0; s
< symcount
; s
++)
3395 if (!bfd_copy_private_symbol_data (ibfd
, osympp
+ s
, obfd
, osympp
+ s
))
3398 if (dhandle
!= NULL
)
3400 if (!write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
3404 bfd_set_symtab (obfd
, osympp
, symcount
);
3406 /* This has to happen before section positions are set. */
3407 for (asection
*s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3408 if (!copy_relocations_in_section (ibfd
, s
, obfd
))
3411 /* This has to happen after the symbol table has been set. */
3412 for (asection
*s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3413 if (!copy_section (ibfd
, s
, obfd
))
3416 if (add_sections
!= NULL
)
3418 struct section_add
*padd
;
3420 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
3422 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
3425 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3431 if (update_sections
!= NULL
)
3433 struct section_add
*pupdate
;
3435 for (pupdate
= update_sections
;
3437 pupdate
= pupdate
->next
)
3439 osec
= pupdate
->section
->output_section
;
3440 if (! bfd_set_section_contents (obfd
, osec
, pupdate
->contents
,
3443 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
3449 if (merged_note_sections
!= NULL
)
3451 merged_note_section
* merged
= NULL
;
3453 for (osec
= obfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
3455 if (! is_mergeable_note_section (obfd
, osec
))
3459 merged
= merged_note_sections
;
3461 /* It is likely that output sections are in the same order
3462 as the input sections, but do not assume that this is
3464 if (merged
->sec
->output_section
!= osec
)
3466 for (merged
= merged_note_sections
;
3468 merged
= merged
->next
)
3469 if (merged
->sec
->output_section
== osec
)
3474 bfd_nonfatal_message
3476 _("error: failed to locate merged notes"));
3481 if (merged
->contents
== NULL
)
3483 bfd_nonfatal_message
3485 _("error: failed to merge notes"));
3489 if (! bfd_set_section_contents (obfd
, osec
, merged
->contents
, 0,
3492 bfd_nonfatal_message
3494 _("error: failed to copy merged notes into output"));
3498 merged
= merged
->next
;
3501 /* Free the memory. */
3502 while (merged_note_sections
!= NULL
)
3504 merged_note_section
*next
= merged_note_sections
->next
;
3505 free (merged_note_sections
->contents
);
3506 free (merged_note_sections
);
3507 merged_note_sections
= next
;
3510 else if (merge_notes
&& ! is_strip
&& ! strip_section_headers
)
3511 non_fatal (_("%s: Could not find any mergeable note sections"),
3512 bfd_get_filename (ibfd
));
3514 if (gnu_debuglink_filename
!= NULL
)
3516 if (! bfd_fill_in_gnu_debuglink_section
3517 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
3519 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3520 _("cannot fill debug link section `%s'"),
3521 gnu_debuglink_filename
);
3530 /* Fill in the gaps. */
3533 buf
= (bfd_byte
*) xmalloc (max_gap
);
3534 memset (buf
, gap_fill
, max_gap
);
3536 for (i
= 0; i
< num_sec
; i
++)
3544 off
= bfd_section_size (osections
[i
]) - left
;
3555 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
3558 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
3576 /* Allow the BFD backend to copy any private data it understands
3577 from the input BFD to the output BFD. This is done last to
3578 permit the routine to look at the filtered symbol table, which is
3579 important for the ECOFF code at least. */
3580 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
3582 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3583 _("error copying private BFD data"));
3587 /* Switch to the alternate machine code. We have to do this at the
3588 very end, because we only initialize the header when we create
3589 the first section. */
3590 if (use_alt_mach_code
!= 0)
3592 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
3594 non_fatal (_("this target does not support %lu alternative machine codes"),
3596 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
3598 non_fatal (_("treating that number as an absolute e_machine value instead"));
3599 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
3602 non_fatal (_("ignoring the alternative value"));
3609 while (merged_note_sections
!= NULL
)
3611 merged_note_section
*next
= merged_note_sections
->next
;
3612 free (merged_note_sections
->contents
);
3613 free (merged_note_sections
);
3614 merged_note_sections
= next
;
3621 /* Read each archive element in turn from IBFD, copy the
3622 contents to temp file, and keep the temp file handle.
3623 If 'force_output_target' is TRUE then make sure that
3624 all elements in the new archive are of the type
3628 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
3629 bool force_output_target
,
3630 const bfd_arch_info_type
*input_arch
)
3634 struct name_list
*next
;
3638 bfd
**ptr
= &obfd
->archive_head
;
3646 /* PR 24281: It is not clear what should happen when copying a thin archive.
3647 One part is straight forward - if the output archive is in a different
3648 directory from the input archive then any relative paths in the library
3649 should be adjusted to the new location. But if any transformation
3650 options are active (eg strip, rename, add, etc) then the implication is
3651 that these should be applied to the files pointed to by the archive.
3652 But since objcopy is not destructive, this means that new files must be
3653 created, and there is no guidance for the names of the new files. (Plus
3654 this conflicts with one of the goals of thin libraries - only taking up
3655 a minimal amount of space in the file system).
3657 So for now we fail if an attempt is made to copy such libraries. */
3658 if (ibfd
->is_thin_archive
)
3661 bfd_set_error (bfd_error_invalid_operation
);
3662 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3663 _("sorry: copying thin archives is not currently supported"));
3664 goto cleanup_and_exit
;
3667 /* Make a temp directory to hold the contents. */
3668 dir
= make_tempdir (bfd_get_filename (obfd
));
3670 fatal (_("cannot create tempdir for archive copying (error: %s)"),
3673 if (strip_symbols
== STRIP_ALL
)
3674 obfd
->has_armap
= false;
3676 obfd
->has_armap
= ibfd
->has_armap
;
3677 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
3680 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
3682 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
3684 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
3687 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
3688 goto cleanup_and_exit
;
3691 while (ok
&& this_element
!= NULL
)
3694 bfd
*output_element
;
3696 int stat_status
= 0;
3698 const char *element_name
;
3700 element_name
= bfd_get_filename (this_element
);
3701 /* PR binutils/17533: Do not allow directory traversal
3702 outside of the current directory tree by archive members. */
3703 if (! is_valid_archive_path (element_name
))
3705 non_fatal (_("warning: illegal pathname found in archive member: %s"),
3707 /* PR binutils/31250: But there tools which create archives
3708 containing absolute paths, so instead of failing here, try to
3709 create a suitable alternative pathname. */
3710 element_name
= lbasename (element_name
);
3711 non_fatal (_("warning: using the basename of the member instead: %s"),
3715 /* Create an output file for this member. */
3716 output_name
= concat (dir
, "/", element_name
, (char *) 0);
3718 /* If the file already exists, make another temp dir. */
3719 if (stat (output_name
, &buf
) >= 0)
3721 char * tmpdir
= make_tempdir (output_name
);
3726 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
3728 bfd_close (this_element
);
3730 goto cleanup_and_exit
;
3733 struct name_list
*l
= xmalloc (sizeof (*l
));
3738 output_name
= concat (tmpdir
, "/", element_name
, (char *) 0);
3743 memset (&buf
, 0, sizeof (buf
));
3744 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
3746 if (stat_status
!= 0)
3747 non_fatal (_("internal stat error on %s"), element_name
);
3750 struct name_list
*l
= xmalloc (sizeof (*l
));
3751 l
->name
= output_name
;
3756 this_element
->plugin_format
= bfd_plugin_no
;
3757 ok_object
= bfd_check_format (this_element
, bfd_object
);
3759 /* PR binutils/3110: Cope with archives
3760 containing multiple target types. */
3761 if (force_output_target
|| !ok_object
)
3762 output_element
= bfd_openw (output_name
, output_target
);
3764 output_element
= bfd_openw (output_name
, bfd_get_target (this_element
));
3766 if (output_element
== NULL
)
3768 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3769 bfd_close (this_element
);
3771 goto cleanup_and_exit
;
3774 /* Copy slim LTO IR file as unknown object. */
3775 if (this_element
->lto_type
== lto_slim_ir_object
)
3779 ok
= copy_object (this_element
, output_element
, input_arch
);
3781 if (!ok
&& bfd_get_arch (this_element
) == bfd_arch_unknown
)
3782 /* Try again as an unknown object file. */
3787 ok
= copy_unknown_object (this_element
, output_element
);
3789 if (!(ok_object
&& ok
3790 ? bfd_close
: bfd_close_all_done
) (output_element
))
3792 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3793 /* Error in new object file. Don't change archive. */
3799 unlink (output_name
);
3802 bfd_close (this_element
);
3806 if (preserve_dates
&& stat_status
== 0)
3807 set_times (output_name
, &buf
);
3809 /* Open the newly created output file and attach to our list. */
3810 const char *targ
= force_output_target
? output_target
: NULL
;
3811 output_element
= bfd_openr (output_name
, targ
);
3813 list
->obfd
= output_element
;
3815 *ptr
= output_element
;
3816 ptr
= &output_element
->archive_next
;
3818 bfd
*last_element
= this_element
;
3819 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
3820 bfd_close (last_element
);
3826 filename
= xstrdup (bfd_get_filename (obfd
));
3827 if (!(ok
? bfd_close
: bfd_close_all_done
) (obfd
))
3830 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3835 filename
= xstrdup (bfd_get_filename (ibfd
));
3836 if (!bfd_close (ibfd
))
3839 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3844 /* Delete all the files that we opened. */
3845 struct name_list
*l
, *next
;
3846 for (l
= list
; l
!= NULL
; l
= next
)
3848 if (l
->name
!= NULL
)
3850 if (l
->obfd
== NULL
)
3868 /* The top-level control. */
3871 copy_file (const char *input_filename
, const char *output_filename
, int ofd
,
3872 struct stat
*in_stat
, const char *input_target
,
3873 const char *output_target
, const bfd_arch_info_type
*input_arch
)
3876 char **obj_matching
;
3877 char **core_matching
;
3878 off_t size
= get_file_size (input_filename
);
3879 const char *target
= input_target
;
3884 non_fatal (_("error: the input file '%s' is empty"),
3890 /* To allow us to do "strip *" without dying on the first
3891 non-object file, failures are nonfatal. */
3892 ibfd
= bfd_openr (input_filename
, target
);
3893 if (ibfd
== NULL
|| bfd_stat (ibfd
, in_stat
) != 0)
3895 if (bfd_get_error () == bfd_error_invalid_target
&& target
!= NULL
)
3896 bfd_nonfatal_message (target
, NULL
, NULL
, NULL
);
3898 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3905 switch (do_debug_sections
)
3907 case compress_gnu_zlib
:
3908 ibfd
->flags
|= BFD_COMPRESS
;
3912 /* The above two cases ought to just set BFD_COMPRESS for non-ELF
3913 but we can't tell whether a file is ELF or not until after
3914 bfd_check_format_matches. FIXME maybe: decide compression
3915 style in BFD after bfd_check_format_matches. */
3916 case compress_gabi_zlib
:
3917 ibfd
->flags
|= BFD_COMPRESS
| BFD_COMPRESS_GABI
;
3920 ibfd
->flags
|= BFD_COMPRESS
| BFD_COMPRESS_GABI
| BFD_COMPRESS_ZSTD
;
3922 fatal (_ ("--compress-debug-sections=zstd: binutils is not built with "
3927 ibfd
->flags
|= BFD_DECOMPRESS
;
3933 switch (do_elf_stt_common
)
3935 case elf_stt_common
:
3936 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
| BFD_USE_ELF_STT_COMMON
;
3939 case no_elf_stt_common
:
3940 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
;
3946 ibfd
->plugin_format
= bfd_plugin_no
;
3948 if (bfd_check_format (ibfd
, bfd_archive
))
3950 bool force_output_target
;
3953 /* bfd_get_target does not return the correct value until
3954 bfd_check_format succeeds. */
3955 if (output_target
== NULL
3956 || strcmp (output_target
, "default") == 0)
3958 output_target
= bfd_get_target (ibfd
);
3959 force_output_target
= false;
3962 force_output_target
= true;
3965 obfd
= bfd_fdopenw (output_filename
, output_target
, ofd
);
3967 obfd
= bfd_openw (output_filename
, output_target
);
3973 if (force_output_target
&& bfd_get_error () == bfd_error_invalid_target
)
3974 bfd_nonfatal_message (output_target
, NULL
, NULL
, NULL
);
3976 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3982 if (gnu_debuglink_filename
!= NULL
)
3984 non_fatal (_("--add-gnu-debuglink ignored for archive %s"),
3985 bfd_get_filename (ibfd
));
3986 gnu_debuglink_filename
= NULL
;
3989 if (!copy_archive (ibfd
, obfd
, output_target
, force_output_target
,
3995 bool ok_plugin
= false;
3996 bool ok_object
= bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
);
3997 bfd_error_type obj_error
= bfd_get_error ();
3998 bfd_error_type core_error
= bfd_error_no_error
;
4001 ok_object
= bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
);
4002 core_error
= bfd_get_error ();
4005 if (obj_error
== bfd_error_file_ambiguously_recognized
)
4006 free (obj_matching
);
4007 obj_error
= bfd_error_no_error
;
4009 else if (bfd_plugin_enabled ())
4011 /* This is for LLVM bytecode files, which are not ELF objects.
4012 Since objcopy/strip does nothing with these files except
4013 copy them whole perhaps we ought to just reject them? */
4014 bfd_find_target ("plugin", ibfd
);
4015 ibfd
->plugin_format
= bfd_plugin_unknown
;
4016 ok_plugin
= bfd_check_format (ibfd
, bfd_object
);
4020 if (obj_error
== bfd_error_file_ambiguously_recognized
)
4022 if (core_error
== bfd_error_file_ambiguously_recognized
)
4023 free (core_matching
);
4024 bfd_set_error (obj_error
);
4026 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
4027 list_matching_formats (obj_matching
);
4029 else if (core_error
== bfd_error_file_ambiguously_recognized
)
4032 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
4033 list_matching_formats (core_matching
);
4035 else if (!ok_object
&& !ok_plugin
)
4038 bfd_set_error (obj_error
);
4039 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
4045 /* bfd_get_target does not return the correct value until
4046 bfd_check_format succeeds. */
4048 && (output_target
== NULL
4049 || strcmp (output_target
, "default") == 0))
4050 output_target
= bfd_get_target (ibfd
);
4053 obfd
= bfd_fdopenw (output_filename
, output_target
, ofd
);
4055 obfd
= bfd_openw (output_filename
, output_target
);
4061 if (bfd_get_error () == bfd_error_invalid_target
)
4062 bfd_nonfatal_message (output_target
, NULL
, NULL
, NULL
);
4064 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
4070 /* Copy slim LTO IR file as unknown file. */
4071 if (ibfd
->lto_type
== lto_slim_ir_object
)
4074 ? !copy_object (ibfd
, obfd
, input_arch
)
4075 : !copy_unknown_file (ibfd
, obfd
,
4076 in_stat
->st_size
, in_stat
->st_mode
))
4079 /* PR 17512: file: 0f15796a.
4080 If the file could not be copied it may not be in a writeable
4081 state. So use bfd_close_all_done to avoid the possibility of
4082 writing uninitialised data into the file. */
4083 if (!(ok_object
&& !status
? bfd_close
: bfd_close_all_done
) (obfd
))
4086 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
4090 if (!bfd_close (ibfd
))
4093 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
4097 /* Add a name to the section renaming list. */
4100 add_section_rename (const char * old_name
, const char * new_name
,
4103 section_rename
* srename
;
4105 /* Check for conflicts first. */
4106 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
4107 if (strcmp (srename
->old_name
, old_name
) == 0)
4109 /* Silently ignore duplicate definitions. */
4110 if (strcmp (srename
->new_name
, new_name
) == 0
4111 && srename
->flags
== flags
)
4114 fatal (_("Multiple renames of section %s"), old_name
);
4117 srename
= (section_rename
*) xmalloc (sizeof (* srename
));
4119 srename
->old_name
= old_name
;
4120 srename
->new_name
= new_name
;
4121 srename
->flags
= flags
;
4122 srename
->next
= section_rename_list
;
4124 section_rename_list
= srename
;
4127 /* Check the section rename list for a new name of the input section
4128 called OLD_NAME. Returns the new name if one is found and sets
4129 RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
4132 find_section_rename (const char *old_name
, flagword
*returned_flags
)
4134 const section_rename
*srename
;
4136 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
4137 if (strcmp (srename
->old_name
, old_name
) == 0)
4139 if (returned_flags
!= NULL
&& srename
->flags
!= (flagword
) -1)
4140 *returned_flags
= srename
->flags
;
4142 return srename
->new_name
;
4148 /* Once each of the sections is copied, we may still need to do some
4149 finalization work for private section headers. Do that here. */
4152 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
4154 /* Allow the BFD backend to copy any private data it understands
4155 from the input section to the output section. */
4156 if (! bfd_copy_private_header_data (ibfd
, obfd
))
4158 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
4159 _("error in private header data"));
4163 /* All went well. */
4167 static inline signed int
4168 power_of_two (bfd_vma val
)
4170 signed int result
= 0;
4175 while ((val
& 1) == 0)
4182 /* Number has more than one 1, i.e. wasn't a power of 2. */
4188 /* Create a section in OBFD with the same
4189 name and attributes as ISECTION in IBFD. */
4192 setup_section (bfd
*ibfd
, sec_ptr isection
, bfd
*obfd
)
4194 struct section_list
*p
;
4200 const char *err
= NULL
;
4202 const char * new_name
;
4203 char *prefix
= NULL
;
4205 unsigned int alignment
;
4207 if (is_strip_section (ibfd
, isection
))
4210 /* Get the, possibly new, name of the output section. */
4211 name
= bfd_section_name (isection
);
4212 flags
= bfd_section_flags (isection
);
4213 if (bfd_get_flavour (ibfd
) != bfd_get_flavour (obfd
))
4215 flags
&= bfd_applicable_section_flags (ibfd
);
4216 flags
&= bfd_applicable_section_flags (obfd
);
4218 new_name
= find_section_rename (name
, &flags
);
4219 if (new_name
!= name
)
4222 flags
= check_new_section_flags (flags
, obfd
, name
);
4225 /* Prefix sections. */
4226 if (prefix_alloc_sections_string
4227 && (bfd_section_flags (isection
) & SEC_ALLOC
) != 0)
4228 prefix
= prefix_alloc_sections_string
;
4229 else if (prefix_sections_string
)
4230 prefix
= prefix_sections_string
;
4236 n
= (char *) xmalloc (strlen (prefix
) + strlen (name
) + 1);
4242 make_nobits
= false;
4244 p
= find_section_list (bfd_section_name (isection
), false,
4245 SECTION_CONTEXT_SET_FLAGS
);
4248 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
4249 flags
= check_new_section_flags (flags
, obfd
, bfd_section_name (isection
));
4255 /* For --extract-symbols where section sizes are zeroed, clear
4256 SEC_LOAD to indicate to coff_compute_section_file_positions that
4257 section sizes should not be adjusted for ALIGN_SECTIONS_IN_FILE.
4258 We don't want to clear SEC_HAS_CONTENTS as that will result
4259 in symbols being classified as 'B' by nm. */
4262 /* If only keeping debug sections then we'll be keeping section
4263 sizes in headers but making the sections have no contents. */
4264 else if (strip_symbols
== STRIP_NONDEBUG
4265 && (flags
& (SEC_ALLOC
| SEC_GROUP
)) != 0
4266 && !is_nondebug_keep_contents_section (ibfd
, isection
))
4267 clr
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
;
4269 if (clr
&& bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
4271 /* PR 29532: Copy group sections intact as otherwise we end up with
4272 empty groups. This prevents separate debug info files from
4273 being used with GDB, if they were based upon files that
4274 originally contained groups. */
4275 if (flags
& SEC_GROUP
)
4277 if ((clr
& SEC_HAS_CONTENTS
) != 0)
4280 /* Twiddle the input section flags so that it seems to
4281 elf.c:copy_private_bfd_data that section flags have not
4282 changed between input and output sections. This hack
4283 prevents wholesale rewriting of the program headers. */
4284 isection
->flags
&= ~clr
;
4289 if (!bfd_convert_section_setup (ibfd
, isection
, obfd
, &name
, &size
))
4292 err
= _("failed to create output section");
4296 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
4298 if (osection
== NULL
)
4300 err
= _("failed to create output section");
4305 size
= (size
+ interleave
- 1) / interleave
* copy_width
;
4306 else if (extract_symbol
)
4308 if (!bfd_set_section_size (osection
, size
))
4309 err
= _("failed to set size");
4311 bool vma_set_by_user
= false;
4313 vma
= bfd_section_vma (isection
);
4314 p
= find_section_list (bfd_section_name (isection
), false,
4315 SECTION_CONTEXT_ALTER_VMA
| SECTION_CONTEXT_SET_VMA
);
4318 if (p
->context
& SECTION_CONTEXT_SET_VMA
)
4322 vma_set_by_user
= true;
4325 vma
+= change_section_address
;
4327 if (!bfd_set_section_vma (osection
, vma
))
4328 err
= _("failed to set vma");
4330 bool lma_set_by_user
= false;
4332 lma
= isection
->lma
;
4333 p
= find_section_list (bfd_section_name (isection
), false,
4334 SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_SET_LMA
);
4337 if (p
->context
& SECTION_CONTEXT_ALTER_LMA
)
4341 lma_set_by_user
= true;
4344 lma
+= change_section_address
;
4346 osection
->lma
= lma
;
4348 p
= find_section_list (bfd_section_name (isection
), false,
4349 SECTION_CONTEXT_SET_ALIGNMENT
);
4351 alignment
= p
->alignment
;
4352 else if (pe_section_alignment
!= (bfd_vma
) -1
4353 && bfd_get_flavour (obfd
) == bfd_target_coff_flavour
4354 && bfd_pei_p (obfd
))
4355 alignment
= power_of_two (pe_section_alignment
);
4357 alignment
= bfd_section_alignment (isection
);
4359 /* FIXME: This is probably not enough. If we change the LMA we
4360 may have to recompute the header for the file as well. */
4361 if (!bfd_set_section_alignment (osection
, alignment
))
4362 err
= _("failed to set alignment");
4364 /* If the output section's VMA is not aligned
4365 and the alignment has changed
4366 and the VMA was not set by the user
4367 and the section does not have relocations associated with it
4368 then warn the user. */
4369 if (osection
->vma
!= 0
4370 && (alignment
>= sizeof (bfd_vma
) * CHAR_BIT
4371 || (osection
->vma
& (((bfd_vma
) 1 << alignment
) - 1)) != 0)
4372 && alignment
!= bfd_section_alignment (isection
)
4373 && change_section_address
== 0
4374 && ! vma_set_by_user
4375 && bfd_get_reloc_upper_bound (ibfd
, isection
) < 1)
4377 non_fatal (_("output section %s's alignment does not match its VMA"), name
);
4380 /* Similar check for a non-aligned LMA.
4381 FIXME: Since this is only an LMA, maybe it does not matter if
4382 it is not aligned ? */
4383 if (osection
->lma
!= 0
4384 && (alignment
>= sizeof (bfd_vma
) * CHAR_BIT
4385 || (osection
->lma
& (((bfd_vma
) 1 << alignment
) - 1)) != 0)
4386 && alignment
!= bfd_section_alignment (isection
)
4387 && change_section_address
== 0
4388 && ! lma_set_by_user
4389 && bfd_get_reloc_upper_bound (ibfd
, isection
) < 1)
4391 non_fatal (_("output section %s's alignment does not match its LMA"), name
);
4394 /* Copy merge entity size. */
4395 osection
->entsize
= isection
->entsize
;
4397 /* Copy compress status. */
4398 osection
->compress_status
= isection
->compress_status
;
4400 /* This used to be mangle_section; we do here to avoid using
4401 bfd_get_section_by_name since some formats allow multiple
4402 sections with the same name. */
4403 isection
->output_section
= osection
;
4404 isection
->output_offset
= 0;
4406 if ((isection
->flags
& SEC_GROUP
) != 0)
4408 asymbol
*gsym
= bfd_group_signature (isection
, isympp
);
4412 gsym
->flags
|= BSF_KEEP
;
4413 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
4414 elf_group_id (isection
) = gsym
;
4418 /* Allow the BFD backend to copy any private data it understands
4419 from the input section to the output section. */
4420 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
, NULL
))
4421 err
= _("failed to copy private data");
4424 elf_section_type (osection
) = SHT_NOBITS
;
4430 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
4434 /* Return TRUE if input section ISECTION should be skipped. */
4437 skip_section (bfd
*ibfd
, sec_ptr isection
, bool skip_copy
)
4443 /* If we have already failed earlier on,
4444 do not keep on generating complaints now. */
4451 if (is_strip_section (ibfd
, isection
))
4454 if (is_update_section (ibfd
, isection
))
4457 /* When merging a note section we skip the copying of the contents,
4458 but not the copying of the relocs associated with the contents. */
4459 if (skip_copy
&& is_mergeable_note_section (ibfd
, isection
))
4462 flags
= bfd_section_flags (isection
);
4463 if ((flags
& SEC_GROUP
) != 0)
4466 osection
= isection
->output_section
;
4467 size
= bfd_section_size (isection
);
4469 if (size
== 0 || osection
== 0)
4475 /* Add section SECTION_PATTERN to the list of sections that will have their
4476 relocations removed. */
4479 handle_remove_relocations_option (const char *section_pattern
)
4481 find_section_list (section_pattern
, true, SECTION_CONTEXT_REMOVE_RELOCS
);
4484 /* Return TRUE if ISECTION from IBFD should have its relocations removed,
4485 otherwise return FALSE. If the user has requested that relocations be
4486 removed from a section that does not have relocations then this
4487 function will still return TRUE. */
4490 discard_relocations (bfd
*ibfd ATTRIBUTE_UNUSED
, asection
*isection
)
4492 return (find_section_list (bfd_section_name (isection
), false,
4493 SECTION_CONTEXT_REMOVE_RELOCS
) != NULL
);
4496 /* Wrapper for dealing with --remove-section (-R) command line arguments.
4497 A special case is detected here, if the user asks to remove a relocation
4498 section (one starting with ".rela" or ".rel") then this removal must
4499 be done using a different technique in a relocatable object. */
4502 handle_remove_section_option (const char *section_pattern
)
4504 find_section_list (section_pattern
, true, SECTION_CONTEXT_REMOVE
);
4505 if (startswith (section_pattern
, ".rel"))
4507 section_pattern
+= 4;
4508 if (*section_pattern
== 'a')
4510 if (*section_pattern
)
4511 handle_remove_relocations_option (section_pattern
);
4513 sections_removed
= true;
4516 /* Copy relocations in input section ISECTION of IBFD to an output
4517 section with the same name in OBFDARG. If stripping then don't
4518 copy any relocation info. */
4521 copy_relocations_in_section (bfd
*ibfd
, sec_ptr isection
, bfd
*obfd
)
4528 if (skip_section (ibfd
, isection
, false))
4531 osection
= isection
->output_section
;
4533 /* Core files and DWO files do not need to be relocated. */
4534 if (bfd_get_format (obfd
) == bfd_core
4535 || strip_symbols
== STRIP_NONDWO
4536 || (strip_symbols
== STRIP_ALL
4537 && htab_elements (keep_specific_htab
) == 0)
4538 || discard_relocations (ibfd
, isection
))
4542 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4546 /* Do not complain if the target does not support relocations. */
4547 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4551 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4558 bfd_set_reloc (obfd
, osection
, NULL
, 0);
4561 if (isection
->orelocation
!= NULL
)
4563 /* Some other function has already set up the output relocs
4564 for us, so scan those instead of the default relocs. */
4565 relcount
= isection
->reloc_count
;
4566 relpp
= isection
->orelocation
;
4570 relpp
= bfd_xalloc (obfd
, relsize
);
4571 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
4574 bfd_nonfatal_message (NULL
, ibfd
, isection
,
4575 _("relocation count is negative"));
4580 if (strip_symbols
== STRIP_ALL
)
4582 /* Remove relocations which are not in
4583 keep_strip_specific_list. */
4587 for (w_relpp
= relpp
, i
= 0; i
< relcount
; i
++)
4588 /* PR 17512: file: 9e907e0c. */
4589 if (relpp
[i
]->sym_ptr_ptr
4591 && *relpp
[i
]->sym_ptr_ptr
4592 && is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
4593 keep_specific_htab
))
4594 *w_relpp
++ = relpp
[i
];
4595 relcount
= w_relpp
- relpp
;
4599 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
4604 /* Copy the data of input section ISECTION of IBFD
4605 to an output section with the same name in OBFD. */
4608 copy_section (bfd
*ibfd
, sec_ptr isection
, bfd
*obfd
)
4610 struct section_list
*p
;
4614 if (skip_section (ibfd
, isection
, true))
4617 osection
= isection
->output_section
;
4618 /* The output SHF_COMPRESSED section size is different from input if
4619 ELF classes of input and output aren't the same. We can't use
4620 the output section size since --interleave will shrink the output
4621 section. Size will be updated if the section is converted. */
4622 size
= bfd_section_size (isection
);
4624 if (bfd_section_flags (isection
) & SEC_HAS_CONTENTS
4625 && bfd_section_flags (osection
) & SEC_HAS_CONTENTS
)
4627 bfd_byte
*memhunk
= NULL
;
4629 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
)
4630 || !bfd_convert_section_contents (ibfd
, isection
, obfd
,
4633 bfd_set_section_size (osection
, 0);
4634 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4641 /* We don't handle leftover bytes (too many possible behaviors,
4642 and we don't know what the user wants). The section length
4643 must be a multiple of the number of bytes to swap. */
4644 if ((size
% reverse_bytes
) == 0)
4649 for (i
= 0; i
< size
; i
+= reverse_bytes
)
4650 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
4652 bfd_byte
*m
= (bfd_byte
*) memhunk
;
4655 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
4656 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
4660 /* User must pad the section up in order to do this. */
4661 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
4662 bfd_section_name (isection
), reverse_bytes
);
4667 /* Keep only every `copy_byte'th byte in MEMHUNK. */
4668 char *from
= (char *) memhunk
+ copy_byte
;
4669 char *to
= (char *) memhunk
;
4670 char *end
= (char *) memhunk
+ size
;
4672 bfd_size_type memhunk_size
= size
;
4674 /* If the section address is not exactly divisible by the interleave,
4675 then we must bias the from address. If the copy_byte is less than
4676 the bias, then we must skip forward one interleave, and increment
4678 int extra
= isection
->lma
% interleave
;
4680 if (copy_byte
< extra
)
4683 for (; from
< end
; from
+= interleave
)
4684 for (i
= 0; i
< copy_width
; i
++)
4686 if (&from
[i
] >= end
)
4691 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
* copy_width
;
4693 /* Don't extend the output section size. */
4694 if (size
> memhunk_size
)
4695 size
= memhunk_size
;
4697 osection
->lma
/= interleave
;
4698 if (copy_byte
< extra
)
4702 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4704 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4710 else if ((p
= find_section_list (bfd_section_name (isection
),
4711 false, SECTION_CONTEXT_SET_FLAGS
)) != NULL
4712 && (p
->flags
& SEC_HAS_CONTENTS
) != 0)
4714 void *memhunk
= xmalloc (size
);
4716 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
4717 flag--they can just remove the section entirely and add it
4718 back again. However, we do permit them to turn on the
4719 SEC_HAS_CONTENTS flag, and take it to mean that the section
4720 contents should be zeroed out. */
4722 memset (memhunk
, 0, size
);
4723 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4725 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4734 /* Sort sections by LMA. This is called via qsort, and is used when
4735 --gap-fill or --pad-to is used. We force non loadable or empty
4736 sections to the front, where they are easier to ignore. */
4739 compare_section_lma (const void *arg1
, const void *arg2
)
4741 const asection
*sec1
= *(const asection
**) arg1
;
4742 const asection
*sec2
= *(const asection
**) arg2
;
4743 flagword flags1
, flags2
;
4745 /* Sort non loadable sections to the front. */
4746 flags1
= sec1
->flags
;
4747 flags2
= sec2
->flags
;
4748 if ((flags1
& SEC_HAS_CONTENTS
) == 0
4749 || (flags1
& SEC_LOAD
) == 0)
4751 if ((flags2
& SEC_HAS_CONTENTS
) != 0
4752 && (flags2
& SEC_LOAD
) != 0)
4757 if ((flags2
& SEC_HAS_CONTENTS
) == 0
4758 || (flags2
& SEC_LOAD
) == 0)
4762 /* Sort sections by LMA. */
4763 if (sec1
->lma
> sec2
->lma
)
4765 if (sec1
->lma
< sec2
->lma
)
4768 /* Sort sections with the same LMA by size. */
4769 if (bfd_section_size (sec1
) > bfd_section_size (sec2
))
4771 if (bfd_section_size (sec1
) < bfd_section_size (sec2
))
4774 if (sec1
->id
> sec2
->id
)
4776 if (sec1
->id
< sec2
->id
)
4781 /* Mark all the symbols which will be used in output relocations with
4782 the BSF_KEEP flag so that those symbols will not be stripped.
4784 Ignore relocations which will not appear in the output file. */
4787 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, asymbol
**symbols
)
4793 /* Ignore an input section with no corresponding output section. */
4794 if (isection
->output_section
== NULL
)
4797 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4800 /* Do not complain if the target does not support relocations. */
4801 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4809 relpp
= (arelent
**) xmalloc (relsize
);
4810 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
4817 /* Examine each symbol used in a relocation. If it's not one of the
4818 special bfd section symbols, then mark it with BSF_KEEP. */
4819 for (i
= 0; i
< relcount
; i
++)
4821 /* See PRs 20923 and 20930 for reproducers for the NULL tests. */
4822 if (relpp
[i
]->sym_ptr_ptr
!= NULL
4823 && * relpp
[i
]->sym_ptr_ptr
!= NULL
4824 && *relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
4825 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
4826 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
4827 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
4834 /* Write out debugging information. */
4837 write_debugging_info (bfd
*obfd
, void *dhandle
,
4838 long *symcountp ATTRIBUTE_UNUSED
,
4839 asymbol
***symppp ATTRIBUTE_UNUSED
)
4841 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
4842 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
4844 bfd_byte
*syms
, *strings
= NULL
;
4845 bfd_size_type symsize
, stringsize
;
4846 asection
*stabsec
, *stabstrsec
;
4850 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
4855 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
4856 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
4857 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
4860 || stabstrsec
== NULL
4861 || !bfd_set_section_size (stabsec
, symsize
)
4862 || !bfd_set_section_size (stabstrsec
, stringsize
)
4863 || !bfd_set_section_alignment (stabsec
, 2)
4864 || !bfd_set_section_alignment (stabstrsec
, 0))
4866 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4867 _("can't create debugging section"));
4871 /* We can get away with setting the section contents now because
4872 the next thing the caller is going to do is copy over the
4873 real sections. We may someday have to split the contents
4874 setting out of this function. */
4876 && (!bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
4877 || !bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
4880 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4881 _("can't set debugging section contents"));
4890 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4891 _("don't know how to write debugging information for %s"),
4892 bfd_get_target (obfd
));
4896 /* If neither -D nor -U was specified explicitly,
4897 then use the configured default. */
4899 default_deterministic (void)
4901 if (deterministic
< 0)
4902 deterministic
= DEFAULT_AR_DETERMINISTIC
;
4906 strip_main (int argc
, char *argv
[])
4908 char *input_target
= NULL
;
4909 char *output_target
= NULL
;
4910 bool show_version
= false;
4911 bool formats_info
= false;
4914 char *output_file
= NULL
;
4915 bool merge_notes_set
= false;
4917 bfd_plugin_set_program_name (argv
[0]);
4919 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU",
4920 strip_options
, (int *) 0)) != EOF
)
4925 input_target
= optarg
;
4928 output_target
= optarg
;
4931 input_target
= output_target
= optarg
;
4934 handle_remove_section_option (optarg
);
4936 case OPTION_KEEP_SECTION
:
4937 find_section_list (optarg
, true, SECTION_CONTEXT_KEEP
);
4939 case OPTION_REMOVE_RELOCS
:
4940 handle_remove_relocations_option (optarg
);
4942 case OPTION_STRIP_SECTION_HEADERS
:
4943 strip_section_headers
= true;
4946 strip_symbols
= STRIP_ALL
;
4950 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
4951 strip_symbols
= STRIP_DEBUG
;
4953 case OPTION_STRIP_DWO
:
4954 strip_symbols
= STRIP_DWO
;
4956 case OPTION_STRIP_UNNEEDED
:
4957 strip_symbols
= STRIP_UNNEEDED
;
4960 add_specific_symbol (optarg
, keep_specific_htab
);
4964 merge_notes_set
= true;
4966 case OPTION_NO_MERGE_NOTES
:
4967 merge_notes
= false;
4968 merge_notes_set
= true;
4971 add_specific_symbol (optarg
, strip_specific_htab
);
4974 output_file
= optarg
;
4977 preserve_dates
= true;
4980 deterministic
= true;
4983 deterministic
= false;
4986 discard_locals
= LOCALS_ALL
;
4989 discard_locals
= LOCALS_START_L
;
4995 show_version
= true;
4997 case OPTION_FORMATS_INFO
:
4998 formats_info
= true;
5000 case OPTION_ONLY_KEEP_DEBUG
:
5001 strip_symbols
= STRIP_NONDEBUG
;
5003 case OPTION_KEEP_FILE_SYMBOLS
:
5004 keep_file_symbols
= 1;
5006 case OPTION_KEEP_SECTION_SYMBOLS
:
5007 keep_section_symbols
= true;
5009 case OPTION_PLUGIN
: /* --plugin */
5010 if (!bfd_plugin_enabled ())
5011 fatal (_("sorry - this program has been built without plugin support\n"));
5012 bfd_plugin_set_plugin (optarg
);
5015 /* We've been given a long option. */
5022 strip_usage (stdout
, 0);
5024 strip_usage (stderr
, 1);
5028 /* If the user has not expressly chosen to merge/not-merge ELF notes
5029 then enable the merging unless we are stripping debug or dwo info. */
5030 if (! merge_notes_set
5031 && (strip_symbols
== STRIP_UNDEF
5032 || strip_symbols
== STRIP_ALL
5033 || strip_symbols
== STRIP_UNNEEDED
5034 || strip_symbols
== STRIP_NONDEBUG
5035 || strip_symbols
== STRIP_NONDWO
))
5045 print_version ("strip");
5047 default_deterministic ();
5049 /* Default is to strip all symbols. */
5050 if (strip_symbols
== STRIP_UNDEF
5051 && discard_locals
== LOCALS_UNDEF
5052 && htab_elements (strip_specific_htab
) == 0)
5053 strip_symbols
= STRIP_ALL
;
5055 if (output_target
== NULL
)
5056 output_target
= input_target
;
5058 /* Check if all GCC LTO sections should be removed, assuming all LTO
5059 sections will be removed with -R .gnu.lto_.*. Remove .gnu.lto_.*
5060 sections will also remove .gnu.debuglto_.* sections.
5062 NB: Must keep .gnu.debuglto_* sections unless all GCC LTO sections
5063 will be removed to avoid undefined references to symbols in GCC LTO
5065 if (!find_section_list (".gnu.lto_.*", false, SECTION_CONTEXT_REMOVE
))
5066 find_section_list (".gnu.debuglto_*", true, SECTION_CONTEXT_KEEP
);
5070 || (output_file
!= NULL
&& (i
+ 1) < argc
))
5071 strip_usage (stderr
, 1);
5073 for (; i
< argc
; i
++)
5075 int hold_status
= status
;
5076 struct stat statbuf
;
5081 if (get_file_size (argv
[i
]) < 1)
5087 if (output_file
== NULL
5088 || filename_cmp (argv
[i
], output_file
) == 0)
5090 tmpname
= make_tempname (argv
[i
], &tmpfd
);
5092 copyfd
= dup (tmpfd
);
5095 tmpname
= output_file
;
5097 if (tmpname
== NULL
)
5099 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
5100 _("could not create temporary file to hold stripped copy"));
5106 copy_file (argv
[i
], tmpname
, tmpfd
, &statbuf
, input_target
,
5107 output_target
, NULL
);
5110 const char *oname
= output_file
? output_file
: argv
[i
];
5111 status
= smart_rename (tmpname
, oname
, copyfd
,
5112 &statbuf
, preserve_dates
) != 0;
5114 status
= hold_status
;
5120 unlink_if_ordinary (tmpname
);
5122 if (output_file
!= tmpname
)
5129 /* Set up PE subsystem. */
5132 set_pe_subsystem (const char *s
)
5134 const char *version
, *subsystem
;
5144 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
5145 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
5146 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
5147 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
5148 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
5149 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
5150 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
5151 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
5152 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
},
5153 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
5159 /* Check for the presence of a version number. */
5160 version
= strchr (s
, ':');
5161 if (version
== NULL
)
5165 int len
= version
- s
;
5169 version
= copy
+ 1 + len
;
5170 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
5172 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
5174 non_fatal (_("%s: bad version in PE subsystem"), s
);
5177 /* Check for numeric subsystem. */
5178 value
= (short) strtol (subsystem
, ©
, 0);
5181 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
5182 if (v
[i
].value
== value
)
5184 pe_subsystem
= value
;
5185 set_def
= v
[i
].set_def
;
5191 /* Search for subsystem by name. */
5192 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
5193 if (strcmp (subsystem
, v
[i
].name
) == 0)
5195 pe_subsystem
= v
[i
].value
;
5196 set_def
= v
[i
].set_def
;
5204 fatal (_("unknown PE subsystem: %s"), s
);
5209 if (pe_file_alignment
== (bfd_vma
) -1)
5210 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
5211 if (pe_section_alignment
== (bfd_vma
) -1)
5212 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
5216 free ((char *) subsystem
);
5219 /* Convert EFI target to PEI target. */
5222 convert_efi_target (char **targ
)
5226 char *efi
= *targ
+ 4;
5229 if (startswith (efi
, "app-"))
5230 subsys
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
5231 else if (startswith (efi
, "bsdrv-"))
5233 subsys
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
5236 else if (startswith (efi
, "rtdrv-"))
5238 subsys
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
5245 pei
= xmalloc (len
+ sizeof ("-little"));
5246 memcpy (pei
, efi
, len
+ 1);
5251 if (strcmp (efi
+ 4, "ia32") == 0)
5253 /* Change ia32 to i386. */
5258 else if (strcmp (efi
+ 4, "x86_64") == 0)
5260 /* Change x86_64 to x86-64. */
5263 else if (strcmp (efi
+ 4, "aarch64") == 0)
5265 /* Change aarch64 to aarch64-little. */
5266 memcpy (pei
+ 4 + sizeof ("aarch64") - 1, "-little", sizeof ("-little"));
5268 else if (strcmp (efi
+ 4, "riscv64") == 0)
5270 /* Change riscv64 to riscv64-little. */
5271 memcpy (pei
+ 4 + sizeof ("riscv64") - 1, "-little", sizeof ("-little"));
5277 /* Allocate and return a pointer to a struct section_add, initializing the
5278 structure using ARG, a string in the format "sectionname=filename".
5279 The returned structure will have its next pointer set to NEXT. The
5280 OPTION field is the name of the command line option currently being
5281 parsed, and is only used if an error needs to be reported. */
5283 static struct section_add
*
5284 init_section_add (const char *arg
,
5285 struct section_add
*next
,
5288 struct section_add
*pa
;
5291 s
= strchr (arg
, '=');
5293 fatal (_("bad format for %s"), option
);
5295 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
5296 pa
->name
= xstrndup (arg
, s
- arg
);
5297 pa
->filename
= s
+ 1;
5299 pa
->contents
= NULL
;
5305 /* Load the file specified in PA, allocating memory to hold the file
5306 contents, and store a pointer to the allocated memory in the contents
5307 field of PA. The size field of PA is also updated. All errors call
5311 section_add_load_file (struct section_add
*pa
)
5316 /* We don't use get_file_size so that we can do
5317 --add-section .note.GNU_stack=/dev/null
5318 get_file_size doesn't work on /dev/null. */
5320 f
= fopen (pa
->filename
, FOPEN_RB
);
5322 fatal (_("cannot open: %s: %s"),
5323 pa
->filename
, strerror (errno
));
5327 pa
->contents
= (bfd_byte
*) xmalloc (alloc
);
5335 pa
->contents
= (bfd_byte
*) xrealloc (pa
->contents
, alloc
);
5338 got
= fread (pa
->contents
+ off
, 1, alloc
- off
, f
);
5340 fatal (_("%s: fread failed"), pa
->filename
);
5351 copy_main (int argc
, char *argv
[])
5353 char *input_filename
= NULL
;
5354 char *output_filename
= NULL
;
5356 char *input_target
= NULL
;
5357 char *output_target
= NULL
;
5358 bool show_version
= false;
5359 bool change_warn
= true;
5360 bool formats_info
= false;
5361 bool use_globalize
= false;
5362 bool use_keep_global
= false;
5366 struct stat statbuf
;
5367 const bfd_arch_info_type
*input_arch
= NULL
;
5369 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
5370 copy_options
, (int *) 0)) != EOF
)
5375 copy_byte
= atoi (optarg
);
5377 fatal (_("byte number must be non-negative"));
5381 input_arch
= bfd_scan_arch (optarg
);
5382 if (input_arch
== NULL
)
5383 fatal (_("architecture %s unknown"), optarg
);
5389 interleave
= atoi (optarg
);
5391 fatal (_("interleave must be positive"));
5397 case OPTION_INTERLEAVE_WIDTH
:
5398 copy_width
= atoi (optarg
);
5400 fatal(_("interleave width must be positive"));
5404 case 's': /* "source" - 'I' is preferred */
5405 input_target
= optarg
;
5408 case OPTION_BINARY_SYMBOL_PREFIX
:
5409 bfd_binary_symbol_prefix
= optarg
;
5413 case 'd': /* "destination" - 'O' is preferred */
5414 output_target
= optarg
;
5418 input_target
= output_target
= optarg
;
5422 find_section_list (optarg
, true, SECTION_CONTEXT_COPY
);
5423 sections_copied
= true;
5427 handle_remove_section_option (optarg
);
5430 case OPTION_KEEP_SECTION
:
5431 find_section_list (optarg
, true, SECTION_CONTEXT_KEEP
);
5434 case OPTION_REMOVE_RELOCS
:
5435 handle_remove_relocations_option (optarg
);
5438 case OPTION_STRIP_SECTION_HEADERS
:
5439 strip_section_headers
= true;
5443 strip_symbols
= STRIP_ALL
;
5447 strip_symbols
= STRIP_DEBUG
;
5450 case OPTION_STRIP_DWO
:
5451 strip_symbols
= STRIP_DWO
;
5454 case OPTION_STRIP_UNNEEDED
:
5455 strip_symbols
= STRIP_UNNEEDED
;
5458 case OPTION_ONLY_KEEP_DEBUG
:
5459 strip_symbols
= STRIP_NONDEBUG
;
5462 case OPTION_KEEP_FILE_SYMBOLS
:
5463 keep_file_symbols
= 1;
5466 case OPTION_ADD_GNU_DEBUGLINK
:
5467 long_section_names
= ENABLE
;
5468 gnu_debuglink_filename
= optarg
;
5472 add_specific_symbol (optarg
, keep_specific_htab
);
5478 case OPTION_NO_MERGE_NOTES
:
5479 merge_notes
= false;
5483 add_specific_symbol (optarg
, strip_specific_htab
);
5486 case OPTION_STRIP_UNNEEDED_SYMBOL
:
5487 add_specific_symbol (optarg
, strip_unneeded_htab
);
5491 add_specific_symbol (optarg
, localize_specific_htab
);
5494 case OPTION_GLOBALIZE_SYMBOL
:
5495 use_globalize
= true;
5496 add_specific_symbol (optarg
, globalize_specific_htab
);
5500 use_keep_global
= true;
5501 add_specific_symbol (optarg
, keepglobal_specific_htab
);
5505 add_specific_symbol (optarg
, weaken_specific_htab
);
5509 preserve_dates
= true;
5513 deterministic
= true;
5517 deterministic
= false;
5525 discard_locals
= LOCALS_ALL
;
5529 discard_locals
= LOCALS_START_L
;
5537 show_version
= true;
5540 case OPTION_FORMATS_INFO
:
5541 formats_info
= true;
5548 case OPTION_ADD_SECTION
:
5549 add_sections
= init_section_add (optarg
, add_sections
,
5551 section_add_load_file (add_sections
);
5554 case OPTION_UPDATE_SECTION
:
5555 update_sections
= init_section_add (optarg
, update_sections
,
5556 "--update-section");
5557 section_add_load_file (update_sections
);
5560 case OPTION_DUMP_SECTION
:
5561 dump_sections
= init_section_add (optarg
, dump_sections
,
5565 case OPTION_ADD_SYMBOL
:
5568 struct addsym_node
*newsym
= xmalloc (sizeof *newsym
);
5570 newsym
->next
= NULL
;
5571 s
= strchr (optarg
, '=');
5573 fatal (_("bad format for %s"), "--add-symbol");
5574 t
= strchr (s
+ 1, ':');
5576 newsym
->symdef
= xstrndup (optarg
, s
- optarg
);
5579 newsym
->section
= xstrndup (s
+ 1, t
- (s
+ 1));
5580 newsym
->symval
= strtol (t
+ 1, NULL
, 0);
5584 newsym
->section
= NULL
;
5585 newsym
->symval
= strtol (s
+ 1, NULL
, 0);
5589 t
= strchr (t
+ 1, ',');
5590 newsym
->othersym
= NULL
;
5592 newsym
->flags
= parse_symflags (t
+1, &newsym
->othersym
);
5594 newsym
->flags
= BSF_GLOBAL
;
5596 /* Keep 'othersym' symbols at the front of the list. */
5597 if (newsym
->othersym
)
5599 newsym
->next
= add_sym_list
;
5601 add_sym_tail
= &newsym
->next
;
5602 add_sym_list
= newsym
;
5606 *add_sym_tail
= newsym
;
5607 add_sym_tail
= &newsym
->next
;
5613 case OPTION_CHANGE_START
:
5614 change_start
= parse_vma (optarg
, "--change-start");
5617 case OPTION_CHANGE_SECTION_ADDRESS
:
5618 case OPTION_CHANGE_SECTION_LMA
:
5619 case OPTION_CHANGE_SECTION_VMA
:
5621 struct section_list
* p
;
5622 unsigned int context
= 0;
5626 char *option
= NULL
;
5631 case OPTION_CHANGE_SECTION_ADDRESS
:
5632 option
= "--change-section-address";
5633 context
= SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_ALTER_VMA
;
5635 case OPTION_CHANGE_SECTION_LMA
:
5636 option
= "--change-section-lma";
5637 context
= SECTION_CONTEXT_ALTER_LMA
;
5639 case OPTION_CHANGE_SECTION_VMA
:
5640 option
= "--change-section-vma";
5641 context
= SECTION_CONTEXT_ALTER_VMA
;
5645 s
= strchr (optarg
, '=');
5648 s
= strchr (optarg
, '+');
5651 s
= strchr (optarg
, '-');
5653 fatal (_("bad format for %s"), option
);
5658 /* Correct the context. */
5661 case OPTION_CHANGE_SECTION_ADDRESS
:
5662 context
= SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_SET_VMA
;
5664 case OPTION_CHANGE_SECTION_LMA
:
5665 context
= SECTION_CONTEXT_SET_LMA
;
5667 case OPTION_CHANGE_SECTION_VMA
:
5668 context
= SECTION_CONTEXT_SET_VMA
;
5674 name
= (char *) xmalloc (len
+ 1);
5675 strncpy (name
, optarg
, len
);
5678 p
= find_section_list (name
, true, context
);
5680 val
= parse_vma (s
+ 1, option
);
5686 case OPTION_CHANGE_SECTION_ADDRESS
:
5690 case OPTION_CHANGE_SECTION_LMA
:
5694 case OPTION_CHANGE_SECTION_VMA
:
5701 case OPTION_CHANGE_ADDRESSES
:
5702 change_section_address
= parse_vma (optarg
, "--change-addresses");
5703 change_start
= change_section_address
;
5706 case OPTION_CHANGE_WARNINGS
:
5710 case OPTION_CHANGE_LEADING_CHAR
:
5711 change_leading_char
= true;
5714 case OPTION_COMPRESS_DEBUG_SECTIONS
:
5717 if (strcasecmp (optarg
, "none") == 0)
5718 do_debug_sections
= decompress
;
5719 else if (strcasecmp (optarg
, "zlib") == 0)
5720 do_debug_sections
= compress_zlib
;
5721 else if (strcasecmp (optarg
, "zlib-gnu") == 0)
5722 do_debug_sections
= compress_gnu_zlib
;
5723 else if (strcasecmp (optarg
, "zlib-gabi") == 0)
5724 do_debug_sections
= compress_gabi_zlib
;
5725 else if (strcasecmp (optarg
, "zstd") == 0)
5726 do_debug_sections
= compress_zstd
;
5728 fatal (_("unrecognized --compress-debug-sections type `%s'"),
5732 do_debug_sections
= compress
;
5735 case OPTION_DEBUGGING
:
5736 convert_debugging
= true;
5739 case OPTION_DECOMPRESS_DEBUG_SECTIONS
:
5740 do_debug_sections
= decompress
;
5743 case OPTION_ELF_STT_COMMON
:
5744 if (strcasecmp (optarg
, "yes") == 0)
5745 do_elf_stt_common
= elf_stt_common
;
5746 else if (strcasecmp (optarg
, "no") == 0)
5747 do_elf_stt_common
= no_elf_stt_common
;
5749 fatal (_("unrecognized --elf-stt-common= option `%s'"),
5753 case OPTION_GAP_FILL
:
5755 bfd_vma gap_fill_vma
;
5757 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
5758 gap_fill
= (bfd_byte
) gap_fill_vma
;
5759 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
5760 non_fatal (_("Warning: truncating gap-fill from 0x%" PRIx64
5762 (uint64_t) gap_fill_vma
, gap_fill
);
5763 gap_fill_set
= true;
5767 case OPTION_NO_CHANGE_WARNINGS
:
5768 change_warn
= false;
5772 pad_to
= parse_vma (optarg
, "--pad-to");
5776 case OPTION_REMOVE_LEADING_CHAR
:
5777 remove_leading_char
= true;
5780 case OPTION_REDEFINE_SYM
:
5782 /* Insert this redefinition onto redefine_specific_htab. */
5786 const char *nextarg
;
5787 char *source
, *target
;
5789 s
= strchr (optarg
, '=');
5791 fatal (_("bad format for %s"), "--redefine-sym");
5794 source
= (char *) xmalloc (len
+ 1);
5795 strncpy (source
, optarg
, len
);
5799 len
= strlen (nextarg
);
5800 target
= (char *) xmalloc (len
+ 1);
5801 strcpy (target
, nextarg
);
5803 add_redefine_and_check ("--redefine-sym", source
, target
);
5810 case OPTION_REDEFINE_SYMS
:
5811 add_redefine_syms_file (optarg
);
5814 case OPTION_SET_SECTION_FLAGS
:
5816 struct section_list
*p
;
5821 s
= strchr (optarg
, '=');
5823 fatal (_("bad format for %s"), "--set-section-flags");
5826 name
= (char *) xmalloc (len
+ 1);
5827 strncpy (name
, optarg
, len
);
5830 p
= find_section_list (name
, true, SECTION_CONTEXT_SET_FLAGS
);
5832 p
->flags
= parse_flags (s
+ 1);
5836 case OPTION_SET_SECTION_ALIGNMENT
:
5838 struct section_list
*p
;
5844 s
= strchr (optarg
, '=');
5846 fatal (_("bad format for --set-section-alignment: argument needed"));
5848 align
= atoi (s
+ 1);
5850 fatal (_("bad format for --set-section-alignment: numeric argument needed"));
5852 /* Convert integer alignment into a power-of-two alignment. */
5853 palign
= power_of_two (align
);
5855 fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
5857 /* Add the alignment setting to the section list. */
5859 name
= (char *) xmalloc (len
+ 1);
5860 strncpy (name
, optarg
, len
);
5863 p
= find_section_list (name
, true, SECTION_CONTEXT_SET_ALIGNMENT
);
5865 p
->alignment
= palign
;
5869 case OPTION_RENAME_SECTION
:
5872 const char *eq
, *fl
;
5877 eq
= strchr (optarg
, '=');
5879 fatal (_("bad format for %s"), "--rename-section");
5883 fatal (_("bad format for %s"), "--rename-section");
5885 old_name
= (char *) xmalloc (len
+ 1);
5886 strncpy (old_name
, optarg
, len
);
5890 fl
= strchr (eq
, ',');
5893 flags
= parse_flags (fl
+ 1);
5903 fatal (_("bad format for %s"), "--rename-section");
5905 new_name
= (char *) xmalloc (len
+ 1);
5906 strncpy (new_name
, eq
, len
);
5909 add_section_rename (old_name
, new_name
, flags
);
5913 case OPTION_SET_START
:
5914 set_start
= parse_vma (optarg
, "--set-start");
5915 set_start_set
= true;
5918 case OPTION_SREC_LEN
:
5919 _bfd_srec_len
= parse_vma (optarg
, "--srec-len");
5922 case OPTION_SREC_FORCES3
:
5923 _bfd_srec_forceS3
= true;
5926 case OPTION_STRIP_SYMBOLS
:
5927 add_specific_symbols (optarg
, strip_specific_htab
,
5928 &strip_specific_buffer
);
5931 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
5932 add_specific_symbols (optarg
, strip_unneeded_htab
,
5933 &strip_unneeded_buffer
);
5936 case OPTION_KEEP_SYMBOLS
:
5937 add_specific_symbols (optarg
, keep_specific_htab
,
5938 &keep_specific_buffer
);
5941 case OPTION_KEEP_SECTION_SYMBOLS
:
5942 keep_section_symbols
= true;
5945 case OPTION_LOCALIZE_HIDDEN
:
5946 localize_hidden
= true;
5949 case OPTION_LOCALIZE_SYMBOLS
:
5950 add_specific_symbols (optarg
, localize_specific_htab
,
5951 &localize_specific_buffer
);
5954 case OPTION_LONG_SECTION_NAMES
:
5955 if (!strcmp ("enable", optarg
))
5956 long_section_names
= ENABLE
;
5957 else if (!strcmp ("disable", optarg
))
5958 long_section_names
= DISABLE
;
5959 else if (!strcmp ("keep", optarg
))
5960 long_section_names
= KEEP
;
5962 fatal (_("unknown long section names option '%s'"), optarg
);
5965 case OPTION_GLOBALIZE_SYMBOLS
:
5966 use_globalize
= true;
5967 add_specific_symbols (optarg
, globalize_specific_htab
,
5968 &globalize_specific_buffer
);
5971 case OPTION_KEEPGLOBAL_SYMBOLS
:
5972 use_keep_global
= true;
5973 add_specific_symbols (optarg
, keepglobal_specific_htab
,
5974 &keepglobal_specific_buffer
);
5977 case OPTION_WEAKEN_SYMBOLS
:
5978 add_specific_symbols (optarg
, weaken_specific_htab
,
5979 &weaken_specific_buffer
);
5982 case OPTION_ALT_MACH_CODE
:
5983 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
5984 if (use_alt_mach_code
== 0)
5985 fatal (_("unable to parse alternative machine code"));
5988 case OPTION_PREFIX_SYMBOLS
:
5989 prefix_symbols_string
= optarg
;
5992 case OPTION_PREFIX_SECTIONS
:
5993 prefix_sections_string
= optarg
;
5996 case OPTION_PREFIX_ALLOC_SECTIONS
:
5997 prefix_alloc_sections_string
= optarg
;
6000 case OPTION_READONLY_TEXT
:
6001 bfd_flags_to_set
|= WP_TEXT
;
6002 bfd_flags_to_clear
&= ~WP_TEXT
;
6005 case OPTION_WRITABLE_TEXT
:
6006 bfd_flags_to_clear
|= WP_TEXT
;
6007 bfd_flags_to_set
&= ~WP_TEXT
;
6011 bfd_flags_to_set
|= D_PAGED
;
6012 bfd_flags_to_clear
&= ~D_PAGED
;
6016 bfd_flags_to_clear
|= D_PAGED
;
6017 bfd_flags_to_set
&= ~D_PAGED
;
6020 case OPTION_EXTRACT_DWO
:
6021 strip_symbols
= STRIP_NONDWO
;
6024 case OPTION_EXTRACT_SYMBOL
:
6025 extract_symbol
= true;
6028 case OPTION_REVERSE_BYTES
:
6030 int prev
= reverse_bytes
;
6032 reverse_bytes
= atoi (optarg
);
6033 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
6034 fatal (_("number of bytes to reverse must be positive and even"));
6036 if (prev
&& prev
!= reverse_bytes
)
6037 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
6042 case OPTION_FILE_ALIGNMENT
:
6043 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
6044 if (power_of_two (pe_file_alignment
) == -1)
6046 non_fatal (_("--file-alignment argument is not a power of two: %s - ignoring"), optarg
);
6047 pe_file_alignment
= (bfd_vma
) -1;
6054 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
6056 || (*end
!= ',' && *end
!= '\0'))
6057 non_fatal (_("%s: invalid reserve value for --heap"),
6059 else if (*end
!= '\0')
6061 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
6063 non_fatal (_("%s: invalid commit value for --heap"),
6069 case OPTION_IMAGE_BASE
:
6070 pe_image_base
= parse_vma (optarg
, "--image-base");
6073 case OPTION_PE_SECTION_ALIGNMENT
:
6074 pe_section_alignment
= parse_vma (optarg
,
6075 "--section-alignment");
6076 if (power_of_two (pe_section_alignment
) == -1)
6078 non_fatal (_("--section-alignment argument is not a power of two: %s - ignoring"), optarg
);
6079 pe_section_alignment
= (bfd_vma
) -1;
6083 case OPTION_SUBSYSTEM
:
6084 set_pe_subsystem (optarg
);
6090 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
6092 || (*end
!= ',' && *end
!= '\0'))
6093 non_fatal (_("%s: invalid reserve value for --stack"),
6095 else if (*end
!= '\0')
6097 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
6099 non_fatal (_("%s: invalid commit value for --stack"),
6105 case OPTION_VERILOG_DATA_WIDTH
:
6106 VerilogDataWidth
= parse_vma (optarg
, "--verilog-data-width");
6107 switch (VerilogDataWidth
)
6113 case 16: /* We do not support widths > 16 because the verilog
6114 data is handled internally in 16 byte wide packets. */
6117 fatal (_("error: verilog data width must be 1, 2, 4, 8 or 16"));
6122 /* We've been given a long option. */
6127 copy_usage (stdout
, 0);
6130 copy_usage (stderr
, 1);
6134 if (use_globalize
&& use_keep_global
)
6135 fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)"));
6144 print_version ("objcopy");
6146 if (interleave
&& copy_byte
== -1)
6147 fatal (_("interleave start byte must be set with --byte"));
6149 if (copy_byte
>= interleave
)
6150 fatal (_("byte number must be less than interleave"));
6152 if (copy_width
> interleave
- copy_byte
)
6153 fatal (_("interleave width must be less than or equal to interleave - byte`"));
6155 if (optind
== argc
|| optind
+ 2 < argc
)
6156 copy_usage (stderr
, 1);
6158 input_filename
= argv
[optind
];
6159 if (optind
+ 1 < argc
)
6160 output_filename
= argv
[optind
+ 1];
6162 default_deterministic ();
6164 /* Default is to strip no symbols. */
6165 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
6166 strip_symbols
= STRIP_NONE
;
6168 if (output_target
== NULL
)
6169 output_target
= input_target
;
6171 /* Convert input EFI target to PEI target. */
6172 if (input_target
!= NULL
6173 && startswith (input_target
, "efi-"))
6175 if (convert_efi_target (&input_target
) < 0)
6176 fatal (_("unknown input EFI target: %s"), input_target
);
6179 /* Convert output EFI target to PEI target. */
6180 if (output_target
!= NULL
6181 && startswith (output_target
, "efi-"))
6183 int subsys
= convert_efi_target (&output_target
);
6186 fatal (_("unknown output EFI target: %s"), output_target
);
6187 if (pe_subsystem
== -1)
6188 pe_subsystem
= subsys
;
6189 if (pe_file_alignment
== (bfd_vma
) -1)
6190 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
6191 if (pe_section_alignment
== (bfd_vma
) -1)
6192 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
6195 /* If there is no destination file, or the source and destination files
6196 are the same, then create a temp and copy the result into the input. */
6198 if (output_filename
== NULL
6199 || filename_cmp (input_filename
, output_filename
) == 0)
6201 tmpname
= make_tempname (input_filename
, &tmpfd
);
6203 copyfd
= dup (tmpfd
);
6206 tmpname
= output_filename
;
6208 if (tmpname
== NULL
)
6210 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
6211 input_filename
, strerror (errno
));
6214 copy_file (input_filename
, tmpname
, tmpfd
, &statbuf
, input_target
,
6215 output_target
, input_arch
);
6218 const char *oname
= output_filename
? output_filename
: input_filename
;
6219 status
= smart_rename (tmpname
, oname
, copyfd
,
6220 &statbuf
, preserve_dates
) != 0;
6226 unlink_if_ordinary (tmpname
);
6229 if (tmpname
!= output_filename
)
6234 struct section_list
*p
;
6236 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
6240 if (p
->context
& (SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
))
6241 /* xgettext:c-format */
6242 non_fatal (_("%s %s%c0x%" PRIx64
" never used"),
6243 "--change-section-vma",
6245 p
->context
& SECTION_CONTEXT_SET_VMA
? '=' : '+',
6246 (uint64_t) p
->vma_val
);
6248 if (p
->context
& (SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
))
6249 /* xgettext:c-format */
6250 non_fatal (_("%s %s%c0x%" PRIx64
" never used"),
6251 "--change-section-lma",
6253 p
->context
& SECTION_CONTEXT_SET_LMA
? '=' : '+',
6254 (uint64_t) p
->lma_val
);
6259 free (strip_specific_buffer
);
6260 free (strip_unneeded_buffer
);
6261 free (keep_specific_buffer
);
6262 free (localize_specific_buffer
);
6263 free (globalize_specific_buffer
);
6264 free (keepglobal_specific_buffer
);
6265 free (weaken_specific_buffer
);
6271 main (int argc
, char *argv
[])
6273 #ifdef HAVE_LC_MESSAGES
6274 setlocale (LC_MESSAGES
, "");
6276 setlocale (LC_CTYPE
, "");
6277 bindtextdomain (PACKAGE
, LOCALEDIR
);
6278 textdomain (PACKAGE
);
6280 program_name
= argv
[0];
6281 xmalloc_set_program_name (program_name
);
6283 expandargv (&argc
, &argv
);
6285 strip_symbols
= STRIP_UNDEF
;
6286 discard_locals
= LOCALS_UNDEF
;
6288 if (bfd_init () != BFD_INIT_MAGIC
)
6289 fatal (_("fatal error: libbfd ABI mismatch"));
6290 set_default_bfd_target ();
6295 size_t i
= strlen (program_name
);
6297 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
6298 /* Drop the .exe suffix, if any. */
6299 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
6302 program_name
[i
] = '\0';
6305 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
6307 #endif /* is_strip */
6309 create_symbol_htabs ();
6310 xatexit (delete_symbol_htabs
);
6313 bfd_set_error_program_name (argv
[0]);
6316 strip_main (argc
, argv
);
6318 copy_main (argc
, argv
);