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
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
27 #include "libiberty.h"
30 #include "filenames.h"
36 struct is_specified_symbol_predicate_data
42 /* A list to support redefine_sym. */
47 struct redefine_node *next;
50 typedef struct section_rename
52 const char * old_name;
53 const char * new_name;
55 struct section_rename * next;
59 /* List of sections to be renamed. */
60 static section_rename *section_rename_list;
62 static asymbol **isympp = NULL; /* Input symbols. */
63 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
65 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
66 static int copy_byte = -1;
67 static int interleave = 4;
69 static bfd_boolean verbose; /* Print file and target names. */
70 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
71 static int status = 0; /* Exit status. */
76 STRIP_NONE, /* Don't strip. */
77 STRIP_DEBUG, /* Strip all debugger symbols. */
78 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
79 STRIP_NONDEBUG, /* Strip everything but debug info. */
80 STRIP_ALL /* Strip all symbols. */
83 /* Which symbols to remove. */
84 static enum strip_action strip_symbols;
89 LOCALS_START_L, /* Discard locals starting with L. */
90 LOCALS_ALL /* Discard all locals. */
93 /* Which local symbols to remove. Overrides STRIP_ALL. */
94 static enum locals_action discard_locals;
96 /* What kind of change to perform. */
104 /* Structure used to hold lists of sections and actions to take. */
107 struct section_list * next; /* Next section to change. */
108 const char * name; /* Section name. */
109 bfd_boolean used; /* Whether this entry was used. */
110 bfd_boolean remove; /* Whether to remove this section. */
111 bfd_boolean copy; /* Whether to copy this section. */
112 enum change_action change_vma;/* Whether to change or set VMA. */
113 bfd_vma vma_val; /* Amount to change by or set to. */
114 enum change_action change_lma;/* Whether to change or set LMA. */
115 bfd_vma lma_val; /* Amount to change by or set to. */
116 bfd_boolean set_flags; /* Whether to set the section flags. */
117 flagword flags; /* What to set the section flags to. */
120 static struct section_list *change_sections;
122 /* TRUE if some sections are to be removed. */
123 static bfd_boolean sections_removed;
125 /* TRUE if only some sections are to be copied. */
126 static bfd_boolean sections_copied;
128 /* Changes to the start address. */
129 static bfd_vma change_start = 0;
130 static bfd_boolean set_start_set = FALSE;
131 static bfd_vma set_start;
133 /* Changes to section addresses. */
134 static bfd_vma change_section_address = 0;
136 /* Filling gaps between sections. */
137 static bfd_boolean gap_fill_set = FALSE;
138 static bfd_byte gap_fill = 0;
140 /* Pad to a given address. */
141 static bfd_boolean pad_to_set = FALSE;
142 static bfd_vma pad_to;
144 /* Use alternative machine code? */
145 static unsigned long use_alt_mach_code = 0;
147 /* Output BFD flags user wants to set or clear */
148 static flagword bfd_flags_to_set;
149 static flagword bfd_flags_to_clear;
151 /* List of sections to add. */
154 /* Next section to add. */
155 struct section_add *next;
156 /* Name of section to add. */
158 /* Name of file holding section contents. */
159 const char *filename;
162 /* Contents of file. */
164 /* BFD section, after it has been added. */
168 /* List of sections to add to the output BFD. */
169 static struct section_add *add_sections;
171 /* If non-NULL the argument to --add-gnu-debuglink.
172 This should be the filename to store in the .gnu_debuglink section. */
173 static const char * gnu_debuglink_filename = NULL;
175 /* Whether to convert debugging information. */
176 static bfd_boolean convert_debugging = FALSE;
178 /* Whether to change the leading character in symbol names. */
179 static bfd_boolean change_leading_char = FALSE;
181 /* Whether to remove the leading character from global symbol names. */
182 static bfd_boolean remove_leading_char = FALSE;
184 /* Whether to permit wildcard in symbol comparison. */
185 static bfd_boolean wildcard = FALSE;
187 /* True if --localize-hidden is in effect. */
188 static bfd_boolean localize_hidden = FALSE;
190 /* List of symbols to strip, keep, localize, keep-global, weaken,
192 static htab_t strip_specific_htab = NULL;
193 static htab_t strip_unneeded_htab = NULL;
194 static htab_t keep_specific_htab = NULL;
195 static htab_t localize_specific_htab = NULL;
196 static htab_t globalize_specific_htab = NULL;
197 static htab_t keepglobal_specific_htab = NULL;
198 static htab_t weaken_specific_htab = NULL;
199 static struct redefine_node *redefine_sym_list = NULL;
201 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
202 static bfd_boolean weaken = FALSE;
204 /* If this is TRUE, we retain BSF_FILE symbols. */
205 static bfd_boolean keep_file_symbols = FALSE;
207 /* Prefix symbols/sections. */
208 static char *prefix_symbols_string = 0;
209 static char *prefix_sections_string = 0;
210 static char *prefix_alloc_sections_string = 0;
212 /* True if --extract-symbol was passed on the command line. */
213 static bfd_boolean extract_symbol = FALSE;
215 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
216 of <reverse_bytes> bytes within each output section. */
217 static int reverse_bytes = 0;
220 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
221 enum command_line_switch
223 OPTION_ADD_SECTION=150,
224 OPTION_CHANGE_ADDRESSES,
225 OPTION_CHANGE_LEADING_CHAR,
227 OPTION_CHANGE_SECTION_ADDRESS,
228 OPTION_CHANGE_SECTION_LMA,
229 OPTION_CHANGE_SECTION_VMA,
230 OPTION_CHANGE_WARNINGS,
233 OPTION_NO_CHANGE_WARNINGS,
235 OPTION_REMOVE_LEADING_CHAR,
236 OPTION_SET_SECTION_FLAGS,
238 OPTION_STRIP_UNNEEDED,
241 OPTION_REDEFINE_SYMS,
244 OPTION_STRIP_SYMBOLS,
245 OPTION_STRIP_UNNEEDED_SYMBOL,
246 OPTION_STRIP_UNNEEDED_SYMBOLS,
248 OPTION_LOCALIZE_HIDDEN,
249 OPTION_LOCALIZE_SYMBOLS,
250 OPTION_GLOBALIZE_SYMBOL,
251 OPTION_GLOBALIZE_SYMBOLS,
252 OPTION_KEEPGLOBAL_SYMBOLS,
253 OPTION_WEAKEN_SYMBOLS,
254 OPTION_RENAME_SECTION,
255 OPTION_ALT_MACH_CODE,
256 OPTION_PREFIX_SYMBOLS,
257 OPTION_PREFIX_SECTIONS,
258 OPTION_PREFIX_ALLOC_SECTIONS,
260 OPTION_ADD_GNU_DEBUGLINK,
261 OPTION_ONLY_KEEP_DEBUG,
262 OPTION_KEEP_FILE_SYMBOLS,
263 OPTION_READONLY_TEXT,
264 OPTION_WRITABLE_TEXT,
267 OPTION_EXTRACT_SYMBOL,
271 /* Options to handle if running as "strip". */
273 static struct option strip_options[] =
275 {"discard-all", no_argument, 0, 'x'},
276 {"discard-locals", no_argument, 0, 'X'},
277 {"format", required_argument, 0, 'F'}, /* Obsolete */
278 {"help", no_argument, 0, 'h'},
279 {"info", no_argument, 0, OPTION_FORMATS_INFO},
280 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
281 {"input-target", required_argument, 0, 'I'},
282 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
283 {"keep-symbol", required_argument, 0, 'K'},
284 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
285 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
286 {"output-target", required_argument, 0, 'O'},
287 {"output-file", required_argument, 0, 'o'},
288 {"preserve-dates", no_argument, 0, 'p'},
289 {"remove-section", required_argument, 0, 'R'},
290 {"strip-all", no_argument, 0, 's'},
291 {"strip-debug", no_argument, 0, 'S'},
292 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
293 {"strip-symbol", required_argument, 0, 'N'},
294 {"target", required_argument, 0, 'F'},
295 {"verbose", no_argument, 0, 'v'},
296 {"version", no_argument, 0, 'V'},
297 {"wildcard", no_argument, 0, 'w'},
298 {0, no_argument, 0, 0}
301 /* Options to handle if running as "objcopy". */
303 static struct option copy_options[] =
305 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
306 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
307 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
308 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
309 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
310 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
311 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
312 {"binary-architecture", required_argument, 0, 'B'},
313 {"byte", required_argument, 0, 'b'},
314 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
315 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
316 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
317 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
318 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
319 {"change-start", required_argument, 0, OPTION_CHANGE_START},
320 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
321 {"debugging", no_argument, 0, OPTION_DEBUGGING},
322 {"discard-all", no_argument, 0, 'x'},
323 {"discard-locals", no_argument, 0, 'X'},
324 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
325 {"format", required_argument, 0, 'F'}, /* Obsolete */
326 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
327 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
328 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
329 {"help", no_argument, 0, 'h'},
330 {"impure", no_argument, 0, OPTION_IMPURE},
331 {"info", no_argument, 0, OPTION_FORMATS_INFO},
332 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
333 {"input-target", required_argument, 0, 'I'},
334 {"interleave", required_argument, 0, 'i'},
335 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
336 {"keep-global-symbol", required_argument, 0, 'G'},
337 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
338 {"keep-symbol", required_argument, 0, 'K'},
339 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
340 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
341 {"localize-symbol", required_argument, 0, 'L'},
342 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
343 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
344 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
345 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
346 {"only-section", required_argument, 0, 'j'},
347 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
348 {"output-target", required_argument, 0, 'O'},
349 {"pad-to", required_argument, 0, OPTION_PAD_TO},
350 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
351 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
352 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
353 {"preserve-dates", no_argument, 0, 'p'},
354 {"pure", no_argument, 0, OPTION_PURE},
355 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
356 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
357 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
358 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
359 {"remove-section", required_argument, 0, 'R'},
360 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
361 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
362 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
363 {"set-start", required_argument, 0, OPTION_SET_START},
364 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
365 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
366 {"strip-all", no_argument, 0, 'S'},
367 {"strip-debug", no_argument, 0, 'g'},
368 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
369 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
370 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
371 {"strip-symbol", required_argument, 0, 'N'},
372 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
373 {"target", required_argument, 0, 'F'},
374 {"verbose", no_argument, 0, 'v'},
375 {"version", no_argument, 0, 'V'},
376 {"weaken", no_argument, 0, OPTION_WEAKEN},
377 {"weaken-symbol", required_argument, 0, 'W'},
378 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
379 {"wildcard", no_argument, 0, 'w'},
380 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
381 {0, no_argument, 0, 0}
385 extern char *program_name;
387 /* This flag distinguishes between strip and objcopy:
388 1 means this is 'strip'; 0 means this is 'objcopy'.
389 -1 means if we should use argv[0] to decide. */
392 /* The maximum length of an S record. This variable is declared in srec.c
393 and can be modified by the --srec-len parameter. */
394 extern unsigned int Chunk;
396 /* Restrict the generation of Srecords to type S3 only.
397 This variable is declare in bfd/srec.c and can be toggled
398 on by the --srec-forceS3 command line switch. */
399 extern bfd_boolean S3Forced;
401 /* Defined in bfd/binary.c. Used to set architecture and machine of input
403 extern enum bfd_architecture bfd_external_binary_architecture;
404 extern unsigned long bfd_external_machine;
406 /* Forward declarations. */
407 static void setup_section (bfd *, asection *, void *);
408 static void setup_bfd_headers (bfd *, bfd *);
409 static void copy_section (bfd *, asection *, void *);
410 static void get_sections (bfd *, asection *, void *);
411 static int compare_section_lma (const void *, const void *);
412 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
413 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
414 static const char *lookup_sym_redefinition (const char *);
417 copy_usage (FILE *stream, int exit_status)
419 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
420 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
421 fprintf (stream, _(" The options are:\n"));
422 fprintf (stream, _("\
423 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
424 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
425 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
426 -F --target <bfdname> Set both input and output format to <bfdname>\n\
427 --debugging Convert debugging information, if possible\n\
428 -p --preserve-dates Copy modified/access timestamps to the output\n\
429 -j --only-section <name> Only copy section <name> into the output\n\
430 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
431 -R --remove-section <name> Remove section <name> from the output\n\
432 -S --strip-all Remove all symbol and relocation information\n\
433 -g --strip-debug Remove all debugging symbols & sections\n\
434 --strip-unneeded Remove all symbols not needed by relocations\n\
435 -N --strip-symbol <name> Do not copy symbol <name>\n\
436 --strip-unneeded-symbol <name>\n\
437 Do not copy symbol <name> unless needed by\n\
439 --only-keep-debug Strip everything but the debug information\n\
440 --extract-symbol Remove section contents but keep symbols\n\
441 -K --keep-symbol <name> Do not strip symbol <name>\n\
442 --keep-file-symbols Do not strip file symbol(s)\n\
443 --localize-hidden Turn all ELF hidden symbols into locals\n\
444 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
445 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
446 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
447 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
448 --weaken Force all global symbols to be marked as weak\n\
449 -w --wildcard Permit wildcard in symbol comparison\n\
450 -x --discard-all Remove all non-global symbols\n\
451 -X --discard-locals Remove any compiler-generated symbols\n\
452 -i --interleave <number> Only copy one out of every <number> bytes\n\
453 -b --byte <num> Select byte <num> in every interleaved block\n\
454 --gap-fill <val> Fill gaps between sections with <val>\n\
455 --pad-to <addr> Pad the last section up to address <addr>\n\
456 --set-start <addr> Set the start address to <addr>\n\
457 {--change-start|--adjust-start} <incr>\n\
458 Add <incr> to the start address\n\
459 {--change-addresses|--adjust-vma} <incr>\n\
460 Add <incr> to LMA, VMA and start addresses\n\
461 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
462 Change LMA and VMA of section <name> by <val>\n\
463 --change-section-lma <name>{=|+|-}<val>\n\
464 Change the LMA of section <name> by <val>\n\
465 --change-section-vma <name>{=|+|-}<val>\n\
466 Change the VMA of section <name> by <val>\n\
467 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
468 Warn if a named section does not exist\n\
469 --set-section-flags <name>=<flags>\n\
470 Set section <name>'s properties to <flags>\n\
471 --add-section <name>=<file> Add section <name> found in <file> to output\n\
472 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
473 --change-leading-char Force output format's leading character style\n\
474 --remove-leading-char Remove leading character from global symbols\n\
475 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
476 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
477 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
479 --srec-len <number> Restrict the length of generated Srecords\n\
480 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
481 --strip-symbols <file> -N for all symbols listed in <file>\n\
482 --strip-unneeded-symbols <file>\n\
483 --strip-unneeded-symbol for all symbols listed\n\
485 --keep-symbols <file> -K for all symbols listed in <file>\n\
486 --localize-symbols <file> -L for all symbols listed in <file>\n\
487 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
488 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
489 --weaken-symbols <file> -W for all symbols listed in <file>\n\
490 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
491 --writable-text Mark the output text as writable\n\
492 --readonly-text Make the output text write protected\n\
493 --pure Mark the output file as demand paged\n\
494 --impure Mark the output file as impure\n\
495 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
496 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
497 --prefix-alloc-sections <prefix>\n\
498 Add <prefix> to start of every allocatable\n\
500 -v --verbose List all object files modified\n\
501 @<file> Read options from <file>\n\
502 -V --version Display this program's version number\n\
503 -h --help Display this output\n\
504 --info List object formats & architectures supported\n\
506 list_supported_targets (program_name, stream);
507 if (REPORT_BUGS_TO[0] && exit_status == 0)
508 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
513 strip_usage (FILE *stream, int exit_status)
515 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
516 fprintf (stream, _(" Removes symbols and sections from files\n"));
517 fprintf (stream, _(" The options are:\n"));
518 fprintf (stream, _("\
519 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
520 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
521 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
522 -p --preserve-dates Copy modified/access timestamps to the output\n\
523 -R --remove-section=<name> Remove section <name> from the output\n\
524 -s --strip-all Remove all symbol and relocation information\n\
525 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
526 --strip-unneeded Remove all symbols not needed by relocations\n\
527 --only-keep-debug Strip everything but the debug information\n\
528 -N --strip-symbol=<name> Do not copy symbol <name>\n\
529 -K --keep-symbol=<name> Do not strip symbol <name>\n\
530 --keep-file-symbols Do not strip file symbol(s)\n\
531 -w --wildcard Permit wildcard in symbol comparison\n\
532 -x --discard-all Remove all non-global symbols\n\
533 -X --discard-locals Remove any compiler-generated symbols\n\
534 -v --verbose List all object files modified\n\
535 -V --version Display this program's version number\n\
536 -h --help Display this output\n\
537 --info List object formats & architectures supported\n\
538 -o <file> Place stripped output into <file>\n\
541 list_supported_targets (program_name, stream);
542 if (REPORT_BUGS_TO[0] && exit_status == 0)
543 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
547 /* Parse section flags into a flagword, with a fatal error if the
548 string can't be parsed. */
551 parse_flags (const char *s)
561 snext = strchr (s, ',');
571 #define PARSE_FLAG(fname,fval) \
572 else if (strncasecmp (fname, s, len) == 0) ret |= fval
573 PARSE_FLAG ("alloc", SEC_ALLOC);
574 PARSE_FLAG ("load", SEC_LOAD);
575 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
576 PARSE_FLAG ("readonly", SEC_READONLY);
577 PARSE_FLAG ("debug", SEC_DEBUGGING);
578 PARSE_FLAG ("code", SEC_CODE);
579 PARSE_FLAG ("data", SEC_DATA);
580 PARSE_FLAG ("rom", SEC_ROM);
581 PARSE_FLAG ("share", SEC_COFF_SHARED);
582 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
588 copy = xmalloc (len + 1);
589 strncpy (copy, s, len);
591 non_fatal (_("unrecognized section flag `%s'"), copy);
592 fatal (_("supported flags: %s"),
593 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
603 /* Find and optionally add an entry in the change_sections list. */
605 static struct section_list *
606 find_section_list (const char *name, bfd_boolean add)
608 struct section_list *p;
610 for (p = change_sections; p != NULL; p = p->next)
611 if (strcmp (p->name, name) == 0)
617 p = xmalloc (sizeof (struct section_list));
622 p->change_vma = CHANGE_IGNORE;
623 p->change_lma = CHANGE_IGNORE;
626 p->set_flags = FALSE;
629 p->next = change_sections;
635 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
638 eq_string (const void *s1, const void *s2)
640 return strcmp (s1, s2) == 0;
644 create_symbol_htab (void)
646 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
650 create_symbol_htabs (void)
652 strip_specific_htab = create_symbol_htab ();
653 strip_unneeded_htab = create_symbol_htab ();
654 keep_specific_htab = create_symbol_htab ();
655 localize_specific_htab = create_symbol_htab ();
656 globalize_specific_htab = create_symbol_htab ();
657 keepglobal_specific_htab = create_symbol_htab ();
658 weaken_specific_htab = create_symbol_htab ();
661 /* Add a symbol to strip_specific_list. */
664 add_specific_symbol (const char *name, htab_t htab)
666 *htab_find_slot (htab, name, INSERT) = (char *) name;
669 /* Add symbols listed in `filename' to strip_specific_list. */
671 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
672 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
675 add_specific_symbols (const char *filename, htab_t htab)
681 unsigned int line_count;
683 size = get_file_size (filename);
690 buffer = xmalloc (size + 2);
691 f = fopen (filename, FOPEN_RT);
693 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
695 if (fread (buffer, 1, size, f) == 0 || ferror (f))
696 fatal (_("%s: fread failed"), filename);
699 buffer [size] = '\n';
700 buffer [size + 1] = '\0';
704 for (line = buffer; * line != '\0'; line ++)
709 int finished = FALSE;
711 for (eol = line;; eol ++)
717 /* Cope with \n\r. */
725 /* Cope with \r\n. */
736 /* Line comment, Terminate the line here, in case a
737 name is present and then allow the rest of the
738 loop to find the real end of the line. */
750 /* A name may now exist somewhere between 'line' and 'eol'.
751 Strip off leading whitespace and trailing whitespace,
752 then add it to the list. */
753 for (name = line; IS_WHITESPACE (* name); name ++)
755 for (name_end = name;
756 (! IS_WHITESPACE (* name_end))
757 && (! IS_LINE_TERMINATOR (* name_end));
761 if (! IS_LINE_TERMINATOR (* name_end))
765 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
768 if (! IS_LINE_TERMINATOR (* extra))
769 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
770 filename, line_count);
776 add_specific_symbol (name, htab);
778 /* Advance line pointer to end of line. The 'eol ++' in the for
779 loop above will then advance us to the start of the next line. */
785 /* See whether a symbol should be stripped or kept
786 based on strip_specific_list and keep_symbols. */
789 is_specified_symbol_predicate (void **slot, void *data)
791 struct is_specified_symbol_predicate_data *d = data;
792 const char *slot_name = *slot;
794 if (*slot_name != '!')
796 if (! fnmatch (slot_name, d->name, 0))
799 /* Stop traversal. */
805 if (fnmatch (slot_name + 1, d->name, 0))
808 /* Stop traversal. */
813 /* Continue traversal. */
818 is_specified_symbol (const char *name, htab_t htab)
822 struct is_specified_symbol_predicate_data data;
827 htab_traverse (htab, is_specified_symbol_predicate, &data);
832 return htab_find (htab, name) != NULL;
835 /* Return a pointer to the symbol used as a signature for GROUP. */
838 group_signature (asection *group)
840 bfd *abfd = group->owner;
841 Elf_Internal_Shdr *ghdr;
843 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
846 ghdr = &elf_section_data (group)->this_hdr;
847 if (ghdr->sh_link < elf_numsections (abfd))
849 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
850 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
852 if (symhdr->sh_type == SHT_SYMTAB
853 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
854 return isympp[ghdr->sh_info - 1];
859 /* See if a section is being removed. */
862 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
864 if (sections_removed || sections_copied)
866 struct section_list *p;
868 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
870 if (sections_removed && p != NULL && p->remove)
872 if (sections_copied && (p == NULL || ! p->copy))
876 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
878 if (strip_symbols == STRIP_DEBUG
879 || strip_symbols == STRIP_UNNEEDED
880 || strip_symbols == STRIP_ALL
881 || discard_locals == LOCALS_ALL
882 || convert_debugging)
885 if (strip_symbols == STRIP_NONDEBUG)
889 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
895 Group sections look like debugging sections but they are not.
896 (They have a non-zero size but they are not ALLOCated). */
897 if (strip_symbols == STRIP_NONDEBUG)
901 If we are going to strip the group signature symbol, then
902 strip the group section too. */
903 gsym = group_signature (sec);
908 if ((strip_symbols == STRIP_ALL
909 && !is_specified_symbol (gname, keep_specific_htab))
910 || is_specified_symbol (gname, strip_specific_htab))
917 /* Return true if SYM is a hidden symbol. */
920 is_hidden_symbol (asymbol *sym)
922 elf_symbol_type *elf_sym;
924 elf_sym = elf_symbol_from (sym->the_bfd, sym);
926 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
935 /* Choose which symbol entries to copy; put the result in OSYMS.
936 We don't copy in place, because that confuses the relocs.
937 Return the number of symbols to print. */
940 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
941 asymbol **isyms, long symcount)
943 asymbol **from = isyms, **to = osyms;
944 long src_count = 0, dst_count = 0;
945 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
947 for (; src_count < symcount; src_count++)
949 asymbol *sym = from[src_count];
950 flagword flags = sym->flags;
951 char *name = (char *) bfd_asymbol_name (sym);
953 bfd_boolean used_in_reloc = FALSE;
954 bfd_boolean undefined;
955 bfd_boolean rem_leading_char;
956 bfd_boolean add_leading_char;
958 undefined = bfd_is_und_section (bfd_get_section (sym));
960 if (redefine_sym_list)
962 char *old_name, *new_name;
964 old_name = (char *) bfd_asymbol_name (sym);
965 new_name = (char *) lookup_sym_redefinition (old_name);
966 bfd_asymbol_name (sym) = new_name;
970 /* Check if we will remove the current leading character. */
972 (name[0] == bfd_get_symbol_leading_char (abfd))
973 && (change_leading_char
974 || (remove_leading_char
975 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
977 || bfd_is_com_section (bfd_get_section (sym)))));
979 /* Check if we will add a new leading character. */
982 && (bfd_get_symbol_leading_char (obfd) != '\0')
983 && (bfd_get_symbol_leading_char (abfd) == '\0'
984 || (name[0] == bfd_get_symbol_leading_char (abfd)));
986 /* Short circuit for change_leading_char if we can do it in-place. */
987 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
989 name[0] = bfd_get_symbol_leading_char (obfd);
990 bfd_asymbol_name (sym) = name;
991 rem_leading_char = FALSE;
992 add_leading_char = FALSE;
995 /* Remove leading char. */
996 if (rem_leading_char)
997 bfd_asymbol_name (sym) = ++name;
999 /* Add new leading char and/or prefix. */
1000 if (add_leading_char || prefix_symbols_string)
1004 ptr = n = xmalloc (1 + strlen (prefix_symbols_string)
1005 + strlen (name) + 1);
1006 if (add_leading_char)
1007 *ptr++ = bfd_get_symbol_leading_char (obfd);
1009 if (prefix_symbols_string)
1011 strcpy (ptr, prefix_symbols_string);
1012 ptr += strlen (prefix_symbols_string);
1016 bfd_asymbol_name (sym) = n;
1020 if (strip_symbols == STRIP_ALL)
1022 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1023 || ((flags & BSF_SECTION_SYM) != 0
1024 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1028 used_in_reloc = TRUE;
1030 else if (relocatable /* Relocatable file. */
1031 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1032 || bfd_is_com_section (bfd_get_section (sym))))
1034 else if (bfd_decode_symclass (sym) == 'I')
1035 /* Global symbols in $idata sections need to be retained
1036 even if relocatable is FALSE. External users of the
1037 library containing the $idata section may reference these
1040 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1041 || (flags & BSF_WEAK) != 0
1043 || bfd_is_com_section (bfd_get_section (sym)))
1044 keep = strip_symbols != STRIP_UNNEEDED;
1045 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1046 keep = (strip_symbols != STRIP_DEBUG
1047 && strip_symbols != STRIP_UNNEEDED
1048 && ! convert_debugging);
1049 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1050 /* COMDAT sections store special information in local
1051 symbols, so we cannot risk stripping any of them. */
1053 else /* Local symbol. */
1054 keep = (strip_symbols != STRIP_UNNEEDED
1055 && (discard_locals != LOCALS_ALL
1056 && (discard_locals != LOCALS_START_L
1057 || ! bfd_is_local_label (abfd, sym))));
1059 if (keep && is_specified_symbol (name, strip_specific_htab))
1061 /* There are multiple ways to set 'keep' above, but if it
1062 was the relocatable symbol case, then that's an error. */
1065 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1073 && !(flags & BSF_KEEP)
1074 && is_specified_symbol (name, strip_unneeded_htab))
1078 && ((keep_file_symbols && (flags & BSF_FILE))
1079 || is_specified_symbol (name, keep_specific_htab)))
1082 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1087 if ((flags & BSF_GLOBAL) != 0
1088 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1090 sym->flags &= ~ BSF_GLOBAL;
1091 sym->flags |= BSF_WEAK;
1095 && (flags & (BSF_GLOBAL | BSF_WEAK))
1096 && (is_specified_symbol (name, localize_specific_htab)
1097 || (htab_elements (keepglobal_specific_htab) != 0
1098 && ! is_specified_symbol (name, keepglobal_specific_htab))
1099 || (localize_hidden && is_hidden_symbol (sym))))
1101 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1102 sym->flags |= BSF_LOCAL;
1106 && (flags & BSF_LOCAL)
1107 && is_specified_symbol (name, globalize_specific_htab))
1109 sym->flags &= ~ BSF_LOCAL;
1110 sym->flags |= BSF_GLOBAL;
1113 to[dst_count++] = sym;
1117 to[dst_count] = NULL;
1122 /* Find the redefined name of symbol SOURCE. */
1125 lookup_sym_redefinition (const char *source)
1127 struct redefine_node *list;
1129 for (list = redefine_sym_list; list != NULL; list = list->next)
1130 if (strcmp (source, list->source) == 0)
1131 return list->target;
1136 /* Add a node to a symbol redefine list. */
1139 redefine_list_append (const char *cause, const char *source, const char *target)
1141 struct redefine_node **p;
1142 struct redefine_node *list;
1143 struct redefine_node *new_node;
1145 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1147 if (strcmp (source, list->source) == 0)
1148 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1151 if (strcmp (target, list->target) == 0)
1152 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1156 new_node = xmalloc (sizeof (struct redefine_node));
1158 new_node->source = strdup (source);
1159 new_node->target = strdup (target);
1160 new_node->next = NULL;
1165 /* Handle the --redefine-syms option. Read lines containing "old new"
1166 from the file, and add them to the symbol redefine list. */
1169 add_redefine_syms_file (const char *filename)
1178 file = fopen (filename, "r");
1180 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1181 filename, strerror (errno));
1184 buf = xmalloc (bufsize);
1192 /* Collect the input symbol name. */
1193 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1201 buf = xrealloc (buf, bufsize);
1209 /* Eat white space between the symbol names. */
1210 while (IS_WHITESPACE (c))
1212 if (c == '#' || IS_LINE_TERMINATOR (c))
1217 /* Collect the output symbol name. */
1219 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1227 buf = xrealloc (buf, bufsize);
1235 /* Eat white space at end of line. */
1236 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1241 if ((c == '\r' && (c = getc (file)) == '\n')
1242 || c == '\n' || c == EOF)
1245 /* Append the redefinition to the list. */
1247 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1258 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1260 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1261 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1264 /* Eat the rest of the line and finish it. */
1265 while (c != '\n' && c != EOF)
1271 fatal (_("%s:%d: premature end of file"), filename, lineno);
1276 /* Copy unkown object file IBFD onto OBFD.
1277 Returns TRUE upon success, FALSE otherwise. */
1280 copy_unknown_object (bfd *ibfd, bfd *obfd)
1288 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1290 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1297 non_fatal (_("stat returns negative size for `%s'"),
1298 bfd_get_archive_filename (ibfd));
1302 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1304 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1309 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1310 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1312 cbuf = xmalloc (BUFSIZE);
1314 while (ncopied < size)
1316 tocopy = size - ncopied;
1317 if (tocopy > BUFSIZE)
1320 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1321 != (bfd_size_type) tocopy)
1323 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1328 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1329 != (bfd_size_type) tocopy)
1331 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1339 chmod (bfd_get_filename (obfd), buf.st_mode);
1344 /* Copy object file IBFD onto OBFD.
1345 Returns TRUE upon success, FALSE otherwise. */
1348 copy_object (bfd *ibfd, bfd *obfd)
1352 asection **osections = NULL;
1353 asection *gnu_debuglink_section = NULL;
1354 bfd_size_type *gaps = NULL;
1355 bfd_size_type max_gap = 0;
1358 enum bfd_architecture iarch;
1361 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1362 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1363 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1364 fatal (_("Unable to change endianness of input file(s)"));
1366 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1368 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1373 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1374 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1375 bfd_get_filename (obfd), bfd_get_target (obfd));
1384 start = bfd_get_start_address (ibfd);
1385 start += change_start;
1388 /* Neither the start address nor the flags
1389 need to be set for a core file. */
1390 if (bfd_get_format (obfd) != bfd_core)
1394 flags = bfd_get_file_flags (ibfd);
1395 flags |= bfd_flags_to_set;
1396 flags &= ~bfd_flags_to_clear;
1397 flags &= bfd_applicable_file_flags (obfd);
1399 if (!bfd_set_start_address (obfd, start)
1400 || !bfd_set_file_flags (obfd, flags))
1402 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1407 /* Copy architecture of input file to output file. */
1408 iarch = bfd_get_arch (ibfd);
1409 imach = bfd_get_mach (ibfd);
1410 if (!bfd_set_arch_mach (obfd, iarch, imach)
1411 && (ibfd->target_defaulted
1412 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1414 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1415 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1416 bfd_get_archive_filename (ibfd));
1418 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1419 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1420 bfd_get_mach (ibfd)));
1424 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1426 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1433 if (osympp != isympp)
1439 symsize = bfd_get_symtab_upper_bound (ibfd);
1442 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1446 osympp = isympp = xmalloc (symsize);
1447 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1450 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1454 /* BFD mandates that all output sections be created and sizes set before
1455 any output is done. Thus, we traverse all sections multiple times. */
1456 bfd_map_over_sections (ibfd, setup_section, obfd);
1458 if (!extract_symbol)
1459 setup_bfd_headers (ibfd, obfd);
1461 if (add_sections != NULL)
1463 struct section_add *padd;
1464 struct section_list *pset;
1466 for (padd = add_sections; padd != NULL; padd = padd->next)
1470 pset = find_section_list (padd->name, FALSE);
1474 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1475 if (pset != NULL && pset->set_flags)
1476 flags = pset->flags | SEC_HAS_CONTENTS;
1478 /* bfd_make_section_with_flags() does not return very helpful
1479 error codes, so check for the most likely user error first. */
1480 if (bfd_get_section_by_name (obfd, padd->name))
1482 bfd_nonfatal_message (NULL, obfd, NULL,
1483 _("can't add section '%s'"), padd->name);
1488 padd->section = bfd_make_section_with_flags (obfd, padd->name, flags);
1489 if (padd->section == NULL)
1491 bfd_nonfatal_message (NULL, obfd, NULL,
1492 _("can't create section `%s'"),
1498 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1500 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1506 if (pset->change_vma != CHANGE_IGNORE)
1507 if (! bfd_set_section_vma (obfd, padd->section,
1510 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1514 if (pset->change_lma != CHANGE_IGNORE)
1516 padd->section->lma = pset->lma_val;
1518 if (! bfd_set_section_alignment
1519 (obfd, padd->section,
1520 bfd_section_alignment (obfd, padd->section)))
1522 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1530 if (gnu_debuglink_filename != NULL)
1532 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1533 (obfd, gnu_debuglink_filename);
1535 if (gnu_debuglink_section == NULL)
1537 bfd_nonfatal_message (NULL, obfd, NULL,
1538 _("cannot create debug link section `%s'"),
1539 gnu_debuglink_filename);
1543 /* Special processing for PE format files. We
1544 have no way to distinguish PE from COFF here. */
1545 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1547 bfd_vma debuglink_vma;
1548 asection * highest_section;
1551 /* The PE spec requires that all sections be adjacent and sorted
1552 in ascending order of VMA. It also specifies that debug
1553 sections should be last. This is despite the fact that debug
1554 sections are not loaded into memory and so in theory have no
1557 This means that the debuglink section must be given a non-zero
1558 VMA which makes it contiguous with other debug sections. So
1559 walk the current section list, find the section with the
1560 highest VMA and start the debuglink section after that one. */
1561 for (sec = obfd->sections, highest_section = NULL;
1565 && (highest_section == NULL
1566 || sec->vma > highest_section->vma))
1567 highest_section = sec;
1569 if (highest_section)
1570 debuglink_vma = BFD_ALIGN (highest_section->vma
1571 + highest_section->size,
1572 /* FIXME: We ought to be using
1573 COFF_PAGE_SIZE here or maybe
1574 bfd_get_section_alignment() (if it
1575 was set) but since this is for PE
1576 and we know the required alignment
1577 it is easier just to hard code it. */
1580 /* Umm, not sure what to do in this case. */
1581 debuglink_vma = 0x1000;
1583 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1587 if (bfd_count_sections (obfd) != 0
1588 && (gap_fill_set || pad_to_set))
1593 /* We must fill in gaps between the sections and/or we must pad
1594 the last section to a specified address. We do this by
1595 grabbing a list of the sections, sorting them by VMA, and
1596 increasing the section sizes as required to fill the gaps.
1597 We write out the gap contents below. */
1599 c = bfd_count_sections (obfd);
1600 osections = xmalloc (c * sizeof (asection *));
1602 bfd_map_over_sections (obfd, get_sections, &set);
1604 qsort (osections, c, sizeof (asection *), compare_section_lma);
1606 gaps = xmalloc (c * sizeof (bfd_size_type));
1607 memset (gaps, 0, c * sizeof (bfd_size_type));
1611 for (i = 0; i < c - 1; i++)
1615 bfd_vma gap_start, gap_stop;
1617 flags = bfd_get_section_flags (obfd, osections[i]);
1618 if ((flags & SEC_HAS_CONTENTS) == 0
1619 || (flags & SEC_LOAD) == 0)
1622 size = bfd_section_size (obfd, osections[i]);
1623 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1624 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1625 if (gap_start < gap_stop)
1627 if (! bfd_set_section_size (obfd, osections[i],
1628 size + (gap_stop - gap_start)))
1630 bfd_nonfatal_message (NULL, obfd, osections[i],
1631 _("Can't fill gap after section"));
1635 gaps[i] = gap_stop - gap_start;
1636 if (max_gap < gap_stop - gap_start)
1637 max_gap = gap_stop - gap_start;
1647 lma = bfd_section_lma (obfd, osections[c - 1]);
1648 size = bfd_section_size (obfd, osections[c - 1]);
1649 if (lma + size < pad_to)
1651 if (! bfd_set_section_size (obfd, osections[c - 1],
1654 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
1655 _("can't add padding"));
1660 gaps[c - 1] = pad_to - (lma + size);
1661 if (max_gap < pad_to - (lma + size))
1662 max_gap = pad_to - (lma + size);
1668 /* Symbol filtering must happen after the output sections
1669 have been created, but before their contents are set. */
1671 if (convert_debugging)
1672 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
1674 if (strip_symbols == STRIP_DEBUG
1675 || strip_symbols == STRIP_ALL
1676 || strip_symbols == STRIP_UNNEEDED
1677 || strip_symbols == STRIP_NONDEBUG
1678 || discard_locals != LOCALS_UNDEF
1680 || htab_elements (strip_specific_htab) != 0
1681 || htab_elements (keep_specific_htab) != 0
1682 || htab_elements (localize_specific_htab) != 0
1683 || htab_elements (globalize_specific_htab) != 0
1684 || htab_elements (keepglobal_specific_htab) != 0
1685 || htab_elements (weaken_specific_htab) != 0
1686 || prefix_symbols_string
1689 || convert_debugging
1690 || change_leading_char
1691 || remove_leading_char
1692 || redefine_sym_list
1695 /* Mark symbols used in output relocations so that they
1696 are kept, even if they are local labels or static symbols.
1698 Note we iterate over the input sections examining their
1699 relocations since the relocations for the output sections
1700 haven't been set yet. mark_symbols_used_in_relocations will
1701 ignore input sections which have no corresponding output
1703 if (strip_symbols != STRIP_ALL)
1704 bfd_map_over_sections (ibfd,
1705 mark_symbols_used_in_relocations,
1707 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1708 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1711 if (convert_debugging && dhandle != NULL)
1713 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1720 bfd_set_symtab (obfd, osympp, symcount);
1722 /* This has to happen after the symbol table has been set. */
1723 bfd_map_over_sections (ibfd, copy_section, obfd);
1725 if (add_sections != NULL)
1727 struct section_add *padd;
1729 for (padd = add_sections; padd != NULL; padd = padd->next)
1731 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1734 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1740 if (gnu_debuglink_filename != NULL)
1742 if (! bfd_fill_in_gnu_debuglink_section
1743 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1745 bfd_nonfatal_message (NULL, obfd, NULL,
1746 _("cannot fill debug link section `%s'"),
1747 gnu_debuglink_filename);
1752 if (gap_fill_set || pad_to_set)
1757 /* Fill in the gaps. */
1760 buf = xmalloc (max_gap);
1761 memset (buf, gap_fill, max_gap);
1763 c = bfd_count_sections (obfd);
1764 for (i = 0; i < c; i++)
1772 off = bfd_section_size (obfd, osections[i]) - left;
1783 if (! bfd_set_section_contents (obfd, osections[i], buf,
1786 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
1797 /* Do not copy backend data if --extract-symbol is passed; anything
1798 that needs to look at the section contents will fail. */
1802 /* Allow the BFD backend to copy any private data it understands
1803 from the input BFD to the output BFD. This is done last to
1804 permit the routine to look at the filtered symbol table, which is
1805 important for the ECOFF code at least. */
1806 if (! bfd_copy_private_bfd_data (ibfd, obfd))
1808 bfd_nonfatal_message (NULL, obfd, NULL,
1809 _("error copying private BFD data"));
1813 /* Switch to the alternate machine code. We have to do this at the
1814 very end, because we only initialize the header when we create
1815 the first section. */
1816 if (use_alt_mach_code != 0)
1818 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
1820 non_fatal (_("this target does not support %lu alternative machine codes"),
1822 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1824 non_fatal (_("treating that number as an absolute e_machine value instead"));
1825 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
1828 non_fatal (_("ignoring the alternative value"));
1835 /* Read each archive element in turn from IBFD, copy the
1836 contents to temp file, and keep the temp file handle.
1837 If 'force_output_target' is TRUE then make sure that
1838 all elements in the new archive are of the type
1842 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
1843 bfd_boolean force_output_target)
1847 struct name_list *next;
1851 bfd **ptr = &obfd->archive_head;
1854 const char *filename;
1856 /* Make a temp directory to hold the contents. */
1857 dir = make_tempdir (bfd_get_filename (obfd));
1859 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1862 obfd->has_armap = ibfd->has_armap;
1863 obfd->is_thin_archive = ibfd->is_thin_archive;
1867 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1869 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1872 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1876 while (!status && this_element != NULL)
1882 int stat_status = 0;
1883 bfd_boolean delete = TRUE;
1885 /* Create an output file for this member. */
1886 output_name = concat (dir, "/",
1887 bfd_get_filename (this_element), (char *) 0);
1889 /* If the file already exists, make another temp dir. */
1890 if (stat (output_name, &buf) >= 0)
1892 output_name = make_tempdir (output_name);
1893 if (output_name == NULL)
1894 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1897 l = xmalloc (sizeof (struct name_list));
1898 l->name = output_name;
1902 output_name = concat (output_name, "/",
1903 bfd_get_filename (this_element), (char *) 0);
1908 stat_status = bfd_stat_arch_elt (this_element, &buf);
1910 if (stat_status != 0)
1911 non_fatal (_("internal stat error on %s"),
1912 bfd_get_filename (this_element));
1915 l = xmalloc (sizeof (struct name_list));
1916 l->name = output_name;
1921 if (bfd_check_format (this_element, bfd_object))
1923 /* PR binutils/3110: Cope with archives
1924 containing multiple target types. */
1925 if (force_output_target)
1926 output_bfd = bfd_openw (output_name, output_target);
1928 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
1930 if (output_bfd == NULL)
1932 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
1937 delete = ! copy_object (this_element, output_bfd);
1940 || bfd_get_arch (this_element) != bfd_arch_unknown)
1942 if (!bfd_close (output_bfd))
1944 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
1945 /* Error in new object file. Don't change archive. */
1950 goto copy_unknown_element;
1954 bfd_nonfatal_message (NULL, this_element, NULL,
1955 _("Unable to recognise the format of file"));
1957 output_bfd = bfd_openw (output_name, output_target);
1958 copy_unknown_element:
1959 delete = !copy_unknown_object (this_element, output_bfd);
1960 if (!bfd_close_all_done (output_bfd))
1962 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
1963 /* Error in new object file. Don't change archive. */
1970 unlink (output_name);
1975 if (preserve_dates && stat_status == 0)
1976 set_times (output_name, &buf);
1978 /* Open the newly output file and attach to our list. */
1979 output_bfd = bfd_openr (output_name, output_target);
1981 l->obfd = output_bfd;
1984 ptr = &output_bfd->archive_next;
1986 last_element = this_element;
1988 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1990 bfd_close (last_element);
1995 filename = bfd_get_filename (obfd);
1996 if (!bfd_close (obfd))
1999 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2003 filename = bfd_get_filename (ibfd);
2004 if (!bfd_close (ibfd))
2007 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2011 /* Delete all the files that we opened. */
2012 for (l = list; l != NULL; l = l->next)
2014 if (l->obfd == NULL)
2018 bfd_close (l->obfd);
2025 /* The top-level control. */
2028 copy_file (const char *input_filename, const char *output_filename,
2029 const char *input_target, const char *output_target)
2032 char **obj_matching;
2033 char **core_matching;
2035 if (get_file_size (input_filename) < 1)
2041 /* To allow us to do "strip *" without dying on the first
2042 non-object file, failures are nonfatal. */
2043 ibfd = bfd_openr (input_filename, input_target);
2046 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2051 if (bfd_check_format (ibfd, bfd_archive))
2053 bfd_boolean force_output_target;
2056 /* bfd_get_target does not return the correct value until
2057 bfd_check_format succeeds. */
2058 if (output_target == NULL)
2060 output_target = bfd_get_target (ibfd);
2061 force_output_target = FALSE;
2064 force_output_target = TRUE;
2066 obfd = bfd_openw (output_filename, output_target);
2069 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2074 copy_archive (ibfd, obfd, output_target, force_output_target);
2076 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2081 /* bfd_get_target does not return the correct value until
2082 bfd_check_format succeeds. */
2083 if (output_target == NULL)
2084 output_target = bfd_get_target (ibfd);
2086 obfd = bfd_openw (output_filename, output_target);
2089 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2094 if (! copy_object (ibfd, obfd))
2097 if (!bfd_close (obfd))
2100 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2104 if (!bfd_close (ibfd))
2107 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2113 bfd_error_type obj_error = bfd_get_error ();
2114 bfd_error_type core_error;
2116 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2118 /* This probably can't happen.. */
2119 if (obj_error == bfd_error_file_ambiguously_recognized)
2120 free (obj_matching);
2124 core_error = bfd_get_error ();
2125 /* Report the object error in preference to the core error. */
2126 if (obj_error != core_error)
2127 bfd_set_error (obj_error);
2129 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2131 if (obj_error == bfd_error_file_ambiguously_recognized)
2133 list_matching_formats (obj_matching);
2134 free (obj_matching);
2136 if (core_error == bfd_error_file_ambiguously_recognized)
2138 list_matching_formats (core_matching);
2139 free (core_matching);
2146 /* Add a name to the section renaming list. */
2149 add_section_rename (const char * old_name, const char * new_name,
2152 section_rename * rename;
2154 /* Check for conflicts first. */
2155 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2156 if (strcmp (rename->old_name, old_name) == 0)
2158 /* Silently ignore duplicate definitions. */
2159 if (strcmp (rename->new_name, new_name) == 0
2160 && rename->flags == flags)
2163 fatal (_("Multiple renames of section %s"), old_name);
2166 rename = xmalloc (sizeof (* rename));
2168 rename->old_name = old_name;
2169 rename->new_name = new_name;
2170 rename->flags = flags;
2171 rename->next = section_rename_list;
2173 section_rename_list = rename;
2176 /* Check the section rename list for a new name of the input section
2177 ISECTION. Return the new name if one is found.
2178 Also set RETURNED_FLAGS to the flags to be used for this section. */
2181 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2182 flagword * returned_flags)
2184 const char * old_name = bfd_section_name (ibfd, isection);
2185 section_rename * rename;
2187 /* Default to using the flags of the input section. */
2188 * returned_flags = bfd_get_section_flags (ibfd, isection);
2190 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2191 if (strcmp (rename->old_name, old_name) == 0)
2193 if (rename->flags != (flagword) -1)
2194 * returned_flags = rename->flags;
2196 return rename->new_name;
2202 /* Once each of the sections is copied, we may still need to do some
2203 finalization work for private section headers. Do that here. */
2206 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2208 /* Allow the BFD backend to copy any private data it understands
2209 from the input section to the output section. */
2210 if (! bfd_copy_private_header_data (ibfd, obfd))
2213 bfd_nonfatal_message (NULL, ibfd, NULL,
2214 _("error in private header data"));
2218 /* All went well. */
2222 /* Create a section in OBFD with the same
2223 name and attributes as ISECTION in IBFD. */
2226 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2228 bfd *obfd = obfdarg;
2229 struct section_list *p;
2237 char *prefix = NULL;
2238 bfd_boolean make_nobits;
2240 if (is_strip_section (ibfd, isection))
2243 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2247 /* Get the, possibly new, name of the output section. */
2248 name = find_section_rename (ibfd, isection, & flags);
2250 /* Prefix sections. */
2251 if ((prefix_alloc_sections_string)
2252 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2253 prefix = prefix_alloc_sections_string;
2254 else if (prefix_sections_string)
2255 prefix = prefix_sections_string;
2261 n = xmalloc (strlen (prefix) + strlen (name) + 1);
2267 make_nobits = FALSE;
2268 if (p != NULL && p->set_flags)
2269 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2270 else if (strip_symbols == STRIP_NONDEBUG
2271 && (flags & SEC_ALLOC) != 0
2272 && (ibfd->xvec->flavour != bfd_target_elf_flavour
2273 || elf_section_type (isection) != SHT_NOTE))
2275 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2276 if (obfd->xvec->flavour == bfd_target_elf_flavour)
2280 /* Twiddle the input section flags so that it seems to
2281 elf.c:copy_private_bfd_data that section flags have not
2282 changed between input and output sections. This hack
2283 prevents wholesale rewriting of the program headers. */
2284 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2288 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2290 if (osection == NULL)
2292 err = _("failed to create output section");
2297 elf_section_type (osection) = SHT_NOBITS;
2299 size = bfd_section_size (ibfd, isection);
2301 size = (size + interleave - 1) / interleave;
2302 else if (extract_symbol)
2304 if (! bfd_set_section_size (obfd, osection, size))
2306 err = _("failed to set size");
2310 vma = bfd_section_vma (ibfd, isection);
2311 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2313 else if (p != NULL && p->change_vma == CHANGE_SET)
2316 vma += change_section_address;
2318 if (! bfd_set_section_vma (obfd, osection, vma))
2320 err = _("failed to set vma");
2324 lma = isection->lma;
2325 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2327 if (p->change_lma == CHANGE_MODIFY)
2329 else if (p->change_lma == CHANGE_SET)
2335 lma += change_section_address;
2337 osection->lma = lma;
2339 /* FIXME: This is probably not enough. If we change the LMA we
2340 may have to recompute the header for the file as well. */
2341 if (!bfd_set_section_alignment (obfd,
2343 bfd_section_alignment (ibfd, isection)))
2345 err = _("failed to set alignment");
2349 /* Copy merge entity size. */
2350 osection->entsize = isection->entsize;
2352 /* This used to be mangle_section; we do here to avoid using
2353 bfd_get_section_by_name since some formats allow multiple
2354 sections with the same name. */
2355 isection->output_section = osection;
2356 isection->output_offset = 0;
2358 /* Do not copy backend data if --extract-symbol is passed; anything
2359 that needs to look at the section contents will fail. */
2363 if ((isection->flags & SEC_GROUP) != 0)
2365 asymbol *gsym = group_signature (isection);
2369 gsym->flags |= BSF_KEEP;
2370 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
2371 elf_group_id (isection) = gsym;
2375 /* Allow the BFD backend to copy any private data it understands
2376 from the input section to the output section. */
2377 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2379 err = _("failed to copy private data");
2383 /* All went well. */
2388 bfd_nonfatal_message (NULL, obfd, osection, err);
2391 /* Copy the data of input section ISECTION of IBFD
2392 to an output section with the same name in OBFD.
2393 If stripping then don't copy any relocation info. */
2396 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2398 bfd *obfd = obfdarg;
2399 struct section_list *p;
2407 /* If we have already failed earlier on,
2408 do not keep on generating complaints now. */
2412 if (is_strip_section (ibfd, isection))
2415 flags = bfd_get_section_flags (ibfd, isection);
2416 if ((flags & SEC_GROUP) != 0)
2419 osection = isection->output_section;
2420 size = bfd_get_section_size (isection);
2422 if (size == 0 || osection == 0)
2428 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2430 /* Core files do not need to be relocated. */
2431 if (bfd_get_format (obfd) == bfd_core)
2435 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2439 /* Do not complain if the target does not support relocations. */
2440 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2445 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2452 bfd_set_reloc (obfd, osection, NULL, 0);
2455 relpp = xmalloc (relsize);
2456 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2460 bfd_nonfatal_message (NULL, ibfd, isection,
2461 _("relocation count is negative"));
2465 if (strip_symbols == STRIP_ALL)
2467 /* Remove relocations which are not in
2468 keep_strip_specific_list. */
2469 arelent **temp_relpp;
2470 long temp_relcount = 0;
2473 temp_relpp = xmalloc (relsize);
2474 for (i = 0; i < relcount; i++)
2475 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2476 keep_specific_htab))
2477 temp_relpp [temp_relcount++] = relpp [i];
2478 relcount = temp_relcount;
2483 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2488 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2489 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2491 void *memhunk = xmalloc (size);
2493 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2496 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2502 /* We don't handle leftover bytes (too many possible behaviors,
2503 and we don't know what the user wants). The section length
2504 must be a multiple of the number of bytes to swap. */
2505 if ((size % reverse_bytes) == 0)
2510 for (i = 0; i < size; i += reverse_bytes)
2511 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
2513 bfd_byte *m = (bfd_byte *) memhunk;
2516 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
2517 m[(i + reverse_bytes) - (j + 1)] = b;
2521 /* User must pad the section up in order to do this. */
2522 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2523 bfd_section_name (ibfd, isection), reverse_bytes);
2528 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2529 char *from = (char *) memhunk + copy_byte;
2531 char *end = (char *) memhunk + size;
2533 for (; from < end; from += interleave)
2536 size = (size + interleave - 1 - copy_byte) / interleave;
2537 osection->lma /= interleave;
2540 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2543 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2548 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2550 void *memhunk = xmalloc (size);
2552 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2553 flag--they can just remove the section entirely and add it
2554 back again. However, we do permit them to turn on the
2555 SEC_HAS_CONTENTS flag, and take it to mean that the section
2556 contents should be zeroed out. */
2558 memset (memhunk, 0, size);
2559 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2562 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2569 /* Get all the sections. This is used when --gap-fill or --pad-to is
2573 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2575 asection ***secppp = secppparg;
2577 **secppp = osection;
2581 /* Sort sections by VMA. This is called via qsort, and is used when
2582 --gap-fill or --pad-to is used. We force non loadable or empty
2583 sections to the front, where they are easier to ignore. */
2586 compare_section_lma (const void *arg1, const void *arg2)
2588 const asection *const *sec1 = arg1;
2589 const asection *const *sec2 = arg2;
2590 flagword flags1, flags2;
2592 /* Sort non loadable sections to the front. */
2593 flags1 = (*sec1)->flags;
2594 flags2 = (*sec2)->flags;
2595 if ((flags1 & SEC_HAS_CONTENTS) == 0
2596 || (flags1 & SEC_LOAD) == 0)
2598 if ((flags2 & SEC_HAS_CONTENTS) != 0
2599 && (flags2 & SEC_LOAD) != 0)
2604 if ((flags2 & SEC_HAS_CONTENTS) == 0
2605 || (flags2 & SEC_LOAD) == 0)
2609 /* Sort sections by LMA. */
2610 if ((*sec1)->lma > (*sec2)->lma)
2612 else if ((*sec1)->lma < (*sec2)->lma)
2615 /* Sort sections with the same LMA by size. */
2616 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2618 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2624 /* Mark all the symbols which will be used in output relocations with
2625 the BSF_KEEP flag so that those symbols will not be stripped.
2627 Ignore relocations which will not appear in the output file. */
2630 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2632 asymbol **symbols = symbolsarg;
2637 /* Ignore an input section with no corresponding output section. */
2638 if (isection->output_section == NULL)
2641 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2644 /* Do not complain if the target does not support relocations. */
2645 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2647 bfd_fatal (bfd_get_filename (ibfd));
2653 relpp = xmalloc (relsize);
2654 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2656 bfd_fatal (bfd_get_filename (ibfd));
2658 /* Examine each symbol used in a relocation. If it's not one of the
2659 special bfd section symbols, then mark it with BSF_KEEP. */
2660 for (i = 0; i < relcount; i++)
2662 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2663 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2664 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2665 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2672 /* Write out debugging information. */
2675 write_debugging_info (bfd *obfd, void *dhandle,
2676 long *symcountp ATTRIBUTE_UNUSED,
2677 asymbol ***symppp ATTRIBUTE_UNUSED)
2679 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2680 return write_ieee_debugging_info (obfd, dhandle);
2682 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2683 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2685 bfd_byte *syms, *strings;
2686 bfd_size_type symsize, stringsize;
2687 asection *stabsec, *stabstrsec;
2690 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2695 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2696 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2697 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2699 || stabstrsec == NULL
2700 || ! bfd_set_section_size (obfd, stabsec, symsize)
2701 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2702 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2703 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2705 bfd_nonfatal_message (NULL, obfd, NULL,
2706 _("can't create debugging section"));
2710 /* We can get away with setting the section contents now because
2711 the next thing the caller is going to do is copy over the
2712 real sections. We may someday have to split the contents
2713 setting out of this function. */
2714 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2715 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2718 bfd_nonfatal_message (NULL, obfd, NULL,
2719 _("can't set debugging section contents"));
2726 bfd_nonfatal_message (NULL, obfd, NULL,
2727 _("don't know how to write debugging information for %s"),
2728 bfd_get_target (obfd));
2733 strip_main (int argc, char *argv[])
2735 char *input_target = NULL;
2736 char *output_target = NULL;
2737 bfd_boolean show_version = FALSE;
2738 bfd_boolean formats_info = FALSE;
2741 struct section_list *p;
2742 char *output_file = NULL;
2744 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2745 strip_options, (int *) 0)) != EOF)
2750 input_target = optarg;
2753 output_target = optarg;
2756 input_target = output_target = optarg;
2759 p = find_section_list (optarg, TRUE);
2761 sections_removed = TRUE;
2764 strip_symbols = STRIP_ALL;
2768 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2769 strip_symbols = STRIP_DEBUG;
2771 case OPTION_STRIP_UNNEEDED:
2772 strip_symbols = STRIP_UNNEEDED;
2775 add_specific_symbol (optarg, keep_specific_htab);
2778 add_specific_symbol (optarg, strip_specific_htab);
2781 output_file = optarg;
2784 preserve_dates = TRUE;
2787 discard_locals = LOCALS_ALL;
2790 discard_locals = LOCALS_START_L;
2796 show_version = TRUE;
2798 case OPTION_FORMATS_INFO:
2799 formats_info = TRUE;
2801 case OPTION_ONLY_KEEP_DEBUG:
2802 strip_symbols = STRIP_NONDEBUG;
2804 case OPTION_KEEP_FILE_SYMBOLS:
2805 keep_file_symbols = 1;
2808 /* We've been given a long option. */
2815 strip_usage (stdout, 0);
2817 strip_usage (stderr, 1);
2828 print_version ("strip");
2830 /* Default is to strip all symbols. */
2831 if (strip_symbols == STRIP_UNDEF
2832 && discard_locals == LOCALS_UNDEF
2833 && htab_elements (strip_specific_htab) == 0)
2834 strip_symbols = STRIP_ALL;
2836 if (output_target == NULL)
2837 output_target = input_target;
2841 || (output_file != NULL && (i + 1) < argc))
2842 strip_usage (stderr, 1);
2844 for (; i < argc; i++)
2846 int hold_status = status;
2847 struct stat statbuf;
2850 if (get_file_size (argv[i]) < 1)
2857 /* No need to check the return value of stat().
2858 It has already been checked in get_file_size(). */
2859 stat (argv[i], &statbuf);
2861 if (output_file == NULL || strcmp (argv[i], output_file) == 0)
2862 tmpname = make_tempname (argv[i]);
2864 tmpname = output_file;
2866 if (tmpname == NULL)
2868 bfd_nonfatal_message (argv[i], NULL, NULL,
2869 _("could not create temporary file to hold stripped copy"));
2875 copy_file (argv[i], tmpname, input_target, output_target);
2879 set_times (tmpname, &statbuf);
2880 if (output_file != tmpname)
2881 smart_rename (tmpname, output_file ? output_file : argv[i],
2883 status = hold_status;
2886 unlink_if_ordinary (tmpname);
2887 if (output_file != tmpname)
2895 copy_main (int argc, char *argv[])
2897 char * binary_architecture = NULL;
2898 char *input_filename = NULL;
2899 char *output_filename = NULL;
2901 char *input_target = NULL;
2902 char *output_target = NULL;
2903 bfd_boolean show_version = FALSE;
2904 bfd_boolean change_warn = TRUE;
2905 bfd_boolean formats_info = FALSE;
2907 struct section_list *p;
2908 struct stat statbuf;
2910 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2911 copy_options, (int *) 0)) != EOF)
2916 copy_byte = atoi (optarg);
2918 fatal (_("byte number must be non-negative"));
2922 binary_architecture = optarg;
2926 interleave = atoi (optarg);
2928 fatal (_("interleave must be positive"));
2932 case 's': /* "source" - 'I' is preferred */
2933 input_target = optarg;
2937 case 'd': /* "destination" - 'O' is preferred */
2938 output_target = optarg;
2942 input_target = output_target = optarg;
2946 p = find_section_list (optarg, TRUE);
2948 fatal (_("%s both copied and removed"), optarg);
2950 sections_copied = TRUE;
2954 p = find_section_list (optarg, TRUE);
2956 fatal (_("%s both copied and removed"), optarg);
2958 sections_removed = TRUE;
2962 strip_symbols = STRIP_ALL;
2966 strip_symbols = STRIP_DEBUG;
2969 case OPTION_STRIP_UNNEEDED:
2970 strip_symbols = STRIP_UNNEEDED;
2973 case OPTION_ONLY_KEEP_DEBUG:
2974 strip_symbols = STRIP_NONDEBUG;
2977 case OPTION_KEEP_FILE_SYMBOLS:
2978 keep_file_symbols = 1;
2981 case OPTION_ADD_GNU_DEBUGLINK:
2982 gnu_debuglink_filename = optarg;
2986 add_specific_symbol (optarg, keep_specific_htab);
2990 add_specific_symbol (optarg, strip_specific_htab);
2993 case OPTION_STRIP_UNNEEDED_SYMBOL:
2994 add_specific_symbol (optarg, strip_unneeded_htab);
2998 add_specific_symbol (optarg, localize_specific_htab);
3001 case OPTION_GLOBALIZE_SYMBOL:
3002 add_specific_symbol (optarg, globalize_specific_htab);
3006 add_specific_symbol (optarg, keepglobal_specific_htab);
3010 add_specific_symbol (optarg, weaken_specific_htab);
3014 preserve_dates = TRUE;
3022 discard_locals = LOCALS_ALL;
3026 discard_locals = LOCALS_START_L;
3034 show_version = TRUE;
3037 case OPTION_FORMATS_INFO:
3038 formats_info = TRUE;
3045 case OPTION_ADD_SECTION:
3049 struct section_add *pa;
3054 s = strchr (optarg, '=');
3057 fatal (_("bad format for %s"), "--add-section");
3059 size = get_file_size (s + 1);
3066 pa = xmalloc (sizeof (struct section_add));
3069 name = xmalloc (len + 1);
3070 strncpy (name, optarg, len);
3074 pa->filename = s + 1;
3076 pa->contents = xmalloc (size);
3078 f = fopen (pa->filename, FOPEN_RB);
3081 fatal (_("cannot open: %s: %s"),
3082 pa->filename, strerror (errno));
3084 if (fread (pa->contents, 1, pa->size, f) == 0
3086 fatal (_("%s: fread failed"), pa->filename);
3090 pa->next = add_sections;
3095 case OPTION_CHANGE_START:
3096 change_start = parse_vma (optarg, "--change-start");
3099 case OPTION_CHANGE_SECTION_ADDRESS:
3100 case OPTION_CHANGE_SECTION_LMA:
3101 case OPTION_CHANGE_SECTION_VMA:
3106 char *option = NULL;
3108 enum change_action what = CHANGE_IGNORE;
3112 case OPTION_CHANGE_SECTION_ADDRESS:
3113 option = "--change-section-address";
3115 case OPTION_CHANGE_SECTION_LMA:
3116 option = "--change-section-lma";
3118 case OPTION_CHANGE_SECTION_VMA:
3119 option = "--change-section-vma";
3123 s = strchr (optarg, '=');
3126 s = strchr (optarg, '+');
3129 s = strchr (optarg, '-');
3131 fatal (_("bad format for %s"), option);
3136 name = xmalloc (len + 1);
3137 strncpy (name, optarg, len);
3140 p = find_section_list (name, TRUE);
3142 val = parse_vma (s + 1, option);
3146 case '=': what = CHANGE_SET; break;
3147 case '-': val = - val; /* Drop through. */
3148 case '+': what = CHANGE_MODIFY; break;
3153 case OPTION_CHANGE_SECTION_ADDRESS:
3154 p->change_vma = what;
3158 case OPTION_CHANGE_SECTION_LMA:
3159 p->change_lma = what;
3163 case OPTION_CHANGE_SECTION_VMA:
3164 p->change_vma = what;
3171 case OPTION_CHANGE_ADDRESSES:
3172 change_section_address = parse_vma (optarg, "--change-addresses");
3173 change_start = change_section_address;
3176 case OPTION_CHANGE_WARNINGS:
3180 case OPTION_CHANGE_LEADING_CHAR:
3181 change_leading_char = TRUE;
3184 case OPTION_DEBUGGING:
3185 convert_debugging = TRUE;
3188 case OPTION_GAP_FILL:
3190 bfd_vma gap_fill_vma;
3192 gap_fill_vma = parse_vma (optarg, "--gap-fill");
3193 gap_fill = (bfd_byte) gap_fill_vma;
3194 if ((bfd_vma) gap_fill != gap_fill_vma)
3198 sprintf_vma (buff, gap_fill_vma);
3200 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3203 gap_fill_set = TRUE;
3207 case OPTION_NO_CHANGE_WARNINGS:
3208 change_warn = FALSE;
3212 pad_to = parse_vma (optarg, "--pad-to");
3216 case OPTION_REMOVE_LEADING_CHAR:
3217 remove_leading_char = TRUE;
3220 case OPTION_REDEFINE_SYM:
3222 /* Push this redefinition onto redefine_symbol_list. */
3226 const char *nextarg;
3227 char *source, *target;
3229 s = strchr (optarg, '=');
3231 fatal (_("bad format for %s"), "--redefine-sym");
3234 source = xmalloc (len + 1);
3235 strncpy (source, optarg, len);
3239 len = strlen (nextarg);
3240 target = xmalloc (len + 1);
3241 strcpy (target, nextarg);
3243 redefine_list_append ("--redefine-sym", source, target);
3250 case OPTION_REDEFINE_SYMS:
3251 add_redefine_syms_file (optarg);
3254 case OPTION_SET_SECTION_FLAGS:
3260 s = strchr (optarg, '=');
3262 fatal (_("bad format for %s"), "--set-section-flags");
3265 name = xmalloc (len + 1);
3266 strncpy (name, optarg, len);
3269 p = find_section_list (name, TRUE);
3271 p->set_flags = TRUE;
3272 p->flags = parse_flags (s + 1);
3276 case OPTION_RENAME_SECTION:
3279 const char *eq, *fl;
3284 eq = strchr (optarg, '=');
3286 fatal (_("bad format for %s"), "--rename-section");
3290 fatal (_("bad format for %s"), "--rename-section");
3292 old_name = xmalloc (len + 1);
3293 strncpy (old_name, optarg, len);
3297 fl = strchr (eq, ',');
3300 flags = parse_flags (fl + 1);
3310 fatal (_("bad format for %s"), "--rename-section");
3312 new_name = xmalloc (len + 1);
3313 strncpy (new_name, eq, len);
3316 add_section_rename (old_name, new_name, flags);
3320 case OPTION_SET_START:
3321 set_start = parse_vma (optarg, "--set-start");
3322 set_start_set = TRUE;
3325 case OPTION_SREC_LEN:
3326 Chunk = parse_vma (optarg, "--srec-len");
3329 case OPTION_SREC_FORCES3:
3333 case OPTION_STRIP_SYMBOLS:
3334 add_specific_symbols (optarg, strip_specific_htab);
3337 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3338 add_specific_symbols (optarg, strip_unneeded_htab);
3341 case OPTION_KEEP_SYMBOLS:
3342 add_specific_symbols (optarg, keep_specific_htab);
3345 case OPTION_LOCALIZE_HIDDEN:
3346 localize_hidden = TRUE;
3349 case OPTION_LOCALIZE_SYMBOLS:
3350 add_specific_symbols (optarg, localize_specific_htab);
3353 case OPTION_GLOBALIZE_SYMBOLS:
3354 add_specific_symbols (optarg, globalize_specific_htab);
3357 case OPTION_KEEPGLOBAL_SYMBOLS:
3358 add_specific_symbols (optarg, keepglobal_specific_htab);
3361 case OPTION_WEAKEN_SYMBOLS:
3362 add_specific_symbols (optarg, weaken_specific_htab);
3365 case OPTION_ALT_MACH_CODE:
3366 use_alt_mach_code = strtoul (optarg, NULL, 0);
3367 if (use_alt_mach_code == 0)
3368 fatal (_("unable to parse alternative machine code"));
3371 case OPTION_PREFIX_SYMBOLS:
3372 prefix_symbols_string = optarg;
3375 case OPTION_PREFIX_SECTIONS:
3376 prefix_sections_string = optarg;
3379 case OPTION_PREFIX_ALLOC_SECTIONS:
3380 prefix_alloc_sections_string = optarg;
3383 case OPTION_READONLY_TEXT:
3384 bfd_flags_to_set |= WP_TEXT;
3385 bfd_flags_to_clear &= ~WP_TEXT;
3388 case OPTION_WRITABLE_TEXT:
3389 bfd_flags_to_clear |= WP_TEXT;
3390 bfd_flags_to_set &= ~WP_TEXT;
3394 bfd_flags_to_set |= D_PAGED;
3395 bfd_flags_to_clear &= ~D_PAGED;
3399 bfd_flags_to_clear |= D_PAGED;
3400 bfd_flags_to_set &= ~D_PAGED;
3403 case OPTION_EXTRACT_SYMBOL:
3404 extract_symbol = TRUE;
3407 case OPTION_REVERSE_BYTES:
3409 int prev = reverse_bytes;
3411 reverse_bytes = atoi (optarg);
3412 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
3413 fatal (_("number of bytes to reverse must be positive and even"));
3415 if (prev && prev != reverse_bytes)
3416 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3422 /* We've been given a long option. */
3427 copy_usage (stdout, 0);
3430 copy_usage (stderr, 1);
3441 print_version ("objcopy");
3443 if (copy_byte >= interleave)
3444 fatal (_("byte number must be less than interleave"));
3446 if (optind == argc || optind + 2 < argc)
3447 copy_usage (stderr, 1);
3449 input_filename = argv[optind];
3450 if (optind + 1 < argc)
3451 output_filename = argv[optind + 1];
3453 /* Default is to strip no symbols. */
3454 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3455 strip_symbols = STRIP_NONE;
3457 if (output_target == NULL)
3458 output_target = input_target;
3460 if (binary_architecture != NULL)
3462 if (input_target && strcmp (input_target, "binary") == 0)
3464 const bfd_arch_info_type * temp_arch_info;
3466 temp_arch_info = bfd_scan_arch (binary_architecture);
3468 if (temp_arch_info != NULL)
3470 bfd_external_binary_architecture = temp_arch_info->arch;
3471 bfd_external_machine = temp_arch_info->mach;
3474 fatal (_("architecture %s unknown"), binary_architecture);
3478 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3479 non_fatal (_(" Argument %s ignored"), binary_architecture);
3484 if (stat (input_filename, & statbuf) < 0)
3485 fatal (_("warning: could not locate '%s'. System error message: %s"),
3486 input_filename, strerror (errno));
3488 /* If there is no destination file, or the source and destination files
3489 are the same, then create a temp and rename the result into the input. */
3490 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3491 tmpname = make_tempname (input_filename);
3493 tmpname = output_filename;
3495 if (tmpname == NULL)
3496 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3497 input_filename, strerror (errno));
3499 copy_file (input_filename, tmpname, input_target, output_target);
3503 set_times (tmpname, &statbuf);
3504 if (tmpname != output_filename)
3505 smart_rename (tmpname, input_filename, preserve_dates);
3508 unlink_if_ordinary (tmpname);
3512 for (p = change_sections; p != NULL; p = p->next)
3516 if (p->change_vma != CHANGE_IGNORE)
3520 sprintf_vma (buff, p->vma_val);
3522 /* xgettext:c-format */
3523 non_fatal (_("%s %s%c0x%s never used"),
3524 "--change-section-vma",
3526 p->change_vma == CHANGE_SET ? '=' : '+',
3530 if (p->change_lma != CHANGE_IGNORE)
3534 sprintf_vma (buff, p->lma_val);
3536 /* xgettext:c-format */
3537 non_fatal (_("%s %s%c0x%s never used"),
3538 "--change-section-lma",
3540 p->change_lma == CHANGE_SET ? '=' : '+',
3551 main (int argc, char *argv[])
3553 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3554 setlocale (LC_MESSAGES, "");
3556 #if defined (HAVE_SETLOCALE)
3557 setlocale (LC_CTYPE, "");
3559 bindtextdomain (PACKAGE, LOCALEDIR);
3560 textdomain (PACKAGE);
3562 program_name = argv[0];
3563 xmalloc_set_program_name (program_name);
3565 START_PROGRESS (program_name, 0);
3567 expandargv (&argc, &argv);
3569 strip_symbols = STRIP_UNDEF;
3570 discard_locals = LOCALS_UNDEF;
3573 set_default_bfd_target ();
3577 int i = strlen (program_name);
3578 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3579 /* Drop the .exe suffix, if any. */
3580 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3583 program_name[i] = '\0';
3586 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3589 create_symbol_htabs ();
3592 strip_main (argc, argv);
3594 copy_main (argc, argv);
3596 END_PROGRESS (program_name);