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
);
658 buffer
= xmalloc (size
+ 2);
659 f
= fopen (filename
, FOPEN_RT
);
661 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
663 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
664 fatal (_("%s: fread failed"), filename
);
667 buffer
[size
] = '\n';
668 buffer
[size
+ 1] = '\0';
672 for (line
= buffer
; * line
!= '\0'; line
++)
677 int finished
= FALSE
;
679 for (eol
= line
;; eol
++)
685 /* Cope with \n\r. */
693 /* Cope with \r\n. */
704 /* Line comment, Terminate the line here, in case a
705 name is present and then allow the rest of the
706 loop to find the real end of the line. */
718 /* A name may now exist somewhere between 'line' and 'eol'.
719 Strip off leading whitespace and trailing whitespace,
720 then add it to the list. */
721 for (name
= line
; IS_WHITESPACE (* name
); name
++)
723 for (name_end
= name
;
724 (! IS_WHITESPACE (* name_end
))
725 && (! IS_LINE_TERMINATOR (* name_end
));
729 if (! IS_LINE_TERMINATOR (* name_end
))
733 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
736 if (! IS_LINE_TERMINATOR (* extra
))
737 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
738 filename
, line_count
);
744 add_specific_symbol (name
, list
);
746 /* Advance line pointer to end of line. The 'eol ++' in the for
747 loop above will then advance us to the start of the next line. */
753 /* See whether a symbol should be stripped or kept based on
754 strip_specific_list and keep_symbols. */
757 is_specified_symbol (const char *name
, struct symlist
*list
)
759 struct symlist
*tmp_list
;
763 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
764 if (*(tmp_list
->name
) != '!')
766 if (!fnmatch (tmp_list
->name
, name
, 0))
771 if (fnmatch (tmp_list
->name
+ 1, name
, 0))
777 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
778 if (strcmp (name
, tmp_list
->name
) == 0)
785 /* See if a section is being removed. */
788 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
790 if (sections_removed
|| sections_copied
)
792 struct section_list
*p
;
794 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
796 if (sections_removed
&& p
!= NULL
&& p
->remove
)
798 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
802 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
804 if (strip_symbols
== STRIP_DEBUG
805 || strip_symbols
== STRIP_UNNEEDED
806 || strip_symbols
== STRIP_ALL
807 || discard_locals
== LOCALS_ALL
808 || convert_debugging
)
811 if (strip_symbols
== STRIP_NONDEBUG
)
818 /* Return true if SYM is a hidden symbol. */
821 is_hidden_symbol (asymbol
*sym
)
823 elf_symbol_type
*elf_sym
;
825 elf_sym
= elf_symbol_from (sym
->the_bfd
, sym
);
827 switch (ELF_ST_VISIBILITY (elf_sym
->internal_elf_sym
.st_other
))
836 /* Choose which symbol entries to copy; put the result in OSYMS.
837 We don't copy in place, because that confuses the relocs.
838 Return the number of symbols to print. */
841 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
842 asymbol
**isyms
, long symcount
)
844 asymbol
**from
= isyms
, **to
= osyms
;
845 long src_count
= 0, dst_count
= 0;
846 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
849 for (; src_count
< symcount
; src_count
++)
851 asymbol
*sym
= from
[src_count
];
852 flagword flags
= sym
->flags
;
853 char *name
= (char *) bfd_asymbol_name (sym
);
855 bfd_boolean undefined
;
856 bfd_boolean rem_leading_char
;
857 bfd_boolean add_leading_char
;
859 undefined
= bfd_is_und_section (bfd_get_section (sym
));
861 if (redefine_sym_list
)
863 char *old_name
, *new_name
;
865 old_name
= (char *) bfd_asymbol_name (sym
);
866 new_name
= (char *) lookup_sym_redefinition (old_name
);
867 bfd_asymbol_name (sym
) = new_name
;
871 /* Check if we will remove the current leading character. */
873 (name
[0] == bfd_get_symbol_leading_char (abfd
))
874 && (change_leading_char
875 || (remove_leading_char
876 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
878 || bfd_is_com_section (bfd_get_section (sym
)))));
880 /* Check if we will add a new leading character. */
883 && (bfd_get_symbol_leading_char (obfd
) != '\0')
884 && (bfd_get_symbol_leading_char (abfd
) == '\0'
885 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
887 /* Short circuit for change_leading_char if we can do it in-place. */
888 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
890 name
[0] = bfd_get_symbol_leading_char (obfd
);
891 bfd_asymbol_name (sym
) = name
;
892 rem_leading_char
= FALSE
;
893 add_leading_char
= FALSE
;
896 /* Remove leading char. */
897 if (rem_leading_char
)
898 bfd_asymbol_name (sym
) = ++name
;
900 /* Add new leading char and/or prefix. */
901 if (add_leading_char
|| prefix_symbols_string
)
905 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
906 + strlen (name
) + 1);
907 if (add_leading_char
)
908 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
910 if (prefix_symbols_string
)
912 strcpy (ptr
, prefix_symbols_string
);
913 ptr
+= strlen (prefix_symbols_string
);
917 bfd_asymbol_name (sym
) = n
;
921 if (strip_symbols
== STRIP_ALL
)
923 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
924 || ((flags
& BSF_SECTION_SYM
) != 0
925 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
928 else if (relocatable
/* Relocatable file. */
929 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
931 else if (bfd_decode_symclass (sym
) == 'I')
932 /* Global symbols in $idata sections need to be retained
933 even if relocatable is FALSE. External users of the
934 library containing the $idata section may reference these
937 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
938 || (flags
& BSF_WEAK
) != 0
940 || bfd_is_com_section (bfd_get_section (sym
)))
941 keep
= strip_symbols
!= STRIP_UNNEEDED
;
942 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
943 keep
= (strip_symbols
!= STRIP_DEBUG
944 && strip_symbols
!= STRIP_UNNEEDED
945 && ! convert_debugging
);
946 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
947 /* COMDAT sections store special information in local
948 symbols, so we cannot risk stripping any of them. */
950 else /* Local symbol. */
951 keep
= (strip_symbols
!= STRIP_UNNEEDED
952 && (discard_locals
!= LOCALS_ALL
953 && (discard_locals
!= LOCALS_START_L
954 || ! bfd_is_local_label (abfd
, sym
))));
956 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
959 && !(flags
& BSF_KEEP
)
960 && is_specified_symbol (name
, strip_unneeded_list
))
963 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
964 || is_specified_symbol (name
, keep_specific_list
)))
966 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
971 if ((flags
& BSF_GLOBAL
) != 0
972 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
974 sym
->flags
&= ~ BSF_GLOBAL
;
975 sym
->flags
|= BSF_WEAK
;
979 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
980 && (is_specified_symbol (name
, localize_specific_list
)
981 || (keepglobal_specific_list
!= NULL
982 && ! is_specified_symbol (name
, keepglobal_specific_list
))
983 || (localize_hidden
&& is_hidden_symbol (sym
))))
985 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
986 sym
->flags
|= BSF_LOCAL
;
990 && (flags
& BSF_LOCAL
)
991 && is_specified_symbol (name
, globalize_specific_list
))
993 sym
->flags
&= ~ BSF_LOCAL
;
994 sym
->flags
|= BSF_GLOBAL
;
997 to
[dst_count
++] = sym
;
1001 to
[dst_count
] = NULL
;
1006 /* Find the redefined name of symbol SOURCE. */
1009 lookup_sym_redefinition (const char *source
)
1011 struct redefine_node
*list
;
1013 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
1014 if (strcmp (source
, list
->source
) == 0)
1015 return list
->target
;
1020 /* Add a node to a symbol redefine list. */
1023 redefine_list_append (const char *cause
, const char *source
, const char *target
)
1025 struct redefine_node
**p
;
1026 struct redefine_node
*list
;
1027 struct redefine_node
*new_node
;
1029 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1031 if (strcmp (source
, list
->source
) == 0)
1032 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1035 if (strcmp (target
, list
->target
) == 0)
1036 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1040 new_node
= xmalloc (sizeof (struct redefine_node
));
1042 new_node
->source
= strdup (source
);
1043 new_node
->target
= strdup (target
);
1044 new_node
->next
= NULL
;
1049 /* Handle the --redefine-syms option. Read lines containing "old new"
1050 from the file, and add them to the symbol redefine list. */
1053 add_redefine_syms_file (const char *filename
)
1062 file
= fopen (filename
, "r");
1064 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1065 filename
, strerror (errno
));
1068 buf
= xmalloc (bufsize
);
1076 /* Collect the input symbol name. */
1077 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1085 buf
= xrealloc (buf
, bufsize
);
1093 /* Eat white space between the symbol names. */
1094 while (IS_WHITESPACE (c
))
1096 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1101 /* Collect the output symbol name. */
1103 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1111 buf
= xrealloc (buf
, bufsize
);
1119 /* Eat white space at end of line. */
1120 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1125 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1126 || c
== '\n' || c
== EOF
)
1129 /* Append the redefinition to the list. */
1131 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1142 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1144 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1145 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1148 /* Eat the rest of the line and finish it. */
1149 while (c
!= '\n' && c
!= EOF
)
1155 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1160 /* Copy unkown object file IBFD onto OBFD.
1161 Returns TRUE upon success, FALSE otherwise. */
1164 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1172 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1174 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1181 non_fatal (_("stat returns negative size for `%s'"),
1182 bfd_get_archive_filename (ibfd
));
1186 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1188 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1193 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1194 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1196 cbuf
= xmalloc (BUFSIZE
);
1198 while (ncopied
< size
)
1200 tocopy
= size
- ncopied
;
1201 if (tocopy
> BUFSIZE
)
1204 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1205 != (bfd_size_type
) tocopy
)
1207 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1212 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1213 != (bfd_size_type
) tocopy
)
1215 bfd_nonfatal (bfd_get_filename (obfd
));
1223 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1228 /* Copy object file IBFD onto OBFD.
1229 Returns TRUE upon success, FALSE otherwise. */
1232 copy_object (bfd
*ibfd
, bfd
*obfd
)
1236 asection
**osections
= NULL
;
1237 asection
*gnu_debuglink_section
= NULL
;
1238 bfd_size_type
*gaps
= NULL
;
1239 bfd_size_type max_gap
= 0;
1242 enum bfd_architecture iarch
;
1245 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1246 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1247 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1248 fatal (_("Unable to change endianness of input file(s)"));
1250 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1252 bfd_nonfatal (bfd_get_filename (obfd
));
1257 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1258 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1259 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1264 start
= bfd_get_start_address (ibfd
);
1265 start
+= change_start
;
1267 /* Neither the start address nor the flags
1268 need to be set for a core file. */
1269 if (bfd_get_format (obfd
) != bfd_core
)
1273 flags
= bfd_get_file_flags (ibfd
);
1274 flags
|= bfd_flags_to_set
;
1275 flags
&= ~bfd_flags_to_clear
;
1276 flags
&= bfd_applicable_file_flags (obfd
);
1278 if (!bfd_set_start_address (obfd
, start
)
1279 || !bfd_set_file_flags (obfd
, flags
))
1281 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1286 /* Copy architecture of input file to output file. */
1287 iarch
= bfd_get_arch (ibfd
);
1288 imach
= bfd_get_mach (ibfd
);
1289 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1290 && (ibfd
->target_defaulted
1291 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1293 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1294 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1295 bfd_get_archive_filename (ibfd
));
1297 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1298 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1299 bfd_get_mach (ibfd
)));
1303 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1305 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1312 if (osympp
!= isympp
)
1318 /* BFD mandates that all output sections be created and sizes set before
1319 any output is done. Thus, we traverse all sections multiple times. */
1320 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1322 setup_bfd_headers (ibfd
, obfd
);
1324 if (add_sections
!= NULL
)
1326 struct section_add
*padd
;
1327 struct section_list
*pset
;
1329 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1333 pset
= find_section_list (padd
->name
, FALSE
);
1337 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1338 if (pset
!= NULL
&& pset
->set_flags
)
1339 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1341 /* bfd_make_section_with_flags() does not return very helpful
1342 error codes, so check for the most likely user error first. */
1343 if (bfd_get_section_by_name (obfd
, padd
->name
))
1345 non_fatal (_("can't add section '%s' - it already exists!"), padd
->name
);
1350 padd
->section
= bfd_make_section_with_flags (obfd
, padd
->name
, flags
);
1351 if (padd
->section
== NULL
)
1353 non_fatal (_("can't create section `%s': %s"),
1354 padd
->name
, bfd_errmsg (bfd_get_error ()));
1359 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1361 bfd_nonfatal (bfd_get_filename (obfd
));
1367 if (pset
->change_vma
!= CHANGE_IGNORE
)
1368 if (! bfd_set_section_vma (obfd
, padd
->section
,
1371 bfd_nonfatal (bfd_get_filename (obfd
));
1375 if (pset
->change_lma
!= CHANGE_IGNORE
)
1377 padd
->section
->lma
= pset
->lma_val
;
1379 if (! bfd_set_section_alignment
1380 (obfd
, padd
->section
,
1381 bfd_section_alignment (obfd
, padd
->section
)))
1383 bfd_nonfatal (bfd_get_filename (obfd
));
1391 if (gnu_debuglink_filename
!= NULL
)
1393 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1394 (obfd
, gnu_debuglink_filename
);
1396 if (gnu_debuglink_section
== NULL
)
1398 bfd_nonfatal (gnu_debuglink_filename
);
1402 /* Special processing for PE format files. We
1403 have no way to distinguish PE from COFF here. */
1404 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
)
1406 bfd_vma debuglink_vma
;
1407 asection
* highest_section
;
1410 /* The PE spec requires that all sections be adjacent and sorted
1411 in ascending order of VMA. It also specifies that debug
1412 sections should be last. This is despite the fact that debug
1413 sections are not loaded into memory and so in theory have no
1416 This means that the debuglink section must be given a non-zero
1417 VMA which makes it contiguous with other debug sections. So
1418 walk the current section list, find the section with the
1419 highest VMA and start the debuglink section after that one. */
1420 for (sec
= obfd
->sections
, highest_section
= NULL
;
1424 && (highest_section
== NULL
1425 || sec
->vma
> highest_section
->vma
))
1426 highest_section
= sec
;
1428 if (highest_section
)
1429 debuglink_vma
= BFD_ALIGN (highest_section
->vma
1430 + highest_section
->size
,
1431 /* FIXME: We ought to be using
1432 COFF_PAGE_SIZE here or maybe
1433 bfd_get_section_alignment() (if it
1434 was set) but since this is for PE
1435 and we know the required alignment
1436 it is easier just to hard code it. */
1439 /* Umm, not sure what to do in this case. */
1440 debuglink_vma
= 0x1000;
1442 bfd_set_section_vma (obfd
, gnu_debuglink_section
, debuglink_vma
);
1446 if (bfd_count_sections (obfd
) == 0)
1448 non_fatal (_("there are no sections to be copied!"));
1452 if (gap_fill_set
|| pad_to_set
)
1457 /* We must fill in gaps between the sections and/or we must pad
1458 the last section to a specified address. We do this by
1459 grabbing a list of the sections, sorting them by VMA, and
1460 increasing the section sizes as required to fill the gaps.
1461 We write out the gap contents below. */
1463 c
= bfd_count_sections (obfd
);
1464 osections
= xmalloc (c
* sizeof (asection
*));
1466 bfd_map_over_sections (obfd
, get_sections
, &set
);
1468 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1470 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1471 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1475 for (i
= 0; i
< c
- 1; i
++)
1479 bfd_vma gap_start
, gap_stop
;
1481 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1482 if ((flags
& SEC_HAS_CONTENTS
) == 0
1483 || (flags
& SEC_LOAD
) == 0)
1486 size
= bfd_section_size (obfd
, osections
[i
]);
1487 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1488 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1489 if (gap_start
< gap_stop
)
1491 if (! bfd_set_section_size (obfd
, osections
[i
],
1492 size
+ (gap_stop
- gap_start
)))
1494 non_fatal (_("Can't fill gap after %s: %s"),
1495 bfd_get_section_name (obfd
, osections
[i
]),
1496 bfd_errmsg (bfd_get_error ()));
1500 gaps
[i
] = gap_stop
- gap_start
;
1501 if (max_gap
< gap_stop
- gap_start
)
1502 max_gap
= gap_stop
- gap_start
;
1512 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1513 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1514 if (lma
+ size
< pad_to
)
1516 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1519 non_fatal (_("Can't add padding to %s: %s"),
1520 bfd_get_section_name (obfd
, osections
[c
- 1]),
1521 bfd_errmsg (bfd_get_error ()));
1526 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1527 if (max_gap
< pad_to
- (lma
+ size
))
1528 max_gap
= pad_to
- (lma
+ size
);
1534 /* Symbol filtering must happen after the output sections
1535 have been created, but before their contents are set. */
1537 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1540 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1544 osympp
= isympp
= xmalloc (symsize
);
1545 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1548 bfd_nonfatal (bfd_get_filename (ibfd
));
1552 if (convert_debugging
)
1553 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1555 if (strip_symbols
== STRIP_DEBUG
1556 || strip_symbols
== STRIP_ALL
1557 || strip_symbols
== STRIP_UNNEEDED
1558 || strip_symbols
== STRIP_NONDEBUG
1559 || discard_locals
!= LOCALS_UNDEF
1561 || strip_specific_list
!= NULL
1562 || keep_specific_list
!= NULL
1563 || localize_specific_list
!= NULL
1564 || globalize_specific_list
!= NULL
1565 || keepglobal_specific_list
!= NULL
1566 || weaken_specific_list
!= NULL
1567 || prefix_symbols_string
1570 || convert_debugging
1571 || change_leading_char
1572 || remove_leading_char
1573 || redefine_sym_list
1576 /* Mark symbols used in output relocations so that they
1577 are kept, even if they are local labels or static symbols.
1579 Note we iterate over the input sections examining their
1580 relocations since the relocations for the output sections
1581 haven't been set yet. mark_symbols_used_in_relocations will
1582 ignore input sections which have no corresponding output
1584 if (strip_symbols
!= STRIP_ALL
)
1585 bfd_map_over_sections (ibfd
,
1586 mark_symbols_used_in_relocations
,
1588 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1589 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1592 if (convert_debugging
&& dhandle
!= NULL
)
1594 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1601 bfd_set_symtab (obfd
, osympp
, symcount
);
1603 /* This has to happen after the symbol table has been set. */
1604 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1606 if (add_sections
!= NULL
)
1608 struct section_add
*padd
;
1610 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1612 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1615 bfd_nonfatal (bfd_get_filename (obfd
));
1621 if (gnu_debuglink_filename
!= NULL
)
1623 if (! bfd_fill_in_gnu_debuglink_section
1624 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1626 bfd_nonfatal (gnu_debuglink_filename
);
1631 if (gap_fill_set
|| pad_to_set
)
1636 /* Fill in the gaps. */
1639 buf
= xmalloc (max_gap
);
1640 memset (buf
, gap_fill
, max_gap
);
1642 c
= bfd_count_sections (obfd
);
1643 for (i
= 0; i
< c
; i
++)
1651 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1662 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1665 bfd_nonfatal (bfd_get_filename (obfd
));
1676 /* Allow the BFD backend to copy any private data it understands
1677 from the input BFD to the output BFD. This is done last to
1678 permit the routine to look at the filtered symbol table, which is
1679 important for the ECOFF code at least. */
1680 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1681 && strip_symbols
== STRIP_NONDEBUG
)
1682 /* Do not copy the private data when creating an ELF format
1683 debug info file. We do not want the program headers. */
1685 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1687 non_fatal (_("%s: error copying private BFD data: %s"),
1688 bfd_get_filename (obfd
),
1689 bfd_errmsg (bfd_get_error ()));
1693 /* Switch to the alternate machine code. We have to do this at the
1694 very end, because we only initialize the header when we create
1695 the first section. */
1696 if (use_alt_mach_code
!= 0)
1698 if (! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1700 non_fatal (_("this target does not support %lu alternative machine codes"),
1702 if (bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1704 non_fatal (_("treating that number as an absolute e_machine value instead"));
1705 elf_elfheader (obfd
)->e_machine
= use_alt_mach_code
;
1708 non_fatal (_("ignoring the alternative value"));
1716 #if defined (_WIN32) && !defined (__CYGWIN32__)
1717 #define MKDIR(DIR, MODE) mkdir (DIR)
1719 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1722 /* Read each archive element in turn from IBFD, copy the
1723 contents to temp file, and keep the temp file handle. */
1726 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1730 struct name_list
*next
;
1734 bfd
**ptr
= &obfd
->archive_head
;
1736 char *dir
= make_tempname (bfd_get_filename (obfd
));
1738 /* Make a temp directory to hold the contents. */
1739 if (MKDIR (dir
, 0700) != 0)
1740 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1741 dir
, strerror (errno
));
1743 obfd
->has_armap
= ibfd
->has_armap
;
1747 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1749 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1750 RETURN_NONFATAL (bfd_get_filename (obfd
));
1752 while (!status
&& this_element
!= NULL
)
1758 int stat_status
= 0;
1759 bfd_boolean
delete = TRUE
;
1761 /* Create an output file for this member. */
1762 output_name
= concat (dir
, "/",
1763 bfd_get_filename (this_element
), (char *) 0);
1765 /* If the file already exists, make another temp dir. */
1766 if (stat (output_name
, &buf
) >= 0)
1768 output_name
= make_tempname (output_name
);
1769 if (MKDIR (output_name
, 0700) != 0)
1770 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1771 output_name
, strerror (errno
));
1773 l
= xmalloc (sizeof (struct name_list
));
1774 l
->name
= output_name
;
1778 output_name
= concat (output_name
, "/",
1779 bfd_get_filename (this_element
), (char *) 0);
1782 output_bfd
= bfd_openw (output_name
, output_target
);
1785 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1787 if (stat_status
!= 0)
1788 non_fatal (_("internal stat error on %s"),
1789 bfd_get_filename (this_element
));
1792 l
= xmalloc (sizeof (struct name_list
));
1793 l
->name
= output_name
;
1798 if (output_bfd
== NULL
)
1799 RETURN_NONFATAL (output_name
);
1801 if (bfd_check_format (this_element
, bfd_object
))
1803 delete = ! copy_object (this_element
, output_bfd
);
1806 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
1808 if (!bfd_close (output_bfd
))
1810 bfd_nonfatal (bfd_get_filename (output_bfd
));
1811 /* Error in new object file. Don't change archive. */
1816 goto copy_unknown_element
;
1820 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1821 bfd_get_archive_filename (this_element
));
1823 copy_unknown_element
:
1824 delete = !copy_unknown_object (this_element
, output_bfd
);
1825 if (!bfd_close_all_done (output_bfd
))
1827 bfd_nonfatal (bfd_get_filename (output_bfd
));
1828 /* Error in new object file. Don't change archive. */
1835 unlink (output_name
);
1840 if (preserve_dates
&& stat_status
== 0)
1841 set_times (output_name
, &buf
);
1843 /* Open the newly output file and attach to our list. */
1844 output_bfd
= bfd_openr (output_name
, output_target
);
1846 l
->obfd
= output_bfd
;
1849 ptr
= &output_bfd
->next
;
1851 last_element
= this_element
;
1853 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1855 bfd_close (last_element
);
1860 if (!bfd_close (obfd
))
1861 RETURN_NONFATAL (bfd_get_filename (obfd
));
1863 if (!bfd_close (ibfd
))
1864 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1866 /* Delete all the files that we opened. */
1867 for (l
= list
; l
!= NULL
; l
= l
->next
)
1869 if (l
->obfd
== NULL
)
1873 bfd_close (l
->obfd
);
1880 /* The top-level control. */
1883 copy_file (const char *input_filename
, const char *output_filename
,
1884 const char *input_target
, const char *output_target
)
1887 char **obj_matching
;
1888 char **core_matching
;
1890 if (get_file_size (input_filename
) < 1)
1892 non_fatal (_("error: the input file '%s' is empty"), input_filename
);
1897 /* To allow us to do "strip *" without dying on the first
1898 non-object file, failures are nonfatal. */
1899 ibfd
= bfd_openr (input_filename
, input_target
);
1901 RETURN_NONFATAL (input_filename
);
1903 if (bfd_check_format (ibfd
, bfd_archive
))
1907 /* bfd_get_target does not return the correct value until
1908 bfd_check_format succeeds. */
1909 if (output_target
== NULL
)
1910 output_target
= bfd_get_target (ibfd
);
1912 obfd
= bfd_openw (output_filename
, output_target
);
1914 RETURN_NONFATAL (output_filename
);
1916 copy_archive (ibfd
, obfd
, output_target
);
1918 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1923 /* bfd_get_target does not return the correct value until
1924 bfd_check_format succeeds. */
1925 if (output_target
== NULL
)
1926 output_target
= bfd_get_target (ibfd
);
1928 obfd
= bfd_openw (output_filename
, output_target
);
1930 RETURN_NONFATAL (output_filename
);
1932 if (! copy_object (ibfd
, obfd
))
1935 if (!bfd_close (obfd
))
1936 RETURN_NONFATAL (output_filename
);
1938 if (!bfd_close (ibfd
))
1939 RETURN_NONFATAL (input_filename
);
1944 bfd_error_type obj_error
= bfd_get_error ();
1945 bfd_error_type core_error
;
1947 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1949 /* This probably can't happen.. */
1950 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1951 free (obj_matching
);
1955 core_error
= bfd_get_error ();
1956 /* Report the object error in preference to the core error. */
1957 if (obj_error
!= core_error
)
1958 bfd_set_error (obj_error
);
1960 bfd_nonfatal (input_filename
);
1962 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1964 list_matching_formats (obj_matching
);
1965 free (obj_matching
);
1967 if (core_error
== bfd_error_file_ambiguously_recognized
)
1969 list_matching_formats (core_matching
);
1970 free (core_matching
);
1977 /* Add a name to the section renaming list. */
1980 add_section_rename (const char * old_name
, const char * new_name
,
1983 section_rename
* rename
;
1985 /* Check for conflicts first. */
1986 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1987 if (strcmp (rename
->old_name
, old_name
) == 0)
1989 /* Silently ignore duplicate definitions. */
1990 if (strcmp (rename
->new_name
, new_name
) == 0
1991 && rename
->flags
== flags
)
1994 fatal (_("Multiple renames of section %s"), old_name
);
1997 rename
= xmalloc (sizeof (* rename
));
1999 rename
->old_name
= old_name
;
2000 rename
->new_name
= new_name
;
2001 rename
->flags
= flags
;
2002 rename
->next
= section_rename_list
;
2004 section_rename_list
= rename
;
2007 /* Check the section rename list for a new name of the input section
2008 ISECTION. Return the new name if one is found.
2009 Also set RETURNED_FLAGS to the flags to be used for this section. */
2012 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
2013 flagword
* returned_flags
)
2015 const char * old_name
= bfd_section_name (ibfd
, isection
);
2016 section_rename
* rename
;
2018 /* Default to using the flags of the input section. */
2019 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
2021 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
2022 if (strcmp (rename
->old_name
, old_name
) == 0)
2024 if (rename
->flags
!= (flagword
) -1)
2025 * returned_flags
= rename
->flags
;
2027 return rename
->new_name
;
2033 /* Once each of the sections is copied, we may still need to do some
2034 finalization work for private section headers. Do that here. */
2037 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
2041 /* Allow the BFD backend to copy any private data it understands
2042 from the input section to the output section. */
2043 if (! bfd_copy_private_header_data (ibfd
, obfd
))
2045 err
= _("private header data");
2049 /* All went well. */
2053 non_fatal (_("%s: error in %s: %s"),
2054 bfd_get_filename (ibfd
),
2055 err
, bfd_errmsg (bfd_get_error ()));
2059 /* Create a section in OBFD with the same
2060 name and attributes as ISECTION in IBFD. */
2063 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2065 bfd
*obfd
= obfdarg
;
2066 struct section_list
*p
;
2074 char *prefix
= NULL
;
2076 if (is_strip_section (ibfd
, isection
))
2079 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
2083 /* Get the, possibly new, name of the output section. */
2084 name
= find_section_rename (ibfd
, isection
, & flags
);
2086 /* Prefix sections. */
2087 if ((prefix_alloc_sections_string
)
2088 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
2089 prefix
= prefix_alloc_sections_string
;
2090 else if (prefix_sections_string
)
2091 prefix
= prefix_sections_string
;
2097 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
2103 if (p
!= NULL
&& p
->set_flags
)
2104 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2105 else if (strip_symbols
== STRIP_NONDEBUG
&& (flags
& SEC_ALLOC
) != 0)
2106 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2108 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2110 if (osection
== NULL
)
2116 if (strip_symbols
== STRIP_NONDEBUG
2117 && obfd
->xvec
->flavour
== bfd_target_elf_flavour
2118 && (flags
& SEC_ALLOC
) != 0
2119 && (p
== NULL
|| !p
->set_flags
))
2120 elf_section_type (osection
) = SHT_NOBITS
;
2122 size
= bfd_section_size (ibfd
, isection
);
2124 size
= (size
+ interleave
- 1) / interleave
;
2125 if (! bfd_set_section_size (obfd
, osection
, size
))
2131 vma
= bfd_section_vma (ibfd
, isection
);
2132 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2134 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2137 vma
+= change_section_address
;
2139 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2145 lma
= isection
->lma
;
2146 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2148 if (p
->change_lma
== CHANGE_MODIFY
)
2150 else if (p
->change_lma
== CHANGE_SET
)
2156 lma
+= change_section_address
;
2158 osection
->lma
= lma
;
2160 /* FIXME: This is probably not enough. If we change the LMA we
2161 may have to recompute the header for the file as well. */
2162 if (!bfd_set_section_alignment (obfd
,
2164 bfd_section_alignment (ibfd
, isection
)))
2166 err
= _("alignment");
2170 /* Copy merge entity size. */
2171 osection
->entsize
= isection
->entsize
;
2173 /* This used to be mangle_section; we do here to avoid using
2174 bfd_get_section_by_name since some formats allow multiple
2175 sections with the same name. */
2176 isection
->output_section
= osection
;
2177 isection
->output_offset
= 0;
2179 /* Allow the BFD backend to copy any private data it understands
2180 from the input section to the output section. */
2181 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
2182 && strip_symbols
== STRIP_NONDEBUG
)
2183 /* Do not copy the private data when creating an ELF format
2184 debug info file. We do not want the program headers. */
2186 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2188 err
= _("private data");
2192 /* All went well. */
2196 non_fatal (_("%s: section `%s': error in %s: %s"),
2197 bfd_get_filename (ibfd
),
2198 bfd_section_name (ibfd
, isection
),
2199 err
, bfd_errmsg (bfd_get_error ()));
2203 /* Copy the data of input section ISECTION of IBFD
2204 to an output section with the same name in OBFD.
2205 If stripping then don't copy any relocation info. */
2208 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2210 bfd
*obfd
= obfdarg
;
2211 struct section_list
*p
;
2219 /* If we have already failed earlier on,
2220 do not keep on generating complaints now. */
2224 if (is_strip_section (ibfd
, isection
))
2227 flags
= bfd_get_section_flags (ibfd
, isection
);
2228 if ((flags
& SEC_GROUP
) != 0)
2231 osection
= isection
->output_section
;
2232 size
= bfd_get_section_size (isection
);
2234 if (size
== 0 || osection
== 0)
2237 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2239 /* Core files do not need to be relocated. */
2240 if (bfd_get_format (obfd
) == bfd_core
)
2244 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2248 /* Do not complain if the target does not support relocations. */
2249 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2252 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2257 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2260 relpp
= xmalloc (relsize
);
2261 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2263 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2265 if (strip_symbols
== STRIP_ALL
)
2267 /* Remove relocations which are not in
2268 keep_strip_specific_list. */
2269 arelent
**temp_relpp
;
2270 long temp_relcount
= 0;
2273 temp_relpp
= xmalloc (relsize
);
2274 for (i
= 0; i
< relcount
; i
++)
2275 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2276 keep_specific_list
))
2277 temp_relpp
[temp_relcount
++] = relpp
[i
];
2278 relcount
= temp_relcount
;
2283 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2288 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2289 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2291 void *memhunk
= xmalloc (size
);
2293 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2294 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2298 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2299 char *from
= (char *) memhunk
+ copy_byte
;
2301 char *end
= (char *) memhunk
+ size
;
2303 for (; from
< end
; from
+= interleave
)
2306 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2307 osection
->lma
/= interleave
;
2310 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2311 RETURN_NONFATAL (bfd_get_filename (obfd
));
2315 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2317 void *memhunk
= xmalloc (size
);
2319 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2320 flag--they can just remove the section entirely and add it
2321 back again. However, we do permit them to turn on the
2322 SEC_HAS_CONTENTS flag, and take it to mean that the section
2323 contents should be zeroed out. */
2325 memset (memhunk
, 0, size
);
2326 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2327 RETURN_NONFATAL (bfd_get_filename (obfd
));
2332 /* Get all the sections. This is used when --gap-fill or --pad-to is
2336 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2338 asection
***secppp
= secppparg
;
2340 **secppp
= osection
;
2344 /* Sort sections by VMA. This is called via qsort, and is used when
2345 --gap-fill or --pad-to is used. We force non loadable or empty
2346 sections to the front, where they are easier to ignore. */
2349 compare_section_lma (const void *arg1
, const void *arg2
)
2351 const asection
*const *sec1
= arg1
;
2352 const asection
*const *sec2
= arg2
;
2353 flagword flags1
, flags2
;
2355 /* Sort non loadable sections to the front. */
2356 flags1
= (*sec1
)->flags
;
2357 flags2
= (*sec2
)->flags
;
2358 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2359 || (flags1
& SEC_LOAD
) == 0)
2361 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2362 && (flags2
& SEC_LOAD
) != 0)
2367 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2368 || (flags2
& SEC_LOAD
) == 0)
2372 /* Sort sections by LMA. */
2373 if ((*sec1
)->lma
> (*sec2
)->lma
)
2375 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2378 /* Sort sections with the same LMA by size. */
2379 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2381 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2387 /* Mark all the symbols which will be used in output relocations with
2388 the BSF_KEEP flag so that those symbols will not be stripped.
2390 Ignore relocations which will not appear in the output file. */
2393 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2395 asymbol
**symbols
= symbolsarg
;
2400 /* Ignore an input section with no corresponding output section. */
2401 if (isection
->output_section
== NULL
)
2404 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2407 /* Do not complain if the target does not support relocations. */
2408 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2410 bfd_fatal (bfd_get_filename (ibfd
));
2416 relpp
= xmalloc (relsize
);
2417 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2419 bfd_fatal (bfd_get_filename (ibfd
));
2421 /* Examine each symbol used in a relocation. If it's not one of the
2422 special bfd section symbols, then mark it with BSF_KEEP. */
2423 for (i
= 0; i
< relcount
; i
++)
2425 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2426 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2427 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2428 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2435 /* Write out debugging information. */
2438 write_debugging_info (bfd
*obfd
, void *dhandle
,
2439 long *symcountp ATTRIBUTE_UNUSED
,
2440 asymbol
***symppp ATTRIBUTE_UNUSED
)
2442 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2443 return write_ieee_debugging_info (obfd
, dhandle
);
2445 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2446 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2448 bfd_byte
*syms
, *strings
;
2449 bfd_size_type symsize
, stringsize
;
2450 asection
*stabsec
, *stabstrsec
;
2453 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2458 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2459 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2460 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2462 || stabstrsec
== NULL
2463 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2464 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2465 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2466 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2468 non_fatal (_("%s: can't create debugging section: %s"),
2469 bfd_get_filename (obfd
),
2470 bfd_errmsg (bfd_get_error ()));
2474 /* We can get away with setting the section contents now because
2475 the next thing the caller is going to do is copy over the
2476 real sections. We may someday have to split the contents
2477 setting out of this function. */
2478 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2479 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2482 non_fatal (_("%s: can't set debugging section contents: %s"),
2483 bfd_get_filename (obfd
),
2484 bfd_errmsg (bfd_get_error ()));
2491 non_fatal (_("%s: don't know how to write debugging information for %s"),
2492 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2497 strip_main (int argc
, char *argv
[])
2499 char *input_target
= NULL
;
2500 char *output_target
= NULL
;
2501 bfd_boolean show_version
= FALSE
;
2502 bfd_boolean formats_info
= FALSE
;
2505 struct section_list
*p
;
2506 char *output_file
= NULL
;
2508 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2509 strip_options
, (int *) 0)) != EOF
)
2514 input_target
= optarg
;
2517 output_target
= optarg
;
2520 input_target
= output_target
= optarg
;
2523 p
= find_section_list (optarg
, TRUE
);
2525 sections_removed
= TRUE
;
2528 strip_symbols
= STRIP_ALL
;
2532 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2533 strip_symbols
= STRIP_DEBUG
;
2535 case OPTION_STRIP_UNNEEDED
:
2536 strip_symbols
= STRIP_UNNEEDED
;
2539 add_specific_symbol (optarg
, &keep_specific_list
);
2542 add_specific_symbol (optarg
, &strip_specific_list
);
2545 output_file
= optarg
;
2548 preserve_dates
= TRUE
;
2551 discard_locals
= LOCALS_ALL
;
2554 discard_locals
= LOCALS_START_L
;
2560 show_version
= TRUE
;
2562 case OPTION_FORMATS_INFO
:
2563 formats_info
= TRUE
;
2565 case OPTION_ONLY_KEEP_DEBUG
:
2566 strip_symbols
= STRIP_NONDEBUG
;
2568 case OPTION_KEEP_FILE_SYMBOLS
:
2569 keep_file_symbols
= 1;
2572 /* We've been given a long option. */
2579 strip_usage (stdout
, 0);
2581 strip_usage (stderr
, 1);
2592 print_version ("strip");
2594 /* Default is to strip all symbols. */
2595 if (strip_symbols
== STRIP_UNDEF
2596 && discard_locals
== LOCALS_UNDEF
2597 && strip_specific_list
== NULL
)
2598 strip_symbols
= STRIP_ALL
;
2600 if (output_target
== NULL
)
2601 output_target
= input_target
;
2605 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2606 strip_usage (stderr
, 1);
2608 for (; i
< argc
; i
++)
2610 int hold_status
= status
;
2611 struct stat statbuf
;
2614 if (get_file_size (argv
[i
]) < 1)
2618 /* No need to check the return value of stat().
2619 It has already been checked in get_file_size(). */
2620 stat (argv
[i
], &statbuf
);
2622 if (output_file
!= NULL
)
2623 tmpname
= output_file
;
2625 tmpname
= make_tempname (argv
[i
]);
2628 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2632 set_times (tmpname
, &statbuf
);
2633 if (output_file
== NULL
)
2634 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2635 status
= hold_status
;
2638 unlink_if_ordinary (tmpname
);
2639 if (output_file
== NULL
)
2647 copy_main (int argc
, char *argv
[])
2649 char * binary_architecture
= NULL
;
2650 char *input_filename
= NULL
;
2651 char *output_filename
= NULL
;
2652 char *input_target
= NULL
;
2653 char *output_target
= NULL
;
2654 bfd_boolean show_version
= FALSE
;
2655 bfd_boolean change_warn
= TRUE
;
2656 bfd_boolean formats_info
= FALSE
;
2658 struct section_list
*p
;
2659 struct stat statbuf
;
2661 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2662 copy_options
, (int *) 0)) != EOF
)
2667 copy_byte
= atoi (optarg
);
2669 fatal (_("byte number must be non-negative"));
2673 binary_architecture
= optarg
;
2677 interleave
= atoi (optarg
);
2679 fatal (_("interleave must be positive"));
2683 case 's': /* "source" - 'I' is preferred */
2684 input_target
= optarg
;
2688 case 'd': /* "destination" - 'O' is preferred */
2689 output_target
= optarg
;
2693 input_target
= output_target
= optarg
;
2697 p
= find_section_list (optarg
, TRUE
);
2699 fatal (_("%s both copied and removed"), optarg
);
2701 sections_copied
= TRUE
;
2705 p
= find_section_list (optarg
, TRUE
);
2707 fatal (_("%s both copied and removed"), optarg
);
2709 sections_removed
= TRUE
;
2713 strip_symbols
= STRIP_ALL
;
2717 strip_symbols
= STRIP_DEBUG
;
2720 case OPTION_STRIP_UNNEEDED
:
2721 strip_symbols
= STRIP_UNNEEDED
;
2724 case OPTION_ONLY_KEEP_DEBUG
:
2725 strip_symbols
= STRIP_NONDEBUG
;
2728 case OPTION_KEEP_FILE_SYMBOLS
:
2729 keep_file_symbols
= 1;
2732 case OPTION_ADD_GNU_DEBUGLINK
:
2733 gnu_debuglink_filename
= optarg
;
2737 add_specific_symbol (optarg
, &keep_specific_list
);
2741 add_specific_symbol (optarg
, &strip_specific_list
);
2744 case OPTION_STRIP_UNNEEDED_SYMBOL
:
2745 add_specific_symbol (optarg
, &strip_unneeded_list
);
2749 add_specific_symbol (optarg
, &localize_specific_list
);
2752 case OPTION_GLOBALIZE_SYMBOL
:
2753 add_specific_symbol (optarg
, &globalize_specific_list
);
2757 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2761 add_specific_symbol (optarg
, &weaken_specific_list
);
2765 preserve_dates
= TRUE
;
2773 discard_locals
= LOCALS_ALL
;
2777 discard_locals
= LOCALS_START_L
;
2785 show_version
= TRUE
;
2788 case OPTION_FORMATS_INFO
:
2789 formats_info
= TRUE
;
2796 case OPTION_ADD_SECTION
:
2800 struct section_add
*pa
;
2805 s
= strchr (optarg
, '=');
2808 fatal (_("bad format for %s"), "--add-section");
2810 size
= get_file_size (s
+ 1);
2814 pa
= xmalloc (sizeof (struct section_add
));
2817 name
= xmalloc (len
+ 1);
2818 strncpy (name
, optarg
, len
);
2822 pa
->filename
= s
+ 1;
2824 pa
->contents
= xmalloc (size
);
2826 f
= fopen (pa
->filename
, FOPEN_RB
);
2829 fatal (_("cannot open: %s: %s"),
2830 pa
->filename
, strerror (errno
));
2832 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2834 fatal (_("%s: fread failed"), pa
->filename
);
2838 pa
->next
= add_sections
;
2843 case OPTION_CHANGE_START
:
2844 change_start
= parse_vma (optarg
, "--change-start");
2847 case OPTION_CHANGE_SECTION_ADDRESS
:
2848 case OPTION_CHANGE_SECTION_LMA
:
2849 case OPTION_CHANGE_SECTION_VMA
:
2854 char *option
= NULL
;
2856 enum change_action what
= CHANGE_IGNORE
;
2860 case OPTION_CHANGE_SECTION_ADDRESS
:
2861 option
= "--change-section-address";
2863 case OPTION_CHANGE_SECTION_LMA
:
2864 option
= "--change-section-lma";
2866 case OPTION_CHANGE_SECTION_VMA
:
2867 option
= "--change-section-vma";
2871 s
= strchr (optarg
, '=');
2874 s
= strchr (optarg
, '+');
2877 s
= strchr (optarg
, '-');
2879 fatal (_("bad format for %s"), option
);
2884 name
= xmalloc (len
+ 1);
2885 strncpy (name
, optarg
, len
);
2888 p
= find_section_list (name
, TRUE
);
2890 val
= parse_vma (s
+ 1, option
);
2894 case '=': what
= CHANGE_SET
; break;
2895 case '-': val
= - val
; /* Drop through. */
2896 case '+': what
= CHANGE_MODIFY
; break;
2901 case OPTION_CHANGE_SECTION_ADDRESS
:
2902 p
->change_vma
= what
;
2906 case OPTION_CHANGE_SECTION_LMA
:
2907 p
->change_lma
= what
;
2911 case OPTION_CHANGE_SECTION_VMA
:
2912 p
->change_vma
= what
;
2919 case OPTION_CHANGE_ADDRESSES
:
2920 change_section_address
= parse_vma (optarg
, "--change-addresses");
2921 change_start
= change_section_address
;
2924 case OPTION_CHANGE_WARNINGS
:
2928 case OPTION_CHANGE_LEADING_CHAR
:
2929 change_leading_char
= TRUE
;
2932 case OPTION_DEBUGGING
:
2933 convert_debugging
= TRUE
;
2936 case OPTION_GAP_FILL
:
2938 bfd_vma gap_fill_vma
;
2940 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2941 gap_fill
= (bfd_byte
) gap_fill_vma
;
2942 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2946 sprintf_vma (buff
, gap_fill_vma
);
2948 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2951 gap_fill_set
= TRUE
;
2955 case OPTION_NO_CHANGE_WARNINGS
:
2956 change_warn
= FALSE
;
2960 pad_to
= parse_vma (optarg
, "--pad-to");
2964 case OPTION_REMOVE_LEADING_CHAR
:
2965 remove_leading_char
= TRUE
;
2968 case OPTION_REDEFINE_SYM
:
2970 /* Push this redefinition onto redefine_symbol_list. */
2974 const char *nextarg
;
2975 char *source
, *target
;
2977 s
= strchr (optarg
, '=');
2979 fatal (_("bad format for %s"), "--redefine-sym");
2982 source
= xmalloc (len
+ 1);
2983 strncpy (source
, optarg
, len
);
2987 len
= strlen (nextarg
);
2988 target
= xmalloc (len
+ 1);
2989 strcpy (target
, nextarg
);
2991 redefine_list_append ("--redefine-sym", source
, target
);
2998 case OPTION_REDEFINE_SYMS
:
2999 add_redefine_syms_file (optarg
);
3002 case OPTION_SET_SECTION_FLAGS
:
3008 s
= strchr (optarg
, '=');
3010 fatal (_("bad format for %s"), "--set-section-flags");
3013 name
= xmalloc (len
+ 1);
3014 strncpy (name
, optarg
, len
);
3017 p
= find_section_list (name
, TRUE
);
3019 p
->set_flags
= TRUE
;
3020 p
->flags
= parse_flags (s
+ 1);
3024 case OPTION_RENAME_SECTION
:
3027 const char *eq
, *fl
;
3032 eq
= strchr (optarg
, '=');
3034 fatal (_("bad format for %s"), "--rename-section");
3038 fatal (_("bad format for %s"), "--rename-section");
3040 old_name
= xmalloc (len
+ 1);
3041 strncpy (old_name
, optarg
, len
);
3045 fl
= strchr (eq
, ',');
3048 flags
= parse_flags (fl
+ 1);
3058 fatal (_("bad format for %s"), "--rename-section");
3060 new_name
= xmalloc (len
+ 1);
3061 strncpy (new_name
, eq
, len
);
3064 add_section_rename (old_name
, new_name
, flags
);
3068 case OPTION_SET_START
:
3069 set_start
= parse_vma (optarg
, "--set-start");
3070 set_start_set
= TRUE
;
3073 case OPTION_SREC_LEN
:
3074 Chunk
= parse_vma (optarg
, "--srec-len");
3077 case OPTION_SREC_FORCES3
:
3081 case OPTION_STRIP_SYMBOLS
:
3082 add_specific_symbols (optarg
, &strip_specific_list
);
3085 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
3086 add_specific_symbols (optarg
, &strip_unneeded_list
);
3089 case OPTION_KEEP_SYMBOLS
:
3090 add_specific_symbols (optarg
, &keep_specific_list
);
3093 case OPTION_LOCALIZE_HIDDEN
:
3094 localize_hidden
= TRUE
;
3097 case OPTION_LOCALIZE_SYMBOLS
:
3098 add_specific_symbols (optarg
, &localize_specific_list
);
3101 case OPTION_GLOBALIZE_SYMBOLS
:
3102 add_specific_symbols (optarg
, &globalize_specific_list
);
3105 case OPTION_KEEPGLOBAL_SYMBOLS
:
3106 add_specific_symbols (optarg
, &keepglobal_specific_list
);
3109 case OPTION_WEAKEN_SYMBOLS
:
3110 add_specific_symbols (optarg
, &weaken_specific_list
);
3113 case OPTION_ALT_MACH_CODE
:
3114 use_alt_mach_code
= strtoul (optarg
, NULL
, 0);
3115 if (use_alt_mach_code
== 0)
3116 fatal (_("unable to parse alternative machine code"));
3119 case OPTION_PREFIX_SYMBOLS
:
3120 prefix_symbols_string
= optarg
;
3123 case OPTION_PREFIX_SECTIONS
:
3124 prefix_sections_string
= optarg
;
3127 case OPTION_PREFIX_ALLOC_SECTIONS
:
3128 prefix_alloc_sections_string
= optarg
;
3131 case OPTION_READONLY_TEXT
:
3132 bfd_flags_to_set
|= WP_TEXT
;
3133 bfd_flags_to_clear
&= ~WP_TEXT
;
3136 case OPTION_WRITABLE_TEXT
:
3137 bfd_flags_to_clear
|= WP_TEXT
;
3138 bfd_flags_to_set
&= ~WP_TEXT
;
3142 bfd_flags_to_set
|= D_PAGED
;
3143 bfd_flags_to_clear
&= ~D_PAGED
;
3147 bfd_flags_to_clear
|= D_PAGED
;
3148 bfd_flags_to_set
&= ~D_PAGED
;
3152 /* We've been given a long option. */
3157 copy_usage (stdout
, 0);
3160 copy_usage (stderr
, 1);
3171 print_version ("objcopy");
3173 if (copy_byte
>= interleave
)
3174 fatal (_("byte number must be less than interleave"));
3176 if (optind
== argc
|| optind
+ 2 < argc
)
3177 copy_usage (stderr
, 1);
3179 input_filename
= argv
[optind
];
3180 if (optind
+ 1 < argc
)
3181 output_filename
= argv
[optind
+ 1];
3183 /* Default is to strip no symbols. */
3184 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3185 strip_symbols
= STRIP_NONE
;
3187 if (output_target
== NULL
)
3188 output_target
= input_target
;
3190 if (binary_architecture
!= NULL
)
3192 if (input_target
&& strcmp (input_target
, "binary") == 0)
3194 const bfd_arch_info_type
* temp_arch_info
;
3196 temp_arch_info
= bfd_scan_arch (binary_architecture
);
3198 if (temp_arch_info
!= NULL
)
3200 bfd_external_binary_architecture
= temp_arch_info
->arch
;
3201 bfd_external_machine
= temp_arch_info
->mach
;
3204 fatal (_("architecture %s unknown"), binary_architecture
);
3208 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3209 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3214 if (stat (input_filename
, & statbuf
) < 0)
3215 fatal (_("warning: could not locate '%s'. System error message: %s"),
3216 input_filename
, strerror (errno
));
3218 /* If there is no destination file, or the source and destination files
3219 are the same, then create a temp and rename the result into the input. */
3220 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3222 char *tmpname
= make_tempname (input_filename
);
3224 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3228 set_times (tmpname
, &statbuf
);
3229 smart_rename (tmpname
, input_filename
, preserve_dates
);
3236 copy_file (input_filename
, output_filename
, input_target
, output_target
);
3238 if (status
== 0 && preserve_dates
)
3239 set_times (output_filename
, &statbuf
);
3240 else if (status
!= 0)
3241 unlink_if_ordinary (output_filename
);
3246 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3250 if (p
->change_vma
!= CHANGE_IGNORE
)
3254 sprintf_vma (buff
, p
->vma_val
);
3256 /* xgettext:c-format */
3257 non_fatal (_("%s %s%c0x%s never used"),
3258 "--change-section-vma",
3260 p
->change_vma
== CHANGE_SET
? '=' : '+',
3264 if (p
->change_lma
!= CHANGE_IGNORE
)
3268 sprintf_vma (buff
, p
->lma_val
);
3270 /* xgettext:c-format */
3271 non_fatal (_("%s %s%c0x%s never used"),
3272 "--change-section-lma",
3274 p
->change_lma
== CHANGE_SET
? '=' : '+',
3285 main (int argc
, char *argv
[])
3287 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3288 setlocale (LC_MESSAGES
, "");
3290 #if defined (HAVE_SETLOCALE)
3291 setlocale (LC_CTYPE
, "");
3293 bindtextdomain (PACKAGE
, LOCALEDIR
);
3294 textdomain (PACKAGE
);
3296 program_name
= argv
[0];
3297 xmalloc_set_program_name (program_name
);
3299 START_PROGRESS (program_name
, 0);
3301 expandargv (&argc
, &argv
);
3303 strip_symbols
= STRIP_UNDEF
;
3304 discard_locals
= LOCALS_UNDEF
;
3307 set_default_bfd_target ();
3311 int i
= strlen (program_name
);
3312 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3313 /* Drop the .exe suffix, if any. */
3314 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3317 program_name
[i
] = '\0';
3320 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3324 strip_main (argc
, argv
);
3326 copy_main (argc
, argv
);
3328 END_PROGRESS (program_name
);