1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2022 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "libiberty.h"
28 #include "filenames.h"
31 #include "coff/internal.h"
33 #include "safe-ctype.h"
35 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
36 header in generic PE code. */
37 #include "coff/i386.h"
40 static bfd_vma pe_file_alignment
= (bfd_vma
) -1;
41 static bfd_vma pe_heap_commit
= (bfd_vma
) -1;
42 static bfd_vma pe_heap_reserve
= (bfd_vma
) -1;
43 static bfd_vma pe_image_base
= (bfd_vma
) -1;
44 static bfd_vma pe_section_alignment
= (bfd_vma
) -1;
45 static bfd_vma pe_stack_commit
= (bfd_vma
) -1;
46 static bfd_vma pe_stack_reserve
= (bfd_vma
) -1;
47 static short pe_subsystem
= -1;
48 static short pe_major_subsystem_version
= -1;
49 static short pe_minor_subsystem_version
= -1;
51 struct is_specified_symbol_predicate_data
57 /* A node includes symbol name mapping to support redefine_sym. */
66 struct addsym_node
*next
;
71 const char * othersym
;
74 typedef struct section_rename
76 const char * old_name
;
77 const char * new_name
;
79 struct section_rename
* next
;
83 /* List of sections to be renamed. */
84 static section_rename
*section_rename_list
;
86 static asymbol
**isympp
= NULL
; /* Input symbols. */
87 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
89 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
90 static int copy_byte
= -1;
91 static int interleave
= 0; /* Initialised to 4 in copy_main(). */
92 static int copy_width
= 1;
94 static bool keep_section_symbols
= false ;/* True if section symbols should be retained. */
95 static bool verbose
; /* Print file and target names. */
96 static bool preserve_dates
; /* Preserve input file timestamp. */
97 static int deterministic
= -1; /* Enable deterministic archives. */
98 static int status
= 0; /* Exit status. */
100 static bool merge_notes
= false; /* Merge note sections. */
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_SYMBOLS
,
370 OPTION_STRIP_UNNEEDED
,
371 OPTION_STRIP_UNNEEDED_SYMBOL
,
372 OPTION_STRIP_UNNEEDED_SYMBOLS
,
374 OPTION_UPDATE_SECTION
,
375 OPTION_VERILOG_DATA_WIDTH
,
377 OPTION_WEAKEN_SYMBOLS
,
381 /* Options to handle if running as "strip". */
383 static struct option strip_options
[] =
385 {"disable-deterministic-archives", no_argument
, 0, 'U'},
386 {"discard-all", no_argument
, 0, 'x'},
387 {"discard-locals", no_argument
, 0, 'X'},
388 {"enable-deterministic-archives", no_argument
, 0, 'D'},
389 {"format", required_argument
, 0, 'F'}, /* Obsolete */
390 {"help", no_argument
, 0, 'h'},
391 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
392 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
393 {"input-target", required_argument
, 0, 'I'},
394 {"keep-section-symbols", no_argument
, 0, OPTION_KEEP_SECTION_SYMBOLS
},
395 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
396 {"keep-section", required_argument
, 0, OPTION_KEEP_SECTION
},
397 {"keep-symbol", required_argument
, 0, 'K'},
398 {"merge-notes", no_argument
, 0, 'M'},
399 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
400 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
401 {"output-file", required_argument
, 0, 'o'},
402 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
403 {"output-target", required_argument
, 0, 'O'},
404 {"preserve-dates", no_argument
, 0, 'p'},
405 {"remove-section", required_argument
, 0, 'R'},
406 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
407 {"strip-all", no_argument
, 0, 's'},
408 {"strip-debug", no_argument
, 0, 'S'},
409 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
410 {"strip-symbol", required_argument
, 0, 'N'},
411 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
412 {"target", required_argument
, 0, 'F'},
413 {"verbose", no_argument
, 0, 'v'},
414 {"version", no_argument
, 0, 'V'},
415 {"wildcard", no_argument
, 0, 'w'},
416 {0, no_argument
, 0, 0}
419 /* Options to handle if running as "objcopy". */
421 static struct option copy_options
[] =
423 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
424 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
425 {"add-symbol", required_argument
, 0, OPTION_ADD_SYMBOL
},
426 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
427 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
428 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
429 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
430 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
431 {"binary-architecture", required_argument
, 0, 'B'},
432 {"byte", required_argument
, 0, 'b'},
433 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
434 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
435 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
436 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
437 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
438 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
439 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
440 {"compress-debug-sections", optional_argument
, 0, OPTION_COMPRESS_DEBUG_SECTIONS
},
441 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
442 {"decompress-debug-sections", no_argument
, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS
},
443 {"disable-deterministic-archives", no_argument
, 0, 'U'},
444 {"discard-all", no_argument
, 0, 'x'},
445 {"discard-locals", no_argument
, 0, 'X'},
446 {"dump-section", required_argument
, 0, OPTION_DUMP_SECTION
},
447 {"elf-stt-common", required_argument
, 0, OPTION_ELF_STT_COMMON
},
448 {"enable-deterministic-archives", no_argument
, 0, 'D'},
449 {"extract-dwo", no_argument
, 0, OPTION_EXTRACT_DWO
},
450 {"extract-symbol", no_argument
, 0, OPTION_EXTRACT_SYMBOL
},
451 {"file-alignment", required_argument
, 0, OPTION_FILE_ALIGNMENT
},
452 {"format", required_argument
, 0, 'F'}, /* Obsolete */
453 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
454 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
455 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
456 {"heap", required_argument
, 0, OPTION_HEAP
},
457 {"help", no_argument
, 0, 'h'},
458 {"image-base", required_argument
, 0 , OPTION_IMAGE_BASE
},
459 {"impure", no_argument
, 0, OPTION_IMPURE
},
460 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
461 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
462 {"input-target", required_argument
, 0, 'I'},
463 {"interleave", optional_argument
, 0, 'i'},
464 {"interleave-width", required_argument
, 0, OPTION_INTERLEAVE_WIDTH
},
465 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
466 {"keep-global-symbol", required_argument
, 0, 'G'},
467 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
468 {"keep-section", required_argument
, 0, OPTION_KEEP_SECTION
},
469 {"keep-symbol", required_argument
, 0, 'K'},
470 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
471 {"keep-section-symbols", required_argument
, 0, OPTION_KEEP_SECTION_SYMBOLS
},
472 {"localize-hidden", no_argument
, 0, OPTION_LOCALIZE_HIDDEN
},
473 {"localize-symbol", required_argument
, 0, 'L'},
474 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
475 {"long-section-names", required_argument
, 0, OPTION_LONG_SECTION_NAMES
},
476 {"merge-notes", no_argument
, 0, 'M'},
477 {"no-merge-notes", no_argument
, 0, OPTION_NO_MERGE_NOTES
},
478 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
479 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
480 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
481 {"only-section", required_argument
, 0, 'j'},
482 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
483 {"output-target", required_argument
, 0, 'O'},
484 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
485 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
486 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
487 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
488 {"preserve-dates", no_argument
, 0, 'p'},
489 {"pure", no_argument
, 0, OPTION_PURE
},
490 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
491 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
492 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
493 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
494 {"remove-section", required_argument
, 0, 'R'},
495 {"remove-relocations", required_argument
, 0, OPTION_REMOVE_RELOCS
},
496 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
497 {"reverse-bytes", required_argument
, 0, OPTION_REVERSE_BYTES
},
498 {"section-alignment", required_argument
, 0, OPTION_PE_SECTION_ALIGNMENT
},
499 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
500 {"set-section-alignment", required_argument
, 0, OPTION_SET_SECTION_ALIGNMENT
},
501 {"set-start", required_argument
, 0, OPTION_SET_START
},
502 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
503 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
504 {"stack", required_argument
, 0, OPTION_STACK
},
505 {"strip-all", no_argument
, 0, 'S'},
506 {"strip-debug", no_argument
, 0, 'g'},
507 {"strip-dwo", no_argument
, 0, OPTION_STRIP_DWO
},
508 {"strip-symbol", required_argument
, 0, 'N'},
509 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
510 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
511 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
512 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
513 {"subsystem", required_argument
, 0, OPTION_SUBSYSTEM
},
514 {"target", required_argument
, 0, 'F'},
515 {"update-section", required_argument
, 0, OPTION_UPDATE_SECTION
},
516 {"verbose", no_argument
, 0, 'v'},
517 {"verilog-data-width", required_argument
, 0, OPTION_VERILOG_DATA_WIDTH
},
518 {"version", no_argument
, 0, 'V'},
519 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
520 {"weaken-symbol", required_argument
, 0, 'W'},
521 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
522 {"wildcard", no_argument
, 0, 'w'},
523 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
524 {0, no_argument
, 0, 0}
528 extern char *program_name
;
530 /* This flag distinguishes between strip and objcopy:
531 1 means this is 'strip'; 0 means this is 'objcopy'.
532 -1 means if we should use argv[0] to decide. */
535 /* The maximum length of an S record. This variable is defined in srec.c
536 and can be modified by the --srec-len parameter. */
537 extern unsigned int _bfd_srec_len
;
539 /* Restrict the generation of Srecords to type S3 only.
540 This variable is defined in bfd/srec.c and can be toggled
541 on by the --srec-forceS3 command line switch. */
542 extern bool _bfd_srec_forceS3
;
544 /* Width of data in bytes for verilog output.
545 This variable is declared in bfd/verilog.c and can be modified by
546 the --verilog-data-width parameter. */
547 extern unsigned int VerilogDataWidth
;
549 /* Endianness of data for verilog output.
550 This variable is declared in bfd/verilog.c and is set in the
551 copy_object() function. */
552 extern enum bfd_endian VerilogDataEndianness
;
554 /* Forward declarations. */
555 static void setup_section (bfd
*, asection
*, void *);
556 static void setup_bfd_headers (bfd
*, bfd
*);
557 static void copy_relocations_in_section (bfd
*, asection
*, void *);
558 static void copy_section (bfd
*, asection
*, void *);
559 static void get_sections (bfd
*, asection
*, void *);
560 static int compare_section_lma (const void *, const void *);
561 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
562 static bool write_debugging_info (bfd
*, void *, long *, asymbol
***);
563 static const char *lookup_sym_redefinition (const char *);
564 static const char *find_section_rename (const char *, flagword
*);
566 ATTRIBUTE_NORETURN
static void
567 copy_usage (FILE *stream
, int exit_status
)
569 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
570 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
571 fprintf (stream
, _(" The options are:\n"));
572 fprintf (stream
, _("\
573 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
574 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
575 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
576 -F --target <bfdname> Set both input and output format to <bfdname>\n\
577 --debugging Convert debugging information, if possible\n\
578 -p --preserve-dates Copy modified/access timestamps to the output\n"));
579 if (DEFAULT_AR_DETERMINISTIC
)
580 fprintf (stream
, _("\
581 -D --enable-deterministic-archives\n\
582 Produce deterministic output when stripping archives (default)\n\
583 -U --disable-deterministic-archives\n\
584 Disable -D behavior\n"));
586 fprintf (stream
, _("\
587 -D --enable-deterministic-archives\n\
588 Produce deterministic output when stripping archives\n\
589 -U --disable-deterministic-archives\n\
590 Disable -D behavior (default)\n"));
591 fprintf (stream
, _("\
592 -j --only-section <name> Only copy section <name> into the output\n\
593 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
594 -R --remove-section <name> Remove section <name> from the output\n\
595 --remove-relocations <name> Remove relocations from section <name>\n\
596 -S --strip-all Remove all symbol and relocation information\n\
597 -g --strip-debug Remove all debugging symbols & sections\n\
598 --strip-dwo Remove all DWO sections\n\
599 --strip-unneeded Remove all symbols not needed by relocations\n\
600 -N --strip-symbol <name> Do not copy symbol <name>\n\
601 --strip-unneeded-symbol <name>\n\
602 Do not copy symbol <name> unless needed by\n\
604 --only-keep-debug Strip everything but the debug information\n\
605 --extract-dwo Copy only DWO sections\n\
606 --extract-symbol Remove section contents but keep symbols\n\
607 --keep-section <name> Do not strip section <name>\n\
608 -K --keep-symbol <name> Do not strip symbol <name>\n\
609 --keep-section-symbols Do not strip section symbols\n\
610 --keep-file-symbols Do not strip file symbol(s)\n\
611 --localize-hidden Turn all ELF hidden symbols into locals\n\
612 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
613 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
614 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
615 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
616 --weaken Force all global symbols to be marked as weak\n\
617 -w --wildcard Permit wildcard in symbol comparison\n\
618 -x --discard-all Remove all non-global symbols\n\
619 -X --discard-locals Remove any compiler-generated symbols\n\
620 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
621 --interleave-width <number> Set N for --interleave\n\
622 -b --byte <num> Select byte <num> in every interleaved block\n\
623 --gap-fill <val> Fill gaps between sections with <val>\n\
624 --pad-to <addr> Pad the last section up to address <addr>\n\
625 --set-start <addr> Set the start address to <addr>\n\
626 {--change-start|--adjust-start} <incr>\n\
627 Add <incr> to the start address\n\
628 {--change-addresses|--adjust-vma} <incr>\n\
629 Add <incr> to LMA, VMA and start addresses\n\
630 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
631 Change LMA and VMA of section <name> by <val>\n\
632 --change-section-lma <name>{=|+|-}<val>\n\
633 Change the LMA of section <name> by <val>\n\
634 --change-section-vma <name>{=|+|-}<val>\n\
635 Change the VMA of section <name> by <val>\n\
636 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
637 Warn if a named section does not exist\n\
638 --set-section-flags <name>=<flags>\n\
639 Set section <name>'s properties to <flags>\n\
640 --set-section-alignment <name>=<align>\n\
641 Set section <name>'s alignment to <align> bytes\n\
642 --add-section <name>=<file> Add section <name> found in <file> to output\n\
643 --update-section <name>=<file>\n\
644 Update contents of section <name> with\n\
645 contents found in <file>\n\
646 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
647 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
648 --long-section-names {enable|disable|keep}\n\
649 Handle long section names in Coff objects.\n\
650 --change-leading-char Force output format's leading character style\n\
651 --remove-leading-char Remove leading character from global symbols\n\
652 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
653 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
654 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
656 --srec-len <number> Restrict the length of generated Srecords\n\
657 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
658 --strip-symbols <file> -N for all symbols listed in <file>\n\
659 --strip-unneeded-symbols <file>\n\
660 --strip-unneeded-symbol for all symbols listed\n\
662 --keep-symbols <file> -K for all symbols listed in <file>\n\
663 --localize-symbols <file> -L for all symbols listed in <file>\n\
664 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
665 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
666 --weaken-symbols <file> -W for all symbols listed in <file>\n\
667 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
668 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
669 --writable-text Mark the output text as writable\n\
670 --readonly-text Make the output text write protected\n\
671 --pure Mark the output file as demand paged\n\
672 --impure Mark the output file as impure\n\
673 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
674 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
675 --prefix-alloc-sections <prefix>\n\
676 Add <prefix> to start of every allocatable\n\
678 --file-alignment <num> Set PE file alignment to <num>\n\
679 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
681 --image-base <address> Set PE image base to <address>\n\
682 --section-alignment <num> Set PE section alignment to <num>\n\
683 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
685 --subsystem <name>[:<version>]\n\
686 Set PE subsystem to <name> [& <version>]\n\
687 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi|zstd}]\n\
688 Compress DWARF debug sections\n\
689 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
690 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
692 --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
693 -M --merge-notes Remove redundant entries in note sections\n\
694 --no-merge-notes Do not attempt to remove redundant notes (default)\n\
695 -v --verbose List all object files modified\n\
696 @<file> Read options from <file>\n\
697 -V --version Display this program's version number\n\
698 -h --help Display this output\n\
699 --info List object formats & architectures supported\n\
701 list_supported_targets (program_name
, stream
);
702 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
703 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
707 ATTRIBUTE_NORETURN
static void
708 strip_usage (FILE *stream
, int exit_status
)
710 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
711 fprintf (stream
, _(" Removes symbols and sections from files\n"));
712 fprintf (stream
, _(" The options are:\n"));
713 fprintf (stream
, _("\
714 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
715 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
716 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
717 -p --preserve-dates Copy modified/access timestamps to the output\n\
719 if (DEFAULT_AR_DETERMINISTIC
)
720 fprintf (stream
, _("\
721 -D --enable-deterministic-archives\n\
722 Produce deterministic output when stripping archives (default)\n\
723 -U --disable-deterministic-archives\n\
724 Disable -D behavior\n"));
726 fprintf (stream
, _("\
727 -D --enable-deterministic-archives\n\
728 Produce deterministic output when stripping archives\n\
729 -U --disable-deterministic-archives\n\
730 Disable -D behavior (default)\n"));
731 fprintf (stream
, _("\
732 -R --remove-section=<name> Also remove section <name> from the output\n\
733 --remove-relocations <name> Remove relocations from section <name>\n\
734 -s --strip-all Remove all symbol and relocation information\n\
735 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
736 --strip-dwo Remove all DWO sections\n\
737 --strip-unneeded Remove all symbols not needed by relocations\n\
738 --only-keep-debug Strip everything but the debug information\n\
739 -M --merge-notes Remove redundant entries in note sections (default)\n\
740 --no-merge-notes Do not attempt to remove redundant notes\n\
741 -N --strip-symbol=<name> Do not copy symbol <name>\n\
742 --keep-section=<name> Do not strip section <name>\n\
743 -K --keep-symbol=<name> Do not strip symbol <name>\n\
744 --keep-section-symbols Do not strip section symbols\n\
745 --keep-file-symbols Do not strip file symbol(s)\n\
746 -w --wildcard Permit wildcard in symbol comparison\n\
747 -x --discard-all Remove all non-global symbols\n\
748 -X --discard-locals Remove any compiler-generated symbols\n\
749 -v --verbose List all object files modified\n\
750 -V --version Display this program's version number\n\
751 -h --help Display this output\n\
752 --info List object formats & architectures supported\n\
753 -o <file> Place stripped output into <file>\n\
756 list_supported_targets (program_name
, stream
);
757 if (REPORT_BUGS_TO
[0] && exit_status
== 0)
758 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
762 /* Parse section flags into a flagword, with a fatal error if the
763 string can't be parsed. */
766 parse_flags (const char *s
)
776 snext
= strchr (s
, ',');
786 #define PARSE_FLAG(fname,fval) \
787 else if (strncasecmp (fname, s, len) == 0) ret |= fval
788 PARSE_FLAG ("alloc", SEC_ALLOC
);
789 PARSE_FLAG ("load", SEC_LOAD
);
790 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
791 PARSE_FLAG ("readonly", SEC_READONLY
);
792 PARSE_FLAG ("debug", SEC_DEBUGGING
);
793 PARSE_FLAG ("code", SEC_CODE
);
794 PARSE_FLAG ("data", SEC_DATA
);
795 PARSE_FLAG ("rom", SEC_ROM
);
796 PARSE_FLAG ("exclude", SEC_EXCLUDE
);
797 PARSE_FLAG ("share", SEC_COFF_SHARED
);
798 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
799 PARSE_FLAG ("merge", SEC_MERGE
);
800 PARSE_FLAG ("strings", SEC_STRINGS
);
806 copy
= (char *) xmalloc (len
+ 1);
807 strncpy (copy
, s
, len
);
809 non_fatal (_("unrecognized section flag `%s'"), copy
);
810 fatal (_("supported flags: %s"),
811 "alloc, load, noload, readonly, debug, code, data, rom, exclude, share, contents, merge, strings");
821 /* Parse symbol flags into a flagword, with a fatal error if the
822 string can't be parsed. */
825 parse_symflags (const char *s
, const char **other
)
835 snext
= strchr (s
, ',');
844 #define PARSE_FLAG(fname, fval) \
845 else if (len == sizeof fname - 1 \
846 && strncasecmp (fname, s, len) == 0) \
849 #define PARSE_OTHER(fname, fval) \
850 else if (len >= sizeof fname \
851 && strncasecmp (fname, s, sizeof fname - 1) == 0) \
852 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
855 PARSE_FLAG ("local", BSF_LOCAL
);
856 PARSE_FLAG ("global", BSF_GLOBAL
);
857 PARSE_FLAG ("export", BSF_EXPORT
);
858 PARSE_FLAG ("debug", BSF_DEBUGGING
);
859 PARSE_FLAG ("function", BSF_FUNCTION
);
860 PARSE_FLAG ("weak", BSF_WEAK
);
861 PARSE_FLAG ("section", BSF_SECTION_SYM
);
862 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR
);
863 PARSE_FLAG ("warning", BSF_WARNING
);
864 PARSE_FLAG ("indirect", BSF_INDIRECT
);
865 PARSE_FLAG ("file", BSF_FILE
);
866 PARSE_FLAG ("object", BSF_OBJECT
);
867 PARSE_FLAG ("synthetic", BSF_SYNTHETIC
);
868 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION
| BSF_FUNCTION
);
869 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE
| BSF_OBJECT
);
870 PARSE_OTHER ("before=", *other
);
878 copy
= (char *) xmalloc (len
+ 1);
879 strncpy (copy
, s
, len
);
881 non_fatal (_("unrecognized symbol flag `%s'"), copy
);
882 fatal (_("supported flags: %s"),
883 "local, global, export, debug, function, weak, section, "
884 "constructor, warning, indirect, file, object, synthetic, "
885 "indirect-function, unique-object, before=<othersym>");
895 /* Find and optionally add an entry in the change_sections list.
897 We need to be careful in how we match section names because of the support
898 for wildcard characters. For example suppose that the user has invoked
901 --set-section-flags .debug_*=debug
902 --set-section-flags .debug_str=readonly,debug
903 --change-section-address .debug_*ranges=0x1000
905 With the idea that all debug sections will receive the DEBUG flag, the
906 .debug_str section will also receive the READONLY flag and the
907 .debug_ranges and .debug_aranges sections will have their address set to
908 0x1000. (This may not make much sense, but it is just an example).
910 When adding the section name patterns to the section list we need to make
911 sure that previous entries do not match with the new entry, unless the
912 match is exact. (In which case we assume that the user is overriding
913 the previous entry with the new context).
915 When matching real section names to the section list we make use of the
916 wildcard characters, but we must do so in context. Eg if we are setting
917 section addresses then we match for .debug_ranges but not for .debug_info.
919 Finally, if ADD is false and we do find a match, we mark the section list
922 static struct section_list
*
923 find_section_list (const char *name
, bool add
, unsigned int context
)
925 struct section_list
*p
, *match
= NULL
;
927 /* assert ((context & ((1 << 7) - 1)) != 0); */
929 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
933 if (strcmp (p
->pattern
, name
) == 0)
935 /* Check for context conflicts. */
936 if (((p
->context
& SECTION_CONTEXT_REMOVE
)
937 && (context
& SECTION_CONTEXT_COPY
))
938 || ((context
& SECTION_CONTEXT_REMOVE
)
939 && (p
->context
& SECTION_CONTEXT_COPY
)))
940 fatal (_("error: %s both copied and removed"), name
);
942 if (((p
->context
& SECTION_CONTEXT_SET_VMA
)
943 && (context
& SECTION_CONTEXT_ALTER_VMA
))
944 || ((context
& SECTION_CONTEXT_SET_VMA
)
945 && (context
& SECTION_CONTEXT_ALTER_VMA
)))
946 fatal (_("error: %s both sets and alters VMA"), name
);
948 if (((p
->context
& SECTION_CONTEXT_SET_LMA
)
949 && (context
& SECTION_CONTEXT_ALTER_LMA
))
950 || ((context
& SECTION_CONTEXT_SET_LMA
)
951 && (context
& SECTION_CONTEXT_ALTER_LMA
)))
952 fatal (_("error: %s both sets and alters LMA"), name
);
954 /* Extend the context. */
955 p
->context
|= context
;
959 /* If we are not adding a new name/pattern then
960 only check for a match if the context applies. */
961 else if (p
->context
& context
)
963 /* We could check for the presence of wildchar characters
964 first and choose between calling strcmp and fnmatch,
965 but is that really worth it ? */
966 if (p
->pattern
[0] == '!')
968 if (fnmatch (p
->pattern
+ 1, name
, 0) == 0)
976 if (fnmatch (p
->pattern
, name
, 0) == 0)
992 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
995 p
->context
= context
;
1000 p
->next
= change_sections
;
1001 change_sections
= p
;
1006 /* S1 is the entry node already in the table, S2 is the key node. */
1009 eq_string_redefnode (const void *s1
, const void *s2
)
1011 struct redefine_node
*node1
= (struct redefine_node
*) s1
;
1012 struct redefine_node
*node2
= (struct redefine_node
*) s2
;
1013 return !strcmp ((const char *) node1
->source
, (const char *) node2
->source
);
1016 /* P is redefine node. Hash value is generated from its "source" filed. */
1019 htab_hash_redefnode (const void *p
)
1021 struct redefine_node
*redefnode
= (struct redefine_node
*) p
;
1022 return htab_hash_string (redefnode
->source
);
1025 /* Create hashtab used for redefine node. */
1028 create_symbol2redef_htab (void)
1030 return htab_create_alloc (16, htab_hash_redefnode
, eq_string_redefnode
, NULL
,
1035 create_symbol_htab (void)
1037 return htab_create_alloc (16, htab_hash_string
, htab_eq_string
, NULL
,
1042 create_symbol_htabs (void)
1044 strip_specific_htab
= create_symbol_htab ();
1045 strip_unneeded_htab
= create_symbol_htab ();
1046 keep_specific_htab
= create_symbol_htab ();
1047 localize_specific_htab
= create_symbol_htab ();
1048 globalize_specific_htab
= create_symbol_htab ();
1049 keepglobal_specific_htab
= create_symbol_htab ();
1050 weaken_specific_htab
= create_symbol_htab ();
1051 redefine_specific_htab
= create_symbol2redef_htab ();
1052 /* As there is no bidirectional hash table in libiberty, need a reverse table
1053 to check duplicated target string. */
1054 redefine_specific_reverse_htab
= create_symbol_htab ();
1058 delete_symbol_htabs (void)
1060 htab_delete (strip_specific_htab
);
1061 htab_delete (strip_unneeded_htab
);
1062 htab_delete (keep_specific_htab
);
1063 htab_delete (localize_specific_htab
);
1064 htab_delete (globalize_specific_htab
);
1065 htab_delete (keepglobal_specific_htab
);
1066 htab_delete (weaken_specific_htab
);
1067 htab_delete (redefine_specific_htab
);
1068 htab_delete (redefine_specific_reverse_htab
);
1071 /* Add a symbol to strip_specific_list. */
1074 add_specific_symbol (const char *name
, htab_t htab
)
1076 *htab_find_slot (htab
, name
, INSERT
) = (char *) name
;
1079 /* Like add_specific_symbol, but the element type is void *. */
1082 add_specific_symbol_node (const void *node
, htab_t htab
)
1084 *htab_find_slot (htab
, node
, INSERT
) = (void *) node
;
1087 /* Add symbols listed in `filename' to strip_specific_list. */
1089 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
1090 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
1093 add_specific_symbols (const char *filename
, htab_t htab
, char **buffer_p
)
1099 unsigned int line_count
;
1101 size
= get_file_size (filename
);
1108 buffer
= (char *) xmalloc (size
+ 2);
1109 f
= fopen (filename
, FOPEN_RT
);
1111 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
1113 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
1114 fatal (_("%s: fread failed"), filename
);
1117 buffer
[size
] = '\n';
1118 buffer
[size
+ 1] = '\0';
1122 for (line
= buffer
; * line
!= '\0'; line
++)
1127 int finished
= false;
1129 for (eol
= line
;; eol
++)
1135 /* Cope with \n\r. */
1143 /* Cope with \r\n. */
1154 /* Line comment, Terminate the line here, in case a
1155 name is present and then allow the rest of the
1156 loop to find the real end of the line. */
1168 /* A name may now exist somewhere between 'line' and 'eol'.
1169 Strip off leading whitespace and trailing whitespace,
1170 then add it to the list. */
1171 for (name
= line
; IS_WHITESPACE (* name
); name
++)
1173 for (name_end
= name
;
1174 (! IS_WHITESPACE (* name_end
))
1175 && (! IS_LINE_TERMINATOR (* name_end
));
1179 if (! IS_LINE_TERMINATOR (* name_end
))
1183 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
1186 if (! IS_LINE_TERMINATOR (* extra
))
1187 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1188 filename
, line_count
);
1193 if (name_end
> name
)
1194 add_specific_symbol (name
, htab
);
1196 /* Advance line pointer to end of line. The 'eol ++' in the for
1197 loop above will then advance us to the start of the next line. */
1202 /* Do not free the buffer. Parts of it will have been referenced
1203 in the calls to add_specific_symbol. */
1207 /* See whether a symbol should be stripped or kept
1208 based on strip_specific_list and keep_symbols. */
1211 is_specified_symbol_predicate (void **slot
, void *data
)
1213 struct is_specified_symbol_predicate_data
*d
=
1214 (struct is_specified_symbol_predicate_data
*) data
;
1215 const char *slot_name
= (char *) *slot
;
1217 if (*slot_name
!= '!')
1219 if (! fnmatch (slot_name
, d
->name
, 0))
1222 /* Continue traversal, there might be a non-match rule. */
1228 if (! fnmatch (slot_name
+ 1, d
->name
, 0))
1231 /* Stop traversal. */
1236 /* Continue traversal. */
1241 is_specified_symbol (const char *name
, htab_t htab
)
1245 struct is_specified_symbol_predicate_data data
;
1250 htab_traverse (htab
, is_specified_symbol_predicate
, &data
);
1255 return htab_find (htab
, name
) != NULL
;
1258 /* Return a pointer to the symbol used as a signature for GROUP. */
1261 group_signature (asection
*group
)
1263 bfd
*abfd
= group
->owner
;
1264 Elf_Internal_Shdr
*ghdr
;
1266 /* PR 20089: An earlier error may have prevented us from loading the symbol table. */
1270 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
1273 ghdr
= &elf_section_data (group
)->this_hdr
;
1274 if (ghdr
->sh_link
== elf_onesymtab (abfd
))
1276 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1277 Elf_Internal_Shdr
*symhdr
= &elf_symtab_hdr (abfd
);
1279 if (ghdr
->sh_info
> 0
1280 && ghdr
->sh_info
< symhdr
->sh_size
/ bed
->s
->sizeof_sym
)
1281 return isympp
[ghdr
->sh_info
- 1];
1286 /* Return TRUE if the section is a DWO section. */
1289 is_dwo_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1294 if (sec
== NULL
|| (name
= bfd_section_name (sec
)) == NULL
)
1297 len
= strlen (name
);
1301 return startswith (name
+ len
- 4, ".dwo");
1304 /* Return TRUE if section SEC is in the update list. */
1307 is_update_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1309 if (update_sections
!= NULL
)
1311 struct section_add
*pupdate
;
1313 for (pupdate
= update_sections
;
1315 pupdate
= pupdate
->next
)
1317 if (strcmp (sec
->name
, pupdate
->name
) == 0)
1326 is_mergeable_note_section (bfd
* abfd
, asection
* sec
)
1329 && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1330 && elf_section_data (sec
)->this_hdr
.sh_type
== SHT_NOTE
1331 /* FIXME: We currently only support merging GNU_BUILD_NOTEs.
1332 We should add support for more note types. */
1333 && (startswith (sec
->name
, GNU_BUILD_ATTRS_SECTION_NAME
)))
1339 /* See if a non-group section is being removed. */
1342 is_strip_section_1 (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1344 if (find_section_list (bfd_section_name (sec
), false, SECTION_CONTEXT_KEEP
)
1348 if (sections_removed
|| sections_copied
)
1350 struct section_list
*p
;
1351 struct section_list
*q
;
1353 p
= find_section_list (bfd_section_name (sec
), false,
1354 SECTION_CONTEXT_REMOVE
);
1355 q
= find_section_list (bfd_section_name (sec
), false,
1356 SECTION_CONTEXT_COPY
);
1359 fatal (_("error: section %s matches both remove and copy options"),
1360 bfd_section_name (sec
));
1361 if (p
&& is_update_section (abfd
, sec
))
1362 fatal (_("error: section %s matches both update and remove options"),
1363 bfd_section_name (sec
));
1367 if (sections_copied
&& q
== NULL
)
1371 if ((bfd_section_flags (sec
) & SEC_DEBUGGING
) != 0)
1373 if (strip_symbols
== STRIP_DEBUG
1374 || strip_symbols
== STRIP_UNNEEDED
1375 || strip_symbols
== STRIP_ALL
1376 || discard_locals
== LOCALS_ALL
1377 || convert_debugging
)
1379 /* By default we don't want to strip .reloc section.
1380 This section has for pe-coff special meaning. See
1381 pe-dll.c file in ld, and peXXigen.c in bfd for details.
1382 Similarly we do not want to strip debuglink sections. */
1383 const char * kept_sections
[] =
1391 for (i
= ARRAY_SIZE (kept_sections
);i
--;)
1392 if (strcmp (bfd_section_name (sec
), kept_sections
[i
]) == 0)
1398 if (strip_symbols
== STRIP_DWO
)
1399 return is_dwo_section (abfd
, sec
);
1401 if (strip_symbols
== STRIP_NONDEBUG
)
1405 if (strip_symbols
== STRIP_NONDWO
)
1406 return !is_dwo_section (abfd
, sec
);
1411 /* See if a section is being removed. */
1414 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1416 if (is_strip_section_1 (abfd
, sec
))
1419 if ((bfd_section_flags (sec
) & SEC_GROUP
) != 0)
1423 asection
*elt
, *first
;
1425 gsym
= group_signature (sec
);
1426 /* Strip groups without a valid signature. */
1431 If we are going to strip the group signature symbol, then
1432 strip the group section too. */
1434 if ((strip_symbols
== STRIP_ALL
1435 && !is_specified_symbol (gname
, keep_specific_htab
))
1436 || is_specified_symbol (gname
, strip_specific_htab
))
1439 /* Remove the group section if all members are removed. */
1440 first
= elt
= elf_next_in_group (sec
);
1443 if (!is_strip_section_1 (abfd
, elt
))
1445 elt
= elf_next_in_group (elt
);
1457 is_nondebug_keep_contents_section (bfd
*ibfd
, asection
*isection
)
1459 /* Always keep ELF note sections. */
1460 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
1461 return elf_section_type (isection
) == SHT_NOTE
;
1463 /* Always keep the .buildid section for PE/COFF.
1465 Strictly, this should be written "always keep the section storing the debug
1466 directory", but that may be the .text section for objects produced by some
1467 tools, which it is not sensible to keep. */
1468 if (bfd_get_flavour (ibfd
) == bfd_target_coff_flavour
)
1469 return strcmp (bfd_section_name (isection
), ".buildid") == 0;
1474 /* Return true if SYM is a hidden symbol. */
1477 is_hidden_symbol (asymbol
*sym
)
1479 elf_symbol_type
*elf_sym
;
1481 elf_sym
= elf_symbol_from (sym
);
1482 if (elf_sym
!= NULL
)
1483 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
1492 /* Empty name is hopefully never a valid symbol name. */
1493 static const char * empty_name
= "";
1496 need_sym_before (struct addsym_node
**node
, const char *sym
)
1499 struct addsym_node
*ptr
= add_sym_list
;
1501 /* 'othersym' symbols are at the front of the list. */
1502 for (count
= 0; count
< add_symbols
; count
++)
1506 if (ptr
->othersym
== empty_name
)
1508 else if (strcmp (ptr
->othersym
, sym
) == 0)
1510 free ((char *) ptr
->othersym
);
1511 ptr
->othersym
= empty_name
;
1521 create_new_symbol (struct addsym_node
*ptr
, bfd
*obfd
)
1523 asymbol
*sym
= bfd_make_empty_symbol (obfd
);
1525 bfd_set_asymbol_name (sym
, ptr
->symdef
);
1526 sym
->value
= ptr
->symval
;
1527 sym
->flags
= ptr
->flags
;
1530 asection
*sec
= bfd_get_section_by_name (obfd
, ptr
->section
);
1532 fatal (_("Section %s not found"), ptr
->section
);
1536 sym
->section
= bfd_abs_section_ptr
;
1540 /* Choose which symbol entries to copy; put the result in OSYMS.
1541 We don't copy in place, because that confuses the relocs.
1542 Return the number of symbols to print. */
1545 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
1546 asymbol
**isyms
, long symcount
)
1548 asymbol
**from
= isyms
, **to
= osyms
;
1549 long src_count
= 0, dst_count
= 0;
1550 int relocatable
= (abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0;
1552 for (; src_count
< symcount
; src_count
++)
1554 asymbol
*sym
= from
[src_count
];
1555 flagword flags
= sym
->flags
;
1556 char *name
= (char *) bfd_asymbol_name (sym
);
1558 bool used_in_reloc
= false;
1560 bool rem_leading_char
;
1561 bool add_leading_char
;
1563 undefined
= bfd_is_und_section (bfd_asymbol_section (sym
));
1567 struct addsym_node
*ptr
;
1569 if (need_sym_before (&ptr
, name
))
1570 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1573 if (htab_elements (redefine_specific_htab
) || section_rename_list
)
1580 && strcmp (name
+ (name
[2] == '_'), "__gnu_lto_slim") == 0)
1582 fatal (_("redefining symbols does not work on LTO-compiled object files"));
1585 new_name
= (char *) lookup_sym_redefinition (name
);
1586 if (new_name
== name
1587 && (flags
& BSF_SECTION_SYM
) != 0)
1588 new_name
= (char *) find_section_rename (name
, NULL
);
1589 bfd_set_asymbol_name (sym
, new_name
);
1593 /* Check if we will remove the current leading character. */
1596 && name
[0] == bfd_get_symbol_leading_char (abfd
)
1597 && (change_leading_char
1598 || (remove_leading_char
1599 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1601 || bfd_is_com_section (bfd_asymbol_section (sym
))))));
1603 /* Check if we will add a new leading character. */
1606 && (bfd_get_symbol_leading_char (obfd
) != '\0')
1607 && (bfd_get_symbol_leading_char (abfd
) == '\0'
1608 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
1610 /* Short circuit for change_leading_char if we can do it in-place. */
1611 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
1613 name
[0] = bfd_get_symbol_leading_char (obfd
);
1614 bfd_set_asymbol_name (sym
, name
);
1615 rem_leading_char
= false;
1616 add_leading_char
= false;
1619 /* Remove leading char. */
1620 if (rem_leading_char
)
1621 bfd_set_asymbol_name (sym
, ++name
);
1623 /* Add new leading char and/or prefix. */
1624 if (add_leading_char
|| prefix_symbols_string
)
1627 size_t len
= strlen (name
) + 1;
1629 if (add_leading_char
)
1631 if (prefix_symbols_string
)
1632 len
+= strlen (prefix_symbols_string
);
1634 ptr
= n
= (char *) xmalloc (len
);
1635 if (add_leading_char
)
1636 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
1638 if (prefix_symbols_string
)
1640 strcpy (ptr
, prefix_symbols_string
);
1641 ptr
+= strlen (prefix_symbols_string
);
1645 bfd_set_asymbol_name (sym
, n
);
1649 if (strip_symbols
== STRIP_ALL
)
1651 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
1652 || ((flags
& BSF_SECTION_SYM
) != 0
1653 && ((*bfd_asymbol_section (sym
)->symbol_ptr_ptr
)->flags
1657 used_in_reloc
= true;
1659 else if (relocatable
/* Relocatable file. */
1660 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1661 || bfd_is_com_section (bfd_asymbol_section (sym
))))
1663 else if (bfd_decode_symclass (sym
) == 'I')
1664 /* Global symbols in $idata sections need to be retained
1665 even if relocatable is FALSE. External users of the
1666 library containing the $idata section may reference these
1669 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
1670 || (flags
& BSF_WEAK
) != 0
1672 || bfd_is_com_section (bfd_asymbol_section (sym
)))
1673 keep
= strip_symbols
!= STRIP_UNNEEDED
;
1674 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
1675 keep
= (strip_symbols
!= STRIP_DEBUG
1676 && strip_symbols
!= STRIP_UNNEEDED
1677 && ! convert_debugging
);
1678 else if (bfd_coff_get_comdat_section (abfd
, bfd_asymbol_section (sym
)))
1679 /* COMDAT sections store special information in local
1680 symbols, so we cannot risk stripping any of them. */
1682 else /* Local symbol. */
1683 keep
= (strip_symbols
!= STRIP_UNNEEDED
1684 && (discard_locals
!= LOCALS_ALL
1685 && (discard_locals
!= LOCALS_START_L
1686 || ! bfd_is_local_label (abfd
, sym
))));
1688 if (keep
&& is_specified_symbol (name
, strip_specific_htab
))
1690 /* There are multiple ways to set 'keep' above, but if it
1691 was the relocatable symbol case, then that's an error. */
1694 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name
);
1702 && !(flags
& BSF_KEEP
)
1703 && is_specified_symbol (name
, strip_unneeded_htab
))
1707 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
1708 || is_specified_symbol (name
, keep_specific_htab
)))
1711 if (keep
&& is_strip_section (abfd
, bfd_asymbol_section (sym
)))
1716 if (((flags
& (BSF_GLOBAL
| BSF_GNU_UNIQUE
))
1718 && (weaken
|| is_specified_symbol (name
, weaken_specific_htab
)))
1720 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_GNU_UNIQUE
);
1721 sym
->flags
|= BSF_WEAK
;
1725 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
1726 && (is_specified_symbol (name
, localize_specific_htab
)
1727 || (htab_elements (keepglobal_specific_htab
) != 0
1728 && ! is_specified_symbol (name
, keepglobal_specific_htab
))
1729 || (localize_hidden
&& is_hidden_symbol (sym
))))
1731 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
1732 sym
->flags
|= BSF_LOCAL
;
1736 && (flags
& BSF_LOCAL
)
1737 && is_specified_symbol (name
, globalize_specific_htab
))
1739 sym
->flags
&= ~ BSF_LOCAL
;
1740 sym
->flags
|= BSF_GLOBAL
;
1743 to
[dst_count
++] = sym
;
1748 struct addsym_node
*ptr
= add_sym_list
;
1750 for (src_count
= 0; src_count
< add_symbols
; src_count
++)
1754 if (ptr
->othersym
!= empty_name
)
1755 fatal (_("'before=%s' not found"), ptr
->othersym
);
1758 to
[dst_count
++] = create_new_symbol (ptr
, obfd
);
1764 to
[dst_count
] = NULL
;
1769 /* Find the redefined name of symbol SOURCE. */
1772 lookup_sym_redefinition (const char *source
)
1774 struct redefine_node key_node
= {(char *) source
, NULL
};
1775 struct redefine_node
*redef_node
1776 = (struct redefine_node
*) htab_find (redefine_specific_htab
, &key_node
);
1778 return redef_node
== NULL
? source
: redef_node
->target
;
1781 /* Insert a node into symbol redefine hash tabel. */
1784 add_redefine_and_check (const char *cause
, const char *source
,
1787 struct redefine_node
*new_node
1788 = (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
1790 new_node
->source
= strdup (source
);
1791 new_node
->target
= strdup (target
);
1793 if (htab_find (redefine_specific_htab
, new_node
) != HTAB_EMPTY_ENTRY
)
1794 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1797 if (htab_find (redefine_specific_reverse_htab
, target
) != HTAB_EMPTY_ENTRY
)
1798 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1801 /* Insert the NEW_NODE into hash table for quick search. */
1802 add_specific_symbol_node (new_node
, redefine_specific_htab
);
1804 /* Insert the target string into the reverse hash table, this is needed for
1805 duplicated target string check. */
1806 add_specific_symbol (new_node
->target
, redefine_specific_reverse_htab
);
1810 /* Handle the --redefine-syms option. Read lines containing "old new"
1811 from the file, and add them to the symbol redefine list. */
1814 add_redefine_syms_file (const char *filename
)
1823 file
= fopen (filename
, "r");
1825 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1826 filename
, strerror (errno
));
1829 buf
= (char *) xmalloc (bufsize
+ 1 /* For the terminating NUL. */);
1837 /* Collect the input symbol name. */
1838 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1846 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1854 /* Eat white space between the symbol names. */
1855 while (IS_WHITESPACE (c
))
1857 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1862 /* Collect the output symbol name. */
1864 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1872 buf
= (char *) xrealloc (buf
, bufsize
+ 1);
1880 /* Eat white space at end of line. */
1881 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1886 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1887 || c
== '\n' || c
== EOF
)
1890 /* Append the redefinition to the list. */
1892 add_redefine_and_check (filename
, &buf
[0], &buf
[outsym_off
]);
1903 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1905 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1906 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1909 /* Eat the rest of the line and finish it. */
1910 while (c
!= '\n' && c
!= EOF
)
1916 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1922 /* Copy unknown object file IBFD onto OBFD.
1923 Returns TRUE upon success, FALSE otherwise. */
1926 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1929 bfd_size_type tocopy
;
1933 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1935 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1942 non_fatal (_("stat returns negative size for `%s'"),
1943 bfd_get_archive_filename (ibfd
));
1947 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1949 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1954 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1955 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1957 cbuf
= (char *) xmalloc (BUFSIZE
);
1965 if (bfd_bread (cbuf
, tocopy
, ibfd
) != tocopy
)
1967 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
1972 if (bfd_bwrite (cbuf
, tocopy
, obfd
) != tocopy
)
1974 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
1982 /* We should at least to be able to read it back when copying an
1983 unknown object in an archive. */
1984 chmod (bfd_get_filename (obfd
), buf
.st_mode
| S_IRUSR
);
1989 typedef struct objcopy_internal_note
1991 Elf_Internal_Note note
;
1992 unsigned long padded_namesz
;
1995 } objcopy_internal_note
;
1997 #define DEBUG_MERGE 0
2000 #define merge_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
2002 #define merge_debug(format, ...)
2005 /* Returns TRUE iff PNOTE1 overlaps or adjoins PNOTE2. */
2008 overlaps_or_adjoins (objcopy_internal_note
* pnote1
,
2009 objcopy_internal_note
* pnote2
)
2011 if (pnote1
->end
< pnote2
->start
)
2012 /* FIXME: Alignment of 16 bytes taken from x86_64 binaries.
2013 Really we should extract the alignment of the section
2014 covered by the notes. */
2015 return BFD_ALIGN (pnote1
->end
, 16) < pnote2
->start
;
2017 if (pnote2
->end
< pnote2
->start
)
2018 return BFD_ALIGN (pnote2
->end
, 16) < pnote1
->start
;
2020 if (pnote1
->end
< pnote2
->end
)
2023 if (pnote2
->end
< pnote1
->end
)
2029 /* Returns TRUE iff NEEDLE is fully contained by HAYSTACK. */
2032 contained_by (objcopy_internal_note
* needle
,
2033 objcopy_internal_note
* haystack
)
2035 return needle
->start
>= haystack
->start
&& needle
->end
<= haystack
->end
;
2039 is_open_note (objcopy_internal_note
* pnote
)
2041 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_OPEN
;
2045 is_func_note (objcopy_internal_note
* pnote
)
2047 return pnote
->note
.type
== NT_GNU_BUILD_ATTRIBUTE_FUNC
;
2051 is_deleted_note (objcopy_internal_note
* pnote
)
2053 return pnote
->note
.type
== 0;
2057 is_version_note (objcopy_internal_note
* pnote
)
2059 return (pnote
->note
.namesz
> 4
2060 && pnote
->note
.namedata
[0] == 'G'
2061 && pnote
->note
.namedata
[1] == 'A'
2062 && pnote
->note
.namedata
[2] == '$'
2063 && pnote
->note
.namedata
[3] == GNU_BUILD_ATTRIBUTE_VERSION
);
2067 is_64bit (bfd
* abfd
)
2069 /* Should never happen, but let's be paranoid. */
2070 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
2073 return elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
;
2076 /* This sorting function is used to get the notes into an order
2077 that makes merging easy. */
2080 compare_gnu_build_notes (const void * data1
, const void * data2
)
2082 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2083 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2085 /* Sort notes based upon the attribute they record. */
2086 int cmp
= memcmp (pnote1
->note
.namedata
+ 3,
2087 pnote2
->note
.namedata
+ 3,
2088 pnote1
->note
.namesz
< pnote2
->note
.namesz
?
2089 pnote1
->note
.namesz
- 3 : pnote2
->note
.namesz
- 3);
2093 if (pnote1
->end
< pnote2
->start
)
2095 if (pnote1
->start
> pnote2
->end
)
2098 /* Overlaps - we should merge the two ranges. */
2099 if (pnote1
->start
< pnote2
->start
)
2101 if (pnote1
->end
> pnote2
->end
)
2103 if (pnote1
->end
< pnote2
->end
)
2106 /* Put OPEN notes before function notes. */
2107 if (is_open_note (pnote1
) && ! is_open_note (pnote2
))
2109 if (! is_open_note (pnote1
) && is_open_note (pnote2
))
2115 /* This sorting function is used to get the notes into an order
2116 that makes eliminating address ranges easier. */
2119 sort_gnu_build_notes (const void * data1
, const void * data2
)
2121 objcopy_internal_note
* pnote1
= (objcopy_internal_note
*) data1
;
2122 objcopy_internal_note
* pnote2
= (objcopy_internal_note
*) data2
;
2124 if (pnote1
->note
.type
!= pnote2
->note
.type
)
2126 /* Move deleted notes to the end. */
2127 if (is_deleted_note (pnote1
)) /* 1: OFD 2: OFD */
2130 /* Move OPEN notes to the start. */
2131 if (is_open_note (pnote1
)) /* 1: OF 2: OFD */
2134 if (is_deleted_note (pnote2
)) /* 1: F 2: O D */
2137 return 1; /* 1: F 2: O */
2140 /* Sort by starting address. */
2141 if (pnote1
->start
< pnote2
->start
)
2143 if (pnote1
->start
> pnote2
->start
)
2146 /* Then by end address (bigger range first). */
2147 if (pnote1
->end
> pnote2
->end
)
2149 if (pnote1
->end
< pnote2
->end
)
2152 /* Then by attribute type. */
2153 if (pnote1
->note
.namesz
> 4
2154 && pnote2
->note
.namesz
> 4
2155 && pnote1
->note
.namedata
[3] != pnote2
->note
.namedata
[3])
2156 return pnote1
->note
.namedata
[3] - pnote2
->note
.namedata
[3];
2161 /* Merge the notes on SEC, removing redundant entries.
2162 Returns the new, smaller size of the section upon success. */
2164 static bfd_size_type
2165 merge_gnu_build_notes (bfd
* abfd
,
2168 bfd_byte
* contents
)
2170 objcopy_internal_note
* pnotes_end
;
2171 objcopy_internal_note
* pnotes
= NULL
;
2172 objcopy_internal_note
* pnote
;
2173 bfd_size_type remain
= size
;
2174 unsigned version_1_seen
= 0;
2175 unsigned version_2_seen
= 0;
2176 unsigned version_3_seen
= 0;
2177 const char * err
= NULL
;
2178 bfd_byte
* in
= contents
;
2179 unsigned long previous_func_start
= 0;
2180 unsigned long previous_open_start
= 0;
2181 unsigned long previous_func_end
= 0;
2182 unsigned long previous_open_end
= 0;
2185 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
2191 /* If there are relocs associated with this section then we
2192 cannot safely merge it. */
2193 relpp
= (arelent
**) xmalloc (relsize
);
2194 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, isympp
);
2199 non_fatal (_("%s[%s]: Cannot merge - there are relocations against this section"),
2200 bfd_get_filename (abfd
), bfd_section_name (sec
));
2205 /* Make a copy of the notes and convert to our internal format.
2206 Minimum size of a note is 12 bytes. Also locate the version
2207 notes and check them. */
2208 pnote
= pnotes
= (objcopy_internal_note
*)
2209 xcalloc ((size
/ 12), sizeof (* pnote
));
2210 while (remain
>= 12)
2214 pnote
->note
.namesz
= bfd_get_32 (abfd
, in
);
2215 pnote
->note
.descsz
= bfd_get_32 (abfd
, in
+ 4);
2216 pnote
->note
.type
= bfd_get_32 (abfd
, in
+ 8);
2217 pnote
->padded_namesz
= (pnote
->note
.namesz
+ 3) & ~3;
2219 if (((pnote
->note
.descsz
+ 3) & ~3) != pnote
->note
.descsz
)
2221 err
= _("corrupt GNU build attribute note: description size not a factor of 4");
2225 if (pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_OPEN
2226 && pnote
->note
.type
!= NT_GNU_BUILD_ATTRIBUTE_FUNC
)
2228 err
= _("corrupt GNU build attribute note: wrong note type");
2232 if (pnote
->padded_namesz
+ pnote
->note
.descsz
+ 12 > remain
)
2234 err
= _("corrupt GNU build attribute note: note too big");
2238 if (pnote
->note
.namesz
< 2)
2240 err
= _("corrupt GNU build attribute note: name too small");
2244 pnote
->note
.namedata
= (char *)(in
+ 12);
2245 pnote
->note
.descdata
= (char *)(in
+ 12 + pnote
->padded_namesz
);
2247 remain
-= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2248 in
+= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2250 if (pnote
->note
.namesz
> 2
2251 && pnote
->note
.namedata
[0] == '$'
2252 && pnote
->note
.namedata
[1] == GNU_BUILD_ATTRIBUTE_VERSION
2253 && pnote
->note
.namedata
[2] == '1')
2255 else if (is_version_note (pnote
))
2257 if (pnote
->note
.namedata
[4] == '2')
2259 else if (pnote
->note
.namedata
[4] == '3')
2263 err
= _("corrupt GNU build attribute note: unsupported version");
2268 switch (pnote
->note
.descsz
)
2275 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2276 /* FIXME: For version 1 and 2 notes we should try to
2277 calculate the end address by finding a symbol whose
2278 value is START, and then adding in its size.
2280 For now though, since v1 and v2 was not intended to
2281 handle gaps, we chose an artificially large end
2287 start
= bfd_get_32 (abfd
, pnote
->note
.descdata
);
2288 end
= bfd_get_32 (abfd
, pnote
->note
.descdata
+ 4);
2292 start
= bfd_get_64 (abfd
, pnote
->note
.descdata
);
2293 end
= bfd_get_64 (abfd
, pnote
->note
.descdata
+ 8);
2297 err
= _("corrupt GNU build attribute note: bad description size");
2302 /* This can happen with PPC64LE binaries where empty notes are
2303 encoded as start = end + 4. */
2306 if (is_open_note (pnote
))
2309 previous_open_start
= start
;
2311 pnote
->start
= previous_open_start
;
2314 previous_open_end
= end
;
2316 pnote
->end
= previous_open_end
;
2321 previous_func_start
= start
;
2323 pnote
->start
= previous_func_start
;
2326 previous_func_end
= end
;
2328 pnote
->end
= previous_func_end
;
2331 if (pnote
->note
.namedata
[pnote
->note
.namesz
- 1] != 0)
2333 err
= _("corrupt GNU build attribute note: name not NUL terminated");
2342 /* Check that the notes are valid. */
2345 err
= _("corrupt GNU build attribute notes: excess data at end");
2349 if (version_1_seen
== 0 && version_2_seen
== 0 && version_3_seen
== 0)
2352 err
= _("bad GNU build attribute notes: no known versions detected");
2355 /* This happens with glibc. No idea why. */
2356 non_fatal (_("%s[%s]: Warning: version note missing - assuming version 3"),
2357 bfd_get_filename (abfd
), bfd_section_name (sec
));
2362 if ( (version_1_seen
> 0 && version_2_seen
> 0)
2363 || (version_1_seen
> 0 && version_3_seen
> 0)
2364 || (version_2_seen
> 0 && version_3_seen
> 0))
2366 err
= _("bad GNU build attribute notes: multiple different versions");
2370 /* We are now only supporting the merging v3+ notes
2371 - it makes things much simpler. */
2372 if (version_3_seen
== 0)
2374 merge_debug ("%s: skipping merge - not using v3 notes", bfd_section_name (sec
));
2378 merge_debug ("Merging section %s which contains %ld notes\n",
2379 sec
->name
, pnotes_end
- pnotes
);
2381 /* Sort the notes. */
2382 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
),
2383 compare_gnu_build_notes
);
2386 merge_debug ("Results of initial sort:\n");
2387 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2388 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2389 (pnote
->note
.namedata
- (char *) contents
) - 12,
2390 pnote
->start
, pnote
->end
,
2392 pnote
->note
.namedata
[3],
2397 /* Now merge the notes. The rules are:
2398 1. If a note has a zero range, it can be eliminated.
2399 2. If two notes have the same namedata then:
2400 2a. If one note's range is fully covered by the other note
2401 then it can be deleted.
2402 2b. If one note's range partially overlaps or adjoins the
2403 other note then if they are both of the same type (open
2404 or func) then they can be merged and one deleted. If
2405 they are of different types then they cannot be merged. */
2406 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2408 /* Skip already deleted notes.
2409 FIXME: Can this happen ? We are scanning forwards and
2410 deleting backwards after all. */
2411 if (is_deleted_note (pnote
))
2414 /* Rule 1 - delete 0-range notes. */
2415 if (pnote
->start
== pnote
->end
)
2417 merge_debug ("Delete note at offset %#08lx - empty range\n",
2418 (pnote
->note
.namedata
- (char *) contents
) - 12);
2419 pnote
->note
.type
= 0;
2424 objcopy_internal_note
* back
;
2426 /* Rule 2: Check to see if there is an identical previous note. */
2427 for (iter
= 0, back
= pnote
- 1; back
>= pnotes
; back
--)
2429 if (is_deleted_note (back
))
2432 /* Our sorting function should have placed all identically
2433 attributed notes together, so if we see a note of a different
2434 attribute type stop searching. */
2435 if (back
->note
.namesz
!= pnote
->note
.namesz
2436 || memcmp (back
->note
.namedata
,
2437 pnote
->note
.namedata
, pnote
->note
.namesz
) != 0)
2440 if (back
->start
== pnote
->start
2441 && back
->end
== pnote
->end
)
2443 merge_debug ("Delete note at offset %#08lx - duplicate of note at offset %#08lx\n",
2444 (pnote
->note
.namedata
- (char *) contents
) - 12,
2445 (back
->note
.namedata
- (char *) contents
) - 12);
2446 pnote
->note
.type
= 0;
2451 if (contained_by (pnote
, back
))
2453 merge_debug ("Delete note at offset %#08lx - fully contained by note at %#08lx\n",
2454 (pnote
->note
.namedata
- (char *) contents
) - 12,
2455 (back
->note
.namedata
- (char *) contents
) - 12);
2456 pnote
->note
.type
= 0;
2461 /* This should not happen as we have sorted the
2462 notes with earlier starting addresses first. */
2463 if (contained_by (back
, pnote
))
2464 merge_debug ("ERROR: UNEXPECTED CONTAINMENT\n");
2468 if (overlaps_or_adjoins (back
, pnote
)
2469 && is_func_note (back
) == is_func_note (pnote
))
2471 merge_debug ("Delete note at offset %#08lx - merge into note at %#08lx\n",
2472 (pnote
->note
.namedata
- (char *) contents
) - 12,
2473 (back
->note
.namedata
- (char *) contents
) - 12);
2475 back
->end
= back
->end
> pnote
->end
? back
->end
: pnote
->end
;
2476 back
->start
= back
->start
< pnote
->start
? back
->start
: pnote
->start
;
2477 pnote
->note
.type
= 0;
2481 /* Don't scan too far back however. */
2484 /* FIXME: Not sure if this can ever be triggered. */
2485 merge_debug ("ITERATION LIMIT REACHED\n");
2490 if (! is_deleted_note (pnote
))
2491 merge_debug ("Unable to do anything with note at %#08lx\n",
2492 (pnote
->note
.namedata
- (char *) contents
) - 12);
2496 /* Resort the notes. */
2497 merge_debug ("Final sorting of notes\n");
2498 qsort (pnotes
, pnotes_end
- pnotes
, sizeof (* pnotes
), sort_gnu_build_notes
);
2500 /* Reconstruct the ELF notes. */
2501 bfd_byte
* new_contents
;
2504 bfd_size_type new_size
;
2505 bfd_vma prev_start
= 0;
2506 bfd_vma prev_end
= 0;
2508 /* Not sure how, but the notes might grow in size.
2509 (eg see PR 1774507). Allow for this here. */
2510 new = new_contents
= xmalloc (size
* 2);
2511 for (pnote
= pnotes
, old
= contents
;
2515 bfd_size_type note_size
= 12 + pnote
->padded_namesz
+ pnote
->note
.descsz
;
2517 if (! is_deleted_note (pnote
))
2519 /* Create the note, potentially using the
2520 address range of the previous note. */
2521 if (pnote
->start
== prev_start
&& pnote
->end
== prev_end
)
2523 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2524 bfd_put_32 (abfd
, 0, new + 4);
2525 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2527 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2528 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2529 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2530 new += pnote
->padded_namesz
;
2534 bfd_put_32 (abfd
, pnote
->note
.namesz
, new);
2535 bfd_put_32 (abfd
, is_64bit (abfd
) ? 16 : 8, new + 4);
2536 bfd_put_32 (abfd
, pnote
->note
.type
, new + 8);
2538 memcpy (new, pnote
->note
.namedata
, pnote
->note
.namesz
);
2539 if (pnote
->note
.namesz
< pnote
->padded_namesz
)
2540 memset (new + pnote
->note
.namesz
, 0, pnote
->padded_namesz
- pnote
->note
.namesz
);
2541 new += pnote
->padded_namesz
;
2542 if (is_64bit (abfd
))
2544 bfd_put_64 (abfd
, pnote
->start
, new);
2545 bfd_put_64 (abfd
, pnote
->end
, new + 8);
2550 bfd_put_32 (abfd
, pnote
->start
, new);
2551 bfd_put_32 (abfd
, pnote
->end
, new + 4);
2555 prev_start
= pnote
->start
;
2556 prev_end
= pnote
->end
;
2564 merge_debug ("Results of merge:\n");
2565 for (pnote
= pnotes
; pnote
< pnotes_end
; pnote
++)
2566 if (! is_deleted_note (pnote
))
2567 merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
2568 (pnote
->note
.namedata
- (char *) contents
) - 12,
2569 pnote
->start
, pnote
->end
,
2571 pnote
->note
.namedata
[3],
2576 new_size
= new - new_contents
;
2577 if (new_size
< size
)
2579 memcpy (contents
, new_contents
, new_size
);
2582 free (new_contents
);
2587 bfd_set_error (bfd_error_bad_value
);
2588 bfd_nonfatal_message (NULL
, abfd
, sec
, err
);
2597 check_new_section_flags (flagword flags
, bfd
* abfd
, const char * secname
)
2599 /* Only set the SEC_COFF_SHARED flag on COFF files.
2600 The same bit value is used by ELF targets to indicate
2601 compressed sections, and setting that flag here breaks
2603 if ((flags
& SEC_COFF_SHARED
)
2604 && bfd_get_flavour (abfd
) != bfd_target_coff_flavour
)
2606 non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"),
2607 bfd_get_filename (abfd
), secname
);
2608 flags
&= ~ SEC_COFF_SHARED
;
2613 /* Copy object file IBFD onto OBFD.
2614 Returns TRUE upon success, FALSE otherwise. */
2617 copy_object (bfd
*ibfd
, bfd
*obfd
, const bfd_arch_info_type
*input_arch
)
2621 asection
**osections
= NULL
;
2623 asection
*gnu_debuglink_section
= NULL
;
2624 bfd_size_type
*gaps
= NULL
;
2625 bfd_size_type max_gap
= 0;
2628 enum bfd_architecture iarch
;
2630 unsigned int num_sec
, i
;
2632 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
2633 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
2634 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
2636 /* PR 17636: Call non-fatal so that we return to our parent who
2637 may need to tidy temporary files. */
2638 non_fatal (_("unable to change endianness of '%s'"),
2639 bfd_get_archive_filename (ibfd
));
2643 if (ibfd
->read_only
)
2645 non_fatal (_("unable to modify '%s' due to errors"),
2646 bfd_get_archive_filename (ibfd
));
2650 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2652 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
2656 if (ibfd
->sections
== NULL
)
2658 non_fatal (_("error: the input file '%s' has no sections"),
2659 bfd_get_archive_filename (ibfd
));
2663 /* Set the Verilog output endianness based upon the input file's
2664 endianness. We may not be producing verilog format output,
2665 but testing this just adds extra code this is not really
2667 VerilogDataEndianness
= ibfd
->xvec
->byteorder
;
2669 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
2671 if ((do_debug_sections
& compress
) != 0
2672 && do_debug_sections
!= compress
)
2674 non_fatal (_ ("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi|"
2675 "zstd] is unsupported on `%s'"),
2676 bfd_get_archive_filename (ibfd
));
2680 if (do_elf_stt_common
)
2682 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
2683 bfd_get_archive_filename (ibfd
));
2689 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
2690 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
2691 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2700 start
= bfd_get_start_address (ibfd
);
2701 start
+= change_start
;
2704 /* Neither the start address nor the flags
2705 need to be set for a core file. */
2706 if (bfd_get_format (obfd
) != bfd_core
)
2710 flags
= bfd_get_file_flags (ibfd
);
2711 flags
|= bfd_flags_to_set
;
2712 flags
&= ~bfd_flags_to_clear
;
2713 flags
&= bfd_applicable_file_flags (obfd
);
2715 if (strip_symbols
== STRIP_ALL
)
2716 flags
&= ~HAS_RELOC
;
2718 if (!bfd_set_start_address (obfd
, start
)
2719 || !bfd_set_file_flags (obfd
, flags
))
2721 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2726 /* Copy architecture of input file to output file. */
2727 iarch
= bfd_get_arch (ibfd
);
2728 imach
= bfd_get_mach (ibfd
);
2731 if (iarch
== bfd_arch_unknown
)
2733 iarch
= input_arch
->arch
;
2734 imach
= input_arch
->mach
;
2737 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
2738 bfd_get_archive_filename (ibfd
));
2740 if (iarch
== bfd_arch_unknown
2741 && bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2742 && bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2744 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
2748 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
2749 && (ibfd
->target_defaulted
2750 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
2752 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
2753 non_fatal (_("Unable to recognise the format of the input file `%s'"),
2754 bfd_get_archive_filename (ibfd
));
2756 non_fatal (_("Output file cannot represent architecture `%s'"),
2757 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
2758 bfd_get_mach (ibfd
)));
2762 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
2764 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2768 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2769 && bfd_pei_p (obfd
))
2771 /* Set up PE parameters. */
2772 pe_data_type
*pe
= pe_data (obfd
);
2774 /* Copy PE parameters before changing them. */
2775 if (bfd_get_flavour (ibfd
) == bfd_target_coff_flavour
2776 && bfd_pei_p (ibfd
))
2778 pe
->pe_opthdr
= pe_data (ibfd
)->pe_opthdr
;
2781 pe
->timestamp
= pe_data (ibfd
)->coff
.timestamp
;
2786 if (pe_file_alignment
!= (bfd_vma
) -1)
2787 pe
->pe_opthdr
.FileAlignment
= pe_file_alignment
;
2789 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
2791 if (pe_heap_commit
!= (bfd_vma
) -1)
2792 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_commit
;
2794 if (pe_heap_reserve
!= (bfd_vma
) -1)
2795 pe
->pe_opthdr
.SizeOfHeapCommit
= pe_heap_reserve
;
2797 if (pe_image_base
!= (bfd_vma
) -1)
2798 pe
->pe_opthdr
.ImageBase
= pe_image_base
;
2800 if (pe_section_alignment
!= (bfd_vma
) -1)
2801 pe
->pe_opthdr
.SectionAlignment
= pe_section_alignment
;
2803 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
2805 if (pe_stack_commit
!= (bfd_vma
) -1)
2806 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_commit
;
2808 if (pe_stack_reserve
!= (bfd_vma
) -1)
2809 pe
->pe_opthdr
.SizeOfStackCommit
= pe_stack_reserve
;
2811 if (pe_subsystem
!= -1)
2812 pe
->pe_opthdr
.Subsystem
= pe_subsystem
;
2814 if (pe_major_subsystem_version
!= -1)
2815 pe
->pe_opthdr
.MajorSubsystemVersion
= pe_major_subsystem_version
;
2817 if (pe_minor_subsystem_version
!= -1)
2818 pe
->pe_opthdr
.MinorSubsystemVersion
= pe_minor_subsystem_version
;
2820 if (pe_file_alignment
> pe_section_alignment
)
2822 non_fatal (_("warning: file alignment (0x%" PRIx64
2823 ") > section alignment (0x%" PRIx64
")"),
2824 (uint64_t) pe_file_alignment
,
2825 (uint64_t) pe_section_alignment
);
2831 if (osympp
!= isympp
)
2837 symsize
= bfd_get_symtab_upper_bound (ibfd
);
2840 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2844 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
2845 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
2848 bfd_nonfatal_message (NULL
, ibfd
, NULL
, NULL
);
2851 /* PR 17512: file: d6323821
2852 If the symbol table could not be loaded do not pretend that we have
2853 any symbols. This trips us up later on when we load the relocs. */
2857 osympp
= isympp
= NULL
;
2860 /* BFD mandates that all output sections be created and sizes set before
2861 any output is done. Thus, we traverse all sections multiple times. */
2862 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
2864 if (!extract_symbol
)
2865 setup_bfd_headers (ibfd
, obfd
);
2867 if (add_sections
!= NULL
)
2869 struct section_add
*padd
;
2870 struct section_list
*pset
;
2872 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
2876 pset
= find_section_list (padd
->name
, false,
2877 SECTION_CONTEXT_SET_FLAGS
);
2880 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
2881 flags
= check_new_section_flags (flags
, obfd
, padd
->name
);
2884 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
2886 /* bfd_make_section_with_flags() does not return very helpful
2887 error codes, so check for the most likely user error first. */
2888 if (bfd_get_section_by_name (obfd
, padd
->name
))
2890 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2891 _("can't add section '%s'"), padd
->name
);
2896 /* We use LINKER_CREATED here so that the backend hooks
2897 will create any special section type information,
2898 instead of presuming we know what we're doing merely
2899 because we set the flags. */
2900 padd
->section
= bfd_make_section_with_flags
2901 (obfd
, padd
->name
, flags
| SEC_LINKER_CREATED
);
2902 if (padd
->section
== NULL
)
2904 bfd_nonfatal_message (NULL
, obfd
, NULL
,
2905 _("can't create section `%s'"),
2911 if (!bfd_set_section_size (padd
->section
, padd
->size
))
2913 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2917 pset
= find_section_list (padd
->name
, false,
2918 SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
);
2920 && !bfd_set_section_vma (padd
->section
, pset
->vma_val
))
2922 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2926 pset
= find_section_list (padd
->name
, false,
2927 SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
);
2930 padd
->section
->lma
= pset
->lma_val
;
2932 if (!bfd_set_section_alignment
2933 (padd
->section
, bfd_section_alignment (padd
->section
)))
2935 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
2942 if (update_sections
!= NULL
)
2944 struct section_add
*pupdate
;
2946 for (pupdate
= update_sections
;
2948 pupdate
= pupdate
->next
)
2950 pupdate
->section
= bfd_get_section_by_name (ibfd
, pupdate
->name
);
2951 if (pupdate
->section
== NULL
)
2953 non_fatal (_("error: %s not found, can't be updated"), pupdate
->name
);
2957 osec
= pupdate
->section
->output_section
;
2958 if (!bfd_set_section_size (osec
, pupdate
->size
))
2960 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
2966 merged_note_section
* merged_note_sections
= NULL
;
2969 /* This palaver is necessary because we must set the output
2970 section size first, before its contents are ready. */
2971 for (osec
= ibfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
2973 if (! is_mergeable_note_section (ibfd
, osec
))
2976 /* If the section is going to be completly deleted then
2977 do not bother to merge it. */
2978 if (osec
->output_section
== NULL
)
2981 bfd_size_type size
= bfd_section_size (osec
);
2984 /* This can happen, eg when stripping a binary for a second
2985 time. See BZ 2121365 for an example. */
2988 merged_note_section
* merged
= xmalloc (sizeof * merged
);
2989 merged
->contents
= NULL
;
2990 if (! bfd_get_full_section_contents (ibfd
, osec
, & merged
->contents
))
2992 bfd_nonfatal_message (NULL
, ibfd
, osec
,
2993 _("warning: could not load note section"));
2998 merged
->size
= merge_gnu_build_notes (ibfd
, osec
, size
,
3001 /* FIXME: Once we have read the contents in, we must write
3002 them out again. So even if the mergeing has achieved
3003 nothing we still add this entry to the merge list. */
3005 if (size
!= merged
->size
3006 && !bfd_set_section_size (osec
->output_section
, merged
->size
))
3008 bfd_nonfatal_message (NULL
, obfd
, osec
,
3009 _("warning: failed to set merged notes size"));
3010 free (merged
->contents
);
3015 /* Add section to list of merged sections. */
3017 merged
->next
= merged_note_sections
;
3018 merged_note_sections
= merged
;
3022 if (dump_sections
!= NULL
)
3024 struct section_add
* pdump
;
3026 for (pdump
= dump_sections
; pdump
!= NULL
; pdump
= pdump
->next
)
3031 osec
= bfd_get_section_by_name (ibfd
, pdump
->name
);
3034 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3035 _("can't dump section '%s' - it does not exist"),
3040 if ((bfd_section_flags (osec
) & SEC_HAS_CONTENTS
) == 0)
3042 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3043 _("can't dump section - it has no contents"));
3047 bfd_size_type size
= bfd_section_size (osec
);
3048 /* Note - we allow the dumping of zero-sized sections,
3049 creating an empty file. */
3051 f
= fopen (pdump
->filename
, FOPEN_WB
);
3054 bfd_nonfatal_message (pdump
->filename
, NULL
, NULL
,
3055 _("could not open section dump file"));
3059 if (bfd_malloc_and_get_section (ibfd
, osec
, &contents
))
3061 if (size
!= 0 && fwrite (contents
, 1, size
, f
) != size
)
3063 non_fatal (_("error writing section contents to %s (error: %s)"),
3072 bfd_nonfatal_message (NULL
, ibfd
, osec
,
3073 _("could not retrieve section contents"));
3080 if (gnu_debuglink_filename
!= NULL
)
3082 /* PR 15125: Give a helpful warning message if
3083 the debuglink section already exists, and
3084 allow the rest of the copy to complete. */
3085 if (bfd_get_section_by_name (obfd
, ".gnu_debuglink"))
3087 non_fatal (_("%s: debuglink section already exists"),
3088 bfd_get_filename (ibfd
));
3089 gnu_debuglink_filename
= NULL
;
3093 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
3094 (obfd
, gnu_debuglink_filename
);
3096 if (gnu_debuglink_section
== NULL
)
3098 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3099 _("cannot create debug link section `%s'"),
3100 gnu_debuglink_filename
);
3104 /* Special processing for PE format files. We
3105 have no way to distinguish PE from COFF here. */
3106 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
3108 bfd_vma debuglink_vma
;
3109 asection
* highest_section
;
3111 /* The PE spec requires that all sections be adjacent and sorted
3112 in ascending order of VMA. It also specifies that debug
3113 sections should be last. This is despite the fact that debug
3114 sections are not loaded into memory and so in theory have no
3117 This means that the debuglink section must be given a non-zero
3118 VMA which makes it contiguous with other debug sections. So
3119 walk the current section list, find the section with the
3120 highest VMA and start the debuglink section after that one. */
3121 for (osec
= obfd
->sections
, highest_section
= NULL
;
3125 && (highest_section
== NULL
3126 || osec
->vma
> highest_section
->vma
))
3127 highest_section
= osec
;
3129 if (highest_section
)
3130 debuglink_vma
= BFD_ALIGN (highest_section
->vma
3131 + highest_section
->size
,
3132 /* FIXME: We ought to be using
3133 COFF_PAGE_SIZE here or maybe
3134 bfd_section_alignment() (if it
3135 was set) but since this is for PE
3136 and we know the required alignment
3137 it is easier just to hard code it. */
3140 /* Umm, not sure what to do in this case. */
3141 debuglink_vma
= 0x1000;
3143 bfd_set_section_vma (gnu_debuglink_section
, debuglink_vma
);
3148 num_sec
= bfd_count_sections (obfd
);
3150 && (gap_fill_set
|| pad_to_set
))
3154 /* We must fill in gaps between the sections and/or we must pad
3155 the last section to a specified address. We do this by
3156 grabbing a list of the sections, sorting them by VMA, and
3157 increasing the section sizes as required to fill the gaps.
3158 We write out the gap contents below. */
3160 osections
= xmalloc (num_sec
* sizeof (*osections
));
3162 bfd_map_over_sections (obfd
, get_sections
, &set
);
3164 qsort (osections
, num_sec
, sizeof (*osections
), compare_section_lma
);
3166 gaps
= xmalloc (num_sec
* sizeof (*gaps
));
3167 memset (gaps
, 0, num_sec
* sizeof (*gaps
));
3171 for (i
= 0; i
< num_sec
- 1; i
++)
3174 bfd_size_type size
; /* Octets. */
3175 bfd_vma gap_start
, gap_stop
; /* Octets. */
3176 unsigned int opb1
= bfd_octets_per_byte (obfd
, osections
[i
]);
3177 unsigned int opb2
= bfd_octets_per_byte (obfd
, osections
[i
+1]);
3179 flags
= bfd_section_flags (osections
[i
]);
3180 if ((flags
& SEC_HAS_CONTENTS
) == 0
3181 || (flags
& SEC_LOAD
) == 0)
3184 size
= bfd_section_size (osections
[i
]);
3185 gap_start
= bfd_section_lma (osections
[i
]) * opb1
+ size
;
3186 gap_stop
= bfd_section_lma (osections
[i
+ 1]) * opb2
;
3187 if (gap_start
< gap_stop
)
3189 if (!bfd_set_section_size (osections
[i
],
3190 size
+ (gap_stop
- gap_start
)))
3192 bfd_nonfatal_message (NULL
, obfd
, osections
[i
],
3193 _("Can't fill gap after section"));
3197 gaps
[i
] = gap_stop
- gap_start
;
3198 if (max_gap
< gap_stop
- gap_start
)
3199 max_gap
= gap_stop
- gap_start
;
3206 bfd_vma lma
; /* Octets. */
3207 bfd_size_type size
; /* Octets. */
3208 unsigned int opb
= bfd_octets_per_byte (obfd
, osections
[num_sec
- 1]);
3209 bfd_vma _pad_to
= pad_to
* opb
;
3211 lma
= bfd_section_lma (osections
[num_sec
- 1]) * opb
;
3212 size
= bfd_section_size (osections
[num_sec
- 1]);
3213 if (lma
+ size
< _pad_to
)
3215 if (!bfd_set_section_size (osections
[num_sec
- 1], _pad_to
- lma
))
3217 bfd_nonfatal_message (NULL
, obfd
, osections
[num_sec
- 1],
3218 _("can't add padding"));
3223 gaps
[num_sec
- 1] = _pad_to
- (lma
+ size
);
3224 if (max_gap
< _pad_to
- (lma
+ size
))
3225 max_gap
= _pad_to
- (lma
+ size
);
3231 /* Symbol filtering must happen after the output sections
3232 have been created, but before their contents are set. */
3234 if (convert_debugging
)
3235 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
, false);
3237 if ((obfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0
3238 && (obfd
->flags
& HAS_RELOC
) == 0)
3240 if (bfd_keep_unused_section_symbols (obfd
) || keep_section_symbols
)
3242 /* Non-relocatable inputs may not have the unused section
3243 symbols. Mark all section symbols as used to generate
3246 for (asect
= obfd
->sections
; asect
!= NULL
; asect
= asect
->next
)
3248 asect
->symbol
->flags
|= BSF_SECTION_SYM_USED
;
3252 /* Non-relocatable inputs may have the unused section symbols.
3253 Mark all section symbols as unused to excluded them. */
3255 for (s
= 0; s
< symcount
; s
++)
3256 if ((isympp
[s
]->flags
& BSF_SECTION_SYM_USED
))
3257 isympp
[s
]->flags
&= ~BSF_SECTION_SYM_USED
;
3261 if (strip_symbols
== STRIP_DEBUG
3262 || strip_symbols
== STRIP_ALL
3263 || strip_symbols
== STRIP_UNNEEDED
3264 || strip_symbols
== STRIP_NONDEBUG
3265 || strip_symbols
== STRIP_DWO
3266 || strip_symbols
== STRIP_NONDWO
3267 || discard_locals
!= LOCALS_UNDEF
3269 || htab_elements (strip_specific_htab
) != 0
3270 || htab_elements (keep_specific_htab
) != 0
3271 || htab_elements (localize_specific_htab
) != 0
3272 || htab_elements (globalize_specific_htab
) != 0
3273 || htab_elements (keepglobal_specific_htab
) != 0
3274 || htab_elements (weaken_specific_htab
) != 0
3275 || htab_elements (redefine_specific_htab
) != 0
3276 || prefix_symbols_string
3279 || convert_debugging
3280 || change_leading_char
3281 || remove_leading_char
3282 || section_rename_list
3286 /* Mark symbols used in output relocations so that they
3287 are kept, even if they are local labels or static symbols.
3289 Note we iterate over the input sections examining their
3290 relocations since the relocations for the output sections
3291 haven't been set yet. mark_symbols_used_in_relocations will
3292 ignore input sections which have no corresponding output
3294 if (strip_symbols
!= STRIP_ALL
)
3296 bfd_set_error (bfd_error_no_error
);
3297 bfd_map_over_sections (ibfd
,
3298 mark_symbols_used_in_relocations
,
3300 if (bfd_get_error () != bfd_error_no_error
)
3307 osympp
= (asymbol
**) xmalloc ((symcount
+ add_symbols
+ 1) * sizeof (asymbol
*));
3308 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
3311 if (convert_debugging
&& dhandle
!= NULL
)
3315 res
= write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
);
3318 dhandle
= NULL
; /* Paranoia... */
3327 bfd_set_symtab (obfd
, osympp
, symcount
);
3329 /* This has to happen before section positions are set. */
3330 bfd_map_over_sections (ibfd
, copy_relocations_in_section
, obfd
);
3332 /* This has to happen after the symbol table has been set. */
3333 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
3335 if (add_sections
!= NULL
)
3337 struct section_add
*padd
;
3339 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
3341 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
3344 bfd_nonfatal_message (NULL
, obfd
, padd
->section
, NULL
);
3350 if (update_sections
!= NULL
)
3352 struct section_add
*pupdate
;
3354 for (pupdate
= update_sections
;
3356 pupdate
= pupdate
->next
)
3358 osec
= pupdate
->section
->output_section
;
3359 if (! bfd_set_section_contents (obfd
, osec
, pupdate
->contents
,
3362 bfd_nonfatal_message (NULL
, obfd
, osec
, NULL
);
3368 if (merged_note_sections
!= NULL
)
3370 merged_note_section
* merged
= NULL
;
3372 for (osec
= obfd
->sections
; osec
!= NULL
; osec
= osec
->next
)
3374 if (! is_mergeable_note_section (obfd
, osec
))
3378 merged
= merged_note_sections
;
3380 /* It is likely that output sections are in the same order
3381 as the input sections, but do not assume that this is
3383 if (merged
->sec
->output_section
!= osec
)
3385 for (merged
= merged_note_sections
;
3387 merged
= merged
->next
)
3388 if (merged
->sec
->output_section
== osec
)
3393 bfd_nonfatal_message
3395 _("error: failed to locate merged notes"));
3400 if (merged
->contents
== NULL
)
3402 bfd_nonfatal_message
3404 _("error: failed to merge notes"));
3408 if (! bfd_set_section_contents (obfd
, osec
, merged
->contents
, 0,
3411 bfd_nonfatal_message
3413 _("error: failed to copy merged notes into output"));
3417 merged
= merged
->next
;
3420 /* Free the memory. */
3421 merged_note_section
* next
;
3422 for (merged
= merged_note_sections
; merged
!= NULL
; merged
= next
)
3424 next
= merged
->next
;
3425 free (merged
->contents
);
3429 else if (merge_notes
&& ! is_strip
)
3430 non_fatal (_("%s: Could not find any mergeable note sections"),
3431 bfd_get_filename (ibfd
));
3433 if (gnu_debuglink_filename
!= NULL
)
3435 if (! bfd_fill_in_gnu_debuglink_section
3436 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
3438 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3439 _("cannot fill debug link section `%s'"),
3440 gnu_debuglink_filename
);
3449 /* Fill in the gaps. */
3452 buf
= (bfd_byte
*) xmalloc (max_gap
);
3453 memset (buf
, gap_fill
, max_gap
);
3455 for (i
= 0; i
< num_sec
; i
++)
3463 off
= bfd_section_size (osections
[i
]) - left
;
3474 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
3477 bfd_nonfatal_message (NULL
, obfd
, osections
[i
], NULL
);
3493 /* Allow the BFD backend to copy any private data it understands
3494 from the input BFD to the output BFD. This is done last to
3495 permit the routine to look at the filtered symbol table, which is
3496 important for the ECOFF code at least. */
3497 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
3499 bfd_nonfatal_message (NULL
, obfd
, NULL
,
3500 _("error copying private BFD data"));
3504 /* Switch to the alternate machine code. We have to do this at the
3505 very end, because we only initialize the header when we create
3506 the first section. */
3507 if (use_alt_mach_code
!= 0)
3509 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
3511 non_fatal (_("this target does not support %lu alternative machine codes"),
3513 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
3515 non_fatal (_("treating that number as an absolute e_machine value instead"));
3516 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
3519 non_fatal (_("ignoring the alternative value"));
3526 /* Read each archive element in turn from IBFD, copy the
3527 contents to temp file, and keep the temp file handle.
3528 If 'force_output_target' is TRUE then make sure that
3529 all elements in the new archive are of the type
3533 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
,
3534 bool force_output_target
,
3535 const bfd_arch_info_type
*input_arch
)
3539 struct name_list
*next
;
3543 bfd
**ptr
= &obfd
->archive_head
;
3548 /* PR 24281: It is not clear what should happen when copying a thin archive.
3549 One part is straight forward - if the output archive is in a different
3550 directory from the input archive then any relative paths in the library
3551 should be adjusted to the new location. But if any transformation
3552 options are active (eg strip, rename, add, etc) then the implication is
3553 that these should be applied to the files pointed to by the archive.
3554 But since objcopy is not destructive, this means that new files must be
3555 created, and there is no guidance for the names of the new files. (Plus
3556 this conflicts with one of the goals of thin libraries - only taking up
3557 a minimal amount of space in the file system).
3559 So for now we fail if an attempt is made to copy such libraries. */
3560 if (ibfd
->is_thin_archive
)
3563 bfd_set_error (bfd_error_invalid_operation
);
3564 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
3565 _("sorry: copying thin archives is not currently supported"));
3569 /* Make a temp directory to hold the contents. */
3570 dir
= make_tempdir (bfd_get_filename (obfd
));
3572 fatal (_("cannot create tempdir for archive copying (error: %s)"),
3575 if (strip_symbols
== STRIP_ALL
)
3576 obfd
->has_armap
= false;
3578 obfd
->has_armap
= ibfd
->has_armap
;
3579 obfd
->is_thin_archive
= ibfd
->is_thin_archive
;
3582 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
3586 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
3588 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
3591 bfd_nonfatal_message (NULL
, obfd
, NULL
, NULL
);
3592 goto cleanup_and_exit
;
3595 while (!status
&& this_element
!= NULL
)
3601 int stat_status
= 0;
3605 /* PR binutils/17533: Do not allow directory traversal
3606 outside of the current directory tree by archive members. */
3607 if (! is_valid_archive_path (bfd_get_filename (this_element
)))
3609 non_fatal (_("illegal pathname found in archive member: %s"),
3610 bfd_get_filename (this_element
));
3612 goto cleanup_and_exit
;
3615 /* Create an output file for this member. */
3616 output_name
= concat (dir
, "/",
3617 bfd_get_filename (this_element
), (char *) 0);
3619 /* If the file already exists, make another temp dir. */
3620 if (stat (output_name
, &buf
) >= 0)
3622 char * tmpdir
= make_tempdir (output_name
);
3627 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
3630 goto cleanup_and_exit
;
3633 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
3638 output_name
= concat (tmpdir
, "/",
3639 bfd_get_filename (this_element
), (char *) 0);
3644 memset (&buf
, 0, sizeof (buf
));
3645 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
3647 if (stat_status
!= 0)
3648 non_fatal (_("internal stat error on %s"),
3649 bfd_get_filename (this_element
));
3652 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
3653 l
->name
= output_name
;
3658 ok_object
= bfd_check_format (this_element
, bfd_object
);
3660 bfd_nonfatal_message (NULL
, this_element
, NULL
,
3661 _("Unable to recognise the format of file"));
3663 /* PR binutils/3110: Cope with archives
3664 containing multiple target types. */
3665 if (force_output_target
|| !ok_object
)
3666 output_bfd
= bfd_openw (output_name
, output_target
);
3668 output_bfd
= bfd_openw (output_name
, bfd_get_target (this_element
));
3670 if (output_bfd
== NULL
)
3672 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3674 goto cleanup_and_exit
;
3679 del
= !copy_object (this_element
, output_bfd
, input_arch
);
3681 if (del
&& bfd_get_arch (this_element
) == bfd_arch_unknown
)
3682 /* Try again as an unknown object file. */
3687 del
= !copy_unknown_object (this_element
, output_bfd
);
3689 if (!(ok_object
&& !del
? bfd_close
: bfd_close_all_done
) (output_bfd
))
3691 bfd_nonfatal_message (output_name
, NULL
, NULL
, NULL
);
3692 /* Error in new object file. Don't change archive. */
3698 unlink (output_name
);
3703 if (preserve_dates
&& stat_status
== 0)
3704 set_times (output_name
, &buf
);
3706 /* Open the newly output file and attach to our list. */
3707 output_bfd
= bfd_openr (output_name
, output_target
);
3709 l
->obfd
= output_bfd
;
3712 ptr
= &output_bfd
->archive_next
;
3714 last_element
= this_element
;
3716 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
3718 bfd_close (last_element
);
3723 filename
= xstrdup (bfd_get_filename (obfd
));
3724 if (!(status
== 0 ? bfd_close
: bfd_close_all_done
) (obfd
))
3727 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3731 filename
= xstrdup (bfd_get_filename (ibfd
));
3732 if (!bfd_close (ibfd
))
3735 bfd_nonfatal_message (filename
, NULL
, NULL
, NULL
);
3740 /* Delete all the files that we opened. */
3742 struct name_list
* next
;
3744 for (l
= list
; l
!= NULL
; l
= next
)
3746 if (l
->obfd
== NULL
)
3750 bfd_close (l
->obfd
);
3753 free ((char *) l
->name
);
3764 set_long_section_mode (bfd
*output_bfd
, bfd
*input_bfd
, enum long_section_name_handling style
)
3766 /* This is only relevant to Coff targets. */
3767 if (bfd_get_flavour (output_bfd
) == bfd_target_coff_flavour
)
3770 && bfd_get_flavour (input_bfd
) == bfd_target_coff_flavour
)
3771 style
= bfd_coff_long_section_names (input_bfd
) ? ENABLE
: DISABLE
;
3772 bfd_coff_set_long_section_names (output_bfd
, style
!= DISABLE
);
3776 /* The top-level control. */
3779 copy_file (const char *input_filename
, const char *output_filename
, int ofd
,
3780 struct stat
*in_stat
, const char *input_target
,
3781 const char *output_target
, const bfd_arch_info_type
*input_arch
)
3784 char **obj_matching
;
3785 char **core_matching
;
3786 off_t size
= get_file_size (input_filename
);
3791 non_fatal (_("error: the input file '%s' is empty"),
3797 /* To allow us to do "strip *" without dying on the first
3798 non-object file, failures are nonfatal. */
3799 ibfd
= bfd_openr (input_filename
, input_target
);
3800 if (ibfd
== NULL
|| bfd_stat (ibfd
, in_stat
) != 0)
3802 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3807 switch (do_debug_sections
)
3809 case compress_gnu_zlib
:
3810 ibfd
->flags
|= BFD_COMPRESS
;
3814 /* The above two cases ought to just set BFD_COMPRESS for non-ELF
3815 but we can't tell whether a file is ELF or not until after
3816 bfd_check_format_matches. FIXME maybe: decide compression
3817 style in BFD after bfd_check_format_matches. */
3818 case compress_gabi_zlib
:
3819 ibfd
->flags
|= BFD_COMPRESS
| BFD_COMPRESS_GABI
;
3822 ibfd
->flags
|= BFD_COMPRESS
| BFD_COMPRESS_GABI
| BFD_COMPRESS_ZSTD
;
3824 fatal (_ ("--compress-debug-sections=zstd: binutils is not built with "
3829 ibfd
->flags
|= BFD_DECOMPRESS
;
3835 switch (do_elf_stt_common
)
3837 case elf_stt_common
:
3838 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
| BFD_USE_ELF_STT_COMMON
;
3841 case no_elf_stt_common
:
3842 ibfd
->flags
|= BFD_CONVERT_ELF_COMMON
;
3848 if (bfd_check_format (ibfd
, bfd_archive
))
3850 bool force_output_target
;
3853 /* bfd_get_target does not return the correct value until
3854 bfd_check_format succeeds. */
3855 if (output_target
== NULL
)
3857 output_target
= bfd_get_target (ibfd
);
3858 force_output_target
= false;
3861 force_output_target
= true;
3864 obfd
= bfd_fdopenw (output_filename
, output_target
, ofd
);
3866 obfd
= bfd_openw (output_filename
, output_target
);
3871 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3876 if (gnu_debuglink_filename
!= NULL
)
3878 non_fatal (_("--add-gnu-debuglink ignored for archive %s"),
3879 bfd_get_filename (ibfd
));
3880 gnu_debuglink_filename
= NULL
;
3883 /* This is a no-op on non-Coff targets. */
3884 set_long_section_mode (obfd
, ibfd
, long_section_names
);
3886 copy_archive (ibfd
, obfd
, output_target
, force_output_target
, input_arch
);
3888 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
3893 /* bfd_get_target does not return the correct value until
3894 bfd_check_format succeeds. */
3895 if (output_target
== NULL
)
3896 output_target
= bfd_get_target (ibfd
);
3899 obfd
= bfd_fdopenw (output_filename
, output_target
, ofd
);
3901 obfd
= bfd_openw (output_filename
, output_target
);
3906 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3911 /* This is a no-op on non-Coff targets. */
3912 set_long_section_mode (obfd
, ibfd
, long_section_names
);
3914 if (! copy_object (ibfd
, obfd
, input_arch
))
3917 /* PR 17512: file: 0f15796a.
3918 If the file could not be copied it may not be in a writeable
3919 state. So use bfd_close_all_done to avoid the possibility of
3920 writing uninitialised data into the file. */
3921 if (! (status
? bfd_close_all_done (obfd
) : bfd_close (obfd
)))
3924 bfd_nonfatal_message (output_filename
, NULL
, NULL
, NULL
);
3928 if (!bfd_close (ibfd
))
3931 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3937 bfd_error_type obj_error
= bfd_get_error ();
3938 bfd_error_type core_error
;
3940 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
3942 /* This probably can't happen.. */
3943 if (obj_error
== bfd_error_file_ambiguously_recognized
)
3944 free (obj_matching
);
3948 core_error
= bfd_get_error ();
3949 /* Report the object error in preference to the core error. */
3950 if (obj_error
!= core_error
)
3951 bfd_set_error (obj_error
);
3953 bfd_nonfatal_message (input_filename
, NULL
, NULL
, NULL
);
3955 if (obj_error
== bfd_error_file_ambiguously_recognized
)
3956 list_matching_formats (obj_matching
);
3957 if (core_error
== bfd_error_file_ambiguously_recognized
)
3958 list_matching_formats (core_matching
);
3964 /* Add a name to the section renaming list. */
3967 add_section_rename (const char * old_name
, const char * new_name
,
3970 section_rename
* srename
;
3972 /* Check for conflicts first. */
3973 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
3974 if (strcmp (srename
->old_name
, old_name
) == 0)
3976 /* Silently ignore duplicate definitions. */
3977 if (strcmp (srename
->new_name
, new_name
) == 0
3978 && srename
->flags
== flags
)
3981 fatal (_("Multiple renames of section %s"), old_name
);
3984 srename
= (section_rename
*) xmalloc (sizeof (* srename
));
3986 srename
->old_name
= old_name
;
3987 srename
->new_name
= new_name
;
3988 srename
->flags
= flags
;
3989 srename
->next
= section_rename_list
;
3991 section_rename_list
= srename
;
3994 /* Check the section rename list for a new name of the input section
3995 called OLD_NAME. Returns the new name if one is found and sets
3996 RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
3999 find_section_rename (const char *old_name
, flagword
*returned_flags
)
4001 const section_rename
*srename
;
4003 for (srename
= section_rename_list
; srename
!= NULL
; srename
= srename
->next
)
4004 if (strcmp (srename
->old_name
, old_name
) == 0)
4006 if (returned_flags
!= NULL
&& srename
->flags
!= (flagword
) -1)
4007 *returned_flags
= srename
->flags
;
4009 return srename
->new_name
;
4015 /* Once each of the sections is copied, we may still need to do some
4016 finalization work for private section headers. Do that here. */
4019 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
4021 /* Allow the BFD backend to copy any private data it understands
4022 from the input section to the output section. */
4023 if (! bfd_copy_private_header_data (ibfd
, obfd
))
4026 bfd_nonfatal_message (NULL
, ibfd
, NULL
,
4027 _("error in private header data"));
4031 /* All went well. */
4035 /* Create a section in OBFD with the same
4036 name and attributes as ISECTION in IBFD. */
4039 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4041 bfd
*obfd
= (bfd
*) obfdarg
;
4042 struct section_list
*p
;
4048 const char *err
= NULL
;
4050 const char * new_name
;
4051 char *prefix
= NULL
;
4053 unsigned int alignment
;
4055 if (is_strip_section (ibfd
, isection
))
4058 /* Get the, possibly new, name of the output section. */
4059 name
= bfd_section_name (isection
);
4060 flags
= bfd_section_flags (isection
);
4061 if (bfd_get_flavour (ibfd
) != bfd_get_flavour (obfd
))
4063 flags
&= bfd_applicable_section_flags (ibfd
);
4064 flags
&= bfd_applicable_section_flags (obfd
);
4066 new_name
= find_section_rename (name
, &flags
);
4067 if (new_name
!= name
)
4070 flags
= check_new_section_flags (flags
, obfd
, name
);
4073 /* Prefix sections. */
4074 if (prefix_alloc_sections_string
4075 && (bfd_section_flags (isection
) & SEC_ALLOC
) != 0)
4076 prefix
= prefix_alloc_sections_string
;
4077 else if (prefix_sections_string
)
4078 prefix
= prefix_sections_string
;
4084 n
= (char *) xmalloc (strlen (prefix
) + strlen (name
) + 1);
4090 make_nobits
= false;
4092 p
= find_section_list (bfd_section_name (isection
), false,
4093 SECTION_CONTEXT_SET_FLAGS
);
4096 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
4097 flags
= check_new_section_flags (flags
, obfd
, bfd_section_name (isection
));
4099 else if (strip_symbols
== STRIP_NONDEBUG
4100 && (flags
& (SEC_ALLOC
| SEC_GROUP
)) != 0
4101 && !is_nondebug_keep_contents_section (ibfd
, isection
))
4103 flagword clr
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_GROUP
;
4105 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
4107 /* PR 29532: Copy group sections intact as otherwise we end up with
4108 empty groups. This prevents separate debug info files from
4109 being used with GDB, if they were based upon files that
4110 originally contained groups. */
4111 if (flags
& SEC_GROUP
)
4116 /* Twiddle the input section flags so that it seems to
4117 elf.c:copy_private_bfd_data that section flags have not
4118 changed between input and output sections. This hack
4119 prevents wholesale rewriting of the program headers. */
4120 isection
->flags
&= ~clr
;
4125 if (!bfd_convert_section_setup (ibfd
, isection
, obfd
, &name
, &size
))
4128 err
= _("failed to create output section");
4132 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
4134 if (osection
== NULL
)
4136 err
= _("failed to create output section");
4141 size
= (size
+ interleave
- 1) / interleave
* copy_width
;
4142 else if (extract_symbol
)
4144 if (!bfd_set_section_size (osection
, size
))
4145 err
= _("failed to set size");
4147 vma
= bfd_section_vma (isection
);
4148 p
= find_section_list (bfd_section_name (isection
), false,
4149 SECTION_CONTEXT_ALTER_VMA
| SECTION_CONTEXT_SET_VMA
);
4152 if (p
->context
& SECTION_CONTEXT_SET_VMA
)
4158 vma
+= change_section_address
;
4160 if (!bfd_set_section_vma (osection
, vma
))
4161 err
= _("failed to set vma");
4163 lma
= isection
->lma
;
4164 p
= find_section_list (bfd_section_name (isection
), false,
4165 SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_SET_LMA
);
4168 if (p
->context
& SECTION_CONTEXT_ALTER_LMA
)
4174 lma
+= change_section_address
;
4176 osection
->lma
= lma
;
4178 p
= find_section_list (bfd_section_name (isection
), false,
4179 SECTION_CONTEXT_SET_ALIGNMENT
);
4181 alignment
= p
->alignment
;
4183 alignment
= bfd_section_alignment (isection
);
4185 /* FIXME: This is probably not enough. If we change the LMA we
4186 may have to recompute the header for the file as well. */
4187 if (!bfd_set_section_alignment (osection
, alignment
))
4188 err
= _("failed to set alignment");
4190 /* Copy merge entity size. */
4191 osection
->entsize
= isection
->entsize
;
4193 /* Copy compress status. */
4194 osection
->compress_status
= isection
->compress_status
;
4196 /* This used to be mangle_section; we do here to avoid using
4197 bfd_get_section_by_name since some formats allow multiple
4198 sections with the same name. */
4199 isection
->output_section
= osection
;
4200 isection
->output_offset
= 0;
4202 if ((isection
->flags
& SEC_GROUP
) != 0)
4204 asymbol
*gsym
= group_signature (isection
);
4208 gsym
->flags
|= BSF_KEEP
;
4209 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
)
4210 elf_group_id (isection
) = gsym
;
4214 /* Allow the BFD backend to copy any private data it understands
4215 from the input section to the output section. */
4216 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
4217 err
= _("failed to copy private data");
4220 elf_section_type (osection
) = SHT_NOBITS
;
4227 bfd_nonfatal_message (NULL
, obfd
, osection
, err
);
4230 /* Return TRUE if input section ISECTION should be skipped. */
4233 skip_section (bfd
*ibfd
, sec_ptr isection
, bool skip_copy
)
4239 /* If we have already failed earlier on,
4240 do not keep on generating complaints now. */
4247 if (is_strip_section (ibfd
, isection
))
4250 if (is_update_section (ibfd
, isection
))
4253 /* When merging a note section we skip the copying of the contents,
4254 but not the copying of the relocs associated with the contents. */
4255 if (skip_copy
&& is_mergeable_note_section (ibfd
, isection
))
4258 flags
= bfd_section_flags (isection
);
4259 if ((flags
& SEC_GROUP
) != 0)
4262 osection
= isection
->output_section
;
4263 size
= bfd_section_size (isection
);
4265 if (size
== 0 || osection
== 0)
4271 /* Add section SECTION_PATTERN to the list of sections that will have their
4272 relocations removed. */
4275 handle_remove_relocations_option (const char *section_pattern
)
4277 find_section_list (section_pattern
, true, SECTION_CONTEXT_REMOVE_RELOCS
);
4280 /* Return TRUE if ISECTION from IBFD should have its relocations removed,
4281 otherwise return FALSE. If the user has requested that relocations be
4282 removed from a section that does not have relocations then this
4283 function will still return TRUE. */
4286 discard_relocations (bfd
*ibfd ATTRIBUTE_UNUSED
, asection
*isection
)
4288 return (find_section_list (bfd_section_name (isection
), false,
4289 SECTION_CONTEXT_REMOVE_RELOCS
) != NULL
);
4292 /* Wrapper for dealing with --remove-section (-R) command line arguments.
4293 A special case is detected here, if the user asks to remove a relocation
4294 section (one starting with ".rela" or ".rel") then this removal must
4295 be done using a different technique in a relocatable object. */
4298 handle_remove_section_option (const char *section_pattern
)
4300 find_section_list (section_pattern
, true, SECTION_CONTEXT_REMOVE
);
4301 if (startswith (section_pattern
, ".rel"))
4303 section_pattern
+= 4;
4304 if (*section_pattern
== 'a')
4306 if (*section_pattern
)
4307 handle_remove_relocations_option (section_pattern
);
4309 sections_removed
= true;
4312 /* Copy relocations in input section ISECTION of IBFD to an output
4313 section with the same name in OBFDARG. If stripping then don't
4314 copy any relocation info. */
4317 copy_relocations_in_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4319 bfd
*obfd
= (bfd
*) obfdarg
;
4325 if (skip_section (ibfd
, isection
, false))
4328 osection
= isection
->output_section
;
4330 /* Core files and DWO files do not need to be relocated. */
4331 if (bfd_get_format (obfd
) == bfd_core
4332 || strip_symbols
== STRIP_NONDWO
4333 || discard_relocations (ibfd
, isection
))
4337 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4341 /* Do not complain if the target does not support relocations. */
4342 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4347 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4354 bfd_set_reloc (obfd
, osection
, NULL
, 0);
4357 if (isection
->orelocation
!= NULL
)
4359 /* Some other function has already set up the output relocs
4360 for us, so scan those instead of the default relocs. */
4361 relcount
= isection
->reloc_count
;
4362 relpp
= isection
->orelocation
;
4366 relpp
= bfd_xalloc (obfd
, relsize
);
4367 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
4371 bfd_nonfatal_message (NULL
, ibfd
, isection
,
4372 _("relocation count is negative"));
4377 if (strip_symbols
== STRIP_ALL
)
4379 /* Remove relocations which are not in
4380 keep_strip_specific_list. */
4384 for (w_relpp
= relpp
, i
= 0; i
< relcount
; i
++)
4385 /* PR 17512: file: 9e907e0c. */
4386 if (relpp
[i
]->sym_ptr_ptr
4388 && *relpp
[i
]->sym_ptr_ptr
4389 && is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
4390 keep_specific_htab
))
4391 *w_relpp
++ = relpp
[i
];
4392 relcount
= w_relpp
- relpp
;
4396 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
4400 /* Copy the data of input section ISECTION of IBFD
4401 to an output section with the same name in OBFD. */
4404 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
4406 bfd
*obfd
= (bfd
*) obfdarg
;
4407 struct section_list
*p
;
4411 if (skip_section (ibfd
, isection
, true))
4414 osection
= isection
->output_section
;
4415 /* The output SHF_COMPRESSED section size is different from input if
4416 ELF classes of input and output aren't the same. We can't use
4417 the output section size since --interleave will shrink the output
4418 section. Size will be updated if the section is converted. */
4419 size
= bfd_section_size (isection
);
4421 if (bfd_section_flags (isection
) & SEC_HAS_CONTENTS
4422 && bfd_section_flags (osection
) & SEC_HAS_CONTENTS
)
4424 bfd_byte
*memhunk
= NULL
;
4426 if (!bfd_get_full_section_contents (ibfd
, isection
, &memhunk
)
4427 || !bfd_convert_section_contents (ibfd
, isection
, obfd
,
4430 bfd_set_section_size (osection
, 0);
4432 bfd_nonfatal_message (NULL
, ibfd
, isection
, NULL
);
4439 /* We don't handle leftover bytes (too many possible behaviors,
4440 and we don't know what the user wants). The section length
4441 must be a multiple of the number of bytes to swap. */
4442 if ((size
% reverse_bytes
) == 0)
4447 for (i
= 0; i
< size
; i
+= reverse_bytes
)
4448 for (j
= 0; j
< (unsigned long)(reverse_bytes
/ 2); j
++)
4450 bfd_byte
*m
= (bfd_byte
*) memhunk
;
4453 m
[i
+ j
] = m
[(i
+ reverse_bytes
) - (j
+ 1)];
4454 m
[(i
+ reverse_bytes
) - (j
+ 1)] = b
;
4458 /* User must pad the section up in order to do this. */
4459 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
4460 bfd_section_name (isection
), reverse_bytes
);
4465 /* Keep only every `copy_byte'th byte in MEMHUNK. */
4466 char *from
= (char *) memhunk
+ copy_byte
;
4467 char *to
= (char *) memhunk
;
4468 char *end
= (char *) memhunk
+ size
;
4471 /* If the section address is not exactly divisible by the interleave,
4472 then we must bias the from address. If the copy_byte is less than
4473 the bias, then we must skip forward one interleave, and increment
4475 int extra
= isection
->lma
% interleave
;
4477 if (copy_byte
< extra
)
4480 for (; from
< end
; from
+= interleave
)
4481 for (i
= 0; i
< copy_width
; i
++)
4483 if (&from
[i
] >= end
)
4488 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
* copy_width
;
4489 osection
->lma
/= interleave
;
4490 if (copy_byte
< extra
)
4494 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4497 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4503 else if ((p
= find_section_list (bfd_section_name (isection
),
4504 false, SECTION_CONTEXT_SET_FLAGS
)) != NULL
4505 && (p
->flags
& SEC_HAS_CONTENTS
) != 0)
4507 void *memhunk
= xmalloc (size
);
4509 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
4510 flag--they can just remove the section entirely and add it
4511 back again. However, we do permit them to turn on the
4512 SEC_HAS_CONTENTS flag, and take it to mean that the section
4513 contents should be zeroed out. */
4515 memset (memhunk
, 0, size
);
4516 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
4519 bfd_nonfatal_message (NULL
, obfd
, osection
, NULL
);
4527 /* Get all the sections. This is used when --gap-fill or --pad-to is
4531 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
4533 asection
***secppp
= (asection
***) secppparg
;
4535 **secppp
= osection
;
4539 /* Sort sections by LMA. This is called via qsort, and is used when
4540 --gap-fill or --pad-to is used. We force non loadable or empty
4541 sections to the front, where they are easier to ignore. */
4544 compare_section_lma (const void *arg1
, const void *arg2
)
4546 const asection
*sec1
= *(const asection
**) arg1
;
4547 const asection
*sec2
= *(const asection
**) arg2
;
4548 flagword flags1
, flags2
;
4550 /* Sort non loadable sections to the front. */
4551 flags1
= sec1
->flags
;
4552 flags2
= sec2
->flags
;
4553 if ((flags1
& SEC_HAS_CONTENTS
) == 0
4554 || (flags1
& SEC_LOAD
) == 0)
4556 if ((flags2
& SEC_HAS_CONTENTS
) != 0
4557 && (flags2
& SEC_LOAD
) != 0)
4562 if ((flags2
& SEC_HAS_CONTENTS
) == 0
4563 || (flags2
& SEC_LOAD
) == 0)
4567 /* Sort sections by LMA. */
4568 if (sec1
->lma
> sec2
->lma
)
4570 if (sec1
->lma
< sec2
->lma
)
4573 /* Sort sections with the same LMA by size. */
4574 if (bfd_section_size (sec1
) > bfd_section_size (sec2
))
4576 if (bfd_section_size (sec1
) < bfd_section_size (sec2
))
4579 if (sec1
->id
> sec2
->id
)
4581 if (sec1
->id
< sec2
->id
)
4586 /* Mark all the symbols which will be used in output relocations with
4587 the BSF_KEEP flag so that those symbols will not be stripped.
4589 Ignore relocations which will not appear in the output file. */
4592 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
4594 asymbol
**symbols
= (asymbol
**) symbolsarg
;
4599 /* Ignore an input section with no corresponding output section. */
4600 if (isection
->output_section
== NULL
)
4603 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
4606 /* Do not complain if the target does not support relocations. */
4607 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
4609 bfd_fatal (bfd_get_filename (ibfd
));
4615 relpp
= (arelent
**) xmalloc (relsize
);
4616 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
4618 bfd_fatal (bfd_get_filename (ibfd
));
4620 /* Examine each symbol used in a relocation. If it's not one of the
4621 special bfd section symbols, then mark it with BSF_KEEP. */
4622 for (i
= 0; i
< relcount
; i
++)
4624 /* See PRs 20923 and 20930 for reproducers for the NULL tests. */
4625 if (relpp
[i
]->sym_ptr_ptr
!= NULL
4626 && * relpp
[i
]->sym_ptr_ptr
!= NULL
4627 && *relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
4628 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
4629 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
4630 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
4636 /* Write out debugging information. */
4639 write_debugging_info (bfd
*obfd
, void *dhandle
,
4640 long *symcountp ATTRIBUTE_UNUSED
,
4641 asymbol
***symppp ATTRIBUTE_UNUSED
)
4643 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
4644 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
4646 bfd_byte
*syms
, *strings
= NULL
;
4647 bfd_size_type symsize
, stringsize
;
4648 asection
*stabsec
, *stabstrsec
;
4651 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
4656 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
4657 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
4658 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
4660 || stabstrsec
== NULL
4661 || !bfd_set_section_size (stabsec
, symsize
)
4662 || !bfd_set_section_size (stabstrsec
, stringsize
)
4663 || !bfd_set_section_alignment (stabsec
, 2)
4664 || !bfd_set_section_alignment (stabstrsec
, 0))
4666 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4667 _("can't create debugging section"));
4672 /* We can get away with setting the section contents now because
4673 the next thing the caller is going to do is copy over the
4674 real sections. We may someday have to split the contents
4675 setting out of this function. */
4676 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
4677 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
4680 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4681 _("can't set debugging section contents"));
4689 bfd_nonfatal_message (NULL
, obfd
, NULL
,
4690 _("don't know how to write debugging information for %s"),
4691 bfd_get_target (obfd
));
4695 /* If neither -D nor -U was specified explicitly,
4696 then use the configured default. */
4698 default_deterministic (void)
4700 if (deterministic
< 0)
4701 deterministic
= DEFAULT_AR_DETERMINISTIC
;
4705 strip_main (int argc
, char *argv
[])
4707 char *input_target
= NULL
;
4708 char *output_target
= NULL
;
4709 bool show_version
= false;
4710 bool formats_info
= false;
4713 char *output_file
= NULL
;
4714 bool merge_notes_set
= false;
4716 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU",
4717 strip_options
, (int *) 0)) != EOF
)
4722 input_target
= optarg
;
4725 output_target
= optarg
;
4728 input_target
= output_target
= optarg
;
4731 handle_remove_section_option (optarg
);
4733 case OPTION_KEEP_SECTION
:
4734 find_section_list (optarg
, true, SECTION_CONTEXT_KEEP
);
4736 case OPTION_REMOVE_RELOCS
:
4737 handle_remove_relocations_option (optarg
);
4740 strip_symbols
= STRIP_ALL
;
4744 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
4745 strip_symbols
= STRIP_DEBUG
;
4747 case OPTION_STRIP_DWO
:
4748 strip_symbols
= STRIP_DWO
;
4750 case OPTION_STRIP_UNNEEDED
:
4751 strip_symbols
= STRIP_UNNEEDED
;
4754 add_specific_symbol (optarg
, keep_specific_htab
);
4758 merge_notes_set
= true;
4760 case OPTION_NO_MERGE_NOTES
:
4761 merge_notes
= false;
4762 merge_notes_set
= true;
4765 add_specific_symbol (optarg
, strip_specific_htab
);
4768 output_file
= optarg
;
4771 preserve_dates
= true;
4774 deterministic
= true;
4777 deterministic
= false;
4780 discard_locals
= LOCALS_ALL
;
4783 discard_locals
= LOCALS_START_L
;
4789 show_version
= true;
4791 case OPTION_FORMATS_INFO
:
4792 formats_info
= true;
4794 case OPTION_ONLY_KEEP_DEBUG
:
4795 strip_symbols
= STRIP_NONDEBUG
;
4797 case OPTION_KEEP_FILE_SYMBOLS
:
4798 keep_file_symbols
= 1;
4800 case OPTION_KEEP_SECTION_SYMBOLS
:
4801 keep_section_symbols
= true;
4804 /* We've been given a long option. */
4811 strip_usage (stdout
, 0);
4813 strip_usage (stderr
, 1);
4817 /* If the user has not expressly chosen to merge/not-merge ELF notes
4818 then enable the merging unless we are stripping debug or dwo info. */
4819 if (! merge_notes_set
4820 && (strip_symbols
== STRIP_UNDEF
4821 || strip_symbols
== STRIP_ALL
4822 || strip_symbols
== STRIP_UNNEEDED
4823 || strip_symbols
== STRIP_NONDEBUG
4824 || strip_symbols
== STRIP_NONDWO
))
4834 print_version ("strip");
4836 default_deterministic ();
4838 /* Default is to strip all symbols. */
4839 if (strip_symbols
== STRIP_UNDEF
4840 && discard_locals
== LOCALS_UNDEF
4841 && htab_elements (strip_specific_htab
) == 0)
4842 strip_symbols
= STRIP_ALL
;
4844 if (output_target
== NULL
)
4845 output_target
= input_target
;
4849 || (output_file
!= NULL
&& (i
+ 1) < argc
))
4850 strip_usage (stderr
, 1);
4852 for (; i
< argc
; i
++)
4854 int hold_status
= status
;
4855 struct stat statbuf
;
4860 if (get_file_size (argv
[i
]) < 1)
4866 if (output_file
== NULL
4867 || filename_cmp (argv
[i
], output_file
) == 0)
4869 tmpname
= make_tempname (argv
[i
], &tmpfd
);
4871 copyfd
= dup (tmpfd
);
4874 tmpname
= output_file
;
4876 if (tmpname
== NULL
)
4878 bfd_nonfatal_message (argv
[i
], NULL
, NULL
,
4879 _("could not create temporary file to hold stripped copy"));
4885 copy_file (argv
[i
], tmpname
, tmpfd
, &statbuf
, input_target
,
4886 output_target
, NULL
);
4889 const char *oname
= output_file
? output_file
: argv
[i
];
4890 status
= smart_rename (tmpname
, oname
, copyfd
,
4891 &statbuf
, preserve_dates
) != 0;
4893 status
= hold_status
;
4899 unlink_if_ordinary (tmpname
);
4901 if (output_file
!= tmpname
)
4908 /* Set up PE subsystem. */
4911 set_pe_subsystem (const char *s
)
4913 const char *version
, *subsystem
;
4923 { "native", 0, IMAGE_SUBSYSTEM_NATIVE
},
4924 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI
},
4925 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI
},
4926 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI
},
4927 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI
},
4928 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION
},
4929 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
},
4930 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
},
4931 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
},
4932 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX
}
4938 /* Check for the presence of a version number. */
4939 version
= strchr (s
, ':');
4940 if (version
== NULL
)
4944 int len
= version
- s
;
4948 version
= copy
+ 1 + len
;
4949 pe_major_subsystem_version
= strtoul (version
, ©
, 0);
4951 pe_minor_subsystem_version
= strtoul (copy
+ 1, ©
, 0);
4953 non_fatal (_("%s: bad version in PE subsystem"), s
);
4956 /* Check for numeric subsystem. */
4957 value
= (short) strtol (subsystem
, ©
, 0);
4960 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
4961 if (v
[i
].value
== value
)
4963 pe_subsystem
= value
;
4964 set_def
= v
[i
].set_def
;
4970 /* Search for subsystem by name. */
4971 for (i
= 0; i
< ARRAY_SIZE (v
); i
++)
4972 if (strcmp (subsystem
, v
[i
].name
) == 0)
4974 pe_subsystem
= v
[i
].value
;
4975 set_def
= v
[i
].set_def
;
4983 fatal (_("unknown PE subsystem: %s"), s
);
4988 if (pe_file_alignment
== (bfd_vma
) -1)
4989 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
4990 if (pe_section_alignment
== (bfd_vma
) -1)
4991 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
4995 free ((char *) subsystem
);
4998 /* Convert EFI target to PEI target. */
5001 convert_efi_target (char **targ
)
5005 char *efi
= *targ
+ 4;
5008 if (startswith (efi
, "app-"))
5009 subsys
= IMAGE_SUBSYSTEM_EFI_APPLICATION
;
5010 else if (startswith (efi
, "bsdrv-"))
5012 subsys
= IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
5015 else if (startswith (efi
, "rtdrv-"))
5017 subsys
= IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
5024 pei
= xmalloc (len
+ sizeof ("-little"));
5025 memcpy (pei
, efi
, len
+ 1);
5030 if (strcmp (efi
+ 4, "ia32") == 0)
5032 /* Change ia32 to i386. */
5037 else if (strcmp (efi
+ 4, "x86_64") == 0)
5039 /* Change x86_64 to x86-64. */
5042 else if (strcmp (efi
+ 4, "aarch64") == 0)
5044 /* Change aarch64 to aarch64-little. */
5045 memcpy (pei
+ 4 + sizeof ("aarch64") - 1, "-little", sizeof ("-little"));
5051 /* Allocate and return a pointer to a struct section_add, initializing the
5052 structure using ARG, a string in the format "sectionname=filename".
5053 The returned structure will have its next pointer set to NEXT. The
5054 OPTION field is the name of the command line option currently being
5055 parsed, and is only used if an error needs to be reported. */
5057 static struct section_add
*
5058 init_section_add (const char *arg
,
5059 struct section_add
*next
,
5062 struct section_add
*pa
;
5065 s
= strchr (arg
, '=');
5067 fatal (_("bad format for %s"), option
);
5069 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
5070 pa
->name
= xstrndup (arg
, s
- arg
);
5071 pa
->filename
= s
+ 1;
5073 pa
->contents
= NULL
;
5079 /* Load the file specified in PA, allocating memory to hold the file
5080 contents, and store a pointer to the allocated memory in the contents
5081 field of PA. The size field of PA is also updated. All errors call
5085 section_add_load_file (struct section_add
*pa
)
5090 /* We don't use get_file_size so that we can do
5091 --add-section .note.GNU_stack=/dev/null
5092 get_file_size doesn't work on /dev/null. */
5094 f
= fopen (pa
->filename
, FOPEN_RB
);
5096 fatal (_("cannot open: %s: %s"),
5097 pa
->filename
, strerror (errno
));
5101 pa
->contents
= (bfd_byte
*) xmalloc (alloc
);
5109 pa
->contents
= (bfd_byte
*) xrealloc (pa
->contents
, alloc
);
5112 got
= fread (pa
->contents
+ off
, 1, alloc
- off
, f
);
5114 fatal (_("%s: fread failed"), pa
->filename
);
5125 copy_main (int argc
, char *argv
[])
5127 char *input_filename
= NULL
;
5128 char *output_filename
= NULL
;
5130 char *input_target
= NULL
;
5131 char *output_target
= NULL
;
5132 bool show_version
= false;
5133 bool change_warn
= true;
5134 bool formats_info
= false;
5135 bool use_globalize
= false;
5136 bool use_keep_global
= false;
5140 struct stat statbuf
;
5141 const bfd_arch_info_type
*input_arch
= NULL
;
5143 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
5144 copy_options
, (int *) 0)) != EOF
)
5149 copy_byte
= atoi (optarg
);
5151 fatal (_("byte number must be non-negative"));
5155 input_arch
= bfd_scan_arch (optarg
);
5156 if (input_arch
== NULL
)
5157 fatal (_("architecture %s unknown"), optarg
);
5163 interleave
= atoi (optarg
);
5165 fatal (_("interleave must be positive"));
5171 case OPTION_INTERLEAVE_WIDTH
:
5172 copy_width
= atoi (optarg
);
5174 fatal(_("interleave width must be positive"));
5178 case 's': /* "source" - 'I' is preferred */
5179 input_target
= optarg
;
5183 case 'd': /* "destination" - 'O' is preferred */
5184 output_target
= optarg
;
5188 input_target
= output_target
= optarg
;
5192 find_section_list (optarg
, true, SECTION_CONTEXT_COPY
);
5193 sections_copied
= true;
5197 handle_remove_section_option (optarg
);
5200 case OPTION_KEEP_SECTION
:
5201 find_section_list (optarg
, true, SECTION_CONTEXT_KEEP
);
5204 case OPTION_REMOVE_RELOCS
:
5205 handle_remove_relocations_option (optarg
);
5209 strip_symbols
= STRIP_ALL
;
5213 strip_symbols
= STRIP_DEBUG
;
5216 case OPTION_STRIP_DWO
:
5217 strip_symbols
= STRIP_DWO
;
5220 case OPTION_STRIP_UNNEEDED
:
5221 strip_symbols
= STRIP_UNNEEDED
;
5224 case OPTION_ONLY_KEEP_DEBUG
:
5225 strip_symbols
= STRIP_NONDEBUG
;
5228 case OPTION_KEEP_FILE_SYMBOLS
:
5229 keep_file_symbols
= 1;
5232 case OPTION_ADD_GNU_DEBUGLINK
:
5233 long_section_names
= ENABLE
;
5234 gnu_debuglink_filename
= optarg
;
5238 add_specific_symbol (optarg
, keep_specific_htab
);
5244 case OPTION_NO_MERGE_NOTES
:
5245 merge_notes
= false;
5249 add_specific_symbol (optarg
, strip_specific_htab
);
5252 case OPTION_STRIP_UNNEEDED_SYMBOL
:
5253 add_specific_symbol (optarg
, strip_unneeded_htab
);
5257 add_specific_symbol (optarg
, localize_specific_htab
);
5260 case OPTION_GLOBALIZE_SYMBOL
:
5261 use_globalize
= true;
5262 add_specific_symbol (optarg
, globalize_specific_htab
);
5266 use_keep_global
= true;
5267 add_specific_symbol (optarg
, keepglobal_specific_htab
);
5271 add_specific_symbol (optarg
, weaken_specific_htab
);
5275 preserve_dates
= true;
5279 deterministic
= true;
5283 deterministic
= false;
5291 discard_locals
= LOCALS_ALL
;
5295 discard_locals
= LOCALS_START_L
;
5303 show_version
= true;
5306 case OPTION_FORMATS_INFO
:
5307 formats_info
= true;
5314 case OPTION_ADD_SECTION
:
5315 add_sections
= init_section_add (optarg
, add_sections
,
5317 section_add_load_file (add_sections
);
5320 case OPTION_UPDATE_SECTION
:
5321 update_sections
= init_section_add (optarg
, update_sections
,
5322 "--update-section");
5323 section_add_load_file (update_sections
);
5326 case OPTION_DUMP_SECTION
:
5327 dump_sections
= init_section_add (optarg
, dump_sections
,
5331 case OPTION_ADD_SYMBOL
:
5334 struct addsym_node
*newsym
= xmalloc (sizeof *newsym
);
5336 newsym
->next
= NULL
;
5337 s
= strchr (optarg
, '=');
5339 fatal (_("bad format for %s"), "--add-symbol");
5340 t
= strchr (s
+ 1, ':');
5342 newsym
->symdef
= xstrndup (optarg
, s
- optarg
);
5345 newsym
->section
= xstrndup (s
+ 1, t
- (s
+ 1));
5346 newsym
->symval
= strtol (t
+ 1, NULL
, 0);
5350 newsym
->section
= NULL
;
5351 newsym
->symval
= strtol (s
+ 1, NULL
, 0);
5355 t
= strchr (t
+ 1, ',');
5356 newsym
->othersym
= NULL
;
5358 newsym
->flags
= parse_symflags (t
+1, &newsym
->othersym
);
5360 newsym
->flags
= BSF_GLOBAL
;
5362 /* Keep 'othersym' symbols at the front of the list. */
5363 if (newsym
->othersym
)
5365 newsym
->next
= add_sym_list
;
5367 add_sym_tail
= &newsym
->next
;
5368 add_sym_list
= newsym
;
5372 *add_sym_tail
= newsym
;
5373 add_sym_tail
= &newsym
->next
;
5379 case OPTION_CHANGE_START
:
5380 change_start
= parse_vma (optarg
, "--change-start");
5383 case OPTION_CHANGE_SECTION_ADDRESS
:
5384 case OPTION_CHANGE_SECTION_LMA
:
5385 case OPTION_CHANGE_SECTION_VMA
:
5387 struct section_list
* p
;
5388 unsigned int context
= 0;
5392 char *option
= NULL
;
5397 case OPTION_CHANGE_SECTION_ADDRESS
:
5398 option
= "--change-section-address";
5399 context
= SECTION_CONTEXT_ALTER_LMA
| SECTION_CONTEXT_ALTER_VMA
;
5401 case OPTION_CHANGE_SECTION_LMA
:
5402 option
= "--change-section-lma";
5403 context
= SECTION_CONTEXT_ALTER_LMA
;
5405 case OPTION_CHANGE_SECTION_VMA
:
5406 option
= "--change-section-vma";
5407 context
= SECTION_CONTEXT_ALTER_VMA
;
5411 s
= strchr (optarg
, '=');
5414 s
= strchr (optarg
, '+');
5417 s
= strchr (optarg
, '-');
5419 fatal (_("bad format for %s"), option
);
5424 /* Correct the context. */
5427 case OPTION_CHANGE_SECTION_ADDRESS
:
5428 context
= SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_SET_VMA
;
5430 case OPTION_CHANGE_SECTION_LMA
:
5431 context
= SECTION_CONTEXT_SET_LMA
;
5433 case OPTION_CHANGE_SECTION_VMA
:
5434 context
= SECTION_CONTEXT_SET_VMA
;
5440 name
= (char *) xmalloc (len
+ 1);
5441 strncpy (name
, optarg
, len
);
5444 p
= find_section_list (name
, true, context
);
5446 val
= parse_vma (s
+ 1, option
);
5452 case OPTION_CHANGE_SECTION_ADDRESS
:
5456 case OPTION_CHANGE_SECTION_LMA
:
5460 case OPTION_CHANGE_SECTION_VMA
:
5467 case OPTION_CHANGE_ADDRESSES
:
5468 change_section_address
= parse_vma (optarg
, "--change-addresses");
5469 change_start
= change_section_address
;
5472 case OPTION_CHANGE_WARNINGS
:
5476 case OPTION_CHANGE_LEADING_CHAR
:
5477 change_leading_char
= true;
5480 case OPTION_COMPRESS_DEBUG_SECTIONS
:
5483 if (strcasecmp (optarg
, "none") == 0)
5484 do_debug_sections
= decompress
;
5485 else if (strcasecmp (optarg
, "zlib") == 0)
5486 do_debug_sections
= compress_zlib
;
5487 else if (strcasecmp (optarg
, "zlib-gnu") == 0)
5488 do_debug_sections
= compress_gnu_zlib
;
5489 else if (strcasecmp (optarg
, "zlib-gabi") == 0)
5490 do_debug_sections
= compress_gabi_zlib
;
5491 else if (strcasecmp (optarg
, "zstd") == 0)
5492 do_debug_sections
= compress_zstd
;
5494 fatal (_("unrecognized --compress-debug-sections type `%s'"),
5498 do_debug_sections
= compress
;
5501 case OPTION_DEBUGGING
:
5502 convert_debugging
= true;
5505 case OPTION_DECOMPRESS_DEBUG_SECTIONS
:
5506 do_debug_sections
= decompress
;
5509 case OPTION_ELF_STT_COMMON
:
5510 if (strcasecmp (optarg
, "yes") == 0)
5511 do_elf_stt_common
= elf_stt_common
;
5512 else if (strcasecmp (optarg
, "no") == 0)
5513 do_elf_stt_common
= no_elf_stt_common
;
5515 fatal (_("unrecognized --elf-stt-common= option `%s'"),
5519 case OPTION_GAP_FILL
:
5521 bfd_vma gap_fill_vma
;
5523 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
5524 gap_fill
= (bfd_byte
) gap_fill_vma
;
5525 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
5526 non_fatal (_("Warning: truncating gap-fill from 0x%" PRIx64
5528 (uint64_t) gap_fill_vma
, gap_fill
);
5529 gap_fill_set
= true;
5533 case OPTION_NO_CHANGE_WARNINGS
:
5534 change_warn
= false;
5538 pad_to
= parse_vma (optarg
, "--pad-to");
5542 case OPTION_REMOVE_LEADING_CHAR
:
5543 remove_leading_char
= true;
5546 case OPTION_REDEFINE_SYM
:
5548 /* Insert this redefinition onto redefine_specific_htab. */
5552 const char *nextarg
;
5553 char *source
, *target
;
5555 s
= strchr (optarg
, '=');
5557 fatal (_("bad format for %s"), "--redefine-sym");
5560 source
= (char *) xmalloc (len
+ 1);
5561 strncpy (source
, optarg
, len
);
5565 len
= strlen (nextarg
);
5566 target
= (char *) xmalloc (len
+ 1);
5567 strcpy (target
, nextarg
);
5569 add_redefine_and_check ("--redefine-sym", source
, target
);
5576 case OPTION_REDEFINE_SYMS
:
5577 add_redefine_syms_file (optarg
);
5580 case OPTION_SET_SECTION_FLAGS
:
5582 struct section_list
*p
;
5587 s
= strchr (optarg
, '=');
5589 fatal (_("bad format for %s"), "--set-section-flags");
5592 name
= (char *) xmalloc (len
+ 1);
5593 strncpy (name
, optarg
, len
);
5596 p
= find_section_list (name
, true, SECTION_CONTEXT_SET_FLAGS
);
5598 p
->flags
= parse_flags (s
+ 1);
5602 case OPTION_SET_SECTION_ALIGNMENT
:
5604 struct section_list
*p
;
5610 s
= strchr (optarg
, '=');
5612 fatal (_("bad format for --set-section-alignment: argument needed"));
5614 align
= atoi (s
+ 1);
5616 fatal (_("bad format for --set-section-alignment: numeric argument needed"));
5618 /* Convert integer alignment into a power-of-two alignment. */
5620 while ((align
& 1) == 0)
5627 /* Number has more than on 1, i.e. wasn't a power of 2. */
5628 fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
5630 /* Add the alignment setting to the section list. */
5632 name
= (char *) xmalloc (len
+ 1);
5633 strncpy (name
, optarg
, len
);
5636 p
= find_section_list (name
, true, SECTION_CONTEXT_SET_ALIGNMENT
);
5638 p
->alignment
= palign
;
5642 case OPTION_RENAME_SECTION
:
5645 const char *eq
, *fl
;
5650 eq
= strchr (optarg
, '=');
5652 fatal (_("bad format for %s"), "--rename-section");
5656 fatal (_("bad format for %s"), "--rename-section");
5658 old_name
= (char *) xmalloc (len
+ 1);
5659 strncpy (old_name
, optarg
, len
);
5663 fl
= strchr (eq
, ',');
5666 flags
= parse_flags (fl
+ 1);
5676 fatal (_("bad format for %s"), "--rename-section");
5678 new_name
= (char *) xmalloc (len
+ 1);
5679 strncpy (new_name
, eq
, len
);
5682 add_section_rename (old_name
, new_name
, flags
);
5686 case OPTION_SET_START
:
5687 set_start
= parse_vma (optarg
, "--set-start");
5688 set_start_set
= true;
5691 case OPTION_SREC_LEN
:
5692 _bfd_srec_len
= parse_vma (optarg
, "--srec-len");
5695 case OPTION_SREC_FORCES3
:
5696 _bfd_srec_forceS3
= true;
5699 case OPTION_STRIP_SYMBOLS
:
5700 add_specific_symbols (optarg
, strip_specific_htab
,
5701 &strip_specific_buffer
);
5704 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
5705 add_specific_symbols (optarg
, strip_unneeded_htab
,
5706 &strip_unneeded_buffer
);
5709 case OPTION_KEEP_SYMBOLS
:
5710 add_specific_symbols (optarg
, keep_specific_htab
,
5711 &keep_specific_buffer
);
5714 case OPTION_KEEP_SECTION_SYMBOLS
:
5715 keep_section_symbols
= true;
5718 case OPTION_LOCALIZE_HIDDEN
:
5719 localize_hidden
= true;
5722 case OPTION_LOCALIZE_SYMBOLS
:
5723 add_specific_symbols (optarg
, localize_specific_htab
,
5724 &localize_specific_buffer
);
5727 case OPTION_LONG_SECTION_NAMES
:
5728 if (!strcmp ("enable", optarg
))
5729 long_section_names
= ENABLE
;
5730 else if (!strcmp ("disable", optarg
))
5731 long_section_names
= DISABLE
;
5732 else if (!strcmp ("keep", optarg
))
5733 long_section_names
= KEEP
;
5735 fatal (_("unknown long section names option '%s'"), optarg
);
5738 case OPTION_GLOBALIZE_SYMBOLS
:
5739 use_globalize
= true;
5740 add_specific_symbols (optarg
, globalize_specific_htab
,
5741 &globalize_specific_buffer
);
5744 case OPTION_KEEPGLOBAL_SYMBOLS
:
5745 use_keep_global
= true;
5746 add_specific_symbols (optarg
, keepglobal_specific_htab
,
5747 &keepglobal_specific_buffer
);
5750 case OPTION_WEAKEN_SYMBOLS
:
5751 add_specific_symbols (optarg
, weaken_specific_htab
,
5752 &weaken_specific_buffer
);
5755 case OPTION_ALT_MACH_CODE
:
5756 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
5757 if (use_alt_mach_code
== 0)
5758 fatal (_("unable to parse alternative machine code"));
5761 case OPTION_PREFIX_SYMBOLS
:
5762 prefix_symbols_string
= optarg
;
5765 case OPTION_PREFIX_SECTIONS
:
5766 prefix_sections_string
= optarg
;
5769 case OPTION_PREFIX_ALLOC_SECTIONS
:
5770 prefix_alloc_sections_string
= optarg
;
5773 case OPTION_READONLY_TEXT
:
5774 bfd_flags_to_set
|= WP_TEXT
;
5775 bfd_flags_to_clear
&= ~WP_TEXT
;
5778 case OPTION_WRITABLE_TEXT
:
5779 bfd_flags_to_clear
|= WP_TEXT
;
5780 bfd_flags_to_set
&= ~WP_TEXT
;
5784 bfd_flags_to_set
|= D_PAGED
;
5785 bfd_flags_to_clear
&= ~D_PAGED
;
5789 bfd_flags_to_clear
|= D_PAGED
;
5790 bfd_flags_to_set
&= ~D_PAGED
;
5793 case OPTION_EXTRACT_DWO
:
5794 strip_symbols
= STRIP_NONDWO
;
5797 case OPTION_EXTRACT_SYMBOL
:
5798 extract_symbol
= true;
5801 case OPTION_REVERSE_BYTES
:
5803 int prev
= reverse_bytes
;
5805 reverse_bytes
= atoi (optarg
);
5806 if ((reverse_bytes
<= 0) || ((reverse_bytes
% 2) != 0))
5807 fatal (_("number of bytes to reverse must be positive and even"));
5809 if (prev
&& prev
!= reverse_bytes
)
5810 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
5815 case OPTION_FILE_ALIGNMENT
:
5816 pe_file_alignment
= parse_vma (optarg
, "--file-alignment");
5822 pe_heap_reserve
= strtoul (optarg
, &end
, 0);
5824 || (*end
!= '.' && *end
!= '\0'))
5825 non_fatal (_("%s: invalid reserve value for --heap"),
5827 else if (*end
!= '\0')
5829 pe_heap_commit
= strtoul (end
+ 1, &end
, 0);
5831 non_fatal (_("%s: invalid commit value for --heap"),
5837 case OPTION_IMAGE_BASE
:
5838 pe_image_base
= parse_vma (optarg
, "--image-base");
5841 case OPTION_PE_SECTION_ALIGNMENT
:
5842 pe_section_alignment
= parse_vma (optarg
,
5843 "--section-alignment");
5846 case OPTION_SUBSYSTEM
:
5847 set_pe_subsystem (optarg
);
5853 pe_stack_reserve
= strtoul (optarg
, &end
, 0);
5855 || (*end
!= '.' && *end
!= '\0'))
5856 non_fatal (_("%s: invalid reserve value for --stack"),
5858 else if (*end
!= '\0')
5860 pe_stack_commit
= strtoul (end
+ 1, &end
, 0);
5862 non_fatal (_("%s: invalid commit value for --stack"),
5868 case OPTION_VERILOG_DATA_WIDTH
:
5869 VerilogDataWidth
= parse_vma (optarg
, "--verilog-data-width");
5870 switch (VerilogDataWidth
)
5876 case 16: /* We do not support widths > 16 because the verilog
5877 data is handled internally in 16 byte wide packets. */
5880 fatal (_("error: verilog data width must be 1, 2, 4, 8 or 16"));
5885 /* We've been given a long option. */
5890 copy_usage (stdout
, 0);
5893 copy_usage (stderr
, 1);
5897 if (use_globalize
&& use_keep_global
)
5898 fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)"));
5907 print_version ("objcopy");
5909 if (interleave
&& copy_byte
== -1)
5910 fatal (_("interleave start byte must be set with --byte"));
5912 if (copy_byte
>= interleave
)
5913 fatal (_("byte number must be less than interleave"));
5915 if (copy_width
> interleave
- copy_byte
)
5916 fatal (_("interleave width must be less than or equal to interleave - byte`"));
5918 if (optind
== argc
|| optind
+ 2 < argc
)
5919 copy_usage (stderr
, 1);
5921 input_filename
= argv
[optind
];
5922 if (optind
+ 1 < argc
)
5923 output_filename
= argv
[optind
+ 1];
5925 default_deterministic ();
5927 /* Default is to strip no symbols. */
5928 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
5929 strip_symbols
= STRIP_NONE
;
5931 if (output_target
== NULL
)
5932 output_target
= input_target
;
5934 /* Convert input EFI target to PEI target. */
5935 if (input_target
!= NULL
5936 && startswith (input_target
, "efi-"))
5938 if (convert_efi_target (&input_target
) < 0)
5939 fatal (_("unknown input EFI target: %s"), input_target
);
5942 /* Convert output EFI target to PEI target. */
5943 if (output_target
!= NULL
5944 && startswith (output_target
, "efi-"))
5946 int subsys
= convert_efi_target (&output_target
);
5949 fatal (_("unknown output EFI target: %s"), output_target
);
5950 if (pe_subsystem
== -1)
5951 pe_subsystem
= subsys
;
5952 if (pe_file_alignment
== (bfd_vma
) -1)
5953 pe_file_alignment
= PE_DEF_FILE_ALIGNMENT
;
5954 if (pe_section_alignment
== (bfd_vma
) -1)
5955 pe_section_alignment
= PE_DEF_SECTION_ALIGNMENT
;
5958 /* If there is no destination file, or the source and destination files
5959 are the same, then create a temp and copy the result into the input. */
5961 if (output_filename
== NULL
5962 || filename_cmp (input_filename
, output_filename
) == 0)
5964 tmpname
= make_tempname (input_filename
, &tmpfd
);
5966 copyfd
= dup (tmpfd
);
5969 tmpname
= output_filename
;
5971 if (tmpname
== NULL
)
5973 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
5974 input_filename
, strerror (errno
));
5977 copy_file (input_filename
, tmpname
, tmpfd
, &statbuf
, input_target
,
5978 output_target
, input_arch
);
5981 const char *oname
= output_filename
? output_filename
: input_filename
;
5982 status
= smart_rename (tmpname
, oname
, copyfd
,
5983 &statbuf
, preserve_dates
) != 0;
5989 unlink_if_ordinary (tmpname
);
5992 if (tmpname
!= output_filename
)
5997 struct section_list
*p
;
5999 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
6003 if (p
->context
& (SECTION_CONTEXT_SET_VMA
| SECTION_CONTEXT_ALTER_VMA
))
6004 /* xgettext:c-format */
6005 non_fatal (_("%s %s%c0x%" PRIx64
" never used"),
6006 "--change-section-vma",
6008 p
->context
& SECTION_CONTEXT_SET_VMA
? '=' : '+',
6009 (uint64_t) p
->vma_val
);
6011 if (p
->context
& (SECTION_CONTEXT_SET_LMA
| SECTION_CONTEXT_ALTER_LMA
))
6012 /* xgettext:c-format */
6013 non_fatal (_("%s %s%c0x%" PRIx64
" never used"),
6014 "--change-section-lma",
6016 p
->context
& SECTION_CONTEXT_SET_LMA
? '=' : '+',
6017 (uint64_t) p
->lma_val
);
6022 free (strip_specific_buffer
);
6023 free (strip_unneeded_buffer
);
6024 free (keep_specific_buffer
);
6025 free (localize_specific_buffer
);
6026 free (globalize_specific_buffer
);
6027 free (keepglobal_specific_buffer
);
6028 free (weaken_specific_buffer
);
6034 main (int argc
, char *argv
[])
6036 #ifdef HAVE_LC_MESSAGES
6037 setlocale (LC_MESSAGES
, "");
6039 setlocale (LC_CTYPE
, "");
6040 bindtextdomain (PACKAGE
, LOCALEDIR
);
6041 textdomain (PACKAGE
);
6043 program_name
= argv
[0];
6044 xmalloc_set_program_name (program_name
);
6046 START_PROGRESS (program_name
, 0);
6048 expandargv (&argc
, &argv
);
6050 strip_symbols
= STRIP_UNDEF
;
6051 discard_locals
= LOCALS_UNDEF
;
6053 if (bfd_init () != BFD_INIT_MAGIC
)
6054 fatal (_("fatal error: libbfd ABI mismatch"));
6055 set_default_bfd_target ();
6059 int i
= strlen (program_name
);
6060 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
6061 /* Drop the .exe suffix, if any. */
6062 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
6065 program_name
[i
] = '\0';
6068 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
6071 create_symbol_htabs ();
6072 xatexit (delete_symbol_htabs
);
6075 bfd_set_error_program_name (argv
[0]);
6078 strip_main (argc
, argv
);
6080 copy_main (argc
, argv
);
6082 END_PROGRESS (program_name
);