* config/tc-h8300.c (h8300_elf_section): Add .zdebug to the list
[binutils.git] / binutils / objcopy.c
bloba9b0d3717d804c1d231eccba250c109b623bbfd2
1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "progress.h"
26 #include "getopt.h"
27 #include "libiberty.h"
28 #include "bucomm.h"
29 #include "budbg.h"
30 #include "filenames.h"
31 #include "fnmatch.h"
32 #include "elf-bfd.h"
33 #include <sys/stat.h>
34 #include "libbfd.h"
35 #include "coff/internal.h"
36 #include "libcoff.h"
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"
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;
54 struct is_specified_symbol_predicate_data
56 const char *name;
57 bfd_boolean found;
60 /* A list to support redefine_sym. */
61 struct redefine_node
63 char *source;
64 char *target;
65 struct redefine_node *next;
68 typedef struct section_rename
70 const char * old_name;
71 const char * new_name;
72 flagword flags;
73 struct section_rename * next;
75 section_rename;
77 /* List of sections to be renamed. */
78 static section_rename *section_rename_list;
80 static asymbol **isympp = NULL; /* Input symbols. */
81 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
83 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
84 static int copy_byte = -1;
85 static int interleave = 4;
87 static bfd_boolean verbose; /* Print file and target names. */
88 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
89 static int status = 0; /* Exit status. */
91 enum strip_action
93 STRIP_UNDEF,
94 STRIP_NONE, /* Don't strip. */
95 STRIP_DEBUG, /* Strip all debugger symbols. */
96 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
97 STRIP_NONDEBUG, /* Strip everything but debug info. */
98 STRIP_ALL /* Strip all symbols. */
101 /* Which symbols to remove. */
102 static enum strip_action strip_symbols;
104 enum locals_action
106 LOCALS_UNDEF,
107 LOCALS_START_L, /* Discard locals starting with L. */
108 LOCALS_ALL /* Discard all locals. */
111 /* Which local symbols to remove. Overrides STRIP_ALL. */
112 static enum locals_action discard_locals;
114 /* What kind of change to perform. */
115 enum change_action
117 CHANGE_IGNORE,
118 CHANGE_MODIFY,
119 CHANGE_SET
122 /* Structure used to hold lists of sections and actions to take. */
123 struct section_list
125 struct section_list * next; /* Next section to change. */
126 const char * name; /* Section name. */
127 bfd_boolean used; /* Whether this entry was used. */
128 bfd_boolean remove; /* Whether to remove this section. */
129 bfd_boolean copy; /* Whether to copy this section. */
130 enum change_action change_vma;/* Whether to change or set VMA. */
131 bfd_vma vma_val; /* Amount to change by or set to. */
132 enum change_action change_lma;/* Whether to change or set LMA. */
133 bfd_vma lma_val; /* Amount to change by or set to. */
134 bfd_boolean set_flags; /* Whether to set the section flags. */
135 flagword flags; /* What to set the section flags to. */
138 static struct section_list *change_sections;
140 /* TRUE if some sections are to be removed. */
141 static bfd_boolean sections_removed;
143 /* TRUE if only some sections are to be copied. */
144 static bfd_boolean sections_copied;
146 /* Changes to the start address. */
147 static bfd_vma change_start = 0;
148 static bfd_boolean set_start_set = FALSE;
149 static bfd_vma set_start;
151 /* Changes to section addresses. */
152 static bfd_vma change_section_address = 0;
154 /* Filling gaps between sections. */
155 static bfd_boolean gap_fill_set = FALSE;
156 static bfd_byte gap_fill = 0;
158 /* Pad to a given address. */
159 static bfd_boolean pad_to_set = FALSE;
160 static bfd_vma pad_to;
162 /* Use alternative machine code? */
163 static unsigned long use_alt_mach_code = 0;
165 /* Output BFD flags user wants to set or clear */
166 static flagword bfd_flags_to_set;
167 static flagword bfd_flags_to_clear;
169 /* List of sections to add. */
170 struct section_add
172 /* Next section to add. */
173 struct section_add *next;
174 /* Name of section to add. */
175 const char *name;
176 /* Name of file holding section contents. */
177 const char *filename;
178 /* Size of file. */
179 size_t size;
180 /* Contents of file. */
181 bfd_byte *contents;
182 /* BFD section, after it has been added. */
183 asection *section;
186 /* List of sections to add to the output BFD. */
187 static struct section_add *add_sections;
189 /* If non-NULL the argument to --add-gnu-debuglink.
190 This should be the filename to store in the .gnu_debuglink section. */
191 static const char * gnu_debuglink_filename = NULL;
193 /* Whether to convert debugging information. */
194 static bfd_boolean convert_debugging = FALSE;
196 /* Whether to change the leading character in symbol names. */
197 static bfd_boolean change_leading_char = FALSE;
199 /* Whether to remove the leading character from global symbol names. */
200 static bfd_boolean remove_leading_char = FALSE;
202 /* Whether to permit wildcard in symbol comparison. */
203 static bfd_boolean wildcard = FALSE;
205 /* True if --localize-hidden is in effect. */
206 static bfd_boolean localize_hidden = FALSE;
208 /* List of symbols to strip, keep, localize, keep-global, weaken,
209 or redefine. */
210 static htab_t strip_specific_htab = NULL;
211 static htab_t strip_unneeded_htab = NULL;
212 static htab_t keep_specific_htab = NULL;
213 static htab_t localize_specific_htab = NULL;
214 static htab_t globalize_specific_htab = NULL;
215 static htab_t keepglobal_specific_htab = NULL;
216 static htab_t weaken_specific_htab = NULL;
217 static struct redefine_node *redefine_sym_list = NULL;
219 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
220 static bfd_boolean weaken = FALSE;
222 /* If this is TRUE, we retain BSF_FILE symbols. */
223 static bfd_boolean keep_file_symbols = FALSE;
225 /* Prefix symbols/sections. */
226 static char *prefix_symbols_string = 0;
227 static char *prefix_sections_string = 0;
228 static char *prefix_alloc_sections_string = 0;
230 /* True if --extract-symbol was passed on the command line. */
231 static bfd_boolean extract_symbol = FALSE;
233 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
234 of <reverse_bytes> bytes within each output section. */
235 static int reverse_bytes = 0;
237 /* For Coff objects, we may want to allow or disallow long section names,
238 or preserve them where found in the inputs. Debug info relies on them. */
239 enum long_section_name_handling
241 DISABLE,
242 ENABLE,
243 KEEP
246 /* The default long section handling mode is to preserve them.
247 This is also the only behaviour for 'strip'. */
248 static enum long_section_name_handling long_section_names = KEEP;
250 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
251 enum command_line_switch
253 OPTION_ADD_SECTION=150,
254 OPTION_CHANGE_ADDRESSES,
255 OPTION_CHANGE_LEADING_CHAR,
256 OPTION_CHANGE_START,
257 OPTION_CHANGE_SECTION_ADDRESS,
258 OPTION_CHANGE_SECTION_LMA,
259 OPTION_CHANGE_SECTION_VMA,
260 OPTION_CHANGE_WARNINGS,
261 OPTION_DEBUGGING,
262 OPTION_GAP_FILL,
263 OPTION_NO_CHANGE_WARNINGS,
264 OPTION_PAD_TO,
265 OPTION_REMOVE_LEADING_CHAR,
266 OPTION_SET_SECTION_FLAGS,
267 OPTION_SET_START,
268 OPTION_STRIP_UNNEEDED,
269 OPTION_WEAKEN,
270 OPTION_REDEFINE_SYM,
271 OPTION_REDEFINE_SYMS,
272 OPTION_SREC_LEN,
273 OPTION_SREC_FORCES3,
274 OPTION_STRIP_SYMBOLS,
275 OPTION_STRIP_UNNEEDED_SYMBOL,
276 OPTION_STRIP_UNNEEDED_SYMBOLS,
277 OPTION_KEEP_SYMBOLS,
278 OPTION_LOCALIZE_HIDDEN,
279 OPTION_LOCALIZE_SYMBOLS,
280 OPTION_LONG_SECTION_NAMES,
281 OPTION_GLOBALIZE_SYMBOL,
282 OPTION_GLOBALIZE_SYMBOLS,
283 OPTION_KEEPGLOBAL_SYMBOLS,
284 OPTION_WEAKEN_SYMBOLS,
285 OPTION_RENAME_SECTION,
286 OPTION_ALT_MACH_CODE,
287 OPTION_PREFIX_SYMBOLS,
288 OPTION_PREFIX_SECTIONS,
289 OPTION_PREFIX_ALLOC_SECTIONS,
290 OPTION_FORMATS_INFO,
291 OPTION_ADD_GNU_DEBUGLINK,
292 OPTION_ONLY_KEEP_DEBUG,
293 OPTION_KEEP_FILE_SYMBOLS,
294 OPTION_READONLY_TEXT,
295 OPTION_WRITABLE_TEXT,
296 OPTION_PURE,
297 OPTION_IMPURE,
298 OPTION_EXTRACT_SYMBOL,
299 OPTION_REVERSE_BYTES,
300 OPTION_FILE_ALIGNMENT,
301 OPTION_HEAP,
302 OPTION_IMAGE_BASE,
303 OPTION_SECTION_ALIGNMENT,
304 OPTION_STACK,
305 OPTION_SUBSYSTEM
308 /* Options to handle if running as "strip". */
310 static struct option strip_options[] =
312 {"discard-all", no_argument, 0, 'x'},
313 {"discard-locals", no_argument, 0, 'X'},
314 {"format", required_argument, 0, 'F'}, /* Obsolete */
315 {"help", no_argument, 0, 'h'},
316 {"info", no_argument, 0, OPTION_FORMATS_INFO},
317 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
318 {"input-target", required_argument, 0, 'I'},
319 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
320 {"keep-symbol", required_argument, 0, 'K'},
321 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
322 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
323 {"output-target", required_argument, 0, 'O'},
324 {"output-file", required_argument, 0, 'o'},
325 {"preserve-dates", no_argument, 0, 'p'},
326 {"remove-section", required_argument, 0, 'R'},
327 {"strip-all", no_argument, 0, 's'},
328 {"strip-debug", no_argument, 0, 'S'},
329 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
330 {"strip-symbol", required_argument, 0, 'N'},
331 {"target", required_argument, 0, 'F'},
332 {"verbose", no_argument, 0, 'v'},
333 {"version", no_argument, 0, 'V'},
334 {"wildcard", no_argument, 0, 'w'},
335 {0, no_argument, 0, 0}
338 /* Options to handle if running as "objcopy". */
340 static struct option copy_options[] =
342 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
343 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
344 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
345 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
346 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
347 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
348 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
349 {"binary-architecture", required_argument, 0, 'B'},
350 {"byte", required_argument, 0, 'b'},
351 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
352 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
353 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
354 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
355 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
356 {"change-start", required_argument, 0, OPTION_CHANGE_START},
357 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
358 {"debugging", no_argument, 0, OPTION_DEBUGGING},
359 {"discard-all", no_argument, 0, 'x'},
360 {"discard-locals", no_argument, 0, 'X'},
361 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
362 {"format", required_argument, 0, 'F'}, /* Obsolete */
363 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
364 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
365 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
366 {"help", no_argument, 0, 'h'},
367 {"impure", no_argument, 0, OPTION_IMPURE},
368 {"info", no_argument, 0, OPTION_FORMATS_INFO},
369 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
370 {"input-target", required_argument, 0, 'I'},
371 {"interleave", required_argument, 0, 'i'},
372 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
373 {"keep-global-symbol", required_argument, 0, 'G'},
374 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
375 {"keep-symbol", required_argument, 0, 'K'},
376 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
377 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
378 {"localize-symbol", required_argument, 0, 'L'},
379 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
380 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
381 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
382 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
383 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
384 {"only-section", required_argument, 0, 'j'},
385 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
386 {"output-target", required_argument, 0, 'O'},
387 {"pad-to", required_argument, 0, OPTION_PAD_TO},
388 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
389 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
390 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
391 {"preserve-dates", no_argument, 0, 'p'},
392 {"pure", no_argument, 0, OPTION_PURE},
393 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
394 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
395 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
396 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
397 {"remove-section", required_argument, 0, 'R'},
398 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
399 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
400 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
401 {"set-start", required_argument, 0, OPTION_SET_START},
402 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
403 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
404 {"strip-all", no_argument, 0, 'S'},
405 {"strip-debug", no_argument, 0, 'g'},
406 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
407 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
408 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
409 {"strip-symbol", required_argument, 0, 'N'},
410 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
411 {"target", required_argument, 0, 'F'},
412 {"verbose", no_argument, 0, 'v'},
413 {"version", no_argument, 0, 'V'},
414 {"weaken", no_argument, 0, OPTION_WEAKEN},
415 {"weaken-symbol", required_argument, 0, 'W'},
416 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
417 {"wildcard", no_argument, 0, 'w'},
418 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
419 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
420 {"heap", required_argument, 0, OPTION_HEAP},
421 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
422 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
423 {"stack", required_argument, 0, OPTION_STACK},
424 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
425 {0, no_argument, 0, 0}
428 /* IMPORTS */
429 extern char *program_name;
431 /* This flag distinguishes between strip and objcopy:
432 1 means this is 'strip'; 0 means this is 'objcopy'.
433 -1 means if we should use argv[0] to decide. */
434 extern int is_strip;
436 /* The maximum length of an S record. This variable is declared in srec.c
437 and can be modified by the --srec-len parameter. */
438 extern unsigned int Chunk;
440 /* Restrict the generation of Srecords to type S3 only.
441 This variable is declare in bfd/srec.c and can be toggled
442 on by the --srec-forceS3 command line switch. */
443 extern bfd_boolean S3Forced;
445 /* Forward declarations. */
446 static void setup_section (bfd *, asection *, void *);
447 static void setup_bfd_headers (bfd *, bfd *);
448 static void copy_section (bfd *, asection *, void *);
449 static void get_sections (bfd *, asection *, void *);
450 static int compare_section_lma (const void *, const void *);
451 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
452 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
453 static const char *lookup_sym_redefinition (const char *);
455 static void
456 copy_usage (FILE *stream, int exit_status)
458 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
459 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
460 fprintf (stream, _(" The options are:\n"));
461 fprintf (stream, _("\
462 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
463 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
464 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
465 -F --target <bfdname> Set both input and output format to <bfdname>\n\
466 --debugging Convert debugging information, if possible\n\
467 -p --preserve-dates Copy modified/access timestamps to the output\n\
468 -j --only-section <name> Only copy section <name> into the output\n\
469 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
470 -R --remove-section <name> Remove section <name> from the output\n\
471 -S --strip-all Remove all symbol and relocation information\n\
472 -g --strip-debug Remove all debugging symbols & sections\n\
473 --strip-unneeded Remove all symbols not needed by relocations\n\
474 -N --strip-symbol <name> Do not copy symbol <name>\n\
475 --strip-unneeded-symbol <name>\n\
476 Do not copy symbol <name> unless needed by\n\
477 relocations\n\
478 --only-keep-debug Strip everything but the debug information\n\
479 --extract-symbol Remove section contents but keep symbols\n\
480 -K --keep-symbol <name> Do not strip symbol <name>\n\
481 --keep-file-symbols Do not strip file symbol(s)\n\
482 --localize-hidden Turn all ELF hidden symbols into locals\n\
483 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
484 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
485 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
486 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
487 --weaken Force all global symbols to be marked as weak\n\
488 -w --wildcard Permit wildcard in symbol comparison\n\
489 -x --discard-all Remove all non-global symbols\n\
490 -X --discard-locals Remove any compiler-generated symbols\n\
491 -i --interleave <number> Only copy one out of every <number> bytes\n\
492 -b --byte <num> Select byte <num> in every interleaved block\n\
493 --gap-fill <val> Fill gaps between sections with <val>\n\
494 --pad-to <addr> Pad the last section up to address <addr>\n\
495 --set-start <addr> Set the start address to <addr>\n\
496 {--change-start|--adjust-start} <incr>\n\
497 Add <incr> to the start address\n\
498 {--change-addresses|--adjust-vma} <incr>\n\
499 Add <incr> to LMA, VMA and start addresses\n\
500 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
501 Change LMA and VMA of section <name> by <val>\n\
502 --change-section-lma <name>{=|+|-}<val>\n\
503 Change the LMA of section <name> by <val>\n\
504 --change-section-vma <name>{=|+|-}<val>\n\
505 Change the VMA of section <name> by <val>\n\
506 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
507 Warn if a named section does not exist\n\
508 --set-section-flags <name>=<flags>\n\
509 Set section <name>'s properties to <flags>\n\
510 --add-section <name>=<file> Add section <name> found in <file> to output\n\
511 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
512 --long-section-names {enable|disable|keep}\n\
513 Handle long section names in Coff objects.\n\
514 --change-leading-char Force output format's leading character style\n\
515 --remove-leading-char Remove leading character from global symbols\n\
516 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
517 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
518 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
519 listed in <file>\n\
520 --srec-len <number> Restrict the length of generated Srecords\n\
521 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
522 --strip-symbols <file> -N for all symbols listed in <file>\n\
523 --strip-unneeded-symbols <file>\n\
524 --strip-unneeded-symbol for all symbols listed\n\
525 in <file>\n\
526 --keep-symbols <file> -K for all symbols listed in <file>\n\
527 --localize-symbols <file> -L for all symbols listed in <file>\n\
528 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
529 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
530 --weaken-symbols <file> -W for all symbols listed in <file>\n\
531 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
532 --writable-text Mark the output text as writable\n\
533 --readonly-text Make the output text write protected\n\
534 --pure Mark the output file as demand paged\n\
535 --impure Mark the output file as impure\n\
536 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
537 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
538 --prefix-alloc-sections <prefix>\n\
539 Add <prefix> to start of every allocatable\n\
540 section name\n\
541 --file-alignment <num> Set PE file alignment to <num>\n\
542 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
543 <commit>\n\
544 --image-base <address> Set PE image base to <address>\n\
545 --section-alignment <num> Set PE section alignment to <num>\n\
546 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
547 <commit>\n\
548 --subsystem <name>[:<version>]\n\
549 Set PE subsystem to <name> [& <version>]\n]\
550 -v --verbose List all object files modified\n\
551 @<file> Read options from <file>\n\
552 -V --version Display this program's version number\n\
553 -h --help Display this output\n\
554 --info List object formats & architectures supported\n\
555 "));
556 list_supported_targets (program_name, stream);
557 if (REPORT_BUGS_TO[0] && exit_status == 0)
558 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
559 exit (exit_status);
562 static void
563 strip_usage (FILE *stream, int exit_status)
565 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
566 fprintf (stream, _(" Removes symbols and sections from files\n"));
567 fprintf (stream, _(" The options are:\n"));
568 fprintf (stream, _("\
569 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
570 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
571 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
572 -p --preserve-dates Copy modified/access timestamps to the output\n\
573 -R --remove-section=<name> Remove section <name> from the output\n\
574 -s --strip-all Remove all symbol and relocation information\n\
575 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
576 --strip-unneeded Remove all symbols not needed by relocations\n\
577 --only-keep-debug Strip everything but the debug information\n\
578 -N --strip-symbol=<name> Do not copy symbol <name>\n\
579 -K --keep-symbol=<name> Do not strip symbol <name>\n\
580 --keep-file-symbols Do not strip file symbol(s)\n\
581 -w --wildcard Permit wildcard in symbol comparison\n\
582 -x --discard-all Remove all non-global symbols\n\
583 -X --discard-locals Remove any compiler-generated symbols\n\
584 -v --verbose List all object files modified\n\
585 -V --version Display this program's version number\n\
586 -h --help Display this output\n\
587 --info List object formats & architectures supported\n\
588 -o <file> Place stripped output into <file>\n\
589 "));
591 list_supported_targets (program_name, stream);
592 if (REPORT_BUGS_TO[0] && exit_status == 0)
593 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
594 exit (exit_status);
597 /* Parse section flags into a flagword, with a fatal error if the
598 string can't be parsed. */
600 static flagword
601 parse_flags (const char *s)
603 flagword ret;
604 const char *snext;
605 int len;
607 ret = SEC_NO_FLAGS;
611 snext = strchr (s, ',');
612 if (snext == NULL)
613 len = strlen (s);
614 else
616 len = snext - s;
617 ++snext;
620 if (0) ;
621 #define PARSE_FLAG(fname,fval) \
622 else if (strncasecmp (fname, s, len) == 0) ret |= fval
623 PARSE_FLAG ("alloc", SEC_ALLOC);
624 PARSE_FLAG ("load", SEC_LOAD);
625 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
626 PARSE_FLAG ("readonly", SEC_READONLY);
627 PARSE_FLAG ("debug", SEC_DEBUGGING);
628 PARSE_FLAG ("code", SEC_CODE);
629 PARSE_FLAG ("data", SEC_DATA);
630 PARSE_FLAG ("rom", SEC_ROM);
631 PARSE_FLAG ("share", SEC_COFF_SHARED);
632 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
633 #undef PARSE_FLAG
634 else
636 char *copy;
638 copy = (char *) xmalloc (len + 1);
639 strncpy (copy, s, len);
640 copy[len] = '\0';
641 non_fatal (_("unrecognized section flag `%s'"), copy);
642 fatal (_("supported flags: %s"),
643 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
646 s = snext;
648 while (s != NULL);
650 return ret;
653 /* Find and optionally add an entry in the change_sections list. */
655 static struct section_list *
656 find_section_list (const char *name, bfd_boolean add)
658 struct section_list *p;
660 for (p = change_sections; p != NULL; p = p->next)
661 if (strcmp (p->name, name) == 0)
662 return p;
664 if (! add)
665 return NULL;
667 p = (struct section_list *) xmalloc (sizeof (struct section_list));
668 p->name = name;
669 p->used = FALSE;
670 p->remove = FALSE;
671 p->copy = FALSE;
672 p->change_vma = CHANGE_IGNORE;
673 p->change_lma = CHANGE_IGNORE;
674 p->vma_val = 0;
675 p->lma_val = 0;
676 p->set_flags = FALSE;
677 p->flags = 0;
679 p->next = change_sections;
680 change_sections = p;
682 return p;
685 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
687 static int
688 eq_string (const void *s1, const void *s2)
690 return strcmp ((const char *) s1, (const char *) s2) == 0;
693 static htab_t
694 create_symbol_htab (void)
696 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
699 static void
700 create_symbol_htabs (void)
702 strip_specific_htab = create_symbol_htab ();
703 strip_unneeded_htab = create_symbol_htab ();
704 keep_specific_htab = create_symbol_htab ();
705 localize_specific_htab = create_symbol_htab ();
706 globalize_specific_htab = create_symbol_htab ();
707 keepglobal_specific_htab = create_symbol_htab ();
708 weaken_specific_htab = create_symbol_htab ();
711 /* Add a symbol to strip_specific_list. */
713 static void
714 add_specific_symbol (const char *name, htab_t htab)
716 *htab_find_slot (htab, name, INSERT) = (char *) name;
719 /* Add symbols listed in `filename' to strip_specific_list. */
721 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
722 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
724 static void
725 add_specific_symbols (const char *filename, htab_t htab)
727 off_t size;
728 FILE * f;
729 char * line;
730 char * buffer;
731 unsigned int line_count;
733 size = get_file_size (filename);
734 if (size == 0)
736 status = 1;
737 return;
740 buffer = (char *) xmalloc (size + 2);
741 f = fopen (filename, FOPEN_RT);
742 if (f == NULL)
743 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
745 if (fread (buffer, 1, size, f) == 0 || ferror (f))
746 fatal (_("%s: fread failed"), filename);
748 fclose (f);
749 buffer [size] = '\n';
750 buffer [size + 1] = '\0';
752 line_count = 1;
754 for (line = buffer; * line != '\0'; line ++)
756 char * eol;
757 char * name;
758 char * name_end;
759 int finished = FALSE;
761 for (eol = line;; eol ++)
763 switch (* eol)
765 case '\n':
766 * eol = '\0';
767 /* Cope with \n\r. */
768 if (eol[1] == '\r')
769 ++ eol;
770 finished = TRUE;
771 break;
773 case '\r':
774 * eol = '\0';
775 /* Cope with \r\n. */
776 if (eol[1] == '\n')
777 ++ eol;
778 finished = TRUE;
779 break;
781 case 0:
782 finished = TRUE;
783 break;
785 case '#':
786 /* Line comment, Terminate the line here, in case a
787 name is present and then allow the rest of the
788 loop to find the real end of the line. */
789 * eol = '\0';
790 break;
792 default:
793 break;
796 if (finished)
797 break;
800 /* A name may now exist somewhere between 'line' and 'eol'.
801 Strip off leading whitespace and trailing whitespace,
802 then add it to the list. */
803 for (name = line; IS_WHITESPACE (* name); name ++)
805 for (name_end = name;
806 (! IS_WHITESPACE (* name_end))
807 && (! IS_LINE_TERMINATOR (* name_end));
808 name_end ++)
811 if (! IS_LINE_TERMINATOR (* name_end))
813 char * extra;
815 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
818 if (! IS_LINE_TERMINATOR (* extra))
819 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
820 filename, line_count);
823 * name_end = '\0';
825 if (name_end > name)
826 add_specific_symbol (name, htab);
828 /* Advance line pointer to end of line. The 'eol ++' in the for
829 loop above will then advance us to the start of the next line. */
830 line = eol;
831 line_count ++;
835 /* See whether a symbol should be stripped or kept
836 based on strip_specific_list and keep_symbols. */
838 static int
839 is_specified_symbol_predicate (void **slot, void *data)
841 struct is_specified_symbol_predicate_data *d =
842 (struct is_specified_symbol_predicate_data *) data;
843 const char *slot_name = (char *) *slot;
845 if (*slot_name != '!')
847 if (! fnmatch (slot_name, d->name, 0))
849 d->found = TRUE;
850 /* Stop traversal. */
851 return 0;
854 else
856 if (fnmatch (slot_name + 1, d->name, 0))
858 d->found = TRUE;
859 /* Stop traversal. */
860 return 0;
864 /* Continue traversal. */
865 return 1;
868 static bfd_boolean
869 is_specified_symbol (const char *name, htab_t htab)
871 if (wildcard)
873 struct is_specified_symbol_predicate_data data;
875 data.name = name;
876 data.found = FALSE;
878 htab_traverse (htab, is_specified_symbol_predicate, &data);
880 return data.found;
883 return htab_find (htab, name) != NULL;
886 /* Return a pointer to the symbol used as a signature for GROUP. */
888 static asymbol *
889 group_signature (asection *group)
891 bfd *abfd = group->owner;
892 Elf_Internal_Shdr *ghdr;
894 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
895 return NULL;
897 ghdr = &elf_section_data (group)->this_hdr;
898 if (ghdr->sh_link < elf_numsections (abfd))
900 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
901 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
903 if (symhdr->sh_type == SHT_SYMTAB
904 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
905 return isympp[ghdr->sh_info - 1];
907 return NULL;
910 /* See if a section is being removed. */
912 static bfd_boolean
913 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
915 if (sections_removed || sections_copied)
917 struct section_list *p;
919 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
921 if (sections_removed && p != NULL && p->remove)
922 return TRUE;
923 if (sections_copied && (p == NULL || ! p->copy))
924 return TRUE;
927 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
929 if (strip_symbols == STRIP_DEBUG
930 || strip_symbols == STRIP_UNNEEDED
931 || strip_symbols == STRIP_ALL
932 || discard_locals == LOCALS_ALL
933 || convert_debugging)
934 return TRUE;
936 if (strip_symbols == STRIP_NONDEBUG)
937 return FALSE;
940 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
942 asymbol *gsym;
943 const char *gname;
945 /* PR binutils/3166
946 Group sections look like debugging sections but they are not.
947 (They have a non-zero size but they are not ALLOCated). */
948 if (strip_symbols == STRIP_NONDEBUG)
949 return TRUE;
951 /* PR binutils/3181
952 If we are going to strip the group signature symbol, then
953 strip the group section too. */
954 gsym = group_signature (sec);
955 if (gsym != NULL)
956 gname = gsym->name;
957 else
958 gname = sec->name;
959 if ((strip_symbols == STRIP_ALL
960 && !is_specified_symbol (gname, keep_specific_htab))
961 || is_specified_symbol (gname, strip_specific_htab))
962 return TRUE;
965 return FALSE;
968 /* Return true if SYM is a hidden symbol. */
970 static bfd_boolean
971 is_hidden_symbol (asymbol *sym)
973 elf_symbol_type *elf_sym;
975 elf_sym = elf_symbol_from (sym->the_bfd, sym);
976 if (elf_sym != NULL)
977 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
979 case STV_HIDDEN:
980 case STV_INTERNAL:
981 return TRUE;
983 return FALSE;
986 /* Choose which symbol entries to copy; put the result in OSYMS.
987 We don't copy in place, because that confuses the relocs.
988 Return the number of symbols to print. */
990 static unsigned int
991 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
992 asymbol **isyms, long symcount)
994 asymbol **from = isyms, **to = osyms;
995 long src_count = 0, dst_count = 0;
996 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
998 for (; src_count < symcount; src_count++)
1000 asymbol *sym = from[src_count];
1001 flagword flags = sym->flags;
1002 char *name = (char *) bfd_asymbol_name (sym);
1003 bfd_boolean keep;
1004 bfd_boolean used_in_reloc = FALSE;
1005 bfd_boolean undefined;
1006 bfd_boolean rem_leading_char;
1007 bfd_boolean add_leading_char;
1009 undefined = bfd_is_und_section (bfd_get_section (sym));
1011 if (redefine_sym_list)
1013 char *old_name, *new_name;
1015 old_name = (char *) bfd_asymbol_name (sym);
1016 new_name = (char *) lookup_sym_redefinition (old_name);
1017 bfd_asymbol_name (sym) = new_name;
1018 name = new_name;
1021 /* Check if we will remove the current leading character. */
1022 rem_leading_char =
1023 (name[0] == bfd_get_symbol_leading_char (abfd))
1024 && (change_leading_char
1025 || (remove_leading_char
1026 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1027 || undefined
1028 || bfd_is_com_section (bfd_get_section (sym)))));
1030 /* Check if we will add a new leading character. */
1031 add_leading_char =
1032 change_leading_char
1033 && (bfd_get_symbol_leading_char (obfd) != '\0')
1034 && (bfd_get_symbol_leading_char (abfd) == '\0'
1035 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1037 /* Short circuit for change_leading_char if we can do it in-place. */
1038 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1040 name[0] = bfd_get_symbol_leading_char (obfd);
1041 bfd_asymbol_name (sym) = name;
1042 rem_leading_char = FALSE;
1043 add_leading_char = FALSE;
1046 /* Remove leading char. */
1047 if (rem_leading_char)
1048 bfd_asymbol_name (sym) = ++name;
1050 /* Add new leading char and/or prefix. */
1051 if (add_leading_char || prefix_symbols_string)
1053 char *n, *ptr;
1055 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1056 + strlen (name) + 1);
1057 if (add_leading_char)
1058 *ptr++ = bfd_get_symbol_leading_char (obfd);
1060 if (prefix_symbols_string)
1062 strcpy (ptr, prefix_symbols_string);
1063 ptr += strlen (prefix_symbols_string);
1066 strcpy (ptr, name);
1067 bfd_asymbol_name (sym) = n;
1068 name = n;
1071 if (strip_symbols == STRIP_ALL)
1072 keep = FALSE;
1073 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1074 || ((flags & BSF_SECTION_SYM) != 0
1075 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1076 & BSF_KEEP) != 0))
1078 keep = TRUE;
1079 used_in_reloc = TRUE;
1081 else if (relocatable /* Relocatable file. */
1082 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1083 || bfd_is_com_section (bfd_get_section (sym))))
1084 keep = TRUE;
1085 else if (bfd_decode_symclass (sym) == 'I')
1086 /* Global symbols in $idata sections need to be retained
1087 even if relocatable is FALSE. External users of the
1088 library containing the $idata section may reference these
1089 symbols. */
1090 keep = TRUE;
1091 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1092 || (flags & BSF_WEAK) != 0
1093 || undefined
1094 || bfd_is_com_section (bfd_get_section (sym)))
1095 keep = strip_symbols != STRIP_UNNEEDED;
1096 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1097 keep = (strip_symbols != STRIP_DEBUG
1098 && strip_symbols != STRIP_UNNEEDED
1099 && ! convert_debugging);
1100 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1101 /* COMDAT sections store special information in local
1102 symbols, so we cannot risk stripping any of them. */
1103 keep = TRUE;
1104 else /* Local symbol. */
1105 keep = (strip_symbols != STRIP_UNNEEDED
1106 && (discard_locals != LOCALS_ALL
1107 && (discard_locals != LOCALS_START_L
1108 || ! bfd_is_local_label (abfd, sym))));
1110 if (keep && is_specified_symbol (name, strip_specific_htab))
1112 /* There are multiple ways to set 'keep' above, but if it
1113 was the relocatable symbol case, then that's an error. */
1114 if (used_in_reloc)
1116 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1117 status = 1;
1119 else
1120 keep = FALSE;
1123 if (keep
1124 && !(flags & BSF_KEEP)
1125 && is_specified_symbol (name, strip_unneeded_htab))
1126 keep = FALSE;
1128 if (!keep
1129 && ((keep_file_symbols && (flags & BSF_FILE))
1130 || is_specified_symbol (name, keep_specific_htab)))
1131 keep = TRUE;
1133 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1134 keep = FALSE;
1136 if (keep)
1138 if ((flags & BSF_GLOBAL) != 0
1139 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1141 sym->flags &= ~ BSF_GLOBAL;
1142 sym->flags |= BSF_WEAK;
1145 if (!undefined
1146 && (flags & (BSF_GLOBAL | BSF_WEAK))
1147 && (is_specified_symbol (name, localize_specific_htab)
1148 || (htab_elements (keepglobal_specific_htab) != 0
1149 && ! is_specified_symbol (name, keepglobal_specific_htab))
1150 || (localize_hidden && is_hidden_symbol (sym))))
1152 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1153 sym->flags |= BSF_LOCAL;
1156 if (!undefined
1157 && (flags & BSF_LOCAL)
1158 && is_specified_symbol (name, globalize_specific_htab))
1160 sym->flags &= ~ BSF_LOCAL;
1161 sym->flags |= BSF_GLOBAL;
1164 to[dst_count++] = sym;
1168 to[dst_count] = NULL;
1170 return dst_count;
1173 /* Find the redefined name of symbol SOURCE. */
1175 static const char *
1176 lookup_sym_redefinition (const char *source)
1178 struct redefine_node *list;
1180 for (list = redefine_sym_list; list != NULL; list = list->next)
1181 if (strcmp (source, list->source) == 0)
1182 return list->target;
1184 return source;
1187 /* Add a node to a symbol redefine list. */
1189 static void
1190 redefine_list_append (const char *cause, const char *source, const char *target)
1192 struct redefine_node **p;
1193 struct redefine_node *list;
1194 struct redefine_node *new_node;
1196 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1198 if (strcmp (source, list->source) == 0)
1199 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1200 cause, source);
1202 if (strcmp (target, list->target) == 0)
1203 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1204 cause, target);
1207 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1209 new_node->source = strdup (source);
1210 new_node->target = strdup (target);
1211 new_node->next = NULL;
1213 *p = new_node;
1216 /* Handle the --redefine-syms option. Read lines containing "old new"
1217 from the file, and add them to the symbol redefine list. */
1219 static void
1220 add_redefine_syms_file (const char *filename)
1222 FILE *file;
1223 char *buf;
1224 size_t bufsize;
1225 size_t len;
1226 size_t outsym_off;
1227 int c, lineno;
1229 file = fopen (filename, "r");
1230 if (file == NULL)
1231 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1232 filename, strerror (errno));
1234 bufsize = 100;
1235 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
1237 lineno = 1;
1238 c = getc (file);
1239 len = 0;
1240 outsym_off = 0;
1241 while (c != EOF)
1243 /* Collect the input symbol name. */
1244 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1246 if (c == '#')
1247 goto comment;
1248 buf[len++] = c;
1249 if (len >= bufsize)
1251 bufsize *= 2;
1252 buf = (char *) xrealloc (buf, bufsize + 1);
1254 c = getc (file);
1256 buf[len++] = '\0';
1257 if (c == EOF)
1258 break;
1260 /* Eat white space between the symbol names. */
1261 while (IS_WHITESPACE (c))
1262 c = getc (file);
1263 if (c == '#' || IS_LINE_TERMINATOR (c))
1264 goto comment;
1265 if (c == EOF)
1266 break;
1268 /* Collect the output symbol name. */
1269 outsym_off = len;
1270 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1272 if (c == '#')
1273 goto comment;
1274 buf[len++] = c;
1275 if (len >= bufsize)
1277 bufsize *= 2;
1278 buf = (char *) xrealloc (buf, bufsize + 1);
1280 c = getc (file);
1282 buf[len++] = '\0';
1283 if (c == EOF)
1284 break;
1286 /* Eat white space at end of line. */
1287 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1288 c = getc (file);
1289 if (c == '#')
1290 goto comment;
1291 /* Handle \r\n. */
1292 if ((c == '\r' && (c = getc (file)) == '\n')
1293 || c == '\n' || c == EOF)
1295 end_of_line:
1296 /* Append the redefinition to the list. */
1297 if (buf[0] != '\0')
1298 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1300 lineno++;
1301 len = 0;
1302 outsym_off = 0;
1303 if (c == EOF)
1304 break;
1305 c = getc (file);
1306 continue;
1308 else
1309 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1310 comment:
1311 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1312 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1313 buf[len++] = '\0';
1315 /* Eat the rest of the line and finish it. */
1316 while (c != '\n' && c != EOF)
1317 c = getc (file);
1318 goto end_of_line;
1321 if (len != 0)
1322 fatal (_("%s:%d: premature end of file"), filename, lineno);
1324 free (buf);
1327 /* Copy unkown object file IBFD onto OBFD.
1328 Returns TRUE upon success, FALSE otherwise. */
1330 static bfd_boolean
1331 copy_unknown_object (bfd *ibfd, bfd *obfd)
1333 char *cbuf;
1334 int tocopy;
1335 long ncopied;
1336 long size;
1337 struct stat buf;
1339 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1341 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1342 return FALSE;
1345 size = buf.st_size;
1346 if (size < 0)
1348 non_fatal (_("stat returns negative size for `%s'"),
1349 bfd_get_archive_filename (ibfd));
1350 return FALSE;
1353 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1355 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1356 return FALSE;
1359 if (verbose)
1360 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1361 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1363 cbuf = (char *) xmalloc (BUFSIZE);
1364 ncopied = 0;
1365 while (ncopied < size)
1367 tocopy = size - ncopied;
1368 if (tocopy > BUFSIZE)
1369 tocopy = BUFSIZE;
1371 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1372 != (bfd_size_type) tocopy)
1374 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1375 free (cbuf);
1376 return FALSE;
1379 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1380 != (bfd_size_type) tocopy)
1382 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1383 free (cbuf);
1384 return FALSE;
1387 ncopied += tocopy;
1390 chmod (bfd_get_filename (obfd), buf.st_mode);
1391 free (cbuf);
1392 return TRUE;
1395 /* Copy object file IBFD onto OBFD.
1396 Returns TRUE upon success, FALSE otherwise. */
1398 static bfd_boolean
1399 copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
1401 bfd_vma start;
1402 long symcount;
1403 asection **osections = NULL;
1404 asection *gnu_debuglink_section = NULL;
1405 bfd_size_type *gaps = NULL;
1406 bfd_size_type max_gap = 0;
1407 long symsize;
1408 void *dhandle;
1409 enum bfd_architecture iarch;
1410 unsigned int imach;
1412 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1413 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1414 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1415 fatal (_("Unable to change endianness of input file(s)"));
1417 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1419 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1420 return FALSE;
1423 if (verbose)
1424 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1425 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1426 bfd_get_filename (obfd), bfd_get_target (obfd));
1428 if (extract_symbol)
1429 start = 0;
1430 else
1432 if (set_start_set)
1433 start = set_start;
1434 else
1435 start = bfd_get_start_address (ibfd);
1436 start += change_start;
1439 /* Neither the start address nor the flags
1440 need to be set for a core file. */
1441 if (bfd_get_format (obfd) != bfd_core)
1443 flagword flags;
1445 flags = bfd_get_file_flags (ibfd);
1446 flags |= bfd_flags_to_set;
1447 flags &= ~bfd_flags_to_clear;
1448 flags &= bfd_applicable_file_flags (obfd);
1450 if (strip_symbols == STRIP_ALL)
1451 flags &= ~HAS_RELOC;
1453 if (!bfd_set_start_address (obfd, start)
1454 || !bfd_set_file_flags (obfd, flags))
1456 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1457 return FALSE;
1461 /* Copy architecture of input file to output file. */
1462 iarch = bfd_get_arch (ibfd);
1463 imach = bfd_get_mach (ibfd);
1464 if (input_arch)
1466 if (bfd_get_arch_info (ibfd) == NULL
1467 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
1469 iarch = input_arch->arch;
1470 imach = input_arch->mach;
1472 else
1473 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1474 bfd_get_archive_filename (ibfd));
1476 if (!bfd_set_arch_mach (obfd, iarch, imach)
1477 && (ibfd->target_defaulted
1478 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1480 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1481 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1482 bfd_get_archive_filename (ibfd));
1483 else
1484 non_fatal (_("Output file cannot represent architecture `%s'"),
1485 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1486 bfd_get_mach (ibfd)));
1487 return FALSE;
1490 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1492 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1493 return FALSE;
1496 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1497 && bfd_pei_p (obfd))
1499 /* Set up PE parameters. */
1500 pe_data_type *pe = pe_data (obfd);
1502 /* Copy PE parameters before changing them. */
1503 if (ibfd->xvec->flavour == bfd_target_coff_flavour
1504 && bfd_pei_p (ibfd))
1505 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1507 if (pe_file_alignment != (bfd_vma) -1)
1508 pe->pe_opthdr.FileAlignment = pe_file_alignment;
1509 else
1510 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
1512 if (pe_heap_commit != (bfd_vma) -1)
1513 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
1515 if (pe_heap_reserve != (bfd_vma) -1)
1516 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
1518 if (pe_image_base != (bfd_vma) -1)
1519 pe->pe_opthdr.ImageBase = pe_image_base;
1521 if (pe_section_alignment != (bfd_vma) -1)
1522 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
1523 else
1524 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
1526 if (pe_stack_commit != (bfd_vma) -1)
1527 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
1529 if (pe_stack_reserve != (bfd_vma) -1)
1530 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
1532 if (pe_subsystem != -1)
1533 pe->pe_opthdr.Subsystem = pe_subsystem;
1535 if (pe_major_subsystem_version != -1)
1536 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
1538 if (pe_minor_subsystem_version != -1)
1539 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
1541 if (pe_file_alignment > pe_section_alignment)
1543 char file_alignment[20], section_alignment[20];
1545 sprintf_vma (file_alignment, pe_file_alignment);
1546 sprintf_vma (section_alignment, pe_section_alignment);
1547 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1549 file_alignment, section_alignment);
1553 if (isympp)
1554 free (isympp);
1556 if (osympp != isympp)
1557 free (osympp);
1559 isympp = NULL;
1560 osympp = NULL;
1562 symsize = bfd_get_symtab_upper_bound (ibfd);
1563 if (symsize < 0)
1565 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1566 return FALSE;
1569 osympp = isympp = (asymbol **) xmalloc (symsize);
1570 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1571 if (symcount < 0)
1573 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1574 return FALSE;
1577 /* BFD mandates that all output sections be created and sizes set before
1578 any output is done. Thus, we traverse all sections multiple times. */
1579 bfd_map_over_sections (ibfd, setup_section, obfd);
1581 if (!extract_symbol)
1582 setup_bfd_headers (ibfd, obfd);
1584 if (add_sections != NULL)
1586 struct section_add *padd;
1587 struct section_list *pset;
1589 for (padd = add_sections; padd != NULL; padd = padd->next)
1591 flagword flags;
1593 pset = find_section_list (padd->name, FALSE);
1594 if (pset != NULL)
1595 pset->used = TRUE;
1597 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1598 if (pset != NULL && pset->set_flags)
1599 flags = pset->flags | SEC_HAS_CONTENTS;
1601 /* bfd_make_section_with_flags() does not return very helpful
1602 error codes, so check for the most likely user error first. */
1603 if (bfd_get_section_by_name (obfd, padd->name))
1605 bfd_nonfatal_message (NULL, obfd, NULL,
1606 _("can't add section '%s'"), padd->name);
1607 return FALSE;
1609 else
1611 /* We use LINKER_CREATED here so that the backend hooks
1612 will create any special section type information,
1613 instead of presuming we know what we're doing merely
1614 because we set the flags. */
1615 padd->section = bfd_make_section_with_flags
1616 (obfd, padd->name, flags | SEC_LINKER_CREATED);
1617 if (padd->section == NULL)
1619 bfd_nonfatal_message (NULL, obfd, NULL,
1620 _("can't create section `%s'"),
1621 padd->name);
1622 return FALSE;
1626 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1628 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1629 return FALSE;
1632 if (pset != NULL)
1634 if (pset->change_vma != CHANGE_IGNORE)
1635 if (! bfd_set_section_vma (obfd, padd->section,
1636 pset->vma_val))
1638 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1639 return FALSE;
1642 if (pset->change_lma != CHANGE_IGNORE)
1644 padd->section->lma = pset->lma_val;
1646 if (! bfd_set_section_alignment
1647 (obfd, padd->section,
1648 bfd_section_alignment (obfd, padd->section)))
1650 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1651 return FALSE;
1658 if (gnu_debuglink_filename != NULL)
1660 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1661 (obfd, gnu_debuglink_filename);
1663 if (gnu_debuglink_section == NULL)
1665 bfd_nonfatal_message (NULL, obfd, NULL,
1666 _("cannot create debug link section `%s'"),
1667 gnu_debuglink_filename);
1668 return FALSE;
1671 /* Special processing for PE format files. We
1672 have no way to distinguish PE from COFF here. */
1673 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1675 bfd_vma debuglink_vma;
1676 asection * highest_section;
1677 asection * sec;
1679 /* The PE spec requires that all sections be adjacent and sorted
1680 in ascending order of VMA. It also specifies that debug
1681 sections should be last. This is despite the fact that debug
1682 sections are not loaded into memory and so in theory have no
1683 use for a VMA.
1685 This means that the debuglink section must be given a non-zero
1686 VMA which makes it contiguous with other debug sections. So
1687 walk the current section list, find the section with the
1688 highest VMA and start the debuglink section after that one. */
1689 for (sec = obfd->sections, highest_section = NULL;
1690 sec != NULL;
1691 sec = sec->next)
1692 if (sec->vma > 0
1693 && (highest_section == NULL
1694 || sec->vma > highest_section->vma))
1695 highest_section = sec;
1697 if (highest_section)
1698 debuglink_vma = BFD_ALIGN (highest_section->vma
1699 + highest_section->size,
1700 /* FIXME: We ought to be using
1701 COFF_PAGE_SIZE here or maybe
1702 bfd_get_section_alignment() (if it
1703 was set) but since this is for PE
1704 and we know the required alignment
1705 it is easier just to hard code it. */
1706 0x1000);
1707 else
1708 /* Umm, not sure what to do in this case. */
1709 debuglink_vma = 0x1000;
1711 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1715 if (bfd_count_sections (obfd) != 0
1716 && (gap_fill_set || pad_to_set))
1718 asection **set;
1719 unsigned int c, i;
1721 /* We must fill in gaps between the sections and/or we must pad
1722 the last section to a specified address. We do this by
1723 grabbing a list of the sections, sorting them by VMA, and
1724 increasing the section sizes as required to fill the gaps.
1725 We write out the gap contents below. */
1727 c = bfd_count_sections (obfd);
1728 osections = (asection **) xmalloc (c * sizeof (asection *));
1729 set = osections;
1730 bfd_map_over_sections (obfd, get_sections, &set);
1732 qsort (osections, c, sizeof (asection *), compare_section_lma);
1734 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
1735 memset (gaps, 0, c * sizeof (bfd_size_type));
1737 if (gap_fill_set)
1739 for (i = 0; i < c - 1; i++)
1741 flagword flags;
1742 bfd_size_type size;
1743 bfd_vma gap_start, gap_stop;
1745 flags = bfd_get_section_flags (obfd, osections[i]);
1746 if ((flags & SEC_HAS_CONTENTS) == 0
1747 || (flags & SEC_LOAD) == 0)
1748 continue;
1750 size = bfd_section_size (obfd, osections[i]);
1751 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1752 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1753 if (gap_start < gap_stop)
1755 if (! bfd_set_section_size (obfd, osections[i],
1756 size + (gap_stop - gap_start)))
1758 bfd_nonfatal_message (NULL, obfd, osections[i],
1759 _("Can't fill gap after section"));
1760 status = 1;
1761 break;
1763 gaps[i] = gap_stop - gap_start;
1764 if (max_gap < gap_stop - gap_start)
1765 max_gap = gap_stop - gap_start;
1770 if (pad_to_set)
1772 bfd_vma lma;
1773 bfd_size_type size;
1775 lma = bfd_section_lma (obfd, osections[c - 1]);
1776 size = bfd_section_size (obfd, osections[c - 1]);
1777 if (lma + size < pad_to)
1779 if (! bfd_set_section_size (obfd, osections[c - 1],
1780 pad_to - lma))
1782 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
1783 _("can't add padding"));
1784 status = 1;
1786 else
1788 gaps[c - 1] = pad_to - (lma + size);
1789 if (max_gap < pad_to - (lma + size))
1790 max_gap = pad_to - (lma + size);
1796 /* Symbol filtering must happen after the output sections
1797 have been created, but before their contents are set. */
1798 dhandle = NULL;
1799 if (convert_debugging)
1800 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
1802 if (strip_symbols == STRIP_DEBUG
1803 || strip_symbols == STRIP_ALL
1804 || strip_symbols == STRIP_UNNEEDED
1805 || strip_symbols == STRIP_NONDEBUG
1806 || discard_locals != LOCALS_UNDEF
1807 || localize_hidden
1808 || htab_elements (strip_specific_htab) != 0
1809 || htab_elements (keep_specific_htab) != 0
1810 || htab_elements (localize_specific_htab) != 0
1811 || htab_elements (globalize_specific_htab) != 0
1812 || htab_elements (keepglobal_specific_htab) != 0
1813 || htab_elements (weaken_specific_htab) != 0
1814 || prefix_symbols_string
1815 || sections_removed
1816 || sections_copied
1817 || convert_debugging
1818 || change_leading_char
1819 || remove_leading_char
1820 || redefine_sym_list
1821 || weaken)
1823 /* Mark symbols used in output relocations so that they
1824 are kept, even if they are local labels or static symbols.
1826 Note we iterate over the input sections examining their
1827 relocations since the relocations for the output sections
1828 haven't been set yet. mark_symbols_used_in_relocations will
1829 ignore input sections which have no corresponding output
1830 section. */
1831 if (strip_symbols != STRIP_ALL)
1832 bfd_map_over_sections (ibfd,
1833 mark_symbols_used_in_relocations,
1834 isympp);
1835 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1836 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1839 if (convert_debugging && dhandle != NULL)
1841 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1843 status = 1;
1844 return FALSE;
1848 bfd_set_symtab (obfd, osympp, symcount);
1850 /* This has to happen after the symbol table has been set. */
1851 bfd_map_over_sections (ibfd, copy_section, obfd);
1853 if (add_sections != NULL)
1855 struct section_add *padd;
1857 for (padd = add_sections; padd != NULL; padd = padd->next)
1859 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1860 0, padd->size))
1862 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1863 return FALSE;
1868 if (gnu_debuglink_filename != NULL)
1870 if (! bfd_fill_in_gnu_debuglink_section
1871 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1873 bfd_nonfatal_message (NULL, obfd, NULL,
1874 _("cannot fill debug link section `%s'"),
1875 gnu_debuglink_filename);
1876 return FALSE;
1880 if (gap_fill_set || pad_to_set)
1882 bfd_byte *buf;
1883 int c, i;
1885 /* Fill in the gaps. */
1886 if (max_gap > 8192)
1887 max_gap = 8192;
1888 buf = (bfd_byte *) xmalloc (max_gap);
1889 memset (buf, gap_fill, max_gap);
1891 c = bfd_count_sections (obfd);
1892 for (i = 0; i < c; i++)
1894 if (gaps[i] != 0)
1896 bfd_size_type left;
1897 file_ptr off;
1899 left = gaps[i];
1900 off = bfd_section_size (obfd, osections[i]) - left;
1902 while (left > 0)
1904 bfd_size_type now;
1906 if (left > 8192)
1907 now = 8192;
1908 else
1909 now = left;
1911 if (! bfd_set_section_contents (obfd, osections[i], buf,
1912 off, now))
1914 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
1915 return FALSE;
1918 left -= now;
1919 off += now;
1925 /* Do not copy backend data if --extract-symbol is passed; anything
1926 that needs to look at the section contents will fail. */
1927 if (extract_symbol)
1928 return TRUE;
1930 /* Allow the BFD backend to copy any private data it understands
1931 from the input BFD to the output BFD. This is done last to
1932 permit the routine to look at the filtered symbol table, which is
1933 important for the ECOFF code at least. */
1934 if (! bfd_copy_private_bfd_data (ibfd, obfd))
1936 bfd_nonfatal_message (NULL, obfd, NULL,
1937 _("error copying private BFD data"));
1938 return FALSE;
1941 /* Switch to the alternate machine code. We have to do this at the
1942 very end, because we only initialize the header when we create
1943 the first section. */
1944 if (use_alt_mach_code != 0)
1946 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
1948 non_fatal (_("this target does not support %lu alternative machine codes"),
1949 use_alt_mach_code);
1950 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1952 non_fatal (_("treating that number as an absolute e_machine value instead"));
1953 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
1955 else
1956 non_fatal (_("ignoring the alternative value"));
1960 return TRUE;
1963 /* Read each archive element in turn from IBFD, copy the
1964 contents to temp file, and keep the temp file handle.
1965 If 'force_output_target' is TRUE then make sure that
1966 all elements in the new archive are of the type
1967 'output_target'. */
1969 static void
1970 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
1971 bfd_boolean force_output_target,
1972 const bfd_arch_info_type *input_arch)
1974 struct name_list
1976 struct name_list *next;
1977 const char *name;
1978 bfd *obfd;
1979 } *list, *l;
1980 bfd **ptr = &obfd->archive_head;
1981 bfd *this_element;
1982 char *dir;
1983 const char *filename;
1985 /* Make a temp directory to hold the contents. */
1986 dir = make_tempdir (bfd_get_filename (obfd));
1987 if (dir == NULL)
1988 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1989 strerror (errno));
1991 obfd->has_armap = ibfd->has_armap;
1992 obfd->is_thin_archive = ibfd->is_thin_archive;
1994 list = NULL;
1996 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1998 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2000 status = 1;
2001 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
2002 return;
2005 while (!status && this_element != NULL)
2007 char *output_name;
2008 bfd *output_bfd;
2009 bfd *last_element;
2010 struct stat buf;
2011 int stat_status = 0;
2012 bfd_boolean del = TRUE;
2014 /* Create an output file for this member. */
2015 output_name = concat (dir, "/",
2016 bfd_get_filename (this_element), (char *) 0);
2018 /* If the file already exists, make another temp dir. */
2019 if (stat (output_name, &buf) >= 0)
2021 output_name = make_tempdir (output_name);
2022 if (output_name == NULL)
2023 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2024 strerror (errno));
2026 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2027 l->name = output_name;
2028 l->next = list;
2029 l->obfd = NULL;
2030 list = l;
2031 output_name = concat (output_name, "/",
2032 bfd_get_filename (this_element), (char *) 0);
2035 if (preserve_dates)
2037 stat_status = bfd_stat_arch_elt (this_element, &buf);
2039 if (stat_status != 0)
2040 non_fatal (_("internal stat error on %s"),
2041 bfd_get_filename (this_element));
2044 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2045 l->name = output_name;
2046 l->next = list;
2047 l->obfd = NULL;
2048 list = l;
2050 if (bfd_check_format (this_element, bfd_object))
2052 /* PR binutils/3110: Cope with archives
2053 containing multiple target types. */
2054 if (force_output_target)
2055 output_bfd = bfd_openw (output_name, output_target);
2056 else
2057 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
2059 if (output_bfd == NULL)
2061 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2062 status = 1;
2063 return;
2066 del = ! copy_object (this_element, output_bfd, input_arch);
2068 if (! del
2069 || bfd_get_arch (this_element) != bfd_arch_unknown)
2071 if (!bfd_close (output_bfd))
2073 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2074 /* Error in new object file. Don't change archive. */
2075 status = 1;
2078 else
2079 goto copy_unknown_element;
2081 else
2083 bfd_nonfatal_message (NULL, this_element, NULL,
2084 _("Unable to recognise the format of file"));
2086 output_bfd = bfd_openw (output_name, output_target);
2087 copy_unknown_element:
2088 del = !copy_unknown_object (this_element, output_bfd);
2089 if (!bfd_close_all_done (output_bfd))
2091 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2092 /* Error in new object file. Don't change archive. */
2093 status = 1;
2097 if (del)
2099 unlink (output_name);
2100 status = 1;
2102 else
2104 if (preserve_dates && stat_status == 0)
2105 set_times (output_name, &buf);
2107 /* Open the newly output file and attach to our list. */
2108 output_bfd = bfd_openr (output_name, output_target);
2110 l->obfd = output_bfd;
2112 *ptr = output_bfd;
2113 ptr = &output_bfd->archive_next;
2115 last_element = this_element;
2117 this_element = bfd_openr_next_archived_file (ibfd, last_element);
2119 bfd_close (last_element);
2122 *ptr = NULL;
2124 filename = bfd_get_filename (obfd);
2125 if (!bfd_close (obfd))
2127 status = 1;
2128 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2129 return;
2132 filename = bfd_get_filename (ibfd);
2133 if (!bfd_close (ibfd))
2135 status = 1;
2136 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2137 return;
2140 /* Delete all the files that we opened. */
2141 for (l = list; l != NULL; l = l->next)
2143 if (l->obfd == NULL)
2144 rmdir (l->name);
2145 else
2147 bfd_close (l->obfd);
2148 unlink (l->name);
2151 rmdir (dir);
2154 static void
2155 set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
2157 /* This is only relevant to Coff targets. */
2158 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
2160 if (style == KEEP
2161 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
2162 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
2163 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
2167 /* The top-level control. */
2169 static void
2170 copy_file (const char *input_filename, const char *output_filename,
2171 const char *input_target, const char *output_target,
2172 const bfd_arch_info_type *input_arch)
2174 bfd *ibfd;
2175 char **obj_matching;
2176 char **core_matching;
2177 off_t size = get_file_size (input_filename);
2179 if (size < 1)
2181 if (size == 0)
2182 non_fatal (_("error: the input file '%s' is empty"),
2183 input_filename);
2184 status = 1;
2185 return;
2188 /* To allow us to do "strip *" without dying on the first
2189 non-object file, failures are nonfatal. */
2190 ibfd = bfd_openr (input_filename, input_target);
2191 if (ibfd == NULL)
2193 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2194 status = 1;
2195 return;
2198 if (bfd_check_format (ibfd, bfd_archive))
2200 bfd_boolean force_output_target;
2201 bfd *obfd;
2203 /* bfd_get_target does not return the correct value until
2204 bfd_check_format succeeds. */
2205 if (output_target == NULL)
2207 output_target = bfd_get_target (ibfd);
2208 force_output_target = FALSE;
2210 else
2211 force_output_target = TRUE;
2213 obfd = bfd_openw (output_filename, output_target);
2214 if (obfd == NULL)
2216 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2217 status = 1;
2218 return;
2220 /* This is a no-op on non-Coff targets. */
2221 set_long_section_mode (obfd, ibfd, long_section_names);
2223 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
2225 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2227 bfd *obfd;
2228 do_copy:
2230 /* bfd_get_target does not return the correct value until
2231 bfd_check_format succeeds. */
2232 if (output_target == NULL)
2233 output_target = bfd_get_target (ibfd);
2235 obfd = bfd_openw (output_filename, output_target);
2236 if (obfd == NULL)
2238 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2239 status = 1;
2240 return;
2242 /* This is a no-op on non-Coff targets. */
2243 set_long_section_mode (obfd, ibfd, long_section_names);
2245 if (! copy_object (ibfd, obfd, input_arch))
2246 status = 1;
2248 if (!bfd_close (obfd))
2250 status = 1;
2251 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2252 return;
2255 if (!bfd_close (ibfd))
2257 status = 1;
2258 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2259 return;
2262 else
2264 bfd_error_type obj_error = bfd_get_error ();
2265 bfd_error_type core_error;
2267 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2269 /* This probably can't happen.. */
2270 if (obj_error == bfd_error_file_ambiguously_recognized)
2271 free (obj_matching);
2272 goto do_copy;
2275 core_error = bfd_get_error ();
2276 /* Report the object error in preference to the core error. */
2277 if (obj_error != core_error)
2278 bfd_set_error (obj_error);
2280 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2282 if (obj_error == bfd_error_file_ambiguously_recognized)
2284 list_matching_formats (obj_matching);
2285 free (obj_matching);
2287 if (core_error == bfd_error_file_ambiguously_recognized)
2289 list_matching_formats (core_matching);
2290 free (core_matching);
2293 status = 1;
2297 /* Add a name to the section renaming list. */
2299 static void
2300 add_section_rename (const char * old_name, const char * new_name,
2301 flagword flags)
2303 section_rename * srename;
2305 /* Check for conflicts first. */
2306 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2307 if (strcmp (srename->old_name, old_name) == 0)
2309 /* Silently ignore duplicate definitions. */
2310 if (strcmp (srename->new_name, new_name) == 0
2311 && srename->flags == flags)
2312 return;
2314 fatal (_("Multiple renames of section %s"), old_name);
2317 srename = (section_rename *) xmalloc (sizeof (* srename));
2319 srename->old_name = old_name;
2320 srename->new_name = new_name;
2321 srename->flags = flags;
2322 srename->next = section_rename_list;
2324 section_rename_list = srename;
2327 /* Check the section rename list for a new name of the input section
2328 ISECTION. Return the new name if one is found.
2329 Also set RETURNED_FLAGS to the flags to be used for this section. */
2331 static const char *
2332 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2333 flagword * returned_flags)
2335 const char * old_name = bfd_section_name (ibfd, isection);
2336 section_rename * srename;
2338 /* Default to using the flags of the input section. */
2339 * returned_flags = bfd_get_section_flags (ibfd, isection);
2341 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2342 if (strcmp (srename->old_name, old_name) == 0)
2344 if (srename->flags != (flagword) -1)
2345 * returned_flags = srename->flags;
2347 return srename->new_name;
2350 return old_name;
2353 /* Once each of the sections is copied, we may still need to do some
2354 finalization work for private section headers. Do that here. */
2356 static void
2357 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2359 /* Allow the BFD backend to copy any private data it understands
2360 from the input section to the output section. */
2361 if (! bfd_copy_private_header_data (ibfd, obfd))
2363 status = 1;
2364 bfd_nonfatal_message (NULL, ibfd, NULL,
2365 _("error in private header data"));
2366 return;
2369 /* All went well. */
2370 return;
2373 /* Create a section in OBFD with the same
2374 name and attributes as ISECTION in IBFD. */
2376 static void
2377 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2379 bfd *obfd = (bfd *) obfdarg;
2380 struct section_list *p;
2381 sec_ptr osection;
2382 bfd_size_type size;
2383 bfd_vma vma;
2384 bfd_vma lma;
2385 flagword flags;
2386 const char *err;
2387 const char * name;
2388 char *prefix = NULL;
2389 bfd_boolean make_nobits;
2391 if (is_strip_section (ibfd, isection))
2392 return;
2394 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2395 if (p != NULL)
2396 p->used = TRUE;
2398 /* Get the, possibly new, name of the output section. */
2399 name = find_section_rename (ibfd, isection, & flags);
2401 /* Prefix sections. */
2402 if ((prefix_alloc_sections_string)
2403 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2404 prefix = prefix_alloc_sections_string;
2405 else if (prefix_sections_string)
2406 prefix = prefix_sections_string;
2408 if (prefix)
2410 char *n;
2412 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
2413 strcpy (n, prefix);
2414 strcat (n, name);
2415 name = n;
2418 make_nobits = FALSE;
2419 if (p != NULL && p->set_flags)
2420 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2421 else if (strip_symbols == STRIP_NONDEBUG
2422 && (flags & SEC_ALLOC) != 0
2423 && (ibfd->xvec->flavour != bfd_target_elf_flavour
2424 || elf_section_type (isection) != SHT_NOTE))
2426 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2427 if (obfd->xvec->flavour == bfd_target_elf_flavour)
2429 make_nobits = TRUE;
2431 /* Twiddle the input section flags so that it seems to
2432 elf.c:copy_private_bfd_data that section flags have not
2433 changed between input and output sections. This hack
2434 prevents wholesale rewriting of the program headers. */
2435 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2439 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2441 if (osection == NULL)
2443 err = _("failed to create output section");
2444 goto loser;
2447 if (make_nobits)
2448 elf_section_type (osection) = SHT_NOBITS;
2450 size = bfd_section_size (ibfd, isection);
2451 if (copy_byte >= 0)
2452 size = (size + interleave - 1) / interleave;
2453 else if (extract_symbol)
2454 size = 0;
2455 if (! bfd_set_section_size (obfd, osection, size))
2457 err = _("failed to set size");
2458 goto loser;
2461 vma = bfd_section_vma (ibfd, isection);
2462 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2463 vma += p->vma_val;
2464 else if (p != NULL && p->change_vma == CHANGE_SET)
2465 vma = p->vma_val;
2466 else
2467 vma += change_section_address;
2469 if (! bfd_set_section_vma (obfd, osection, vma))
2471 err = _("failed to set vma");
2472 goto loser;
2475 lma = isection->lma;
2476 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2478 if (p->change_lma == CHANGE_MODIFY)
2479 lma += p->lma_val;
2480 else if (p->change_lma == CHANGE_SET)
2481 lma = p->lma_val;
2482 else
2483 abort ();
2485 else
2486 lma += change_section_address;
2488 osection->lma = lma;
2490 /* FIXME: This is probably not enough. If we change the LMA we
2491 may have to recompute the header for the file as well. */
2492 if (!bfd_set_section_alignment (obfd,
2493 osection,
2494 bfd_section_alignment (ibfd, isection)))
2496 err = _("failed to set alignment");
2497 goto loser;
2500 /* Copy merge entity size. */
2501 osection->entsize = isection->entsize;
2503 /* This used to be mangle_section; we do here to avoid using
2504 bfd_get_section_by_name since some formats allow multiple
2505 sections with the same name. */
2506 isection->output_section = osection;
2507 isection->output_offset = 0;
2509 /* Do not copy backend data if --extract-symbol is passed; anything
2510 that needs to look at the section contents will fail. */
2511 if (extract_symbol)
2512 return;
2514 if ((isection->flags & SEC_GROUP) != 0)
2516 asymbol *gsym = group_signature (isection);
2518 if (gsym != NULL)
2520 gsym->flags |= BSF_KEEP;
2521 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
2522 elf_group_id (isection) = gsym;
2526 /* Allow the BFD backend to copy any private data it understands
2527 from the input section to the output section. */
2528 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2530 err = _("failed to copy private data");
2531 goto loser;
2534 /* All went well. */
2535 return;
2537 loser:
2538 status = 1;
2539 bfd_nonfatal_message (NULL, obfd, osection, err);
2542 /* Copy the data of input section ISECTION of IBFD
2543 to an output section with the same name in OBFD.
2544 If stripping then don't copy any relocation info. */
2546 static void
2547 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2549 bfd *obfd = (bfd *) obfdarg;
2550 struct section_list *p;
2551 arelent **relpp;
2552 long relcount;
2553 sec_ptr osection;
2554 bfd_size_type size;
2555 long relsize;
2556 flagword flags;
2558 /* If we have already failed earlier on,
2559 do not keep on generating complaints now. */
2560 if (status != 0)
2561 return;
2563 if (is_strip_section (ibfd, isection))
2564 return;
2566 flags = bfd_get_section_flags (ibfd, isection);
2567 if ((flags & SEC_GROUP) != 0)
2568 return;
2570 osection = isection->output_section;
2571 size = bfd_get_section_size (isection);
2573 if (size == 0 || osection == 0)
2574 return;
2576 if (extract_symbol)
2577 return;
2579 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2581 /* Core files do not need to be relocated. */
2582 if (bfd_get_format (obfd) == bfd_core)
2583 relsize = 0;
2584 else
2586 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2588 if (relsize < 0)
2590 /* Do not complain if the target does not support relocations. */
2591 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2592 relsize = 0;
2593 else
2595 status = 1;
2596 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2597 return;
2602 if (relsize == 0)
2603 bfd_set_reloc (obfd, osection, NULL, 0);
2604 else
2606 relpp = (arelent **) xmalloc (relsize);
2607 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2608 if (relcount < 0)
2610 status = 1;
2611 bfd_nonfatal_message (NULL, ibfd, isection,
2612 _("relocation count is negative"));
2613 return;
2616 if (strip_symbols == STRIP_ALL)
2618 /* Remove relocations which are not in
2619 keep_strip_specific_list. */
2620 arelent **temp_relpp;
2621 long temp_relcount = 0;
2622 long i;
2624 temp_relpp = (arelent **) xmalloc (relsize);
2625 for (i = 0; i < relcount; i++)
2626 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2627 keep_specific_htab))
2628 temp_relpp [temp_relcount++] = relpp [i];
2629 relcount = temp_relcount;
2630 free (relpp);
2631 relpp = temp_relpp;
2634 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2635 if (relcount == 0)
2636 free (relpp);
2639 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2640 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2642 void *memhunk = xmalloc (size);
2644 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2646 status = 1;
2647 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2648 return;
2651 if (reverse_bytes)
2653 /* We don't handle leftover bytes (too many possible behaviors,
2654 and we don't know what the user wants). The section length
2655 must be a multiple of the number of bytes to swap. */
2656 if ((size % reverse_bytes) == 0)
2658 unsigned long i, j;
2659 bfd_byte b;
2661 for (i = 0; i < size; i += reverse_bytes)
2662 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
2664 bfd_byte *m = (bfd_byte *) memhunk;
2666 b = m[i + j];
2667 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
2668 m[(i + reverse_bytes) - (j + 1)] = b;
2671 else
2672 /* User must pad the section up in order to do this. */
2673 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2674 bfd_section_name (ibfd, isection), reverse_bytes);
2677 if (copy_byte >= 0)
2679 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2680 char *from = (char *) memhunk + copy_byte;
2681 char *to = (char *) memhunk;
2682 char *end = (char *) memhunk + size;
2684 for (; from < end; from += interleave)
2685 *to++ = *from;
2687 size = (size + interleave - 1 - copy_byte) / interleave;
2688 osection->lma /= interleave;
2691 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2693 status = 1;
2694 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2695 return;
2697 free (memhunk);
2699 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2701 void *memhunk = xmalloc (size);
2703 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2704 flag--they can just remove the section entirely and add it
2705 back again. However, we do permit them to turn on the
2706 SEC_HAS_CONTENTS flag, and take it to mean that the section
2707 contents should be zeroed out. */
2709 memset (memhunk, 0, size);
2710 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2712 status = 1;
2713 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2714 return;
2716 free (memhunk);
2720 /* Get all the sections. This is used when --gap-fill or --pad-to is
2721 used. */
2723 static void
2724 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2726 asection ***secppp = (asection ***) secppparg;
2728 **secppp = osection;
2729 ++(*secppp);
2732 /* Sort sections by VMA. This is called via qsort, and is used when
2733 --gap-fill or --pad-to is used. We force non loadable or empty
2734 sections to the front, where they are easier to ignore. */
2736 static int
2737 compare_section_lma (const void *arg1, const void *arg2)
2739 const asection *const *sec1 = (const asection * const *) arg1;
2740 const asection *const *sec2 = (const asection * const *) arg2;
2741 flagword flags1, flags2;
2743 /* Sort non loadable sections to the front. */
2744 flags1 = (*sec1)->flags;
2745 flags2 = (*sec2)->flags;
2746 if ((flags1 & SEC_HAS_CONTENTS) == 0
2747 || (flags1 & SEC_LOAD) == 0)
2749 if ((flags2 & SEC_HAS_CONTENTS) != 0
2750 && (flags2 & SEC_LOAD) != 0)
2751 return -1;
2753 else
2755 if ((flags2 & SEC_HAS_CONTENTS) == 0
2756 || (flags2 & SEC_LOAD) == 0)
2757 return 1;
2760 /* Sort sections by LMA. */
2761 if ((*sec1)->lma > (*sec2)->lma)
2762 return 1;
2763 else if ((*sec1)->lma < (*sec2)->lma)
2764 return -1;
2766 /* Sort sections with the same LMA by size. */
2767 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2768 return 1;
2769 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2770 return -1;
2772 return 0;
2775 /* Mark all the symbols which will be used in output relocations with
2776 the BSF_KEEP flag so that those symbols will not be stripped.
2778 Ignore relocations which will not appear in the output file. */
2780 static void
2781 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2783 asymbol **symbols = (asymbol **) symbolsarg;
2784 long relsize;
2785 arelent **relpp;
2786 long relcount, i;
2788 /* Ignore an input section with no corresponding output section. */
2789 if (isection->output_section == NULL)
2790 return;
2792 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2793 if (relsize < 0)
2795 /* Do not complain if the target does not support relocations. */
2796 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2797 return;
2798 bfd_fatal (bfd_get_filename (ibfd));
2801 if (relsize == 0)
2802 return;
2804 relpp = (arelent **) xmalloc (relsize);
2805 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2806 if (relcount < 0)
2807 bfd_fatal (bfd_get_filename (ibfd));
2809 /* Examine each symbol used in a relocation. If it's not one of the
2810 special bfd section symbols, then mark it with BSF_KEEP. */
2811 for (i = 0; i < relcount; i++)
2813 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2814 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2815 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2816 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2819 if (relpp != NULL)
2820 free (relpp);
2823 /* Write out debugging information. */
2825 static bfd_boolean
2826 write_debugging_info (bfd *obfd, void *dhandle,
2827 long *symcountp ATTRIBUTE_UNUSED,
2828 asymbol ***symppp ATTRIBUTE_UNUSED)
2830 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2831 return write_ieee_debugging_info (obfd, dhandle);
2833 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2834 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2836 bfd_byte *syms, *strings;
2837 bfd_size_type symsize, stringsize;
2838 asection *stabsec, *stabstrsec;
2839 flagword flags;
2841 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2842 &symsize, &strings,
2843 &stringsize))
2844 return FALSE;
2846 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2847 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2848 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2849 if (stabsec == NULL
2850 || stabstrsec == NULL
2851 || ! bfd_set_section_size (obfd, stabsec, symsize)
2852 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2853 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2854 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2856 bfd_nonfatal_message (NULL, obfd, NULL,
2857 _("can't create debugging section"));
2858 return FALSE;
2861 /* We can get away with setting the section contents now because
2862 the next thing the caller is going to do is copy over the
2863 real sections. We may someday have to split the contents
2864 setting out of this function. */
2865 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2866 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2867 stringsize))
2869 bfd_nonfatal_message (NULL, obfd, NULL,
2870 _("can't set debugging section contents"));
2871 return FALSE;
2874 return TRUE;
2877 bfd_nonfatal_message (NULL, obfd, NULL,
2878 _("don't know how to write debugging information for %s"),
2879 bfd_get_target (obfd));
2880 return FALSE;
2883 static int
2884 strip_main (int argc, char *argv[])
2886 char *input_target = NULL;
2887 char *output_target = NULL;
2888 bfd_boolean show_version = FALSE;
2889 bfd_boolean formats_info = FALSE;
2890 int c;
2891 int i;
2892 struct section_list *p;
2893 char *output_file = NULL;
2895 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2896 strip_options, (int *) 0)) != EOF)
2898 switch (c)
2900 case 'I':
2901 input_target = optarg;
2902 break;
2903 case 'O':
2904 output_target = optarg;
2905 break;
2906 case 'F':
2907 input_target = output_target = optarg;
2908 break;
2909 case 'R':
2910 p = find_section_list (optarg, TRUE);
2911 p->remove = TRUE;
2912 sections_removed = TRUE;
2913 break;
2914 case 's':
2915 strip_symbols = STRIP_ALL;
2916 break;
2917 case 'S':
2918 case 'g':
2919 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2920 strip_symbols = STRIP_DEBUG;
2921 break;
2922 case OPTION_STRIP_UNNEEDED:
2923 strip_symbols = STRIP_UNNEEDED;
2924 break;
2925 case 'K':
2926 add_specific_symbol (optarg, keep_specific_htab);
2927 break;
2928 case 'N':
2929 add_specific_symbol (optarg, strip_specific_htab);
2930 break;
2931 case 'o':
2932 output_file = optarg;
2933 break;
2934 case 'p':
2935 preserve_dates = TRUE;
2936 break;
2937 case 'x':
2938 discard_locals = LOCALS_ALL;
2939 break;
2940 case 'X':
2941 discard_locals = LOCALS_START_L;
2942 break;
2943 case 'v':
2944 verbose = TRUE;
2945 break;
2946 case 'V':
2947 show_version = TRUE;
2948 break;
2949 case OPTION_FORMATS_INFO:
2950 formats_info = TRUE;
2951 break;
2952 case OPTION_ONLY_KEEP_DEBUG:
2953 strip_symbols = STRIP_NONDEBUG;
2954 break;
2955 case OPTION_KEEP_FILE_SYMBOLS:
2956 keep_file_symbols = 1;
2957 break;
2958 case 0:
2959 /* We've been given a long option. */
2960 break;
2961 case 'w':
2962 wildcard = TRUE;
2963 break;
2964 case 'H':
2965 case 'h':
2966 strip_usage (stdout, 0);
2967 default:
2968 strip_usage (stderr, 1);
2972 if (formats_info)
2974 display_info ();
2975 return 0;
2978 if (show_version)
2979 print_version ("strip");
2981 /* Default is to strip all symbols. */
2982 if (strip_symbols == STRIP_UNDEF
2983 && discard_locals == LOCALS_UNDEF
2984 && htab_elements (strip_specific_htab) == 0)
2985 strip_symbols = STRIP_ALL;
2987 if (output_target == NULL)
2988 output_target = input_target;
2990 i = optind;
2991 if (i == argc
2992 || (output_file != NULL && (i + 1) < argc))
2993 strip_usage (stderr, 1);
2995 for (; i < argc; i++)
2997 int hold_status = status;
2998 struct stat statbuf;
2999 char *tmpname;
3001 if (get_file_size (argv[i]) < 1)
3003 status = 1;
3004 continue;
3007 if (preserve_dates)
3008 /* No need to check the return value of stat().
3009 It has already been checked in get_file_size(). */
3010 stat (argv[i], &statbuf);
3012 if (output_file == NULL || strcmp (argv[i], output_file) == 0)
3013 tmpname = make_tempname (argv[i]);
3014 else
3015 tmpname = output_file;
3017 if (tmpname == NULL)
3019 bfd_nonfatal_message (argv[i], NULL, NULL,
3020 _("could not create temporary file to hold stripped copy"));
3021 status = 1;
3022 continue;
3025 status = 0;
3026 copy_file (argv[i], tmpname, input_target, output_target, NULL);
3027 if (status == 0)
3029 if (preserve_dates)
3030 set_times (tmpname, &statbuf);
3031 if (output_file != tmpname)
3032 status = (smart_rename (tmpname,
3033 output_file ? output_file : argv[i],
3034 preserve_dates) != 0);
3035 if (status == 0)
3036 status = hold_status;
3038 else
3039 unlink_if_ordinary (tmpname);
3040 if (output_file != tmpname)
3041 free (tmpname);
3044 return status;
3047 /* Set up PE subsystem. */
3049 static void
3050 set_pe_subsystem (const char *s)
3052 const char *version, *subsystem;
3053 size_t i;
3054 static const struct
3056 const char *name;
3057 const char set_def;
3058 const short value;
3060 v[] =
3062 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
3063 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
3064 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
3065 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
3066 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
3067 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
3068 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
3069 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
3070 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
3071 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
3073 short value;
3074 char *copy;
3075 int set_def = -1;
3077 /* Check for the presence of a version number. */
3078 version = strchr (s, ':');
3079 if (version == NULL)
3080 subsystem = s;
3081 else
3083 int len = version - s;
3084 copy = xstrdup (s);
3085 subsystem = copy;
3086 copy[len] = '\0';
3087 version = copy + 1 + len;
3088 pe_major_subsystem_version = strtoul (version, &copy, 0);
3089 if (*copy == '.')
3090 pe_minor_subsystem_version = strtoul (copy + 1, &copy, 0);
3091 if (*copy != '\0')
3092 non_fatal (_("%s: bad version in PE subsystem"), s);
3095 /* Check for numeric subsystem. */
3096 value = (short) strtol (subsystem, &copy, 0);
3097 if (*copy == '\0')
3099 for (i = 0; i < ARRAY_SIZE (v); i++)
3100 if (v[i].value == value)
3102 pe_subsystem = value;
3103 set_def = v[i].set_def;
3104 break;
3107 else
3109 /* Search for subsystem by name. */
3110 for (i = 0; i < ARRAY_SIZE (v); i++)
3111 if (strcmp (subsystem, v[i].name) == 0)
3113 pe_subsystem = v[i].value;
3114 set_def = v[i].set_def;
3115 break;
3119 switch (set_def)
3121 case -1:
3122 fatal (_("unknown PE subsystem: %s"), s);
3123 break;
3124 case 0:
3125 break;
3126 default:
3127 if (pe_file_alignment == (bfd_vma) -1)
3128 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3129 if (pe_section_alignment == (bfd_vma) -1)
3130 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3131 break;
3135 /* Convert EFI target to PEI target. */
3137 static void
3138 convert_efi_target (char *efi)
3140 efi[0] = 'p';
3141 efi[1] = 'e';
3142 efi[2] = 'i';
3144 if (strcmp (efi + 4, "ia32") == 0)
3146 /* Change ia32 to i386. */
3147 efi[5]= '3';
3148 efi[6]= '8';
3149 efi[7]= '6';
3151 else if (strcmp (efi + 4, "x86_64") == 0)
3153 /* Change x86_64 to x86-64. */
3154 efi[7] = '-';
3158 static int
3159 copy_main (int argc, char *argv[])
3161 char *input_filename = NULL;
3162 char *output_filename = NULL;
3163 char *tmpname;
3164 char *input_target = NULL;
3165 char *output_target = NULL;
3166 bfd_boolean show_version = FALSE;
3167 bfd_boolean change_warn = TRUE;
3168 bfd_boolean formats_info = FALSE;
3169 int c;
3170 struct section_list *p;
3171 struct stat statbuf;
3172 const bfd_arch_info_type *input_arch = NULL;
3174 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
3175 copy_options, (int *) 0)) != EOF)
3177 switch (c)
3179 case 'b':
3180 copy_byte = atoi (optarg);
3181 if (copy_byte < 0)
3182 fatal (_("byte number must be non-negative"));
3183 break;
3185 case 'B':
3186 input_arch = bfd_scan_arch (optarg);
3187 if (input_arch == NULL)
3188 fatal (_("architecture %s unknown"), optarg);
3189 break;
3191 case 'i':
3192 interleave = atoi (optarg);
3193 if (interleave < 1)
3194 fatal (_("interleave must be positive"));
3195 break;
3197 case 'I':
3198 case 's': /* "source" - 'I' is preferred */
3199 input_target = optarg;
3200 break;
3202 case 'O':
3203 case 'd': /* "destination" - 'O' is preferred */
3204 output_target = optarg;
3205 break;
3207 case 'F':
3208 input_target = output_target = optarg;
3209 break;
3211 case 'j':
3212 p = find_section_list (optarg, TRUE);
3213 if (p->remove)
3214 fatal (_("%s both copied and removed"), optarg);
3215 p->copy = TRUE;
3216 sections_copied = TRUE;
3217 break;
3219 case 'R':
3220 p = find_section_list (optarg, TRUE);
3221 if (p->copy)
3222 fatal (_("%s both copied and removed"), optarg);
3223 p->remove = TRUE;
3224 sections_removed = TRUE;
3225 break;
3227 case 'S':
3228 strip_symbols = STRIP_ALL;
3229 break;
3231 case 'g':
3232 strip_symbols = STRIP_DEBUG;
3233 break;
3235 case OPTION_STRIP_UNNEEDED:
3236 strip_symbols = STRIP_UNNEEDED;
3237 break;
3239 case OPTION_ONLY_KEEP_DEBUG:
3240 strip_symbols = STRIP_NONDEBUG;
3241 break;
3243 case OPTION_KEEP_FILE_SYMBOLS:
3244 keep_file_symbols = 1;
3245 break;
3247 case OPTION_ADD_GNU_DEBUGLINK:
3248 gnu_debuglink_filename = optarg;
3249 break;
3251 case 'K':
3252 add_specific_symbol (optarg, keep_specific_htab);
3253 break;
3255 case 'N':
3256 add_specific_symbol (optarg, strip_specific_htab);
3257 break;
3259 case OPTION_STRIP_UNNEEDED_SYMBOL:
3260 add_specific_symbol (optarg, strip_unneeded_htab);
3261 break;
3263 case 'L':
3264 add_specific_symbol (optarg, localize_specific_htab);
3265 break;
3267 case OPTION_GLOBALIZE_SYMBOL:
3268 add_specific_symbol (optarg, globalize_specific_htab);
3269 break;
3271 case 'G':
3272 add_specific_symbol (optarg, keepglobal_specific_htab);
3273 break;
3275 case 'W':
3276 add_specific_symbol (optarg, weaken_specific_htab);
3277 break;
3279 case 'p':
3280 preserve_dates = TRUE;
3281 break;
3283 case 'w':
3284 wildcard = TRUE;
3285 break;
3287 case 'x':
3288 discard_locals = LOCALS_ALL;
3289 break;
3291 case 'X':
3292 discard_locals = LOCALS_START_L;
3293 break;
3295 case 'v':
3296 verbose = TRUE;
3297 break;
3299 case 'V':
3300 show_version = TRUE;
3301 break;
3303 case OPTION_FORMATS_INFO:
3304 formats_info = TRUE;
3305 break;
3307 case OPTION_WEAKEN:
3308 weaken = TRUE;
3309 break;
3311 case OPTION_ADD_SECTION:
3313 const char *s;
3314 size_t off, alloc;
3315 struct section_add *pa;
3316 FILE *f;
3318 s = strchr (optarg, '=');
3320 if (s == NULL)
3321 fatal (_("bad format for %s"), "--add-section");
3323 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
3324 pa->name = xstrndup (optarg, s - optarg);
3325 pa->filename = s + 1;
3327 /* We don't use get_file_size so that we can do
3328 --add-section .note.GNU_stack=/dev/null
3329 get_file_size doesn't work on /dev/null. */
3331 f = fopen (pa->filename, FOPEN_RB);
3332 if (f == NULL)
3333 fatal (_("cannot open: %s: %s"),
3334 pa->filename, strerror (errno));
3336 off = 0;
3337 alloc = 4096;
3338 pa->contents = (bfd_byte *) xmalloc (alloc);
3339 while (!feof (f))
3341 off_t got;
3343 if (off == alloc)
3345 alloc <<= 1;
3346 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
3349 got = fread (pa->contents + off, 1, alloc - off, f);
3350 if (ferror (f))
3351 fatal (_("%s: fread failed"), pa->filename);
3353 off += got;
3356 pa->size = off;
3358 fclose (f);
3360 pa->next = add_sections;
3361 add_sections = pa;
3363 break;
3365 case OPTION_CHANGE_START:
3366 change_start = parse_vma (optarg, "--change-start");
3367 break;
3369 case OPTION_CHANGE_SECTION_ADDRESS:
3370 case OPTION_CHANGE_SECTION_LMA:
3371 case OPTION_CHANGE_SECTION_VMA:
3373 const char *s;
3374 int len;
3375 char *name;
3376 char *option = NULL;
3377 bfd_vma val;
3378 enum change_action what = CHANGE_IGNORE;
3380 switch (c)
3382 case OPTION_CHANGE_SECTION_ADDRESS:
3383 option = "--change-section-address";
3384 break;
3385 case OPTION_CHANGE_SECTION_LMA:
3386 option = "--change-section-lma";
3387 break;
3388 case OPTION_CHANGE_SECTION_VMA:
3389 option = "--change-section-vma";
3390 break;
3393 s = strchr (optarg, '=');
3394 if (s == NULL)
3396 s = strchr (optarg, '+');
3397 if (s == NULL)
3399 s = strchr (optarg, '-');
3400 if (s == NULL)
3401 fatal (_("bad format for %s"), option);
3405 len = s - optarg;
3406 name = (char *) xmalloc (len + 1);
3407 strncpy (name, optarg, len);
3408 name[len] = '\0';
3410 p = find_section_list (name, TRUE);
3412 val = parse_vma (s + 1, option);
3414 switch (*s)
3416 case '=': what = CHANGE_SET; break;
3417 case '-': val = - val; /* Drop through. */
3418 case '+': what = CHANGE_MODIFY; break;
3421 switch (c)
3423 case OPTION_CHANGE_SECTION_ADDRESS:
3424 p->change_vma = what;
3425 p->vma_val = val;
3426 /* Drop through. */
3428 case OPTION_CHANGE_SECTION_LMA:
3429 p->change_lma = what;
3430 p->lma_val = val;
3431 break;
3433 case OPTION_CHANGE_SECTION_VMA:
3434 p->change_vma = what;
3435 p->vma_val = val;
3436 break;
3439 break;
3441 case OPTION_CHANGE_ADDRESSES:
3442 change_section_address = parse_vma (optarg, "--change-addresses");
3443 change_start = change_section_address;
3444 break;
3446 case OPTION_CHANGE_WARNINGS:
3447 change_warn = TRUE;
3448 break;
3450 case OPTION_CHANGE_LEADING_CHAR:
3451 change_leading_char = TRUE;
3452 break;
3454 case OPTION_DEBUGGING:
3455 convert_debugging = TRUE;
3456 break;
3458 case OPTION_GAP_FILL:
3460 bfd_vma gap_fill_vma;
3462 gap_fill_vma = parse_vma (optarg, "--gap-fill");
3463 gap_fill = (bfd_byte) gap_fill_vma;
3464 if ((bfd_vma) gap_fill != gap_fill_vma)
3466 char buff[20];
3468 sprintf_vma (buff, gap_fill_vma);
3470 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3471 buff, gap_fill);
3473 gap_fill_set = TRUE;
3475 break;
3477 case OPTION_NO_CHANGE_WARNINGS:
3478 change_warn = FALSE;
3479 break;
3481 case OPTION_PAD_TO:
3482 pad_to = parse_vma (optarg, "--pad-to");
3483 pad_to_set = TRUE;
3484 break;
3486 case OPTION_REMOVE_LEADING_CHAR:
3487 remove_leading_char = TRUE;
3488 break;
3490 case OPTION_REDEFINE_SYM:
3492 /* Push this redefinition onto redefine_symbol_list. */
3494 int len;
3495 const char *s;
3496 const char *nextarg;
3497 char *source, *target;
3499 s = strchr (optarg, '=');
3500 if (s == NULL)
3501 fatal (_("bad format for %s"), "--redefine-sym");
3503 len = s - optarg;
3504 source = (char *) xmalloc (len + 1);
3505 strncpy (source, optarg, len);
3506 source[len] = '\0';
3508 nextarg = s + 1;
3509 len = strlen (nextarg);
3510 target = (char *) xmalloc (len + 1);
3511 strcpy (target, nextarg);
3513 redefine_list_append ("--redefine-sym", source, target);
3515 free (source);
3516 free (target);
3518 break;
3520 case OPTION_REDEFINE_SYMS:
3521 add_redefine_syms_file (optarg);
3522 break;
3524 case OPTION_SET_SECTION_FLAGS:
3526 const char *s;
3527 int len;
3528 char *name;
3530 s = strchr (optarg, '=');
3531 if (s == NULL)
3532 fatal (_("bad format for %s"), "--set-section-flags");
3534 len = s - optarg;
3535 name = (char *) xmalloc (len + 1);
3536 strncpy (name, optarg, len);
3537 name[len] = '\0';
3539 p = find_section_list (name, TRUE);
3541 p->set_flags = TRUE;
3542 p->flags = parse_flags (s + 1);
3544 break;
3546 case OPTION_RENAME_SECTION:
3548 flagword flags;
3549 const char *eq, *fl;
3550 char *old_name;
3551 char *new_name;
3552 unsigned int len;
3554 eq = strchr (optarg, '=');
3555 if (eq == NULL)
3556 fatal (_("bad format for %s"), "--rename-section");
3558 len = eq - optarg;
3559 if (len == 0)
3560 fatal (_("bad format for %s"), "--rename-section");
3562 old_name = (char *) xmalloc (len + 1);
3563 strncpy (old_name, optarg, len);
3564 old_name[len] = 0;
3566 eq++;
3567 fl = strchr (eq, ',');
3568 if (fl)
3570 flags = parse_flags (fl + 1);
3571 len = fl - eq;
3573 else
3575 flags = -1;
3576 len = strlen (eq);
3579 if (len == 0)
3580 fatal (_("bad format for %s"), "--rename-section");
3582 new_name = (char *) xmalloc (len + 1);
3583 strncpy (new_name, eq, len);
3584 new_name[len] = 0;
3586 add_section_rename (old_name, new_name, flags);
3588 break;
3590 case OPTION_SET_START:
3591 set_start = parse_vma (optarg, "--set-start");
3592 set_start_set = TRUE;
3593 break;
3595 case OPTION_SREC_LEN:
3596 Chunk = parse_vma (optarg, "--srec-len");
3597 break;
3599 case OPTION_SREC_FORCES3:
3600 S3Forced = TRUE;
3601 break;
3603 case OPTION_STRIP_SYMBOLS:
3604 add_specific_symbols (optarg, strip_specific_htab);
3605 break;
3607 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3608 add_specific_symbols (optarg, strip_unneeded_htab);
3609 break;
3611 case OPTION_KEEP_SYMBOLS:
3612 add_specific_symbols (optarg, keep_specific_htab);
3613 break;
3615 case OPTION_LOCALIZE_HIDDEN:
3616 localize_hidden = TRUE;
3617 break;
3619 case OPTION_LOCALIZE_SYMBOLS:
3620 add_specific_symbols (optarg, localize_specific_htab);
3621 break;
3623 case OPTION_LONG_SECTION_NAMES:
3624 if (!strcmp ("enable", optarg))
3625 long_section_names = ENABLE;
3626 else if (!strcmp ("disable", optarg))
3627 long_section_names = DISABLE;
3628 else if (!strcmp ("keep", optarg))
3629 long_section_names = KEEP;
3630 else
3631 fatal (_("unknown long section names option '%s'"), optarg);
3632 break;
3634 case OPTION_GLOBALIZE_SYMBOLS:
3635 add_specific_symbols (optarg, globalize_specific_htab);
3636 break;
3638 case OPTION_KEEPGLOBAL_SYMBOLS:
3639 add_specific_symbols (optarg, keepglobal_specific_htab);
3640 break;
3642 case OPTION_WEAKEN_SYMBOLS:
3643 add_specific_symbols (optarg, weaken_specific_htab);
3644 break;
3646 case OPTION_ALT_MACH_CODE:
3647 use_alt_mach_code = strtoul (optarg, NULL, 0);
3648 if (use_alt_mach_code == 0)
3649 fatal (_("unable to parse alternative machine code"));
3650 break;
3652 case OPTION_PREFIX_SYMBOLS:
3653 prefix_symbols_string = optarg;
3654 break;
3656 case OPTION_PREFIX_SECTIONS:
3657 prefix_sections_string = optarg;
3658 break;
3660 case OPTION_PREFIX_ALLOC_SECTIONS:
3661 prefix_alloc_sections_string = optarg;
3662 break;
3664 case OPTION_READONLY_TEXT:
3665 bfd_flags_to_set |= WP_TEXT;
3666 bfd_flags_to_clear &= ~WP_TEXT;
3667 break;
3669 case OPTION_WRITABLE_TEXT:
3670 bfd_flags_to_clear |= WP_TEXT;
3671 bfd_flags_to_set &= ~WP_TEXT;
3672 break;
3674 case OPTION_PURE:
3675 bfd_flags_to_set |= D_PAGED;
3676 bfd_flags_to_clear &= ~D_PAGED;
3677 break;
3679 case OPTION_IMPURE:
3680 bfd_flags_to_clear |= D_PAGED;
3681 bfd_flags_to_set &= ~D_PAGED;
3682 break;
3684 case OPTION_EXTRACT_SYMBOL:
3685 extract_symbol = TRUE;
3686 break;
3688 case OPTION_REVERSE_BYTES:
3690 int prev = reverse_bytes;
3692 reverse_bytes = atoi (optarg);
3693 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
3694 fatal (_("number of bytes to reverse must be positive and even"));
3696 if (prev && prev != reverse_bytes)
3697 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3698 prev);
3699 break;
3702 case OPTION_FILE_ALIGNMENT:
3703 pe_file_alignment = parse_vma (optarg, "--file-alignment");
3704 break;
3706 case OPTION_HEAP:
3708 char *end;
3709 pe_heap_reserve = strtoul (optarg, &end, 0);
3710 if (end == optarg
3711 || (*end != '.' && *end != '\0'))
3712 non_fatal (_("%s: invalid reserve value for --heap"),
3713 optarg);
3714 else if (*end != '\0')
3716 pe_heap_commit = strtoul (end + 1, &end, 0);
3717 if (*end != '\0')
3718 non_fatal (_("%s: invalid commit value for --heap"),
3719 optarg);
3722 break;
3724 case OPTION_IMAGE_BASE:
3725 pe_image_base = parse_vma (optarg, "--image-base");
3726 break;
3728 case OPTION_SECTION_ALIGNMENT:
3729 pe_section_alignment = parse_vma (optarg,
3730 "--section-alignment");
3731 break;
3733 case OPTION_SUBSYSTEM:
3734 set_pe_subsystem (optarg);
3735 break;
3737 case OPTION_STACK:
3739 char *end;
3740 pe_stack_reserve = strtoul (optarg, &end, 0);
3741 if (end == optarg
3742 || (*end != '.' && *end != '\0'))
3743 non_fatal (_("%s: invalid reserve value for --stack"),
3744 optarg);
3745 else if (*end != '\0')
3747 pe_stack_commit = strtoul (end + 1, &end, 0);
3748 if (*end != '\0')
3749 non_fatal (_("%s: invalid commit value for --stack"),
3750 optarg);
3753 break;
3755 case 0:
3756 /* We've been given a long option. */
3757 break;
3759 case 'H':
3760 case 'h':
3761 copy_usage (stdout, 0);
3763 default:
3764 copy_usage (stderr, 1);
3768 if (formats_info)
3770 display_info ();
3771 return 0;
3774 if (show_version)
3775 print_version ("objcopy");
3777 if (copy_byte >= interleave)
3778 fatal (_("byte number must be less than interleave"));
3780 if (optind == argc || optind + 2 < argc)
3781 copy_usage (stderr, 1);
3783 input_filename = argv[optind];
3784 if (optind + 1 < argc)
3785 output_filename = argv[optind + 1];
3787 /* Default is to strip no symbols. */
3788 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3789 strip_symbols = STRIP_NONE;
3791 if (output_target == NULL)
3792 output_target = input_target;
3794 /* Convert input EFI target to PEI target. */
3795 if (input_target != NULL
3796 && strncmp (input_target, "efi-", 4) == 0)
3798 char *efi;
3800 efi = xstrdup (output_target + 4);
3801 if (strncmp (efi, "bsdrv-", 6) == 0
3802 || strncmp (efi, "rtdrv-", 6) == 0)
3803 efi += 2;
3804 else if (strncmp (efi, "app-", 4) != 0)
3805 fatal (_("unknown input EFI target: %s"), input_target);
3807 input_target = efi;
3808 convert_efi_target (efi);
3811 /* Convert output EFI target to PEI target. */
3812 if (output_target != NULL
3813 && strncmp (output_target, "efi-", 4) == 0)
3815 char *efi;
3817 efi = xstrdup (output_target + 4);
3818 if (strncmp (efi, "app-", 4) == 0)
3820 if (pe_subsystem == -1)
3821 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
3823 else if (strncmp (efi, "bsdrv-", 6) == 0)
3825 if (pe_subsystem == -1)
3826 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
3827 efi += 2;
3829 else if (strncmp (efi, "rtdrv-", 6) == 0)
3831 if (pe_subsystem == -1)
3832 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
3833 efi += 2;
3835 else
3836 fatal (_("unknown output EFI target: %s"), output_target);
3838 if (pe_file_alignment == (bfd_vma) -1)
3839 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3840 if (pe_section_alignment == (bfd_vma) -1)
3841 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3843 output_target = efi;
3844 convert_efi_target (efi);
3847 if (preserve_dates)
3848 if (stat (input_filename, & statbuf) < 0)
3849 fatal (_("warning: could not locate '%s'. System error message: %s"),
3850 input_filename, strerror (errno));
3852 /* If there is no destination file, or the source and destination files
3853 are the same, then create a temp and rename the result into the input. */
3854 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3855 tmpname = make_tempname (input_filename);
3856 else
3857 tmpname = output_filename;
3859 if (tmpname == NULL)
3860 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3861 input_filename, strerror (errno));
3863 copy_file (input_filename, tmpname, input_target, output_target, input_arch);
3864 if (status == 0)
3866 if (preserve_dates)
3867 set_times (tmpname, &statbuf);
3868 if (tmpname != output_filename)
3869 status = (smart_rename (tmpname, input_filename,
3870 preserve_dates) != 0);
3872 else
3873 unlink_if_ordinary (tmpname);
3875 if (change_warn)
3877 for (p = change_sections; p != NULL; p = p->next)
3879 if (! p->used)
3881 if (p->change_vma != CHANGE_IGNORE)
3883 char buff [20];
3885 sprintf_vma (buff, p->vma_val);
3887 /* xgettext:c-format */
3888 non_fatal (_("%s %s%c0x%s never used"),
3889 "--change-section-vma",
3890 p->name,
3891 p->change_vma == CHANGE_SET ? '=' : '+',
3892 buff);
3895 if (p->change_lma != CHANGE_IGNORE)
3897 char buff [20];
3899 sprintf_vma (buff, p->lma_val);
3901 /* xgettext:c-format */
3902 non_fatal (_("%s %s%c0x%s never used"),
3903 "--change-section-lma",
3904 p->name,
3905 p->change_lma == CHANGE_SET ? '=' : '+',
3906 buff);
3912 return 0;
3916 main (int argc, char *argv[])
3918 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3919 setlocale (LC_MESSAGES, "");
3920 #endif
3921 #if defined (HAVE_SETLOCALE)
3922 setlocale (LC_CTYPE, "");
3923 #endif
3924 bindtextdomain (PACKAGE, LOCALEDIR);
3925 textdomain (PACKAGE);
3927 program_name = argv[0];
3928 xmalloc_set_program_name (program_name);
3930 START_PROGRESS (program_name, 0);
3932 expandargv (&argc, &argv);
3934 strip_symbols = STRIP_UNDEF;
3935 discard_locals = LOCALS_UNDEF;
3937 bfd_init ();
3938 set_default_bfd_target ();
3940 if (is_strip < 0)
3942 int i = strlen (program_name);
3943 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3944 /* Drop the .exe suffix, if any. */
3945 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3947 i -= 4;
3948 program_name[i] = '\0';
3950 #endif
3951 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3954 create_symbol_htabs ();
3956 if (is_strip)
3957 strip_main (argc, argv);
3958 else
3959 copy_main (argc, argv);
3961 END_PROGRESS (program_name);
3963 return status;