Add an option to objcopy to change the alignment of sections.
[deliverable/binutils-gdb.git] / binutils / objcopy.c
1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
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.
10
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.
15
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
19 02110-1301, USA. */
20 \f
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "progress.h"
24 #include "getopt.h"
25 #include "libiberty.h"
26 #include "bucomm.h"
27 #include "budbg.h"
28 #include "filenames.h"
29 #include "fnmatch.h"
30 #include "elf-bfd.h"
31 #include "coff/internal.h"
32 #include "libcoff.h"
33 #include "safe-ctype.h"
34
35 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
36 header in generic PE code. */
37 #include "coff/i386.h"
38 #include "coff/pe.h"
39
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;
50
51 struct is_specified_symbol_predicate_data
52 {
53 const char * name;
54 bfd_boolean found;
55 };
56
57 /* A node includes symbol name mapping to support redefine_sym. */
58 struct redefine_node
59 {
60 char *source;
61 char *target;
62 };
63
64 struct addsym_node
65 {
66 struct addsym_node *next;
67 char * symdef;
68 long symval;
69 flagword flags;
70 char * section;
71 char * othersym;
72 };
73
74 typedef struct section_rename
75 {
76 const char * old_name;
77 const char * new_name;
78 flagword flags;
79 struct section_rename * next;
80 }
81 section_rename;
82
83 /* List of sections to be renamed. */
84 static section_rename *section_rename_list;
85
86 static asymbol **isympp = NULL; /* Input symbols. */
87 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
88
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;
93
94 static bfd_boolean verbose; /* Print file and target names. */
95 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
96 static int deterministic = -1; /* Enable deterministic archives. */
97 static int status = 0; /* Exit status. */
98
99 static bfd_boolean merge_notes = FALSE; /* Merge note sections. */
100 static bfd_byte * merged_notes = NULL; /* Contents on note section undergoing a merge. */
101 static bfd_size_type merged_size = 0; /* New, smaller size of the merged note section. */
102
103 enum strip_action
104 {
105 STRIP_UNDEF,
106 STRIP_NONE, /* Don't strip. */
107 STRIP_DEBUG, /* Strip all debugger symbols. */
108 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
109 STRIP_NONDEBUG, /* Strip everything but debug info. */
110 STRIP_DWO, /* Strip all DWO info. */
111 STRIP_NONDWO, /* Strip everything but DWO info. */
112 STRIP_ALL /* Strip all symbols. */
113 };
114
115 /* Which symbols to remove. */
116 static enum strip_action strip_symbols = STRIP_UNDEF;
117
118 enum locals_action
119 {
120 LOCALS_UNDEF,
121 LOCALS_START_L, /* Discard locals starting with L. */
122 LOCALS_ALL /* Discard all locals. */
123 };
124
125 /* Which local symbols to remove. Overrides STRIP_ALL. */
126 static enum locals_action discard_locals;
127
128 /* Structure used to hold lists of sections and actions to take. */
129 struct section_list
130 {
131 struct section_list * next; /* Next section to change. */
132 const char * pattern; /* Section name pattern. */
133 bfd_boolean used; /* Whether this entry was used. */
134
135 unsigned int context; /* What to do with matching sections. */
136 /* Flag bits used in the context field.
137 COPY and REMOVE are mutually exlusive. SET and ALTER are mutually exclusive. */
138 #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
139 #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
140 #define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */
141 #define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */
142 #define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */
143 #define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */
144 #define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */
145 #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 7) /* Remove relocations for this section. */
146 #define SECTION_CONTEXT_SET_ALIGNMENT (1 << 8) /* Set alignment for section. */
147
148 bfd_vma vma_val; /* Amount to change by or set to. */
149 bfd_vma lma_val; /* Amount to change by or set to. */
150 flagword flags; /* What to set the section flags to. */
151 unsigned int alignment; /* Alignment of output section. */
152 };
153
154 static struct section_list *change_sections;
155
156 /* TRUE if some sections are to be removed. */
157 static bfd_boolean sections_removed;
158
159 /* TRUE if only some sections are to be copied. */
160 static bfd_boolean sections_copied;
161
162 /* Changes to the start address. */
163 static bfd_vma change_start = 0;
164 static bfd_boolean set_start_set = FALSE;
165 static bfd_vma set_start;
166
167 /* Changes to section addresses. */
168 static bfd_vma change_section_address = 0;
169
170 /* Filling gaps between sections. */
171 static bfd_boolean gap_fill_set = FALSE;
172 static bfd_byte gap_fill = 0;
173
174 /* Pad to a given address. */
175 static bfd_boolean pad_to_set = FALSE;
176 static bfd_vma pad_to;
177
178 /* Use alternative machine code? */
179 static unsigned long use_alt_mach_code = 0;
180
181 /* Output BFD flags user wants to set or clear */
182 static flagword bfd_flags_to_set;
183 static flagword bfd_flags_to_clear;
184
185 /* List of sections to add. */
186 struct section_add
187 {
188 /* Next section to add. */
189 struct section_add *next;
190 /* Name of section to add. */
191 const char *name;
192 /* Name of file holding section contents. */
193 const char *filename;
194 /* Size of file. */
195 size_t size;
196 /* Contents of file. */
197 bfd_byte *contents;
198 /* BFD section, after it has been added. */
199 asection *section;
200 };
201
202 /* List of sections to add to the output BFD. */
203 static struct section_add *add_sections;
204
205 /* List of sections to update in the output BFD. */
206 static struct section_add *update_sections;
207
208 /* List of sections to dump from the output BFD. */
209 static struct section_add *dump_sections;
210
211 /* If non-NULL the argument to --add-gnu-debuglink.
212 This should be the filename to store in the .gnu_debuglink section. */
213 static const char * gnu_debuglink_filename = NULL;
214
215 /* Whether to convert debugging information. */
216 static bfd_boolean convert_debugging = FALSE;
217
218 /* Whether to compress/decompress DWARF debug sections. */
219 static enum
220 {
221 nothing = 0,
222 compress = 1 << 0,
223 compress_zlib = compress | 1 << 1,
224 compress_gnu_zlib = compress | 1 << 2,
225 compress_gabi_zlib = compress | 1 << 3,
226 decompress = 1 << 4
227 } do_debug_sections = nothing;
228
229 /* Whether to generate ELF common symbols with the STT_COMMON type. */
230 static enum bfd_link_elf_stt_common do_elf_stt_common = unchanged;
231
232 /* Whether to change the leading character in symbol names. */
233 static bfd_boolean change_leading_char = FALSE;
234
235 /* Whether to remove the leading character from global symbol names. */
236 static bfd_boolean remove_leading_char = FALSE;
237
238 /* Whether to permit wildcard in symbol comparison. */
239 static bfd_boolean wildcard = FALSE;
240
241 /* True if --localize-hidden is in effect. */
242 static bfd_boolean localize_hidden = FALSE;
243
244 /* List of symbols to strip, keep, localize, keep-global, weaken,
245 or redefine. */
246 static htab_t strip_specific_htab = NULL;
247 static htab_t strip_unneeded_htab = NULL;
248 static htab_t keep_specific_htab = NULL;
249 static htab_t localize_specific_htab = NULL;
250 static htab_t globalize_specific_htab = NULL;
251 static htab_t keepglobal_specific_htab = NULL;
252 static htab_t weaken_specific_htab = NULL;
253 static htab_t redefine_specific_htab = NULL;
254 static htab_t redefine_specific_reverse_htab = NULL;
255 static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
256 static int add_symbols = 0;
257
258 static char *strip_specific_buffer = NULL;
259 static char *strip_unneeded_buffer = NULL;
260 static char *keep_specific_buffer = NULL;
261 static char *localize_specific_buffer = NULL;
262 static char *globalize_specific_buffer = NULL;
263 static char *keepglobal_specific_buffer = NULL;
264 static char *weaken_specific_buffer = NULL;
265
266 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
267 static bfd_boolean weaken = FALSE;
268
269 /* If this is TRUE, we retain BSF_FILE symbols. */
270 static bfd_boolean keep_file_symbols = FALSE;
271
272 /* Prefix symbols/sections. */
273 static char *prefix_symbols_string = 0;
274 static char *prefix_sections_string = 0;
275 static char *prefix_alloc_sections_string = 0;
276
277 /* True if --extract-symbol was passed on the command line. */
278 static bfd_boolean extract_symbol = FALSE;
279
280 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
281 of <reverse_bytes> bytes within each output section. */
282 static int reverse_bytes = 0;
283
284 /* For Coff objects, we may want to allow or disallow long section names,
285 or preserve them where found in the inputs. Debug info relies on them. */
286 enum long_section_name_handling
287 {
288 DISABLE,
289 ENABLE,
290 KEEP
291 };
292
293 /* The default long section handling mode is to preserve them.
294 This is also the only behaviour for 'strip'. */
295 static enum long_section_name_handling long_section_names = KEEP;
296
297 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
298 enum command_line_switch
299 {
300 OPTION_ADD_SECTION=150,
301 OPTION_ADD_GNU_DEBUGLINK,
302 OPTION_ADD_SYMBOL,
303 OPTION_ALT_MACH_CODE,
304 OPTION_CHANGE_ADDRESSES,
305 OPTION_CHANGE_LEADING_CHAR,
306 OPTION_CHANGE_SECTION_ADDRESS,
307 OPTION_CHANGE_SECTION_LMA,
308 OPTION_CHANGE_SECTION_VMA,
309 OPTION_CHANGE_START,
310 OPTION_CHANGE_WARNINGS,
311 OPTION_COMPRESS_DEBUG_SECTIONS,
312 OPTION_DEBUGGING,
313 OPTION_DECOMPRESS_DEBUG_SECTIONS,
314 OPTION_DUMP_SECTION,
315 OPTION_ELF_STT_COMMON,
316 OPTION_EXTRACT_DWO,
317 OPTION_EXTRACT_SYMBOL,
318 OPTION_FILE_ALIGNMENT,
319 OPTION_FORMATS_INFO,
320 OPTION_GAP_FILL,
321 OPTION_GLOBALIZE_SYMBOL,
322 OPTION_GLOBALIZE_SYMBOLS,
323 OPTION_HEAP,
324 OPTION_IMAGE_BASE,
325 OPTION_IMPURE,
326 OPTION_INTERLEAVE_WIDTH,
327 OPTION_KEEPGLOBAL_SYMBOLS,
328 OPTION_KEEP_FILE_SYMBOLS,
329 OPTION_KEEP_SYMBOLS,
330 OPTION_LOCALIZE_HIDDEN,
331 OPTION_LOCALIZE_SYMBOLS,
332 OPTION_LONG_SECTION_NAMES,
333 OPTION_MERGE_NOTES,
334 OPTION_NO_MERGE_NOTES,
335 OPTION_NO_CHANGE_WARNINGS,
336 OPTION_ONLY_KEEP_DEBUG,
337 OPTION_PAD_TO,
338 OPTION_PREFIX_ALLOC_SECTIONS,
339 OPTION_PREFIX_SECTIONS,
340 OPTION_PREFIX_SYMBOLS,
341 OPTION_PURE,
342 OPTION_READONLY_TEXT,
343 OPTION_REDEFINE_SYM,
344 OPTION_REDEFINE_SYMS,
345 OPTION_REMOVE_LEADING_CHAR,
346 OPTION_REMOVE_RELOCS,
347 OPTION_RENAME_SECTION,
348 OPTION_REVERSE_BYTES,
349 OPTION_PE_SECTION_ALIGNMENT,
350 OPTION_SET_SECTION_FLAGS,
351 OPTION_SET_SECTION_ALIGNMENT,
352 OPTION_SET_START,
353 OPTION_SREC_FORCES3,
354 OPTION_SREC_LEN,
355 OPTION_STACK,
356 OPTION_STRIP_DWO,
357 OPTION_STRIP_SYMBOLS,
358 OPTION_STRIP_UNNEEDED,
359 OPTION_STRIP_UNNEEDED_SYMBOL,
360 OPTION_STRIP_UNNEEDED_SYMBOLS,
361 OPTION_SUBSYSTEM,
362 OPTION_UPDATE_SECTION,
363 OPTION_VERILOG_DATA_WIDTH,
364 OPTION_WEAKEN,
365 OPTION_WEAKEN_SYMBOLS,
366 OPTION_WRITABLE_TEXT
367 };
368
369 /* Options to handle if running as "strip". */
370
371 static struct option strip_options[] =
372 {
373 {"disable-deterministic-archives", no_argument, 0, 'U'},
374 {"discard-all", no_argument, 0, 'x'},
375 {"discard-locals", no_argument, 0, 'X'},
376 {"enable-deterministic-archives", no_argument, 0, 'D'},
377 {"format", required_argument, 0, 'F'}, /* Obsolete */
378 {"help", no_argument, 0, 'h'},
379 {"info", no_argument, 0, OPTION_FORMATS_INFO},
380 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
381 {"input-target", required_argument, 0, 'I'},
382 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
383 {"keep-symbol", required_argument, 0, 'K'},
384 {"merge-notes", no_argument, 0, 'M'},
385 {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
386 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
387 {"output-file", required_argument, 0, 'o'},
388 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
389 {"output-target", required_argument, 0, 'O'},
390 {"preserve-dates", no_argument, 0, 'p'},
391 {"remove-section", required_argument, 0, 'R'},
392 {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
393 {"strip-all", no_argument, 0, 's'},
394 {"strip-debug", no_argument, 0, 'S'},
395 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
396 {"strip-symbol", required_argument, 0, 'N'},
397 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
398 {"target", required_argument, 0, 'F'},
399 {"verbose", no_argument, 0, 'v'},
400 {"version", no_argument, 0, 'V'},
401 {"wildcard", no_argument, 0, 'w'},
402 {0, no_argument, 0, 0}
403 };
404
405 /* Options to handle if running as "objcopy". */
406
407 static struct option copy_options[] =
408 {
409 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
410 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
411 {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
412 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
413 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
414 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
415 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
416 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
417 {"binary-architecture", required_argument, 0, 'B'},
418 {"byte", required_argument, 0, 'b'},
419 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
420 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
421 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
422 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
423 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
424 {"change-start", required_argument, 0, OPTION_CHANGE_START},
425 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
426 {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
427 {"debugging", no_argument, 0, OPTION_DEBUGGING},
428 {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
429 {"disable-deterministic-archives", no_argument, 0, 'U'},
430 {"discard-all", no_argument, 0, 'x'},
431 {"discard-locals", no_argument, 0, 'X'},
432 {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
433 {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
434 {"enable-deterministic-archives", no_argument, 0, 'D'},
435 {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
436 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
437 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
438 {"format", required_argument, 0, 'F'}, /* Obsolete */
439 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
440 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
441 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
442 {"heap", required_argument, 0, OPTION_HEAP},
443 {"help", no_argument, 0, 'h'},
444 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
445 {"impure", no_argument, 0, OPTION_IMPURE},
446 {"info", no_argument, 0, OPTION_FORMATS_INFO},
447 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
448 {"input-target", required_argument, 0, 'I'},
449 {"interleave", optional_argument, 0, 'i'},
450 {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
451 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
452 {"keep-global-symbol", required_argument, 0, 'G'},
453 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
454 {"keep-symbol", required_argument, 0, 'K'},
455 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
456 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
457 {"localize-symbol", required_argument, 0, 'L'},
458 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
459 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
460 {"merge-notes", no_argument, 0, 'M'},
461 {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
462 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
463 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
464 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
465 {"only-section", required_argument, 0, 'j'},
466 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
467 {"output-target", required_argument, 0, 'O'},
468 {"pad-to", required_argument, 0, OPTION_PAD_TO},
469 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
470 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
471 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
472 {"preserve-dates", no_argument, 0, 'p'},
473 {"pure", no_argument, 0, OPTION_PURE},
474 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
475 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
476 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
477 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
478 {"remove-section", required_argument, 0, 'R'},
479 {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
480 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
481 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
482 {"section-alignment", required_argument, 0, OPTION_PE_SECTION_ALIGNMENT},
483 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
484 {"set-section-alignment", required_argument, 0, OPTION_SET_SECTION_ALIGNMENT},
485 {"set-start", required_argument, 0, OPTION_SET_START},
486 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
487 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
488 {"stack", required_argument, 0, OPTION_STACK},
489 {"strip-all", no_argument, 0, 'S'},
490 {"strip-debug", no_argument, 0, 'g'},
491 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
492 {"strip-symbol", required_argument, 0, 'N'},
493 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
494 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
495 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
496 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
497 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
498 {"target", required_argument, 0, 'F'},
499 {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
500 {"verbose", no_argument, 0, 'v'},
501 {"verilog-data-width", required_argument, 0, OPTION_VERILOG_DATA_WIDTH},
502 {"version", no_argument, 0, 'V'},
503 {"weaken", no_argument, 0, OPTION_WEAKEN},
504 {"weaken-symbol", required_argument, 0, 'W'},
505 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
506 {"wildcard", no_argument, 0, 'w'},
507 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
508 {0, no_argument, 0, 0}
509 };
510
511 /* IMPORTS */
512 extern char *program_name;
513
514 /* This flag distinguishes between strip and objcopy:
515 1 means this is 'strip'; 0 means this is 'objcopy'.
516 -1 means if we should use argv[0] to decide. */
517 extern int is_strip;
518
519 /* The maximum length of an S record. This variable is defined in srec.c
520 and can be modified by the --srec-len parameter. */
521 extern unsigned int _bfd_srec_len;
522
523 /* Restrict the generation of Srecords to type S3 only.
524 This variable is defined in bfd/srec.c and can be toggled
525 on by the --srec-forceS3 command line switch. */
526 extern bfd_boolean _bfd_srec_forceS3;
527
528 /* Width of data in bytes for verilog output.
529 This variable is declared in bfd/verilog.c and can be modified by
530 the --verilog-data-width parameter. */
531 extern unsigned int VerilogDataWidth;
532
533 /* Forward declarations. */
534 static void setup_section (bfd *, asection *, void *);
535 static void setup_bfd_headers (bfd *, bfd *);
536 static void copy_relocations_in_section (bfd *, asection *, void *);
537 static void copy_section (bfd *, asection *, void *);
538 static void get_sections (bfd *, asection *, void *);
539 static int compare_section_lma (const void *, const void *);
540 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
541 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
542 static const char *lookup_sym_redefinition (const char *);
543 static const char *find_section_rename (const char *, flagword *);
544 \f
545 ATTRIBUTE_NORETURN static void
546 copy_usage (FILE *stream, int exit_status)
547 {
548 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
549 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
550 fprintf (stream, _(" The options are:\n"));
551 fprintf (stream, _("\
552 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
553 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
554 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
555 -F --target <bfdname> Set both input and output format to <bfdname>\n\
556 --debugging Convert debugging information, if possible\n\
557 -p --preserve-dates Copy modified/access timestamps to the output\n"));
558 if (DEFAULT_AR_DETERMINISTIC)
559 fprintf (stream, _("\
560 -D --enable-deterministic-archives\n\
561 Produce deterministic output when stripping archives (default)\n\
562 -U --disable-deterministic-archives\n\
563 Disable -D behavior\n"));
564 else
565 fprintf (stream, _("\
566 -D --enable-deterministic-archives\n\
567 Produce deterministic output when stripping archives\n\
568 -U --disable-deterministic-archives\n\
569 Disable -D behavior (default)\n"));
570 fprintf (stream, _("\
571 -j --only-section <name> Only copy section <name> into the output\n\
572 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
573 -R --remove-section <name> Remove section <name> from the output\n\
574 --remove-relocations <name> Remove relocations from section <name>\n\
575 -S --strip-all Remove all symbol and relocation information\n\
576 -g --strip-debug Remove all debugging symbols & sections\n\
577 --strip-dwo Remove all DWO sections\n\
578 --strip-unneeded Remove all symbols not needed by relocations\n\
579 -N --strip-symbol <name> Do not copy symbol <name>\n\
580 --strip-unneeded-symbol <name>\n\
581 Do not copy symbol <name> unless needed by\n\
582 relocations\n\
583 --only-keep-debug Strip everything but the debug information\n\
584 --extract-dwo Copy only DWO sections\n\
585 --extract-symbol Remove section contents but keep symbols\n\
586 -K --keep-symbol <name> Do not strip symbol <name>\n\
587 --keep-file-symbols Do not strip file symbol(s)\n\
588 --localize-hidden Turn all ELF hidden symbols into locals\n\
589 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
590 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
591 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
592 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
593 --weaken Force all global symbols to be marked as weak\n\
594 -w --wildcard Permit wildcard in symbol comparison\n\
595 -x --discard-all Remove all non-global symbols\n\
596 -X --discard-locals Remove any compiler-generated symbols\n\
597 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
598 --interleave-width <number> Set N for --interleave\n\
599 -b --byte <num> Select byte <num> in every interleaved block\n\
600 --gap-fill <val> Fill gaps between sections with <val>\n\
601 --pad-to <addr> Pad the last section up to address <addr>\n\
602 --set-start <addr> Set the start address to <addr>\n\
603 {--change-start|--adjust-start} <incr>\n\
604 Add <incr> to the start address\n\
605 {--change-addresses|--adjust-vma} <incr>\n\
606 Add <incr> to LMA, VMA and start addresses\n\
607 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
608 Change LMA and VMA of section <name> by <val>\n\
609 --change-section-lma <name>{=|+|-}<val>\n\
610 Change the LMA of section <name> by <val>\n\
611 --change-section-vma <name>{=|+|-}<val>\n\
612 Change the VMA of section <name> by <val>\n\
613 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
614 Warn if a named section does not exist\n\
615 --set-section-flags <name>=<flags>\n\
616 Set section <name>'s properties to <flags>\n\
617 --set-section-alignment <name>=<align>\n\
618 Set section <name>'s alignment to 2^<align> bytes\n\
619 --add-section <name>=<file> Add section <name> found in <file> to output\n\
620 --update-section <name>=<file>\n\
621 Update contents of section <name> with\n\
622 contents found in <file>\n\
623 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
624 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
625 --long-section-names {enable|disable|keep}\n\
626 Handle long section names in Coff objects.\n\
627 --change-leading-char Force output format's leading character style\n\
628 --remove-leading-char Remove leading character from global symbols\n\
629 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
630 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
631 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
632 listed in <file>\n\
633 --srec-len <number> Restrict the length of generated Srecords\n\
634 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
635 --strip-symbols <file> -N for all symbols listed in <file>\n\
636 --strip-unneeded-symbols <file>\n\
637 --strip-unneeded-symbol for all symbols listed\n\
638 in <file>\n\
639 --keep-symbols <file> -K for all symbols listed in <file>\n\
640 --localize-symbols <file> -L for all symbols listed in <file>\n\
641 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
642 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
643 --weaken-symbols <file> -W for all symbols listed in <file>\n\
644 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
645 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
646 --writable-text Mark the output text as writable\n\
647 --readonly-text Make the output text write protected\n\
648 --pure Mark the output file as demand paged\n\
649 --impure Mark the output file as impure\n\
650 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
651 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
652 --prefix-alloc-sections <prefix>\n\
653 Add <prefix> to start of every allocatable\n\
654 section name\n\
655 --file-alignment <num> Set PE file alignment to <num>\n\
656 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
657 <commit>\n\
658 --image-base <address> Set PE image base to <address>\n\
659 --section-alignment <num> Set PE section alignment to <num>\n\
660 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
661 <commit>\n\
662 --subsystem <name>[:<version>]\n\
663 Set PE subsystem to <name> [& <version>]\n\
664 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
665 Compress DWARF debug sections using zlib\n\
666 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
667 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
668 type\n\
669 --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
670 -M --merge-notes Remove redundant entries in note sections\n\
671 --no-merge-notes Do not attempt to remove redundant notes (default)\n\
672 -v --verbose List all object files modified\n\
673 @<file> Read options from <file>\n\
674 -V --version Display this program's version number\n\
675 -h --help Display this output\n\
676 --info List object formats & architectures supported\n\
677 "));
678 list_supported_targets (program_name, stream);
679 if (REPORT_BUGS_TO[0] && exit_status == 0)
680 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
681 exit (exit_status);
682 }
683
684 ATTRIBUTE_NORETURN static void
685 strip_usage (FILE *stream, int exit_status)
686 {
687 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
688 fprintf (stream, _(" Removes symbols and sections from files\n"));
689 fprintf (stream, _(" The options are:\n"));
690 fprintf (stream, _("\
691 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
692 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
693 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
694 -p --preserve-dates Copy modified/access timestamps to the output\n\
695 "));
696 if (DEFAULT_AR_DETERMINISTIC)
697 fprintf (stream, _("\
698 -D --enable-deterministic-archives\n\
699 Produce deterministic output when stripping archives (default)\n\
700 -U --disable-deterministic-archives\n\
701 Disable -D behavior\n"));
702 else
703 fprintf (stream, _("\
704 -D --enable-deterministic-archives\n\
705 Produce deterministic output when stripping archives\n\
706 -U --disable-deterministic-archives\n\
707 Disable -D behavior (default)\n"));
708 fprintf (stream, _("\
709 -R --remove-section=<name> Also remove section <name> from the output\n\
710 --remove-relocations <name> Remove relocations from section <name>\n\
711 -s --strip-all Remove all symbol and relocation information\n\
712 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
713 --strip-dwo Remove all DWO sections\n\
714 --strip-unneeded Remove all symbols not needed by relocations\n\
715 --only-keep-debug Strip everything but the debug information\n\
716 -M --merge-notes Remove redundant entries in note sections (default)\n\
717 --no-merge-notes Do not attempt to remove redundant notes\n\
718 -N --strip-symbol=<name> Do not copy symbol <name>\n\
719 -K --keep-symbol=<name> Do not strip symbol <name>\n\
720 --keep-file-symbols Do not strip file symbol(s)\n\
721 -w --wildcard Permit wildcard in symbol comparison\n\
722 -x --discard-all Remove all non-global symbols\n\
723 -X --discard-locals Remove any compiler-generated symbols\n\
724 -v --verbose List all object files modified\n\
725 -V --version Display this program's version number\n\
726 -h --help Display this output\n\
727 --info List object formats & architectures supported\n\
728 -o <file> Place stripped output into <file>\n\
729 "));
730
731 list_supported_targets (program_name, stream);
732 if (REPORT_BUGS_TO[0] && exit_status == 0)
733 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
734 exit (exit_status);
735 }
736
737 /* Parse section flags into a flagword, with a fatal error if the
738 string can't be parsed. */
739
740 static flagword
741 parse_flags (const char *s)
742 {
743 flagword ret;
744 const char *snext;
745 int len;
746
747 ret = SEC_NO_FLAGS;
748
749 do
750 {
751 snext = strchr (s, ',');
752 if (snext == NULL)
753 len = strlen (s);
754 else
755 {
756 len = snext - s;
757 ++snext;
758 }
759
760 if (0) ;
761 #define PARSE_FLAG(fname,fval) \
762 else if (strncasecmp (fname, s, len) == 0) ret |= fval
763 PARSE_FLAG ("alloc", SEC_ALLOC);
764 PARSE_FLAG ("load", SEC_LOAD);
765 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
766 PARSE_FLAG ("readonly", SEC_READONLY);
767 PARSE_FLAG ("debug", SEC_DEBUGGING);
768 PARSE_FLAG ("code", SEC_CODE);
769 PARSE_FLAG ("data", SEC_DATA);
770 PARSE_FLAG ("rom", SEC_ROM);
771 PARSE_FLAG ("share", SEC_COFF_SHARED);
772 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
773 PARSE_FLAG ("merge", SEC_MERGE);
774 PARSE_FLAG ("strings", SEC_STRINGS);
775 #undef PARSE_FLAG
776 else
777 {
778 char *copy;
779
780 copy = (char *) xmalloc (len + 1);
781 strncpy (copy, s, len);
782 copy[len] = '\0';
783 non_fatal (_("unrecognized section flag `%s'"), copy);
784 fatal (_("supported flags: %s"),
785 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
786 }
787
788 s = snext;
789 }
790 while (s != NULL);
791
792 return ret;
793 }
794
795 /* Parse symbol flags into a flagword, with a fatal error if the
796 string can't be parsed. */
797
798 static flagword
799 parse_symflags (const char *s, char **other)
800 {
801 flagword ret;
802 const char *snext;
803 size_t len;
804
805 ret = BSF_NO_FLAGS;
806
807 do
808 {
809 snext = strchr (s, ',');
810 if (snext == NULL)
811 len = strlen (s);
812 else
813 {
814 len = snext - s;
815 ++snext;
816 }
817
818 #define PARSE_FLAG(fname, fval) \
819 else if (len == sizeof fname - 1 \
820 && strncasecmp (fname, s, len) == 0) \
821 ret |= fval
822
823 #define PARSE_OTHER(fname, fval) \
824 else if (len >= sizeof fname \
825 && strncasecmp (fname, s, sizeof fname - 1) == 0) \
826 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
827
828 if (0) ;
829 PARSE_FLAG ("local", BSF_LOCAL);
830 PARSE_FLAG ("global", BSF_GLOBAL);
831 PARSE_FLAG ("export", BSF_EXPORT);
832 PARSE_FLAG ("debug", BSF_DEBUGGING);
833 PARSE_FLAG ("function", BSF_FUNCTION);
834 PARSE_FLAG ("weak", BSF_WEAK);
835 PARSE_FLAG ("section", BSF_SECTION_SYM);
836 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
837 PARSE_FLAG ("warning", BSF_WARNING);
838 PARSE_FLAG ("indirect", BSF_INDIRECT);
839 PARSE_FLAG ("file", BSF_FILE);
840 PARSE_FLAG ("object", BSF_OBJECT);
841 PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
842 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
843 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
844 PARSE_OTHER ("before=", *other);
845
846 #undef PARSE_FLAG
847 #undef PARSE_OTHER
848 else
849 {
850 char *copy;
851
852 copy = (char *) xmalloc (len + 1);
853 strncpy (copy, s, len);
854 copy[len] = '\0';
855 non_fatal (_("unrecognized symbol flag `%s'"), copy);
856 fatal (_("supported flags: %s"),
857 "local, global, export, debug, function, weak, section, "
858 "constructor, warning, indirect, file, object, synthetic, "
859 "indirect-function, unique-object, before=<othersym>");
860 }
861
862 s = snext;
863 }
864 while (s != NULL);
865
866 return ret;
867 }
868
869 /* Find and optionally add an entry in the change_sections list.
870
871 We need to be careful in how we match section names because of the support
872 for wildcard characters. For example suppose that the user has invoked
873 objcopy like this:
874
875 --set-section-flags .debug_*=debug
876 --set-section-flags .debug_str=readonly,debug
877 --change-section-address .debug_*ranges=0x1000
878
879 With the idea that all debug sections will receive the DEBUG flag, the
880 .debug_str section will also receive the READONLY flag and the
881 .debug_ranges and .debug_aranges sections will have their address set to
882 0x1000. (This may not make much sense, but it is just an example).
883
884 When adding the section name patterns to the section list we need to make
885 sure that previous entries do not match with the new entry, unless the
886 match is exact. (In which case we assume that the user is overriding
887 the previous entry with the new context).
888
889 When matching real section names to the section list we make use of the
890 wildcard characters, but we must do so in context. Eg if we are setting
891 section addresses then we match for .debug_ranges but not for .debug_info.
892
893 Finally, if ADD is false and we do find a match, we mark the section list
894 entry as used. */
895
896 static struct section_list *
897 find_section_list (const char *name, bfd_boolean add, unsigned int context)
898 {
899 struct section_list *p, *match = NULL;
900
901 /* assert ((context & ((1 << 7) - 1)) != 0); */
902
903 for (p = change_sections; p != NULL; p = p->next)
904 {
905 if (add)
906 {
907 if (strcmp (p->pattern, name) == 0)
908 {
909 /* Check for context conflicts. */
910 if (((p->context & SECTION_CONTEXT_REMOVE)
911 && (context & SECTION_CONTEXT_COPY))
912 || ((context & SECTION_CONTEXT_REMOVE)
913 && (p->context & SECTION_CONTEXT_COPY)))
914 fatal (_("error: %s both copied and removed"), name);
915
916 if (((p->context & SECTION_CONTEXT_SET_VMA)
917 && (context & SECTION_CONTEXT_ALTER_VMA))
918 || ((context & SECTION_CONTEXT_SET_VMA)
919 && (context & SECTION_CONTEXT_ALTER_VMA)))
920 fatal (_("error: %s both sets and alters VMA"), name);
921
922 if (((p->context & SECTION_CONTEXT_SET_LMA)
923 && (context & SECTION_CONTEXT_ALTER_LMA))
924 || ((context & SECTION_CONTEXT_SET_LMA)
925 && (context & SECTION_CONTEXT_ALTER_LMA)))
926 fatal (_("error: %s both sets and alters LMA"), name);
927
928 /* Extend the context. */
929 p->context |= context;
930 return p;
931 }
932 }
933 /* If we are not adding a new name/pattern then
934 only check for a match if the context applies. */
935 else if (p->context & context)
936 {
937 /* We could check for the presence of wildchar characters
938 first and choose between calling strcmp and fnmatch,
939 but is that really worth it ? */
940 if (p->pattern [0] == '!')
941 {
942 if (fnmatch (p->pattern + 1, name, 0) == 0)
943 {
944 p->used = TRUE;
945 return NULL;
946 }
947 }
948 else
949 {
950 if (fnmatch (p->pattern, name, 0) == 0)
951 {
952 if (match == NULL)
953 match = p;
954 }
955 }
956 }
957 }
958
959 if (! add)
960 {
961 if (match != NULL)
962 match->used = TRUE;
963 return match;
964 }
965
966 p = (struct section_list *) xmalloc (sizeof (struct section_list));
967 p->pattern = name;
968 p->used = FALSE;
969 p->context = context;
970 p->vma_val = 0;
971 p->lma_val = 0;
972 p->flags = 0;
973 p->alignment = 0;
974 p->next = change_sections;
975 change_sections = p;
976
977 return p;
978 }
979
980 /* S1 is the entry node already in the table, S2 is the key node. */
981
982 static int
983 eq_string_redefnode (const void *s1, const void *s2)
984 {
985 struct redefine_node *node1 = (struct redefine_node *) s1;
986 struct redefine_node *node2 = (struct redefine_node *) s2;
987 return !strcmp ((const char *) node1->source, (const char *) node2->source);
988 }
989
990 /* P is redefine node. Hash value is generated from its "source" filed. */
991
992 static hashval_t
993 htab_hash_redefnode (const void *p)
994 {
995 struct redefine_node *redefnode = (struct redefine_node *) p;
996 return htab_hash_string (redefnode->source);
997 }
998
999 /* Create hashtab used for redefine node. */
1000
1001 static htab_t
1002 create_symbol2redef_htab (void)
1003 {
1004 return htab_create_alloc (16, htab_hash_redefnode, eq_string_redefnode, NULL,
1005 xcalloc, free);
1006 }
1007
1008 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
1009
1010 static int
1011 eq_string (const void *s1, const void *s2)
1012 {
1013 return strcmp ((const char *) s1, (const char *) s2) == 0;
1014 }
1015
1016 static htab_t
1017 create_symbol_htab (void)
1018 {
1019 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
1020 }
1021
1022 static void
1023 create_symbol_htabs (void)
1024 {
1025 strip_specific_htab = create_symbol_htab ();
1026 strip_unneeded_htab = create_symbol_htab ();
1027 keep_specific_htab = create_symbol_htab ();
1028 localize_specific_htab = create_symbol_htab ();
1029 globalize_specific_htab = create_symbol_htab ();
1030 keepglobal_specific_htab = create_symbol_htab ();
1031 weaken_specific_htab = create_symbol_htab ();
1032 redefine_specific_htab = create_symbol2redef_htab ();
1033 /* As there is no bidirectional hash table in libiberty, need a reverse table
1034 to check duplicated target string. */
1035 redefine_specific_reverse_htab = create_symbol_htab ();
1036 }
1037
1038 /* Add a symbol to strip_specific_list. */
1039
1040 static void
1041 add_specific_symbol (const char *name, htab_t htab)
1042 {
1043 *htab_find_slot (htab, name, INSERT) = (char *) name;
1044 }
1045
1046 /* Like add_specific_symbol, but the element type is void *. */
1047
1048 static void
1049 add_specific_symbol_node (const void *node, htab_t htab)
1050 {
1051 *htab_find_slot (htab, node, INSERT) = (void *) node;
1052 }
1053
1054 /* Add symbols listed in `filename' to strip_specific_list. */
1055
1056 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
1057 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
1058
1059 static void
1060 add_specific_symbols (const char *filename, htab_t htab, char **buffer_p)
1061 {
1062 off_t size;
1063 FILE * f;
1064 char * line;
1065 char * buffer;
1066 unsigned int line_count;
1067
1068 size = get_file_size (filename);
1069 if (size == 0)
1070 {
1071 status = 1;
1072 return;
1073 }
1074
1075 buffer = (char *) xmalloc (size + 2);
1076 f = fopen (filename, FOPEN_RT);
1077 if (f == NULL)
1078 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
1079
1080 if (fread (buffer, 1, size, f) == 0 || ferror (f))
1081 fatal (_("%s: fread failed"), filename);
1082
1083 fclose (f);
1084 buffer [size] = '\n';
1085 buffer [size + 1] = '\0';
1086
1087 line_count = 1;
1088
1089 for (line = buffer; * line != '\0'; line ++)
1090 {
1091 char * eol;
1092 char * name;
1093 char * name_end;
1094 int finished = FALSE;
1095
1096 for (eol = line;; eol ++)
1097 {
1098 switch (* eol)
1099 {
1100 case '\n':
1101 * eol = '\0';
1102 /* Cope with \n\r. */
1103 if (eol[1] == '\r')
1104 ++ eol;
1105 finished = TRUE;
1106 break;
1107
1108 case '\r':
1109 * eol = '\0';
1110 /* Cope with \r\n. */
1111 if (eol[1] == '\n')
1112 ++ eol;
1113 finished = TRUE;
1114 break;
1115
1116 case 0:
1117 finished = TRUE;
1118 break;
1119
1120 case '#':
1121 /* Line comment, Terminate the line here, in case a
1122 name is present and then allow the rest of the
1123 loop to find the real end of the line. */
1124 * eol = '\0';
1125 break;
1126
1127 default:
1128 break;
1129 }
1130
1131 if (finished)
1132 break;
1133 }
1134
1135 /* A name may now exist somewhere between 'line' and 'eol'.
1136 Strip off leading whitespace and trailing whitespace,
1137 then add it to the list. */
1138 for (name = line; IS_WHITESPACE (* name); name ++)
1139 ;
1140 for (name_end = name;
1141 (! IS_WHITESPACE (* name_end))
1142 && (! IS_LINE_TERMINATOR (* name_end));
1143 name_end ++)
1144 ;
1145
1146 if (! IS_LINE_TERMINATOR (* name_end))
1147 {
1148 char * extra;
1149
1150 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
1151 ;
1152
1153 if (! IS_LINE_TERMINATOR (* extra))
1154 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1155 filename, line_count);
1156 }
1157
1158 * name_end = '\0';
1159
1160 if (name_end > name)
1161 add_specific_symbol (name, htab);
1162
1163 /* Advance line pointer to end of line. The 'eol ++' in the for
1164 loop above will then advance us to the start of the next line. */
1165 line = eol;
1166 line_count ++;
1167 }
1168
1169 /* Do not free the buffer. Parts of it will have been referenced
1170 in the calls to add_specific_symbol. */
1171 *buffer_p = buffer;
1172 }
1173
1174 /* See whether a symbol should be stripped or kept
1175 based on strip_specific_list and keep_symbols. */
1176
1177 static int
1178 is_specified_symbol_predicate (void **slot, void *data)
1179 {
1180 struct is_specified_symbol_predicate_data *d =
1181 (struct is_specified_symbol_predicate_data *) data;
1182 const char *slot_name = (char *) *slot;
1183
1184 if (*slot_name != '!')
1185 {
1186 if (! fnmatch (slot_name, d->name, 0))
1187 {
1188 d->found = TRUE;
1189 /* Continue traversal, there might be a non-match rule. */
1190 return 1;
1191 }
1192 }
1193 else
1194 {
1195 if (! fnmatch (slot_name + 1, d->name, 0))
1196 {
1197 d->found = FALSE;
1198 /* Stop traversal. */
1199 return 0;
1200 }
1201 }
1202
1203 /* Continue traversal. */
1204 return 1;
1205 }
1206
1207 static bfd_boolean
1208 is_specified_symbol (const char *name, htab_t htab)
1209 {
1210 if (wildcard)
1211 {
1212 struct is_specified_symbol_predicate_data data;
1213
1214 data.name = name;
1215 data.found = FALSE;
1216
1217 htab_traverse (htab, is_specified_symbol_predicate, &data);
1218
1219 return data.found;
1220 }
1221
1222 return htab_find (htab, name) != NULL;
1223 }
1224
1225 /* Return a pointer to the symbol used as a signature for GROUP. */
1226
1227 static asymbol *
1228 group_signature (asection *group)
1229 {
1230 bfd *abfd = group->owner;
1231 Elf_Internal_Shdr *ghdr;
1232
1233 /* PR 20089: An earlier error may have prevented us from loading the symbol table. */
1234 if (isympp == NULL)
1235 return NULL;
1236
1237 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1238 return NULL;
1239
1240 ghdr = &elf_section_data (group)->this_hdr;
1241 if (ghdr->sh_link == elf_onesymtab (abfd))
1242 {
1243 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1244 Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
1245
1246 if (ghdr->sh_info > 0
1247 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
1248 return isympp[ghdr->sh_info - 1];
1249 }
1250 return NULL;
1251 }
1252
1253 /* Return TRUE if the section is a DWO section. */
1254
1255 static bfd_boolean
1256 is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1257 {
1258 const char *name = bfd_get_section_name (abfd, sec);
1259 int len = strlen (name);
1260
1261 return strncmp (name + len - 4, ".dwo", 4) == 0;
1262 }
1263
1264 /* Return TRUE if section SEC is in the update list. */
1265
1266 static bfd_boolean
1267 is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1268 {
1269 if (update_sections != NULL)
1270 {
1271 struct section_add *pupdate;
1272
1273 for (pupdate = update_sections;
1274 pupdate != NULL;
1275 pupdate = pupdate->next)
1276 {
1277 if (strcmp (sec->name, pupdate->name) == 0)
1278 return TRUE;
1279 }
1280 }
1281
1282 return FALSE;
1283 }
1284
1285 static bfd_boolean
1286 is_merged_note_section (bfd * abfd, asection * sec)
1287 {
1288 if (merge_notes
1289 && bfd_get_flavour (abfd) == bfd_target_elf_flavour
1290 && elf_section_data (sec)->this_hdr.sh_type == SHT_NOTE
1291 /* FIXME: We currently only support merging GNU_BUILD_NOTEs.
1292 We should add support for more note types. */
1293 && ((elf_section_data (sec)->this_hdr.sh_flags & SHF_GNU_BUILD_NOTE) != 0
1294 /* Old versions of GAS (prior to 2.27) could not set the section
1295 flags to OS-specific values, so we also accept sections with the
1296 expected name. */
1297 || (strcmp (sec->name, GNU_BUILD_ATTRS_SECTION_NAME) == 0)))
1298 return TRUE;
1299
1300 return FALSE;
1301 }
1302
1303 /* See if a non-group section is being removed. */
1304
1305 static bfd_boolean
1306 is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1307 {
1308 if (sections_removed || sections_copied)
1309 {
1310 struct section_list *p;
1311 struct section_list *q;
1312
1313 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1314 SECTION_CONTEXT_REMOVE);
1315 q = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1316 SECTION_CONTEXT_COPY);
1317
1318 if (p && q)
1319 fatal (_("error: section %s matches both remove and copy options"),
1320 bfd_get_section_name (abfd, sec));
1321 if (p && is_update_section (abfd, sec))
1322 fatal (_("error: section %s matches both update and remove options"),
1323 bfd_get_section_name (abfd, sec));
1324
1325 if (p != NULL)
1326 return TRUE;
1327 if (sections_copied && q == NULL)
1328 return TRUE;
1329 }
1330
1331 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
1332 {
1333 if (strip_symbols == STRIP_DEBUG
1334 || strip_symbols == STRIP_UNNEEDED
1335 || strip_symbols == STRIP_ALL
1336 || discard_locals == LOCALS_ALL
1337 || convert_debugging)
1338 {
1339 /* By default we don't want to strip .reloc section.
1340 This section has for pe-coff special meaning. See
1341 pe-dll.c file in ld, and peXXigen.c in bfd for details. */
1342 if (strcmp (bfd_get_section_name (abfd, sec), ".reloc") != 0)
1343 return TRUE;
1344 }
1345
1346 if (strip_symbols == STRIP_DWO)
1347 return is_dwo_section (abfd, sec);
1348
1349 if (strip_symbols == STRIP_NONDEBUG)
1350 return FALSE;
1351 }
1352
1353 if (strip_symbols == STRIP_NONDWO)
1354 return !is_dwo_section (abfd, sec);
1355
1356 return FALSE;
1357 }
1358
1359 /* See if a section is being removed. */
1360
1361 static bfd_boolean
1362 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1363 {
1364 if (is_strip_section_1 (abfd, sec))
1365 return TRUE;
1366
1367 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
1368 {
1369 asymbol *gsym;
1370 const char *gname;
1371 asection *elt, *first;
1372
1373 gsym = group_signature (sec);
1374 /* Strip groups without a valid signature. */
1375 if (gsym == NULL)
1376 return TRUE;
1377
1378 /* PR binutils/3181
1379 If we are going to strip the group signature symbol, then
1380 strip the group section too. */
1381 gname = gsym->name;
1382 if ((strip_symbols == STRIP_ALL
1383 && !is_specified_symbol (gname, keep_specific_htab))
1384 || is_specified_symbol (gname, strip_specific_htab))
1385 return TRUE;
1386
1387 /* Remove the group section if all members are removed. */
1388 first = elt = elf_next_in_group (sec);
1389 while (elt != NULL)
1390 {
1391 if (!is_strip_section_1 (abfd, elt))
1392 return FALSE;
1393 elt = elf_next_in_group (elt);
1394 if (elt == first)
1395 break;
1396 }
1397
1398 return TRUE;
1399 }
1400
1401 return FALSE;
1402 }
1403
1404 static bfd_boolean
1405 is_nondebug_keep_contents_section (bfd *ibfd, asection *isection)
1406 {
1407 /* Always keep ELF note sections. */
1408 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
1409 return (elf_section_type (isection) == SHT_NOTE);
1410
1411 /* Always keep the .buildid section for PE/COFF.
1412
1413 Strictly, this should be written "always keep the section storing the debug
1414 directory", but that may be the .text section for objects produced by some
1415 tools, which it is not sensible to keep. */
1416 if (ibfd->xvec->flavour == bfd_target_coff_flavour)
1417 return (strcmp (bfd_get_section_name (ibfd, isection), ".buildid") == 0);
1418
1419 return FALSE;
1420 }
1421
1422 /* Return true if SYM is a hidden symbol. */
1423
1424 static bfd_boolean
1425 is_hidden_symbol (asymbol *sym)
1426 {
1427 elf_symbol_type *elf_sym;
1428
1429 elf_sym = elf_symbol_from (sym->the_bfd, sym);
1430 if (elf_sym != NULL)
1431 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
1432 {
1433 case STV_HIDDEN:
1434 case STV_INTERNAL:
1435 return TRUE;
1436 }
1437 return FALSE;
1438 }
1439
1440 static bfd_boolean
1441 need_sym_before (struct addsym_node **node, const char *sym)
1442 {
1443 int count;
1444 struct addsym_node *ptr = add_sym_list;
1445
1446 /* 'othersym' symbols are at the front of the list. */
1447 for (count = 0; count < add_symbols; count++)
1448 {
1449 if (!ptr->othersym)
1450 break;
1451 else if (strcmp (ptr->othersym, sym) == 0)
1452 {
1453 free (ptr->othersym);
1454 ptr->othersym = ""; /* Empty name is hopefully never a valid symbol name. */
1455 *node = ptr;
1456 return TRUE;
1457 }
1458 ptr = ptr->next;
1459 }
1460 return FALSE;
1461 }
1462
1463 static asymbol *
1464 create_new_symbol (struct addsym_node *ptr, bfd *obfd)
1465 {
1466 asymbol *sym = bfd_make_empty_symbol (obfd);
1467
1468 bfd_asymbol_name (sym) = ptr->symdef;
1469 sym->value = ptr->symval;
1470 sym->flags = ptr->flags;
1471 if (ptr->section)
1472 {
1473 asection *sec = bfd_get_section_by_name (obfd, ptr->section);
1474 if (!sec)
1475 fatal (_("Section %s not found"), ptr->section);
1476 sym->section = sec;
1477 }
1478 else
1479 sym->section = bfd_abs_section_ptr;
1480 return sym;
1481 }
1482
1483 /* Choose which symbol entries to copy; put the result in OSYMS.
1484 We don't copy in place, because that confuses the relocs.
1485 Return the number of symbols to print. */
1486
1487 static unsigned int
1488 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
1489 asymbol **isyms, long symcount)
1490 {
1491 asymbol **from = isyms, **to = osyms;
1492 long src_count = 0, dst_count = 0;
1493 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1494
1495 for (; src_count < symcount; src_count++)
1496 {
1497 asymbol *sym = from[src_count];
1498 flagword flags = sym->flags;
1499 char *name = (char *) bfd_asymbol_name (sym);
1500 bfd_boolean keep;
1501 bfd_boolean used_in_reloc = FALSE;
1502 bfd_boolean undefined;
1503 bfd_boolean rem_leading_char;
1504 bfd_boolean add_leading_char;
1505
1506 undefined = bfd_is_und_section (bfd_get_section (sym));
1507
1508 if (add_sym_list)
1509 {
1510 struct addsym_node *ptr;
1511
1512 if (need_sym_before (&ptr, name))
1513 to[dst_count++] = create_new_symbol (ptr, obfd);
1514 }
1515
1516 if (htab_elements (redefine_specific_htab) || section_rename_list)
1517 {
1518 char *new_name;
1519
1520 new_name = (char *) lookup_sym_redefinition (name);
1521 if (new_name == name
1522 && (flags & BSF_SECTION_SYM) != 0)
1523 new_name = (char *) find_section_rename (name, NULL);
1524 bfd_asymbol_name (sym) = new_name;
1525 name = new_name;
1526 }
1527
1528 /* Check if we will remove the current leading character. */
1529 rem_leading_char =
1530 (name[0] == bfd_get_symbol_leading_char (abfd))
1531 && (change_leading_char
1532 || (remove_leading_char
1533 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1534 || undefined
1535 || bfd_is_com_section (bfd_get_section (sym)))));
1536
1537 /* Check if we will add a new leading character. */
1538 add_leading_char =
1539 change_leading_char
1540 && (bfd_get_symbol_leading_char (obfd) != '\0')
1541 && (bfd_get_symbol_leading_char (abfd) == '\0'
1542 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1543
1544 /* Short circuit for change_leading_char if we can do it in-place. */
1545 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1546 {
1547 name[0] = bfd_get_symbol_leading_char (obfd);
1548 bfd_asymbol_name (sym) = name;
1549 rem_leading_char = FALSE;
1550 add_leading_char = FALSE;
1551 }
1552
1553 /* Remove leading char. */
1554 if (rem_leading_char)
1555 bfd_asymbol_name (sym) = ++name;
1556
1557 /* Add new leading char and/or prefix. */
1558 if (add_leading_char || prefix_symbols_string)
1559 {
1560 char *n, *ptr;
1561
1562 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1563 + strlen (name) + 1);
1564 if (add_leading_char)
1565 *ptr++ = bfd_get_symbol_leading_char (obfd);
1566
1567 if (prefix_symbols_string)
1568 {
1569 strcpy (ptr, prefix_symbols_string);
1570 ptr += strlen (prefix_symbols_string);
1571 }
1572
1573 strcpy (ptr, name);
1574 bfd_asymbol_name (sym) = n;
1575 name = n;
1576 }
1577
1578 if (strip_symbols == STRIP_ALL)
1579 keep = FALSE;
1580 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1581 || ((flags & BSF_SECTION_SYM) != 0
1582 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1583 & BSF_KEEP) != 0))
1584 {
1585 keep = TRUE;
1586 used_in_reloc = TRUE;
1587 }
1588 else if (relocatable /* Relocatable file. */
1589 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1590 || bfd_is_com_section (bfd_get_section (sym))))
1591 keep = TRUE;
1592 else if (bfd_decode_symclass (sym) == 'I')
1593 /* Global symbols in $idata sections need to be retained
1594 even if relocatable is FALSE. External users of the
1595 library containing the $idata section may reference these
1596 symbols. */
1597 keep = TRUE;
1598 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1599 || (flags & BSF_WEAK) != 0
1600 || undefined
1601 || bfd_is_com_section (bfd_get_section (sym)))
1602 keep = strip_symbols != STRIP_UNNEEDED;
1603 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1604 keep = (strip_symbols != STRIP_DEBUG
1605 && strip_symbols != STRIP_UNNEEDED
1606 && ! convert_debugging);
1607 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1608 /* COMDAT sections store special information in local
1609 symbols, so we cannot risk stripping any of them. */
1610 keep = TRUE;
1611 else /* Local symbol. */
1612 keep = (strip_symbols != STRIP_UNNEEDED
1613 && (discard_locals != LOCALS_ALL
1614 && (discard_locals != LOCALS_START_L
1615 || ! bfd_is_local_label (abfd, sym))));
1616
1617 if (keep && is_specified_symbol (name, strip_specific_htab))
1618 {
1619 /* There are multiple ways to set 'keep' above, but if it
1620 was the relocatable symbol case, then that's an error. */
1621 if (used_in_reloc)
1622 {
1623 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1624 status = 1;
1625 }
1626 else
1627 keep = FALSE;
1628 }
1629
1630 if (keep
1631 && !(flags & BSF_KEEP)
1632 && is_specified_symbol (name, strip_unneeded_htab))
1633 keep = FALSE;
1634
1635 if (!keep
1636 && ((keep_file_symbols && (flags & BSF_FILE))
1637 || is_specified_symbol (name, keep_specific_htab)))
1638 keep = TRUE;
1639
1640 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1641 keep = FALSE;
1642
1643 if (keep)
1644 {
1645 if ((flags & BSF_GLOBAL) != 0
1646 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1647 {
1648 sym->flags &= ~ BSF_GLOBAL;
1649 sym->flags |= BSF_WEAK;
1650 }
1651
1652 if (!undefined
1653 && (flags & (BSF_GLOBAL | BSF_WEAK))
1654 && (is_specified_symbol (name, localize_specific_htab)
1655 || (htab_elements (keepglobal_specific_htab) != 0
1656 && ! is_specified_symbol (name, keepglobal_specific_htab))
1657 || (localize_hidden && is_hidden_symbol (sym))))
1658 {
1659 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1660 sym->flags |= BSF_LOCAL;
1661 }
1662
1663 if (!undefined
1664 && (flags & BSF_LOCAL)
1665 && is_specified_symbol (name, globalize_specific_htab))
1666 {
1667 sym->flags &= ~ BSF_LOCAL;
1668 sym->flags |= BSF_GLOBAL;
1669 }
1670
1671 to[dst_count++] = sym;
1672 }
1673 }
1674 if (add_sym_list)
1675 {
1676 struct addsym_node *ptr = add_sym_list;
1677
1678 for (src_count = 0; src_count < add_symbols; src_count++)
1679 {
1680 if (ptr->othersym)
1681 {
1682 if (strcmp (ptr->othersym, ""))
1683 fatal (_("'before=%s' not found"), ptr->othersym);
1684 }
1685 else
1686 to[dst_count++] = create_new_symbol (ptr, obfd);
1687
1688 ptr = ptr->next;
1689 }
1690 }
1691
1692 to[dst_count] = NULL;
1693
1694 return dst_count;
1695 }
1696
1697 /* Find the redefined name of symbol SOURCE. */
1698
1699 static const char *
1700 lookup_sym_redefinition (const char *source)
1701 {
1702 struct redefine_node key_node = {(char *) source, NULL};
1703 struct redefine_node *redef_node
1704 = (struct redefine_node *) htab_find (redefine_specific_htab, &key_node);
1705
1706 return redef_node == NULL ? source : redef_node->target;
1707 }
1708
1709 /* Insert a node into symbol redefine hash tabel. */
1710
1711 static void
1712 add_redefine_and_check (const char *cause, const char *source,
1713 const char *target)
1714 {
1715 struct redefine_node *new_node
1716 = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1717
1718 new_node->source = strdup (source);
1719 new_node->target = strdup (target);
1720
1721 if (htab_find (redefine_specific_htab, new_node) != HTAB_EMPTY_ENTRY)
1722 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1723 cause, source);
1724
1725 if (htab_find (redefine_specific_reverse_htab, target) != HTAB_EMPTY_ENTRY)
1726 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1727 cause, target);
1728
1729 /* Insert the NEW_NODE into hash table for quick search. */
1730 add_specific_symbol_node (new_node, redefine_specific_htab);
1731
1732 /* Insert the target string into the reverse hash table, this is needed for
1733 duplicated target string check. */
1734 add_specific_symbol (new_node->target, redefine_specific_reverse_htab);
1735
1736 }
1737
1738 /* Handle the --redefine-syms option. Read lines containing "old new"
1739 from the file, and add them to the symbol redefine list. */
1740
1741 static void
1742 add_redefine_syms_file (const char *filename)
1743 {
1744 FILE *file;
1745 char *buf;
1746 size_t bufsize;
1747 size_t len;
1748 size_t outsym_off;
1749 int c, lineno;
1750
1751 file = fopen (filename, "r");
1752 if (file == NULL)
1753 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1754 filename, strerror (errno));
1755
1756 bufsize = 100;
1757 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
1758
1759 lineno = 1;
1760 c = getc (file);
1761 len = 0;
1762 outsym_off = 0;
1763 while (c != EOF)
1764 {
1765 /* Collect the input symbol name. */
1766 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1767 {
1768 if (c == '#')
1769 goto comment;
1770 buf[len++] = c;
1771 if (len >= bufsize)
1772 {
1773 bufsize *= 2;
1774 buf = (char *) xrealloc (buf, bufsize + 1);
1775 }
1776 c = getc (file);
1777 }
1778 buf[len++] = '\0';
1779 if (c == EOF)
1780 break;
1781
1782 /* Eat white space between the symbol names. */
1783 while (IS_WHITESPACE (c))
1784 c = getc (file);
1785 if (c == '#' || IS_LINE_TERMINATOR (c))
1786 goto comment;
1787 if (c == EOF)
1788 break;
1789
1790 /* Collect the output symbol name. */
1791 outsym_off = len;
1792 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1793 {
1794 if (c == '#')
1795 goto comment;
1796 buf[len++] = c;
1797 if (len >= bufsize)
1798 {
1799 bufsize *= 2;
1800 buf = (char *) xrealloc (buf, bufsize + 1);
1801 }
1802 c = getc (file);
1803 }
1804 buf[len++] = '\0';
1805 if (c == EOF)
1806 break;
1807
1808 /* Eat white space at end of line. */
1809 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1810 c = getc (file);
1811 if (c == '#')
1812 goto comment;
1813 /* Handle \r\n. */
1814 if ((c == '\r' && (c = getc (file)) == '\n')
1815 || c == '\n' || c == EOF)
1816 {
1817 end_of_line:
1818 /* Append the redefinition to the list. */
1819 if (buf[0] != '\0')
1820 add_redefine_and_check (filename, &buf[0], &buf[outsym_off]);
1821
1822 lineno++;
1823 len = 0;
1824 outsym_off = 0;
1825 if (c == EOF)
1826 break;
1827 c = getc (file);
1828 continue;
1829 }
1830 else
1831 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1832 comment:
1833 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1834 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1835 buf[len++] = '\0';
1836
1837 /* Eat the rest of the line and finish it. */
1838 while (c != '\n' && c != EOF)
1839 c = getc (file);
1840 goto end_of_line;
1841 }
1842
1843 if (len != 0)
1844 fatal (_("%s:%d: premature end of file"), filename, lineno);
1845
1846 free (buf);
1847 fclose (file);
1848 }
1849
1850 /* Copy unknown object file IBFD onto OBFD.
1851 Returns TRUE upon success, FALSE otherwise. */
1852
1853 static bfd_boolean
1854 copy_unknown_object (bfd *ibfd, bfd *obfd)
1855 {
1856 char *cbuf;
1857 int tocopy;
1858 long ncopied;
1859 long size;
1860 struct stat buf;
1861
1862 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1863 {
1864 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1865 return FALSE;
1866 }
1867
1868 size = buf.st_size;
1869 if (size < 0)
1870 {
1871 non_fatal (_("stat returns negative size for `%s'"),
1872 bfd_get_archive_filename (ibfd));
1873 return FALSE;
1874 }
1875
1876 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1877 {
1878 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1879 return FALSE;
1880 }
1881
1882 if (verbose)
1883 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1884 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1885
1886 cbuf = (char *) xmalloc (BUFSIZE);
1887 ncopied = 0;
1888 while (ncopied < size)
1889 {
1890 tocopy = size - ncopied;
1891 if (tocopy > BUFSIZE)
1892 tocopy = BUFSIZE;
1893
1894 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1895 != (bfd_size_type) tocopy)
1896 {
1897 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1898 free (cbuf);
1899 return FALSE;
1900 }
1901
1902 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1903 != (bfd_size_type) tocopy)
1904 {
1905 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1906 free (cbuf);
1907 return FALSE;
1908 }
1909
1910 ncopied += tocopy;
1911 }
1912
1913 /* We should at least to be able to read it back when copying an
1914 unknown object in an archive. */
1915 chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
1916 free (cbuf);
1917 return TRUE;
1918 }
1919
1920 /* Returns the number of bytes needed to store VAL. */
1921
1922 static inline unsigned int
1923 num_bytes (unsigned long val)
1924 {
1925 unsigned int count = 0;
1926
1927 /* FIXME: There must be a faster way to do this. */
1928 while (val)
1929 {
1930 count ++;
1931 val >>= 8;
1932 }
1933 return count;
1934 }
1935
1936 typedef struct objcopy_internal_note
1937 {
1938 Elf_Internal_Note note;
1939 bfd_vma start;
1940 bfd_vma end;
1941 bfd_boolean modified;
1942 } objcopy_internal_note;
1943
1944 /* Returns TRUE if a gap does, or could, exist between the address range
1945 covered by PNOTE1 and PNOTE2. */
1946
1947 static bfd_boolean
1948 gap_exists (objcopy_internal_note * pnote1,
1949 objcopy_internal_note * pnote2)
1950 {
1951 /* Without range end notes, we assume that a gap might exist. */
1952 if (pnote1->end == 0 || pnote2->end == 0)
1953 return TRUE;
1954
1955 /* FIXME: Alignment of 16 bytes taken from x86_64 binaries.
1956 Really we should extract the alignment of the section covered by the notes. */
1957 return BFD_ALIGN (pnote1->end, 16) < pnote2->start;
1958 }
1959
1960 static bfd_boolean
1961 is_open_note (objcopy_internal_note * pnote)
1962 {
1963 return (pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_OPEN);
1964 }
1965
1966 static bfd_boolean
1967 is_func_note (objcopy_internal_note * pnote)
1968 {
1969 return (pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_FUNC);
1970 }
1971
1972 static bfd_boolean
1973 is_64bit (bfd * abfd)
1974 {
1975 /* Should never happen, but let's be paranoid. */
1976 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1977 return FALSE;
1978
1979 return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64;
1980 }
1981
1982 /* Merge the notes on SEC, removing redundant entries.
1983 Returns the new, smaller size of the section upon success. */
1984
1985 static bfd_size_type
1986 merge_gnu_build_notes (bfd * abfd, asection * sec, bfd_size_type size, bfd_byte * contents)
1987 {
1988 objcopy_internal_note * pnotes_end;
1989 objcopy_internal_note * pnotes = NULL;
1990 objcopy_internal_note * pnote;
1991 bfd_size_type remain = size;
1992 unsigned version_1_seen = 0;
1993 unsigned version_2_seen = 0;
1994 unsigned version_3_seen = 0;
1995 bfd_boolean duplicate_found = FALSE;
1996 const char * err = NULL;
1997 bfd_byte * in = contents;
1998 int attribute_type_byte;
1999 int val_start;
2000 unsigned long previous_func_start = 0;
2001 unsigned long previous_open_start = 0;
2002 unsigned long previous_func_end = 0;
2003 unsigned long previous_open_end = 0;
2004 long relsize;
2005
2006 relsize = bfd_get_reloc_upper_bound (abfd, sec);
2007 if (relsize > 0)
2008 {
2009 arelent ** relpp;
2010 long relcount;
2011
2012 /* If there are relocs associated with this section then we
2013 cannot safely merge it. */
2014 relpp = (arelent **) xmalloc (relsize);
2015 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, isympp);
2016 free (relpp);
2017 if (relcount != 0)
2018 goto done;
2019 }
2020
2021 /* Make a copy of the notes and convert to our internal format.
2022 Minimum size of a note is 12 bytes. Also locate the version
2023 notes and check them. */
2024 pnote = pnotes = (objcopy_internal_note *) xcalloc ((size / 12), sizeof (* pnote));
2025 while (remain >= 12)
2026 {
2027 bfd_vma start, end;
2028
2029 pnote->note.namesz = (bfd_get_32 (abfd, in ) + 3) & ~3;
2030 pnote->note.descsz = (bfd_get_32 (abfd, in + 4) + 3) & ~3;
2031 pnote->note.type = bfd_get_32 (abfd, in + 8);
2032
2033 if (pnote->note.type != NT_GNU_BUILD_ATTRIBUTE_OPEN
2034 && pnote->note.type != NT_GNU_BUILD_ATTRIBUTE_FUNC)
2035 {
2036 err = _("corrupt GNU build attribute note: wrong note type");
2037 goto done;
2038 }
2039
2040 if (pnote->note.namesz + pnote->note.descsz + 12 > remain)
2041 {
2042 err = _("corrupt GNU build attribute note: note too big");
2043 goto done;
2044 }
2045
2046 if (pnote->note.namesz < 2)
2047 {
2048 err = _("corrupt GNU build attribute note: name too small");
2049 goto done;
2050 }
2051
2052 pnote->note.namedata = (char *)(in + 12);
2053 pnote->note.descdata = (char *)(in + 12 + pnote->note.namesz);
2054
2055 remain -= 12 + pnote->note.namesz + pnote->note.descsz;
2056 in += 12 + pnote->note.namesz + pnote->note.descsz;
2057
2058 if (pnote->note.namesz > 2
2059 && pnote->note.namedata[0] == '$'
2060 && pnote->note.namedata[1] == GNU_BUILD_ATTRIBUTE_VERSION
2061 && pnote->note.namedata[2] == '1')
2062 ++ version_1_seen;
2063 else if (pnote->note.namesz > 4
2064 && pnote->note.namedata[0] == 'G'
2065 && pnote->note.namedata[1] == 'A'
2066 && pnote->note.namedata[2] == '$'
2067 && pnote->note.namedata[3] == GNU_BUILD_ATTRIBUTE_VERSION)
2068 {
2069 if (pnote->note.namedata[4] == '2')
2070 ++ version_2_seen;
2071 else if (pnote->note.namedata[4] == '3')
2072 ++ version_3_seen;
2073 else
2074 {
2075 err = _("corrupt GNU build attribute note: unsupported version");
2076 goto done;
2077 }
2078 }
2079
2080 switch (pnote->note.descsz)
2081 {
2082 case 0:
2083 start = end = 0;
2084 break;
2085
2086 case 4:
2087 start = bfd_get_32 (abfd, pnote->note.descdata);
2088 /* FIXME: For version 1 and 2 notes we should try to
2089 calculate the end address by finding a symbol whose
2090 value is START, and then adding in its size.
2091
2092 For now though, since v1 and v2 was not intended to
2093 handle gaps, we chose an artificially large end
2094 address. */
2095 end = (bfd_vma) -1;
2096 break;
2097
2098 case 8:
2099 if (! is_64bit (abfd))
2100 {
2101 start = bfd_get_32 (abfd, pnote->note.descdata);
2102 end = bfd_get_32 (abfd, pnote->note.descdata + 4);
2103 }
2104 else
2105 {
2106 start = bfd_get_64 (abfd, pnote->note.descdata);
2107 /* FIXME: For version 1 and 2 notes we should try to
2108 calculate the end address by finding a symbol whose
2109 value is START, and then adding in its size.
2110
2111 For now though, since v1 and v2 was not intended to
2112 handle gaps, we chose an artificially large end
2113 address. */
2114 end = (bfd_vma) -1;
2115 }
2116 break;
2117
2118 case 16:
2119 start = bfd_get_64 (abfd, pnote->note.descdata);
2120 end = bfd_get_64 (abfd, pnote->note.descdata + 8);
2121 break;
2122
2123 default:
2124 err = _("corrupt GNU build attribute note: bad description size");
2125 goto done;
2126 }
2127
2128 if (is_open_note (pnote))
2129 {
2130 if (start)
2131 previous_open_start = start;
2132
2133 pnote->start = previous_open_start;
2134
2135 if (end)
2136 previous_open_end = end;
2137
2138 pnote->end = previous_open_end;
2139 }
2140 else
2141 {
2142 if (start)
2143 previous_func_start = start;
2144
2145 pnote->start = previous_func_start;
2146
2147 if (end)
2148 previous_func_end = end;
2149
2150 pnote->end = previous_func_end;
2151 }
2152
2153 if (pnote->note.namedata[pnote->note.namesz - 1] != 0)
2154 {
2155 err = _("corrupt GNU build attribute note: name not NUL terminated");
2156 goto done;
2157 }
2158
2159 pnote ++;
2160 }
2161
2162 pnotes_end = pnote;
2163
2164 /* Check that the notes are valid. */
2165 if (remain != 0)
2166 {
2167 err = _("corrupt GNU build attribute notes: excess data at end");
2168 goto done;
2169 }
2170
2171 if (version_1_seen == 0 && version_2_seen == 0 && version_3_seen == 0)
2172 {
2173 err = _("bad GNU build attribute notes: no known versions detected");
2174 goto done;
2175 }
2176
2177 if ((version_1_seen > 0 && version_2_seen > 0)
2178 || (version_1_seen > 0 && version_3_seen > 0)
2179 || (version_2_seen > 0 && version_3_seen > 0))
2180 {
2181 err = _("bad GNU build attribute notes: multiple different versions");
2182 goto done;
2183 }
2184
2185 /* Merging is only needed if there is more than one version note... */
2186 if (version_1_seen == 1 || version_2_seen == 1 || version_3_seen == 1)
2187 goto done;
2188
2189 attribute_type_byte = version_1_seen ? 1 : 3;
2190 val_start = attribute_type_byte + 1;
2191
2192 /* We used to require that the first note be a version note,
2193 but this is no longer enforced. Due to the problems with
2194 linking sections with the same name (eg .gnu.build.note.hot)
2195 we cannot guarantee that the first note will be a version note. */
2196
2197 /* Now merge the notes. The rules are:
2198 1. Preserve the ordering of the notes.
2199 2. Preserve any NT_GNU_BUILD_ATTRIBUTE_FUNC notes.
2200 3. Eliminate any NT_GNU_BUILD_ATTRIBUTE_OPEN notes that have the same
2201 full name field as the immediately preceeding note with the same type
2202 of name and whose address ranges coincide.
2203 IE - if there are gaps in the coverage of the notes, then these gaps
2204 must be preserved.
2205 4. Combine the numeric value of any NT_GNU_BUILD_ATTRIBUTE_OPEN notes
2206 of type GNU_BUILD_ATTRIBUTE_STACK_SIZE.
2207 5. If an NT_GNU_BUILD_ATTRIBUTE_OPEN note is going to be preserved and
2208 its description field is empty then the nearest preceeding OPEN note
2209 with a non-empty description field must also be preserved *OR* the
2210 description field of the note must be changed to contain the starting
2211 address to which it refers.
2212 6. Notes with the same start and end address can be deleted.
2213 7. FIXME: Elminate duplicate version notes - even function specific ones ? */
2214 for (pnote = pnotes; pnote < pnotes_end; pnote ++)
2215 {
2216 int note_type;
2217 objcopy_internal_note * back;
2218 objcopy_internal_note * prev_open_with_range = NULL;
2219
2220 /* Rule 6 - delete 0-range notes. */
2221 if (pnote->start == pnote->end)
2222 {
2223 duplicate_found = TRUE;
2224 pnote->note.type = 0;
2225 continue;
2226 }
2227
2228 /* Rule 2 - preserve function notes. */
2229 if (! is_open_note (pnote))
2230 {
2231 int iter;
2232
2233 /* Check to see if there is an identical previous function note.
2234 This can happen with overlays for example. */
2235 for (iter = 0, back = pnote -1; back >= pnotes; back --)
2236 {
2237 if (back->start == pnote->start
2238 && back->end == pnote->end
2239 && back->note.namesz == pnote->note.namesz
2240 && memcmp (back->note.namedata, pnote->note.namedata, pnote->note.namesz) == 0)
2241 {
2242 duplicate_found = TRUE;
2243 pnote->note.type = 0;
2244 break;
2245 }
2246
2247 /* Don't scan too far back however. */
2248 if (iter ++ > 16)
2249 break;
2250 }
2251 continue;
2252 }
2253
2254 note_type = pnote->note.namedata[attribute_type_byte];
2255
2256 /* Scan backwards from pnote, looking for duplicates.
2257 Clear the type field of any found - but do not delete them just yet. */
2258 for (back = pnote - 1; back >= pnotes; back --)
2259 {
2260 int back_type = back->note.namedata[attribute_type_byte];
2261
2262 /* If this is the first open note with an address
2263 range that we have encountered then record it. */
2264 if (prev_open_with_range == NULL
2265 && back->note.descsz > 0
2266 && ! is_func_note (back))
2267 prev_open_with_range = back;
2268
2269 if (! is_open_note (back))
2270 continue;
2271
2272 /* If the two notes are different then keep on searching. */
2273 if (back_type != note_type)
2274 continue;
2275
2276 /* Rule 4 - combine stack size notes. */
2277 if (back_type == GNU_BUILD_ATTRIBUTE_STACK_SIZE)
2278 {
2279 unsigned char * name;
2280 unsigned long note_val;
2281 unsigned long back_val;
2282 unsigned int shift;
2283 unsigned int bytes;
2284 unsigned long byte;
2285
2286 for (shift = 0, note_val = 0,
2287 bytes = pnote->note.namesz - val_start,
2288 name = (unsigned char *) pnote->note.namedata + val_start;
2289 bytes--;)
2290 {
2291 byte = (* name ++) & 0xff;
2292 note_val |= byte << shift;
2293 shift += 8;
2294 }
2295
2296 for (shift = 0, back_val = 0,
2297 bytes = back->note.namesz - val_start,
2298 name = (unsigned char *) back->note.namedata + val_start;
2299 bytes--;)
2300 {
2301 byte = (* name ++) & 0xff;
2302 back_val |= byte << shift;
2303 shift += 8;
2304 }
2305
2306 back_val += note_val;
2307 if (num_bytes (back_val) >= back->note.namesz - val_start)
2308 {
2309 /* We have a problem - the new value requires more bytes of
2310 storage in the name field than are available. Currently
2311 we have no way of fixing this, so we just preserve both
2312 notes. */
2313 continue;
2314 }
2315
2316 /* Write the new val into back. */
2317 name = (unsigned char *) back->note.namedata + val_start;
2318 while (name < (unsigned char *) back->note.namedata
2319 + back->note.namesz)
2320 {
2321 byte = back_val & 0xff;
2322 * name ++ = byte;
2323 if (back_val == 0)
2324 break;
2325 back_val >>= 8;
2326 }
2327
2328 duplicate_found = TRUE;
2329 pnote->note.type = 0;
2330 break;
2331 }
2332
2333 /* Rule 3 - combine identical open notes. */
2334 if (back->note.namesz == pnote->note.namesz
2335 && memcmp (back->note.namedata,
2336 pnote->note.namedata, back->note.namesz) == 0
2337 && ! gap_exists (back, pnote))
2338 {
2339 duplicate_found = TRUE;
2340 pnote->note.type = 0;
2341
2342 if (pnote->end > back->end)
2343 back->end = pnote->end;
2344
2345 if (version_3_seen)
2346 back->modified = TRUE;
2347 break;
2348 }
2349
2350 /* Rule 5 - Since we are keeping this note we must check to see
2351 if its description refers back to an earlier OPEN version
2352 note that has been scheduled for deletion. If so then we
2353 must make sure that version note is also preserved. */
2354 if (version_3_seen)
2355 {
2356 /* As of version 3 we can just
2357 move the range into the note. */
2358 pnote->modified = TRUE;
2359 pnote->note.type = NT_GNU_BUILD_ATTRIBUTE_FUNC;
2360 back->modified = TRUE;
2361 back->note.type = NT_GNU_BUILD_ATTRIBUTE_FUNC;
2362 }
2363 else
2364 {
2365 if (pnote->note.descsz == 0
2366 && prev_open_with_range != NULL
2367 && prev_open_with_range->note.type == 0)
2368 prev_open_with_range->note.type = NT_GNU_BUILD_ATTRIBUTE_OPEN;
2369 }
2370
2371 /* We have found a similar attribute but the details do not match.
2372 Stop searching backwards. */
2373 break;
2374 }
2375 }
2376
2377 if (duplicate_found)
2378 {
2379 bfd_byte * new_contents;
2380 bfd_byte * old;
2381 bfd_byte * new;
2382 bfd_size_type new_size;
2383 bfd_vma prev_start = 0;
2384 bfd_vma prev_end = 0;
2385
2386 /* Eliminate the duplicates. */
2387 new = new_contents = xmalloc (size);
2388 for (pnote = pnotes, old = contents;
2389 pnote < pnotes_end;
2390 pnote ++)
2391 {
2392 bfd_size_type note_size = 12 + pnote->note.namesz + pnote->note.descsz;
2393
2394 if (pnote->note.type != 0)
2395 {
2396 if (pnote->modified)
2397 {
2398 /* If the note has been modified then we must copy it by
2399 hand, potentially adding in a new description field. */
2400 if (pnote->start == prev_start && pnote->end == prev_end)
2401 {
2402 bfd_put_32 (abfd, pnote->note.namesz, new);
2403 bfd_put_32 (abfd, 0, new + 4);
2404 bfd_put_32 (abfd, pnote->note.type, new + 8);
2405 new += 12;
2406 memcpy (new, pnote->note.namedata, pnote->note.namesz);
2407 new += pnote->note.namesz;
2408 }
2409 else
2410 {
2411 bfd_put_32 (abfd, pnote->note.namesz, new);
2412 bfd_put_32 (abfd, is_64bit (abfd) ? 16 : 8, new + 4);
2413 bfd_put_32 (abfd, pnote->note.type, new + 8);
2414 new += 12;
2415 memcpy (new, pnote->note.namedata, pnote->note.namesz);
2416 new += pnote->note.namesz;
2417 if (is_64bit (abfd))
2418 {
2419 bfd_put_64 (abfd, pnote->start, new);
2420 bfd_put_64 (abfd, pnote->end, new + 8);
2421 new += 16;
2422 }
2423 else
2424 {
2425 bfd_put_32 (abfd, pnote->start, new);
2426 bfd_put_32 (abfd, pnote->end, new + 4);
2427 new += 8;
2428 }
2429 }
2430 }
2431 else
2432 {
2433 memcpy (new, old, note_size);
2434 new += note_size;
2435 }
2436 prev_start = pnote->start;
2437 prev_end = pnote->end;
2438 }
2439
2440 old += note_size;
2441 }
2442
2443 new_size = new - new_contents;
2444 memcpy (contents, new_contents, new_size);
2445 size = new_size;
2446 free (new_contents);
2447 }
2448
2449 done:
2450 if (err)
2451 {
2452 bfd_set_error (bfd_error_bad_value);
2453 bfd_nonfatal_message (NULL, abfd, sec, err);
2454 status = 1;
2455 }
2456
2457 free (pnotes);
2458 return size;
2459 }
2460
2461 /* Copy object file IBFD onto OBFD.
2462 Returns TRUE upon success, FALSE otherwise. */
2463
2464 static bfd_boolean
2465 copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
2466 {
2467 bfd_vma start;
2468 long symcount;
2469 asection **osections = NULL;
2470 asection *osec;
2471 asection *gnu_debuglink_section = NULL;
2472 bfd_size_type *gaps = NULL;
2473 bfd_size_type max_gap = 0;
2474 long symsize;
2475 void *dhandle;
2476 enum bfd_architecture iarch;
2477 unsigned int imach;
2478 unsigned int c, i;
2479
2480 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2481 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2482 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2483 {
2484 /* PR 17636: Call non-fatal so that we return to our parent who
2485 may need to tidy temporary files. */
2486 non_fatal (_("Unable to change endianness of input file(s)"));
2487 return FALSE;
2488 }
2489
2490 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2491 {
2492 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
2493 return FALSE;
2494 }
2495
2496 if (ibfd->sections == NULL)
2497 {
2498 non_fatal (_("error: the input file '%s' has no sections"),
2499 bfd_get_archive_filename (ibfd));
2500 return FALSE;
2501 }
2502
2503 if (ibfd->xvec->flavour != bfd_target_elf_flavour)
2504 {
2505 if ((do_debug_sections & compress) != 0
2506 && do_debug_sections != compress)
2507 {
2508 non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
2509 bfd_get_archive_filename (ibfd));
2510 return FALSE;
2511 }
2512
2513 if (do_elf_stt_common)
2514 {
2515 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
2516 bfd_get_archive_filename (ibfd));
2517 return FALSE;
2518 }
2519 }
2520
2521 if (verbose)
2522 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
2523 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
2524 bfd_get_filename (obfd), bfd_get_target (obfd));
2525
2526 if (extract_symbol)
2527 start = 0;
2528 else
2529 {
2530 if (set_start_set)
2531 start = set_start;
2532 else
2533 start = bfd_get_start_address (ibfd);
2534 start += change_start;
2535 }
2536
2537 /* Neither the start address nor the flags
2538 need to be set for a core file. */
2539 if (bfd_get_format (obfd) != bfd_core)
2540 {
2541 flagword flags;
2542
2543 flags = bfd_get_file_flags (ibfd);
2544 flags |= bfd_flags_to_set;
2545 flags &= ~bfd_flags_to_clear;
2546 flags &= bfd_applicable_file_flags (obfd);
2547
2548 if (strip_symbols == STRIP_ALL)
2549 flags &= ~HAS_RELOC;
2550
2551 if (!bfd_set_start_address (obfd, start)
2552 || !bfd_set_file_flags (obfd, flags))
2553 {
2554 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2555 return FALSE;
2556 }
2557 }
2558
2559 /* Copy architecture of input file to output file. */
2560 iarch = bfd_get_arch (ibfd);
2561 imach = bfd_get_mach (ibfd);
2562 if (input_arch)
2563 {
2564 if (bfd_get_arch_info (ibfd) == NULL
2565 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
2566 {
2567 iarch = input_arch->arch;
2568 imach = input_arch->mach;
2569 }
2570 else
2571 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
2572 bfd_get_archive_filename (ibfd));
2573 }
2574 if (!bfd_set_arch_mach (obfd, iarch, imach)
2575 && (ibfd->target_defaulted
2576 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
2577 {
2578 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
2579 non_fatal (_("Unable to recognise the format of the input file `%s'"),
2580 bfd_get_archive_filename (ibfd));
2581 else
2582 non_fatal (_("Output file cannot represent architecture `%s'"),
2583 bfd_printable_arch_mach (bfd_get_arch (ibfd),
2584 bfd_get_mach (ibfd)));
2585 return FALSE;
2586 }
2587
2588 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2589 {
2590 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2591 return FALSE;
2592 }
2593
2594 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2595 && bfd_pei_p (obfd))
2596 {
2597 /* Set up PE parameters. */
2598 pe_data_type *pe = pe_data (obfd);
2599
2600 /* Copy PE parameters before changing them. */
2601 if (ibfd->xvec->flavour == bfd_target_coff_flavour
2602 && bfd_pei_p (ibfd))
2603 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
2604
2605 if (pe_file_alignment != (bfd_vma) -1)
2606 pe->pe_opthdr.FileAlignment = pe_file_alignment;
2607 else
2608 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
2609
2610 if (pe_heap_commit != (bfd_vma) -1)
2611 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
2612
2613 if (pe_heap_reserve != (bfd_vma) -1)
2614 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
2615
2616 if (pe_image_base != (bfd_vma) -1)
2617 pe->pe_opthdr.ImageBase = pe_image_base;
2618
2619 if (pe_section_alignment != (bfd_vma) -1)
2620 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
2621 else
2622 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
2623
2624 if (pe_stack_commit != (bfd_vma) -1)
2625 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
2626
2627 if (pe_stack_reserve != (bfd_vma) -1)
2628 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
2629
2630 if (pe_subsystem != -1)
2631 pe->pe_opthdr.Subsystem = pe_subsystem;
2632
2633 if (pe_major_subsystem_version != -1)
2634 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
2635
2636 if (pe_minor_subsystem_version != -1)
2637 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
2638
2639 if (pe_file_alignment > pe_section_alignment)
2640 {
2641 char file_alignment[20], section_alignment[20];
2642
2643 sprintf_vma (file_alignment, pe_file_alignment);
2644 sprintf_vma (section_alignment, pe_section_alignment);
2645 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
2646
2647 file_alignment, section_alignment);
2648 }
2649 }
2650
2651 if (isympp)
2652 free (isympp);
2653
2654 if (osympp != isympp)
2655 free (osympp);
2656
2657 isympp = NULL;
2658 osympp = NULL;
2659
2660 symsize = bfd_get_symtab_upper_bound (ibfd);
2661 if (symsize < 0)
2662 {
2663 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2664 return FALSE;
2665 }
2666
2667 osympp = isympp = (asymbol **) xmalloc (symsize);
2668 symcount = bfd_canonicalize_symtab (ibfd, isympp);
2669 if (symcount < 0)
2670 {
2671 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
2672 return FALSE;
2673 }
2674 /* PR 17512: file: d6323821
2675 If the symbol table could not be loaded do not pretend that we have
2676 any symbols. This trips us up later on when we load the relocs. */
2677 if (symcount == 0)
2678 {
2679 free (isympp);
2680 osympp = isympp = NULL;
2681 }
2682
2683 /* BFD mandates that all output sections be created and sizes set before
2684 any output is done. Thus, we traverse all sections multiple times. */
2685 bfd_map_over_sections (ibfd, setup_section, obfd);
2686
2687 if (!extract_symbol)
2688 setup_bfd_headers (ibfd, obfd);
2689
2690 if (add_sections != NULL)
2691 {
2692 struct section_add *padd;
2693 struct section_list *pset;
2694
2695 for (padd = add_sections; padd != NULL; padd = padd->next)
2696 {
2697 flagword flags;
2698
2699 pset = find_section_list (padd->name, FALSE,
2700 SECTION_CONTEXT_SET_FLAGS);
2701 if (pset != NULL)
2702 flags = pset->flags | SEC_HAS_CONTENTS;
2703 else
2704 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
2705
2706 /* bfd_make_section_with_flags() does not return very helpful
2707 error codes, so check for the most likely user error first. */
2708 if (bfd_get_section_by_name (obfd, padd->name))
2709 {
2710 bfd_nonfatal_message (NULL, obfd, NULL,
2711 _("can't add section '%s'"), padd->name);
2712 return FALSE;
2713 }
2714 else
2715 {
2716 /* We use LINKER_CREATED here so that the backend hooks
2717 will create any special section type information,
2718 instead of presuming we know what we're doing merely
2719 because we set the flags. */
2720 padd->section = bfd_make_section_with_flags
2721 (obfd, padd->name, flags | SEC_LINKER_CREATED);
2722 if (padd->section == NULL)
2723 {
2724 bfd_nonfatal_message (NULL, obfd, NULL,
2725 _("can't create section `%s'"),
2726 padd->name);
2727 return FALSE;
2728 }
2729 }
2730
2731 if (! bfd_set_section_size (obfd, padd->section, padd->size))
2732 {
2733 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2734 return FALSE;
2735 }
2736
2737 pset = find_section_list (padd->name, FALSE,
2738 SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
2739 if (pset != NULL
2740 && ! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
2741 {
2742 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2743 return FALSE;
2744 }
2745
2746 pset = find_section_list (padd->name, FALSE,
2747 SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
2748 if (pset != NULL)
2749 {
2750 padd->section->lma = pset->lma_val;
2751
2752 if (! bfd_set_section_alignment
2753 (obfd, padd->section,
2754 bfd_section_alignment (obfd, padd->section)))
2755 {
2756 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2757 return FALSE;
2758 }
2759 }
2760 }
2761 }
2762
2763 if (update_sections != NULL)
2764 {
2765 struct section_add *pupdate;
2766
2767 for (pupdate = update_sections;
2768 pupdate != NULL;
2769 pupdate = pupdate->next)
2770 {
2771 pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name);
2772 if (pupdate->section == NULL)
2773 {
2774 non_fatal (_("error: %s not found, can't be updated"), pupdate->name);
2775 return FALSE;
2776 }
2777
2778 osec = pupdate->section->output_section;
2779 if (! bfd_set_section_size (obfd, osec, pupdate->size))
2780 {
2781 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2782 return FALSE;
2783 }
2784 }
2785 }
2786
2787 if (merge_notes)
2788 {
2789 /* This palaver is necessary because we must set the output
2790 section size first, before its contents are ready. */
2791 osec = bfd_get_section_by_name (ibfd, GNU_BUILD_ATTRS_SECTION_NAME);
2792 if (osec && is_merged_note_section (ibfd, osec))
2793 {
2794 bfd_size_type size;
2795
2796 size = bfd_get_section_size (osec);
2797 if (size == 0)
2798 {
2799 bfd_nonfatal_message (NULL, ibfd, osec, _("warning: note section is empty"));
2800 merge_notes = FALSE;
2801 }
2802 else if (! bfd_get_full_section_contents (ibfd, osec, & merged_notes))
2803 {
2804 bfd_nonfatal_message (NULL, ibfd, osec, _("warning: could not load note section"));
2805 free (merged_notes);
2806 merged_notes = NULL;
2807 merge_notes = FALSE;
2808 }
2809 else
2810 {
2811 merged_size = merge_gnu_build_notes (ibfd, osec, size, merged_notes);
2812 if (merged_size == size)
2813 {
2814 /* Merging achieves nothing. */
2815 free (merged_notes);
2816 merged_notes = NULL;
2817 merge_notes = FALSE;
2818 merged_size = 0;
2819 }
2820 else
2821 {
2822 if (osec->output_section == NULL
2823 || ! bfd_set_section_size (obfd, osec->output_section, merged_size))
2824 {
2825 bfd_nonfatal_message (NULL, obfd, osec, _("warning: failed to set merged notes size"));
2826 free (merged_notes);
2827 merged_notes = NULL;
2828 merge_notes = FALSE;
2829 merged_size = 0;
2830 }
2831 }
2832 }
2833 }
2834 }
2835
2836 if (dump_sections != NULL)
2837 {
2838 struct section_add * pdump;
2839
2840 for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
2841 {
2842 osec = bfd_get_section_by_name (ibfd, pdump->name);
2843 if (osec == NULL)
2844 {
2845 bfd_nonfatal_message (NULL, ibfd, NULL,
2846 _("can't dump section '%s' - it does not exist"),
2847 pdump->name);
2848 continue;
2849 }
2850
2851 if ((bfd_get_section_flags (ibfd, osec) & SEC_HAS_CONTENTS) == 0)
2852 {
2853 bfd_nonfatal_message (NULL, ibfd, osec,
2854 _("can't dump section - it has no contents"));
2855 continue;
2856 }
2857
2858 bfd_size_type size = bfd_get_section_size (osec);
2859 if (size == 0)
2860 {
2861 bfd_nonfatal_message (NULL, ibfd, osec,
2862 _("can't dump section - it is empty"));
2863 continue;
2864 }
2865
2866 FILE * f;
2867 f = fopen (pdump->filename, FOPEN_WB);
2868 if (f == NULL)
2869 {
2870 bfd_nonfatal_message (pdump->filename, NULL, NULL,
2871 _("could not open section dump file"));
2872 continue;
2873 }
2874
2875 bfd_byte *contents;
2876 if (bfd_malloc_and_get_section (ibfd, osec, &contents))
2877 {
2878 if (fwrite (contents, 1, size, f) != size)
2879 {
2880 non_fatal (_("error writing section contents to %s (error: %s)"),
2881 pdump->filename,
2882 strerror (errno));
2883 free (contents);
2884 fclose (f);
2885 return FALSE;
2886 }
2887 }
2888 else
2889 bfd_nonfatal_message (NULL, ibfd, osec,
2890 _("could not retrieve section contents"));
2891
2892 fclose (f);
2893 free (contents);
2894 }
2895 }
2896
2897 if (gnu_debuglink_filename != NULL)
2898 {
2899 /* PR 15125: Give a helpful warning message if
2900 the debuglink section already exists, and
2901 allow the rest of the copy to complete. */
2902 if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
2903 {
2904 non_fatal (_("%s: debuglink section already exists"),
2905 bfd_get_filename (obfd));
2906 gnu_debuglink_filename = NULL;
2907 }
2908 else
2909 {
2910 gnu_debuglink_section = bfd_create_gnu_debuglink_section
2911 (obfd, gnu_debuglink_filename);
2912
2913 if (gnu_debuglink_section == NULL)
2914 {
2915 bfd_nonfatal_message (NULL, obfd, NULL,
2916 _("cannot create debug link section `%s'"),
2917 gnu_debuglink_filename);
2918 return FALSE;
2919 }
2920
2921 /* Special processing for PE format files. We
2922 have no way to distinguish PE from COFF here. */
2923 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
2924 {
2925 bfd_vma debuglink_vma;
2926 asection * highest_section;
2927
2928 /* The PE spec requires that all sections be adjacent and sorted
2929 in ascending order of VMA. It also specifies that debug
2930 sections should be last. This is despite the fact that debug
2931 sections are not loaded into memory and so in theory have no
2932 use for a VMA.
2933
2934 This means that the debuglink section must be given a non-zero
2935 VMA which makes it contiguous with other debug sections. So
2936 walk the current section list, find the section with the
2937 highest VMA and start the debuglink section after that one. */
2938 for (osec = obfd->sections, highest_section = NULL;
2939 osec != NULL;
2940 osec = osec->next)
2941 if (osec->vma > 0
2942 && (highest_section == NULL
2943 || osec->vma > highest_section->vma))
2944 highest_section = osec;
2945
2946 if (highest_section)
2947 debuglink_vma = BFD_ALIGN (highest_section->vma
2948 + highest_section->size,
2949 /* FIXME: We ought to be using
2950 COFF_PAGE_SIZE here or maybe
2951 bfd_get_section_alignment() (if it
2952 was set) but since this is for PE
2953 and we know the required alignment
2954 it is easier just to hard code it. */
2955 0x1000);
2956 else
2957 /* Umm, not sure what to do in this case. */
2958 debuglink_vma = 0x1000;
2959
2960 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
2961 }
2962 }
2963 }
2964
2965 c = bfd_count_sections (obfd);
2966 if (c != 0
2967 && (gap_fill_set || pad_to_set))
2968 {
2969 asection **set;
2970
2971 /* We must fill in gaps between the sections and/or we must pad
2972 the last section to a specified address. We do this by
2973 grabbing a list of the sections, sorting them by VMA, and
2974 increasing the section sizes as required to fill the gaps.
2975 We write out the gap contents below. */
2976
2977 osections = (asection **) xmalloc (c * sizeof (asection *));
2978 set = osections;
2979 bfd_map_over_sections (obfd, get_sections, &set);
2980
2981 qsort (osections, c, sizeof (asection *), compare_section_lma);
2982
2983 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
2984 memset (gaps, 0, c * sizeof (bfd_size_type));
2985
2986 if (gap_fill_set)
2987 {
2988 for (i = 0; i < c - 1; i++)
2989 {
2990 flagword flags;
2991 bfd_size_type size;
2992 bfd_vma gap_start, gap_stop;
2993
2994 flags = bfd_get_section_flags (obfd, osections[i]);
2995 if ((flags & SEC_HAS_CONTENTS) == 0
2996 || (flags & SEC_LOAD) == 0)
2997 continue;
2998
2999 size = bfd_section_size (obfd, osections[i]);
3000 gap_start = bfd_section_lma (obfd, osections[i]) + size;
3001 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
3002 if (gap_start < gap_stop)
3003 {
3004 if (! bfd_set_section_size (obfd, osections[i],
3005 size + (gap_stop - gap_start)))
3006 {
3007 bfd_nonfatal_message (NULL, obfd, osections[i],
3008 _("Can't fill gap after section"));
3009 status = 1;
3010 break;
3011 }
3012 gaps[i] = gap_stop - gap_start;
3013 if (max_gap < gap_stop - gap_start)
3014 max_gap = gap_stop - gap_start;
3015 }
3016 }
3017 }
3018
3019 if (pad_to_set)
3020 {
3021 bfd_vma lma;
3022 bfd_size_type size;
3023
3024 lma = bfd_section_lma (obfd, osections[c - 1]);
3025 size = bfd_section_size (obfd, osections[c - 1]);
3026 if (lma + size < pad_to)
3027 {
3028 if (! bfd_set_section_size (obfd, osections[c - 1],
3029 pad_to - lma))
3030 {
3031 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
3032 _("can't add padding"));
3033 status = 1;
3034 }
3035 else
3036 {
3037 gaps[c - 1] = pad_to - (lma + size);
3038 if (max_gap < pad_to - (lma + size))
3039 max_gap = pad_to - (lma + size);
3040 }
3041 }
3042 }
3043 }
3044
3045 /* Symbol filtering must happen after the output sections
3046 have been created, but before their contents are set. */
3047 dhandle = NULL;
3048 if (convert_debugging)
3049 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
3050
3051 if (strip_symbols == STRIP_DEBUG
3052 || strip_symbols == STRIP_ALL
3053 || strip_symbols == STRIP_UNNEEDED
3054 || strip_symbols == STRIP_NONDEBUG
3055 || strip_symbols == STRIP_DWO
3056 || strip_symbols == STRIP_NONDWO
3057 || discard_locals != LOCALS_UNDEF
3058 || localize_hidden
3059 || htab_elements (strip_specific_htab) != 0
3060 || htab_elements (keep_specific_htab) != 0
3061 || htab_elements (localize_specific_htab) != 0
3062 || htab_elements (globalize_specific_htab) != 0
3063 || htab_elements (keepglobal_specific_htab) != 0
3064 || htab_elements (weaken_specific_htab) != 0
3065 || htab_elements (redefine_specific_htab) != 0
3066 || prefix_symbols_string
3067 || sections_removed
3068 || sections_copied
3069 || convert_debugging
3070 || change_leading_char
3071 || remove_leading_char
3072 || section_rename_list
3073 || weaken
3074 || add_symbols)
3075 {
3076 /* Mark symbols used in output relocations so that they
3077 are kept, even if they are local labels or static symbols.
3078
3079 Note we iterate over the input sections examining their
3080 relocations since the relocations for the output sections
3081 haven't been set yet. mark_symbols_used_in_relocations will
3082 ignore input sections which have no corresponding output
3083 section. */
3084 if (strip_symbols != STRIP_ALL)
3085 {
3086 bfd_set_error (bfd_error_no_error);
3087 bfd_map_over_sections (ibfd,
3088 mark_symbols_used_in_relocations,
3089 isympp);
3090 if (bfd_get_error () != bfd_error_no_error)
3091 {
3092 status = 1;
3093 return FALSE;
3094 }
3095 }
3096
3097 osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *));
3098 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
3099 }
3100
3101 if (convert_debugging && dhandle != NULL)
3102 {
3103 bfd_boolean res;
3104
3105 res = write_debugging_info (obfd, dhandle, &symcount, &osympp);
3106
3107 free (dhandle);
3108 dhandle = NULL; /* Paranoia... */
3109
3110 if (! res)
3111 {
3112 status = 1;
3113 return FALSE;
3114 }
3115 }
3116
3117 bfd_set_symtab (obfd, osympp, symcount);
3118
3119 /* This has to happen before section positions are set. */
3120 bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
3121
3122 /* This has to happen after the symbol table has been set. */
3123 bfd_map_over_sections (ibfd, copy_section, obfd);
3124
3125 if (add_sections != NULL)
3126 {
3127 struct section_add *padd;
3128
3129 for (padd = add_sections; padd != NULL; padd = padd->next)
3130 {
3131 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
3132 0, padd->size))
3133 {
3134 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
3135 return FALSE;
3136 }
3137 }
3138 }
3139
3140 if (update_sections != NULL)
3141 {
3142 struct section_add *pupdate;
3143
3144 for (pupdate = update_sections;
3145 pupdate != NULL;
3146 pupdate = pupdate->next)
3147 {
3148 osec = pupdate->section->output_section;
3149 if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
3150 0, pupdate->size))
3151 {
3152 bfd_nonfatal_message (NULL, obfd, osec, NULL);
3153 return FALSE;
3154 }
3155 }
3156 }
3157
3158 if (merge_notes)
3159 {
3160 osec = bfd_get_section_by_name (obfd, GNU_BUILD_ATTRS_SECTION_NAME);
3161 if (osec && is_merged_note_section (obfd, osec))
3162 {
3163 if (! bfd_set_section_contents (obfd, osec, merged_notes, 0, merged_size))
3164 {
3165 bfd_nonfatal_message (NULL, obfd, osec, _("error: failed to copy merged notes into output"));
3166 return FALSE;
3167 }
3168 }
3169 else if (! is_strip)
3170 bfd_nonfatal_message (NULL, obfd, osec, _("could not find any mergeable note sections"));
3171 free (merged_notes);
3172 merged_notes = NULL;
3173 merge_notes = FALSE;
3174 }
3175
3176 if (gnu_debuglink_filename != NULL)
3177 {
3178 if (! bfd_fill_in_gnu_debuglink_section
3179 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
3180 {
3181 bfd_nonfatal_message (NULL, obfd, NULL,
3182 _("cannot fill debug link section `%s'"),
3183 gnu_debuglink_filename);
3184 return FALSE;
3185 }
3186 }
3187
3188 if (gap_fill_set || pad_to_set)
3189 {
3190 bfd_byte *buf;
3191
3192 /* Fill in the gaps. */
3193 if (max_gap > 8192)
3194 max_gap = 8192;
3195 buf = (bfd_byte *) xmalloc (max_gap);
3196 memset (buf, gap_fill, max_gap);
3197
3198 c = bfd_count_sections (obfd);
3199 for (i = 0; i < c; i++)
3200 {
3201 if (gaps[i] != 0)
3202 {
3203 bfd_size_type left;
3204 file_ptr off;
3205
3206 left = gaps[i];
3207 off = bfd_section_size (obfd, osections[i]) - left;
3208
3209 while (left > 0)
3210 {
3211 bfd_size_type now;
3212
3213 if (left > 8192)
3214 now = 8192;
3215 else
3216 now = left;
3217
3218 if (! bfd_set_section_contents (obfd, osections[i], buf,
3219 off, now))
3220 {
3221 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
3222 free (buf);
3223 return FALSE;
3224 }
3225
3226 left -= now;
3227 off += now;
3228 }
3229 }
3230 }
3231
3232 free (buf);
3233 free (gaps);
3234 gaps = NULL;
3235 }
3236
3237 /* Allow the BFD backend to copy any private data it understands
3238 from the input BFD to the output BFD. This is done last to
3239 permit the routine to look at the filtered symbol table, which is
3240 important for the ECOFF code at least. */
3241 if (! bfd_copy_private_bfd_data (ibfd, obfd))
3242 {
3243 bfd_nonfatal_message (NULL, obfd, NULL,
3244 _("error copying private BFD data"));
3245 return FALSE;
3246 }
3247
3248 /* Switch to the alternate machine code. We have to do this at the
3249 very end, because we only initialize the header when we create
3250 the first section. */
3251 if (use_alt_mach_code != 0)
3252 {
3253 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
3254 {
3255 non_fatal (_("this target does not support %lu alternative machine codes"),
3256 use_alt_mach_code);
3257 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
3258 {
3259 non_fatal (_("treating that number as an absolute e_machine value instead"));
3260 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
3261 }
3262 else
3263 non_fatal (_("ignoring the alternative value"));
3264 }
3265 }
3266
3267 return TRUE;
3268 }
3269
3270 /* Read each archive element in turn from IBFD, copy the
3271 contents to temp file, and keep the temp file handle.
3272 If 'force_output_target' is TRUE then make sure that
3273 all elements in the new archive are of the type
3274 'output_target'. */
3275
3276 static void
3277 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
3278 bfd_boolean force_output_target,
3279 const bfd_arch_info_type *input_arch)
3280 {
3281 struct name_list
3282 {
3283 struct name_list *next;
3284 const char *name;
3285 bfd *obfd;
3286 } *list, *l;
3287 bfd **ptr = &obfd->archive_head;
3288 bfd *this_element;
3289 char *dir;
3290 const char *filename;
3291
3292 /* PR 24281: It is not clear what should happen when copying a thin archive.
3293 One part is straight forward - if the output archive is in a different
3294 directory from the input archive then any relative paths in the library
3295 should be adjusted to the new location. But if any transformation
3296 options are active (eg strip, rename, add, etc) then the implication is
3297 that these should be applied to the files pointed to by the archive.
3298 But since objcopy is not destructive, this means that new files must be
3299 created, and there is no guidance for the names of the new files. (Plus
3300 this conflicts with one of the goals of thin libraries - only taking up
3301 a minimal amount of space in the file system).
3302
3303 So for now we fail if an attempt is made to copy such libraries. */
3304 if (ibfd->is_thin_archive)
3305 {
3306 status = 1;
3307 bfd_set_error (bfd_error_invalid_operation);
3308 bfd_nonfatal_message (NULL, ibfd, NULL,
3309 _("sorry: copying thin archives is not currently supported"));
3310 return;
3311 }
3312
3313 /* Make a temp directory to hold the contents. */
3314 dir = make_tempdir (bfd_get_filename (obfd));
3315 if (dir == NULL)
3316 fatal (_("cannot create tempdir for archive copying (error: %s)"),
3317 strerror (errno));
3318
3319 if (strip_symbols == STRIP_ALL)
3320 obfd->has_armap = FALSE;
3321 else
3322 obfd->has_armap = ibfd->has_armap;
3323 obfd->is_thin_archive = ibfd->is_thin_archive;
3324
3325 if (deterministic)
3326 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
3327
3328 list = NULL;
3329
3330 this_element = bfd_openr_next_archived_file (ibfd, NULL);
3331
3332 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
3333 {
3334 status = 1;
3335 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
3336 goto cleanup_and_exit;
3337 }
3338
3339 while (!status && this_element != NULL)
3340 {
3341 char *output_name;
3342 bfd *output_bfd;
3343 bfd *last_element;
3344 struct stat buf;
3345 int stat_status = 0;
3346 bfd_boolean del = TRUE;
3347 bfd_boolean ok_object;
3348
3349 /* PR binutils/17533: Do not allow directory traversal
3350 outside of the current directory tree by archive members. */
3351 if (! is_valid_archive_path (bfd_get_filename (this_element)))
3352 {
3353 non_fatal (_("illegal pathname found in archive member: %s"),
3354 bfd_get_filename (this_element));
3355 status = 1;
3356 goto cleanup_and_exit;
3357 }
3358
3359 /* Create an output file for this member. */
3360 output_name = concat (dir, "/",
3361 bfd_get_filename (this_element), (char *) 0);
3362
3363 /* If the file already exists, make another temp dir. */
3364 if (stat (output_name, &buf) >= 0)
3365 {
3366 char * tmpdir = make_tempdir (output_name);
3367
3368 free (output_name);
3369 if (tmpdir == NULL)
3370 {
3371 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
3372 strerror (errno));
3373 status = 1;
3374 goto cleanup_and_exit;
3375 }
3376
3377 l = (struct name_list *) xmalloc (sizeof (struct name_list));
3378 l->name = tmpdir;
3379 l->next = list;
3380 l->obfd = NULL;
3381 list = l;
3382 output_name = concat (tmpdir, "/",
3383 bfd_get_filename (this_element), (char *) 0);
3384 }
3385
3386 if (preserve_dates)
3387 {
3388 stat_status = bfd_stat_arch_elt (this_element, &buf);
3389
3390 if (stat_status != 0)
3391 non_fatal (_("internal stat error on %s"),
3392 bfd_get_filename (this_element));
3393 }
3394
3395 l = (struct name_list *) xmalloc (sizeof (struct name_list));
3396 l->name = output_name;
3397 l->next = list;
3398 l->obfd = NULL;
3399 list = l;
3400
3401 ok_object = bfd_check_format (this_element, bfd_object);
3402 if (!ok_object)
3403 bfd_nonfatal_message (NULL, this_element, NULL,
3404 _("Unable to recognise the format of file"));
3405
3406 /* PR binutils/3110: Cope with archives
3407 containing multiple target types. */
3408 if (force_output_target || !ok_object)
3409 output_bfd = bfd_openw (output_name, output_target);
3410 else
3411 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
3412
3413 if (output_bfd == NULL)
3414 {
3415 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
3416 status = 1;
3417 goto cleanup_and_exit;
3418 }
3419
3420 if (ok_object)
3421 {
3422 del = !copy_object (this_element, output_bfd, input_arch);
3423
3424 if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
3425 /* Try again as an unknown object file. */
3426 ok_object = FALSE;
3427 else if (!bfd_close (output_bfd))
3428 {
3429 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
3430 /* Error in new object file. Don't change archive. */
3431 status = 1;
3432 }
3433 }
3434
3435 if (!ok_object)
3436 {
3437 del = !copy_unknown_object (this_element, output_bfd);
3438 if (!bfd_close_all_done (output_bfd))
3439 {
3440 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
3441 /* Error in new object file. Don't change archive. */
3442 status = 1;
3443 }
3444 }
3445
3446 if (del)
3447 {
3448 unlink (output_name);
3449 status = 1;
3450 }
3451 else
3452 {
3453 if (preserve_dates && stat_status == 0)
3454 set_times (output_name, &buf);
3455
3456 /* Open the newly output file and attach to our list. */
3457 output_bfd = bfd_openr (output_name, output_target);
3458
3459 l->obfd = output_bfd;
3460
3461 *ptr = output_bfd;
3462 ptr = &output_bfd->archive_next;
3463
3464 last_element = this_element;
3465
3466 this_element = bfd_openr_next_archived_file (ibfd, last_element);
3467
3468 bfd_close (last_element);
3469 }
3470 }
3471 *ptr = NULL;
3472
3473 filename = bfd_get_filename (obfd);
3474 if (!bfd_close (obfd))
3475 {
3476 status = 1;
3477 bfd_nonfatal_message (filename, NULL, NULL, NULL);
3478 }
3479
3480 filename = bfd_get_filename (ibfd);
3481 if (!bfd_close (ibfd))
3482 {
3483 status = 1;
3484 bfd_nonfatal_message (filename, NULL, NULL, NULL);
3485 }
3486
3487 cleanup_and_exit:
3488 /* Delete all the files that we opened. */
3489 {
3490 struct name_list * next;
3491
3492 for (l = list; l != NULL; l = next)
3493 {
3494 if (l->obfd == NULL)
3495 rmdir (l->name);
3496 else
3497 {
3498 bfd_close (l->obfd);
3499 unlink (l->name);
3500 }
3501 next = l->next;
3502 free (l);
3503 }
3504 }
3505
3506 rmdir (dir);
3507 }
3508
3509 static void
3510 set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
3511 {
3512 /* This is only relevant to Coff targets. */
3513 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
3514 {
3515 if (style == KEEP
3516 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
3517 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
3518 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
3519 }
3520 }
3521
3522 /* The top-level control. */
3523
3524 static void
3525 copy_file (const char *input_filename, const char *output_filename,
3526 const char *input_target, const char *output_target,
3527 const bfd_arch_info_type *input_arch)
3528 {
3529 bfd *ibfd;
3530 char **obj_matching;
3531 char **core_matching;
3532 off_t size = get_file_size (input_filename);
3533
3534 if (size < 1)
3535 {
3536 if (size == 0)
3537 non_fatal (_("error: the input file '%s' is empty"),
3538 input_filename);
3539 status = 1;
3540 return;
3541 }
3542
3543 /* To allow us to do "strip *" without dying on the first
3544 non-object file, failures are nonfatal. */
3545 ibfd = bfd_openr (input_filename, input_target);
3546 if (ibfd == NULL)
3547 {
3548 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
3549 status = 1;
3550 return;
3551 }
3552
3553 switch (do_debug_sections)
3554 {
3555 case compress:
3556 case compress_zlib:
3557 case compress_gnu_zlib:
3558 case compress_gabi_zlib:
3559 ibfd->flags |= BFD_COMPRESS;
3560 /* Don't check if input is ELF here since this information is
3561 only available after bfd_check_format_matches is called. */
3562 if (do_debug_sections != compress_gnu_zlib)
3563 ibfd->flags |= BFD_COMPRESS_GABI;
3564 break;
3565 case decompress:
3566 ibfd->flags |= BFD_DECOMPRESS;
3567 break;
3568 default:
3569 break;
3570 }
3571
3572 switch (do_elf_stt_common)
3573 {
3574 case elf_stt_common:
3575 ibfd->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
3576 break;
3577 break;
3578 case no_elf_stt_common:
3579 ibfd->flags |= BFD_CONVERT_ELF_COMMON;
3580 break;
3581 default:
3582 break;
3583 }
3584
3585 if (bfd_check_format (ibfd, bfd_archive))
3586 {
3587 bfd_boolean force_output_target;
3588 bfd *obfd;
3589
3590 /* bfd_get_target does not return the correct value until
3591 bfd_check_format succeeds. */
3592 if (output_target == NULL)
3593 {
3594 output_target = bfd_get_target (ibfd);
3595 force_output_target = FALSE;
3596 }
3597 else
3598 force_output_target = TRUE;
3599
3600 obfd = bfd_openw (output_filename, output_target);
3601 if (obfd == NULL)
3602 {
3603 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
3604 status = 1;
3605 return;
3606 }
3607 /* This is a no-op on non-Coff targets. */
3608 set_long_section_mode (obfd, ibfd, long_section_names);
3609
3610 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
3611 }
3612 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
3613 {
3614 bfd *obfd;
3615 do_copy:
3616
3617 /* bfd_get_target does not return the correct value until
3618 bfd_check_format succeeds. */
3619 if (output_target == NULL)
3620 output_target = bfd_get_target (ibfd);
3621
3622 obfd = bfd_openw (output_filename, output_target);
3623 if (obfd == NULL)
3624 {
3625 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
3626 status = 1;
3627 return;
3628 }
3629 /* This is a no-op on non-Coff targets. */
3630 set_long_section_mode (obfd, ibfd, long_section_names);
3631
3632 if (! copy_object (ibfd, obfd, input_arch))
3633 status = 1;
3634
3635 /* PR 17512: file: 0f15796a.
3636 If the file could not be copied it may not be in a writeable
3637 state. So use bfd_close_all_done to avoid the possibility of
3638 writing uninitialised data into the file. */
3639 if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd)))
3640 {
3641 status = 1;
3642 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
3643 return;
3644 }
3645
3646 if (!bfd_close (ibfd))
3647 {
3648 status = 1;
3649 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
3650 return;
3651 }
3652 }
3653 else
3654 {
3655 bfd_error_type obj_error = bfd_get_error ();
3656 bfd_error_type core_error;
3657
3658 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
3659 {
3660 /* This probably can't happen.. */
3661 if (obj_error == bfd_error_file_ambiguously_recognized)
3662 free (obj_matching);
3663 goto do_copy;
3664 }
3665
3666 core_error = bfd_get_error ();
3667 /* Report the object error in preference to the core error. */
3668 if (obj_error != core_error)
3669 bfd_set_error (obj_error);
3670
3671 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
3672
3673 if (obj_error == bfd_error_file_ambiguously_recognized)
3674 {
3675 list_matching_formats (obj_matching);
3676 free (obj_matching);
3677 }
3678 if (core_error == bfd_error_file_ambiguously_recognized)
3679 {
3680 list_matching_formats (core_matching);
3681 free (core_matching);
3682 }
3683
3684 status = 1;
3685 }
3686 }
3687
3688 /* Add a name to the section renaming list. */
3689
3690 static void
3691 add_section_rename (const char * old_name, const char * new_name,
3692 flagword flags)
3693 {
3694 section_rename * srename;
3695
3696 /* Check for conflicts first. */
3697 for (srename = section_rename_list; srename != NULL; srename = srename->next)
3698 if (strcmp (srename->old_name, old_name) == 0)
3699 {
3700 /* Silently ignore duplicate definitions. */
3701 if (strcmp (srename->new_name, new_name) == 0
3702 && srename->flags == flags)
3703 return;
3704
3705 fatal (_("Multiple renames of section %s"), old_name);
3706 }
3707
3708 srename = (section_rename *) xmalloc (sizeof (* srename));
3709
3710 srename->old_name = old_name;
3711 srename->new_name = new_name;
3712 srename->flags = flags;
3713 srename->next = section_rename_list;
3714
3715 section_rename_list = srename;
3716 }
3717
3718 /* Check the section rename list for a new name of the input section
3719 called OLD_NAME. Returns the new name if one is found and sets
3720 RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
3721
3722 static const char *
3723 find_section_rename (const char *old_name, flagword *returned_flags)
3724 {
3725 const section_rename *srename;
3726
3727 for (srename = section_rename_list; srename != NULL; srename = srename->next)
3728 if (strcmp (srename->old_name, old_name) == 0)
3729 {
3730 if (returned_flags != NULL && srename->flags != (flagword) -1)
3731 *returned_flags = srename->flags;
3732
3733 return srename->new_name;
3734 }
3735
3736 return old_name;
3737 }
3738
3739 /* Once each of the sections is copied, we may still need to do some
3740 finalization work for private section headers. Do that here. */
3741
3742 static void
3743 setup_bfd_headers (bfd *ibfd, bfd *obfd)
3744 {
3745 /* Allow the BFD backend to copy any private data it understands
3746 from the input section to the output section. */
3747 if (! bfd_copy_private_header_data (ibfd, obfd))
3748 {
3749 status = 1;
3750 bfd_nonfatal_message (NULL, ibfd, NULL,
3751 _("error in private header data"));
3752 return;
3753 }
3754
3755 /* All went well. */
3756 return;
3757 }
3758
3759 /* Create a section in OBFD with the same
3760 name and attributes as ISECTION in IBFD. */
3761
3762 static void
3763 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3764 {
3765 bfd *obfd = (bfd *) obfdarg;
3766 struct section_list *p;
3767 sec_ptr osection;
3768 bfd_size_type size;
3769 bfd_vma vma;
3770 bfd_vma lma;
3771 flagword flags;
3772 const char *err;
3773 const char * name;
3774 char *prefix = NULL;
3775 bfd_boolean make_nobits;
3776 unsigned int alignment;
3777
3778 if (is_strip_section (ibfd, isection))
3779 return;
3780
3781 /* Get the, possibly new, name of the output section. */
3782 name = bfd_section_name (ibfd, isection);
3783 flags = bfd_get_section_flags (ibfd, isection);
3784 name = find_section_rename (name, &flags);
3785
3786 /* Prefix sections. */
3787 if ((prefix_alloc_sections_string)
3788 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
3789 prefix = prefix_alloc_sections_string;
3790 else if (prefix_sections_string)
3791 prefix = prefix_sections_string;
3792
3793 if (prefix)
3794 {
3795 char *n;
3796
3797 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
3798 strcpy (n, prefix);
3799 strcat (n, name);
3800 name = n;
3801 }
3802
3803 make_nobits = FALSE;
3804
3805 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3806 SECTION_CONTEXT_SET_FLAGS);
3807 if (p != NULL)
3808 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
3809 else if (strip_symbols == STRIP_NONDEBUG
3810 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
3811 && !is_nondebug_keep_contents_section (ibfd, isection))
3812 {
3813 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3814 if (obfd->xvec->flavour == bfd_target_elf_flavour)
3815 {
3816 make_nobits = TRUE;
3817
3818 /* Twiddle the input section flags so that it seems to
3819 elf.c:copy_private_bfd_data that section flags have not
3820 changed between input and output sections. This hack
3821 prevents wholesale rewriting of the program headers. */
3822 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
3823 }
3824 }
3825
3826 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
3827
3828 if (osection == NULL)
3829 {
3830 err = _("failed to create output section");
3831 goto loser;
3832 }
3833
3834 if (make_nobits)
3835 elf_section_type (osection) = SHT_NOBITS;
3836
3837 size = bfd_section_size (ibfd, isection);
3838 size = bfd_convert_section_size (ibfd, isection, obfd, size);
3839 if (copy_byte >= 0)
3840 size = (size + interleave - 1) / interleave * copy_width;
3841 else if (extract_symbol)
3842 size = 0;
3843 if (! bfd_set_section_size (obfd, osection, size))
3844 {
3845 err = _("failed to set size");
3846 goto loser;
3847 }
3848
3849 vma = bfd_section_vma (ibfd, isection);
3850 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3851 SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
3852 if (p != NULL)
3853 {
3854 if (p->context & SECTION_CONTEXT_SET_VMA)
3855 vma = p->vma_val;
3856 else
3857 vma += p->vma_val;
3858 }
3859 else
3860 vma += change_section_address;
3861
3862 if (! bfd_set_section_vma (obfd, osection, vma))
3863 {
3864 err = _("failed to set vma");
3865 goto loser;
3866 }
3867
3868 lma = isection->lma;
3869 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3870 SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
3871 if (p != NULL)
3872 {
3873 if (p->context & SECTION_CONTEXT_ALTER_LMA)
3874 lma += p->lma_val;
3875 else
3876 lma = p->lma_val;
3877 }
3878 else
3879 lma += change_section_address;
3880
3881 osection->lma = lma;
3882
3883 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3884 SECTION_CONTEXT_SET_ALIGNMENT);
3885 if (p != NULL)
3886 alignment = p->alignment;
3887 else
3888 alignment = bfd_section_alignment (ibfd, isection);
3889
3890 /* FIXME: This is probably not enough. If we change the LMA we
3891 may have to recompute the header for the file as well. */
3892 if (!bfd_set_section_alignment (obfd,
3893 osection,
3894 alignment))
3895 {
3896 err = _("failed to set alignment");
3897 goto loser;
3898 }
3899
3900 /* Copy merge entity size. */
3901 osection->entsize = isection->entsize;
3902
3903 /* Copy compress status. */
3904 osection->compress_status = isection->compress_status;
3905
3906 /* This used to be mangle_section; we do here to avoid using
3907 bfd_get_section_by_name since some formats allow multiple
3908 sections with the same name. */
3909 isection->output_section = osection;
3910 isection->output_offset = 0;
3911
3912 if ((isection->flags & SEC_GROUP) != 0)
3913 {
3914 asymbol *gsym = group_signature (isection);
3915
3916 if (gsym != NULL)
3917 {
3918 gsym->flags |= BSF_KEEP;
3919 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
3920 elf_group_id (isection) = gsym;
3921 }
3922 }
3923
3924 /* Allow the BFD backend to copy any private data it understands
3925 from the input section to the output section. */
3926 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
3927 {
3928 err = _("failed to copy private data");
3929 goto loser;
3930 }
3931
3932 /* All went well. */
3933 return;
3934
3935 loser:
3936 status = 1;
3937 bfd_nonfatal_message (NULL, obfd, osection, err);
3938 }
3939
3940 /* Return TRUE if input section ISECTION should be skipped. */
3941
3942 static bfd_boolean
3943 skip_section (bfd *ibfd, sec_ptr isection, bfd_boolean skip_copy)
3944 {
3945 sec_ptr osection;
3946 bfd_size_type size;
3947 flagword flags;
3948
3949 /* If we have already failed earlier on,
3950 do not keep on generating complaints now. */
3951 if (status != 0)
3952 return TRUE;
3953
3954 if (extract_symbol)
3955 return TRUE;
3956
3957 if (is_strip_section (ibfd, isection))
3958 return TRUE;
3959
3960 if (is_update_section (ibfd, isection))
3961 return TRUE;
3962
3963 /* When merging a note section we skip the copying of the contents,
3964 but not the copying of the relocs associated with the contents. */
3965 if (skip_copy && is_merged_note_section (ibfd, isection))
3966 return TRUE;
3967
3968 flags = bfd_get_section_flags (ibfd, isection);
3969 if ((flags & SEC_GROUP) != 0)
3970 return TRUE;
3971
3972 osection = isection->output_section;
3973 size = bfd_get_section_size (isection);
3974
3975 if (size == 0 || osection == 0)
3976 return TRUE;
3977
3978 return FALSE;
3979 }
3980
3981 /* Add section SECTION_PATTERN to the list of sections that will have their
3982 relocations removed. */
3983
3984 static void
3985 handle_remove_relocations_option (const char *section_pattern)
3986 {
3987 find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE_RELOCS);
3988 }
3989
3990 /* Return TRUE if ISECTION from IBFD should have its relocations removed,
3991 otherwise return FALSE. If the user has requested that relocations be
3992 removed from a section that does not have relocations then this
3993 function will still return TRUE. */
3994
3995 static bfd_boolean
3996 discard_relocations (bfd *ibfd ATTRIBUTE_UNUSED, asection *isection)
3997 {
3998 return (find_section_list (bfd_section_name (ibfd, isection), FALSE,
3999 SECTION_CONTEXT_REMOVE_RELOCS) != NULL);
4000 }
4001
4002 /* Wrapper for dealing with --remove-section (-R) command line arguments.
4003 A special case is detected here, if the user asks to remove a relocation
4004 section (one starting with ".rela" or ".rel") then this removal must
4005 be done using a different technique in a relocatable object. */
4006
4007 static void
4008 handle_remove_section_option (const char *section_pattern)
4009 {
4010 find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE);
4011 if (strncmp (section_pattern, ".rel", 4) == 0)
4012 {
4013 section_pattern += 4;
4014 if (*section_pattern == 'a')
4015 section_pattern++;
4016 if (*section_pattern)
4017 handle_remove_relocations_option (section_pattern);
4018 }
4019 sections_removed = TRUE;
4020 }
4021
4022 /* Copy relocations in input section ISECTION of IBFD to an output
4023 section with the same name in OBFDARG. If stripping then don't
4024 copy any relocation info. */
4025
4026 static void
4027 copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
4028 {
4029 bfd *obfd = (bfd *) obfdarg;
4030 long relsize;
4031 arelent **relpp;
4032 long relcount;
4033 sec_ptr osection;
4034
4035 if (skip_section (ibfd, isection, FALSE))
4036 return;
4037
4038 osection = isection->output_section;
4039
4040 /* Core files and DWO files do not need to be relocated. */
4041 if (bfd_get_format (obfd) == bfd_core
4042 || strip_symbols == STRIP_NONDWO
4043 || discard_relocations (ibfd, isection))
4044 relsize = 0;
4045 else
4046 {
4047 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
4048
4049 if (relsize < 0)
4050 {
4051 /* Do not complain if the target does not support relocations. */
4052 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
4053 relsize = 0;
4054 else
4055 {
4056 status = 1;
4057 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
4058 return;
4059 }
4060 }
4061 }
4062
4063 if (relsize == 0)
4064 {
4065 bfd_set_reloc (obfd, osection, NULL, 0);
4066 osection->flags &= ~SEC_RELOC;
4067 }
4068 else
4069 {
4070 if (isection->orelocation != NULL)
4071 {
4072 /* Some other function has already set up the output relocs
4073 for us, so scan those instead of the default relocs. */
4074 relcount = isection->reloc_count;
4075 relpp = isection->orelocation;
4076 }
4077 else
4078 {
4079 relpp = (arelent **) xmalloc (relsize);
4080 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
4081 if (relcount < 0)
4082 {
4083 status = 1;
4084 bfd_nonfatal_message (NULL, ibfd, isection,
4085 _("relocation count is negative"));
4086 return;
4087 }
4088 }
4089
4090 if (strip_symbols == STRIP_ALL)
4091 {
4092 /* Remove relocations which are not in
4093 keep_strip_specific_list. */
4094 arelent **temp_relpp;
4095 long temp_relcount = 0;
4096 long i;
4097
4098 temp_relpp = (arelent **) xmalloc (relsize);
4099 for (i = 0; i < relcount; i++)
4100 {
4101 /* PR 17512: file: 9e907e0c. */
4102 if (relpp[i]->sym_ptr_ptr
4103 /* PR 20096 */
4104 && * relpp[i]->sym_ptr_ptr)
4105 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
4106 keep_specific_htab))
4107 temp_relpp [temp_relcount++] = relpp [i];
4108 }
4109 relcount = temp_relcount;
4110 if (isection->orelocation == NULL)
4111 free (relpp);
4112 relpp = temp_relpp;
4113 }
4114
4115 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
4116 if (relcount == 0)
4117 {
4118 osection->flags &= ~SEC_RELOC;
4119 free (relpp);
4120 }
4121 }
4122 }
4123
4124 /* Copy the data of input section ISECTION of IBFD
4125 to an output section with the same name in OBFD. */
4126
4127 static void
4128 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
4129 {
4130 bfd *obfd = (bfd *) obfdarg;
4131 struct section_list *p;
4132 sec_ptr osection;
4133 bfd_size_type size;
4134
4135 if (skip_section (ibfd, isection, TRUE))
4136 return;
4137
4138 osection = isection->output_section;
4139 /* The output SHF_COMPRESSED section size is different from input if
4140 ELF classes of input and output aren't the same. We can't use
4141 the output section size since --interleave will shrink the output
4142 section. Size will be updated if the section is converted. */
4143 size = bfd_get_section_size (isection);
4144
4145 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
4146 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
4147 {
4148 bfd_byte *memhunk = NULL;
4149
4150 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)
4151 || !bfd_convert_section_contents (ibfd, isection, obfd,
4152 &memhunk, &size))
4153 {
4154 status = 1;
4155 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
4156 free (memhunk);
4157 return;
4158 }
4159
4160 if (reverse_bytes)
4161 {
4162 /* We don't handle leftover bytes (too many possible behaviors,
4163 and we don't know what the user wants). The section length
4164 must be a multiple of the number of bytes to swap. */
4165 if ((size % reverse_bytes) == 0)
4166 {
4167 unsigned long i, j;
4168 bfd_byte b;
4169
4170 for (i = 0; i < size; i += reverse_bytes)
4171 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
4172 {
4173 bfd_byte *m = (bfd_byte *) memhunk;
4174
4175 b = m[i + j];
4176 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
4177 m[(i + reverse_bytes) - (j + 1)] = b;
4178 }
4179 }
4180 else
4181 /* User must pad the section up in order to do this. */
4182 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
4183 bfd_section_name (ibfd, isection), reverse_bytes);
4184 }
4185
4186 if (copy_byte >= 0)
4187 {
4188 /* Keep only every `copy_byte'th byte in MEMHUNK. */
4189 char *from = (char *) memhunk + copy_byte;
4190 char *to = (char *) memhunk;
4191 char *end = (char *) memhunk + size;
4192 int i;
4193
4194 /* If the section address is not exactly divisible by the interleave,
4195 then we must bias the from address. If the copy_byte is less than
4196 the bias, then we must skip forward one interleave, and increment
4197 the final lma. */
4198 int extra = isection->lma % interleave;
4199 from -= extra;
4200 if (copy_byte < extra)
4201 from += interleave;
4202
4203 for (; from < end; from += interleave)
4204 for (i = 0; i < copy_width; i++)
4205 {
4206 if (&from[i] >= end)
4207 break;
4208 *to++ = from[i];
4209 }
4210
4211 size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
4212 osection->lma /= interleave;
4213 if (copy_byte < extra)
4214 osection->lma++;
4215 }
4216
4217 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
4218 {
4219 status = 1;
4220 bfd_nonfatal_message (NULL, obfd, osection, NULL);
4221 free (memhunk);
4222 return;
4223 }
4224 free (memhunk);
4225 }
4226 else if ((p = find_section_list (bfd_get_section_name (ibfd, isection),
4227 FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL
4228 && (p->flags & SEC_HAS_CONTENTS) != 0)
4229 {
4230 void *memhunk = xmalloc (size);
4231
4232 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
4233 flag--they can just remove the section entirely and add it
4234 back again. However, we do permit them to turn on the
4235 SEC_HAS_CONTENTS flag, and take it to mean that the section
4236 contents should be zeroed out. */
4237
4238 memset (memhunk, 0, size);
4239 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
4240 {
4241 status = 1;
4242 bfd_nonfatal_message (NULL, obfd, osection, NULL);
4243 free (memhunk);
4244 return;
4245 }
4246 free (memhunk);
4247 }
4248 }
4249
4250 /* Get all the sections. This is used when --gap-fill or --pad-to is
4251 used. */
4252
4253 static void
4254 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
4255 {
4256 asection ***secppp = (asection ***) secppparg;
4257
4258 **secppp = osection;
4259 ++(*secppp);
4260 }
4261
4262 /* Sort sections by VMA. This is called via qsort, and is used when
4263 --gap-fill or --pad-to is used. We force non loadable or empty
4264 sections to the front, where they are easier to ignore. */
4265
4266 static int
4267 compare_section_lma (const void *arg1, const void *arg2)
4268 {
4269 const asection *const *sec1 = (const asection * const *) arg1;
4270 const asection *const *sec2 = (const asection * const *) arg2;
4271 flagword flags1, flags2;
4272
4273 /* Sort non loadable sections to the front. */
4274 flags1 = (*sec1)->flags;
4275 flags2 = (*sec2)->flags;
4276 if ((flags1 & SEC_HAS_CONTENTS) == 0
4277 || (flags1 & SEC_LOAD) == 0)
4278 {
4279 if ((flags2 & SEC_HAS_CONTENTS) != 0
4280 && (flags2 & SEC_LOAD) != 0)
4281 return -1;
4282 }
4283 else
4284 {
4285 if ((flags2 & SEC_HAS_CONTENTS) == 0
4286 || (flags2 & SEC_LOAD) == 0)
4287 return 1;
4288 }
4289
4290 /* Sort sections by LMA. */
4291 if ((*sec1)->lma > (*sec2)->lma)
4292 return 1;
4293 else if ((*sec1)->lma < (*sec2)->lma)
4294 return -1;
4295
4296 /* Sort sections with the same LMA by size. */
4297 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
4298 return 1;
4299 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
4300 return -1;
4301
4302 return 0;
4303 }
4304
4305 /* Mark all the symbols which will be used in output relocations with
4306 the BSF_KEEP flag so that those symbols will not be stripped.
4307
4308 Ignore relocations which will not appear in the output file. */
4309
4310 static void
4311 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
4312 {
4313 asymbol **symbols = (asymbol **) symbolsarg;
4314 long relsize;
4315 arelent **relpp;
4316 long relcount, i;
4317
4318 /* Ignore an input section with no corresponding output section. */
4319 if (isection->output_section == NULL)
4320 return;
4321
4322 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
4323 if (relsize < 0)
4324 {
4325 /* Do not complain if the target does not support relocations. */
4326 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
4327 return;
4328 bfd_fatal (bfd_get_filename (ibfd));
4329 }
4330
4331 if (relsize == 0)
4332 return;
4333
4334 relpp = (arelent **) xmalloc (relsize);
4335 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
4336 if (relcount < 0)
4337 bfd_fatal (bfd_get_filename (ibfd));
4338
4339 /* Examine each symbol used in a relocation. If it's not one of the
4340 special bfd section symbols, then mark it with BSF_KEEP. */
4341 for (i = 0; i < relcount; i++)
4342 {
4343 /* See PRs 20923 and 20930 for reproducers for the NULL tests. */
4344 if (relpp[i]->sym_ptr_ptr != NULL
4345 && * relpp[i]->sym_ptr_ptr != NULL
4346 && *relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
4347 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
4348 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
4349 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
4350 }
4351
4352 if (relpp != NULL)
4353 free (relpp);
4354 }
4355
4356 /* Write out debugging information. */
4357
4358 static bfd_boolean
4359 write_debugging_info (bfd *obfd, void *dhandle,
4360 long *symcountp ATTRIBUTE_UNUSED,
4361 asymbol ***symppp ATTRIBUTE_UNUSED)
4362 {
4363 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
4364 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
4365 {
4366 bfd_byte *syms, *strings = NULL;
4367 bfd_size_type symsize, stringsize;
4368 asection *stabsec, *stabstrsec;
4369 flagword flags;
4370
4371 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
4372 &symsize, &strings,
4373 &stringsize))
4374 return FALSE;
4375
4376 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
4377 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
4378 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
4379 if (stabsec == NULL
4380 || stabstrsec == NULL
4381 || ! bfd_set_section_size (obfd, stabsec, symsize)
4382 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
4383 || ! bfd_set_section_alignment (obfd, stabsec, 2)
4384 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
4385 {
4386 bfd_nonfatal_message (NULL, obfd, NULL,
4387 _("can't create debugging section"));
4388 free (strings);
4389 return FALSE;
4390 }
4391
4392 /* We can get away with setting the section contents now because
4393 the next thing the caller is going to do is copy over the
4394 real sections. We may someday have to split the contents
4395 setting out of this function. */
4396 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
4397 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
4398 stringsize))
4399 {
4400 bfd_nonfatal_message (NULL, obfd, NULL,
4401 _("can't set debugging section contents"));
4402 free (strings);
4403 return FALSE;
4404 }
4405
4406 return TRUE;
4407 }
4408
4409 bfd_nonfatal_message (NULL, obfd, NULL,
4410 _("don't know how to write debugging information for %s"),
4411 bfd_get_target (obfd));
4412 return FALSE;
4413 }
4414
4415 /* If neither -D nor -U was specified explicitly,
4416 then use the configured default. */
4417 static void
4418 default_deterministic (void)
4419 {
4420 if (deterministic < 0)
4421 deterministic = DEFAULT_AR_DETERMINISTIC;
4422 }
4423
4424 static int
4425 strip_main (int argc, char *argv[])
4426 {
4427 char *input_target = NULL;
4428 char *output_target = NULL;
4429 bfd_boolean show_version = FALSE;
4430 bfd_boolean formats_info = FALSE;
4431 int c;
4432 int i;
4433 char *output_file = NULL;
4434 bfd_boolean merge_notes_set = FALSE;
4435
4436 while ((c = getopt_long (argc, argv, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU",
4437 strip_options, (int *) 0)) != EOF)
4438 {
4439 switch (c)
4440 {
4441 case 'I':
4442 input_target = optarg;
4443 break;
4444 case 'O':
4445 output_target = optarg;
4446 break;
4447 case 'F':
4448 input_target = output_target = optarg;
4449 break;
4450 case 'R':
4451 handle_remove_section_option (optarg);
4452 break;
4453 case OPTION_REMOVE_RELOCS:
4454 handle_remove_relocations_option (optarg);
4455 break;
4456 case 's':
4457 strip_symbols = STRIP_ALL;
4458 break;
4459 case 'S':
4460 case 'g':
4461 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
4462 strip_symbols = STRIP_DEBUG;
4463 break;
4464 case OPTION_STRIP_DWO:
4465 strip_symbols = STRIP_DWO;
4466 break;
4467 case OPTION_STRIP_UNNEEDED:
4468 strip_symbols = STRIP_UNNEEDED;
4469 break;
4470 case 'K':
4471 add_specific_symbol (optarg, keep_specific_htab);
4472 break;
4473 case 'M':
4474 merge_notes = TRUE;
4475 merge_notes_set = TRUE;
4476 break;
4477 case OPTION_NO_MERGE_NOTES:
4478 merge_notes = FALSE;
4479 merge_notes_set = TRUE;
4480 break;
4481 case 'N':
4482 add_specific_symbol (optarg, strip_specific_htab);
4483 break;
4484 case 'o':
4485 output_file = optarg;
4486 break;
4487 case 'p':
4488 preserve_dates = TRUE;
4489 break;
4490 case 'D':
4491 deterministic = TRUE;
4492 break;
4493 case 'U':
4494 deterministic = FALSE;
4495 break;
4496 case 'x':
4497 discard_locals = LOCALS_ALL;
4498 break;
4499 case 'X':
4500 discard_locals = LOCALS_START_L;
4501 break;
4502 case 'v':
4503 verbose = TRUE;
4504 break;
4505 case 'V':
4506 show_version = TRUE;
4507 break;
4508 case OPTION_FORMATS_INFO:
4509 formats_info = TRUE;
4510 break;
4511 case OPTION_ONLY_KEEP_DEBUG:
4512 strip_symbols = STRIP_NONDEBUG;
4513 break;
4514 case OPTION_KEEP_FILE_SYMBOLS:
4515 keep_file_symbols = 1;
4516 break;
4517 case 0:
4518 /* We've been given a long option. */
4519 break;
4520 case 'w':
4521 wildcard = TRUE;
4522 break;
4523 case 'H':
4524 case 'h':
4525 strip_usage (stdout, 0);
4526 default:
4527 strip_usage (stderr, 1);
4528 }
4529 }
4530
4531 /* If the user has not expressly chosen to merge/not-merge ELF notes
4532 then enable the merging unless we are stripping debug or dwo info. */
4533 if (! merge_notes_set
4534 && (strip_symbols == STRIP_UNDEF
4535 || strip_symbols == STRIP_ALL
4536 || strip_symbols == STRIP_UNNEEDED
4537 || strip_symbols == STRIP_NONDEBUG
4538 || strip_symbols == STRIP_NONDWO))
4539 merge_notes = TRUE;
4540
4541 if (formats_info)
4542 {
4543 display_info ();
4544 return 0;
4545 }
4546
4547 if (show_version)
4548 print_version ("strip");
4549
4550 default_deterministic ();
4551
4552 /* Default is to strip all symbols. */
4553 if (strip_symbols == STRIP_UNDEF
4554 && discard_locals == LOCALS_UNDEF
4555 && htab_elements (strip_specific_htab) == 0)
4556 strip_symbols = STRIP_ALL;
4557
4558 if (output_target == NULL)
4559 output_target = input_target;
4560
4561 i = optind;
4562 if (i == argc
4563 || (output_file != NULL && (i + 1) < argc))
4564 strip_usage (stderr, 1);
4565
4566 for (; i < argc; i++)
4567 {
4568 int hold_status = status;
4569 struct stat statbuf;
4570 char *tmpname;
4571
4572 if (get_file_size (argv[i]) < 1)
4573 {
4574 status = 1;
4575 continue;
4576 }
4577
4578 if (preserve_dates)
4579 /* No need to check the return value of stat().
4580 It has already been checked in get_file_size(). */
4581 stat (argv[i], &statbuf);
4582
4583 if (output_file == NULL
4584 || filename_cmp (argv[i], output_file) == 0)
4585 tmpname = make_tempname (argv[i]);
4586 else
4587 tmpname = output_file;
4588
4589 if (tmpname == NULL)
4590 {
4591 bfd_nonfatal_message (argv[i], NULL, NULL,
4592 _("could not create temporary file to hold stripped copy"));
4593 status = 1;
4594 continue;
4595 }
4596
4597 status = 0;
4598 copy_file (argv[i], tmpname, input_target, output_target, NULL);
4599 if (status == 0)
4600 {
4601 if (preserve_dates)
4602 set_times (tmpname, &statbuf);
4603 if (output_file != tmpname)
4604 status = (smart_rename (tmpname,
4605 output_file ? output_file : argv[i],
4606 preserve_dates) != 0);
4607 if (status == 0)
4608 status = hold_status;
4609 }
4610 else
4611 unlink_if_ordinary (tmpname);
4612 if (output_file != tmpname)
4613 free (tmpname);
4614 }
4615
4616 return status;
4617 }
4618
4619 /* Set up PE subsystem. */
4620
4621 static void
4622 set_pe_subsystem (const char *s)
4623 {
4624 const char *version, *subsystem;
4625 size_t i;
4626 static const struct
4627 {
4628 const char *name;
4629 const char set_def;
4630 const short value;
4631 }
4632 v[] =
4633 {
4634 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
4635 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
4636 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
4637 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
4638 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
4639 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
4640 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
4641 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
4642 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
4643 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
4644 };
4645 short value;
4646 char *copy;
4647 int set_def = -1;
4648
4649 /* Check for the presence of a version number. */
4650 version = strchr (s, ':');
4651 if (version == NULL)
4652 subsystem = s;
4653 else
4654 {
4655 int len = version - s;
4656 copy = xstrdup (s);
4657 subsystem = copy;
4658 copy[len] = '\0';
4659 version = copy + 1 + len;
4660 pe_major_subsystem_version = strtoul (version, &copy, 0);
4661 if (*copy == '.')
4662 pe_minor_subsystem_version = strtoul (copy + 1, &copy, 0);
4663 if (*copy != '\0')
4664 non_fatal (_("%s: bad version in PE subsystem"), s);
4665 }
4666
4667 /* Check for numeric subsystem. */
4668 value = (short) strtol (subsystem, &copy, 0);
4669 if (*copy == '\0')
4670 {
4671 for (i = 0; i < ARRAY_SIZE (v); i++)
4672 if (v[i].value == value)
4673 {
4674 pe_subsystem = value;
4675 set_def = v[i].set_def;
4676 break;
4677 }
4678 }
4679 else
4680 {
4681 /* Search for subsystem by name. */
4682 for (i = 0; i < ARRAY_SIZE (v); i++)
4683 if (strcmp (subsystem, v[i].name) == 0)
4684 {
4685 pe_subsystem = v[i].value;
4686 set_def = v[i].set_def;
4687 break;
4688 }
4689 }
4690
4691 switch (set_def)
4692 {
4693 case -1:
4694 fatal (_("unknown PE subsystem: %s"), s);
4695 break;
4696 case 0:
4697 break;
4698 default:
4699 if (pe_file_alignment == (bfd_vma) -1)
4700 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
4701 if (pe_section_alignment == (bfd_vma) -1)
4702 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
4703 break;
4704 }
4705 if (s != subsystem)
4706 free ((char *) subsystem);
4707 }
4708
4709 /* Convert EFI target to PEI target. */
4710
4711 static void
4712 convert_efi_target (char *efi)
4713 {
4714 efi[0] = 'p';
4715 efi[1] = 'e';
4716 efi[2] = 'i';
4717
4718 if (strcmp (efi + 4, "ia32") == 0)
4719 {
4720 /* Change ia32 to i386. */
4721 efi[5]= '3';
4722 efi[6]= '8';
4723 efi[7]= '6';
4724 }
4725 else if (strcmp (efi + 4, "x86_64") == 0)
4726 {
4727 /* Change x86_64 to x86-64. */
4728 efi[7] = '-';
4729 }
4730 }
4731
4732 /* Allocate and return a pointer to a struct section_add, initializing the
4733 structure using ARG, a string in the format "sectionname=filename".
4734 The returned structure will have its next pointer set to NEXT. The
4735 OPTION field is the name of the command line option currently being
4736 parsed, and is only used if an error needs to be reported. */
4737
4738 static struct section_add *
4739 init_section_add (const char *arg,
4740 struct section_add *next,
4741 const char *option)
4742 {
4743 struct section_add *pa;
4744 const char *s;
4745
4746 s = strchr (arg, '=');
4747 if (s == NULL)
4748 fatal (_("bad format for %s"), option);
4749
4750 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
4751 pa->name = xstrndup (arg, s - arg);
4752 pa->filename = s + 1;
4753 pa->next = next;
4754 pa->contents = NULL;
4755 pa->size = 0;
4756
4757 return pa;
4758 }
4759
4760 /* Load the file specified in PA, allocating memory to hold the file
4761 contents, and store a pointer to the allocated memory in the contents
4762 field of PA. The size field of PA is also updated. All errors call
4763 FATAL. */
4764
4765 static void
4766 section_add_load_file (struct section_add *pa)
4767 {
4768 size_t off, alloc;
4769 FILE *f;
4770
4771 /* We don't use get_file_size so that we can do
4772 --add-section .note.GNU_stack=/dev/null
4773 get_file_size doesn't work on /dev/null. */
4774
4775 f = fopen (pa->filename, FOPEN_RB);
4776 if (f == NULL)
4777 fatal (_("cannot open: %s: %s"),
4778 pa->filename, strerror (errno));
4779
4780 off = 0;
4781 alloc = 4096;
4782 pa->contents = (bfd_byte *) xmalloc (alloc);
4783 while (!feof (f))
4784 {
4785 off_t got;
4786
4787 if (off == alloc)
4788 {
4789 alloc <<= 1;
4790 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
4791 }
4792
4793 got = fread (pa->contents + off, 1, alloc - off, f);
4794 if (ferror (f))
4795 fatal (_("%s: fread failed"), pa->filename);
4796
4797 off += got;
4798 }
4799
4800 pa->size = off;
4801
4802 fclose (f);
4803 }
4804
4805 static int
4806 copy_main (int argc, char *argv[])
4807 {
4808 char *input_filename = NULL;
4809 char *output_filename = NULL;
4810 char *tmpname;
4811 char *input_target = NULL;
4812 char *output_target = NULL;
4813 bfd_boolean show_version = FALSE;
4814 bfd_boolean change_warn = TRUE;
4815 bfd_boolean formats_info = FALSE;
4816 bfd_boolean use_globalize = FALSE;
4817 bfd_boolean use_keep_global = FALSE;
4818 int c;
4819 struct stat statbuf;
4820 const bfd_arch_info_type *input_arch = NULL;
4821
4822 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
4823 copy_options, (int *) 0)) != EOF)
4824 {
4825 switch (c)
4826 {
4827 case 'b':
4828 copy_byte = atoi (optarg);
4829 if (copy_byte < 0)
4830 fatal (_("byte number must be non-negative"));
4831 break;
4832
4833 case 'B':
4834 input_arch = bfd_scan_arch (optarg);
4835 if (input_arch == NULL)
4836 fatal (_("architecture %s unknown"), optarg);
4837 break;
4838
4839 case 'i':
4840 if (optarg)
4841 {
4842 interleave = atoi (optarg);
4843 if (interleave < 1)
4844 fatal (_("interleave must be positive"));
4845 }
4846 else
4847 interleave = 4;
4848 break;
4849
4850 case OPTION_INTERLEAVE_WIDTH:
4851 copy_width = atoi (optarg);
4852 if (copy_width < 1)
4853 fatal(_("interleave width must be positive"));
4854 break;
4855
4856 case 'I':
4857 case 's': /* "source" - 'I' is preferred */
4858 input_target = optarg;
4859 break;
4860
4861 case 'O':
4862 case 'd': /* "destination" - 'O' is preferred */
4863 output_target = optarg;
4864 break;
4865
4866 case 'F':
4867 input_target = output_target = optarg;
4868 break;
4869
4870 case 'j':
4871 find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY);
4872 sections_copied = TRUE;
4873 break;
4874
4875 case 'R':
4876 handle_remove_section_option (optarg);
4877 break;
4878
4879 case OPTION_REMOVE_RELOCS:
4880 handle_remove_relocations_option (optarg);
4881 break;
4882
4883 case 'S':
4884 strip_symbols = STRIP_ALL;
4885 break;
4886
4887 case 'g':
4888 strip_symbols = STRIP_DEBUG;
4889 break;
4890
4891 case OPTION_STRIP_DWO:
4892 strip_symbols = STRIP_DWO;
4893 break;
4894
4895 case OPTION_STRIP_UNNEEDED:
4896 strip_symbols = STRIP_UNNEEDED;
4897 break;
4898
4899 case OPTION_ONLY_KEEP_DEBUG:
4900 strip_symbols = STRIP_NONDEBUG;
4901 break;
4902
4903 case OPTION_KEEP_FILE_SYMBOLS:
4904 keep_file_symbols = 1;
4905 break;
4906
4907 case OPTION_ADD_GNU_DEBUGLINK:
4908 long_section_names = ENABLE ;
4909 gnu_debuglink_filename = optarg;
4910 break;
4911
4912 case 'K':
4913 add_specific_symbol (optarg, keep_specific_htab);
4914 break;
4915
4916 case 'M':
4917 merge_notes = TRUE;
4918 break;
4919 case OPTION_NO_MERGE_NOTES:
4920 merge_notes = FALSE;
4921 break;
4922
4923 case 'N':
4924 add_specific_symbol (optarg, strip_specific_htab);
4925 break;
4926
4927 case OPTION_STRIP_UNNEEDED_SYMBOL:
4928 add_specific_symbol (optarg, strip_unneeded_htab);
4929 break;
4930
4931 case 'L':
4932 add_specific_symbol (optarg, localize_specific_htab);
4933 break;
4934
4935 case OPTION_GLOBALIZE_SYMBOL:
4936 use_globalize = TRUE;
4937 add_specific_symbol (optarg, globalize_specific_htab);
4938 break;
4939
4940 case 'G':
4941 use_keep_global = TRUE;
4942 add_specific_symbol (optarg, keepglobal_specific_htab);
4943 break;
4944
4945 case 'W':
4946 add_specific_symbol (optarg, weaken_specific_htab);
4947 break;
4948
4949 case 'p':
4950 preserve_dates = TRUE;
4951 break;
4952
4953 case 'D':
4954 deterministic = TRUE;
4955 break;
4956
4957 case 'U':
4958 deterministic = FALSE;
4959 break;
4960
4961 case 'w':
4962 wildcard = TRUE;
4963 break;
4964
4965 case 'x':
4966 discard_locals = LOCALS_ALL;
4967 break;
4968
4969 case 'X':
4970 discard_locals = LOCALS_START_L;
4971 break;
4972
4973 case 'v':
4974 verbose = TRUE;
4975 break;
4976
4977 case 'V':
4978 show_version = TRUE;
4979 break;
4980
4981 case OPTION_FORMATS_INFO:
4982 formats_info = TRUE;
4983 break;
4984
4985 case OPTION_WEAKEN:
4986 weaken = TRUE;
4987 break;
4988
4989 case OPTION_ADD_SECTION:
4990 add_sections = init_section_add (optarg, add_sections,
4991 "--add-section");
4992 section_add_load_file (add_sections);
4993 break;
4994
4995 case OPTION_UPDATE_SECTION:
4996 update_sections = init_section_add (optarg, update_sections,
4997 "--update-section");
4998 section_add_load_file (update_sections);
4999 break;
5000
5001 case OPTION_DUMP_SECTION:
5002 dump_sections = init_section_add (optarg, dump_sections,
5003 "--dump-section");
5004 break;
5005
5006 case OPTION_ADD_SYMBOL:
5007 {
5008 char *s, *t;
5009 struct addsym_node *newsym = xmalloc (sizeof *newsym);
5010
5011 newsym->next = NULL;
5012 s = strchr (optarg, '=');
5013 if (s == NULL)
5014 fatal (_("bad format for %s"), "--add-symbol");
5015 t = strchr (s + 1, ':');
5016
5017 newsym->symdef = xstrndup (optarg, s - optarg);
5018 if (t)
5019 {
5020 newsym->section = xstrndup (s + 1, t - (s + 1));
5021 newsym->symval = strtol (t + 1, NULL, 0);
5022 }
5023 else
5024 {
5025 newsym->section = NULL;
5026 newsym->symval = strtol (s + 1, NULL, 0);
5027 t = s;
5028 }
5029
5030 t = strchr (t + 1, ',');
5031 newsym->othersym = NULL;
5032 if (t)
5033 newsym->flags = parse_symflags (t+1, &newsym->othersym);
5034 else
5035 newsym->flags = BSF_GLOBAL;
5036
5037 /* Keep 'othersym' symbols at the front of the list. */
5038 if (newsym->othersym)
5039 {
5040 newsym->next = add_sym_list;
5041 if (!add_sym_list)
5042 add_sym_tail = &newsym->next;
5043 add_sym_list = newsym;
5044 }
5045 else
5046 {
5047 *add_sym_tail = newsym;
5048 add_sym_tail = &newsym->next;
5049 }
5050 add_symbols++;
5051 }
5052 break;
5053
5054 case OPTION_CHANGE_START:
5055 change_start = parse_vma (optarg, "--change-start");
5056 break;
5057
5058 case OPTION_CHANGE_SECTION_ADDRESS:
5059 case OPTION_CHANGE_SECTION_LMA:
5060 case OPTION_CHANGE_SECTION_VMA:
5061 {
5062 struct section_list * p;
5063 unsigned int context = 0;
5064 const char *s;
5065 int len;
5066 char *name;
5067 char *option = NULL;
5068 bfd_vma val;
5069
5070 switch (c)
5071 {
5072 case OPTION_CHANGE_SECTION_ADDRESS:
5073 option = "--change-section-address";
5074 context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
5075 break;
5076 case OPTION_CHANGE_SECTION_LMA:
5077 option = "--change-section-lma";
5078 context = SECTION_CONTEXT_ALTER_LMA;
5079 break;
5080 case OPTION_CHANGE_SECTION_VMA:
5081 option = "--change-section-vma";
5082 context = SECTION_CONTEXT_ALTER_VMA;
5083 break;
5084 }
5085
5086 s = strchr (optarg, '=');
5087 if (s == NULL)
5088 {
5089 s = strchr (optarg, '+');
5090 if (s == NULL)
5091 {
5092 s = strchr (optarg, '-');
5093 if (s == NULL)
5094 fatal (_("bad format for %s"), option);
5095 }
5096 }
5097 else
5098 {
5099 /* Correct the context. */
5100 switch (c)
5101 {
5102 case OPTION_CHANGE_SECTION_ADDRESS:
5103 context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
5104 break;
5105 case OPTION_CHANGE_SECTION_LMA:
5106 context = SECTION_CONTEXT_SET_LMA;
5107 break;
5108 case OPTION_CHANGE_SECTION_VMA:
5109 context = SECTION_CONTEXT_SET_VMA;
5110 break;
5111 }
5112 }
5113
5114 len = s - optarg;
5115 name = (char *) xmalloc (len + 1);
5116 strncpy (name, optarg, len);
5117 name[len] = '\0';
5118
5119 p = find_section_list (name, TRUE, context);
5120
5121 val = parse_vma (s + 1, option);
5122 if (*s == '-')
5123 val = - val;
5124
5125 switch (c)
5126 {
5127 case OPTION_CHANGE_SECTION_ADDRESS:
5128 p->vma_val = val;
5129 /* Fall through. */
5130
5131 case OPTION_CHANGE_SECTION_LMA:
5132 p->lma_val = val;
5133 break;
5134
5135 case OPTION_CHANGE_SECTION_VMA:
5136 p->vma_val = val;
5137 break;
5138 }
5139 }
5140 break;
5141
5142 case OPTION_CHANGE_ADDRESSES:
5143 change_section_address = parse_vma (optarg, "--change-addresses");
5144 change_start = change_section_address;
5145 break;
5146
5147 case OPTION_CHANGE_WARNINGS:
5148 change_warn = TRUE;
5149 break;
5150
5151 case OPTION_CHANGE_LEADING_CHAR:
5152 change_leading_char = TRUE;
5153 break;
5154
5155 case OPTION_COMPRESS_DEBUG_SECTIONS:
5156 if (optarg)
5157 {
5158 if (strcasecmp (optarg, "none") == 0)
5159 do_debug_sections = decompress;
5160 else if (strcasecmp (optarg, "zlib") == 0)
5161 do_debug_sections = compress_zlib;
5162 else if (strcasecmp (optarg, "zlib-gnu") == 0)
5163 do_debug_sections = compress_gnu_zlib;
5164 else if (strcasecmp (optarg, "zlib-gabi") == 0)
5165 do_debug_sections = compress_gabi_zlib;
5166 else
5167 fatal (_("unrecognized --compress-debug-sections type `%s'"),
5168 optarg);
5169 }
5170 else
5171 do_debug_sections = compress;
5172 break;
5173
5174 case OPTION_DEBUGGING:
5175 convert_debugging = TRUE;
5176 break;
5177
5178 case OPTION_DECOMPRESS_DEBUG_SECTIONS:
5179 do_debug_sections = decompress;
5180 break;
5181
5182 case OPTION_ELF_STT_COMMON:
5183 if (strcasecmp (optarg, "yes") == 0)
5184 do_elf_stt_common = elf_stt_common;
5185 else if (strcasecmp (optarg, "no") == 0)
5186 do_elf_stt_common = no_elf_stt_common;
5187 else
5188 fatal (_("unrecognized --elf-stt-common= option `%s'"),
5189 optarg);
5190 break;
5191
5192 case OPTION_GAP_FILL:
5193 {
5194 bfd_vma gap_fill_vma;
5195
5196 gap_fill_vma = parse_vma (optarg, "--gap-fill");
5197 gap_fill = (bfd_byte) gap_fill_vma;
5198 if ((bfd_vma) gap_fill != gap_fill_vma)
5199 {
5200 char buff[20];
5201
5202 sprintf_vma (buff, gap_fill_vma);
5203
5204 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
5205 buff, gap_fill);
5206 }
5207 gap_fill_set = TRUE;
5208 }
5209 break;
5210
5211 case OPTION_NO_CHANGE_WARNINGS:
5212 change_warn = FALSE;
5213 break;
5214
5215 case OPTION_PAD_TO:
5216 pad_to = parse_vma (optarg, "--pad-to");
5217 pad_to_set = TRUE;
5218 break;
5219
5220 case OPTION_REMOVE_LEADING_CHAR:
5221 remove_leading_char = TRUE;
5222 break;
5223
5224 case OPTION_REDEFINE_SYM:
5225 {
5226 /* Insert this redefinition onto redefine_specific_htab. */
5227
5228 int len;
5229 const char *s;
5230 const char *nextarg;
5231 char *source, *target;
5232
5233 s = strchr (optarg, '=');
5234 if (s == NULL)
5235 fatal (_("bad format for %s"), "--redefine-sym");
5236
5237 len = s - optarg;
5238 source = (char *) xmalloc (len + 1);
5239 strncpy (source, optarg, len);
5240 source[len] = '\0';
5241
5242 nextarg = s + 1;
5243 len = strlen (nextarg);
5244 target = (char *) xmalloc (len + 1);
5245 strcpy (target, nextarg);
5246
5247 add_redefine_and_check ("--redefine-sym", source, target);
5248
5249 free (source);
5250 free (target);
5251 }
5252 break;
5253
5254 case OPTION_REDEFINE_SYMS:
5255 add_redefine_syms_file (optarg);
5256 break;
5257
5258 case OPTION_SET_SECTION_FLAGS:
5259 {
5260 struct section_list *p;
5261 const char *s;
5262 int len;
5263 char *name;
5264
5265 s = strchr (optarg, '=');
5266 if (s == NULL)
5267 fatal (_("bad format for %s"), "--set-section-flags");
5268
5269 len = s - optarg;
5270 name = (char *) xmalloc (len + 1);
5271 strncpy (name, optarg, len);
5272 name[len] = '\0';
5273
5274 p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS);
5275
5276 p->flags = parse_flags (s + 1);
5277 }
5278 break;
5279
5280 case OPTION_SET_SECTION_ALIGNMENT:
5281 {
5282 struct section_list *p;
5283 const char *s;
5284 int len;
5285 char *name;
5286 int align;
5287
5288 s = strchr (optarg, '=');
5289 if (s == NULL)
5290 fatal (_("bad format for %s"), "--set-section-alignment");
5291
5292 align = atoi(s+1);
5293 if (align < 0)
5294 fatal (_("bad format for %s"), "--set-section-alignment");
5295
5296 len = s - optarg;
5297 name = (char *) xmalloc (len + 1);
5298 strncpy (name, optarg, len);
5299 name[len] = '\0';
5300
5301 p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_ALIGNMENT);
5302
5303 p->alignment = align;
5304 }
5305 break;
5306
5307 case OPTION_RENAME_SECTION:
5308 {
5309 flagword flags;
5310 const char *eq, *fl;
5311 char *old_name;
5312 char *new_name;
5313 unsigned int len;
5314
5315 eq = strchr (optarg, '=');
5316 if (eq == NULL)
5317 fatal (_("bad format for %s"), "--rename-section");
5318
5319 len = eq - optarg;
5320 if (len == 0)
5321 fatal (_("bad format for %s"), "--rename-section");
5322
5323 old_name = (char *) xmalloc (len + 1);
5324 strncpy (old_name, optarg, len);
5325 old_name[len] = 0;
5326
5327 eq++;
5328 fl = strchr (eq, ',');
5329 if (fl)
5330 {
5331 flags = parse_flags (fl + 1);
5332 len = fl - eq;
5333 }
5334 else
5335 {
5336 flags = -1;
5337 len = strlen (eq);
5338 }
5339
5340 if (len == 0)
5341 fatal (_("bad format for %s"), "--rename-section");
5342
5343 new_name = (char *) xmalloc (len + 1);
5344 strncpy (new_name, eq, len);
5345 new_name[len] = 0;
5346
5347 add_section_rename (old_name, new_name, flags);
5348 }
5349 break;
5350
5351 case OPTION_SET_START:
5352 set_start = parse_vma (optarg, "--set-start");
5353 set_start_set = TRUE;
5354 break;
5355
5356 case OPTION_SREC_LEN:
5357 _bfd_srec_len = parse_vma (optarg, "--srec-len");
5358 break;
5359
5360 case OPTION_SREC_FORCES3:
5361 _bfd_srec_forceS3 = TRUE;
5362 break;
5363
5364 case OPTION_STRIP_SYMBOLS:
5365 add_specific_symbols (optarg, strip_specific_htab,
5366 &strip_specific_buffer);
5367 break;
5368
5369 case OPTION_STRIP_UNNEEDED_SYMBOLS:
5370 add_specific_symbols (optarg, strip_unneeded_htab,
5371 &strip_unneeded_buffer);
5372 break;
5373
5374 case OPTION_KEEP_SYMBOLS:
5375 add_specific_symbols (optarg, keep_specific_htab,
5376 &keep_specific_buffer);
5377 break;
5378
5379 case OPTION_LOCALIZE_HIDDEN:
5380 localize_hidden = TRUE;
5381 break;
5382
5383 case OPTION_LOCALIZE_SYMBOLS:
5384 add_specific_symbols (optarg, localize_specific_htab,
5385 &localize_specific_buffer);
5386 break;
5387
5388 case OPTION_LONG_SECTION_NAMES:
5389 if (!strcmp ("enable", optarg))
5390 long_section_names = ENABLE;
5391 else if (!strcmp ("disable", optarg))
5392 long_section_names = DISABLE;
5393 else if (!strcmp ("keep", optarg))
5394 long_section_names = KEEP;
5395 else
5396 fatal (_("unknown long section names option '%s'"), optarg);
5397 break;
5398
5399 case OPTION_GLOBALIZE_SYMBOLS:
5400 use_globalize = TRUE;
5401 add_specific_symbols (optarg, globalize_specific_htab,
5402 &globalize_specific_buffer);
5403 break;
5404
5405 case OPTION_KEEPGLOBAL_SYMBOLS:
5406 use_keep_global = TRUE;
5407 add_specific_symbols (optarg, keepglobal_specific_htab,
5408 &keepglobal_specific_buffer);
5409 break;
5410
5411 case OPTION_WEAKEN_SYMBOLS:
5412 add_specific_symbols (optarg, weaken_specific_htab,
5413 &weaken_specific_buffer);
5414 break;
5415
5416 case OPTION_ALT_MACH_CODE:
5417 use_alt_mach_code = strtoul (optarg, NULL, 0);
5418 if (use_alt_mach_code == 0)
5419 fatal (_("unable to parse alternative machine code"));
5420 break;
5421
5422 case OPTION_PREFIX_SYMBOLS:
5423 prefix_symbols_string = optarg;
5424 break;
5425
5426 case OPTION_PREFIX_SECTIONS:
5427 prefix_sections_string = optarg;
5428 break;
5429
5430 case OPTION_PREFIX_ALLOC_SECTIONS:
5431 prefix_alloc_sections_string = optarg;
5432 break;
5433
5434 case OPTION_READONLY_TEXT:
5435 bfd_flags_to_set |= WP_TEXT;
5436 bfd_flags_to_clear &= ~WP_TEXT;
5437 break;
5438
5439 case OPTION_WRITABLE_TEXT:
5440 bfd_flags_to_clear |= WP_TEXT;
5441 bfd_flags_to_set &= ~WP_TEXT;
5442 break;
5443
5444 case OPTION_PURE:
5445 bfd_flags_to_set |= D_PAGED;
5446 bfd_flags_to_clear &= ~D_PAGED;
5447 break;
5448
5449 case OPTION_IMPURE:
5450 bfd_flags_to_clear |= D_PAGED;
5451 bfd_flags_to_set &= ~D_PAGED;
5452 break;
5453
5454 case OPTION_EXTRACT_DWO:
5455 strip_symbols = STRIP_NONDWO;
5456 break;
5457
5458 case OPTION_EXTRACT_SYMBOL:
5459 extract_symbol = TRUE;
5460 break;
5461
5462 case OPTION_REVERSE_BYTES:
5463 {
5464 int prev = reverse_bytes;
5465
5466 reverse_bytes = atoi (optarg);
5467 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
5468 fatal (_("number of bytes to reverse must be positive and even"));
5469
5470 if (prev && prev != reverse_bytes)
5471 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
5472 prev);
5473 break;
5474 }
5475
5476 case OPTION_FILE_ALIGNMENT:
5477 pe_file_alignment = parse_vma (optarg, "--file-alignment");
5478 break;
5479
5480 case OPTION_HEAP:
5481 {
5482 char *end;
5483 pe_heap_reserve = strtoul (optarg, &end, 0);
5484 if (end == optarg
5485 || (*end != '.' && *end != '\0'))
5486 non_fatal (_("%s: invalid reserve value for --heap"),
5487 optarg);
5488 else if (*end != '\0')
5489 {
5490 pe_heap_commit = strtoul (end + 1, &end, 0);
5491 if (*end != '\0')
5492 non_fatal (_("%s: invalid commit value for --heap"),
5493 optarg);
5494 }
5495 }
5496 break;
5497
5498 case OPTION_IMAGE_BASE:
5499 pe_image_base = parse_vma (optarg, "--image-base");
5500 break;
5501
5502 case OPTION_PE_SECTION_ALIGNMENT:
5503 pe_section_alignment = parse_vma (optarg,
5504 "--section-alignment");
5505 break;
5506
5507 case OPTION_SUBSYSTEM:
5508 set_pe_subsystem (optarg);
5509 break;
5510
5511 case OPTION_STACK:
5512 {
5513 char *end;
5514 pe_stack_reserve = strtoul (optarg, &end, 0);
5515 if (end == optarg
5516 || (*end != '.' && *end != '\0'))
5517 non_fatal (_("%s: invalid reserve value for --stack"),
5518 optarg);
5519 else if (*end != '\0')
5520 {
5521 pe_stack_commit = strtoul (end + 1, &end, 0);
5522 if (*end != '\0')
5523 non_fatal (_("%s: invalid commit value for --stack"),
5524 optarg);
5525 }
5526 }
5527 break;
5528
5529 case OPTION_VERILOG_DATA_WIDTH:
5530 VerilogDataWidth = parse_vma (optarg, "--verilog-data-width");
5531 if (VerilogDataWidth < 1)
5532 fatal (_("verilog data width must be at least 1 byte"));
5533 break;
5534
5535 case 0:
5536 /* We've been given a long option. */
5537 break;
5538
5539 case 'H':
5540 case 'h':
5541 copy_usage (stdout, 0);
5542
5543 default:
5544 copy_usage (stderr, 1);
5545 }
5546 }
5547
5548 if (use_globalize && use_keep_global)
5549 fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)"));
5550
5551 if (formats_info)
5552 {
5553 display_info ();
5554 return 0;
5555 }
5556
5557 if (show_version)
5558 print_version ("objcopy");
5559
5560 if (interleave && copy_byte == -1)
5561 fatal (_("interleave start byte must be set with --byte"));
5562
5563 if (copy_byte >= interleave)
5564 fatal (_("byte number must be less than interleave"));
5565
5566 if (copy_width > interleave - copy_byte)
5567 fatal (_("interleave width must be less than or equal to interleave - byte`"));
5568
5569 if (optind == argc || optind + 2 < argc)
5570 copy_usage (stderr, 1);
5571
5572 input_filename = argv[optind];
5573 if (optind + 1 < argc)
5574 output_filename = argv[optind + 1];
5575
5576 default_deterministic ();
5577
5578 /* Default is to strip no symbols. */
5579 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
5580 strip_symbols = STRIP_NONE;
5581
5582 if (output_target == NULL)
5583 output_target = input_target;
5584
5585 /* Convert input EFI target to PEI target. */
5586 if (input_target != NULL
5587 && strncmp (input_target, "efi-", 4) == 0)
5588 {
5589 char *efi;
5590
5591 efi = xstrdup (output_target + 4);
5592 if (strncmp (efi, "bsdrv-", 6) == 0
5593 || strncmp (efi, "rtdrv-", 6) == 0)
5594 efi += 2;
5595 else if (strncmp (efi, "app-", 4) != 0)
5596 fatal (_("unknown input EFI target: %s"), input_target);
5597
5598 input_target = efi;
5599 convert_efi_target (efi);
5600 }
5601
5602 /* Convert output EFI target to PEI target. */
5603 if (output_target != NULL
5604 && strncmp (output_target, "efi-", 4) == 0)
5605 {
5606 char *efi;
5607
5608 efi = xstrdup (output_target + 4);
5609 if (strncmp (efi, "app-", 4) == 0)
5610 {
5611 if (pe_subsystem == -1)
5612 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
5613 }
5614 else if (strncmp (efi, "bsdrv-", 6) == 0)
5615 {
5616 if (pe_subsystem == -1)
5617 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
5618 efi += 2;
5619 }
5620 else if (strncmp (efi, "rtdrv-", 6) == 0)
5621 {
5622 if (pe_subsystem == -1)
5623 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
5624 efi += 2;
5625 }
5626 else
5627 fatal (_("unknown output EFI target: %s"), output_target);
5628
5629 if (pe_file_alignment == (bfd_vma) -1)
5630 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
5631 if (pe_section_alignment == (bfd_vma) -1)
5632 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
5633
5634 output_target = efi;
5635 convert_efi_target (efi);
5636 }
5637
5638 if (preserve_dates)
5639 if (stat (input_filename, & statbuf) < 0)
5640 fatal (_("warning: could not locate '%s'. System error message: %s"),
5641 input_filename, strerror (errno));
5642
5643 /* If there is no destination file, or the source and destination files
5644 are the same, then create a temp and rename the result into the input. */
5645 if (output_filename == NULL
5646 || filename_cmp (input_filename, output_filename) == 0)
5647 tmpname = make_tempname (input_filename);
5648 else
5649 tmpname = output_filename;
5650
5651 if (tmpname == NULL)
5652 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
5653 input_filename, strerror (errno));
5654
5655 copy_file (input_filename, tmpname, input_target, output_target, input_arch);
5656 if (status == 0)
5657 {
5658 if (preserve_dates)
5659 set_times (tmpname, &statbuf);
5660 if (tmpname != output_filename)
5661 status = (smart_rename (tmpname, input_filename,
5662 preserve_dates) != 0);
5663 }
5664 else
5665 unlink_if_ordinary (tmpname);
5666
5667 if (tmpname != output_filename)
5668 free (tmpname);
5669
5670 if (change_warn)
5671 {
5672 struct section_list *p;
5673
5674 for (p = change_sections; p != NULL; p = p->next)
5675 {
5676 if (! p->used)
5677 {
5678 if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
5679 {
5680 char buff [20];
5681
5682 sprintf_vma (buff, p->vma_val);
5683
5684 /* xgettext:c-format */
5685 non_fatal (_("%s %s%c0x%s never used"),
5686 "--change-section-vma",
5687 p->pattern,
5688 p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
5689 buff);
5690 }
5691
5692 if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
5693 {
5694 char buff [20];
5695
5696 sprintf_vma (buff, p->lma_val);
5697
5698 /* xgettext:c-format */
5699 non_fatal (_("%s %s%c0x%s never used"),
5700 "--change-section-lma",
5701 p->pattern,
5702 p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
5703 buff);
5704 }
5705 }
5706 }
5707 }
5708
5709 if (strip_specific_buffer)
5710 free (strip_specific_buffer);
5711
5712 if (strip_unneeded_buffer)
5713 free (strip_unneeded_buffer);
5714
5715 if (keep_specific_buffer)
5716 free (keep_specific_buffer);
5717
5718 if (localize_specific_buffer)
5719 free (globalize_specific_buffer);
5720
5721 if (globalize_specific_buffer)
5722 free (globalize_specific_buffer);
5723
5724 if (keepglobal_specific_buffer)
5725 free (keepglobal_specific_buffer);
5726
5727 if (weaken_specific_buffer)
5728 free (weaken_specific_buffer);
5729
5730 return 0;
5731 }
5732
5733 int
5734 main (int argc, char *argv[])
5735 {
5736 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
5737 setlocale (LC_MESSAGES, "");
5738 #endif
5739 #if defined (HAVE_SETLOCALE)
5740 setlocale (LC_CTYPE, "");
5741 #endif
5742 bindtextdomain (PACKAGE, LOCALEDIR);
5743 textdomain (PACKAGE);
5744
5745 program_name = argv[0];
5746 xmalloc_set_program_name (program_name);
5747
5748 START_PROGRESS (program_name, 0);
5749
5750 expandargv (&argc, &argv);
5751
5752 strip_symbols = STRIP_UNDEF;
5753 discard_locals = LOCALS_UNDEF;
5754
5755 if (bfd_init () != BFD_INIT_MAGIC)
5756 fatal (_("fatal error: libbfd ABI mismatch"));
5757 set_default_bfd_target ();
5758
5759 if (is_strip < 0)
5760 {
5761 int i = strlen (program_name);
5762 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5763 /* Drop the .exe suffix, if any. */
5764 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
5765 {
5766 i -= 4;
5767 program_name[i] = '\0';
5768 }
5769 #endif
5770 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
5771 }
5772
5773 create_symbol_htabs ();
5774
5775 if (argv != NULL)
5776 bfd_set_error_program_name (argv[0]);
5777
5778 if (is_strip)
5779 strip_main (argc, argv);
5780 else
5781 copy_main (argc, argv);
5782
5783 END_PROGRESS (program_name);
5784
5785 return status;
5786 }
This page took 0.143662 seconds and 5 git commands to generate.