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