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
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 2 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
27 #include "libiberty.h"
29 #include "filenames.h"
35 /* A list of symbols to explicitly strip out, or to keep. A linked
36 list is good enough for a small number from the command line, but
37 this will slow things down a lot if many symbols are being
46 /* A list to support redefine_sym. */
51 struct redefine_node *next;
54 typedef struct section_rename
56 const char * old_name;
57 const char * new_name;
59 struct section_rename * next;
63 /* List of sections to be renamed. */
64 static section_rename *section_rename_list;
66 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
68 static asymbol **isympp = NULL; /* Input symbols. */
69 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
71 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
72 static int copy_byte = -1;
73 static int interleave = 4;
75 static bfd_boolean verbose; /* Print file and target names. */
76 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
77 static int status = 0; /* Exit status. */
82 STRIP_NONE, /* Don't strip. */
83 STRIP_DEBUG, /* Strip all debugger symbols. */
84 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
85 STRIP_NONDEBUG, /* Strip everything but debug info. */
86 STRIP_ALL /* Strip all symbols. */
89 /* Which symbols to remove. */
90 static enum strip_action strip_symbols;
95 LOCALS_START_L, /* Discard locals starting with L. */
96 LOCALS_ALL /* Discard all locals. */
99 /* Which local symbols to remove. Overrides STRIP_ALL. */
100 static enum locals_action discard_locals;
102 /* What kind of change to perform. */
110 /* Structure used to hold lists of sections and actions to take. */
113 struct section_list * next; /* Next section to change. */
114 const char * name; /* Section name. */
115 bfd_boolean used; /* Whether this entry was used. */
116 bfd_boolean remove; /* Whether to remove this section. */
117 bfd_boolean copy; /* Whether to copy this section. */
118 enum change_action change_vma;/* Whether to change or set VMA. */
119 bfd_vma vma_val; /* Amount to change by or set to. */
120 enum change_action change_lma;/* Whether to change or set LMA. */
121 bfd_vma lma_val; /* Amount to change by or set to. */
122 bfd_boolean set_flags; /* Whether to set the section flags. */
123 flagword flags; /* What to set the section flags to. */
126 static struct section_list *change_sections;
128 /* TRUE if some sections are to be removed. */
129 static bfd_boolean sections_removed;
131 /* TRUE if only some sections are to be copied. */
132 static bfd_boolean sections_copied;
134 /* Changes to the start address. */
135 static bfd_vma change_start = 0;
136 static bfd_boolean set_start_set = FALSE;
137 static bfd_vma set_start;
139 /* Changes to section addresses. */
140 static bfd_vma change_section_address = 0;
142 /* Filling gaps between sections. */
143 static bfd_boolean gap_fill_set = FALSE;
144 static bfd_byte gap_fill = 0;
146 /* Pad to a given address. */
147 static bfd_boolean pad_to_set = FALSE;
148 static bfd_vma pad_to;
150 /* Use alternative machine code? */
151 static unsigned long use_alt_mach_code = 0;
153 /* Output BFD flags user wants to set or clear */
154 static flagword bfd_flags_to_set;
155 static flagword bfd_flags_to_clear;
157 /* List of sections to add. */
160 /* Next section to add. */
161 struct section_add *next;
162 /* Name of section to add. */
164 /* Name of file holding section contents. */
165 const char *filename;
168 /* Contents of file. */
170 /* BFD section, after it has been added. */
174 /* List of sections to add to the output BFD. */
175 static struct section_add *add_sections;
177 /* If non-NULL the argument to --add-gnu-debuglink.
178 This should be the filename to store in the .gnu_debuglink section. */
179 static const char * gnu_debuglink_filename = NULL;
181 /* Whether to convert debugging information. */
182 static bfd_boolean convert_debugging = FALSE;
184 /* Whether to change the leading character in symbol names. */
185 static bfd_boolean change_leading_char = FALSE;
187 /* Whether to remove the leading character from global symbol names. */
188 static bfd_boolean remove_leading_char = FALSE;
190 /* Whether to permit wildcard in symbol comparison. */
191 static bfd_boolean wildcard = FALSE;
193 /* True if --localize-hidden is in effect. */
194 static bfd_boolean localize_hidden = FALSE;
196 /* List of symbols to strip, keep, localize, keep-global, weaken,
198 static struct symlist *strip_specific_list = NULL;
199 static struct symlist *strip_unneeded_list = NULL;
200 static struct symlist *keep_specific_list = NULL;
201 static struct symlist *localize_specific_list = NULL;
202 static struct symlist *globalize_specific_list = NULL;
203 static struct symlist *keepglobal_specific_list = NULL;
204 static struct symlist *weaken_specific_list = NULL;
205 static struct redefine_node *redefine_sym_list = NULL;
207 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
208 static bfd_boolean weaken = FALSE;
210 /* If this is TRUE, we retain BSF_FILE symbols. */
211 static bfd_boolean keep_file_symbols = FALSE;
213 /* Prefix symbols/sections. */
214 static char *prefix_symbols_string = 0;
215 static char *prefix_sections_string = 0;
216 static char *prefix_alloc_sections_string = 0;
218 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
219 enum command_line_switch
221 OPTION_ADD_SECTION=150,
222 OPTION_CHANGE_ADDRESSES,
223 OPTION_CHANGE_LEADING_CHAR,
225 OPTION_CHANGE_SECTION_ADDRESS,
226 OPTION_CHANGE_SECTION_LMA,
227 OPTION_CHANGE_SECTION_VMA,
228 OPTION_CHANGE_WARNINGS,
231 OPTION_NO_CHANGE_WARNINGS,
233 OPTION_REMOVE_LEADING_CHAR,
234 OPTION_SET_SECTION_FLAGS,
236 OPTION_STRIP_UNNEEDED,
239 OPTION_REDEFINE_SYMS,
242 OPTION_STRIP_SYMBOLS,
243 OPTION_STRIP_UNNEEDED_SYMBOL,
244 OPTION_STRIP_UNNEEDED_SYMBOLS,
246 OPTION_LOCALIZE_HIDDEN,
247 OPTION_LOCALIZE_SYMBOLS,
248 OPTION_GLOBALIZE_SYMBOL,
249 OPTION_GLOBALIZE_SYMBOLS,
250 OPTION_KEEPGLOBAL_SYMBOLS,
251 OPTION_WEAKEN_SYMBOLS,
252 OPTION_RENAME_SECTION,
253 OPTION_ALT_MACH_CODE,
254 OPTION_PREFIX_SYMBOLS,
255 OPTION_PREFIX_SECTIONS,
256 OPTION_PREFIX_ALLOC_SECTIONS,
258 OPTION_ADD_GNU_DEBUGLINK,
259 OPTION_ONLY_KEEP_DEBUG,
260 OPTION_KEEP_FILE_SYMBOLS,
261 OPTION_READONLY_TEXT,
262 OPTION_WRITABLE_TEXT,
267 /* Options to handle if running as "strip". */
269 static struct option strip_options[] =
271 {"discard-all", no_argument, 0, 'x'},
272 {"discard-locals", no_argument, 0, 'X'},
273 {"format", required_argument, 0, 'F'}, /* Obsolete */
274 {"help", no_argument, 0, 'h'},
275 {"info", no_argument, 0, OPTION_FORMATS_INFO},
276 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
277 {"input-target", required_argument, 0, 'I'},
278 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
279 {"keep-symbol", required_argument, 0, 'K'},
280 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
281 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
282 {"output-target", required_argument, 0, 'O'},
283 {"output-file", required_argument, 0, 'o'},
284 {"preserve-dates", no_argument, 0, 'p'},
285 {"remove-section", required_argument, 0, 'R'},
286 {"strip-all", no_argument, 0, 's'},
287 {"strip-debug", no_argument, 0, 'S'},
288 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
289 {"strip-symbol", required_argument, 0, 'N'},
290 {"target", required_argument, 0, 'F'},
291 {"verbose", no_argument, 0, 'v'},
292 {"version", no_argument, 0, 'V'},
293 {"wildcard", no_argument, 0, 'w'},
294 {0, no_argument, 0, 0}
297 /* Options to handle if running as "objcopy". */
299 static struct option copy_options[] =
301 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
302 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
303 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
304 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
305 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
306 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
307 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
308 {"binary-architecture", required_argument, 0, 'B'},
309 {"byte", required_argument, 0, 'b'},
310 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
311 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
312 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
313 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
314 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
315 {"change-start", required_argument, 0, OPTION_CHANGE_START},
316 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
317 {"debugging", no_argument, 0, OPTION_DEBUGGING},
318 {"discard-all", no_argument, 0, 'x'},
319 {"discard-locals", no_argument, 0, 'X'},
320 {"format", required_argument, 0, 'F'}, /* Obsolete */
321 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
322 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
323 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
324 {"help", no_argument, 0, 'h'},
325 {"impure", no_argument, 0, OPTION_IMPURE},
326 {"info", no_argument, 0, OPTION_FORMATS_INFO},
327 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
328 {"input-target", required_argument, 0, 'I'},
329 {"interleave", required_argument, 0, 'i'},
330 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
331 {"keep-global-symbol", required_argument, 0, 'G'},
332 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
333 {"keep-symbol", required_argument, 0, 'K'},
334 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
335 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
336 {"localize-symbol", required_argument, 0, 'L'},
337 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
338 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
339 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
340 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
341 {"only-section", required_argument, 0, 'j'},
342 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
343 {"output-target", required_argument, 0, 'O'},
344 {"pad-to", required_argument, 0, OPTION_PAD_TO},
345 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
346 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
347 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
348 {"preserve-dates", no_argument, 0, 'p'},
349 {"pure", no_argument, 0, OPTION_PURE},
350 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
351 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
352 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
353 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
354 {"remove-section", required_argument, 0, 'R'},
355 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
356 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
357 {"set-start", required_argument, 0, OPTION_SET_START},
358 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
359 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
360 {"strip-all", no_argument, 0, 'S'},
361 {"strip-debug", no_argument, 0, 'g'},
362 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
363 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
364 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
365 {"strip-symbol", required_argument, 0, 'N'},
366 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
367 {"target", required_argument, 0, 'F'},
368 {"verbose", no_argument, 0, 'v'},
369 {"version", no_argument, 0, 'V'},
370 {"weaken", no_argument, 0, OPTION_WEAKEN},
371 {"weaken-symbol", required_argument, 0, 'W'},
372 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
373 {"wildcard", no_argument, 0, 'w'},
374 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
375 {0, no_argument, 0, 0}
379 extern char *program_name;
381 /* This flag distinguishes between strip and objcopy:
382 1 means this is 'strip'; 0 means this is 'objcopy'.
383 -1 means if we should use argv[0] to decide. */
386 /* The maximum length of an S record. This variable is declared in srec.c
387 and can be modified by the --srec-len parameter. */
388 extern unsigned int Chunk;
390 /* Restrict the generation of Srecords to type S3 only.
391 This variable is declare in bfd/srec.c and can be toggled
392 on by the --srec-forceS3 command line switch. */
393 extern bfd_boolean S3Forced;
395 /* Defined in bfd/binary.c. Used to set architecture and machine of input
397 extern enum bfd_architecture bfd_external_binary_architecture;
398 extern unsigned long bfd_external_machine;
400 /* Forward declarations. */
401 static void setup_section (bfd *, asection *, void *);
402 static void setup_bfd_headers (bfd *, bfd *);
403 static void copy_section (bfd *, asection *, void *);
404 static void get_sections (bfd *, asection *, void *);
405 static int compare_section_lma (const void *, const void *);
406 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
407 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
408 static const char *lookup_sym_redefinition (const char *);
411 copy_usage (FILE *stream, int exit_status)
413 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
414 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
415 fprintf (stream, _(" The options are:\n"));
416 fprintf (stream, _("\
417 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
418 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
419 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
420 -F --target <bfdname> Set both input and output format to <bfdname>\n\
421 --debugging Convert debugging information, if possible\n\
422 -p --preserve-dates Copy modified/access timestamps to the output\n\
423 -j --only-section <name> Only copy section <name> into the output\n\
424 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
425 -R --remove-section <name> Remove section <name> from the output\n\
426 -S --strip-all Remove all symbol and relocation information\n\
427 -g --strip-debug Remove all debugging symbols & sections\n\
428 --strip-unneeded Remove all symbols not needed by relocations\n\
429 -N --strip-symbol <name> Do not copy symbol <name>\n\
430 --strip-unneeded-symbol <name>\n\
431 Do not copy symbol <name> unless needed by\n\
433 --only-keep-debug Strip everything but the debug information\n\
434 -K --keep-symbol <name> Do not strip symbol <name>\n\
435 --keep-file-symbols Do not strip file symbol(s)\n\
436 --localize-hidden Turn all ELF hidden symbols into locals\n\
437 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
438 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
439 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
440 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
441 --weaken Force all global symbols to be marked as weak\n\
442 -w --wildcard Permit wildcard in symbol comparison\n\
443 -x --discard-all Remove all non-global symbols\n\
444 -X --discard-locals Remove any compiler-generated symbols\n\
445 -i --interleave <number> Only copy one out of every <number> bytes\n\
446 -b --byte <num> Select byte <num> in every interleaved block\n\
447 --gap-fill <val> Fill gaps between sections with <val>\n\
448 --pad-to <addr> Pad the last section up to address <addr>\n\
449 --set-start <addr> Set the start address to <addr>\n\
450 {--change-start|--adjust-start} <incr>\n\
451 Add <incr> to the start address\n\
452 {--change-addresses|--adjust-vma} <incr>\n\
453 Add <incr> to LMA, VMA and start addresses\n\
454 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
455 Change LMA and VMA of section <name> by <val>\n\
456 --change-section-lma <name>{=|+|-}<val>\n\
457 Change the LMA of section <name> by <val>\n\
458 --change-section-vma <name>{=|+|-}<val>\n\
459 Change the VMA of section <name> by <val>\n\
460 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
461 Warn if a named section does not exist\n\
462 --set-section-flags <name>=<flags>\n\
463 Set section <name>'s properties to <flags>\n\
464 --add-section <name>=<file> Add section <name> found in <file> to output\n\
465 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
466 --change-leading-char Force output format's leading character style\n\
467 --remove-leading-char Remove leading character from global symbols\n\
468 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
469 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
471 --srec-len <number> Restrict the length of generated Srecords\n\
472 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
473 --strip-symbols <file> -N for all symbols listed in <file>\n\
474 --strip-unneeded-symbols <file>\n\
475 --strip-unneeded-symbol for all symbols listed\n\
477 --keep-symbols <file> -K for all symbols listed in <file>\n\
478 --localize-symbols <file> -L for all symbols listed in <file>\n\
479 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
480 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
481 --weaken-symbols <file> -W for all symbols listed in <file>\n\
482 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
483 --writable-text Mark the output text as writable\n\
484 --readonly-text Make the output text write protected\n\
485 --pure Mark the output file as demand paged\n\
486 --impure Mark the output file as impure\n\
487 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
488 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
489 --prefix-alloc-sections <prefix>\n\
490 Add <prefix> to start of every allocatable\n\
492 -v --verbose List all object files modified\n\
493 @<file> Read options from <file>\n\
494 -V --version Display this program's version number\n\
495 -h --help Display this output\n\
496 --info List object formats & architectures supported\n\
498 list_supported_targets (program_name, stream);
499 if (exit_status == 0)
500 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
505 strip_usage (FILE *stream, int exit_status)
507 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
508 fprintf (stream, _(" Removes symbols and sections from files\n"));
509 fprintf (stream, _(" The options are:\n"));
510 fprintf (stream, _("\
511 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
512 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
513 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
514 -p --preserve-dates Copy modified/access timestamps to the output\n\
515 -R --remove-section=<name> Remove section <name> from the output\n\
516 -s --strip-all Remove all symbol and relocation information\n\
517 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
518 --strip-unneeded Remove all symbols not needed by relocations\n\
519 --only-keep-debug Strip everything but the debug information\n\
520 -N --strip-symbol=<name> Do not copy symbol <name>\n\
521 -K --keep-symbol=<name> Do not strip symbol <name>\n\
522 --keep-file-symbols Do not strip file symbol(s)\n\
523 -w --wildcard Permit wildcard in symbol comparison\n\
524 -x --discard-all Remove all non-global symbols\n\
525 -X --discard-locals Remove any compiler-generated symbols\n\
526 -v --verbose List all object files modified\n\
527 -V --version Display this program's version number\n\
528 -h --help Display this output\n\
529 --info List object formats & architectures supported\n\
530 -o <file> Place stripped output into <file>\n\
533 list_supported_targets (program_name, stream);
534 if (exit_status == 0)
535 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
539 /* Parse section flags into a flagword, with a fatal error if the
540 string can't be parsed. */
543 parse_flags (const char *s)
553 snext = strchr (s, ',');
563 #define PARSE_FLAG(fname,fval) \
564 else if (strncasecmp (fname, s, len) == 0) ret |= fval
565 PARSE_FLAG ("alloc", SEC_ALLOC);
566 PARSE_FLAG ("load", SEC_LOAD);
567 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
568 PARSE_FLAG ("readonly", SEC_READONLY);
569 PARSE_FLAG ("debug", SEC_DEBUGGING);
570 PARSE_FLAG ("code", SEC_CODE);
571 PARSE_FLAG ("data", SEC_DATA);
572 PARSE_FLAG ("rom", SEC_ROM);
573 PARSE_FLAG ("share", SEC_COFF_SHARED);
574 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
580 copy = xmalloc (len + 1);
581 strncpy (copy, s, len);
583 non_fatal (_("unrecognized section flag `%s'"), copy);
584 fatal (_("supported flags: %s"),
585 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
595 /* Find and optionally add an entry in the change_sections list. */
597 static struct section_list *
598 find_section_list (const char *name, bfd_boolean add)
600 struct section_list *p;
602 for (p = change_sections; p != NULL; p = p->next)
603 if (strcmp (p->name, name) == 0)
609 p = xmalloc (sizeof (struct section_list));
614 p->change_vma = CHANGE_IGNORE;
615 p->change_lma = CHANGE_IGNORE;
618 p->set_flags = FALSE;
621 p->next = change_sections;
627 /* Add a symbol to strip_specific_list. */
630 add_specific_symbol (const char *name, struct symlist **list)
632 struct symlist *tmp_list;
634 tmp_list = xmalloc (sizeof (struct symlist));
635 tmp_list->name = name;
636 tmp_list->next = *list;
640 /* Add symbols listed in `filename' to strip_specific_list. */
642 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
643 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
646 add_specific_symbols (const char *filename, struct symlist **list)
652 unsigned int line_count;
654 size = get_file_size (filename);
661 buffer = xmalloc (size + 2);
662 f = fopen (filename, FOPEN_RT);
664 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
666 if (fread (buffer, 1, size, f) == 0 || ferror (f))
667 fatal (_("%s: fread failed"), filename);
670 buffer [size] = '\n';
671 buffer [size + 1] = '\0';
675 for (line = buffer; * line != '\0'; line ++)
680 int finished = FALSE;
682 for (eol = line;; eol ++)
688 /* Cope with \n\r. */
696 /* Cope with \r\n. */
707 /* Line comment, Terminate the line here, in case a
708 name is present and then allow the rest of the
709 loop to find the real end of the line. */
721 /* A name may now exist somewhere between 'line' and 'eol'.
722 Strip off leading whitespace and trailing whitespace,
723 then add it to the list. */
724 for (name = line; IS_WHITESPACE (* name); name ++)
726 for (name_end = name;
727 (! IS_WHITESPACE (* name_end))
728 && (! IS_LINE_TERMINATOR (* name_end));
732 if (! IS_LINE_TERMINATOR (* name_end))
736 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
739 if (! IS_LINE_TERMINATOR (* extra))
740 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
741 filename, line_count);
747 add_specific_symbol (name, list);
749 /* Advance line pointer to end of line. The 'eol ++' in the for
750 loop above will then advance us to the start of the next line. */
756 /* See whether a symbol should be stripped or kept based on
757 strip_specific_list and keep_symbols. */
760 is_specified_symbol (const char *name, struct symlist *list)
762 struct symlist *tmp_list;
766 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
767 if (*(tmp_list->name) != '!')
769 if (!fnmatch (tmp_list->name, name, 0))
774 if (fnmatch (tmp_list->name + 1, name, 0))
780 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
781 if (strcmp (name, tmp_list->name) == 0)
788 /* See if a section is being removed. */
791 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
793 if (sections_removed || sections_copied)
795 struct section_list *p;
797 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
799 if (sections_removed && p != NULL && p->remove)
801 if (sections_copied && (p == NULL || ! p->copy))
805 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
807 if (strip_symbols == STRIP_DEBUG
808 || strip_symbols == STRIP_UNNEEDED
809 || strip_symbols == STRIP_ALL
810 || discard_locals == LOCALS_ALL
811 || convert_debugging)
814 if (strip_symbols == STRIP_NONDEBUG)
819 Group sections look like debugging sections but they are not.
820 (They have a non-zero size but they are not ALLOCated). */
821 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0
822 && strip_symbols == STRIP_NONDEBUG)
828 /* Return true if SYM is a hidden symbol. */
831 is_hidden_symbol (asymbol *sym)
833 elf_symbol_type *elf_sym;
835 elf_sym = elf_symbol_from (sym->the_bfd, sym);
837 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
846 /* Choose which symbol entries to copy; put the result in OSYMS.
847 We don't copy in place, because that confuses the relocs.
848 Return the number of symbols to print. */
851 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
852 asymbol **isyms, long symcount)
854 asymbol **from = isyms, **to = osyms;
855 long src_count = 0, dst_count = 0;
856 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
859 for (; src_count < symcount; src_count++)
861 asymbol *sym = from[src_count];
862 flagword flags = sym->flags;
863 char *name = (char *) bfd_asymbol_name (sym);
865 bfd_boolean undefined;
866 bfd_boolean rem_leading_char;
867 bfd_boolean add_leading_char;
869 undefined = bfd_is_und_section (bfd_get_section (sym));
871 if (redefine_sym_list)
873 char *old_name, *new_name;
875 old_name = (char *) bfd_asymbol_name (sym);
876 new_name = (char *) lookup_sym_redefinition (old_name);
877 bfd_asymbol_name (sym) = new_name;
881 /* Check if we will remove the current leading character. */
883 (name[0] == bfd_get_symbol_leading_char (abfd))
884 && (change_leading_char
885 || (remove_leading_char
886 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
888 || bfd_is_com_section (bfd_get_section (sym)))));
890 /* Check if we will add a new leading character. */
893 && (bfd_get_symbol_leading_char (obfd) != '\0')
894 && (bfd_get_symbol_leading_char (abfd) == '\0'
895 || (name[0] == bfd_get_symbol_leading_char (abfd)));
897 /* Short circuit for change_leading_char if we can do it in-place. */
898 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
900 name[0] = bfd_get_symbol_leading_char (obfd);
901 bfd_asymbol_name (sym) = name;
902 rem_leading_char = FALSE;
903 add_leading_char = FALSE;
906 /* Remove leading char. */
907 if (rem_leading_char)
908 bfd_asymbol_name (sym) = ++name;
910 /* Add new leading char and/or prefix. */
911 if (add_leading_char || prefix_symbols_string)
915 ptr = n = xmalloc (1 + strlen (prefix_symbols_string)
916 + strlen (name) + 1);
917 if (add_leading_char)
918 *ptr++ = bfd_get_symbol_leading_char (obfd);
920 if (prefix_symbols_string)
922 strcpy (ptr, prefix_symbols_string);
923 ptr += strlen (prefix_symbols_string);
927 bfd_asymbol_name (sym) = n;
931 if (strip_symbols == STRIP_ALL)
933 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
934 || ((flags & BSF_SECTION_SYM) != 0
935 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
938 else if (relocatable /* Relocatable file. */
939 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
941 else if (bfd_decode_symclass (sym) == 'I')
942 /* Global symbols in $idata sections need to be retained
943 even if relocatable is FALSE. External users of the
944 library containing the $idata section may reference these
947 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
948 || (flags & BSF_WEAK) != 0
950 || bfd_is_com_section (bfd_get_section (sym)))
951 keep = strip_symbols != STRIP_UNNEEDED;
952 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
953 keep = (strip_symbols != STRIP_DEBUG
954 && strip_symbols != STRIP_UNNEEDED
955 && ! convert_debugging);
956 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
957 /* COMDAT sections store special information in local
958 symbols, so we cannot risk stripping any of them. */
960 else /* Local symbol. */
961 keep = (strip_symbols != STRIP_UNNEEDED
962 && (discard_locals != LOCALS_ALL
963 && (discard_locals != LOCALS_START_L
964 || ! bfd_is_local_label (abfd, sym))));
966 if (keep && is_specified_symbol (name, strip_specific_list))
969 && !(flags & BSF_KEEP)
970 && is_specified_symbol (name, strip_unneeded_list))
973 && ((keep_file_symbols && (flags & BSF_FILE))
974 || is_specified_symbol (name, keep_specific_list)))
976 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
981 if ((flags & BSF_GLOBAL) != 0
982 && (weaken || is_specified_symbol (name, weaken_specific_list)))
984 sym->flags &= ~ BSF_GLOBAL;
985 sym->flags |= BSF_WEAK;
989 && (flags & (BSF_GLOBAL | BSF_WEAK))
990 && (is_specified_symbol (name, localize_specific_list)
991 || (keepglobal_specific_list != NULL
992 && ! is_specified_symbol (name, keepglobal_specific_list))
993 || (localize_hidden && is_hidden_symbol (sym))))
995 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
996 sym->flags |= BSF_LOCAL;
1000 && (flags & BSF_LOCAL)
1001 && is_specified_symbol (name, globalize_specific_list))
1003 sym->flags &= ~ BSF_LOCAL;
1004 sym->flags |= BSF_GLOBAL;
1007 to[dst_count++] = sym;
1011 to[dst_count] = NULL;
1016 /* Find the redefined name of symbol SOURCE. */
1019 lookup_sym_redefinition (const char *source)
1021 struct redefine_node *list;
1023 for (list = redefine_sym_list; list != NULL; list = list->next)
1024 if (strcmp (source, list->source) == 0)
1025 return list->target;
1030 /* Add a node to a symbol redefine list. */
1033 redefine_list_append (const char *cause, const char *source, const char *target)
1035 struct redefine_node **p;
1036 struct redefine_node *list;
1037 struct redefine_node *new_node;
1039 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1041 if (strcmp (source, list->source) == 0)
1042 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1045 if (strcmp (target, list->target) == 0)
1046 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1050 new_node = xmalloc (sizeof (struct redefine_node));
1052 new_node->source = strdup (source);
1053 new_node->target = strdup (target);
1054 new_node->next = NULL;
1059 /* Handle the --redefine-syms option. Read lines containing "old new"
1060 from the file, and add them to the symbol redefine list. */
1063 add_redefine_syms_file (const char *filename)
1072 file = fopen (filename, "r");
1074 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1075 filename, strerror (errno));
1078 buf = xmalloc (bufsize);
1086 /* Collect the input symbol name. */
1087 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1095 buf = xrealloc (buf, bufsize);
1103 /* Eat white space between the symbol names. */
1104 while (IS_WHITESPACE (c))
1106 if (c == '#' || IS_LINE_TERMINATOR (c))
1111 /* Collect the output symbol name. */
1113 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1121 buf = xrealloc (buf, bufsize);
1129 /* Eat white space at end of line. */
1130 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1135 if ((c == '\r' && (c = getc (file)) == '\n')
1136 || c == '\n' || c == EOF)
1139 /* Append the redefinition to the list. */
1141 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1152 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1154 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1155 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1158 /* Eat the rest of the line and finish it. */
1159 while (c != '\n' && c != EOF)
1165 fatal (_("%s:%d: premature end of file"), filename, lineno);
1170 /* Copy unkown object file IBFD onto OBFD.
1171 Returns TRUE upon success, FALSE otherwise. */
1174 copy_unknown_object (bfd *ibfd, bfd *obfd)
1182 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1184 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1191 non_fatal (_("stat returns negative size for `%s'"),
1192 bfd_get_archive_filename (ibfd));
1196 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1198 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1203 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1204 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1206 cbuf = xmalloc (BUFSIZE);
1208 while (ncopied < size)
1210 tocopy = size - ncopied;
1211 if (tocopy > BUFSIZE)
1214 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1215 != (bfd_size_type) tocopy)
1217 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1222 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1223 != (bfd_size_type) tocopy)
1225 bfd_nonfatal (bfd_get_filename (obfd));
1233 chmod (bfd_get_filename (obfd), buf.st_mode);
1238 /* Copy object file IBFD onto OBFD.
1239 Returns TRUE upon success, FALSE otherwise. */
1242 copy_object (bfd *ibfd, bfd *obfd)
1246 asection **osections = NULL;
1247 asection *gnu_debuglink_section = NULL;
1248 bfd_size_type *gaps = NULL;
1249 bfd_size_type max_gap = 0;
1252 enum bfd_architecture iarch;
1255 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1256 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1257 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1258 fatal (_("Unable to change endianness of input file(s)"));
1260 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1262 bfd_nonfatal (bfd_get_filename (obfd));
1267 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1268 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1269 bfd_get_filename (obfd), bfd_get_target (obfd));
1274 start = bfd_get_start_address (ibfd);
1275 start += change_start;
1277 /* Neither the start address nor the flags
1278 need to be set for a core file. */
1279 if (bfd_get_format (obfd) != bfd_core)
1283 flags = bfd_get_file_flags (ibfd);
1284 flags |= bfd_flags_to_set;
1285 flags &= ~bfd_flags_to_clear;
1286 flags &= bfd_applicable_file_flags (obfd);
1288 if (!bfd_set_start_address (obfd, start)
1289 || !bfd_set_file_flags (obfd, flags))
1291 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1296 /* Copy architecture of input file to output file. */
1297 iarch = bfd_get_arch (ibfd);
1298 imach = bfd_get_mach (ibfd);
1299 if (!bfd_set_arch_mach (obfd, iarch, imach)
1300 && (ibfd->target_defaulted
1301 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1303 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1304 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1305 bfd_get_archive_filename (ibfd));
1307 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1308 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1309 bfd_get_mach (ibfd)));
1313 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1315 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1322 if (osympp != isympp)
1328 symsize = bfd_get_symtab_upper_bound (ibfd);
1331 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1335 osympp = isympp = xmalloc (symsize);
1336 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1339 bfd_nonfatal (bfd_get_filename (ibfd));
1343 /* BFD mandates that all output sections be created and sizes set before
1344 any output is done. Thus, we traverse all sections multiple times. */
1345 bfd_map_over_sections (ibfd, setup_section, obfd);
1347 setup_bfd_headers (ibfd, obfd);
1349 if (add_sections != NULL)
1351 struct section_add *padd;
1352 struct section_list *pset;
1354 for (padd = add_sections; padd != NULL; padd = padd->next)
1358 pset = find_section_list (padd->name, FALSE);
1362 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1363 if (pset != NULL && pset->set_flags)
1364 flags = pset->flags | SEC_HAS_CONTENTS;
1366 /* bfd_make_section_with_flags() does not return very helpful
1367 error codes, so check for the most likely user error first. */
1368 if (bfd_get_section_by_name (obfd, padd->name))
1370 non_fatal (_("can't add section '%s' - it already exists!"), padd->name);
1375 padd->section = bfd_make_section_with_flags (obfd, padd->name, flags);
1376 if (padd->section == NULL)
1378 non_fatal (_("can't create section `%s': %s"),
1379 padd->name, bfd_errmsg (bfd_get_error ()));
1384 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1386 bfd_nonfatal (bfd_get_filename (obfd));
1392 if (pset->change_vma != CHANGE_IGNORE)
1393 if (! bfd_set_section_vma (obfd, padd->section,
1396 bfd_nonfatal (bfd_get_filename (obfd));
1400 if (pset->change_lma != CHANGE_IGNORE)
1402 padd->section->lma = pset->lma_val;
1404 if (! bfd_set_section_alignment
1405 (obfd, padd->section,
1406 bfd_section_alignment (obfd, padd->section)))
1408 bfd_nonfatal (bfd_get_filename (obfd));
1416 if (gnu_debuglink_filename != NULL)
1418 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1419 (obfd, gnu_debuglink_filename);
1421 if (gnu_debuglink_section == NULL)
1423 bfd_nonfatal (gnu_debuglink_filename);
1427 /* Special processing for PE format files. We
1428 have no way to distinguish PE from COFF here. */
1429 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1431 bfd_vma debuglink_vma;
1432 asection * highest_section;
1435 /* The PE spec requires that all sections be adjacent and sorted
1436 in ascending order of VMA. It also specifies that debug
1437 sections should be last. This is despite the fact that debug
1438 sections are not loaded into memory and so in theory have no
1441 This means that the debuglink section must be given a non-zero
1442 VMA which makes it contiguous with other debug sections. So
1443 walk the current section list, find the section with the
1444 highest VMA and start the debuglink section after that one. */
1445 for (sec = obfd->sections, highest_section = NULL;
1449 && (highest_section == NULL
1450 || sec->vma > highest_section->vma))
1451 highest_section = sec;
1453 if (highest_section)
1454 debuglink_vma = BFD_ALIGN (highest_section->vma
1455 + highest_section->size,
1456 /* FIXME: We ought to be using
1457 COFF_PAGE_SIZE here or maybe
1458 bfd_get_section_alignment() (if it
1459 was set) but since this is for PE
1460 and we know the required alignment
1461 it is easier just to hard code it. */
1464 /* Umm, not sure what to do in this case. */
1465 debuglink_vma = 0x1000;
1467 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1471 if (bfd_count_sections (obfd) == 0)
1473 non_fatal (_("there are no sections to be copied!"));
1477 if (gap_fill_set || pad_to_set)
1482 /* We must fill in gaps between the sections and/or we must pad
1483 the last section to a specified address. We do this by
1484 grabbing a list of the sections, sorting them by VMA, and
1485 increasing the section sizes as required to fill the gaps.
1486 We write out the gap contents below. */
1488 c = bfd_count_sections (obfd);
1489 osections = xmalloc (c * sizeof (asection *));
1491 bfd_map_over_sections (obfd, get_sections, &set);
1493 qsort (osections, c, sizeof (asection *), compare_section_lma);
1495 gaps = xmalloc (c * sizeof (bfd_size_type));
1496 memset (gaps, 0, c * sizeof (bfd_size_type));
1500 for (i = 0; i < c - 1; i++)
1504 bfd_vma gap_start, gap_stop;
1506 flags = bfd_get_section_flags (obfd, osections[i]);
1507 if ((flags & SEC_HAS_CONTENTS) == 0
1508 || (flags & SEC_LOAD) == 0)
1511 size = bfd_section_size (obfd, osections[i]);
1512 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1513 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1514 if (gap_start < gap_stop)
1516 if (! bfd_set_section_size (obfd, osections[i],
1517 size + (gap_stop - gap_start)))
1519 non_fatal (_("Can't fill gap after %s: %s"),
1520 bfd_get_section_name (obfd, osections[i]),
1521 bfd_errmsg (bfd_get_error ()));
1525 gaps[i] = gap_stop - gap_start;
1526 if (max_gap < gap_stop - gap_start)
1527 max_gap = gap_stop - gap_start;
1537 lma = bfd_section_lma (obfd, osections[c - 1]);
1538 size = bfd_section_size (obfd, osections[c - 1]);
1539 if (lma + size < pad_to)
1541 if (! bfd_set_section_size (obfd, osections[c - 1],
1544 non_fatal (_("Can't add padding to %s: %s"),
1545 bfd_get_section_name (obfd, osections[c - 1]),
1546 bfd_errmsg (bfd_get_error ()));
1551 gaps[c - 1] = pad_to - (lma + size);
1552 if (max_gap < pad_to - (lma + size))
1553 max_gap = pad_to - (lma + size);
1559 /* Symbol filtering must happen after the output sections
1560 have been created, but before their contents are set. */
1562 if (convert_debugging)
1563 dhandle = read_debugging_info (ibfd, isympp, symcount);
1565 if (strip_symbols == STRIP_DEBUG
1566 || strip_symbols == STRIP_ALL
1567 || strip_symbols == STRIP_UNNEEDED
1568 || strip_symbols == STRIP_NONDEBUG
1569 || discard_locals != LOCALS_UNDEF
1571 || strip_specific_list != NULL
1572 || keep_specific_list != NULL
1573 || localize_specific_list != NULL
1574 || globalize_specific_list != NULL
1575 || keepglobal_specific_list != NULL
1576 || weaken_specific_list != NULL
1577 || prefix_symbols_string
1580 || convert_debugging
1581 || change_leading_char
1582 || remove_leading_char
1583 || redefine_sym_list
1586 /* Mark symbols used in output relocations so that they
1587 are kept, even if they are local labels or static symbols.
1589 Note we iterate over the input sections examining their
1590 relocations since the relocations for the output sections
1591 haven't been set yet. mark_symbols_used_in_relocations will
1592 ignore input sections which have no corresponding output
1594 if (strip_symbols != STRIP_ALL)
1595 bfd_map_over_sections (ibfd,
1596 mark_symbols_used_in_relocations,
1598 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1599 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1602 if (convert_debugging && dhandle != NULL)
1604 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1611 bfd_set_symtab (obfd, osympp, symcount);
1613 /* This has to happen after the symbol table has been set. */
1614 bfd_map_over_sections (ibfd, copy_section, obfd);
1616 if (add_sections != NULL)
1618 struct section_add *padd;
1620 for (padd = add_sections; padd != NULL; padd = padd->next)
1622 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1625 bfd_nonfatal (bfd_get_filename (obfd));
1631 if (gnu_debuglink_filename != NULL)
1633 if (! bfd_fill_in_gnu_debuglink_section
1634 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1636 bfd_nonfatal (gnu_debuglink_filename);
1641 if (gap_fill_set || pad_to_set)
1646 /* Fill in the gaps. */
1649 buf = xmalloc (max_gap);
1650 memset (buf, gap_fill, max_gap);
1652 c = bfd_count_sections (obfd);
1653 for (i = 0; i < c; i++)
1661 off = bfd_section_size (obfd, osections[i]) - left;
1672 if (! bfd_set_section_contents (obfd, osections[i], buf,
1675 bfd_nonfatal (bfd_get_filename (obfd));
1686 /* Allow the BFD backend to copy any private data it understands
1687 from the input BFD to the output BFD. This is done last to
1688 permit the routine to look at the filtered symbol table, which is
1689 important for the ECOFF code at least. */
1690 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1691 && strip_symbols == STRIP_NONDEBUG)
1692 /* Do not copy the private data when creating an ELF format
1693 debug info file. We do not want the program headers. */
1695 else if (! bfd_copy_private_bfd_data (ibfd, obfd))
1697 non_fatal (_("%s: error copying private BFD data: %s"),
1698 bfd_get_filename (obfd),
1699 bfd_errmsg (bfd_get_error ()));
1703 /* Switch to the alternate machine code. We have to do this at the
1704 very end, because we only initialize the header when we create
1705 the first section. */
1706 if (use_alt_mach_code != 0)
1708 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
1710 non_fatal (_("this target does not support %lu alternative machine codes"),
1712 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1714 non_fatal (_("treating that number as an absolute e_machine value instead"));
1715 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
1718 non_fatal (_("ignoring the alternative value"));
1726 #if defined (_WIN32) && !defined (__CYGWIN32__)
1727 #define MKDIR(DIR, MODE) mkdir (DIR)
1729 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1732 /* Read each archive element in turn from IBFD, copy the
1733 contents to temp file, and keep the temp file handle.
1734 If 'force_output_target' is TRUE then make sure that
1735 all elements in the new archive are of the type
1739 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
1740 bfd_boolean force_output_target)
1744 struct name_list *next;
1748 bfd **ptr = &obfd->archive_head;
1750 char *dir = make_tempname (bfd_get_filename (obfd));
1752 /* Make a temp directory to hold the contents. */
1753 if (MKDIR (dir, 0700) != 0)
1754 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1755 dir, strerror (errno));
1757 obfd->has_armap = ibfd->has_armap;
1761 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1763 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1764 RETURN_NONFATAL (bfd_get_filename (obfd));
1766 while (!status && this_element != NULL)
1772 int stat_status = 0;
1773 bfd_boolean delete = TRUE;
1775 /* Create an output file for this member. */
1776 output_name = concat (dir, "/",
1777 bfd_get_filename (this_element), (char *) 0);
1779 /* If the file already exists, make another temp dir. */
1780 if (stat (output_name, &buf) >= 0)
1782 output_name = make_tempname (output_name);
1783 if (MKDIR (output_name, 0700) != 0)
1784 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1785 output_name, strerror (errno));
1787 l = xmalloc (sizeof (struct name_list));
1788 l->name = output_name;
1792 output_name = concat (output_name, "/",
1793 bfd_get_filename (this_element), (char *) 0);
1798 stat_status = bfd_stat_arch_elt (this_element, &buf);
1800 if (stat_status != 0)
1801 non_fatal (_("internal stat error on %s"),
1802 bfd_get_filename (this_element));
1805 l = xmalloc (sizeof (struct name_list));
1806 l->name = output_name;
1811 if (bfd_check_format (this_element, bfd_object))
1813 /* PR binutils/3110: Cope with archives
1814 containing multiple target types. */
1815 if (force_output_target)
1816 output_bfd = bfd_openw (output_name, output_target);
1818 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
1820 if (output_bfd == NULL)
1821 RETURN_NONFATAL (output_name);
1823 delete = ! copy_object (this_element, output_bfd);
1826 || bfd_get_arch (this_element) != bfd_arch_unknown)
1828 if (!bfd_close (output_bfd))
1830 bfd_nonfatal (bfd_get_filename (output_bfd));
1831 /* Error in new object file. Don't change archive. */
1836 goto copy_unknown_element;
1840 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1841 bfd_get_archive_filename (this_element));
1843 output_bfd = bfd_openw (output_name, output_target);
1844 copy_unknown_element:
1845 delete = !copy_unknown_object (this_element, output_bfd);
1846 if (!bfd_close_all_done (output_bfd))
1848 bfd_nonfatal (bfd_get_filename (output_bfd));
1849 /* Error in new object file. Don't change archive. */
1856 unlink (output_name);
1861 if (preserve_dates && stat_status == 0)
1862 set_times (output_name, &buf);
1864 /* Open the newly output file and attach to our list. */
1865 output_bfd = bfd_openr (output_name, output_target);
1867 l->obfd = output_bfd;
1870 ptr = &output_bfd->next;
1872 last_element = this_element;
1874 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1876 bfd_close (last_element);
1881 if (!bfd_close (obfd))
1882 RETURN_NONFATAL (bfd_get_filename (obfd));
1884 if (!bfd_close (ibfd))
1885 RETURN_NONFATAL (bfd_get_filename (ibfd));
1887 /* Delete all the files that we opened. */
1888 for (l = list; l != NULL; l = l->next)
1890 if (l->obfd == NULL)
1894 bfd_close (l->obfd);
1901 /* The top-level control. */
1904 copy_file (const char *input_filename, const char *output_filename,
1905 const char *input_target, const char *output_target)
1908 char **obj_matching;
1909 char **core_matching;
1911 if (get_file_size (input_filename) < 1)
1917 /* To allow us to do "strip *" without dying on the first
1918 non-object file, failures are nonfatal. */
1919 ibfd = bfd_openr (input_filename, input_target);
1921 RETURN_NONFATAL (input_filename);
1923 if (bfd_check_format (ibfd, bfd_archive))
1925 bfd_boolean force_output_target;
1928 /* bfd_get_target does not return the correct value until
1929 bfd_check_format succeeds. */
1930 if (output_target == NULL)
1932 output_target = bfd_get_target (ibfd);
1933 force_output_target = FALSE;
1936 force_output_target = TRUE;
1938 obfd = bfd_openw (output_filename, output_target);
1940 RETURN_NONFATAL (output_filename);
1942 copy_archive (ibfd, obfd, output_target, force_output_target);
1944 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
1949 /* bfd_get_target does not return the correct value until
1950 bfd_check_format succeeds. */
1951 if (output_target == NULL)
1952 output_target = bfd_get_target (ibfd);
1954 obfd = bfd_openw (output_filename, output_target);
1956 RETURN_NONFATAL (output_filename);
1958 if (! copy_object (ibfd, obfd))
1961 if (!bfd_close (obfd))
1962 RETURN_NONFATAL (output_filename);
1964 if (!bfd_close (ibfd))
1965 RETURN_NONFATAL (input_filename);
1970 bfd_error_type obj_error = bfd_get_error ();
1971 bfd_error_type core_error;
1973 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1975 /* This probably can't happen.. */
1976 if (obj_error == bfd_error_file_ambiguously_recognized)
1977 free (obj_matching);
1981 core_error = bfd_get_error ();
1982 /* Report the object error in preference to the core error. */
1983 if (obj_error != core_error)
1984 bfd_set_error (obj_error);
1986 bfd_nonfatal (input_filename);
1988 if (obj_error == bfd_error_file_ambiguously_recognized)
1990 list_matching_formats (obj_matching);
1991 free (obj_matching);
1993 if (core_error == bfd_error_file_ambiguously_recognized)
1995 list_matching_formats (core_matching);
1996 free (core_matching);
2003 /* Add a name to the section renaming list. */
2006 add_section_rename (const char * old_name, const char * new_name,
2009 section_rename * rename;
2011 /* Check for conflicts first. */
2012 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2013 if (strcmp (rename->old_name, old_name) == 0)
2015 /* Silently ignore duplicate definitions. */
2016 if (strcmp (rename->new_name, new_name) == 0
2017 && rename->flags == flags)
2020 fatal (_("Multiple renames of section %s"), old_name);
2023 rename = xmalloc (sizeof (* rename));
2025 rename->old_name = old_name;
2026 rename->new_name = new_name;
2027 rename->flags = flags;
2028 rename->next = section_rename_list;
2030 section_rename_list = rename;
2033 /* Check the section rename list for a new name of the input section
2034 ISECTION. Return the new name if one is found.
2035 Also set RETURNED_FLAGS to the flags to be used for this section. */
2038 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2039 flagword * returned_flags)
2041 const char * old_name = bfd_section_name (ibfd, isection);
2042 section_rename * rename;
2044 /* Default to using the flags of the input section. */
2045 * returned_flags = bfd_get_section_flags (ibfd, isection);
2047 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2048 if (strcmp (rename->old_name, old_name) == 0)
2050 if (rename->flags != (flagword) -1)
2051 * returned_flags = rename->flags;
2053 return rename->new_name;
2059 /* Once each of the sections is copied, we may still need to do some
2060 finalization work for private section headers. Do that here. */
2063 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2067 /* Allow the BFD backend to copy any private data it understands
2068 from the input section to the output section. */
2069 if (! bfd_copy_private_header_data (ibfd, obfd))
2071 err = _("private header data");
2075 /* All went well. */
2079 non_fatal (_("%s: error in %s: %s"),
2080 bfd_get_filename (ibfd),
2081 err, bfd_errmsg (bfd_get_error ()));
2085 /* Create a section in OBFD with the same
2086 name and attributes as ISECTION in IBFD. */
2089 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2091 bfd *obfd = obfdarg;
2092 struct section_list *p;
2100 char *prefix = NULL;
2102 if (is_strip_section (ibfd, isection))
2105 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2109 /* Get the, possibly new, name of the output section. */
2110 name = find_section_rename (ibfd, isection, & flags);
2112 /* Prefix sections. */
2113 if ((prefix_alloc_sections_string)
2114 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2115 prefix = prefix_alloc_sections_string;
2116 else if (prefix_sections_string)
2117 prefix = prefix_sections_string;
2123 n = xmalloc (strlen (prefix) + strlen (name) + 1);
2129 if (p != NULL && p->set_flags)
2130 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2131 else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
2132 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2134 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2136 if (osection == NULL)
2142 if (strip_symbols == STRIP_NONDEBUG
2143 && obfd->xvec->flavour == bfd_target_elf_flavour
2144 && (flags & SEC_ALLOC) != 0
2145 && (p == NULL || !p->set_flags))
2146 elf_section_type (osection) = SHT_NOBITS;
2148 size = bfd_section_size (ibfd, isection);
2150 size = (size + interleave - 1) / interleave;
2151 if (! bfd_set_section_size (obfd, osection, size))
2157 vma = bfd_section_vma (ibfd, isection);
2158 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2160 else if (p != NULL && p->change_vma == CHANGE_SET)
2163 vma += change_section_address;
2165 if (! bfd_set_section_vma (obfd, osection, vma))
2171 lma = isection->lma;
2172 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2174 if (p->change_lma == CHANGE_MODIFY)
2176 else if (p->change_lma == CHANGE_SET)
2182 lma += change_section_address;
2184 osection->lma = lma;
2186 /* FIXME: This is probably not enough. If we change the LMA we
2187 may have to recompute the header for the file as well. */
2188 if (!bfd_set_section_alignment (obfd,
2190 bfd_section_alignment (ibfd, isection)))
2192 err = _("alignment");
2196 /* Copy merge entity size. */
2197 osection->entsize = isection->entsize;
2199 /* This used to be mangle_section; we do here to avoid using
2200 bfd_get_section_by_name since some formats allow multiple
2201 sections with the same name. */
2202 isection->output_section = osection;
2203 isection->output_offset = 0;
2205 /* Allow the BFD backend to copy any private data it understands
2206 from the input section to the output section. */
2207 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
2208 && strip_symbols == STRIP_NONDEBUG)
2209 /* Do not copy the private data when creating an ELF format
2210 debug info file. We do not want the program headers. */
2212 else if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2214 err = _("private data");
2217 else if ((isection->flags & SEC_GROUP) != 0
2218 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour)
2220 Elf_Internal_Shdr *ghdr;
2222 ghdr = &elf_section_data (isection)->this_hdr;
2223 if (ghdr->sh_link < elf_numsections (ibfd))
2225 const struct elf_backend_data *bed = get_elf_backend_data (ibfd);
2226 Elf_Internal_Shdr *symhdr = elf_elfsections (ibfd) [ghdr->sh_link];
2228 if (symhdr->sh_type == SHT_SYMTAB
2229 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
2230 isympp[ghdr->sh_info]->flags |= BSF_KEEP;
2234 /* All went well. */
2238 non_fatal (_("%s: section `%s': error in %s: %s"),
2239 bfd_get_filename (ibfd),
2240 bfd_section_name (ibfd, isection),
2241 err, bfd_errmsg (bfd_get_error ()));
2245 /* Copy the data of input section ISECTION of IBFD
2246 to an output section with the same name in OBFD.
2247 If stripping then don't copy any relocation info. */
2250 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2252 bfd *obfd = obfdarg;
2253 struct section_list *p;
2261 /* If we have already failed earlier on,
2262 do not keep on generating complaints now. */
2266 if (is_strip_section (ibfd, isection))
2269 flags = bfd_get_section_flags (ibfd, isection);
2270 if ((flags & SEC_GROUP) != 0)
2273 osection = isection->output_section;
2274 size = bfd_get_section_size (isection);
2276 if (size == 0 || osection == 0)
2279 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2281 /* Core files do not need to be relocated. */
2282 if (bfd_get_format (obfd) == bfd_core)
2286 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2290 /* Do not complain if the target does not support relocations. */
2291 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2294 RETURN_NONFATAL (bfd_get_filename (ibfd));
2299 bfd_set_reloc (obfd, osection, NULL, 0);
2302 relpp = xmalloc (relsize);
2303 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2305 RETURN_NONFATAL (bfd_get_filename (ibfd));
2307 if (strip_symbols == STRIP_ALL)
2309 /* Remove relocations which are not in
2310 keep_strip_specific_list. */
2311 arelent **temp_relpp;
2312 long temp_relcount = 0;
2315 temp_relpp = xmalloc (relsize);
2316 for (i = 0; i < relcount; i++)
2317 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2318 keep_specific_list))
2319 temp_relpp [temp_relcount++] = relpp [i];
2320 relcount = temp_relcount;
2325 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2330 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2331 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2333 void *memhunk = xmalloc (size);
2335 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2336 RETURN_NONFATAL (bfd_get_filename (ibfd));
2340 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2341 char *from = (char *) memhunk + copy_byte;
2343 char *end = (char *) memhunk + size;
2345 for (; from < end; from += interleave)
2348 size = (size + interleave - 1 - copy_byte) / interleave;
2349 osection->lma /= interleave;
2352 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2353 RETURN_NONFATAL (bfd_get_filename (obfd));
2357 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2359 void *memhunk = xmalloc (size);
2361 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2362 flag--they can just remove the section entirely and add it
2363 back again. However, we do permit them to turn on the
2364 SEC_HAS_CONTENTS flag, and take it to mean that the section
2365 contents should be zeroed out. */
2367 memset (memhunk, 0, size);
2368 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2369 RETURN_NONFATAL (bfd_get_filename (obfd));
2374 /* Get all the sections. This is used when --gap-fill or --pad-to is
2378 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2380 asection ***secppp = secppparg;
2382 **secppp = osection;
2386 /* Sort sections by VMA. This is called via qsort, and is used when
2387 --gap-fill or --pad-to is used. We force non loadable or empty
2388 sections to the front, where they are easier to ignore. */
2391 compare_section_lma (const void *arg1, const void *arg2)
2393 const asection *const *sec1 = arg1;
2394 const asection *const *sec2 = arg2;
2395 flagword flags1, flags2;
2397 /* Sort non loadable sections to the front. */
2398 flags1 = (*sec1)->flags;
2399 flags2 = (*sec2)->flags;
2400 if ((flags1 & SEC_HAS_CONTENTS) == 0
2401 || (flags1 & SEC_LOAD) == 0)
2403 if ((flags2 & SEC_HAS_CONTENTS) != 0
2404 && (flags2 & SEC_LOAD) != 0)
2409 if ((flags2 & SEC_HAS_CONTENTS) == 0
2410 || (flags2 & SEC_LOAD) == 0)
2414 /* Sort sections by LMA. */
2415 if ((*sec1)->lma > (*sec2)->lma)
2417 else if ((*sec1)->lma < (*sec2)->lma)
2420 /* Sort sections with the same LMA by size. */
2421 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2423 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2429 /* Mark all the symbols which will be used in output relocations with
2430 the BSF_KEEP flag so that those symbols will not be stripped.
2432 Ignore relocations which will not appear in the output file. */
2435 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2437 asymbol **symbols = symbolsarg;
2442 /* Ignore an input section with no corresponding output section. */
2443 if (isection->output_section == NULL)
2446 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2449 /* Do not complain if the target does not support relocations. */
2450 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2452 bfd_fatal (bfd_get_filename (ibfd));
2458 relpp = xmalloc (relsize);
2459 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2461 bfd_fatal (bfd_get_filename (ibfd));
2463 /* Examine each symbol used in a relocation. If it's not one of the
2464 special bfd section symbols, then mark it with BSF_KEEP. */
2465 for (i = 0; i < relcount; i++)
2467 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2468 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2469 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2470 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2477 /* Write out debugging information. */
2480 write_debugging_info (bfd *obfd, void *dhandle,
2481 long *symcountp ATTRIBUTE_UNUSED,
2482 asymbol ***symppp ATTRIBUTE_UNUSED)
2484 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2485 return write_ieee_debugging_info (obfd, dhandle);
2487 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2488 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2490 bfd_byte *syms, *strings;
2491 bfd_size_type symsize, stringsize;
2492 asection *stabsec, *stabstrsec;
2495 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2500 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2501 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2502 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2504 || stabstrsec == NULL
2505 || ! bfd_set_section_size (obfd, stabsec, symsize)
2506 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2507 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2508 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2510 non_fatal (_("%s: can't create debugging section: %s"),
2511 bfd_get_filename (obfd),
2512 bfd_errmsg (bfd_get_error ()));
2516 /* We can get away with setting the section contents now because
2517 the next thing the caller is going to do is copy over the
2518 real sections. We may someday have to split the contents
2519 setting out of this function. */
2520 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2521 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2524 non_fatal (_("%s: can't set debugging section contents: %s"),
2525 bfd_get_filename (obfd),
2526 bfd_errmsg (bfd_get_error ()));
2533 non_fatal (_("%s: don't know how to write debugging information for %s"),
2534 bfd_get_filename (obfd), bfd_get_target (obfd));
2539 strip_main (int argc, char *argv[])
2541 char *input_target = NULL;
2542 char *output_target = NULL;
2543 bfd_boolean show_version = FALSE;
2544 bfd_boolean formats_info = FALSE;
2547 struct section_list *p;
2548 char *output_file = NULL;
2550 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2551 strip_options, (int *) 0)) != EOF)
2556 input_target = optarg;
2559 output_target = optarg;
2562 input_target = output_target = optarg;
2565 p = find_section_list (optarg, TRUE);
2567 sections_removed = TRUE;
2570 strip_symbols = STRIP_ALL;
2574 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2575 strip_symbols = STRIP_DEBUG;
2577 case OPTION_STRIP_UNNEEDED:
2578 strip_symbols = STRIP_UNNEEDED;
2581 add_specific_symbol (optarg, &keep_specific_list);
2584 add_specific_symbol (optarg, &strip_specific_list);
2587 output_file = optarg;
2590 preserve_dates = TRUE;
2593 discard_locals = LOCALS_ALL;
2596 discard_locals = LOCALS_START_L;
2602 show_version = TRUE;
2604 case OPTION_FORMATS_INFO:
2605 formats_info = TRUE;
2607 case OPTION_ONLY_KEEP_DEBUG:
2608 strip_symbols = STRIP_NONDEBUG;
2610 case OPTION_KEEP_FILE_SYMBOLS:
2611 keep_file_symbols = 1;
2614 /* We've been given a long option. */
2621 strip_usage (stdout, 0);
2623 strip_usage (stderr, 1);
2634 print_version ("strip");
2636 /* Default is to strip all symbols. */
2637 if (strip_symbols == STRIP_UNDEF
2638 && discard_locals == LOCALS_UNDEF
2639 && strip_specific_list == NULL)
2640 strip_symbols = STRIP_ALL;
2642 if (output_target == NULL)
2643 output_target = input_target;
2647 || (output_file != NULL && (i + 1) < argc))
2648 strip_usage (stderr, 1);
2650 for (; i < argc; i++)
2652 int hold_status = status;
2653 struct stat statbuf;
2656 if (get_file_size (argv[i]) < 1)
2663 /* No need to check the return value of stat().
2664 It has already been checked in get_file_size(). */
2665 stat (argv[i], &statbuf);
2667 if (output_file != NULL)
2668 tmpname = output_file;
2670 tmpname = make_tempname (argv[i]);
2673 copy_file (argv[i], tmpname, input_target, output_target);
2677 set_times (tmpname, &statbuf);
2678 if (output_file == NULL)
2679 smart_rename (tmpname, argv[i], preserve_dates);
2680 status = hold_status;
2683 unlink_if_ordinary (tmpname);
2684 if (output_file == NULL)
2692 copy_main (int argc, char *argv[])
2694 char * binary_architecture = NULL;
2695 char *input_filename = NULL;
2696 char *output_filename = NULL;
2697 char *input_target = NULL;
2698 char *output_target = NULL;
2699 bfd_boolean show_version = FALSE;
2700 bfd_boolean change_warn = TRUE;
2701 bfd_boolean formats_info = FALSE;
2703 struct section_list *p;
2704 struct stat statbuf;
2706 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2707 copy_options, (int *) 0)) != EOF)
2712 copy_byte = atoi (optarg);
2714 fatal (_("byte number must be non-negative"));
2718 binary_architecture = optarg;
2722 interleave = atoi (optarg);
2724 fatal (_("interleave must be positive"));
2728 case 's': /* "source" - 'I' is preferred */
2729 input_target = optarg;
2733 case 'd': /* "destination" - 'O' is preferred */
2734 output_target = optarg;
2738 input_target = output_target = optarg;
2742 p = find_section_list (optarg, TRUE);
2744 fatal (_("%s both copied and removed"), optarg);
2746 sections_copied = TRUE;
2750 p = find_section_list (optarg, TRUE);
2752 fatal (_("%s both copied and removed"), optarg);
2754 sections_removed = TRUE;
2758 strip_symbols = STRIP_ALL;
2762 strip_symbols = STRIP_DEBUG;
2765 case OPTION_STRIP_UNNEEDED:
2766 strip_symbols = STRIP_UNNEEDED;
2769 case OPTION_ONLY_KEEP_DEBUG:
2770 strip_symbols = STRIP_NONDEBUG;
2773 case OPTION_KEEP_FILE_SYMBOLS:
2774 keep_file_symbols = 1;
2777 case OPTION_ADD_GNU_DEBUGLINK:
2778 gnu_debuglink_filename = optarg;
2782 add_specific_symbol (optarg, &keep_specific_list);
2786 add_specific_symbol (optarg, &strip_specific_list);
2789 case OPTION_STRIP_UNNEEDED_SYMBOL:
2790 add_specific_symbol (optarg, &strip_unneeded_list);
2794 add_specific_symbol (optarg, &localize_specific_list);
2797 case OPTION_GLOBALIZE_SYMBOL:
2798 add_specific_symbol (optarg, &globalize_specific_list);
2802 add_specific_symbol (optarg, &keepglobal_specific_list);
2806 add_specific_symbol (optarg, &weaken_specific_list);
2810 preserve_dates = TRUE;
2818 discard_locals = LOCALS_ALL;
2822 discard_locals = LOCALS_START_L;
2830 show_version = TRUE;
2833 case OPTION_FORMATS_INFO:
2834 formats_info = TRUE;
2841 case OPTION_ADD_SECTION:
2845 struct section_add *pa;
2850 s = strchr (optarg, '=');
2853 fatal (_("bad format for %s"), "--add-section");
2855 size = get_file_size (s + 1);
2862 pa = xmalloc (sizeof (struct section_add));
2865 name = xmalloc (len + 1);
2866 strncpy (name, optarg, len);
2870 pa->filename = s + 1;
2872 pa->contents = xmalloc (size);
2874 f = fopen (pa->filename, FOPEN_RB);
2877 fatal (_("cannot open: %s: %s"),
2878 pa->filename, strerror (errno));
2880 if (fread (pa->contents, 1, pa->size, f) == 0
2882 fatal (_("%s: fread failed"), pa->filename);
2886 pa->next = add_sections;
2891 case OPTION_CHANGE_START:
2892 change_start = parse_vma (optarg, "--change-start");
2895 case OPTION_CHANGE_SECTION_ADDRESS:
2896 case OPTION_CHANGE_SECTION_LMA:
2897 case OPTION_CHANGE_SECTION_VMA:
2902 char *option = NULL;
2904 enum change_action what = CHANGE_IGNORE;
2908 case OPTION_CHANGE_SECTION_ADDRESS:
2909 option = "--change-section-address";
2911 case OPTION_CHANGE_SECTION_LMA:
2912 option = "--change-section-lma";
2914 case OPTION_CHANGE_SECTION_VMA:
2915 option = "--change-section-vma";
2919 s = strchr (optarg, '=');
2922 s = strchr (optarg, '+');
2925 s = strchr (optarg, '-');
2927 fatal (_("bad format for %s"), option);
2932 name = xmalloc (len + 1);
2933 strncpy (name, optarg, len);
2936 p = find_section_list (name, TRUE);
2938 val = parse_vma (s + 1, option);
2942 case '=': what = CHANGE_SET; break;
2943 case '-': val = - val; /* Drop through. */
2944 case '+': what = CHANGE_MODIFY; break;
2949 case OPTION_CHANGE_SECTION_ADDRESS:
2950 p->change_vma = what;
2954 case OPTION_CHANGE_SECTION_LMA:
2955 p->change_lma = what;
2959 case OPTION_CHANGE_SECTION_VMA:
2960 p->change_vma = what;
2967 case OPTION_CHANGE_ADDRESSES:
2968 change_section_address = parse_vma (optarg, "--change-addresses");
2969 change_start = change_section_address;
2972 case OPTION_CHANGE_WARNINGS:
2976 case OPTION_CHANGE_LEADING_CHAR:
2977 change_leading_char = TRUE;
2980 case OPTION_DEBUGGING:
2981 convert_debugging = TRUE;
2984 case OPTION_GAP_FILL:
2986 bfd_vma gap_fill_vma;
2988 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2989 gap_fill = (bfd_byte) gap_fill_vma;
2990 if ((bfd_vma) gap_fill != gap_fill_vma)
2994 sprintf_vma (buff, gap_fill_vma);
2996 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2999 gap_fill_set = TRUE;
3003 case OPTION_NO_CHANGE_WARNINGS:
3004 change_warn = FALSE;
3008 pad_to = parse_vma (optarg, "--pad-to");
3012 case OPTION_REMOVE_LEADING_CHAR:
3013 remove_leading_char = TRUE;
3016 case OPTION_REDEFINE_SYM:
3018 /* Push this redefinition onto redefine_symbol_list. */
3022 const char *nextarg;
3023 char *source, *target;
3025 s = strchr (optarg, '=');
3027 fatal (_("bad format for %s"), "--redefine-sym");
3030 source = xmalloc (len + 1);
3031 strncpy (source, optarg, len);
3035 len = strlen (nextarg);
3036 target = xmalloc (len + 1);
3037 strcpy (target, nextarg);
3039 redefine_list_append ("--redefine-sym", source, target);
3046 case OPTION_REDEFINE_SYMS:
3047 add_redefine_syms_file (optarg);
3050 case OPTION_SET_SECTION_FLAGS:
3056 s = strchr (optarg, '=');
3058 fatal (_("bad format for %s"), "--set-section-flags");
3061 name = xmalloc (len + 1);
3062 strncpy (name, optarg, len);
3065 p = find_section_list (name, TRUE);
3067 p->set_flags = TRUE;
3068 p->flags = parse_flags (s + 1);
3072 case OPTION_RENAME_SECTION:
3075 const char *eq, *fl;
3080 eq = strchr (optarg, '=');
3082 fatal (_("bad format for %s"), "--rename-section");
3086 fatal (_("bad format for %s"), "--rename-section");
3088 old_name = xmalloc (len + 1);
3089 strncpy (old_name, optarg, len);
3093 fl = strchr (eq, ',');
3096 flags = parse_flags (fl + 1);
3106 fatal (_("bad format for %s"), "--rename-section");
3108 new_name = xmalloc (len + 1);
3109 strncpy (new_name, eq, len);
3112 add_section_rename (old_name, new_name, flags);
3116 case OPTION_SET_START:
3117 set_start = parse_vma (optarg, "--set-start");
3118 set_start_set = TRUE;
3121 case OPTION_SREC_LEN:
3122 Chunk = parse_vma (optarg, "--srec-len");
3125 case OPTION_SREC_FORCES3:
3129 case OPTION_STRIP_SYMBOLS:
3130 add_specific_symbols (optarg, &strip_specific_list);
3133 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3134 add_specific_symbols (optarg, &strip_unneeded_list);
3137 case OPTION_KEEP_SYMBOLS:
3138 add_specific_symbols (optarg, &keep_specific_list);
3141 case OPTION_LOCALIZE_HIDDEN:
3142 localize_hidden = TRUE;
3145 case OPTION_LOCALIZE_SYMBOLS:
3146 add_specific_symbols (optarg, &localize_specific_list);
3149 case OPTION_GLOBALIZE_SYMBOLS:
3150 add_specific_symbols (optarg, &globalize_specific_list);
3153 case OPTION_KEEPGLOBAL_SYMBOLS:
3154 add_specific_symbols (optarg, &keepglobal_specific_list);
3157 case OPTION_WEAKEN_SYMBOLS:
3158 add_specific_symbols (optarg, &weaken_specific_list);
3161 case OPTION_ALT_MACH_CODE:
3162 use_alt_mach_code = strtoul (optarg, NULL, 0);
3163 if (use_alt_mach_code == 0)
3164 fatal (_("unable to parse alternative machine code"));
3167 case OPTION_PREFIX_SYMBOLS:
3168 prefix_symbols_string = optarg;
3171 case OPTION_PREFIX_SECTIONS:
3172 prefix_sections_string = optarg;
3175 case OPTION_PREFIX_ALLOC_SECTIONS:
3176 prefix_alloc_sections_string = optarg;
3179 case OPTION_READONLY_TEXT:
3180 bfd_flags_to_set |= WP_TEXT;
3181 bfd_flags_to_clear &= ~WP_TEXT;
3184 case OPTION_WRITABLE_TEXT:
3185 bfd_flags_to_clear |= WP_TEXT;
3186 bfd_flags_to_set &= ~WP_TEXT;
3190 bfd_flags_to_set |= D_PAGED;
3191 bfd_flags_to_clear &= ~D_PAGED;
3195 bfd_flags_to_clear |= D_PAGED;
3196 bfd_flags_to_set &= ~D_PAGED;
3200 /* We've been given a long option. */
3205 copy_usage (stdout, 0);
3208 copy_usage (stderr, 1);
3219 print_version ("objcopy");
3221 if (copy_byte >= interleave)
3222 fatal (_("byte number must be less than interleave"));
3224 if (optind == argc || optind + 2 < argc)
3225 copy_usage (stderr, 1);
3227 input_filename = argv[optind];
3228 if (optind + 1 < argc)
3229 output_filename = argv[optind + 1];
3231 /* Default is to strip no symbols. */
3232 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3233 strip_symbols = STRIP_NONE;
3235 if (output_target == NULL)
3236 output_target = input_target;
3238 if (binary_architecture != NULL)
3240 if (input_target && strcmp (input_target, "binary") == 0)
3242 const bfd_arch_info_type * temp_arch_info;
3244 temp_arch_info = bfd_scan_arch (binary_architecture);
3246 if (temp_arch_info != NULL)
3248 bfd_external_binary_architecture = temp_arch_info->arch;
3249 bfd_external_machine = temp_arch_info->mach;
3252 fatal (_("architecture %s unknown"), binary_architecture);
3256 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3257 non_fatal (_(" Argument %s ignored"), binary_architecture);
3262 if (stat (input_filename, & statbuf) < 0)
3263 fatal (_("warning: could not locate '%s'. System error message: %s"),
3264 input_filename, strerror (errno));
3266 /* If there is no destination file, or the source and destination files
3267 are the same, then create a temp and rename the result into the input. */
3268 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3270 char *tmpname = make_tempname (input_filename);
3272 copy_file (input_filename, tmpname, input_target, output_target);
3276 set_times (tmpname, &statbuf);
3277 smart_rename (tmpname, input_filename, preserve_dates);
3284 copy_file (input_filename, output_filename, input_target, output_target);
3286 if (status == 0 && preserve_dates)
3287 set_times (output_filename, &statbuf);
3288 else if (status != 0)
3289 unlink_if_ordinary (output_filename);
3294 for (p = change_sections; p != NULL; p = p->next)
3298 if (p->change_vma != CHANGE_IGNORE)
3302 sprintf_vma (buff, p->vma_val);
3304 /* xgettext:c-format */
3305 non_fatal (_("%s %s%c0x%s never used"),
3306 "--change-section-vma",
3308 p->change_vma == CHANGE_SET ? '=' : '+',
3312 if (p->change_lma != CHANGE_IGNORE)
3316 sprintf_vma (buff, p->lma_val);
3318 /* xgettext:c-format */
3319 non_fatal (_("%s %s%c0x%s never used"),
3320 "--change-section-lma",
3322 p->change_lma == CHANGE_SET ? '=' : '+',
3333 main (int argc, char *argv[])
3335 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3336 setlocale (LC_MESSAGES, "");
3338 #if defined (HAVE_SETLOCALE)
3339 setlocale (LC_CTYPE, "");
3341 bindtextdomain (PACKAGE, LOCALEDIR);
3342 textdomain (PACKAGE);
3344 program_name = argv[0];
3345 xmalloc_set_program_name (program_name);
3347 START_PROGRESS (program_name, 0);
3349 expandargv (&argc, &argv);
3351 strip_symbols = STRIP_UNDEF;
3352 discard_locals = LOCALS_UNDEF;
3355 set_default_bfd_target ();
3359 int i = strlen (program_name);
3360 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3361 /* Drop the .exe suffix, if any. */
3362 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3365 program_name[i] = '\0';
3368 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3372 strip_main (argc, argv);
3374 copy_main (argc, argv);
3376 END_PROGRESS (program_name);