1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2024 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"
34 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
35 header in generic PE code. */
36 #include "coff/i386.h"
39 static bfd_vma pe_file_alignment
= (bfd_vma
) -1;
40 static bfd_vma pe_heap_commit
= (bfd_vma
) -1;
41 static bfd_vma pe_heap_reserve
= (bfd_vma
) -1;
42 static bfd_vma pe_image_base
= (bfd_vma
) -1;
43 static bfd_vma pe_section_alignment
= (bfd_vma
) -1;
44 static bfd_vma pe_stack_commit
= (bfd_vma
) -1;
45 static bfd_vma pe_stack_reserve
= (bfd_vma
) -1;
46 static short pe_subsystem
= -1;
47 static short pe_major_subsystem_version
= -1;
48 static short pe_minor_subsystem_version
= -1;
50 struct is_specified_symbol_predicate_data
56 /* A node includes symbol name mapping to support redefine_sym. */
65 struct addsym_node
*next
;
70 const char * othersym
;
73 typedef struct section_rename
75 const char * old_name
;
76 const char * new_name
;
78 struct section_rename
* next
;
82 /* List of sections to be renamed. */
83 static section_rename
*section_rename_list
;
85 static asymbol
**isympp
= NULL
; /* Input symbols. */
86 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
88 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
89 static int copy_byte
= -1;
90 static int interleave
= 0; /* Initialised to 4 in copy_main(). */
91 static int copy_width
= 1;
93 static bool keep_section_symbols
= false ;/* True if section symbols should be retained. */
94 static bool verbose
; /* Print file and target names. */
95 static bool preserve_dates
; /* Preserve input file timestamp. */
96 static int deterministic
= -1; /* Enable deterministic archives. */
97 static int status
= 0; /* Exit status. */
99 static bool merge_notes
= false; /* Merge note sections. */
100 static bool strip_section_headers
= false;/* Strip section headers. */
102 typedef struct merged_note_section
104 asection
* sec
; /* The section that is being merged. */
105 bfd_byte
* contents
;/* New contents of the section. */
106 bfd_size_type size
; /* New size of the section. */
107 struct merged_note_section
* next
; /* Link to next merged note section. */
108 } merged_note_section
;
113 STRIP_NONE
, /* Don't strip. */
114 STRIP_DEBUG
, /* Strip all debugger symbols. */
115 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
116 STRIP_NONDEBUG
, /* Strip everything but debug info. */
117 STRIP_DWO
, /* Strip all DWO info. */
118 STRIP_NONDWO
, /* Strip everything but DWO info. */
119 STRIP_ALL
/* Strip all symbols. */
122 /* Which symbols to remove. */
123 static enum strip_action strip_symbols
= STRIP_UNDEF
;
128 LOCALS_START_L
, /* Discard locals starting with L. */
129 LOCALS_ALL
/* Discard all locals. */
132 /* Which local symbols to remove. Overrides STRIP_ALL. */
133 static enum locals_action discard_locals
;
135 /* Structure used to hold lists of sections and actions to take. */
138 struct section_list
*next
; /* Next section to change. */
139 const char *pattern
; /* Section name pattern. */
140 bool used
; /* Whether this entry was used. */
142 unsigned int context
; /* What to do with matching sections. */
143 /* Flag bits used in the context field.
144 COPY and REMOVE are mutually exlusive.
145 SET and ALTER are mutually exclusive. */
146 #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
147 #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
148 #define SECTION_CONTEXT_KEEP (1 << 2) /* Keep this section. */
149 #define SECTION_CONTEXT_SET_VMA (1 << 3) /* Set the sections' VMA address. */
150 #define SECTION_CONTEXT_ALTER_VMA (1 << 4) /* Increment or decrement the section's VMA address. */
151 #define SECTION_CONTEXT_SET_LMA (1 << 5) /* Set the sections' LMA address. */
152 #define SECTION_CONTEXT_ALTER_LMA (1 << 6) /* Increment or decrement the section's LMA address. */
153 #define SECTION_CONTEXT_SET_FLAGS (1 << 7) /* Set the section's flags. */
154 #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 8) /* Remove relocations for this section. */
155 #define SECTION_CONTEXT_SET_ALIGNMENT (1 << 9) /* Set alignment for section. */
157 bfd_vma vma_val
; /* Amount to change by or set to. */
158 bfd_vma lma_val
; /* Amount to change by or set to. */
159 flagword flags
; /* What to set the section flags to. */
160 unsigned int alignment
; /* Alignment of output section. */
163 static struct section_list
*change_sections
;
165 /* TRUE if some sections are to be removed. */
166 static bool sections_removed
;
168 /* TRUE if only some sections are to be copied. */
169 static bool sections_copied
;
171 /* Changes to the start address. */
172 static bfd_vma change_start
= 0;
173 static bool set_start_set
= false;
174 static bfd_vma set_start
;
176 /* Changes to section addresses. */
177 static bfd_vma change_section_address
= 0;
179 /* Filling gaps between sections. */
180 static bool gap_fill_set
= false;
181 static bfd_byte gap_fill
= 0;
183 /* Pad to a given address. */
184 static bool pad_to_set
= false;
185 static bfd_vma pad_to
;
187 /* Use alternative machine code? */
188 static unsigned long use_alt_mach_code
= 0;
190 /* Output BFD flags user wants to set or clear */
191 static flagword bfd_flags_to_set
;
192 static flagword bfd_flags_to_clear
;
194 /* List of sections to add. */
197 /* Next section to add. */
198 struct section_add
*next
;
199 /* Name of section to add. */
201 /* Name of file holding section contents. */
202 const char *filename
;
205 /* Contents of file. */
207 /* BFD section, after it has been added. */
211 /* List of sections to add to the output BFD. */
212 static struct section_add
*add_sections
;
214 /* List of sections to update in the output BFD. */
215 static struct section_add
*update_sections
;
217 /* List of sections to dump from the output BFD. */
218 static struct section_add
*dump_sections
;
220 /* If non-NULL the argument to --add-gnu-debuglink.
221 This should be the filename to store in the .gnu_debuglink section. */
222 static const char * gnu_debuglink_filename
= NULL
;
224 /* Whether to convert debugging information. */
225 static bool convert_debugging
= false;
227 /* Whether to compress/decompress DWARF debug sections. */
232 compress_zlib
= compress
| 1 << 1,
233 compress_gnu_zlib
= compress
| 1 << 2,
234 compress_gabi_zlib
= compress
| 1 << 3,
235 compress_zstd
= compress
| 1 << 4,
237 } do_debug_sections
= nothing
;
239 /* Whether to generate ELF common symbols with the STT_COMMON type. */
240 static enum bfd_link_elf_stt_common do_elf_stt_common
= unchanged
;
242 /* Whether to change the leading character in symbol names. */
243 static bool change_leading_char
= false;
245 /* Whether to remove the leading character from global symbol names. */
246 static bool remove_leading_char
= false;
248 /* Whether to permit wildcard in symbol comparison. */
249 static bool wildcard
= false;
251 /* True if --localize-hidden is in effect. */
252 static bool localize_hidden
= false;
254 /* List of symbols to strip, keep, localize, keep-global, weaken,
256 static htab_t strip_specific_htab
= NULL
;
257 static htab_t strip_unneeded_htab
= NULL
;
258 static htab_t keep_specific_htab
= NULL
;
259 static htab_t localize_specific_htab
= NULL
;
260 static htab_t globalize_specific_htab
= NULL
;
261 static htab_t keepglobal_specific_htab
= NULL
;
262 static htab_t weaken_specific_htab
= NULL
;
263 static htab_t redefine_specific_htab
= NULL
;
264 static htab_t redefine_specific_reverse_htab
= NULL
;
265 static struct addsym_node
*add_sym_list
= NULL
, **add_sym_tail
= &add_sym_list
;
266 static int add_symbols
= 0;
268 static char *strip_specific_buffer
= NULL
;
269 static char *strip_unneeded_buffer
= NULL
;
270 static char *keep_specific_buffer
= NULL
;
271 static char *localize_specific_buffer
= NULL
;
272 static char *globalize_specific_buffer
= NULL
;
273 static char *keepglobal_specific_buffer
= NULL
;
274 static char *weaken_specific_buffer
= NULL
;
276 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
277 static bool weaken
= false;
279 /* If this is TRUE, we retain BSF_FILE symbols. */
280 static bool keep_file_symbols
= false;
282 /* Prefix symbols/sections. */
283 static char *prefix_symbols_string
= 0;
284 static char *prefix_sections_string
= 0;
285 static char *prefix_alloc_sections_string
= 0;
287 /* True if --extract-symbol was passed on the command line. */
288 static bool extract_symbol
= false;
290 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
291 of <reverse_bytes> bytes within each output section. */
292 static int reverse_bytes
= 0;
294 /* For Coff objects, we may want to allow or disallow long section names,
295 or preserve them where found in the inputs. Debug info relies on them. */
296 enum long_section_name_handling
303 /* The default long section handling mode is to preserve them.
304 This is also the only behaviour for 'strip'. */
305 static enum long_section_name_handling long_section_names
= KEEP
;
307 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
308 enum command_line_switch
310 OPTION_ADD_SECTION
=150,
311 OPTION_ADD_GNU_DEBUGLINK
,
313 OPTION_ALT_MACH_CODE
,
314 OPTION_CHANGE_ADDRESSES
,
315 OPTION_CHANGE_LEADING_CHAR
,
316 OPTION_CHANGE_SECTION_ADDRESS
,
317 OPTION_CHANGE_SECTION_LMA
,
318 OPTION_CHANGE_SECTION_VMA
,
320 OPTION_CHANGE_WARNINGS
,
321 OPTION_COMPRESS_DEBUG_SECTIONS
,
323 OPTION_DECOMPRESS_DEBUG_SECTIONS
,
325 OPTION_ELF_STT_COMMON
,
327 OPTION_EXTRACT_SYMBOL
,
328 OPTION_FILE_ALIGNMENT
,
331 OPTION_GLOBALIZE_SYMBOL
,
332 OPTION_GLOBALIZE_SYMBOLS
,
336 OPTION_INTERLEAVE_WIDTH
,
337 OPTION_KEEPGLOBAL_SYMBOLS
,
338 OPTION_KEEP_FILE_SYMBOLS
,
341 OPTION_KEEP_SECTION_SYMBOLS
,
342 OPTION_LOCALIZE_HIDDEN
,
343 OPTION_LOCALIZE_SYMBOLS
,
344 OPTION_LONG_SECTION_NAMES
,
346 OPTION_NO_MERGE_NOTES
,
347 OPTION_NO_CHANGE_WARNINGS
,
348 OPTION_ONLY_KEEP_DEBUG
,
350 OPTION_PREFIX_ALLOC_SECTIONS
,
351 OPTION_PREFIX_SECTIONS
,
352 OPTION_PREFIX_SYMBOLS
,
354 OPTION_READONLY_TEXT
,
356 OPTION_REDEFINE_SYMS
,
357 OPTION_REMOVE_LEADING_CHAR
,
358 OPTION_REMOVE_RELOCS
,
359 OPTION_RENAME_SECTION
,
360 OPTION_REVERSE_BYTES
,
361 OPTION_PE_SECTION_ALIGNMENT
,
362 OPTION_SET_SECTION_FLAGS
,
363 OPTION_SET_SECTION_ALIGNMENT
,
369 OPTION_STRIP_SECTION_HEADERS
,
370 OPTION_STRIP_SYMBOLS
,
371 OPTION_STRIP_UNNEEDED
,
372 OPTION_STRIP_UNNEEDED_SYMBOL
,
373 OPTION_STRIP_UNNEEDED_SYMBOLS
,
375 OPTION_UPDATE_SECTION
,
376 OPTION_VERILOG_DATA_WIDTH
,
378 OPTION_WEAKEN_SYMBOLS
,
382 /* Options to handle if running as "strip". */
384 static struct option strip_options
[] =
386 {"disable-deterministic-archives", no_argument
, 0, 'U'},
387 {"discard-all", no_argument
, 0, 'x'},
388 {"discard-locals", no_argument
, 0, 'X'},
389 {"enable-deterministic-archives", no_argument
, 0, 'D'},
390 {"format", required_argument
, 0, 'F'}, /* Obsolete */
391 {"help", no_argument
, 0, 'h'},
392 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
393 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
394 {"input-target", required_argument
, 0, 'I'},
395 {"keep-section-symbols", no_argument
, 0, OPTION_KEEP_SECTION_SYMBOLS
},
396 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
397 {"keep-section", required_argument
, 0, OPTION_KEEP_SECTION
},
398 {"keep-symbol", required_argument
, 0, 'K'},
399 {"merge-notes", no_argument
, 0, 'M'},
400 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
401 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
402 {"output-file", required_argument
, 0, 'o'},
403 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
404 {"output-target", required_argument
, 0, 'O'},
405 {"preserve-dates", no_argument
, 0, 'p'},
406 {"remove-section", required_argument
, 0, 'R'},
407 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
408 {"strip-section-headers", no_argument
, 0, OPTION_STRIP_SECTION_HEADERS
},
409 {"strip-all", no_argument
, 0, 's'},
410 {"strip-debug", no_argument
, 0, 'S'},
411 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
412 {"strip-symbol", required_argument
, 0, 'N'},
413 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
414 {"target", required_argument
, 0, 'F'},
415 {"verbose", no_argument
, 0, 'v'},
416 {"version", no_argument
, 0, 'V'},
417 {"wildcard", no_argument
, 0, 'w'},
418 {0, no_argument
, 0, 0}
421 /* Options to handle if running as "objcopy". */
423 static struct option copy_options
[] =
425 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
426 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
427 {"add-symbol", required_argument
, 0, OPTION_ADD_SYMBOL
},
428 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
429 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
430 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
431 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
432 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
433 {"binary-architecture", required_argument
, 0, 'B'},
434 {"byte", required_argument
, 0, 'b'},
435 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
436 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
437 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
438 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
439 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
440 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
441 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
442 {"compress-debug-sections", optional_argument
, 0, OPTION_COMPRESS_DEBUG_SECTIONS
},
443 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
444 {"decompress-debug-sections", no_argument
, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS
},
445 {"disable-deterministic-archives", no_argument
, 0, 'U'},
446 {"discard-all", no_argument
, 0, 'x'},
447 {"discard-locals", no_argument
, 0, 'X'},
448 {"dump-section", required_argument
, 0, OPTION_DUMP_SECTION
},
449 {"elf-stt-common", required_argument
, 0, OPTION_ELF_STT_COMMON
},
450 {"enable-deterministic-archives", no_argument
, 0, 'D'},
451 {"extract-dwo", no_argument
, 0, OPTION_EXTRACT_DWO
},
452 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
453 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
454 {"format", required_argument
, 0, 'F'}, /* Obsolete */
455 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
456 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
457 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
458 {"heap", required_argument
, 0, OPTION_HEAP
},
459 {"help", no_argument
, 0, 'h'},
460 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
461 {"impure", no_argument
, 0, OPTION_IMPURE
},
462 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
463 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
464 {"input-target", required_argument
, 0, 'I'},
465 {"interleave", optional_argument
, 0, 'i'},
466 {"interleave-width", required_argument
, 0, OPTION_INTERLEAVE_WIDTH
},
467 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
468 {"keep-global-symbol", required_argument
, 0, 'G'},
469 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
470 {"keep-section", required_argument
, 0, OPTION_KEEP_SECTION
},
471 {"keep-symbol", required_argument
, 0, 'K'},
472 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
473 {"keep-section-symbols", required_argument
, 0, OPTION_KEEP_SECTION_SYMBOLS
},
474 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
475 {"localize-symbol", required_argument
, 0, 'L'},
476 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
477 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
478 {"merge-notes", no_argument
, 0, 'M'},
479 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
480 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
481 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
482 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
483 {"only-section", required_argument
, 0, 'j'},
484 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
485 {"output-target", required_argument
, 0, 'O'},
486 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
487 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
488 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
489 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
490 {"preserve-dates", no_argument
, 0, 'p'},
491 {"pure", no_argument
, 0, OPTION_PURE
},
492 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
493 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
494 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
495 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
496 {"remove-section", required_argument
, 0, 'R'},
497 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
498 {"strip-section-headers", no_argument
, 0, OPTION_STRIP_SECTION_HEADERS
},
499 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
500 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
501 {"section-alignment", required_argument
, 0, OPTION_PE_SECTION_ALIGNMENT
},
502 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
503 {"set-section-alignment", required_argument
, 0, OPTION_SET_SECTION_ALIGNMENT
},
504 {"set-start", required_argument
, 0, OPTION_SET_START
},
505 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
506 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
507 {"stack", required_argument
, 0, OPTION_STACK
},
508 {"strip-all", no_argument
, 0, 'S'},
509 {"strip-debug", no_argument
, 0, 'g'},
510 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
511 {"strip-symbol", required_argument
, 0, 'N'},
512 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
513 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
514 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
515 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
516 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
517 {"target", required_argument
, 0, 'F'},
518 {"update-section", required_argument
, 0, OPTION_UPDATE_SECTION
},
519 {"verbose", no_argument
, 0, 'v'},
520 {"verilog-data-width", required_argument
, 0, OPTION_VERILOG_DATA_WIDTH
},
521 {"version", no_argument
, 0, 'V'},
522 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
523 {"weaken-symbol", required_argument
, 0, 'W'},
524 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
525 {"wildcard", no_argument
, 0, 'w'},
526 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
527 {0, no_argument
, 0, 0}
531 extern char *program_name
;
533 /* This flag distinguishes between strip and objcopy:
534 1 means this is 'strip'; 0 means this is 'objcopy'.
535 -1 means if we should use argv[0] to decide. */
538 /* The maximum length of an S record. This variable is defined in srec.c
539 and can be modified by the --srec-len parameter. */
540 extern unsigned int _bfd_srec_len
;
542 /* Restrict the generation of Srecords to type S3 only.
543 This variable is defined in bfd/srec.c and can be toggled
544 on by the --srec-forceS3 command line switch. */
545 extern bool _bfd_srec_forceS3
;
547 /* Width of data in bytes for verilog output.
548 This variable is declared in bfd/verilog.c and can be modified by
549 the --verilog-data-width parameter. */
550 extern unsigned int VerilogDataWidth
;
552 /* Endianness of data for verilog output.
553 This variable is declared in bfd/verilog.c and is set in the
554 copy_object() function. */
555 extern enum bfd_endian VerilogDataEndianness
;
557 /* Forward declarations. */
558 static void setup_section (bfd
*, asection
*, void *);
559 static void setup_bfd_headers (bfd
*, bfd
*);
560 static void copy_relocations_in_section (bfd
*, asection
*, void *);
561 static void copy_section (bfd
*, asection
*, void *);
562 static void get_sections (bfd
*, asection
*, void *);
563 static int compare_section_lma (const void *, const void *);
564 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
565 static bool write_debugging_info (bfd
*, void *, long *, asymbol
***);
566 static const char *lookup_sym_redefinition (const char *);
567 static const char *find_section_rename (const char *, flagword
*);
569 ATTRIBUTE_NORETURN
static void
570 copy_usage (FILE *stream
, int exit_status
)
572 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
573 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
574 fprintf (stream
, _(" The options are:\n"));
575 fprintf (stream
, _("\
576 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
577 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
578 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
579 -F --target <bfdname> Set both input and output format to <bfdname>\n\
580 --debugging Convert debugging information, if possible\n\
581 -p --preserve-dates Copy modified/access timestamps to the output\n"));
582 if (DEFAULT_AR_DETERMINISTIC
)
583 fprintf (stream
, _("\
584 -D --enable-deterministic-archives\n\
585 Produce deterministic output when stripping archives (default)\n\
586 -U --disable-deterministic-archives\n\
587 Disable -D behavior\n"));
589 fprintf (stream
, _("\
590 -D --enable-deterministic-archives\n\
591 Produce deterministic output when stripping archives\n\
592 -U --disable-deterministic-archives\n\
593 Disable -D behavior (default)\n"));
594 fprintf (stream
, _("\
595 -j --only-section <name> Only copy section <name> into the output\n\
596 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
597 -R --remove-section <name> Remove section <name> from the output\n\
598 --remove-relocations <name> Remove relocations from section <name>\n\
599 --strip-section-headers Strip section header from the output\n\
600 -S --strip-all Remove all symbol and relocation information\n\
601 -g --strip-debug Remove all debugging symbols & sections\n\
602 --strip-dwo Remove all DWO sections\n\
603 --strip-unneeded Remove all symbols not needed by relocations\n\
604 -N --strip-symbol <name> Do not copy symbol <name>\n\
605 --strip-unneeded-symbol <name>\n\
606 Do not copy symbol <name> unless needed by\n\
608 --only-keep-debug Strip everything but the debug information\n\
609 --extract-dwo Copy only DWO sections\n\
610 --extract-symbol Remove section contents but keep symbols\n\
611 --keep-section <name> Do not strip section <name>\n\
612 -K --keep-symbol <name> Do not strip symbol <name>\n\
613 --keep-section-symbols Do not strip section symbols\n\
614 --keep-file-symbols Do not strip file symbol(s)\n\
615 --localize-hidden Turn all ELF hidden symbols into locals\n\
616 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
617 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
618 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
619 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
620 --weaken Force all global symbols to be marked as weak\n\
621 -w --wildcard Permit wildcard in symbol comparison\n\
622 -x --discard-all Remove all non-global symbols\n\
623 -X --discard-locals Remove any compiler-generated symbols\n\
624 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
625 --interleave-width <number> Set N for --interleave\n\
626 -b --byte <num> Select byte <num> in every interleaved block\n\
627 --gap-fill <val> Fill gaps between sections with <val>\n\
628 --pad-to <addr> Pad the last section up to address <addr>\n\
629 --set-start <addr> Set the start address to <addr>\n\
630 {--change-start|--adjust-start} <incr>\n\
631 Add <incr> to the start address\n\
632 {--change-addresses|--adjust-vma} <incr>\n\
633 Add <incr> to LMA, VMA and start addresses\n\
634 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
635 Change LMA and VMA of section <name> by <val>\n\
636 --change-section-lma <name>{=|+|-}<val>\n\
637 Change the LMA of section <name> by <val>\n\
638 --change-section-vma <name>{=|+|-}<val>\n\
639 Change the VMA of section <name> by <val>\n\
640 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
641 Warn if a named section does not exist\n\
642 --set-section-flags <name>=<flags>\n\
643 Set section <name>'s properties to <flags>\n\
644 --set-section-alignment <name>=<align>\n\
645 Set section <name>'s alignment to <align> bytes\n\
646 --add-section <name>=<file> Add section <name> found in <file> to output\n\
647 --update-section <name>=<file>\n\
648 Update contents of section <name> with\n\
649 contents found in <file>\n\
650 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
651 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
652 --long-section-names {enable|disable|keep}\n\
653 Handle long section names in Coff objects.\n\
654 --change-leading-char Force output format's leading character style\n\
655 --remove-leading-char Remove leading character from global symbols\n\
656 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
657 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
658 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
660 --srec-len <number> Restrict the length of generated Srecords\n\
661 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
662 --strip-symbols <file> -N for all symbols listed in <file>\n\
663 --strip-unneeded-symbols <file>\n\
664 --strip-unneeded-symbol for all symbols listed\n\
666 --keep-symbols <file> -K for all symbols listed in <file>\n\
667 --localize-symbols <file> -L for all symbols listed in <file>\n\
668 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
669 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
670 --weaken-symbols <file> -W for all symbols listed in <file>\n\
671 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
672 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
673 --writable-text Mark the output text as writable\n\
674 --readonly-text Make the output text write protected\n\
675 --pure Mark the output file as demand paged\n\
676 --impure Mark the output file as impure\n\
677 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
678 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
679 --prefix-alloc-sections <prefix>\n\
680 Add <prefix> to start of every allocatable\n\
682 --file-alignment <num> Set PE file alignment to <num>\n\
683 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
685 --image-base <address> Set PE image base to <address>\n\
686 --section-alignment <num> Set PE section alignment to <num>\n\
687 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
689 --subsystem <name>[:<version>]\n\
690 Set PE subsystem to <name> [& <version>]\n\
691 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
692 Compress DWARF debug sections\n\
693 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
694 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
696 --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
697 -M --merge-notes Remove redundant entries in note sections\n\
698 --no-merge-notes Do not attempt to remove redundant notes (default)\n\
699 -v --verbose List all object files modified\n\
700 @<file> Read options from <file>\n\
701 -V --version Display this program's version number\n\
702 -h --help Display this output\n\
703 --info List object formats & architectures supported\n\
705 list_supported_targets (program_name
, stream
);
706 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
707 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
711 ATTRIBUTE_NORETURN
static void
712 strip_usage (FILE *stream
, int exit_status
)
714 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
715 fprintf (stream
, _(" Removes symbols and sections from files\n"));
716 fprintf (stream
, _(" The options are:\n"));
717 fprintf (stream
, _("\
718 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
719 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
720 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
721 -p --preserve-dates Copy modified/access timestamps to the output\n\
723 if (DEFAULT_AR_DETERMINISTIC
)
724 fprintf (stream
, _("\
725 -D --enable-deterministic-archives\n\
726 Produce deterministic output when stripping archives (default)\n\
727 -U --disable-deterministic-archives\n\
728 Disable -D behavior\n"));
730 fprintf (stream
, _("\
731 -D --enable-deterministic-archives\n\
732 Produce deterministic output when stripping archives\n\
733 -U --disable-deterministic-archives\n\
734 Disable -D behavior (default)\n"));
735 fprintf (stream
, _("\
736 -R --remove-section=<name> Also remove section <name> from the output\n\
737 --remove-relocations <name> Remove relocations from section <name>\n\
738 --strip-section-headers Strip section headers from the output\n\
739 -s --strip-all Remove all symbol and relocation information\n\
740 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
741 --strip-dwo Remove all DWO sections\n\
742 --strip-unneeded Remove all symbols not needed by relocations\n\
743 --only-keep-debug Strip everything but the debug information\n\
744 -M --merge-notes Remove redundant entries in note sections (default)\n\
745 --no-merge-notes Do not attempt to remove redundant notes\n\
746 -N --strip-symbol=<name> Do not copy symbol <name>\n\
747 --keep-section=<name> Do not strip section <name>\n\
748 -K --keep-symbol=<name> Do not strip symbol <name>\n\
749 --keep-section-symbols Do not strip section symbols\n\
750 --keep-file-symbols Do not strip file symbol(s)\n\
751 -w --wildcard Permit wildcard in symbol comparison\n\
752 -x --discard-all Remove all non-global symbols\n\
753 -X --discard-locals Remove any compiler-generated symbols\n\
754 -v --verbose List all object files modified\n\
755 -V --version Display this program's version number\n\
756 -h --help Display this output\n\
757 --info List object formats & architectures supported\n\
758 -o <file> Place stripped output into <file>\n\
761 list_supported_targets (program_name
, stream
);
762 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
763 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
767 /* Parse section flags into a flagword, with a fatal error if the
768 string can't be parsed. */
771 parse_flags (const char *s
)
781 snext
= strchr (s
, ',');
791 #define PARSE_FLAG(fname,fval) \
792 else if (strncasecmp (fname, s, len) == 0) ret |= fval
793 PARSE_FLAG ("alloc", SEC_ALLOC
);
794 PARSE_FLAG ("load", SEC_LOAD
);
795 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
796 PARSE_FLAG ("readonly", SEC_READONLY
);
797 PARSE_FLAG ("debug", SEC_DEBUGGING
);
798 PARSE_FLAG ("code", SEC_CODE
);
799 PARSE_FLAG ("data", SEC_DATA
);
800 PARSE_FLAG ("rom", SEC_ROM
);
801 PARSE_FLAG ("exclude", SEC_EXCLUDE
);
802 PARSE_FLAG ("share", SEC_COFF_SHARED
);
803 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
804 PARSE_FLAG ("merge", SEC_MERGE
);
805 PARSE_FLAG ("strings", SEC_STRINGS
);
806 PARSE_FLAG ("large", SEC_ELF_LARGE
);
812 copy
= (char *) xmalloc (len
+ 1);
813 strncpy (copy
, s
, len
);
815 non_fatal (_("unrecognized section flag `%s'"), copy
);
816 fatal (_ ("supported flags: %s"),
817 "alloc, load, noload, readonly, debug, code, data, rom, "
818 "exclude, contents, merge, strings, (COFF specific) share, "
819 "(ELF x86-64 specific) large");
829 /* Parse symbol flags into a flagword, with a fatal error if the
830 string can't be parsed. */
833 parse_symflags (const char *s
, const char **other
)
843 snext
= strchr (s
, ',');
852 #define PARSE_FLAG(fname, fval) \
853 else if (len == sizeof fname - 1 \
854 && strncasecmp (fname, s, len) == 0) \
857 #define PARSE_OTHER(fname, fval) \
858 else if (len >= sizeof fname \
859 && strncasecmp (fname, s, sizeof fname - 1) == 0) \
860 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
863 PARSE_FLAG ("local", BSF_LOCAL
);
864 PARSE_FLAG ("global", BSF_GLOBAL
);
865 PARSE_FLAG ("export", BSF_EXPORT
);
866 PARSE_FLAG ("debug", BSF_DEBUGGING
);
867 PARSE_FLAG ("function", BSF_FUNCTION
);
868 PARSE_FLAG ("weak", BSF_WEAK
);
869 PARSE_FLAG ("section", BSF_SECTION_SYM
);
870 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR
);
871 PARSE_FLAG ("warning", BSF_WARNING
);
872 PARSE_FLAG ("indirect", BSF_INDIRECT
);
873 PARSE_FLAG ("file", BSF_FILE
);
874 PARSE_FLAG ("object", BSF_OBJECT
);
875 PARSE_FLAG ("synthetic", BSF_SYNTHETIC
);
876 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION
| BSF_FUNCTION
);
877 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE
| BSF_OBJECT
);
878 PARSE_OTHER ("before=", *other
);
886 copy
= (char *) xmalloc (len
+ 1);
887 strncpy (copy
, s
, len
);
889 non_fatal (_("unrecognized symbol flag `%s'"), copy
);
890 fatal (_("supported flags: %s"),
891 "local, global, export, debug, function, weak, section, "
892 "constructor, warning, indirect, file, object, synthetic, "
893 "indirect-function, unique-object, before=<othersym>");
903 /* Find and optionally add an entry in the change_sections list.
905 We need to be careful in how we match section names because of the support
906 for wildcard characters. For example suppose that the user has invoked
909 --set-section-flags .debug_*=debug
910 --set-section-flags .debug_str=readonly,debug
911 --change-section-address .debug_*ranges=0x1000
913 With the idea that all debug sections will receive the DEBUG flag, the
914 .debug_str section will also receive the READONLY flag and the
915 .debug_ranges and .debug_aranges sections will have their address set to
916 0x1000. (This may not make much sense, but it is just an example).
918 When adding the section name patterns to the section list we need to make
919 sure that previous entries do not match with the new entry, unless the
920 match is exact. (In which case we assume that the user is overriding
921 the previous entry with the new context).
923 When matching real section names to the section list we make use of the
924 wildcard characters, but we must do so in context. Eg if we are setting
925 section addresses then we match for .debug_ranges but not for .debug_info.
927 Finally, if ADD is false and we do find a match, we mark the section list
930 static struct section_list
*
931 find_section_list (const char *name
, bool add
, unsigned int context
)
933 struct section_list
*p
, *match
= NULL
;
935 /* assert ((context & ((1 << 7) - 1)) != 0); */
937 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
941 if (strcmp (p
->pattern
, name
) == 0)
943 /* Check for context conflicts. */
944 if (((p
->context
& SECTION_CONTEXT_REMOVE
)
945 && (context
& SECTION_CONTEXT_COPY
))
946 || ((context
& SECTION_CONTEXT_REMOVE
)
947 && (p
->context
& SECTION_CONTEXT_COPY
)))
948 fatal (_("error: %s both copied and removed"), name
);
950 if (((p
->context
& SECTION_CONTEXT_SET_VMA
)
951 && (context
& SECTION_CONTEXT_ALTER_VMA
))
952 || ((context
& SECTION_CONTEXT_SET_VMA
)
953 && (context
& SECTION_CONTEXT_ALTER_VMA
)))
954 fatal (_("error: %s both sets and alters VMA"), name
);
956 if (((p
->context
& SECTION_CONTEXT_SET_LMA
)
957 && (context
& SECTION_CONTEXT_ALTER_LMA
))
958 || ((context
& SECTION_CONTEXT_SET_LMA
)
959 && (context
& SECTION_CONTEXT_ALTER_LMA
)))
960 fatal (_("error: %s both sets and alters LMA"), name
);
962 /* Extend the context. */
963 p
->context
|= context
;
967 /* If we are not adding a new name/pattern then
968 only check for a match if the context applies. */
969 else if (p
->context
& context
)
971 /* We could check for the presence of wildchar characters
972 first and choose between calling strcmp and fnmatch,
973 but is that really worth it ? */
974 if (p
->pattern
[0] == '!')
976 if (fnmatch (p
->pattern
+ 1, name
, 0) == 0)
984 if (fnmatch (p
->pattern
, name
, 0) == 0)
1000 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
1003 p
->context
= context
;
1008 p
->next
= change_sections
;
1009 change_sections
= p
;
1014 /* S1 is the entry node already in the table, S2 is the key node. */
1017 eq_string_redefnode (const void *s1
, const void *s2
)
1019 struct redefine_node
*node1
= (struct redefine_node
*) s1
;
1020 struct redefine_node
*node2
= (struct redefine_node
*) s2
;
1021 return !strcmp ((const char *) node1
->source
, (const char *) node2
->source
);
1024 /* P is redefine node. Hash value is generated from its "source" filed. */
1027 htab_hash_redefnode (const void *p
)
1029 struct redefine_node
*redefnode
= (struct redefine_node
*) p
;
1030 return htab_hash_string (redefnode
->source
);
1033 /* Create hashtab used for redefine node. */
1036 create_symbol2redef_htab (void)
1038 return htab_create_alloc (16, htab_hash_redefnode
, eq_string_redefnode
, NULL
,
1043 create_symbol_htab (void)
1045 return htab_create_alloc (16, htab_hash_string
, htab_eq_string
, NULL
,
1050 create_symbol_htabs (void)
1052 strip_specific_htab
= create_symbol_htab ();
1053 strip_unneeded_htab
= create_symbol_htab ();
1054 keep_specific_htab
= create_symbol_htab ();
1055 localize_specific_htab
= create_symbol_htab ();
1056 globalize_specific_htab
= create_symbol_htab ();
1057 keepglobal_specific_htab
= create_symbol_htab ();
1058 weaken_specific_htab
= create_symbol_htab ();
1059 redefine_specific_htab
= create_symbol2redef_htab ();
1060 /* As there is no bidirectional hash table in libiberty, need a reverse table
1061 to check duplicated target string. */
1062 redefine_specific_reverse_htab
= create_symbol_htab ();
1066 delete_symbol_htabs (void)
1068 htab_delete (strip_specific_htab
);
1069 htab_delete (strip_unneeded_htab
);
1070 htab_delete (keep_specific_htab
);
1071 htab_delete (localize_specific_htab
);
1072 htab_delete (globalize_specific_htab
);
1073 htab_delete (keepglobal_specific_htab
);
1074 htab_delete (weaken_specific_htab
);
1075 htab_delete (redefine_specific_htab
);
1076 htab_delete (redefine_specific_reverse_htab
);
1079 if (osympp
!= isympp
)
1083 /* Add a symbol to strip_specific_list. */
1086 add_specific_symbol (const char *name
, htab_t htab
)
1088 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
1091 /* Like add_specific_symbol, but the element type is void *. */
1094 add_specific_symbol_node (const void *node
, htab_t htab
)
1096 *htab_find_slot (htab
, node
, INSERT
) = (void *) node
;
1099 /* Add symbols listed in `filename' to strip_specific_list. */
1101 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
1102 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
1105 add_specific_symbols (const char *filename
, htab_t htab
, char **buffer_p
)
1111 unsigned int line_count
;
1113 size
= get_file_size (filename
);
1120 buffer
= (char *) xmalloc (size
+ 2);
1121 f
= fopen (filename
, FOPEN_RT
);
1123 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
1125 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
1126 fatal (_("%s: fread failed"), filename
);
1129 buffer
[size
] = '\n';
1130 buffer
[size
+ 1] = '\0';
1134 for (line
= buffer
; * line
!= '\0'; line
++)
1139 int finished
= false;
1141 for (eol
= line
;; eol
++)
1147 /* Cope with \n\r. */
1155 /* Cope with \r\n. */
1166 /* Line comment, Terminate the line here, in case a
1167 name is present and then allow the rest of the
1168 loop to find the real end of the line. */
1180 /* A name may now exist somewhere between 'line' and 'eol'.
1181 Strip off leading whitespace and trailing whitespace,
1182 then add it to the list. */
1183 for (name
= line
; IS_WHITESPACE (* name
); name
++)
1185 for (name_end
= name
;
1186 (! IS_WHITESPACE (* name_end
))
1187 && (! IS_LINE_TERMINATOR (* name_end
));
1191 if (! IS_LINE_TERMINATOR (* name_end
))
1195 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
1198 if (! IS_LINE_TERMINATOR (* extra
))
1199 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1200 filename
, line_count
);
1205 if (name_end
> name
)
1206 add_specific_symbol (name
, htab
);
1208 /* Advance line pointer to end of line. The 'eol ++' in the for
1209 loop above will then advance us to the start of the next line. */
1214 /* Do not free the buffer. Parts of it will have been referenced
1215 in the calls to add_specific_symbol. */
1219 /* See whether a symbol should be stripped or kept
1220 based on strip_specific_list and keep_symbols. */
1223 is_specified_symbol_predicate (void **slot
, void *data
)
1225 struct is_specified_symbol_predicate_data
*d
=
1226 (struct is_specified_symbol_predicate_data
*) data
;
1227 const char *slot_name
= (char *) *slot
;
1229 if (*slot_name
!= '!')
1231 if (! fnmatch (slot_name
, d
->name
, 0))
1234 /* Continue traversal, there might be a non-match rule. */
1240 if (! fnmatch (slot_name
+ 1, d
->name
, 0))
1243 /* Stop traversal. */
1248 /* Continue traversal. */
1253 is_specified_symbol (const char *name
, htab_t htab
)
1257 struct is_specified_symbol_predicate_data data
;
1262 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
1267 return htab_find (htab
, name
) != NULL
;
1270 /* Return a pointer to the symbol used as a signature for GROUP. */
1273 group_signature (asection
*group
)
1275 bfd
*abfd
= group
->owner
;
1276 Elf_Internal_Shdr
*ghdr
;
1278 /* PR 20089: An earlier error may have prevented us from loading the symbol table. */
1282 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
1285 ghdr
= &elf_section_data (group
)->this_hdr
;
1286 if (ghdr
->sh_link
== elf_onesymtab (abfd
))
1288 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1289 Elf_Internal_Shdr
*symhdr
= &elf_symtab_hdr (abfd
);
1291 if (ghdr
->sh_info
> 0
1292 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
1293 return isympp
[ghdr
->sh_info
- 1];
1298 /* Return TRUE if the section is a DWO section. */
1301 is_dwo_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1306 if (sec
== NULL
|| (name
= bfd_section_name (sec
)) == NULL
)
1309 len
= strlen (name
);
1313 return startswith (name
+ len
- 4, ".dwo");
1316 /* Return TRUE if section SEC is in the update list. */
1319 is_update_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1321 if (update_sections
!= NULL
)
1323 struct section_add
*pupdate
;
1325 for (pupdate
= update_sections
;
1327 pupdate
= pupdate
->next
)
1329 if (strcmp (sec
->name
, pupdate
->name
) == 0)
1338 is_mergeable_note_section (bfd
* abfd
, asection
* sec
)
1341 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1342 && elf_section_data (sec
)->this_hdr
.sh_type
== SHT_NOTE
1343 /* FIXME: We currently only support merging GNU_BUILD_NOTEs.
1344 We should add support for more note types. */
1345 && (startswith (sec
->name
, GNU_BUILD_ATTRS_SECTION_NAME
)))
1351 /* See if a non-group section is being removed. */
1354 is_strip_section_1 (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1356 if (find_section_list (bfd_section_name (sec
), false, SECTION_CONTEXT_KEEP
)
1360 if (sections_removed
|| sections_copied
)
1362 struct section_list
*p
;
1363 struct section_list
*q
;
1365 p
= find_section_list (bfd_section_name (sec
), false,
1366 SECTION_CONTEXT_REMOVE
);
1367 q
= find_section_list (bfd_section_name (sec
), false,
1368 SECTION_CONTEXT_COPY
);
1371 fatal (_("error: section %s matches both remove and copy options"),
1372 bfd_section_name (sec
));
1373 if (p
&& is_update_section (abfd
, sec
))
1374 fatal (_("error: section %s matches both update and remove options"),
1375 bfd_section_name (sec
));
1379 if (sections_copied
&& q
== NULL
)
1383 /* Remove non-alloc sections for --strip-section-headers. */
1384 if (strip_section_headers
1385 && (bfd_section_flags (sec
) & SEC_ALLOC
) == 0)
1388 if ((bfd_section_flags (sec
) & SEC_DEBUGGING
) != 0)
1390 if (strip_symbols
== STRIP_DEBUG
1391 || strip_symbols
== STRIP_UNNEEDED
1392 || strip_symbols
== STRIP_ALL
1393 || discard_locals
== LOCALS_ALL
1394 || convert_debugging
)
1396 /* By default we don't want to strip .reloc section.
1397 This section has for pe-coff special meaning. See
1398 pe-dll.c file in ld, and peXXigen.c in bfd for details.
1399 Similarly we do not want to strip debuglink sections. */
1400 const char * kept_sections
[] =
1408 for (i
= ARRAY_SIZE (kept_sections
);i
--;)
1409 if (strcmp (bfd_section_name (sec
), kept_sections
[i
]) == 0)
1415 if (strip_symbols
== STRIP_DWO
)
1416 return is_dwo_section (abfd
, sec
);
1418 if (strip_symbols
== STRIP_NONDEBUG
)
1422 if (strip_symbols
== STRIP_NONDWO
)
1423 return !is_dwo_section (abfd
, sec
);
1428 /* See if a section is being removed. */
1431 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1433 if (is_strip_section_1 (abfd
, sec
))
1436 if ((bfd_section_flags (sec
) & SEC_GROUP
) != 0)
1440 asection
*elt
, *first
;
1442 gsym
= group_signature (sec
);
1443 /* Strip groups without a valid signature. */
1448 If we are going to strip the group signature symbol, then
1449 strip the group section too. */
1451 if ((strip_symbols
== STRIP_ALL
1452 && !is_specified_symbol (gname
, keep_specific_htab
))
1453 || is_specified_symbol (gname
, strip_specific_htab
))
1456 /* Remove the group section if all members are removed. */
1457 first
= elt
= elf_next_in_group (sec
);
1460 if (!is_strip_section_1 (abfd
, elt
))
1462 elt
= elf_next_in_group (elt
);
1474 is_nondebug_keep_contents_section (bfd
*ibfd
, asection
*isection
)
1476 /* Always keep ELF note sections. */
1477 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
1478 return elf_section_type (isection
) == SHT_NOTE
;
1480 /* Always keep the .buildid section for PE/COFF.
1482 Strictly, this should be written "always keep the section storing the debug
1483 directory", but that may be the .text section for objects produced by some
1484 tools, which it is not sensible to keep. */
1485 if (bfd_get_flavour (ibfd
) == bfd_target_coff_flavour
)
1486 return strcmp (bfd_section_name (isection
), ".buildid") == 0;
1491 /* Return true if SYM is a hidden symbol. */
1494 is_hidden_symbol (asymbol
*sym
)
1496 elf_symbol_type
*elf_sym
;
1498 elf_sym
= elf_symbol_from (sym
);
1499 if (elf_sym
!= NULL
)
1500 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
1509 /* Empty name is hopefully never a valid symbol name. */
1510 static const char * empty_name
= "";
1513 need_sym_before (struct addsym_node
**node
, const char *sym
)
1516 struct addsym_node
*ptr
= add_sym_list
;
1518 /* 'othersym' symbols are at the front of the list. */
1519 for (count
= 0; count
< add_symbols
; count
++)
1523 if (ptr
->othersym
== empty_name
)
1525 else if (strcmp (ptr
->othersym
, sym
) == 0)
1527 free ((char *) ptr
->othersym
);
1528 ptr
->othersym
= empty_name
;
1538 create_new_symbol (struct addsym_node
*ptr
, bfd
*obfd
)
1540 asymbol
*sym
= bfd_make_empty_symbol (obfd
);
1542 bfd_set_asymbol_name (sym
, ptr
->symdef
);
1543 sym
->value
= ptr
->symval
;
1544 sym
->flags
= ptr
->flags
;
1547 asection
*sec
= bfd_get_section_by_name (obfd
, ptr
->section
);
1549 fatal (_("Section %s not found"), ptr
->section
);
1553 sym
->section
= bfd_abs_section_ptr
;
1557 /* Choose which symbol entries to copy; put the result in OSYMS.
1558 We don't copy in place, because that confuses the relocs.
1559 Return the number of symbols to print. */
1562 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
1563 asymbol
**isyms
, long symcount
)
1565 asymbol
**from
= isyms
, **to
= osyms
;
1566 long src_count
= 0, dst_count
= 0;
1567 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
1569 for (; src_count
< symcount
; src_count
++)
1571 asymbol
*sym
= from
[src_count
];
1572 flagword flags
= sym
->flags
;
1573 char *name
= (char *) bfd_asymbol_name (sym
);
1575 bool used_in_reloc
= false;
1577 bool rem_leading_char
;
1578 bool add_leading_char
;
1580 undefined
= bfd_is_und_section (bfd_asymbol_section (sym
));
1584 struct addsym_node
*ptr
;
1586 if (need_sym_before (&ptr
, name
))
1587 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1590 if (htab_elements (redefine_specific_htab
) || section_rename_list
)
1597 && strcmp (name
+ (name
[2] == '_'), "__gnu_lto_slim") == 0)
1599 fatal (_("redefining symbols does not work on LTO-compiled object files"));
1602 new_name
= (char *) lookup_sym_redefinition (name
);
1603 if (new_name
== name
1604 && (flags
& BSF_SECTION_SYM
) != 0)
1605 new_name
= (char *) find_section_rename (name
, NULL
);
1606 bfd_set_asymbol_name (sym
, new_name
);
1610 /* Check if we will remove the current leading character. */
1613 && name
[0] == bfd_get_symbol_leading_char (abfd
)
1614 && (change_leading_char
1615 || (remove_leading_char
1616 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1618 || bfd_is_com_section (bfd_asymbol_section (sym
))))));
1620 /* Check if we will add a new leading character. */
1623 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1624 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1625 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1627 /* Short circuit for change_leading_char if we can do it in-place. */
1628 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1630 name
[0] = bfd_get_symbol_leading_char (obfd
);
1631 bfd_set_asymbol_name (sym
, name
);
1632 rem_leading_char
= false;
1633 add_leading_char
= false;
1636 /* Remove leading char. */
1637 if (rem_leading_char
)
1638 bfd_set_asymbol_name (sym
, ++name
);
1640 /* Add new leading char and/or prefix. */
1641 if (add_leading_char
|| prefix_symbols_string
)
1644 size_t len
= strlen (name
) + 1;
1646 if (add_leading_char
)
1648 if (prefix_symbols_string
)
1649 len
+= strlen (prefix_symbols_string
);
1651 ptr
= n
= (char *) xmalloc (len
);
1652 if (add_leading_char
)
1653 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1655 if (prefix_symbols_string
)
1657 strcpy (ptr
, prefix_symbols_string
);
1658 ptr
+= strlen (prefix_symbols_string
);
1662 bfd_set_asymbol_name (sym
, n
);
1666 if (strip_symbols
== STRIP_ALL
)
1668 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1669 || ((flags
& BSF_SECTION_SYM
) != 0
1670 && ((*bfd_asymbol_section (sym
)->symbol_ptr_ptr
)->flags
1674 used_in_reloc
= true;
1676 else if (relocatable
/* Relocatable file. */
1677 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1678 || bfd_is_com_section (bfd_asymbol_section (sym
))))
1680 else if (bfd_decode_symclass (sym
) == 'I')
1681 /* Global symbols in $idata sections need to be retained
1682 even if relocatable is FALSE. External users of the
1683 library containing the $idata section may reference these
1686 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1687 || (flags
& BSF_WEAK
) != 0
1689 || bfd_is_com_section (bfd_asymbol_section (sym
)))
1690 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1691 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1692 keep
= (strip_symbols
!= STRIP_DEBUG
1693 && strip_symbols
!= STRIP_UNNEEDED
1694 && ! convert_debugging
);
1695 else if (bfd_coff_get_comdat_section (abfd
, bfd_asymbol_section (sym
)))
1696 /* COMDAT sections store special information in local
1697 symbols, so we cannot risk stripping any of them. */
1699 else /* Local symbol. */
1700 keep
= (strip_symbols
!= STRIP_UNNEEDED
1701 && (discard_locals
!= LOCALS_ALL
1702 && (discard_locals
!= LOCALS_START_L
1703 || ! bfd_is_local_label (abfd
, sym
))));
1705 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1707 /* There are multiple ways to set 'keep' above, but if it
1708 was the relocatable symbol case, then that's an error. */
1711 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1719 && !(flags
& BSF_KEEP
)
1720 && is_specified_symbol (name
, strip_unneeded_htab
))
1724 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1725 || is_specified_symbol (name
, keep_specific_htab
)))
1728 if (keep
&& is_strip_section (abfd
, bfd_asymbol_section (sym
)))
1733 if (((flags
& (BSF_GLOBAL
| BSF_GNU_UNIQUE
))
1735 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1737 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_GNU_UNIQUE
);
1738 sym
->flags
|= BSF_WEAK
;
1742 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1743 && (is_specified_symbol (name
, localize_specific_htab
)
1744 || (htab_elements (keepglobal_specific_htab
) != 0
1745 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1746 || (localize_hidden
&& is_hidden_symbol (sym
))))
1748 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1749 sym
->flags
|= BSF_LOCAL
;
1753 && (flags
& BSF_LOCAL
)
1754 && is_specified_symbol (name
, globalize_specific_htab
))
1756 sym
->flags
&= ~ BSF_LOCAL
;
1757 sym
->flags
|= BSF_GLOBAL
;
1760 to
[dst_count
++] = sym
;
1765 struct addsym_node
*ptr
= add_sym_list
;
1767 for (src_count
= 0; src_count
< add_symbols
; src_count
++)
1771 if (ptr
->othersym
!= empty_name
)
1772 fatal (_("'before=%s' not found"), ptr
->othersym
);
1775 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1781 to
[dst_count
] = NULL
;
1786 /* Find the redefined name of symbol SOURCE. */
1789 lookup_sym_redefinition (const char *source
)
1791 struct redefine_node key_node
= {(char *) source
, NULL
};
1792 struct redefine_node
*redef_node
1793 = (struct redefine_node
*) htab_find (redefine_specific_htab
, &key_node
);
1795 return redef_node
== NULL
? source
: redef_node
->target
;
1798 /* Insert a node into symbol redefine hash tabel. */
1801 add_redefine_and_check (const char *cause
, const char *source
,
1804 struct redefine_node
*new_node
1805 = (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
1807 new_node
->source
= strdup (source
);
1808 new_node
->target
= strdup (target
);
1810 if (htab_find (redefine_specific_htab
, new_node
) != HTAB_EMPTY_ENTRY
)
1811 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1814 if (htab_find (redefine_specific_reverse_htab
, target
) != HTAB_EMPTY_ENTRY
)
1815 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1818 /* Insert the NEW_NODE into hash table for quick search. */
1819 add_specific_symbol_node (new_node
, redefine_specific_htab
);
1821 /* Insert the target string into the reverse hash table, this is needed for
1822 duplicated target string check. */
1823 add_specific_symbol (new_node
->target
, redefine_specific_reverse_htab
);
1827 /* Handle the --redefine-syms option. Read lines containing "old new"
1828 from the file, and add them to the symbol redefine list. */
1831 add_redefine_syms_file (const char *filename
)
1840 file
= fopen (filename
, "r");
1842 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1843 filename
, strerror (errno
));
1846 buf
= (char *) xmalloc (bufsize
+ 1 /* For the terminating NUL. */);
1854 /* Collect the input symbol name. */
1855 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1863 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1871 /* Eat white space between the symbol names. */
1872 while (IS_WHITESPACE (c
))
1874 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1879 /* Collect the output symbol name. */
1881 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1889 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1897 /* Eat white space at end of line. */
1898 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1903 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1904 || c
== '\n' || c
== EOF
)
1907 /* Append the redefinition to the list. */
1909 add_redefine_and_check (filename
, &buf
[0], &buf
[outsym_off
]);
1920 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1922 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1923 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1926 /* Eat the rest of the line and finish it. */
1927 while (c
!= '\n' && c
!= EOF
)
1933 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1939 /* Copy unknown object file IBFD onto OBFD.
1940 Returns TRUE upon success, FALSE otherwise. */
1943 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1946 bfd_size_type tocopy
;
1950 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1952 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1959 non_fatal (_("stat returns negative size for `%s'"),
1960 bfd_get_archive_filename (ibfd
));
1964 if (bfd_seek (ibfd
, 0, SEEK_SET
) != 0)
1966 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1971 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1972 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1974 cbuf
= (char *) xmalloc (BUFSIZE
);
1982 if (bfd_read (cbuf
, tocopy
, ibfd
) != tocopy
)
1984 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1989 if (bfd_write (cbuf
, tocopy
, obfd
) != tocopy
)
1991 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1999 /* We should at least to be able to read it back when copying an
2000 unknown object in an archive. */
2001 chmod (bfd_get_filename (obfd
), buf
.st_mode
| S_IRUSR
);
2006 typedef struct objcopy_internal_note
2008 Elf_Internal_Note note
;
2009 unsigned long padded_namesz
;
2012 } objcopy_internal_note
;
2014 #define DEBUG_MERGE 0
2017 #define merge_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
2019 #define merge_debug(format, ...)
2022 /* Returns TRUE iff PNOTE1 overlaps or adjoins PNOTE2. */
2025 overlaps_or_adjoins (objcopy_internal_note
* pnote1
,
2026 objcopy_internal_note
* pnote2
)
2028 if (pnote1
->end
< pnote2
->start
)
2029 /* FIXME: Alignment of 16 bytes taken from x86_64 binaries.
2030 Really we should extract the alignment of the section
2031 covered by the notes. */
2032 return BFD_ALIGN (pnote1
->end
, 16) < pnote2
->start
;
2034 if (pnote2
->end
< pnote2
->start
)
2035 return BFD_ALIGN (pnote2
->end
, 16) < pnote1
->start
;
2037 if (pnote1
->end
< pnote2
->end
)
2040 if (pnote2
->end
< pnote1
->end
)
2046 /* Returns TRUE iff NEEDLE is fully contained by HAYSTACK. */
2049 contained_by (objcopy_internal_note
* needle
,
2050 objcopy_internal_note
* haystack
)
2052 return needle
->start
>= haystack
->start
&& needle
->end
<= haystack
->end
;
2056 is_open_note (objcopy_internal_note
* pnote
)
2058 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_OPEN
;
2062 is_func_note (objcopy_internal_note
* pnote
)
2064 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_FUNC
;
2068 is_deleted_note (objcopy_internal_note
* pnote
)
2070 return pnote
->note
.type
== 0;
2074 is_version_note (objcopy_internal_note
* pnote
)
2076 return (pnote
->note
.namesz
> 4
2077 && pnote
->note
.namedata
[0] == 'G'
2078 && pnote
->note
.namedata
[1] == 'A'
2079 && pnote
->note
.namedata
[2] == '$'
2080 && pnote
->note
.namedata
[3] == GNU_BUILD_ATTRIBUTE_VERSION
);
2084 is_64bit (bfd
* abfd
)
2086 /* Should never happen, but let's be paranoid. */
2087 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
2090 return elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
;
2093 /* This sorting function is used to get the notes into an order
2094 that makes merging easy. */
2097 compare_gnu_build_notes (const void * data1
, const void * data2
)
2099 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2100 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2102 /* Sort notes based upon the attribute they record. */
2103 int cmp
= memcmp (pnote1
->note
.namedata
+ 3,
2104 pnote2
->note
.namedata
+ 3,
2105 pnote1
->note
.namesz
< pnote2
->note
.namesz
?
2106 pnote1
->note
.namesz
- 3 : pnote2
->note
.namesz
- 3);
2110 if (pnote1
->end
< pnote2
->start
)
2112 if (pnote1
->start
> pnote2
->end
)
2115 /* Overlaps - we should merge the two ranges. */
2116 if (pnote1
->start
< pnote2
->start
)
2118 if (pnote1
->end
> pnote2
->end
)
2120 if (pnote1
->end
< pnote2
->end
)
2123 /* Put OPEN notes before function notes. */
2124 if (is_open_note (pnote1
) && ! is_open_note (pnote2
))
2126 if (! is_open_note (pnote1
) && is_open_note (pnote2
))
2132 /* This sorting function is used to get the notes into an order
2133 that makes eliminating address ranges easier. */
2136 sort_gnu_build_notes (const void * data1
, const void * data2
)
2138 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2139 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2141 if (pnote1
->note
.type
!= pnote2
->note
.type
)
2143 /* Move deleted notes to the end. */
2144 if (is_deleted_note (pnote1
)) /* 1: OFD 2: OFD */
2147 /* Move OPEN notes to the start. */
2148 if (is_open_note (pnote1
)) /* 1: OF 2: OFD */
2151 if (is_deleted_note (pnote2
)) /* 1: F 2: O D */
2154 return 1; /* 1: F 2: O */
2157 /* Sort by starting address. */
2158 if (pnote1
->start
< pnote2
->start
)
2160 if (pnote1
->start
> pnote2
->start
)
2163 /* Then by end address (bigger range first). */
2164 if (pnote1
->end
> pnote2
->end
)
2166 if (pnote1
->end
< pnote2
->end
)
2169 /* Then by attribute type. */
2170 if (pnote1
->note
.namesz
> 4
2171 && pnote2
->note
.namesz
> 4
2172 && pnote1
->note
.namedata
[3] != pnote2
->note
.namedata
[3])
2173 return pnote1
->note
.namedata
[3] - pnote2
->note
.namedata
[3];
2178 /* Merge the notes on SEC, removing redundant entries.
2179 Returns the new, smaller size of the section upon success. */
2181 static bfd_size_type
2182 merge_gnu_build_notes (bfd
* abfd
,
2185 bfd_byte
* contents
)
2187 objcopy_internal_note
* pnotes_end
;
2188 objcopy_internal_note
* pnotes
= NULL
;
2189 objcopy_internal_note
* pnote
;
2190 bfd_size_type remain
= size
;
2191 unsigned version_1_seen
= 0;
2192 unsigned version_2_seen
= 0;
2193 unsigned version_3_seen
= 0;
2194 const char * err
= NULL
;
2195 bfd_byte
* in
= contents
;
2196 unsigned long previous_func_start
= 0;
2197 unsigned long previous_open_start
= 0;
2198 unsigned long previous_func_end
= 0;
2199 unsigned long previous_open_end
= 0;
2202 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
2208 /* If there are relocs associated with this section then we
2209 cannot safely merge it. */
2210 relpp
= (arelent
**) xmalloc (relsize
);
2211 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, isympp
);
2216 non_fatal (_("%s[%s]: Cannot merge - there are relocations against this section"),
2217 bfd_get_filename (abfd
), bfd_section_name (sec
));
2222 /* Make a copy of the notes and convert to our internal format.
2223 Minimum size of a note is 12 bytes. Also locate the version
2224 notes and check them. */
2225 pnote
= pnotes
= (objcopy_internal_note
*)
2226 xcalloc ((size
/ 12), sizeof (* pnote
));
2227 while (remain
>= 12)
2231 pnote
->note
.namesz
= bfd_get_32 (abfd
, in
);
2232 pnote
->note
.descsz
= bfd_get_32 (abfd
, in
+ 4);
2233 pnote
->note
.type
= bfd_get_32 (abfd
, in
+ 8);
2234 pnote
->padded_namesz
= (pnote
->note
.namesz
+ 3) & ~3;
2236 if (((pnote
->note
.descsz
+ 3) & ~3) != pnote
->note
.descsz
)
2238 err
= _("corrupt GNU build attribute note: description size not a factor of 4");
2242 if (pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_OPEN
2243 && pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_FUNC
)
2245 err
= _("corrupt GNU build attribute note: wrong note type");
2249 if (pnote
->padded_namesz
+ pnote
->note
.descsz
+ 12 > remain
)
2251 err
= _("corrupt GNU build attribute note: note too big");
2255 if (pnote
->note
.namesz
< 2)
2257 err
= _("corrupt GNU build attribute note: name too small");
2261 pnote
->note
.namedata
= (char *)(in
+ 12);
2262 pnote
->note
.descdata
= (char *)(in
+ 12 + pnote
->padded_namesz
);
2264 remain
-= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2265 in
+= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2267 if (pnote
->note
.namesz
> 2
2268 && pnote
->note
.namedata
[0] == '$'
2269 && pnote
->note
.namedata
[1] == GNU_BUILD_ATTRIBUTE_VERSION
2270 && pnote
->note
.namedata
[2] == '1')
2272 else if (is_version_note (pnote
))
2274 if (pnote
->note
.namedata
[4] == '2')
2276 else if (pnote
->note
.namedata
[4] == '3')
2280 err
= _("corrupt GNU build attribute note: unsupported version");
2285 switch (pnote
->note
.descsz
)
2292 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2293 /* FIXME: For version 1 and 2 notes we should try to
2294 calculate the end address by finding a symbol whose
2295 value is START, and then adding in its size.
2297 For now though, since v1 and v2 was not intended to
2298 handle gaps, we chose an artificially large end
2304 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2305 end
= bfd_get_32 (abfd
, pnote
->note
.descdata
+ 4);
2309 start
= bfd_get_64 (abfd
, pnote
->note
.descdata
);
2310 end
= bfd_get_64 (abfd
, pnote
->note
.descdata
+ 8);
2314 err
= _("corrupt GNU build attribute note: bad description size");
2319 /* This can happen with PPC64LE binaries where empty notes are
2320 encoded as start = end + 4. */
2323 if (is_open_note (pnote
))
2326 previous_open_start
= start
;
2328 pnote
->start
= previous_open_start
;
2331 previous_open_end
= end
;
2333 pnote
->end
= previous_open_end
;
2338 previous_func_start
= start
;
2340 pnote
->start
= previous_func_start
;
2343 previous_func_end
= end
;
2345 pnote
->end
= previous_func_end
;
2348 if (pnote
->note
.namedata
[pnote
->note
.namesz
- 1] != 0)
2350 err
= _("corrupt GNU build attribute note: name not NUL terminated");
2359 /* Check that the notes are valid. */
2362 err
= _("corrupt GNU build attribute notes: excess data at end");
2366 if (version_1_seen
== 0 && version_2_seen
== 0 && version_3_seen
== 0)
2369 err
= _("bad GNU build attribute notes: no known versions detected");
2372 /* This happens with glibc. No idea why. */
2373 non_fatal (_("%s[%s]: Warning: version note missing - assuming version 3"),
2374 bfd_get_filename (abfd
), bfd_section_name (sec
));
2379 if ( (version_1_seen
> 0 && version_2_seen
> 0)
2380 || (version_1_seen
> 0 && version_3_seen
> 0)
2381 || (version_2_seen
> 0 && version_3_seen
> 0))
2383 err
= _("bad GNU build attribute notes: multiple different versions");
2387 /* We are now only supporting the merging v3+ notes
2388 - it makes things much simpler. */
2389 if (version_3_seen
== 0)
2391 merge_debug ("%s: skipping merge - not using v3 notes", bfd_section_name (sec
));
2395 merge_debug ("Merging section %s which contains %ld notes\n",
2396 sec
->name
, pnotes_end
- pnotes
);
2398 /* Sort the notes. */
2399 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
),
2400 compare_gnu_build_notes
);
2403 merge_debug ("Results of initial sort:\n");
2404 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2405 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2406 (pnote
->note
.namedata
- (char *) contents
) - 12,
2407 pnote
->start
, pnote
->end
,
2409 pnote
->note
.namedata
[3],
2414 /* Now merge the notes. The rules are:
2415 1. If a note has a zero range, it can be eliminated.
2416 2. If two notes have the same namedata then:
2417 2a. If one note's range is fully covered by the other note
2418 then it can be deleted.
2419 2b. If one note's range partially overlaps or adjoins the
2420 other note then if they are both of the same type (open
2421 or func) then they can be merged and one deleted. If
2422 they are of different types then they cannot be merged. */
2423 objcopy_internal_note
* prev_note
= NULL
;
2425 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2427 /* Skip already deleted notes.
2428 FIXME: Can this happen ? We are scanning forwards and
2429 deleting backwards after all. */
2430 if (is_deleted_note (pnote
))
2433 /* Rule 1 - delete 0-range notes. */
2434 if (pnote
->start
== pnote
->end
)
2436 merge_debug ("Delete note at offset %#08lx - empty range\n",
2437 (pnote
->note
.namedata
- (char *) contents
) - 12);
2438 pnote
->note
.type
= 0;
2443 objcopy_internal_note
* back
;
2445 /* Rule 2: Check to see if there is an identical previous note. */
2446 for (iter
= 0, back
= prev_note
? prev_note
: pnote
- 1;
2450 if (is_deleted_note (back
))
2453 /* Our sorting function should have placed all identically
2454 attributed notes together, so if we see a note of a different
2455 attribute type stop searching. */
2456 if (back
->note
.namesz
!= pnote
->note
.namesz
2457 || memcmp (back
->note
.namedata
,
2458 pnote
->note
.namedata
, pnote
->note
.namesz
) != 0)
2461 if (back
->start
== pnote
->start
2462 && back
->end
== pnote
->end
)
2464 merge_debug ("Delete note at offset %#08lx - duplicate of note at offset %#08lx\n",
2465 (pnote
->note
.namedata
- (char *) contents
) - 12,
2466 (back
->note
.namedata
- (char *) contents
) - 12);
2467 pnote
->note
.type
= 0;
2472 if (contained_by (pnote
, back
))
2474 merge_debug ("Delete note at offset %#08lx - fully contained by note at %#08lx\n",
2475 (pnote
->note
.namedata
- (char *) contents
) - 12,
2476 (back
->note
.namedata
- (char *) contents
) - 12);
2477 pnote
->note
.type
= 0;
2482 /* This should not happen as we have sorted the
2483 notes with earlier starting addresses first. */
2484 if (contained_by (back
, pnote
))
2485 merge_debug ("ERROR: UNEXPECTED CONTAINMENT\n");
2489 if (overlaps_or_adjoins (back
, pnote
)
2490 && is_func_note (back
) == is_func_note (pnote
))
2492 merge_debug ("Delete note at offset %#08lx - merge into note at %#08lx\n",
2493 (pnote
->note
.namedata
- (char *) contents
) - 12,
2494 (back
->note
.namedata
- (char *) contents
) - 12);
2496 back
->end
= back
->end
> pnote
->end
? back
->end
: pnote
->end
;
2497 back
->start
= back
->start
< pnote
->start
? back
->start
: pnote
->start
;
2498 pnote
->note
.type
= 0;
2502 /* Don't scan too far back however. */
2505 /* FIXME: Not sure if this can ever be triggered. */
2506 merge_debug ("ITERATION LIMIT REACHED\n");
2511 if (! is_deleted_note (pnote
))
2513 /* Keep a pointer to this note, so that we can
2514 start the next search for rule 2 matches here. */
2517 merge_debug ("Unable to do anything with note at %#08lx\n",
2518 (pnote
->note
.namedata
- (char *) contents
) - 12);
2523 /* Resort the notes. */
2524 merge_debug ("Final sorting of notes\n");
2525 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
), sort_gnu_build_notes
);
2527 /* Reconstruct the ELF notes. */
2528 bfd_byte
* new_contents
;
2531 bfd_size_type new_size
;
2532 bfd_vma prev_start
= 0;
2533 bfd_vma prev_end
= 0;
2535 /* Not sure how, but the notes might grow in size.
2536 (eg see PR 1774507). Allow for this here. */
2537 new = new_contents
= xmalloc (size
* 2);
2538 for (pnote
= pnotes
, old
= contents
;
2542 bfd_size_type note_size
= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2544 if (! is_deleted_note (pnote
))
2546 /* Create the note, potentially using the
2547 address range of the previous note. */
2548 if (pnote
->start
== prev_start
&& pnote
->end
== prev_end
)
2550 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2551 bfd_put_32 (abfd
, 0, new + 4);
2552 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2554 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2555 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2556 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2557 new += pnote
->padded_namesz
;
2561 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2562 bfd_put_32 (abfd
, is_64bit (abfd
) ? 16 : 8, new + 4);
2563 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2565 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2566 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2567 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2568 new += pnote
->padded_namesz
;
2569 if (is_64bit (abfd
))
2571 bfd_put_64 (abfd
, pnote
->start
, new);
2572 bfd_put_64 (abfd
, pnote
->end
, new + 8);
2577 bfd_put_32 (abfd
, pnote
->start
, new);
2578 bfd_put_32 (abfd
, pnote
->end
, new + 4);
2582 prev_start
= pnote
->start
;
2583 prev_end
= pnote
->end
;
2591 merge_debug ("Results of merge:\n");
2592 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2593 if (! is_deleted_note (pnote
))
2594 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2595 (pnote
->note
.namedata
- (char *) contents
) - 12,
2596 pnote
->start
, pnote
->end
,
2598 pnote
->note
.namedata
[3],
2603 new_size
= new - new_contents
;
2604 if (new_size
< size
)
2606 memcpy (contents
, new_contents
, new_size
);
2609 free (new_contents
);
2614 bfd_set_error (bfd_error_bad_value
);
2615 bfd_nonfatal_message (NULL
, abfd
, sec
, err
);
2624 check_new_section_flags (flagword flags
, bfd
*abfd
, const char * secname
)
2626 /* Only set the SEC_COFF_SHARED flag on COFF files.
2627 The same bit value is used by ELF targets to indicate
2628 compressed sections, and setting that flag here breaks
2630 if ((flags
& SEC_COFF_SHARED
)
2631 && bfd_get_flavour (abfd
) != bfd_target_coff_flavour
)
2633 non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"),
2634 bfd_get_filename (abfd
), secname
);
2635 flags
&= ~ SEC_COFF_SHARED
;
2638 /* Report a fatal error if 'large' is used with a non-x86-64 ELF target.
2639 Suppress the error for non-ELF targets to allow -O binary and formats that
2640 use the bit value SEC_ELF_LARGE for other purposes. */
2641 if ((flags
& SEC_ELF_LARGE
) != 0
2642 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
2643 && get_elf_backend_data (abfd
)->elf_machine_code
!= EM_X86_64
)
2645 fatal (_ ("%s[%s]: 'large' flag is ELF x86-64 specific"),
2646 bfd_get_filename (abfd
), secname
);
2647 flags
&= ~SEC_ELF_LARGE
;
2654 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
2656 /* This is only relevant to Coff targets. */
2657 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
2660 && bfd_get_flavour (input_bfd
) == bfd_target_coff_flavour
)
2661 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
2662 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
2666 /* Copy object file IBFD onto OBFD.
2667 Returns TRUE upon success, FALSE otherwise. */
2670 copy_object (bfd
*ibfd
, bfd
*obfd
, const bfd_arch_info_type
*input_arch
)
2674 asection
**osections
= NULL
;
2676 asection
*gnu_debuglink_section
= NULL
;
2677 bfd_size_type
*gaps
= NULL
;
2678 bfd_size_type max_gap
= 0;
2681 enum bfd_architecture iarch
;
2683 unsigned int num_sec
, i
;
2685 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
2686 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
2687 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
2689 /* PR 17636: Call non-fatal so that we return to our parent who
2690 may need to tidy temporary files. */
2691 non_fatal (_("unable to change endianness of '%s'"),
2692 bfd_get_archive_filename (ibfd
));
2696 if (ibfd
->read_only
)
2698 non_fatal (_("unable to modify '%s' due to errors"),
2699 bfd_get_archive_filename (ibfd
));
2703 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2705 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
2709 if (ibfd
->sections
== NULL
)
2711 non_fatal (_("error: the input file '%s' has no sections"),
2712 bfd_get_archive_filename (ibfd
));
2716 /* This is a no-op on non-Coff targets. */
2717 set_long_section_mode (obfd
, ibfd
, long_section_names
);
2719 /* Set the Verilog output endianness based upon the input file's
2720 endianness. We may not be producing verilog format output,
2721 but testing this just adds extra code this is not really
2723 VerilogDataEndianness
= ibfd
->xvec
->byteorder
;
2725 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
2727 if (strip_section_headers
)
2729 ibfd
->flags
|= BFD_NO_SECTION_HEADER
;
2730 strip_symbols
= STRIP_ALL
;
2736 if ((do_debug_sections
& compress
) != 0
2737 && do_debug_sections
!= compress
)
2739 non_fatal (_ ("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi|"
2740 "zstd] is unsupported on `%s'"),
2741 bfd_get_archive_filename (ibfd
));
2745 if (do_elf_stt_common
)
2747 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
2748 bfd_get_archive_filename (ibfd
));
2752 if (strip_section_headers
)
2754 non_fatal (_("--strip-section-headers is unsupported on `%s'"),
2755 bfd_get_archive_filename (ibfd
));
2761 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
2762 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
2763 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2772 start
= bfd_get_start_address (ibfd
);
2773 start
+= change_start
;
2776 /* Neither the start address nor the flags
2777 need to be set for a core file. */
2778 if (bfd_get_format (obfd
) != bfd_core
)
2782 flags
= bfd_get_file_flags (ibfd
);
2783 flags
|= bfd_flags_to_set
;
2784 flags
&= ~bfd_flags_to_clear
;
2785 flags
&= bfd_applicable_file_flags (obfd
);
2787 if (strip_symbols
== STRIP_ALL
)
2788 flags
&= ~HAS_RELOC
;
2790 if (!bfd_set_start_address (obfd
, start
)
2791 || !bfd_set_file_flags (obfd
, flags
))
2793 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2798 /* Copy architecture of input file to output file. */
2799 iarch
= bfd_get_arch (ibfd
);
2800 imach
= bfd_get_mach (ibfd
);
2803 if (iarch
== bfd_arch_unknown
)
2805 iarch
= input_arch
->arch
;
2806 imach
= input_arch
->mach
;
2809 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
2810 bfd_get_archive_filename (ibfd
));
2812 if (iarch
== bfd_arch_unknown
2813 && bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2814 && bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2816 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
2820 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
2821 && (ibfd
->target_defaulted
2822 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
2824 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
2825 non_fatal (_("Unable to recognise the format of the input file `%s'"),
2826 bfd_get_archive_filename (ibfd
));
2828 non_fatal (_("Output file cannot represent architecture `%s'"),
2829 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
2830 bfd_get_mach (ibfd
)));
2834 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2836 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2840 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2841 && bfd_pei_p (obfd
))
2843 /* Set up PE parameters. */
2844 pe_data_type
*pe
= pe_data (obfd
);
2846 /* Copy PE parameters before changing them. */
2847 if (bfd_get_flavour (ibfd
) == bfd_target_coff_flavour
2848 && bfd_pei_p (ibfd
))
2850 pe
->pe_opthdr
= pe_data (ibfd
)->pe_opthdr
;
2853 pe
->timestamp
= pe_data (ibfd
)->coff
.timestamp
;
2858 if (pe_file_alignment
!= (bfd_vma
) -1)
2859 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
2861 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
2863 if (pe_heap_commit
!= (bfd_vma
) -1)
2864 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
2866 if (pe_heap_reserve
!= (bfd_vma
) -1)
2867 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
2869 if (pe_image_base
!= (bfd_vma
) -1)
2870 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
2872 if (pe_section_alignment
!= (bfd_vma
) -1)
2873 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
2875 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
2877 if (pe_stack_commit
!= (bfd_vma
) -1)
2878 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
2880 if (pe_stack_reserve
!= (bfd_vma
) -1)
2881 pe
->pe_opthdr
.SizeOfStackReserve
= pe_stack_reserve
;
2883 if (pe_subsystem
!= -1)
2884 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
2886 if (pe_major_subsystem_version
!= -1)
2887 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
2889 if (pe_minor_subsystem_version
!= -1)
2890 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
2892 if (pe_file_alignment
> pe_section_alignment
)
2894 non_fatal (_("warning: file alignment (0x%" PRIx64
2895 ") > section alignment (0x%" PRIx64
")"),
2896 (uint64_t) pe_file_alignment
,
2897 (uint64_t) pe_section_alignment
);
2903 if (osympp
!= isympp
)
2909 symsize
= bfd_get_symtab_upper_bound (ibfd
);
2912 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2916 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
2917 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
2920 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2923 /* PR 17512: file: d6323821
2924 If the symbol table could not be loaded do not pretend that we have
2925 any symbols. This trips us up later on when we load the relocs. */
2929 osympp
= isympp
= NULL
;
2932 /* BFD mandates that all output sections be created and sizes set before
2933 any output is done. Thus, we traverse all sections multiple times. */
2934 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
2936 if (!extract_symbol
)
2937 setup_bfd_headers (ibfd
, obfd
);
2939 if (add_sections
!= NULL
)
2941 struct section_add
*padd
;
2942 struct section_list
*pset
;
2944 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
2948 pset
= find_section_list (padd
->name
, false,
2949 SECTION_CONTEXT_SET_FLAGS
);
2952 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
2953 flags
= check_new_section_flags (flags
, obfd
, padd
->name
);
2956 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
2958 /* bfd_make_section_with_flags() does not return very helpful
2959 error codes, so check for the most likely user error first. */
2960 if (bfd_get_section_by_name (obfd
, padd
->name
))
2962 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2963 _("can't add section '%s'"), padd
->name
);
2968 /* We use LINKER_CREATED here so that the backend hooks
2969 will create any special section type information,
2970 instead of presuming we know what we're doing merely
2971 because we set the flags. */
2972 padd
->section
= bfd_make_section_with_flags
2973 (obfd
, padd
->name
, flags
| SEC_LINKER_CREATED
);
2974 if (padd
->section
== NULL
)
2976 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2977 _("can't create section `%s'"),
2983 if (!bfd_set_section_size (padd
->section
, padd
->size
))
2985 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2989 pset
= find_section_list (padd
->name
, false,
2990 SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
);
2992 && !bfd_set_section_vma (padd
->section
, pset
->vma_val
))
2994 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2998 pset
= find_section_list (padd
->name
, false,
2999 SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
);
3002 padd
->section
->lma
= pset
->lma_val
;
3004 if (!bfd_set_section_alignment
3005 (padd
->section
, bfd_section_alignment (padd
->section
)))
3007 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3014 if (update_sections
!= NULL
)
3016 struct section_add
*pupdate
;
3018 for (pupdate
= update_sections
;
3020 pupdate
= pupdate
->next
)
3022 pupdate
->section
= bfd_get_section_by_name (ibfd
, pupdate
->name
);
3023 if (pupdate
->section
== NULL
)
3025 non_fatal (_("error: %s not found, can't be updated"), pupdate
->name
);
3029 osec
= pupdate
->section
->output_section
;
3030 if (!bfd_set_section_size (osec
, pupdate
->size
))
3032 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
3038 merged_note_section
* merged_note_sections
= NULL
;
3041 /* This palaver is necessary because we must set the output
3042 section size first, before its contents are ready. */
3043 for (osec
= ibfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
3045 if (! is_mergeable_note_section (ibfd
, osec
))
3048 /* If the section is going to be completly deleted then
3049 do not bother to merge it. */
3050 if (osec
->output_section
== NULL
)
3053 bfd_size_type size
= bfd_section_size (osec
);
3056 /* This can happen, eg when stripping a binary for a second
3057 time. See BZ 2121365 for an example. */
3060 merged_note_section
* merged
= xmalloc (sizeof * merged
);
3061 merged
->contents
= NULL
;
3062 if (! bfd_get_full_section_contents (ibfd
, osec
, & merged
->contents
))
3064 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3065 _("warning: could not load note section"));
3070 merged
->size
= merge_gnu_build_notes (ibfd
, osec
, size
,
3073 /* FIXME: Once we have read the contents in, we must write
3074 them out again. So even if the mergeing has achieved
3075 nothing we still add this entry to the merge list. */
3077 if (size
!= merged
->size
3078 && !bfd_set_section_size (osec
->output_section
, merged
->size
))
3080 bfd_nonfatal_message (NULL
, obfd
, osec
,
3081 _("warning: failed to set merged notes size"));
3082 free (merged
->contents
);
3087 /* Add section to list of merged sections. */
3089 merged
->next
= merged_note_sections
;
3090 merged_note_sections
= merged
;
3094 if (dump_sections
!= NULL
)
3096 struct section_add
* pdump
;
3098 for (pdump
= dump_sections
; pdump
!= NULL
; pdump
= pdump
->next
)
3103 osec
= bfd_get_section_by_name (ibfd
, pdump
->name
);
3106 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3107 _("can't dump section '%s' - it does not exist"),
3112 if ((bfd_section_flags (osec
) & SEC_HAS_CONTENTS
) == 0)
3114 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3115 _("can't dump section - it has no contents"));
3119 bfd_size_type size
= bfd_section_size (osec
);
3120 /* Note - we allow the dumping of zero-sized sections,
3121 creating an empty file. */
3123 f
= fopen (pdump
->filename
, FOPEN_WB
);
3126 bfd_nonfatal_message (pdump
->filename
, NULL
, NULL
,
3127 _("could not open section dump file"));
3131 if (bfd_malloc_and_get_section (ibfd
, osec
, &contents
))
3133 if (size
!= 0 && fwrite (contents
, 1, size
, f
) != size
)
3135 non_fatal (_("error writing section contents to %s (error: %s)"),
3144 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3145 _("could not retrieve section contents"));
3152 if (gnu_debuglink_filename
!= NULL
)
3154 /* PR 15125: Give a helpful warning message if
3155 the debuglink section already exists, and
3156 allow the rest of the copy to complete. */
3157 if (bfd_get_section_by_name (obfd
, ".gnu_debuglink"))
3159 non_fatal (_("%s: debuglink section already exists"),
3160 bfd_get_filename (ibfd
));
3161 gnu_debuglink_filename
= NULL
;
3165 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
3166 (obfd
, gnu_debuglink_filename
);
3168 if (gnu_debuglink_section
== NULL
)
3170 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3171 _("cannot create debug link section `%s'"),
3172 gnu_debuglink_filename
);
3176 /* Special processing for PE format files. We
3177 have no way to distinguish PE from COFF here. */
3178 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
3180 bfd_vma debuglink_vma
;
3181 asection
* highest_section
;
3183 /* The PE spec requires that all sections be adjacent and sorted
3184 in ascending order of VMA. It also specifies that debug
3185 sections should be last. This is despite the fact that debug
3186 sections are not loaded into memory and so in theory have no
3189 This means that the debuglink section must be given a non-zero
3190 VMA which makes it contiguous with other debug sections. So
3191 walk the current section list, find the section with the
3192 highest VMA and start the debuglink section after that one. */
3193 for (osec
= obfd
->sections
, highest_section
= NULL
;
3197 && (highest_section
== NULL
3198 || osec
->vma
> highest_section
->vma
))
3199 highest_section
= osec
;
3201 if (highest_section
)
3202 debuglink_vma
= BFD_ALIGN (highest_section
->vma
3203 + highest_section
->size
,
3204 /* FIXME: We ought to be using
3205 COFF_PAGE_SIZE here or maybe
3206 bfd_section_alignment() (if it
3207 was set) but since this is for PE
3208 and we know the required alignment
3209 it is easier just to hard code it. */
3212 /* Umm, not sure what to do in this case. */
3213 debuglink_vma
= 0x1000;
3215 bfd_set_section_vma (gnu_debuglink_section
, debuglink_vma
);
3220 num_sec
= bfd_count_sections (obfd
);
3222 && (gap_fill_set
|| pad_to_set
))
3226 /* We must fill in gaps between the sections and/or we must pad
3227 the last section to a specified address. We do this by
3228 grabbing a list of the sections, sorting them by VMA, and
3229 increasing the section sizes as required to fill the gaps.
3230 We write out the gap contents below. */
3232 osections
= xmalloc (num_sec
* sizeof (*osections
));
3234 bfd_map_over_sections (obfd
, get_sections
, &set
);
3236 qsort (osections
, num_sec
, sizeof (*osections
), compare_section_lma
);
3238 gaps
= xmalloc (num_sec
* sizeof (*gaps
));
3239 memset (gaps
, 0, num_sec
* sizeof (*gaps
));
3243 for (i
= 0; i
< num_sec
- 1; i
++)
3246 bfd_size_type size
; /* Octets. */
3247 bfd_vma gap_start
, gap_stop
; /* Octets. */
3248 unsigned int opb1
= bfd_octets_per_byte (obfd
, osections
[i
]);
3249 unsigned int opb2
= bfd_octets_per_byte (obfd
, osections
[i
+1]);
3251 flags
= bfd_section_flags (osections
[i
]);
3252 if ((flags
& SEC_HAS_CONTENTS
) == 0
3253 || (flags
& SEC_LOAD
) == 0)
3256 size
= bfd_section_size (osections
[i
]);
3257 gap_start
= bfd_section_lma (osections
[i
]) * opb1
+ size
;
3258 gap_stop
= bfd_section_lma (osections
[i
+ 1]) * opb2
;
3259 if (gap_start
< gap_stop
)
3261 if (!bfd_set_section_size (osections
[i
],
3262 size
+ (gap_stop
- gap_start
)))
3264 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
3265 _("Can't fill gap after section"));
3269 gaps
[i
] = gap_stop
- gap_start
;
3270 if (max_gap
< gap_stop
- gap_start
)
3271 max_gap
= gap_stop
- gap_start
;
3278 bfd_vma lma
; /* Octets. */
3279 bfd_size_type size
; /* Octets. */
3280 unsigned int opb
= bfd_octets_per_byte (obfd
, osections
[num_sec
- 1]);
3281 bfd_vma _pad_to
= pad_to
* opb
;
3283 lma
= bfd_section_lma (osections
[num_sec
- 1]) * opb
;
3284 size
= bfd_section_size (osections
[num_sec
- 1]);
3285 if (lma
+ size
< _pad_to
)
3287 if (!bfd_set_section_size (osections
[num_sec
- 1], _pad_to
- lma
))
3289 bfd_nonfatal_message (NULL
, obfd
, osections
[num_sec
- 1],
3290 _("can't add padding"));
3295 gaps
[num_sec
- 1] = _pad_to
- (lma
+ size
);
3296 if (max_gap
< _pad_to
- (lma
+ size
))
3297 max_gap
= _pad_to
- (lma
+ size
);
3303 /* Symbol filtering must happen after the output sections
3304 have been created, but before their contents are set. */
3306 if (convert_debugging
)
3307 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, false);
3309 if ((obfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0
3310 && (obfd
->flags
& HAS_RELOC
) == 0)
3312 if (bfd_keep_unused_section_symbols (obfd
) || keep_section_symbols
)
3314 /* Non-relocatable inputs may not have the unused section
3315 symbols. Mark all section symbols as used to generate
3318 for (asect
= obfd
->sections
; asect
!= NULL
; asect
= asect
->next
)
3320 asect
->symbol
->flags
|= BSF_SECTION_SYM_USED
;
3324 /* Non-relocatable inputs may have the unused section symbols.
3325 Mark all section symbols as unused to excluded them. */
3327 for (s
= 0; s
< symcount
; s
++)
3328 if ((isympp
[s
]->flags
& BSF_SECTION_SYM_USED
))
3329 isympp
[s
]->flags
&= ~BSF_SECTION_SYM_USED
;
3333 if (strip_symbols
== STRIP_DEBUG
3334 || strip_symbols
== STRIP_ALL
3335 || strip_symbols
== STRIP_UNNEEDED
3336 || strip_symbols
== STRIP_NONDEBUG
3337 || strip_symbols
== STRIP_DWO
3338 || strip_symbols
== STRIP_NONDWO
3339 || discard_locals
!= LOCALS_UNDEF
3341 || htab_elements (strip_specific_htab
) != 0
3342 || htab_elements (keep_specific_htab
) != 0
3343 || htab_elements (localize_specific_htab
) != 0
3344 || htab_elements (globalize_specific_htab
) != 0
3345 || htab_elements (keepglobal_specific_htab
) != 0
3346 || htab_elements (weaken_specific_htab
) != 0
3347 || htab_elements (redefine_specific_htab
) != 0
3348 || prefix_symbols_string
3351 || convert_debugging
3352 || change_leading_char
3353 || remove_leading_char
3354 || section_rename_list
3358 /* Mark symbols used in output relocations so that they
3359 are kept, even if they are local labels or static symbols.
3361 Note we iterate over the input sections examining their
3362 relocations since the relocations for the output sections
3363 haven't been set yet. mark_symbols_used_in_relocations will
3364 ignore input sections which have no corresponding output
3366 if (strip_symbols
!= STRIP_ALL
)
3368 bfd_set_error (bfd_error_no_error
);
3369 bfd_map_over_sections (ibfd
,
3370 mark_symbols_used_in_relocations
,
3372 if (bfd_get_error () != bfd_error_no_error
)
3379 osympp
= (asymbol
**) xmalloc ((symcount
+ add_symbols
+ 1) * sizeof (asymbol
*));
3380 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
3383 if (dhandle
!= NULL
)
3387 res
= write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
);
3396 bfd_set_symtab (obfd
, osympp
, symcount
);
3398 /* This has to happen before section positions are set. */
3399 bfd_map_over_sections (ibfd
, copy_relocations_in_section
, obfd
);
3403 /* This has to happen after the symbol table has been set. */
3404 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
3408 if (add_sections
!= NULL
)
3410 struct section_add
*padd
;
3412 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
3414 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
3417 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3423 if (update_sections
!= NULL
)
3425 struct section_add
*pupdate
;
3427 for (pupdate
= update_sections
;
3429 pupdate
= pupdate
->next
)
3431 osec
= pupdate
->section
->output_section
;
3432 if (! bfd_set_section_contents (obfd
, osec
, pupdate
->contents
,
3435 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
3441 if (merged_note_sections
!= NULL
)
3443 merged_note_section
* merged
= NULL
;
3445 for (osec
= obfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
3447 if (! is_mergeable_note_section (obfd
, osec
))
3451 merged
= merged_note_sections
;
3453 /* It is likely that output sections are in the same order
3454 as the input sections, but do not assume that this is
3456 if (merged
->sec
->output_section
!= osec
)
3458 for (merged
= merged_note_sections
;
3460 merged
= merged
->next
)
3461 if (merged
->sec
->output_section
== osec
)
3466 bfd_nonfatal_message
3468 _("error: failed to locate merged notes"));
3473 if (merged
->contents
== NULL
)
3475 bfd_nonfatal_message
3477 _("error: failed to merge notes"));
3481 if (! bfd_set_section_contents (obfd
, osec
, merged
->contents
, 0,
3484 bfd_nonfatal_message
3486 _("error: failed to copy merged notes into output"));
3490 merged
= merged
->next
;
3493 /* Free the memory. */
3494 merged_note_section
* next
;
3495 for (merged
= merged_note_sections
; merged
!= NULL
; merged
= next
)
3497 next
= merged
->next
;
3498 free (merged
->contents
);
3502 else if (merge_notes
&& ! is_strip
&& ! strip_section_headers
)
3503 non_fatal (_("%s: Could not find any mergeable note sections"),
3504 bfd_get_filename (ibfd
));
3506 if (gnu_debuglink_filename
!= NULL
)
3508 if (! bfd_fill_in_gnu_debuglink_section
3509 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
3511 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3512 _("cannot fill debug link section `%s'"),
3513 gnu_debuglink_filename
);
3522 /* Fill in the gaps. */
3525 buf
= (bfd_byte
*) xmalloc (max_gap
);
3526 memset (buf
, gap_fill
, max_gap
);
3528 for (i
= 0; i
< num_sec
; i
++)
3536 off
= bfd_section_size (osections
[i
]) - left
;
3547 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
3550 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
3566 /* Allow the BFD backend to copy any private data it understands
3567 from the input BFD to the output BFD. This is done last to
3568 permit the routine to look at the filtered symbol table, which is
3569 important for the ECOFF code at least. */
3570 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
3572 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3573 _("error copying private BFD data"));
3577 /* Switch to the alternate machine code. We have to do this at the
3578 very end, because we only initialize the header when we create
3579 the first section. */
3580 if (use_alt_mach_code
!= 0)
3582 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
3584 non_fatal (_("this target does not support %lu alternative machine codes"),
3586 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
3588 non_fatal (_("treating that number as an absolute e_machine value instead"));
3589 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
3592 non_fatal (_("ignoring the alternative value"));
3599 /* Read each archive element in turn from IBFD, copy the
3600 contents to temp file, and keep the temp file handle.
3601 If 'force_output_target' is TRUE then make sure that
3602 all elements in the new archive are of the type
3606 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
3607 bool force_output_target
,
3608 const bfd_arch_info_type
*input_arch
)
3612 struct name_list
*next
;
3616 bfd
**ptr
= &obfd
->archive_head
;
3623 /* PR 24281: It is not clear what should happen when copying a thin archive.
3624 One part is straight forward - if the output archive is in a different
3625 directory from the input archive then any relative paths in the library
3626 should be adjusted to the new location. But if any transformation
3627 options are active (eg strip, rename, add, etc) then the implication is
3628 that these should be applied to the files pointed to by the archive.
3629 But since objcopy is not destructive, this means that new files must be
3630 created, and there is no guidance for the names of the new files. (Plus
3631 this conflicts with one of the goals of thin libraries - only taking up
3632 a minimal amount of space in the file system).
3634 So for now we fail if an attempt is made to copy such libraries. */
3635 if (ibfd
->is_thin_archive
)
3638 bfd_set_error (bfd_error_invalid_operation
);
3639 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3640 _("sorry: copying thin archives is not currently supported"));
3641 goto cleanup_and_exit
;
3644 /* Make a temp directory to hold the contents. */
3645 dir
= make_tempdir (bfd_get_filename (obfd
));
3647 fatal (_("cannot create tempdir for archive copying (error: %s)"),
3650 if (strip_symbols
== STRIP_ALL
)
3651 obfd
->has_armap
= false;
3653 obfd
->has_armap
= ibfd
->has_armap
;
3654 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
3657 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
3659 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
3661 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
3664 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
3665 goto cleanup_and_exit
;
3668 while (!status
&& this_element
!= NULL
)
3671 bfd
*output_element
;
3673 int stat_status
= 0;
3675 const char *element_name
;
3677 element_name
= bfd_get_filename (this_element
);
3678 /* PR binutils/17533: Do not allow directory traversal
3679 outside of the current directory tree by archive members. */
3680 if (! is_valid_archive_path (element_name
))
3682 non_fatal (_("warning: illegal pathname found in archive member: %s"),
3684 /* PR binutils/31250: But there tools which create archives
3685 containing absolute paths, so instead of failing here, try to
3686 create a suitable alternative pathname. */
3687 element_name
= lbasename (element_name
);
3688 non_fatal (_("warning: using the basename of the member instead: %s"),
3692 /* Create an output file for this member. */
3693 output_name
= concat (dir
, "/", element_name
, (char *) 0);
3695 /* If the file already exists, make another temp dir. */
3696 if (stat (output_name
, &buf
) >= 0)
3698 char * tmpdir
= make_tempdir (output_name
);
3703 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
3705 bfd_close (this_element
);
3707 goto cleanup_and_exit
;
3710 struct name_list
*l
= xmalloc (sizeof (*l
));
3715 output_name
= concat (tmpdir
, "/", element_name
, (char *) 0);
3720 memset (&buf
, 0, sizeof (buf
));
3721 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
3723 if (stat_status
!= 0)
3724 non_fatal (_("internal stat error on %s"), element_name
);
3727 struct name_list
*l
= xmalloc (sizeof (*l
));
3728 l
->name
= output_name
;
3733 ok_object
= bfd_check_format (this_element
, bfd_object
);
3735 bfd_nonfatal_message (NULL
, this_element
, NULL
,
3736 _("Unable to recognise the format of file"));
3738 /* PR binutils/3110: Cope with archives
3739 containing multiple target types. */
3740 if (force_output_target
|| !ok_object
)
3741 output_element
= bfd_openw (output_name
, output_target
);
3743 output_element
= bfd_openw (output_name
, bfd_get_target (this_element
));
3745 if (output_element
== NULL
)
3747 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3748 bfd_close (this_element
);
3750 goto cleanup_and_exit
;
3755 status
= !copy_object (this_element
, output_element
, input_arch
);
3757 if (status
&& bfd_get_arch (this_element
) == bfd_arch_unknown
)
3758 /* Try again as an unknown object file. */
3763 status
= !copy_unknown_object (this_element
, output_element
);
3765 if (!(ok_object
&& !status
3766 ? bfd_close
: bfd_close_all_done
) (output_element
))
3768 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3769 /* Error in new object file. Don't change archive. */
3775 unlink (output_name
);
3778 bfd_close (this_element
);
3782 if (preserve_dates
&& stat_status
== 0)
3783 set_times (output_name
, &buf
);
3785 /* Open the newly created output file and attach to our list. */
3786 output_element
= bfd_openr (output_name
, output_target
);
3788 list
->obfd
= output_element
;
3790 *ptr
= output_element
;
3791 ptr
= &output_element
->archive_next
;
3793 bfd
*last_element
= this_element
;
3794 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
3795 bfd_close (last_element
);
3801 filename
= xstrdup (bfd_get_filename (obfd
));
3802 if (!(status
== 0 ? bfd_close
: bfd_close_all_done
) (obfd
))
3805 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3810 filename
= xstrdup (bfd_get_filename (ibfd
));
3811 if (!bfd_close (ibfd
))
3814 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3819 /* Delete all the files that we opened. */
3820 struct name_list
*l
, *next
;
3821 for (l
= list
; l
!= NULL
; l
= next
)
3823 if (l
->name
!= NULL
)
3825 if (l
->obfd
== NULL
)
3829 bfd_close (l
->obfd
);
3845 /* The top-level control. */
3848 copy_file (const char *input_filename
, const char *output_filename
, int ofd
,
3849 struct stat
*in_stat
, const char *input_target
,
3850 const char *output_target
, const bfd_arch_info_type
*input_arch
)
3853 char **obj_matching
;
3854 char **core_matching
;
3855 off_t size
= get_file_size (input_filename
);
3860 non_fatal (_("error: the input file '%s' is empty"),
3866 /* To allow us to do "strip *" without dying on the first
3867 non-object file, failures are nonfatal. */
3868 ibfd
= bfd_openr (input_filename
, input_target
);
3869 if (ibfd
== NULL
|| bfd_stat (ibfd
, in_stat
) != 0)
3871 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3878 switch (do_debug_sections
)
3880 case compress_gnu_zlib
:
3881 ibfd
->flags
|= BFD_COMPRESS
;
3885 /* The above two cases ought to just set BFD_COMPRESS for non-ELF
3886 but we can't tell whether a file is ELF or not until after
3887 bfd_check_format_matches. FIXME maybe: decide compression
3888 style in BFD after bfd_check_format_matches. */
3889 case compress_gabi_zlib
:
3890 ibfd
->flags
|= BFD_COMPRESS
| BFD_COMPRESS_GABI
;
3893 ibfd
->flags
|= BFD_COMPRESS
| BFD_COMPRESS_GABI
| BFD_COMPRESS_ZSTD
;
3895 fatal (_ ("--compress-debug-sections=zstd: binutils is not built with "
3900 ibfd
->flags
|= BFD_DECOMPRESS
;
3906 switch (do_elf_stt_common
)
3908 case elf_stt_common
:
3909 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
| BFD_USE_ELF_STT_COMMON
;
3912 case no_elf_stt_common
:
3913 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
;
3919 if (bfd_check_format (ibfd
, bfd_archive
))
3921 bool force_output_target
;
3924 /* bfd_get_target does not return the correct value until
3925 bfd_check_format succeeds. */
3926 if (output_target
== NULL
)
3928 output_target
= bfd_get_target (ibfd
);
3929 force_output_target
= false;
3932 force_output_target
= true;
3935 obfd
= bfd_fdopenw (output_filename
, output_target
, ofd
);
3937 obfd
= bfd_openw (output_filename
, output_target
);
3943 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3949 if (gnu_debuglink_filename
!= NULL
)
3951 non_fatal (_("--add-gnu-debuglink ignored for archive %s"),
3952 bfd_get_filename (ibfd
));
3953 gnu_debuglink_filename
= NULL
;
3956 copy_archive (ibfd
, obfd
, output_target
, force_output_target
, input_arch
);
3958 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
3963 /* bfd_get_target does not return the correct value until
3964 bfd_check_format succeeds. */
3965 if (output_target
== NULL
)
3966 output_target
= bfd_get_target (ibfd
);
3969 obfd
= bfd_fdopenw (output_filename
, output_target
, ofd
);
3971 obfd
= bfd_openw (output_filename
, output_target
);
3977 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3983 if (! copy_object (ibfd
, obfd
, input_arch
))
3986 /* PR 17512: file: 0f15796a.
3987 If the file could not be copied it may not be in a writeable
3988 state. So use bfd_close_all_done to avoid the possibility of
3989 writing uninitialised data into the file. */
3990 if (! (status
? bfd_close_all_done (obfd
) : bfd_close (obfd
)))
3993 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3996 if (!bfd_close (ibfd
))
3999 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
4004 bfd_error_type obj_error
= bfd_get_error ();
4005 bfd_error_type core_error
;
4007 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
4009 /* This probably can't happen.. */
4010 if (obj_error
== bfd_error_file_ambiguously_recognized
)
4011 free (obj_matching
);
4015 core_error
= bfd_get_error ();
4016 /* Report the object error in preference to the core error. */
4017 if (obj_error
!= core_error
)
4018 bfd_set_error (obj_error
);
4020 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
4022 if (obj_error
== bfd_error_file_ambiguously_recognized
)
4023 list_matching_formats (obj_matching
);
4024 if (core_error
== bfd_error_file_ambiguously_recognized
)
4025 list_matching_formats (core_matching
);
4032 /* Add a name to the section renaming list. */
4035 add_section_rename (const char * old_name
, const char * new_name
,
4038 section_rename
* srename
;
4040 /* Check for conflicts first. */
4041 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
4042 if (strcmp (srename
->old_name
, old_name
) == 0)
4044 /* Silently ignore duplicate definitions. */
4045 if (strcmp (srename
->new_name
, new_name
) == 0
4046 && srename
->flags
== flags
)
4049 fatal (_("Multiple renames of section %s"), old_name
);
4052 srename
= (section_rename
*) xmalloc (sizeof (* srename
));
4054 srename
->old_name
= old_name
;
4055 srename
->new_name
= new_name
;
4056 srename
->flags
= flags
;
4057 srename
->next
= section_rename_list
;
4059 section_rename_list
= srename
;
4062 /* Check the section rename list for a new name of the input section
4063 called OLD_NAME. Returns the new name if one is found and sets
4064 RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
4067 find_section_rename (const char *old_name
, flagword
*returned_flags
)
4069 const section_rename
*srename
;
4071 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
4072 if (strcmp (srename
->old_name
, old_name
) == 0)
4074 if (returned_flags
!= NULL
&& srename
->flags
!= (flagword
) -1)
4075 *returned_flags
= srename
->flags
;
4077 return srename
->new_name
;
4083 /* Once each of the sections is copied, we may still need to do some
4084 finalization work for private section headers. Do that here. */
4087 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
4089 /* Allow the BFD backend to copy any private data it understands
4090 from the input section to the output section. */
4091 if (! bfd_copy_private_header_data (ibfd
, obfd
))
4094 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
4095 _("error in private header data"));
4099 /* All went well. */
4103 static inline signed int
4104 power_of_two (bfd_vma val
)
4106 signed int result
= 0;
4111 while ((val
& 1) == 0)
4118 /* Number has more than one 1, i.e. wasn't a power of 2. */
4125 image_scn_align (unsigned int alignment
)
4129 case 8192: return IMAGE_SCN_ALIGN_8192BYTES
;
4130 case 4096: return IMAGE_SCN_ALIGN_4096BYTES
;
4131 case 2048: return IMAGE_SCN_ALIGN_2048BYTES
;
4132 case 1024: return IMAGE_SCN_ALIGN_1024BYTES
;
4133 case 512: return IMAGE_SCN_ALIGN_512BYTES
;
4134 case 256: return IMAGE_SCN_ALIGN_256BYTES
;
4135 case 128: return IMAGE_SCN_ALIGN_128BYTES
;
4136 case 64: return IMAGE_SCN_ALIGN_64BYTES
;
4137 case 32: return IMAGE_SCN_ALIGN_32BYTES
;
4138 case 16: return IMAGE_SCN_ALIGN_16BYTES
;
4139 case 8: return IMAGE_SCN_ALIGN_8BYTES
;
4140 case 4: return IMAGE_SCN_ALIGN_4BYTES
;
4141 case 2: return IMAGE_SCN_ALIGN_2BYTES
;
4142 case 1: return IMAGE_SCN_ALIGN_1BYTES
;
4147 /* Create a section in OBFD with the same
4148 name and attributes as ISECTION in IBFD. */
4151 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4153 bfd
*obfd
= (bfd
*) obfdarg
;
4154 struct section_list
*p
;
4160 const char *err
= NULL
;
4162 const char * new_name
;
4163 char *prefix
= NULL
;
4165 unsigned int alignment
;
4167 if (is_strip_section (ibfd
, isection
))
4170 /* Get the, possibly new, name of the output section. */
4171 name
= bfd_section_name (isection
);
4172 flags
= bfd_section_flags (isection
);
4173 if (bfd_get_flavour (ibfd
) != bfd_get_flavour (obfd
))
4175 flags
&= bfd_applicable_section_flags (ibfd
);
4176 flags
&= bfd_applicable_section_flags (obfd
);
4178 new_name
= find_section_rename (name
, &flags
);
4179 if (new_name
!= name
)
4182 flags
= check_new_section_flags (flags
, obfd
, name
);
4185 /* Prefix sections. */
4186 if (prefix_alloc_sections_string
4187 && (bfd_section_flags (isection
) & SEC_ALLOC
) != 0)
4188 prefix
= prefix_alloc_sections_string
;
4189 else if (prefix_sections_string
)
4190 prefix
= prefix_sections_string
;
4196 n
= (char *) xmalloc (strlen (prefix
) + strlen (name
) + 1);
4202 make_nobits
= false;
4204 p
= find_section_list (bfd_section_name (isection
), false,
4205 SECTION_CONTEXT_SET_FLAGS
);
4208 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
4209 flags
= check_new_section_flags (flags
, obfd
, bfd_section_name (isection
));
4215 /* For --extract-symbols where section sizes are zeroed, clear
4216 SEC_LOAD to indicate to coff_compute_section_file_positions that
4217 section sizes should not be adjusted for ALIGN_SECTIONS_IN_FILE.
4218 We don't want to clear SEC_HAS_CONTENTS as that will result
4219 in symbols being classified as 'B' by nm. */
4222 /* If only keeping debug sections then we'll be keeping section
4223 sizes in headers but making the sections have no contents. */
4224 else if (strip_symbols
== STRIP_NONDEBUG
4225 && (flags
& (SEC_ALLOC
| SEC_GROUP
)) != 0
4226 && !is_nondebug_keep_contents_section (ibfd
, isection
))
4227 clr
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
;
4229 if (clr
&& bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
4231 /* PR 29532: Copy group sections intact as otherwise we end up with
4232 empty groups. This prevents separate debug info files from
4233 being used with GDB, if they were based upon files that
4234 originally contained groups. */
4235 if (flags
& SEC_GROUP
)
4237 if ((clr
& SEC_HAS_CONTENTS
) != 0)
4240 /* Twiddle the input section flags so that it seems to
4241 elf.c:copy_private_bfd_data that section flags have not
4242 changed between input and output sections. This hack
4243 prevents wholesale rewriting of the program headers. */
4244 isection
->flags
&= ~clr
;
4249 if (!bfd_convert_section_setup (ibfd
, isection
, obfd
, &name
, &size
))
4252 err
= _("failed to create output section");
4256 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
4258 if (osection
== NULL
)
4260 err
= _("failed to create output section");
4265 size
= (size
+ interleave
- 1) / interleave
* copy_width
;
4266 else if (extract_symbol
)
4268 if (!bfd_set_section_size (osection
, size
))
4269 err
= _("failed to set size");
4271 bool vma_set_by_user
= false;
4273 vma
= bfd_section_vma (isection
);
4274 p
= find_section_list (bfd_section_name (isection
), false,
4275 SECTION_CONTEXT_ALTER_VMA
| SECTION_CONTEXT_SET_VMA
);
4278 if (p
->context
& SECTION_CONTEXT_SET_VMA
)
4282 vma_set_by_user
= true;
4285 vma
+= change_section_address
;
4287 if (!bfd_set_section_vma (osection
, vma
))
4288 err
= _("failed to set vma");
4290 bool lma_set_by_user
= false;
4292 lma
= isection
->lma
;
4293 p
= find_section_list (bfd_section_name (isection
), false,
4294 SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_SET_LMA
);
4297 if (p
->context
& SECTION_CONTEXT_ALTER_LMA
)
4301 lma_set_by_user
= true;
4304 lma
+= change_section_address
;
4306 osection
->lma
= lma
;
4308 p
= find_section_list (bfd_section_name (isection
), false,
4309 SECTION_CONTEXT_SET_ALIGNMENT
);
4311 alignment
= p
->alignment
;
4312 else if (pe_section_alignment
!= (bfd_vma
) -1
4313 && bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
4315 alignment
= power_of_two (pe_section_alignment
);
4317 if (coff_section_data (ibfd
, isection
))
4319 struct pei_section_tdata
* pei_data
= pei_section_data (ibfd
, isection
);
4321 if (pei_data
!= NULL
)
4323 /* Set the alignment flag of the input section, which will
4324 be copied to the output section later on. */
4325 pei_data
->pe_flags
&= ~IMAGE_SCN_ALIGN_POWER_BIT_MASK
;
4326 pei_data
->pe_flags
|= image_scn_align (pe_section_alignment
);
4331 alignment
= bfd_section_alignment (isection
);
4333 /* FIXME: This is probably not enough. If we change the LMA we
4334 may have to recompute the header for the file as well. */
4335 if (!bfd_set_section_alignment (osection
, alignment
))
4336 err
= _("failed to set alignment");
4338 /* If the output section's VMA is not aligned
4339 and the alignment has changed
4340 and the VMA was not set by the user
4341 and the section does not have relocations associated with it
4342 then warn the user. */
4343 if (osection
->vma
!= 0
4344 && (alignment
>= sizeof (bfd_vma
) * CHAR_BIT
4345 || (osection
->vma
& (((bfd_vma
) 1 << alignment
) - 1)) != 0)
4346 && alignment
!= bfd_section_alignment (isection
)
4347 && change_section_address
== 0
4348 && ! vma_set_by_user
4349 && bfd_get_reloc_upper_bound (ibfd
, isection
) < 1)
4351 non_fatal (_("output section %s's alignment does not match its VMA"), name
);
4354 /* Similar check for a non-aligned LMA.
4355 FIXME: Since this is only an LMA, maybe it does not matter if
4356 it is not aligned ? */
4357 if (osection
->lma
!= 0
4358 && (alignment
>= sizeof (bfd_vma
) * CHAR_BIT
4359 || (osection
->lma
& (((bfd_vma
) 1 << alignment
) - 1)) != 0)
4360 && alignment
!= bfd_section_alignment (isection
)
4361 && change_section_address
== 0
4362 && ! lma_set_by_user
4363 && bfd_get_reloc_upper_bound (ibfd
, isection
) < 1)
4365 non_fatal (_("output section %s's alignment does not match its LMA"), name
);
4368 /* Copy merge entity size. */
4369 osection
->entsize
= isection
->entsize
;
4371 /* Copy compress status. */
4372 osection
->compress_status
= isection
->compress_status
;
4374 /* This used to be mangle_section; we do here to avoid using
4375 bfd_get_section_by_name since some formats allow multiple
4376 sections with the same name. */
4377 isection
->output_section
= osection
;
4378 isection
->output_offset
= 0;
4380 if ((isection
->flags
& SEC_GROUP
) != 0)
4382 asymbol
*gsym
= group_signature (isection
);
4386 gsym
->flags
|= BSF_KEEP
;
4387 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
4388 elf_group_id (isection
) = gsym
;
4392 /* Allow the BFD backend to copy any private data it understands
4393 from the input section to the output section. */
4394 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
4395 err
= _("failed to copy private data");
4398 elf_section_type (osection
) = SHT_NOBITS
;
4405 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
4408 /* Return TRUE if input section ISECTION should be skipped. */
4411 skip_section (bfd
*ibfd
, sec_ptr isection
, bool skip_copy
)
4417 /* If we have already failed earlier on,
4418 do not keep on generating complaints now. */
4425 if (is_strip_section (ibfd
, isection
))
4428 if (is_update_section (ibfd
, isection
))
4431 /* When merging a note section we skip the copying of the contents,
4432 but not the copying of the relocs associated with the contents. */
4433 if (skip_copy
&& is_mergeable_note_section (ibfd
, isection
))
4436 flags
= bfd_section_flags (isection
);
4437 if ((flags
& SEC_GROUP
) != 0)
4440 osection
= isection
->output_section
;
4441 size
= bfd_section_size (isection
);
4443 if (size
== 0 || osection
== 0)
4449 /* Add section SECTION_PATTERN to the list of sections that will have their
4450 relocations removed. */
4453 handle_remove_relocations_option (const char *section_pattern
)
4455 find_section_list (section_pattern
, true, SECTION_CONTEXT_REMOVE_RELOCS
);
4458 /* Return TRUE if ISECTION from IBFD should have its relocations removed,
4459 otherwise return FALSE. If the user has requested that relocations be
4460 removed from a section that does not have relocations then this
4461 function will still return TRUE. */
4464 discard_relocations (bfd
*ibfd ATTRIBUTE_UNUSED
, asection
*isection
)
4466 return (find_section_list (bfd_section_name (isection
), false,
4467 SECTION_CONTEXT_REMOVE_RELOCS
) != NULL
);
4470 /* Wrapper for dealing with --remove-section (-R) command line arguments.
4471 A special case is detected here, if the user asks to remove a relocation
4472 section (one starting with ".rela" or ".rel") then this removal must
4473 be done using a different technique in a relocatable object. */
4476 handle_remove_section_option (const char *section_pattern
)
4478 find_section_list (section_pattern
, true, SECTION_CONTEXT_REMOVE
);
4479 if (startswith (section_pattern
, ".rel"))
4481 section_pattern
+= 4;
4482 if (*section_pattern
== 'a')
4484 if (*section_pattern
)
4485 handle_remove_relocations_option (section_pattern
);
4487 sections_removed
= true;
4490 /* Copy relocations in input section ISECTION of IBFD to an output
4491 section with the same name in OBFDARG. If stripping then don't
4492 copy any relocation info. */
4495 copy_relocations_in_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4497 bfd
*obfd
= (bfd
*) obfdarg
;
4503 if (skip_section (ibfd
, isection
, false))
4506 osection
= isection
->output_section
;
4508 /* Core files and DWO files do not need to be relocated. */
4509 if (bfd_get_format (obfd
) == bfd_core
4510 || strip_symbols
== STRIP_NONDWO
4511 || (strip_symbols
== STRIP_ALL
4512 && htab_elements (keep_specific_htab
) == 0)
4513 || discard_relocations (ibfd
, isection
))
4517 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4521 /* Do not complain if the target does not support relocations. */
4522 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4527 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4534 bfd_set_reloc (obfd
, osection
, NULL
, 0);
4537 if (isection
->orelocation
!= NULL
)
4539 /* Some other function has already set up the output relocs
4540 for us, so scan those instead of the default relocs. */
4541 relcount
= isection
->reloc_count
;
4542 relpp
= isection
->orelocation
;
4546 relpp
= bfd_xalloc (obfd
, relsize
);
4547 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
4551 bfd_nonfatal_message (NULL
, ibfd
, isection
,
4552 _("relocation count is negative"));
4557 if (strip_symbols
== STRIP_ALL
)
4559 /* Remove relocations which are not in
4560 keep_strip_specific_list. */
4564 for (w_relpp
= relpp
, i
= 0; i
< relcount
; i
++)
4565 /* PR 17512: file: 9e907e0c. */
4566 if (relpp
[i
]->sym_ptr_ptr
4568 && *relpp
[i
]->sym_ptr_ptr
4569 && is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
4570 keep_specific_htab
))
4571 *w_relpp
++ = relpp
[i
];
4572 relcount
= w_relpp
- relpp
;
4576 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
4580 /* Copy the data of input section ISECTION of IBFD
4581 to an output section with the same name in OBFD. */
4584 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4586 bfd
*obfd
= (bfd
*) obfdarg
;
4587 struct section_list
*p
;
4591 if (skip_section (ibfd
, isection
, true))
4594 osection
= isection
->output_section
;
4595 /* The output SHF_COMPRESSED section size is different from input if
4596 ELF classes of input and output aren't the same. We can't use
4597 the output section size since --interleave will shrink the output
4598 section. Size will be updated if the section is converted. */
4599 size
= bfd_section_size (isection
);
4601 if (bfd_section_flags (isection
) & SEC_HAS_CONTENTS
4602 && bfd_section_flags (osection
) & SEC_HAS_CONTENTS
)
4604 bfd_byte
*memhunk
= NULL
;
4606 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
)
4607 || !bfd_convert_section_contents (ibfd
, isection
, obfd
,
4610 bfd_set_section_size (osection
, 0);
4612 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4619 /* We don't handle leftover bytes (too many possible behaviors,
4620 and we don't know what the user wants). The section length
4621 must be a multiple of the number of bytes to swap. */
4622 if ((size
% reverse_bytes
) == 0)
4627 for (i
= 0; i
< size
; i
+= reverse_bytes
)
4628 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
4630 bfd_byte
*m
= (bfd_byte
*) memhunk
;
4633 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
4634 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
4638 /* User must pad the section up in order to do this. */
4639 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
4640 bfd_section_name (isection
), reverse_bytes
);
4645 /* Keep only every `copy_byte'th byte in MEMHUNK. */
4646 char *from
= (char *) memhunk
+ copy_byte
;
4647 char *to
= (char *) memhunk
;
4648 char *end
= (char *) memhunk
+ size
;
4651 /* If the section address is not exactly divisible by the interleave,
4652 then we must bias the from address. If the copy_byte is less than
4653 the bias, then we must skip forward one interleave, and increment
4655 int extra
= isection
->lma
% interleave
;
4657 if (copy_byte
< extra
)
4660 for (; from
< end
; from
+= interleave
)
4661 for (i
= 0; i
< copy_width
; i
++)
4663 if (&from
[i
] >= end
)
4668 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
* copy_width
;
4669 osection
->lma
/= interleave
;
4670 if (copy_byte
< extra
)
4674 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4677 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4683 else if ((p
= find_section_list (bfd_section_name (isection
),
4684 false, SECTION_CONTEXT_SET_FLAGS
)) != NULL
4685 && (p
->flags
& SEC_HAS_CONTENTS
) != 0)
4687 void *memhunk
= xmalloc (size
);
4689 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
4690 flag--they can just remove the section entirely and add it
4691 back again. However, we do permit them to turn on the
4692 SEC_HAS_CONTENTS flag, and take it to mean that the section
4693 contents should be zeroed out. */
4695 memset (memhunk
, 0, size
);
4696 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4699 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4707 /* Get all the sections. This is used when --gap-fill or --pad-to is
4711 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
4713 asection
***secppp
= (asection
***) secppparg
;
4715 **secppp
= osection
;
4719 /* Sort sections by LMA. This is called via qsort, and is used when
4720 --gap-fill or --pad-to is used. We force non loadable or empty
4721 sections to the front, where they are easier to ignore. */
4724 compare_section_lma (const void *arg1
, const void *arg2
)
4726 const asection
*sec1
= *(const asection
**) arg1
;
4727 const asection
*sec2
= *(const asection
**) arg2
;
4728 flagword flags1
, flags2
;
4730 /* Sort non loadable sections to the front. */
4731 flags1
= sec1
->flags
;
4732 flags2
= sec2
->flags
;
4733 if ((flags1
& SEC_HAS_CONTENTS
) == 0
4734 || (flags1
& SEC_LOAD
) == 0)
4736 if ((flags2
& SEC_HAS_CONTENTS
) != 0
4737 && (flags2
& SEC_LOAD
) != 0)
4742 if ((flags2
& SEC_HAS_CONTENTS
) == 0
4743 || (flags2
& SEC_LOAD
) == 0)
4747 /* Sort sections by LMA. */
4748 if (sec1
->lma
> sec2
->lma
)
4750 if (sec1
->lma
< sec2
->lma
)
4753 /* Sort sections with the same LMA by size. */
4754 if (bfd_section_size (sec1
) > bfd_section_size (sec2
))
4756 if (bfd_section_size (sec1
) < bfd_section_size (sec2
))
4759 if (sec1
->id
> sec2
->id
)
4761 if (sec1
->id
< sec2
->id
)
4766 /* Mark all the symbols which will be used in output relocations with
4767 the BSF_KEEP flag so that those symbols will not be stripped.
4769 Ignore relocations which will not appear in the output file. */
4772 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
4774 asymbol
**symbols
= (asymbol
**) symbolsarg
;
4779 /* Ignore an input section with no corresponding output section. */
4780 if (isection
->output_section
== NULL
)
4783 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4786 /* Do not complain if the target does not support relocations. */
4787 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4789 bfd_fatal (bfd_get_filename (ibfd
));
4795 relpp
= (arelent
**) xmalloc (relsize
);
4796 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
4798 bfd_fatal (bfd_get_filename (ibfd
));
4800 /* Examine each symbol used in a relocation. If it's not one of the
4801 special bfd section symbols, then mark it with BSF_KEEP. */
4802 for (i
= 0; i
< relcount
; i
++)
4804 /* See PRs 20923 and 20930 for reproducers for the NULL tests. */
4805 if (relpp
[i
]->sym_ptr_ptr
!= NULL
4806 && * relpp
[i
]->sym_ptr_ptr
!= NULL
4807 && *relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
4808 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
4809 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
4810 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
4816 /* Write out debugging information. */
4819 write_debugging_info (bfd
*obfd
, void *dhandle
,
4820 long *symcountp ATTRIBUTE_UNUSED
,
4821 asymbol
***symppp ATTRIBUTE_UNUSED
)
4823 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
4824 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
4826 bfd_byte
*syms
, *strings
= NULL
;
4827 bfd_size_type symsize
, stringsize
;
4828 asection
*stabsec
, *stabstrsec
;
4832 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
4837 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
4838 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
4839 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
4842 || stabstrsec
== NULL
4843 || !bfd_set_section_size (stabsec
, symsize
)
4844 || !bfd_set_section_size (stabstrsec
, stringsize
)
4845 || !bfd_set_section_alignment (stabsec
, 2)
4846 || !bfd_set_section_alignment (stabstrsec
, 0))
4848 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4849 _("can't create debugging section"));
4853 /* We can get away with setting the section contents now because
4854 the next thing the caller is going to do is copy over the
4855 real sections. We may someday have to split the contents
4856 setting out of this function. */
4858 && (!bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
4859 || !bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
4862 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4863 _("can't set debugging section contents"));
4872 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4873 _("don't know how to write debugging information for %s"),
4874 bfd_get_target (obfd
));
4878 /* If neither -D nor -U was specified explicitly,
4879 then use the configured default. */
4881 default_deterministic (void)
4883 if (deterministic
< 0)
4884 deterministic
= DEFAULT_AR_DETERMINISTIC
;
4888 strip_main (int argc
, char *argv
[])
4890 char *input_target
= NULL
;
4891 char *output_target
= NULL
;
4892 bool show_version
= false;
4893 bool formats_info
= false;
4896 char *output_file
= NULL
;
4897 bool merge_notes_set
= false;
4899 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU",
4900 strip_options
, (int *) 0)) != EOF
)
4905 input_target
= optarg
;
4908 output_target
= optarg
;
4911 input_target
= output_target
= optarg
;
4914 handle_remove_section_option (optarg
);
4916 case OPTION_KEEP_SECTION
:
4917 find_section_list (optarg
, true, SECTION_CONTEXT_KEEP
);
4919 case OPTION_REMOVE_RELOCS
:
4920 handle_remove_relocations_option (optarg
);
4922 case OPTION_STRIP_SECTION_HEADERS
:
4923 strip_section_headers
= true;
4926 strip_symbols
= STRIP_ALL
;
4930 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
4931 strip_symbols
= STRIP_DEBUG
;
4933 case OPTION_STRIP_DWO
:
4934 strip_symbols
= STRIP_DWO
;
4936 case OPTION_STRIP_UNNEEDED
:
4937 strip_symbols
= STRIP_UNNEEDED
;
4940 add_specific_symbol (optarg
, keep_specific_htab
);
4944 merge_notes_set
= true;
4946 case OPTION_NO_MERGE_NOTES
:
4947 merge_notes
= false;
4948 merge_notes_set
= true;
4951 add_specific_symbol (optarg
, strip_specific_htab
);
4954 output_file
= optarg
;
4957 preserve_dates
= true;
4960 deterministic
= true;
4963 deterministic
= false;
4966 discard_locals
= LOCALS_ALL
;
4969 discard_locals
= LOCALS_START_L
;
4975 show_version
= true;
4977 case OPTION_FORMATS_INFO
:
4978 formats_info
= true;
4980 case OPTION_ONLY_KEEP_DEBUG
:
4981 strip_symbols
= STRIP_NONDEBUG
;
4983 case OPTION_KEEP_FILE_SYMBOLS
:
4984 keep_file_symbols
= 1;
4986 case OPTION_KEEP_SECTION_SYMBOLS
:
4987 keep_section_symbols
= true;
4990 /* We've been given a long option. */
4997 strip_usage (stdout
, 0);
4999 strip_usage (stderr
, 1);
5003 /* If the user has not expressly chosen to merge/not-merge ELF notes
5004 then enable the merging unless we are stripping debug or dwo info. */
5005 if (! merge_notes_set
5006 && (strip_symbols
== STRIP_UNDEF
5007 || strip_symbols
== STRIP_ALL
5008 || strip_symbols
== STRIP_UNNEEDED
5009 || strip_symbols
== STRIP_NONDEBUG
5010 || strip_symbols
== STRIP_NONDWO
))
5020 print_version ("strip");
5022 default_deterministic ();
5024 /* Default is to strip all symbols. */
5025 if (strip_symbols
== STRIP_UNDEF
5026 && discard_locals
== LOCALS_UNDEF
5027 && htab_elements (strip_specific_htab
) == 0)
5028 strip_symbols
= STRIP_ALL
;
5030 if (output_target
== NULL
)
5031 output_target
= input_target
;
5035 || (output_file
!= NULL
&& (i
+ 1) < argc
))
5036 strip_usage (stderr
, 1);
5038 for (; i
< argc
; i
++)
5040 int hold_status
= status
;
5041 struct stat statbuf
;
5046 if (get_file_size (argv
[i
]) < 1)
5052 if (output_file
== NULL
5053 || filename_cmp (argv
[i
], output_file
) == 0)
5055 tmpname
= make_tempname (argv
[i
], &tmpfd
);
5057 copyfd
= dup (tmpfd
);
5060 tmpname
= output_file
;
5062 if (tmpname
== NULL
)
5064 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
5065 _("could not create temporary file to hold stripped copy"));
5071 copy_file (argv
[i
], tmpname
, tmpfd
, &statbuf
, input_target
,
5072 output_target
, NULL
);
5075 const char *oname
= output_file
? output_file
: argv
[i
];
5076 status
= smart_rename (tmpname
, oname
, copyfd
,
5077 &statbuf
, preserve_dates
) != 0;
5079 status
= hold_status
;
5085 unlink_if_ordinary (tmpname
);
5087 if (output_file
!= tmpname
)
5094 /* Set up PE subsystem. */
5097 set_pe_subsystem (const char *s
)
5099 const char *version
, *subsystem
;
5109 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
5110 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
5111 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
5112 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
5113 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
5114 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
5115 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
5116 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
5117 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
},
5118 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
5124 /* Check for the presence of a version number. */
5125 version
= strchr (s
, ':');
5126 if (version
== NULL
)
5130 int len
= version
- s
;
5134 version
= copy
+ 1 + len
;
5135 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
5137 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
5139 non_fatal (_("%s: bad version in PE subsystem"), s
);
5142 /* Check for numeric subsystem. */
5143 value
= (short) strtol (subsystem
, ©
, 0);
5146 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
5147 if (v
[i
].value
== value
)
5149 pe_subsystem
= value
;
5150 set_def
= v
[i
].set_def
;
5156 /* Search for subsystem by name. */
5157 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
5158 if (strcmp (subsystem
, v
[i
].name
) == 0)
5160 pe_subsystem
= v
[i
].value
;
5161 set_def
= v
[i
].set_def
;
5169 fatal (_("unknown PE subsystem: %s"), s
);
5174 if (pe_file_alignment
== (bfd_vma
) -1)
5175 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
5176 if (pe_section_alignment
== (bfd_vma
) -1)
5177 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
5181 free ((char *) subsystem
);
5184 /* Convert EFI target to PEI target. */
5187 convert_efi_target (char **targ
)
5191 char *efi
= *targ
+ 4;
5194 if (startswith (efi
, "app-"))
5195 subsys
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
5196 else if (startswith (efi
, "bsdrv-"))
5198 subsys
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
5201 else if (startswith (efi
, "rtdrv-"))
5203 subsys
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
5210 pei
= xmalloc (len
+ sizeof ("-little"));
5211 memcpy (pei
, efi
, len
+ 1);
5216 if (strcmp (efi
+ 4, "ia32") == 0)
5218 /* Change ia32 to i386. */
5223 else if (strcmp (efi
+ 4, "x86_64") == 0)
5225 /* Change x86_64 to x86-64. */
5228 else if (strcmp (efi
+ 4, "aarch64") == 0)
5230 /* Change aarch64 to aarch64-little. */
5231 memcpy (pei
+ 4 + sizeof ("aarch64") - 1, "-little", sizeof ("-little"));
5233 else if (strcmp (efi
+ 4, "riscv64") == 0)
5235 /* Change riscv64 to riscv64-little. */
5236 memcpy (pei
+ 4 + sizeof ("riscv64") - 1, "-little", sizeof ("-little"));
5242 /* Allocate and return a pointer to a struct section_add, initializing the
5243 structure using ARG, a string in the format "sectionname=filename".
5244 The returned structure will have its next pointer set to NEXT. The
5245 OPTION field is the name of the command line option currently being
5246 parsed, and is only used if an error needs to be reported. */
5248 static struct section_add
*
5249 init_section_add (const char *arg
,
5250 struct section_add
*next
,
5253 struct section_add
*pa
;
5256 s
= strchr (arg
, '=');
5258 fatal (_("bad format for %s"), option
);
5260 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
5261 pa
->name
= xstrndup (arg
, s
- arg
);
5262 pa
->filename
= s
+ 1;
5264 pa
->contents
= NULL
;
5270 /* Load the file specified in PA, allocating memory to hold the file
5271 contents, and store a pointer to the allocated memory in the contents
5272 field of PA. The size field of PA is also updated. All errors call
5276 section_add_load_file (struct section_add
*pa
)
5281 /* We don't use get_file_size so that we can do
5282 --add-section .note.GNU_stack=/dev/null
5283 get_file_size doesn't work on /dev/null. */
5285 f
= fopen (pa
->filename
, FOPEN_RB
);
5287 fatal (_("cannot open: %s: %s"),
5288 pa
->filename
, strerror (errno
));
5292 pa
->contents
= (bfd_byte
*) xmalloc (alloc
);
5300 pa
->contents
= (bfd_byte
*) xrealloc (pa
->contents
, alloc
);
5303 got
= fread (pa
->contents
+ off
, 1, alloc
- off
, f
);
5305 fatal (_("%s: fread failed"), pa
->filename
);
5316 copy_main (int argc
, char *argv
[])
5318 char *input_filename
= NULL
;
5319 char *output_filename
= NULL
;
5321 char *input_target
= NULL
;
5322 char *output_target
= NULL
;
5323 bool show_version
= false;
5324 bool change_warn
= true;
5325 bool formats_info
= false;
5326 bool use_globalize
= false;
5327 bool use_keep_global
= false;
5331 struct stat statbuf
;
5332 const bfd_arch_info_type
*input_arch
= NULL
;
5334 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
5335 copy_options
, (int *) 0)) != EOF
)
5340 copy_byte
= atoi (optarg
);
5342 fatal (_("byte number must be non-negative"));
5346 input_arch
= bfd_scan_arch (optarg
);
5347 if (input_arch
== NULL
)
5348 fatal (_("architecture %s unknown"), optarg
);
5354 interleave
= atoi (optarg
);
5356 fatal (_("interleave must be positive"));
5362 case OPTION_INTERLEAVE_WIDTH
:
5363 copy_width
= atoi (optarg
);
5365 fatal(_("interleave width must be positive"));
5369 case 's': /* "source" - 'I' is preferred */
5370 input_target
= optarg
;
5374 case 'd': /* "destination" - 'O' is preferred */
5375 output_target
= optarg
;
5379 input_target
= output_target
= optarg
;
5383 find_section_list (optarg
, true, SECTION_CONTEXT_COPY
);
5384 sections_copied
= true;
5388 handle_remove_section_option (optarg
);
5391 case OPTION_KEEP_SECTION
:
5392 find_section_list (optarg
, true, SECTION_CONTEXT_KEEP
);
5395 case OPTION_REMOVE_RELOCS
:
5396 handle_remove_relocations_option (optarg
);
5399 case OPTION_STRIP_SECTION_HEADERS
:
5400 strip_section_headers
= true;
5404 strip_symbols
= STRIP_ALL
;
5408 strip_symbols
= STRIP_DEBUG
;
5411 case OPTION_STRIP_DWO
:
5412 strip_symbols
= STRIP_DWO
;
5415 case OPTION_STRIP_UNNEEDED
:
5416 strip_symbols
= STRIP_UNNEEDED
;
5419 case OPTION_ONLY_KEEP_DEBUG
:
5420 strip_symbols
= STRIP_NONDEBUG
;
5423 case OPTION_KEEP_FILE_SYMBOLS
:
5424 keep_file_symbols
= 1;
5427 case OPTION_ADD_GNU_DEBUGLINK
:
5428 long_section_names
= ENABLE
;
5429 gnu_debuglink_filename
= optarg
;
5433 add_specific_symbol (optarg
, keep_specific_htab
);
5439 case OPTION_NO_MERGE_NOTES
:
5440 merge_notes
= false;
5444 add_specific_symbol (optarg
, strip_specific_htab
);
5447 case OPTION_STRIP_UNNEEDED_SYMBOL
:
5448 add_specific_symbol (optarg
, strip_unneeded_htab
);
5452 add_specific_symbol (optarg
, localize_specific_htab
);
5455 case OPTION_GLOBALIZE_SYMBOL
:
5456 use_globalize
= true;
5457 add_specific_symbol (optarg
, globalize_specific_htab
);
5461 use_keep_global
= true;
5462 add_specific_symbol (optarg
, keepglobal_specific_htab
);
5466 add_specific_symbol (optarg
, weaken_specific_htab
);
5470 preserve_dates
= true;
5474 deterministic
= true;
5478 deterministic
= false;
5486 discard_locals
= LOCALS_ALL
;
5490 discard_locals
= LOCALS_START_L
;
5498 show_version
= true;
5501 case OPTION_FORMATS_INFO
:
5502 formats_info
= true;
5509 case OPTION_ADD_SECTION
:
5510 add_sections
= init_section_add (optarg
, add_sections
,
5512 section_add_load_file (add_sections
);
5515 case OPTION_UPDATE_SECTION
:
5516 update_sections
= init_section_add (optarg
, update_sections
,
5517 "--update-section");
5518 section_add_load_file (update_sections
);
5521 case OPTION_DUMP_SECTION
:
5522 dump_sections
= init_section_add (optarg
, dump_sections
,
5526 case OPTION_ADD_SYMBOL
:
5529 struct addsym_node
*newsym
= xmalloc (sizeof *newsym
);
5531 newsym
->next
= NULL
;
5532 s
= strchr (optarg
, '=');
5534 fatal (_("bad format for %s"), "--add-symbol");
5535 t
= strchr (s
+ 1, ':');
5537 newsym
->symdef
= xstrndup (optarg
, s
- optarg
);
5540 newsym
->section
= xstrndup (s
+ 1, t
- (s
+ 1));
5541 newsym
->symval
= strtol (t
+ 1, NULL
, 0);
5545 newsym
->section
= NULL
;
5546 newsym
->symval
= strtol (s
+ 1, NULL
, 0);
5550 t
= strchr (t
+ 1, ',');
5551 newsym
->othersym
= NULL
;
5553 newsym
->flags
= parse_symflags (t
+1, &newsym
->othersym
);
5555 newsym
->flags
= BSF_GLOBAL
;
5557 /* Keep 'othersym' symbols at the front of the list. */
5558 if (newsym
->othersym
)
5560 newsym
->next
= add_sym_list
;
5562 add_sym_tail
= &newsym
->next
;
5563 add_sym_list
= newsym
;
5567 *add_sym_tail
= newsym
;
5568 add_sym_tail
= &newsym
->next
;
5574 case OPTION_CHANGE_START
:
5575 change_start
= parse_vma (optarg
, "--change-start");
5578 case OPTION_CHANGE_SECTION_ADDRESS
:
5579 case OPTION_CHANGE_SECTION_LMA
:
5580 case OPTION_CHANGE_SECTION_VMA
:
5582 struct section_list
* p
;
5583 unsigned int context
= 0;
5587 char *option
= NULL
;
5592 case OPTION_CHANGE_SECTION_ADDRESS
:
5593 option
= "--change-section-address";
5594 context
= SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_ALTER_VMA
;
5596 case OPTION_CHANGE_SECTION_LMA
:
5597 option
= "--change-section-lma";
5598 context
= SECTION_CONTEXT_ALTER_LMA
;
5600 case OPTION_CHANGE_SECTION_VMA
:
5601 option
= "--change-section-vma";
5602 context
= SECTION_CONTEXT_ALTER_VMA
;
5606 s
= strchr (optarg
, '=');
5609 s
= strchr (optarg
, '+');
5612 s
= strchr (optarg
, '-');
5614 fatal (_("bad format for %s"), option
);
5619 /* Correct the context. */
5622 case OPTION_CHANGE_SECTION_ADDRESS
:
5623 context
= SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_SET_VMA
;
5625 case OPTION_CHANGE_SECTION_LMA
:
5626 context
= SECTION_CONTEXT_SET_LMA
;
5628 case OPTION_CHANGE_SECTION_VMA
:
5629 context
= SECTION_CONTEXT_SET_VMA
;
5635 name
= (char *) xmalloc (len
+ 1);
5636 strncpy (name
, optarg
, len
);
5639 p
= find_section_list (name
, true, context
);
5641 val
= parse_vma (s
+ 1, option
);
5647 case OPTION_CHANGE_SECTION_ADDRESS
:
5651 case OPTION_CHANGE_SECTION_LMA
:
5655 case OPTION_CHANGE_SECTION_VMA
:
5662 case OPTION_CHANGE_ADDRESSES
:
5663 change_section_address
= parse_vma (optarg
, "--change-addresses");
5664 change_start
= change_section_address
;
5667 case OPTION_CHANGE_WARNINGS
:
5671 case OPTION_CHANGE_LEADING_CHAR
:
5672 change_leading_char
= true;
5675 case OPTION_COMPRESS_DEBUG_SECTIONS
:
5678 if (strcasecmp (optarg
, "none") == 0)
5679 do_debug_sections
= decompress
;
5680 else if (strcasecmp (optarg
, "zlib") == 0)
5681 do_debug_sections
= compress_zlib
;
5682 else if (strcasecmp (optarg
, "zlib-gnu") == 0)
5683 do_debug_sections
= compress_gnu_zlib
;
5684 else if (strcasecmp (optarg
, "zlib-gabi") == 0)
5685 do_debug_sections
= compress_gabi_zlib
;
5686 else if (strcasecmp (optarg
, "zstd") == 0)
5687 do_debug_sections
= compress_zstd
;
5689 fatal (_("unrecognized --compress-debug-sections type `%s'"),
5693 do_debug_sections
= compress
;
5696 case OPTION_DEBUGGING
:
5697 convert_debugging
= true;
5700 case OPTION_DECOMPRESS_DEBUG_SECTIONS
:
5701 do_debug_sections
= decompress
;
5704 case OPTION_ELF_STT_COMMON
:
5705 if (strcasecmp (optarg
, "yes") == 0)
5706 do_elf_stt_common
= elf_stt_common
;
5707 else if (strcasecmp (optarg
, "no") == 0)
5708 do_elf_stt_common
= no_elf_stt_common
;
5710 fatal (_("unrecognized --elf-stt-common= option `%s'"),
5714 case OPTION_GAP_FILL
:
5716 bfd_vma gap_fill_vma
;
5718 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
5719 gap_fill
= (bfd_byte
) gap_fill_vma
;
5720 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
5721 non_fatal (_("Warning: truncating gap-fill from 0x%" PRIx64
5723 (uint64_t) gap_fill_vma
, gap_fill
);
5724 gap_fill_set
= true;
5728 case OPTION_NO_CHANGE_WARNINGS
:
5729 change_warn
= false;
5733 pad_to
= parse_vma (optarg
, "--pad-to");
5737 case OPTION_REMOVE_LEADING_CHAR
:
5738 remove_leading_char
= true;
5741 case OPTION_REDEFINE_SYM
:
5743 /* Insert this redefinition onto redefine_specific_htab. */
5747 const char *nextarg
;
5748 char *source
, *target
;
5750 s
= strchr (optarg
, '=');
5752 fatal (_("bad format for %s"), "--redefine-sym");
5755 source
= (char *) xmalloc (len
+ 1);
5756 strncpy (source
, optarg
, len
);
5760 len
= strlen (nextarg
);
5761 target
= (char *) xmalloc (len
+ 1);
5762 strcpy (target
, nextarg
);
5764 add_redefine_and_check ("--redefine-sym", source
, target
);
5771 case OPTION_REDEFINE_SYMS
:
5772 add_redefine_syms_file (optarg
);
5775 case OPTION_SET_SECTION_FLAGS
:
5777 struct section_list
*p
;
5782 s
= strchr (optarg
, '=');
5784 fatal (_("bad format for %s"), "--set-section-flags");
5787 name
= (char *) xmalloc (len
+ 1);
5788 strncpy (name
, optarg
, len
);
5791 p
= find_section_list (name
, true, SECTION_CONTEXT_SET_FLAGS
);
5793 p
->flags
= parse_flags (s
+ 1);
5797 case OPTION_SET_SECTION_ALIGNMENT
:
5799 struct section_list
*p
;
5805 s
= strchr (optarg
, '=');
5807 fatal (_("bad format for --set-section-alignment: argument needed"));
5809 align
= atoi (s
+ 1);
5811 fatal (_("bad format for --set-section-alignment: numeric argument needed"));
5813 /* Convert integer alignment into a power-of-two alignment. */
5814 palign
= power_of_two (align
);
5816 fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
5818 /* Add the alignment setting to the section list. */
5820 name
= (char *) xmalloc (len
+ 1);
5821 strncpy (name
, optarg
, len
);
5824 p
= find_section_list (name
, true, SECTION_CONTEXT_SET_ALIGNMENT
);
5826 p
->alignment
= palign
;
5830 case OPTION_RENAME_SECTION
:
5833 const char *eq
, *fl
;
5838 eq
= strchr (optarg
, '=');
5840 fatal (_("bad format for %s"), "--rename-section");
5844 fatal (_("bad format for %s"), "--rename-section");
5846 old_name
= (char *) xmalloc (len
+ 1);
5847 strncpy (old_name
, optarg
, len
);
5851 fl
= strchr (eq
, ',');
5854 flags
= parse_flags (fl
+ 1);
5864 fatal (_("bad format for %s"), "--rename-section");
5866 new_name
= (char *) xmalloc (len
+ 1);
5867 strncpy (new_name
, eq
, len
);
5870 add_section_rename (old_name
, new_name
, flags
);
5874 case OPTION_SET_START
:
5875 set_start
= parse_vma (optarg
, "--set-start");
5876 set_start_set
= true;
5879 case OPTION_SREC_LEN
:
5880 _bfd_srec_len
= parse_vma (optarg
, "--srec-len");
5883 case OPTION_SREC_FORCES3
:
5884 _bfd_srec_forceS3
= true;
5887 case OPTION_STRIP_SYMBOLS
:
5888 add_specific_symbols (optarg
, strip_specific_htab
,
5889 &strip_specific_buffer
);
5892 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
5893 add_specific_symbols (optarg
, strip_unneeded_htab
,
5894 &strip_unneeded_buffer
);
5897 case OPTION_KEEP_SYMBOLS
:
5898 add_specific_symbols (optarg
, keep_specific_htab
,
5899 &keep_specific_buffer
);
5902 case OPTION_KEEP_SECTION_SYMBOLS
:
5903 keep_section_symbols
= true;
5906 case OPTION_LOCALIZE_HIDDEN
:
5907 localize_hidden
= true;
5910 case OPTION_LOCALIZE_SYMBOLS
:
5911 add_specific_symbols (optarg
, localize_specific_htab
,
5912 &localize_specific_buffer
);
5915 case OPTION_LONG_SECTION_NAMES
:
5916 if (!strcmp ("enable", optarg
))
5917 long_section_names
= ENABLE
;
5918 else if (!strcmp ("disable", optarg
))
5919 long_section_names
= DISABLE
;
5920 else if (!strcmp ("keep", optarg
))
5921 long_section_names
= KEEP
;
5923 fatal (_("unknown long section names option '%s'"), optarg
);
5926 case OPTION_GLOBALIZE_SYMBOLS
:
5927 use_globalize
= true;
5928 add_specific_symbols (optarg
, globalize_specific_htab
,
5929 &globalize_specific_buffer
);
5932 case OPTION_KEEPGLOBAL_SYMBOLS
:
5933 use_keep_global
= true;
5934 add_specific_symbols (optarg
, keepglobal_specific_htab
,
5935 &keepglobal_specific_buffer
);
5938 case OPTION_WEAKEN_SYMBOLS
:
5939 add_specific_symbols (optarg
, weaken_specific_htab
,
5940 &weaken_specific_buffer
);
5943 case OPTION_ALT_MACH_CODE
:
5944 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
5945 if (use_alt_mach_code
== 0)
5946 fatal (_("unable to parse alternative machine code"));
5949 case OPTION_PREFIX_SYMBOLS
:
5950 prefix_symbols_string
= optarg
;
5953 case OPTION_PREFIX_SECTIONS
:
5954 prefix_sections_string
= optarg
;
5957 case OPTION_PREFIX_ALLOC_SECTIONS
:
5958 prefix_alloc_sections_string
= optarg
;
5961 case OPTION_READONLY_TEXT
:
5962 bfd_flags_to_set
|= WP_TEXT
;
5963 bfd_flags_to_clear
&= ~WP_TEXT
;
5966 case OPTION_WRITABLE_TEXT
:
5967 bfd_flags_to_clear
|= WP_TEXT
;
5968 bfd_flags_to_set
&= ~WP_TEXT
;
5972 bfd_flags_to_set
|= D_PAGED
;
5973 bfd_flags_to_clear
&= ~D_PAGED
;
5977 bfd_flags_to_clear
|= D_PAGED
;
5978 bfd_flags_to_set
&= ~D_PAGED
;
5981 case OPTION_EXTRACT_DWO
:
5982 strip_symbols
= STRIP_NONDWO
;
5985 case OPTION_EXTRACT_SYMBOL
:
5986 extract_symbol
= true;
5989 case OPTION_REVERSE_BYTES
:
5991 int prev
= reverse_bytes
;
5993 reverse_bytes
= atoi (optarg
);
5994 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
5995 fatal (_("number of bytes to reverse must be positive and even"));
5997 if (prev
&& prev
!= reverse_bytes
)
5998 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
6003 case OPTION_FILE_ALIGNMENT
:
6004 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
6010 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
6012 || (*end
!= ',' && *end
!= '\0'))
6013 non_fatal (_("%s: invalid reserve value for --heap"),
6015 else if (*end
!= '\0')
6017 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
6019 non_fatal (_("%s: invalid commit value for --heap"),
6025 case OPTION_IMAGE_BASE
:
6026 pe_image_base
= parse_vma (optarg
, "--image-base");
6029 case OPTION_PE_SECTION_ALIGNMENT
:
6030 pe_section_alignment
= parse_vma (optarg
,
6031 "--section-alignment");
6032 if (power_of_two (pe_section_alignment
) == -1)
6034 non_fatal (_("--section-alignment argument is not a power of two: %s - ignoring"), optarg
);
6035 pe_section_alignment
= (bfd_vma
) -1;
6039 case OPTION_SUBSYSTEM
:
6040 set_pe_subsystem (optarg
);
6046 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
6048 || (*end
!= ',' && *end
!= '\0'))
6049 non_fatal (_("%s: invalid reserve value for --stack"),
6051 else if (*end
!= '\0')
6053 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
6055 non_fatal (_("%s: invalid commit value for --stack"),
6061 case OPTION_VERILOG_DATA_WIDTH
:
6062 VerilogDataWidth
= parse_vma (optarg
, "--verilog-data-width");
6063 switch (VerilogDataWidth
)
6069 case 16: /* We do not support widths > 16 because the verilog
6070 data is handled internally in 16 byte wide packets. */
6073 fatal (_("error: verilog data width must be 1, 2, 4, 8 or 16"));
6078 /* We've been given a long option. */
6083 copy_usage (stdout
, 0);
6086 copy_usage (stderr
, 1);
6090 if (use_globalize
&& use_keep_global
)
6091 fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)"));
6100 print_version ("objcopy");
6102 if (interleave
&& copy_byte
== -1)
6103 fatal (_("interleave start byte must be set with --byte"));
6105 if (copy_byte
>= interleave
)
6106 fatal (_("byte number must be less than interleave"));
6108 if (copy_width
> interleave
- copy_byte
)
6109 fatal (_("interleave width must be less than or equal to interleave - byte`"));
6111 if (optind
== argc
|| optind
+ 2 < argc
)
6112 copy_usage (stderr
, 1);
6114 input_filename
= argv
[optind
];
6115 if (optind
+ 1 < argc
)
6116 output_filename
= argv
[optind
+ 1];
6118 default_deterministic ();
6120 /* Default is to strip no symbols. */
6121 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
6122 strip_symbols
= STRIP_NONE
;
6124 if (output_target
== NULL
)
6125 output_target
= input_target
;
6127 /* Convert input EFI target to PEI target. */
6128 if (input_target
!= NULL
6129 && startswith (input_target
, "efi-"))
6131 if (convert_efi_target (&input_target
) < 0)
6132 fatal (_("unknown input EFI target: %s"), input_target
);
6135 /* Convert output EFI target to PEI target. */
6136 if (output_target
!= NULL
6137 && startswith (output_target
, "efi-"))
6139 int subsys
= convert_efi_target (&output_target
);
6142 fatal (_("unknown output EFI target: %s"), output_target
);
6143 if (pe_subsystem
== -1)
6144 pe_subsystem
= subsys
;
6145 if (pe_file_alignment
== (bfd_vma
) -1)
6146 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
6147 if (pe_section_alignment
== (bfd_vma
) -1)
6148 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
6151 /* If there is no destination file, or the source and destination files
6152 are the same, then create a temp and copy the result into the input. */
6154 if (output_filename
== NULL
6155 || filename_cmp (input_filename
, output_filename
) == 0)
6157 tmpname
= make_tempname (input_filename
, &tmpfd
);
6159 copyfd
= dup (tmpfd
);
6162 tmpname
= output_filename
;
6164 if (tmpname
== NULL
)
6166 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
6167 input_filename
, strerror (errno
));
6170 copy_file (input_filename
, tmpname
, tmpfd
, &statbuf
, input_target
,
6171 output_target
, input_arch
);
6174 const char *oname
= output_filename
? output_filename
: input_filename
;
6175 status
= smart_rename (tmpname
, oname
, copyfd
,
6176 &statbuf
, preserve_dates
) != 0;
6182 unlink_if_ordinary (tmpname
);
6185 if (tmpname
!= output_filename
)
6190 struct section_list
*p
;
6192 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
6196 if (p
->context
& (SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
))
6197 /* xgettext:c-format */
6198 non_fatal (_("%s %s%c0x%" PRIx64
" never used"),
6199 "--change-section-vma",
6201 p
->context
& SECTION_CONTEXT_SET_VMA
? '=' : '+',
6202 (uint64_t) p
->vma_val
);
6204 if (p
->context
& (SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
))
6205 /* xgettext:c-format */
6206 non_fatal (_("%s %s%c0x%" PRIx64
" never used"),
6207 "--change-section-lma",
6209 p
->context
& SECTION_CONTEXT_SET_LMA
? '=' : '+',
6210 (uint64_t) p
->lma_val
);
6215 free (strip_specific_buffer
);
6216 free (strip_unneeded_buffer
);
6217 free (keep_specific_buffer
);
6218 free (localize_specific_buffer
);
6219 free (globalize_specific_buffer
);
6220 free (keepglobal_specific_buffer
);
6221 free (weaken_specific_buffer
);
6227 main (int argc
, char *argv
[])
6229 #ifdef HAVE_LC_MESSAGES
6230 setlocale (LC_MESSAGES
, "");
6232 setlocale (LC_CTYPE
, "");
6233 bindtextdomain (PACKAGE
, LOCALEDIR
);
6234 textdomain (PACKAGE
);
6236 program_name
= argv
[0];
6237 xmalloc_set_program_name (program_name
);
6239 expandargv (&argc
, &argv
);
6241 strip_symbols
= STRIP_UNDEF
;
6242 discard_locals
= LOCALS_UNDEF
;
6244 if (bfd_init () != BFD_INIT_MAGIC
)
6245 fatal (_("fatal error: libbfd ABI mismatch"));
6246 set_default_bfd_target ();
6250 int i
= strlen (program_name
);
6251 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
6252 /* Drop the .exe suffix, if any. */
6253 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
6256 program_name
[i
] = '\0';
6259 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
6262 create_symbol_htabs ();
6263 xatexit (delete_symbol_htabs
);
6266 bfd_set_error_program_name (argv
[0]);
6269 strip_main (argc
, argv
);
6271 copy_main (argc
, argv
);