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
)
821 /* Return true if SYM is a hidden symbol. */
824 is_hidden_symbol (asymbol
*sym
)
826 elf_symbol_type
*elf_sym
;
828 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
830 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
839 /* Choose which symbol entries to copy; put the result in OSYMS.
840 We don't copy in place, because that confuses the relocs.
841 Return the number of symbols to print. */
844 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
845 asymbol
**isyms
, long symcount
)
847 asymbol
**from
= isyms
, **to
= osyms
;
848 long src_count
= 0, dst_count
= 0;
849 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
852 for (; src_count
< symcount
; src_count
++)
854 asymbol
*sym
= from
[src_count
];
855 flagword flags
= sym
->flags
;
856 char *name
= (char *) bfd_asymbol_name (sym
);
858 bfd_boolean undefined
;
859 bfd_boolean rem_leading_char
;
860 bfd_boolean add_leading_char
;
862 undefined
= bfd_is_und_section (bfd_get_section (sym
));
864 if (redefine_sym_list
)
866 char *old_name
, *new_name
;
868 old_name
= (char *) bfd_asymbol_name (sym
);
869 new_name
= (char *) lookup_sym_redefinition (old_name
);
870 bfd_asymbol_name (sym
) = new_name
;
874 /* Check if we will remove the current leading character. */
876 (name
[0] == bfd_get_symbol_leading_char (abfd
))
877 && (change_leading_char
878 || (remove_leading_char
879 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
881 || bfd_is_com_section (bfd_get_section (sym
)))));
883 /* Check if we will add a new leading character. */
886 && (bfd_get_symbol_leading_char (obfd
) != '\0')
887 && (bfd_get_symbol_leading_char (abfd
) == '\0'
888 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
890 /* Short circuit for change_leading_char if we can do it in-place. */
891 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
893 name
[0] = bfd_get_symbol_leading_char (obfd
);
894 bfd_asymbol_name (sym
) = name
;
895 rem_leading_char
= FALSE
;
896 add_leading_char
= FALSE
;
899 /* Remove leading char. */
900 if (rem_leading_char
)
901 bfd_asymbol_name (sym
) = ++name
;
903 /* Add new leading char and/or prefix. */
904 if (add_leading_char
|| prefix_symbols_string
)
908 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
909 + strlen (name
) + 1);
910 if (add_leading_char
)
911 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
913 if (prefix_symbols_string
)
915 strcpy (ptr
, prefix_symbols_string
);
916 ptr
+= strlen (prefix_symbols_string
);
920 bfd_asymbol_name (sym
) = n
;
924 if (strip_symbols
== STRIP_ALL
)
926 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
927 || ((flags
& BSF_SECTION_SYM
) != 0
928 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
931 else if (relocatable
/* Relocatable file. */
932 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
934 else if (bfd_decode_symclass (sym
) == 'I')
935 /* Global symbols in $idata sections need to be retained
936 even if relocatable is FALSE. External users of the
937 library containing the $idata section may reference these
940 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
941 || (flags
& BSF_WEAK
) != 0
943 || bfd_is_com_section (bfd_get_section (sym
)))
944 keep
= strip_symbols
!= STRIP_UNNEEDED
;
945 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
946 keep
= (strip_symbols
!= STRIP_DEBUG
947 && strip_symbols
!= STRIP_UNNEEDED
948 && ! convert_debugging
);
949 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
950 /* COMDAT sections store special information in local
951 symbols, so we cannot risk stripping any of them. */
953 else /* Local symbol. */
954 keep
= (strip_symbols
!= STRIP_UNNEEDED
955 && (discard_locals
!= LOCALS_ALL
956 && (discard_locals
!= LOCALS_START_L
957 || ! bfd_is_local_label (abfd
, sym
))));
959 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
962 && !(flags
& BSF_KEEP
)
963 && is_specified_symbol (name
, strip_unneeded_list
))
966 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
967 || is_specified_symbol (name
, keep_specific_list
)))
969 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
974 if ((flags
& BSF_GLOBAL
) != 0
975 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
977 sym
->flags
&= ~ BSF_GLOBAL
;
978 sym
->flags
|= BSF_WEAK
;
982 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
983 && (is_specified_symbol (name
, localize_specific_list
)
984 || (keepglobal_specific_list
!= NULL
985 && ! is_specified_symbol (name
, keepglobal_specific_list
))
986 || (localize_hidden
&& is_hidden_symbol (sym
))))
988 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
989 sym
->flags
|= BSF_LOCAL
;
993 && (flags
& BSF_LOCAL
)
994 && is_specified_symbol (name
, globalize_specific_list
))
996 sym
->flags
&= ~ BSF_LOCAL
;
997 sym
->flags
|= BSF_GLOBAL
;
1000 to
[dst_count
++] = sym
;
1004 to
[dst_count
] = NULL
;
1009 /* Find the redefined name of symbol SOURCE. */
1012 lookup_sym_redefinition (const char *source
)
1014 struct redefine_node
*list
;
1016 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1017 if (strcmp (source
, list
->source
) == 0)
1018 return list
->target
;
1023 /* Add a node to a symbol redefine list. */
1026 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1028 struct redefine_node
**p
;
1029 struct redefine_node
*list
;
1030 struct redefine_node
*new_node
;
1032 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1034 if (strcmp (source
, list
->source
) == 0)
1035 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1038 if (strcmp (target
, list
->target
) == 0)
1039 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1043 new_node
= xmalloc (sizeof (struct redefine_node
));
1045 new_node
->source
= strdup (source
);
1046 new_node
->target
= strdup (target
);
1047 new_node
->next
= NULL
;
1052 /* Handle the --redefine-syms option. Read lines containing "old new"
1053 from the file, and add them to the symbol redefine list. */
1056 add_redefine_syms_file (const char *filename
)
1065 file
= fopen (filename
, "r");
1067 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1068 filename
, strerror (errno
));
1071 buf
= xmalloc (bufsize
);
1079 /* Collect the input symbol name. */
1080 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1088 buf
= xrealloc (buf
, bufsize
);
1096 /* Eat white space between the symbol names. */
1097 while (IS_WHITESPACE (c
))
1099 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1104 /* Collect the output symbol name. */
1106 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1114 buf
= xrealloc (buf
, bufsize
);
1122 /* Eat white space at end of line. */
1123 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1128 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1129 || c
== '\n' || c
== EOF
)
1132 /* Append the redefinition to the list. */
1134 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1145 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1147 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1148 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1151 /* Eat the rest of the line and finish it. */
1152 while (c
!= '\n' && c
!= EOF
)
1158 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1163 /* Copy unkown object file IBFD onto OBFD.
1164 Returns TRUE upon success, FALSE otherwise. */
1167 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1175 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1177 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1184 non_fatal (_("stat returns negative size for `%s'"),
1185 bfd_get_archive_filename (ibfd
));
1189 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1191 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1196 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1197 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1199 cbuf
= xmalloc (BUFSIZE
);
1201 while (ncopied
< size
)
1203 tocopy
= size
- ncopied
;
1204 if (tocopy
> BUFSIZE
)
1207 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1208 != (bfd_size_type
) tocopy
)
1210 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1215 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1216 != (bfd_size_type
) tocopy
)
1218 bfd_nonfatal (bfd_get_filename (obfd
));
1226 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1231 /* Copy object file IBFD onto OBFD.
1232 Returns TRUE upon success, FALSE otherwise. */
1235 copy_object (bfd
*ibfd
, bfd
*obfd
)
1239 asection
**osections
= NULL
;
1240 asection
*gnu_debuglink_section
= NULL
;
1241 bfd_size_type
*gaps
= NULL
;
1242 bfd_size_type max_gap
= 0;
1245 enum bfd_architecture iarch
;
1248 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1249 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1250 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1251 fatal (_("Unable to change endianness of input file(s)"));
1253 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1255 bfd_nonfatal (bfd_get_filename (obfd
));
1260 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1261 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1262 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1267 start
= bfd_get_start_address (ibfd
);
1268 start
+= change_start
;
1270 /* Neither the start address nor the flags
1271 need to be set for a core file. */
1272 if (bfd_get_format (obfd
) != bfd_core
)
1276 flags
= bfd_get_file_flags (ibfd
);
1277 flags
|= bfd_flags_to_set
;
1278 flags
&= ~bfd_flags_to_clear
;
1279 flags
&= bfd_applicable_file_flags (obfd
);
1281 if (!bfd_set_start_address (obfd
, start
)
1282 || !bfd_set_file_flags (obfd
, flags
))
1284 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1289 /* Copy architecture of input file to output file. */
1290 iarch
= bfd_get_arch (ibfd
);
1291 imach
= bfd_get_mach (ibfd
);
1292 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1293 && (ibfd
->target_defaulted
1294 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1296 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1297 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1298 bfd_get_archive_filename (ibfd
));
1300 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1301 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1302 bfd_get_mach (ibfd
)));
1306 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1308 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1315 if (osympp
!= isympp
)
1321 /* BFD mandates that all output sections be created and sizes set before
1322 any output is done. Thus, we traverse all sections multiple times. */
1323 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1325 setup_bfd_headers (ibfd
, obfd
);
1327 if (add_sections
!= NULL
)
1329 struct section_add
*padd
;
1330 struct section_list
*pset
;
1332 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1336 pset
= find_section_list (padd
->name
, FALSE
);
1340 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1341 if (pset
!= NULL
&& pset
->set_flags
)
1342 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1344 /* bfd_make_section_with_flags() does not return very helpful
1345 error codes, so check for the most likely user error first. */
1346 if (bfd_get_section_by_name (obfd
, padd
->name
))
1348 non_fatal (_("can't add section '%s' - it already exists!"), padd
->name
);
1353 padd
->section
= bfd_make_section_with_flags (obfd
, padd
->name
, flags
);
1354 if (padd
->section
== NULL
)
1356 non_fatal (_("can't create section `%s': %s"),
1357 padd
->name
, bfd_errmsg (bfd_get_error ()));
1362 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1364 bfd_nonfatal (bfd_get_filename (obfd
));
1370 if (pset
->change_vma
!= CHANGE_IGNORE
)
1371 if (! bfd_set_section_vma (obfd
, padd
->section
,
1374 bfd_nonfatal (bfd_get_filename (obfd
));
1378 if (pset
->change_lma
!= CHANGE_IGNORE
)
1380 padd
->section
->lma
= pset
->lma_val
;
1382 if (! bfd_set_section_alignment
1383 (obfd
, padd
->section
,
1384 bfd_section_alignment (obfd
, padd
->section
)))
1386 bfd_nonfatal (bfd_get_filename (obfd
));
1394 if (gnu_debuglink_filename
!= NULL
)
1396 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1397 (obfd
, gnu_debuglink_filename
);
1399 if (gnu_debuglink_section
== NULL
)
1401 bfd_nonfatal (gnu_debuglink_filename
);
1405 /* Special processing for PE format files. We
1406 have no way to distinguish PE from COFF here. */
1407 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1409 bfd_vma debuglink_vma
;
1410 asection
* highest_section
;
1413 /* The PE spec requires that all sections be adjacent and sorted
1414 in ascending order of VMA. It also specifies that debug
1415 sections should be last. This is despite the fact that debug
1416 sections are not loaded into memory and so in theory have no
1419 This means that the debuglink section must be given a non-zero
1420 VMA which makes it contiguous with other debug sections. So
1421 walk the current section list, find the section with the
1422 highest VMA and start the debuglink section after that one. */
1423 for (sec
= obfd
->sections
, highest_section
= NULL
;
1427 && (highest_section
== NULL
1428 || sec
->vma
> highest_section
->vma
))
1429 highest_section
= sec
;
1431 if (highest_section
)
1432 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1433 + highest_section
->size
,
1434 /* FIXME: We ought to be using
1435 COFF_PAGE_SIZE here or maybe
1436 bfd_get_section_alignment() (if it
1437 was set) but since this is for PE
1438 and we know the required alignment
1439 it is easier just to hard code it. */
1442 /* Umm, not sure what to do in this case. */
1443 debuglink_vma
= 0x1000;
1445 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1449 if (bfd_count_sections (obfd
) == 0)
1451 non_fatal (_("there are no sections to be copied!"));
1455 if (gap_fill_set
|| pad_to_set
)
1460 /* We must fill in gaps between the sections and/or we must pad
1461 the last section to a specified address. We do this by
1462 grabbing a list of the sections, sorting them by VMA, and
1463 increasing the section sizes as required to fill the gaps.
1464 We write out the gap contents below. */
1466 c
= bfd_count_sections (obfd
);
1467 osections
= xmalloc (c
* sizeof (asection
*));
1469 bfd_map_over_sections (obfd
, get_sections
, &set
);
1471 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1473 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1474 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1478 for (i
= 0; i
< c
- 1; i
++)
1482 bfd_vma gap_start
, gap_stop
;
1484 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1485 if ((flags
& SEC_HAS_CONTENTS
) == 0
1486 || (flags
& SEC_LOAD
) == 0)
1489 size
= bfd_section_size (obfd
, osections
[i
]);
1490 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1491 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1492 if (gap_start
< gap_stop
)
1494 if (! bfd_set_section_size (obfd
, osections
[i
],
1495 size
+ (gap_stop
- gap_start
)))
1497 non_fatal (_("Can't fill gap after %s: %s"),
1498 bfd_get_section_name (obfd
, osections
[i
]),
1499 bfd_errmsg (bfd_get_error ()));
1503 gaps
[i
] = gap_stop
- gap_start
;
1504 if (max_gap
< gap_stop
- gap_start
)
1505 max_gap
= gap_stop
- gap_start
;
1515 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1516 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1517 if (lma
+ size
< pad_to
)
1519 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1522 non_fatal (_("Can't add padding to %s: %s"),
1523 bfd_get_section_name (obfd
, osections
[c
- 1]),
1524 bfd_errmsg (bfd_get_error ()));
1529 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1530 if (max_gap
< pad_to
- (lma
+ size
))
1531 max_gap
= pad_to
- (lma
+ size
);
1537 /* Symbol filtering must happen after the output sections
1538 have been created, but before their contents are set. */
1540 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1543 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1547 osympp
= isympp
= xmalloc (symsize
);
1548 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1551 bfd_nonfatal (bfd_get_filename (ibfd
));
1555 if (convert_debugging
)
1556 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1558 if (strip_symbols
== STRIP_DEBUG
1559 || strip_symbols
== STRIP_ALL
1560 || strip_symbols
== STRIP_UNNEEDED
1561 || strip_symbols
== STRIP_NONDEBUG
1562 || discard_locals
!= LOCALS_UNDEF
1564 || strip_specific_list
!= NULL
1565 || keep_specific_list
!= NULL
1566 || localize_specific_list
!= NULL
1567 || globalize_specific_list
!= NULL
1568 || keepglobal_specific_list
!= NULL
1569 || weaken_specific_list
!= NULL
1570 || prefix_symbols_string
1573 || convert_debugging
1574 || change_leading_char
1575 || remove_leading_char
1576 || redefine_sym_list
1579 /* Mark symbols used in output relocations so that they
1580 are kept, even if they are local labels or static symbols.
1582 Note we iterate over the input sections examining their
1583 relocations since the relocations for the output sections
1584 haven't been set yet. mark_symbols_used_in_relocations will
1585 ignore input sections which have no corresponding output
1587 if (strip_symbols
!= STRIP_ALL
)
1588 bfd_map_over_sections (ibfd
,
1589 mark_symbols_used_in_relocations
,
1591 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1592 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1595 if (convert_debugging
&& dhandle
!= NULL
)
1597 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1604 bfd_set_symtab (obfd
, osympp
, symcount
);
1606 /* This has to happen after the symbol table has been set. */
1607 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1609 if (add_sections
!= NULL
)
1611 struct section_add
*padd
;
1613 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1615 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1618 bfd_nonfatal (bfd_get_filename (obfd
));
1624 if (gnu_debuglink_filename
!= NULL
)
1626 if (! bfd_fill_in_gnu_debuglink_section
1627 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1629 bfd_nonfatal (gnu_debuglink_filename
);
1634 if (gap_fill_set
|| pad_to_set
)
1639 /* Fill in the gaps. */
1642 buf
= xmalloc (max_gap
);
1643 memset (buf
, gap_fill
, max_gap
);
1645 c
= bfd_count_sections (obfd
);
1646 for (i
= 0; i
< c
; i
++)
1654 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1665 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1668 bfd_nonfatal (bfd_get_filename (obfd
));
1679 /* Allow the BFD backend to copy any private data it understands
1680 from the input BFD to the output BFD. This is done last to
1681 permit the routine to look at the filtered symbol table, which is
1682 important for the ECOFF code at least. */
1683 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1684 && strip_symbols
== STRIP_NONDEBUG
)
1685 /* Do not copy the private data when creating an ELF format
1686 debug info file. We do not want the program headers. */
1688 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1690 non_fatal (_("%s: error copying private BFD data: %s"),
1691 bfd_get_filename (obfd
),
1692 bfd_errmsg (bfd_get_error ()));
1696 /* Switch to the alternate machine code. We have to do this at the
1697 very end, because we only initialize the header when we create
1698 the first section. */
1699 if (use_alt_mach_code
!= 0)
1701 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1703 non_fatal (_("this target does not support %lu alternative machine codes"),
1705 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1707 non_fatal (_("treating that number as an absolute e_machine value instead"));
1708 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
1711 non_fatal (_("ignoring the alternative value"));
1719 #if defined (_WIN32) && !defined (__CYGWIN32__)
1720 #define MKDIR(DIR, MODE) mkdir (DIR)
1722 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1725 /* Read each archive element in turn from IBFD, copy the
1726 contents to temp file, and keep the temp file handle. */
1729 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1733 struct name_list
*next
;
1737 bfd
**ptr
= &obfd
->archive_head
;
1739 char *dir
= make_tempname (bfd_get_filename (obfd
));
1741 /* Make a temp directory to hold the contents. */
1742 if (MKDIR (dir
, 0700) != 0)
1743 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1744 dir
, strerror (errno
));
1746 obfd
->has_armap
= ibfd
->has_armap
;
1750 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1752 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1753 RETURN_NONFATAL (bfd_get_filename (obfd
));
1755 while (!status
&& this_element
!= NULL
)
1761 int stat_status
= 0;
1762 bfd_boolean
delete = TRUE
;
1764 /* Create an output file for this member. */
1765 output_name
= concat (dir
, "/",
1766 bfd_get_filename (this_element
), (char *) 0);
1768 /* If the file already exists, make another temp dir. */
1769 if (stat (output_name
, &buf
) >= 0)
1771 output_name
= make_tempname (output_name
);
1772 if (MKDIR (output_name
, 0700) != 0)
1773 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1774 output_name
, strerror (errno
));
1776 l
= xmalloc (sizeof (struct name_list
));
1777 l
->name
= output_name
;
1781 output_name
= concat (output_name
, "/",
1782 bfd_get_filename (this_element
), (char *) 0);
1785 output_bfd
= bfd_openw (output_name
, output_target
);
1788 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1790 if (stat_status
!= 0)
1791 non_fatal (_("internal stat error on %s"),
1792 bfd_get_filename (this_element
));
1795 l
= xmalloc (sizeof (struct name_list
));
1796 l
->name
= output_name
;
1801 if (output_bfd
== NULL
)
1802 RETURN_NONFATAL (output_name
);
1804 if (bfd_check_format (this_element
, bfd_object
))
1806 delete = ! copy_object (this_element
, output_bfd
);
1809 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
1811 if (!bfd_close (output_bfd
))
1813 bfd_nonfatal (bfd_get_filename (output_bfd
));
1814 /* Error in new object file. Don't change archive. */
1819 goto copy_unknown_element
;
1823 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1824 bfd_get_archive_filename (this_element
));
1826 copy_unknown_element
:
1827 delete = !copy_unknown_object (this_element
, output_bfd
);
1828 if (!bfd_close_all_done (output_bfd
))
1830 bfd_nonfatal (bfd_get_filename (output_bfd
));
1831 /* Error in new object file. Don't change archive. */
1838 unlink (output_name
);
1843 if (preserve_dates
&& stat_status
== 0)
1844 set_times (output_name
, &buf
);
1846 /* Open the newly output file and attach to our list. */
1847 output_bfd
= bfd_openr (output_name
, output_target
);
1849 l
->obfd
= output_bfd
;
1852 ptr
= &output_bfd
->next
;
1854 last_element
= this_element
;
1856 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1858 bfd_close (last_element
);
1863 if (!bfd_close (obfd
))
1864 RETURN_NONFATAL (bfd_get_filename (obfd
));
1866 if (!bfd_close (ibfd
))
1867 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1869 /* Delete all the files that we opened. */
1870 for (l
= list
; l
!= NULL
; l
= l
->next
)
1872 if (l
->obfd
== NULL
)
1876 bfd_close (l
->obfd
);
1883 /* The top-level control. */
1886 copy_file (const char *input_filename
, const char *output_filename
,
1887 const char *input_target
, const char *output_target
)
1890 char **obj_matching
;
1891 char **core_matching
;
1893 if (get_file_size (input_filename
) < 1)
1899 /* To allow us to do "strip *" without dying on the first
1900 non-object file, failures are nonfatal. */
1901 ibfd
= bfd_openr (input_filename
, input_target
);
1903 RETURN_NONFATAL (input_filename
);
1905 if (bfd_check_format (ibfd
, bfd_archive
))
1909 /* bfd_get_target does not return the correct value until
1910 bfd_check_format succeeds. */
1911 if (output_target
== NULL
)
1912 output_target
= bfd_get_target (ibfd
);
1914 obfd
= bfd_openw (output_filename
, output_target
);
1916 RETURN_NONFATAL (output_filename
);
1918 copy_archive (ibfd
, obfd
, output_target
);
1920 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1925 /* bfd_get_target does not return the correct value until
1926 bfd_check_format succeeds. */
1927 if (output_target
== NULL
)
1928 output_target
= bfd_get_target (ibfd
);
1930 obfd
= bfd_openw (output_filename
, output_target
);
1932 RETURN_NONFATAL (output_filename
);
1934 if (! copy_object (ibfd
, obfd
))
1937 if (!bfd_close (obfd
))
1938 RETURN_NONFATAL (output_filename
);
1940 if (!bfd_close (ibfd
))
1941 RETURN_NONFATAL (input_filename
);
1946 bfd_error_type obj_error
= bfd_get_error ();
1947 bfd_error_type core_error
;
1949 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1951 /* This probably can't happen.. */
1952 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1953 free (obj_matching
);
1957 core_error
= bfd_get_error ();
1958 /* Report the object error in preference to the core error. */
1959 if (obj_error
!= core_error
)
1960 bfd_set_error (obj_error
);
1962 bfd_nonfatal (input_filename
);
1964 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1966 list_matching_formats (obj_matching
);
1967 free (obj_matching
);
1969 if (core_error
== bfd_error_file_ambiguously_recognized
)
1971 list_matching_formats (core_matching
);
1972 free (core_matching
);
1979 /* Add a name to the section renaming list. */
1982 add_section_rename (const char * old_name
, const char * new_name
,
1985 section_rename
* rename
;
1987 /* Check for conflicts first. */
1988 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1989 if (strcmp (rename
->old_name
, old_name
) == 0)
1991 /* Silently ignore duplicate definitions. */
1992 if (strcmp (rename
->new_name
, new_name
) == 0
1993 && rename
->flags
== flags
)
1996 fatal (_("Multiple renames of section %s"), old_name
);
1999 rename
= xmalloc (sizeof (* rename
));
2001 rename
->old_name
= old_name
;
2002 rename
->new_name
= new_name
;
2003 rename
->flags
= flags
;
2004 rename
->next
= section_rename_list
;
2006 section_rename_list
= rename
;
2009 /* Check the section rename list for a new name of the input section
2010 ISECTION. Return the new name if one is found.
2011 Also set RETURNED_FLAGS to the flags to be used for this section. */
2014 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2015 flagword
* returned_flags
)
2017 const char * old_name
= bfd_section_name (ibfd
, isection
);
2018 section_rename
* rename
;
2020 /* Default to using the flags of the input section. */
2021 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2023 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2024 if (strcmp (rename
->old_name
, old_name
) == 0)
2026 if (rename
->flags
!= (flagword
) -1)
2027 * returned_flags
= rename
->flags
;
2029 return rename
->new_name
;
2035 /* Once each of the sections is copied, we may still need to do some
2036 finalization work for private section headers. Do that here. */
2039 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2043 /* Allow the BFD backend to copy any private data it understands
2044 from the input section to the output section. */
2045 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2047 err
= _("private header data");
2051 /* All went well. */
2055 non_fatal (_("%s: error in %s: %s"),
2056 bfd_get_filename (ibfd
),
2057 err
, bfd_errmsg (bfd_get_error ()));
2061 /* Create a section in OBFD with the same
2062 name and attributes as ISECTION in IBFD. */
2065 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2067 bfd
*obfd
= obfdarg
;
2068 struct section_list
*p
;
2076 char *prefix
= NULL
;
2078 if (is_strip_section (ibfd
, isection
))
2081 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2085 /* Get the, possibly new, name of the output section. */
2086 name
= find_section_rename (ibfd
, isection
, & flags
);
2088 /* Prefix sections. */
2089 if ((prefix_alloc_sections_string
)
2090 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2091 prefix
= prefix_alloc_sections_string
;
2092 else if (prefix_sections_string
)
2093 prefix
= prefix_sections_string
;
2099 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
2105 if (p
!= NULL
&& p
->set_flags
)
2106 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2107 else if (strip_symbols
== STRIP_NONDEBUG
&& (flags
& SEC_ALLOC
) != 0)
2108 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2110 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2112 if (osection
== NULL
)
2118 if (strip_symbols
== STRIP_NONDEBUG
2119 && obfd
->xvec
->flavour
== bfd_target_elf_flavour
2120 && (flags
& SEC_ALLOC
) != 0
2121 && (p
== NULL
|| !p
->set_flags
))
2122 elf_section_type (osection
) = SHT_NOBITS
;
2124 size
= bfd_section_size (ibfd
, isection
);
2126 size
= (size
+ interleave
- 1) / interleave
;
2127 if (! bfd_set_section_size (obfd
, osection
, size
))
2133 vma
= bfd_section_vma (ibfd
, isection
);
2134 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2136 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2139 vma
+= change_section_address
;
2141 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2147 lma
= isection
->lma
;
2148 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2150 if (p
->change_lma
== CHANGE_MODIFY
)
2152 else if (p
->change_lma
== CHANGE_SET
)
2158 lma
+= change_section_address
;
2160 osection
->lma
= lma
;
2162 /* FIXME: This is probably not enough. If we change the LMA we
2163 may have to recompute the header for the file as well. */
2164 if (!bfd_set_section_alignment (obfd
,
2166 bfd_section_alignment (ibfd
, isection
)))
2168 err
= _("alignment");
2172 /* Copy merge entity size. */
2173 osection
->entsize
= isection
->entsize
;
2175 /* This used to be mangle_section; we do here to avoid using
2176 bfd_get_section_by_name since some formats allow multiple
2177 sections with the same name. */
2178 isection
->output_section
= osection
;
2179 isection
->output_offset
= 0;
2181 /* Allow the BFD backend to copy any private data it understands
2182 from the input section to the output section. */
2183 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
2184 && strip_symbols
== STRIP_NONDEBUG
)
2185 /* Do not copy the private data when creating an ELF format
2186 debug info file. We do not want the program headers. */
2188 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2190 err
= _("private data");
2194 /* All went well. */
2198 non_fatal (_("%s: section `%s': error in %s: %s"),
2199 bfd_get_filename (ibfd
),
2200 bfd_section_name (ibfd
, isection
),
2201 err
, bfd_errmsg (bfd_get_error ()));
2205 /* Copy the data of input section ISECTION of IBFD
2206 to an output section with the same name in OBFD.
2207 If stripping then don't copy any relocation info. */
2210 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2212 bfd
*obfd
= obfdarg
;
2213 struct section_list
*p
;
2221 /* If we have already failed earlier on,
2222 do not keep on generating complaints now. */
2226 if (is_strip_section (ibfd
, isection
))
2229 flags
= bfd_get_section_flags (ibfd
, isection
);
2230 if ((flags
& SEC_GROUP
) != 0)
2233 osection
= isection
->output_section
;
2234 size
= bfd_get_section_size (isection
);
2236 if (size
== 0 || osection
== 0)
2239 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2241 /* Core files do not need to be relocated. */
2242 if (bfd_get_format (obfd
) == bfd_core
)
2246 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2250 /* Do not complain if the target does not support relocations. */
2251 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2254 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2259 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2262 relpp
= xmalloc (relsize
);
2263 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2265 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2267 if (strip_symbols
== STRIP_ALL
)
2269 /* Remove relocations which are not in
2270 keep_strip_specific_list. */
2271 arelent
**temp_relpp
;
2272 long temp_relcount
= 0;
2275 temp_relpp
= xmalloc (relsize
);
2276 for (i
= 0; i
< relcount
; i
++)
2277 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2278 keep_specific_list
))
2279 temp_relpp
[temp_relcount
++] = relpp
[i
];
2280 relcount
= temp_relcount
;
2285 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2290 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2291 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2293 void *memhunk
= xmalloc (size
);
2295 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2296 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2300 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2301 char *from
= (char *) memhunk
+ copy_byte
;
2303 char *end
= (char *) memhunk
+ size
;
2305 for (; from
< end
; from
+= interleave
)
2308 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2309 osection
->lma
/= interleave
;
2312 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2313 RETURN_NONFATAL (bfd_get_filename (obfd
));
2317 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2319 void *memhunk
= xmalloc (size
);
2321 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2322 flag--they can just remove the section entirely and add it
2323 back again. However, we do permit them to turn on the
2324 SEC_HAS_CONTENTS flag, and take it to mean that the section
2325 contents should be zeroed out. */
2327 memset (memhunk
, 0, size
);
2328 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2329 RETURN_NONFATAL (bfd_get_filename (obfd
));
2334 /* Get all the sections. This is used when --gap-fill or --pad-to is
2338 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2340 asection
***secppp
= secppparg
;
2342 **secppp
= osection
;
2346 /* Sort sections by VMA. This is called via qsort, and is used when
2347 --gap-fill or --pad-to is used. We force non loadable or empty
2348 sections to the front, where they are easier to ignore. */
2351 compare_section_lma (const void *arg1
, const void *arg2
)
2353 const asection
*const *sec1
= arg1
;
2354 const asection
*const *sec2
= arg2
;
2355 flagword flags1
, flags2
;
2357 /* Sort non loadable sections to the front. */
2358 flags1
= (*sec1
)->flags
;
2359 flags2
= (*sec2
)->flags
;
2360 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2361 || (flags1
& SEC_LOAD
) == 0)
2363 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2364 && (flags2
& SEC_LOAD
) != 0)
2369 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2370 || (flags2
& SEC_LOAD
) == 0)
2374 /* Sort sections by LMA. */
2375 if ((*sec1
)->lma
> (*sec2
)->lma
)
2377 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2380 /* Sort sections with the same LMA by size. */
2381 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2383 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2389 /* Mark all the symbols which will be used in output relocations with
2390 the BSF_KEEP flag so that those symbols will not be stripped.
2392 Ignore relocations which will not appear in the output file. */
2395 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2397 asymbol
**symbols
= symbolsarg
;
2402 /* Ignore an input section with no corresponding output section. */
2403 if (isection
->output_section
== NULL
)
2406 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2409 /* Do not complain if the target does not support relocations. */
2410 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2412 bfd_fatal (bfd_get_filename (ibfd
));
2418 relpp
= xmalloc (relsize
);
2419 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2421 bfd_fatal (bfd_get_filename (ibfd
));
2423 /* Examine each symbol used in a relocation. If it's not one of the
2424 special bfd section symbols, then mark it with BSF_KEEP. */
2425 for (i
= 0; i
< relcount
; i
++)
2427 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2428 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2429 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2430 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2437 /* Write out debugging information. */
2440 write_debugging_info (bfd
*obfd
, void *dhandle
,
2441 long *symcountp ATTRIBUTE_UNUSED
,
2442 asymbol
***symppp ATTRIBUTE_UNUSED
)
2444 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2445 return write_ieee_debugging_info (obfd
, dhandle
);
2447 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2448 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2450 bfd_byte
*syms
, *strings
;
2451 bfd_size_type symsize
, stringsize
;
2452 asection
*stabsec
, *stabstrsec
;
2455 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2460 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2461 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2462 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2464 || stabstrsec
== NULL
2465 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2466 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2467 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2468 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2470 non_fatal (_("%s: can't create debugging section: %s"),
2471 bfd_get_filename (obfd
),
2472 bfd_errmsg (bfd_get_error ()));
2476 /* We can get away with setting the section contents now because
2477 the next thing the caller is going to do is copy over the
2478 real sections. We may someday have to split the contents
2479 setting out of this function. */
2480 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2481 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2484 non_fatal (_("%s: can't set debugging section contents: %s"),
2485 bfd_get_filename (obfd
),
2486 bfd_errmsg (bfd_get_error ()));
2493 non_fatal (_("%s: don't know how to write debugging information for %s"),
2494 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2499 strip_main (int argc
, char *argv
[])
2501 char *input_target
= NULL
;
2502 char *output_target
= NULL
;
2503 bfd_boolean show_version
= FALSE
;
2504 bfd_boolean formats_info
= FALSE
;
2507 struct section_list
*p
;
2508 char *output_file
= NULL
;
2510 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2511 strip_options
, (int *) 0)) != EOF
)
2516 input_target
= optarg
;
2519 output_target
= optarg
;
2522 input_target
= output_target
= optarg
;
2525 p
= find_section_list (optarg
, TRUE
);
2527 sections_removed
= TRUE
;
2530 strip_symbols
= STRIP_ALL
;
2534 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2535 strip_symbols
= STRIP_DEBUG
;
2537 case OPTION_STRIP_UNNEEDED
:
2538 strip_symbols
= STRIP_UNNEEDED
;
2541 add_specific_symbol (optarg
, &keep_specific_list
);
2544 add_specific_symbol (optarg
, &strip_specific_list
);
2547 output_file
= optarg
;
2550 preserve_dates
= TRUE
;
2553 discard_locals
= LOCALS_ALL
;
2556 discard_locals
= LOCALS_START_L
;
2562 show_version
= TRUE
;
2564 case OPTION_FORMATS_INFO
:
2565 formats_info
= TRUE
;
2567 case OPTION_ONLY_KEEP_DEBUG
:
2568 strip_symbols
= STRIP_NONDEBUG
;
2570 case OPTION_KEEP_FILE_SYMBOLS
:
2571 keep_file_symbols
= 1;
2574 /* We've been given a long option. */
2581 strip_usage (stdout
, 0);
2583 strip_usage (stderr
, 1);
2594 print_version ("strip");
2596 /* Default is to strip all symbols. */
2597 if (strip_symbols
== STRIP_UNDEF
2598 && discard_locals
== LOCALS_UNDEF
2599 && strip_specific_list
== NULL
)
2600 strip_symbols
= STRIP_ALL
;
2602 if (output_target
== NULL
)
2603 output_target
= input_target
;
2607 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2608 strip_usage (stderr
, 1);
2610 for (; i
< argc
; i
++)
2612 int hold_status
= status
;
2613 struct stat statbuf
;
2616 if (get_file_size (argv
[i
]) < 1)
2623 /* No need to check the return value of stat().
2624 It has already been checked in get_file_size(). */
2625 stat (argv
[i
], &statbuf
);
2627 if (output_file
!= NULL
)
2628 tmpname
= output_file
;
2630 tmpname
= make_tempname (argv
[i
]);
2633 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2637 set_times (tmpname
, &statbuf
);
2638 if (output_file
== NULL
)
2639 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2640 status
= hold_status
;
2643 unlink_if_ordinary (tmpname
);
2644 if (output_file
== NULL
)
2652 copy_main (int argc
, char *argv
[])
2654 char * binary_architecture
= NULL
;
2655 char *input_filename
= NULL
;
2656 char *output_filename
= NULL
;
2657 char *input_target
= NULL
;
2658 char *output_target
= NULL
;
2659 bfd_boolean show_version
= FALSE
;
2660 bfd_boolean change_warn
= TRUE
;
2661 bfd_boolean formats_info
= FALSE
;
2663 struct section_list
*p
;
2664 struct stat statbuf
;
2666 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2667 copy_options
, (int *) 0)) != EOF
)
2672 copy_byte
= atoi (optarg
);
2674 fatal (_("byte number must be non-negative"));
2678 binary_architecture
= optarg
;
2682 interleave
= atoi (optarg
);
2684 fatal (_("interleave must be positive"));
2688 case 's': /* "source" - 'I' is preferred */
2689 input_target
= optarg
;
2693 case 'd': /* "destination" - 'O' is preferred */
2694 output_target
= optarg
;
2698 input_target
= output_target
= optarg
;
2702 p
= find_section_list (optarg
, TRUE
);
2704 fatal (_("%s both copied and removed"), optarg
);
2706 sections_copied
= TRUE
;
2710 p
= find_section_list (optarg
, TRUE
);
2712 fatal (_("%s both copied and removed"), optarg
);
2714 sections_removed
= TRUE
;
2718 strip_symbols
= STRIP_ALL
;
2722 strip_symbols
= STRIP_DEBUG
;
2725 case OPTION_STRIP_UNNEEDED
:
2726 strip_symbols
= STRIP_UNNEEDED
;
2729 case OPTION_ONLY_KEEP_DEBUG
:
2730 strip_symbols
= STRIP_NONDEBUG
;
2733 case OPTION_KEEP_FILE_SYMBOLS
:
2734 keep_file_symbols
= 1;
2737 case OPTION_ADD_GNU_DEBUGLINK
:
2738 gnu_debuglink_filename
= optarg
;
2742 add_specific_symbol (optarg
, &keep_specific_list
);
2746 add_specific_symbol (optarg
, &strip_specific_list
);
2749 case OPTION_STRIP_UNNEEDED_SYMBOL
:
2750 add_specific_symbol (optarg
, &strip_unneeded_list
);
2754 add_specific_symbol (optarg
, &localize_specific_list
);
2757 case OPTION_GLOBALIZE_SYMBOL
:
2758 add_specific_symbol (optarg
, &globalize_specific_list
);
2762 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2766 add_specific_symbol (optarg
, &weaken_specific_list
);
2770 preserve_dates
= TRUE
;
2778 discard_locals
= LOCALS_ALL
;
2782 discard_locals
= LOCALS_START_L
;
2790 show_version
= TRUE
;
2793 case OPTION_FORMATS_INFO
:
2794 formats_info
= TRUE
;
2801 case OPTION_ADD_SECTION
:
2805 struct section_add
*pa
;
2810 s
= strchr (optarg
, '=');
2813 fatal (_("bad format for %s"), "--add-section");
2815 size
= get_file_size (s
+ 1);
2822 pa
= xmalloc (sizeof (struct section_add
));
2825 name
= xmalloc (len
+ 1);
2826 strncpy (name
, optarg
, len
);
2830 pa
->filename
= s
+ 1;
2832 pa
->contents
= xmalloc (size
);
2834 f
= fopen (pa
->filename
, FOPEN_RB
);
2837 fatal (_("cannot open: %s: %s"),
2838 pa
->filename
, strerror (errno
));
2840 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2842 fatal (_("%s: fread failed"), pa
->filename
);
2846 pa
->next
= add_sections
;
2851 case OPTION_CHANGE_START
:
2852 change_start
= parse_vma (optarg
, "--change-start");
2855 case OPTION_CHANGE_SECTION_ADDRESS
:
2856 case OPTION_CHANGE_SECTION_LMA
:
2857 case OPTION_CHANGE_SECTION_VMA
:
2862 char *option
= NULL
;
2864 enum change_action what
= CHANGE_IGNORE
;
2868 case OPTION_CHANGE_SECTION_ADDRESS
:
2869 option
= "--change-section-address";
2871 case OPTION_CHANGE_SECTION_LMA
:
2872 option
= "--change-section-lma";
2874 case OPTION_CHANGE_SECTION_VMA
:
2875 option
= "--change-section-vma";
2879 s
= strchr (optarg
, '=');
2882 s
= strchr (optarg
, '+');
2885 s
= strchr (optarg
, '-');
2887 fatal (_("bad format for %s"), option
);
2892 name
= xmalloc (len
+ 1);
2893 strncpy (name
, optarg
, len
);
2896 p
= find_section_list (name
, TRUE
);
2898 val
= parse_vma (s
+ 1, option
);
2902 case '=': what
= CHANGE_SET
; break;
2903 case '-': val
= - val
; /* Drop through. */
2904 case '+': what
= CHANGE_MODIFY
; break;
2909 case OPTION_CHANGE_SECTION_ADDRESS
:
2910 p
->change_vma
= what
;
2914 case OPTION_CHANGE_SECTION_LMA
:
2915 p
->change_lma
= what
;
2919 case OPTION_CHANGE_SECTION_VMA
:
2920 p
->change_vma
= what
;
2927 case OPTION_CHANGE_ADDRESSES
:
2928 change_section_address
= parse_vma (optarg
, "--change-addresses");
2929 change_start
= change_section_address
;
2932 case OPTION_CHANGE_WARNINGS
:
2936 case OPTION_CHANGE_LEADING_CHAR
:
2937 change_leading_char
= TRUE
;
2940 case OPTION_DEBUGGING
:
2941 convert_debugging
= TRUE
;
2944 case OPTION_GAP_FILL
:
2946 bfd_vma gap_fill_vma
;
2948 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2949 gap_fill
= (bfd_byte
) gap_fill_vma
;
2950 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2954 sprintf_vma (buff
, gap_fill_vma
);
2956 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2959 gap_fill_set
= TRUE
;
2963 case OPTION_NO_CHANGE_WARNINGS
:
2964 change_warn
= FALSE
;
2968 pad_to
= parse_vma (optarg
, "--pad-to");
2972 case OPTION_REMOVE_LEADING_CHAR
:
2973 remove_leading_char
= TRUE
;
2976 case OPTION_REDEFINE_SYM
:
2978 /* Push this redefinition onto redefine_symbol_list. */
2982 const char *nextarg
;
2983 char *source
, *target
;
2985 s
= strchr (optarg
, '=');
2987 fatal (_("bad format for %s"), "--redefine-sym");
2990 source
= xmalloc (len
+ 1);
2991 strncpy (source
, optarg
, len
);
2995 len
= strlen (nextarg
);
2996 target
= xmalloc (len
+ 1);
2997 strcpy (target
, nextarg
);
2999 redefine_list_append ("--redefine-sym", source
, target
);
3006 case OPTION_REDEFINE_SYMS
:
3007 add_redefine_syms_file (optarg
);
3010 case OPTION_SET_SECTION_FLAGS
:
3016 s
= strchr (optarg
, '=');
3018 fatal (_("bad format for %s"), "--set-section-flags");
3021 name
= xmalloc (len
+ 1);
3022 strncpy (name
, optarg
, len
);
3025 p
= find_section_list (name
, TRUE
);
3027 p
->set_flags
= TRUE
;
3028 p
->flags
= parse_flags (s
+ 1);
3032 case OPTION_RENAME_SECTION
:
3035 const char *eq
, *fl
;
3040 eq
= strchr (optarg
, '=');
3042 fatal (_("bad format for %s"), "--rename-section");
3046 fatal (_("bad format for %s"), "--rename-section");
3048 old_name
= xmalloc (len
+ 1);
3049 strncpy (old_name
, optarg
, len
);
3053 fl
= strchr (eq
, ',');
3056 flags
= parse_flags (fl
+ 1);
3066 fatal (_("bad format for %s"), "--rename-section");
3068 new_name
= xmalloc (len
+ 1);
3069 strncpy (new_name
, eq
, len
);
3072 add_section_rename (old_name
, new_name
, flags
);
3076 case OPTION_SET_START
:
3077 set_start
= parse_vma (optarg
, "--set-start");
3078 set_start_set
= TRUE
;
3081 case OPTION_SREC_LEN
:
3082 Chunk
= parse_vma (optarg
, "--srec-len");
3085 case OPTION_SREC_FORCES3
:
3089 case OPTION_STRIP_SYMBOLS
:
3090 add_specific_symbols (optarg
, &strip_specific_list
);
3093 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3094 add_specific_symbols (optarg
, &strip_unneeded_list
);
3097 case OPTION_KEEP_SYMBOLS
:
3098 add_specific_symbols (optarg
, &keep_specific_list
);
3101 case OPTION_LOCALIZE_HIDDEN
:
3102 localize_hidden
= TRUE
;
3105 case OPTION_LOCALIZE_SYMBOLS
:
3106 add_specific_symbols (optarg
, &localize_specific_list
);
3109 case OPTION_GLOBALIZE_SYMBOLS
:
3110 add_specific_symbols (optarg
, &globalize_specific_list
);
3113 case OPTION_KEEPGLOBAL_SYMBOLS
:
3114 add_specific_symbols (optarg
, &keepglobal_specific_list
);
3117 case OPTION_WEAKEN_SYMBOLS
:
3118 add_specific_symbols (optarg
, &weaken_specific_list
);
3121 case OPTION_ALT_MACH_CODE
:
3122 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3123 if (use_alt_mach_code
== 0)
3124 fatal (_("unable to parse alternative machine code"));
3127 case OPTION_PREFIX_SYMBOLS
:
3128 prefix_symbols_string
= optarg
;
3131 case OPTION_PREFIX_SECTIONS
:
3132 prefix_sections_string
= optarg
;
3135 case OPTION_PREFIX_ALLOC_SECTIONS
:
3136 prefix_alloc_sections_string
= optarg
;
3139 case OPTION_READONLY_TEXT
:
3140 bfd_flags_to_set
|= WP_TEXT
;
3141 bfd_flags_to_clear
&= ~WP_TEXT
;
3144 case OPTION_WRITABLE_TEXT
:
3145 bfd_flags_to_clear
|= WP_TEXT
;
3146 bfd_flags_to_set
&= ~WP_TEXT
;
3150 bfd_flags_to_set
|= D_PAGED
;
3151 bfd_flags_to_clear
&= ~D_PAGED
;
3155 bfd_flags_to_clear
|= D_PAGED
;
3156 bfd_flags_to_set
&= ~D_PAGED
;
3160 /* We've been given a long option. */
3165 copy_usage (stdout
, 0);
3168 copy_usage (stderr
, 1);
3179 print_version ("objcopy");
3181 if (copy_byte
>= interleave
)
3182 fatal (_("byte number must be less than interleave"));
3184 if (optind
== argc
|| optind
+ 2 < argc
)
3185 copy_usage (stderr
, 1);
3187 input_filename
= argv
[optind
];
3188 if (optind
+ 1 < argc
)
3189 output_filename
= argv
[optind
+ 1];
3191 /* Default is to strip no symbols. */
3192 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3193 strip_symbols
= STRIP_NONE
;
3195 if (output_target
== NULL
)
3196 output_target
= input_target
;
3198 if (binary_architecture
!= NULL
)
3200 if (input_target
&& strcmp (input_target
, "binary") == 0)
3202 const bfd_arch_info_type
* temp_arch_info
;
3204 temp_arch_info
= bfd_scan_arch (binary_architecture
);
3206 if (temp_arch_info
!= NULL
)
3208 bfd_external_binary_architecture
= temp_arch_info
->arch
;
3209 bfd_external_machine
= temp_arch_info
->mach
;
3212 fatal (_("architecture %s unknown"), binary_architecture
);
3216 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3217 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3222 if (stat (input_filename
, & statbuf
) < 0)
3223 fatal (_("warning: could not locate '%s'. System error message: %s"),
3224 input_filename
, strerror (errno
));
3226 /* If there is no destination file, or the source and destination files
3227 are the same, then create a temp and rename the result into the input. */
3228 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3230 char *tmpname
= make_tempname (input_filename
);
3232 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3236 set_times (tmpname
, &statbuf
);
3237 smart_rename (tmpname
, input_filename
, preserve_dates
);
3244 copy_file (input_filename
, output_filename
, input_target
, output_target
);
3246 if (status
== 0 && preserve_dates
)
3247 set_times (output_filename
, &statbuf
);
3248 else if (status
!= 0)
3249 unlink_if_ordinary (output_filename
);
3254 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3258 if (p
->change_vma
!= CHANGE_IGNORE
)
3262 sprintf_vma (buff
, p
->vma_val
);
3264 /* xgettext:c-format */
3265 non_fatal (_("%s %s%c0x%s never used"),
3266 "--change-section-vma",
3268 p
->change_vma
== CHANGE_SET
? '=' : '+',
3272 if (p
->change_lma
!= CHANGE_IGNORE
)
3276 sprintf_vma (buff
, p
->lma_val
);
3278 /* xgettext:c-format */
3279 non_fatal (_("%s %s%c0x%s never used"),
3280 "--change-section-lma",
3282 p
->change_lma
== CHANGE_SET
? '=' : '+',
3293 main (int argc
, char *argv
[])
3295 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3296 setlocale (LC_MESSAGES
, "");
3298 #if defined (HAVE_SETLOCALE)
3299 setlocale (LC_CTYPE
, "");
3301 bindtextdomain (PACKAGE
, LOCALEDIR
);
3302 textdomain (PACKAGE
);
3304 program_name
= argv
[0];
3305 xmalloc_set_program_name (program_name
);
3307 START_PROGRESS (program_name
, 0);
3309 expandargv (&argc
, &argv
);
3311 strip_symbols
= STRIP_UNDEF
;
3312 discard_locals
= LOCALS_UNDEF
;
3315 set_default_bfd_target ();
3319 int i
= strlen (program_name
);
3320 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3321 /* Drop the .exe suffix, if any. */
3322 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3325 program_name
[i
] = '\0';
3328 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3332 strip_main (argc
, argv
);
3334 copy_main (argc
, argv
);
3336 END_PROGRESS (program_name
);