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
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"
34 /* A list of symbols to explicitly strip out, or to keep. A linked
35 list is good enough for a small number from the command line, but
36 this will slow things down a lot if many symbols are being
45 /* A list to support redefine_sym. */
50 struct redefine_node
*next
;
53 typedef struct section_rename
55 const char * old_name
;
56 const char * new_name
;
58 struct section_rename
* next
;
62 /* List of sections to be renamed. */
63 static section_rename
*section_rename_list
;
65 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
67 static asymbol
**isympp
= NULL
; /* Input symbols. */
68 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
70 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
71 static int copy_byte
= -1;
72 static int interleave
= 4;
74 static bfd_boolean verbose
; /* Print file and target names. */
75 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
76 static int status
= 0; /* Exit status. */
81 STRIP_NONE
, /* Don't strip. */
82 STRIP_DEBUG
, /* Strip all debugger symbols. */
83 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
84 STRIP_NONDEBUG
, /* Strip everything but debug info. */
85 STRIP_ALL
/* Strip all symbols. */
88 /* Which symbols to remove. */
89 static enum strip_action strip_symbols
;
94 LOCALS_START_L
, /* Discard locals starting with L. */
95 LOCALS_ALL
/* Discard all locals. */
98 /* Which local symbols to remove. Overrides STRIP_ALL. */
99 static enum locals_action discard_locals
;
101 /* What kind of change to perform. */
109 /* Structure used to hold lists of sections and actions to take. */
112 struct section_list
* next
; /* Next section to change. */
113 const char * name
; /* Section name. */
114 bfd_boolean used
; /* Whether this entry was used. */
115 bfd_boolean remove
; /* Whether to remove this section. */
116 bfd_boolean copy
; /* Whether to copy this section. */
117 enum change_action change_vma
;/* Whether to change or set VMA. */
118 bfd_vma vma_val
; /* Amount to change by or set to. */
119 enum change_action change_lma
;/* Whether to change or set LMA. */
120 bfd_vma lma_val
; /* Amount to change by or set to. */
121 bfd_boolean set_flags
; /* Whether to set the section flags. */
122 flagword flags
; /* What to set the section flags to. */
125 static struct section_list
*change_sections
;
127 /* TRUE if some sections are to be removed. */
128 static bfd_boolean sections_removed
;
130 /* TRUE if only some sections are to be copied. */
131 static bfd_boolean sections_copied
;
133 /* Changes to the start address. */
134 static bfd_vma change_start
= 0;
135 static bfd_boolean set_start_set
= FALSE
;
136 static bfd_vma set_start
;
138 /* Changes to section addresses. */
139 static bfd_vma change_section_address
= 0;
141 /* Filling gaps between sections. */
142 static bfd_boolean gap_fill_set
= FALSE
;
143 static bfd_byte gap_fill
= 0;
145 /* Pad to a given address. */
146 static bfd_boolean pad_to_set
= FALSE
;
147 static bfd_vma pad_to
;
149 /* Use alternate machine code? */
150 static int use_alt_mach_code
= 0;
152 /* Output BFD flags user wants to set or clear */
153 static flagword bfd_flags_to_set
;
154 static flagword bfd_flags_to_clear
;
156 /* List of sections to add. */
159 /* Next section to add. */
160 struct section_add
*next
;
161 /* Name of section to add. */
163 /* Name of file holding section contents. */
164 const char *filename
;
167 /* Contents of file. */
169 /* BFD section, after it has been added. */
173 /* List of sections to add to the output BFD. */
174 static struct section_add
*add_sections
;
176 /* If non-NULL the argument to --add-gnu-debuglink.
177 This should be the filename to store in the .gnu_debuglink section. */
178 static const char * gnu_debuglink_filename
= NULL
;
180 /* Whether to convert debugging information. */
181 static bfd_boolean convert_debugging
= FALSE
;
183 /* Whether to change the leading character in symbol names. */
184 static bfd_boolean change_leading_char
= FALSE
;
186 /* Whether to remove the leading character from global symbol names. */
187 static bfd_boolean remove_leading_char
= FALSE
;
189 /* Whether to permit wildcard in symbol comparison. */
190 static bfd_boolean wildcard
= FALSE
;
192 /* List of symbols to strip, keep, localize, keep-global, weaken,
194 static struct symlist
*strip_specific_list
= NULL
;
195 static struct symlist
*strip_unneeded_list
= NULL
;
196 static struct symlist
*keep_specific_list
= NULL
;
197 static struct symlist
*localize_specific_list
= NULL
;
198 static struct symlist
*globalize_specific_list
= NULL
;
199 static struct symlist
*keepglobal_specific_list
= NULL
;
200 static struct symlist
*weaken_specific_list
= NULL
;
201 static struct redefine_node
*redefine_sym_list
= NULL
;
203 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
204 static bfd_boolean weaken
= FALSE
;
206 /* If this is TRUE, we retain BSF_FILE symbols. */
207 static bfd_boolean keep_file_symbols
= FALSE
;
209 /* Prefix symbols/sections. */
210 static char *prefix_symbols_string
= 0;
211 static char *prefix_sections_string
= 0;
212 static char *prefix_alloc_sections_string
= 0;
214 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
215 enum command_line_switch
217 OPTION_ADD_SECTION
=150,
218 OPTION_CHANGE_ADDRESSES
,
219 OPTION_CHANGE_LEADING_CHAR
,
221 OPTION_CHANGE_SECTION_ADDRESS
,
222 OPTION_CHANGE_SECTION_LMA
,
223 OPTION_CHANGE_SECTION_VMA
,
224 OPTION_CHANGE_WARNINGS
,
227 OPTION_NO_CHANGE_WARNINGS
,
229 OPTION_REMOVE_LEADING_CHAR
,
230 OPTION_SET_SECTION_FLAGS
,
232 OPTION_STRIP_UNNEEDED
,
235 OPTION_REDEFINE_SYMS
,
238 OPTION_STRIP_SYMBOLS
,
239 OPTION_STRIP_UNNEEDED_SYMBOL
,
240 OPTION_STRIP_UNNEEDED_SYMBOLS
,
242 OPTION_LOCALIZE_SYMBOLS
,
243 OPTION_GLOBALIZE_SYMBOL
,
244 OPTION_GLOBALIZE_SYMBOLS
,
245 OPTION_KEEPGLOBAL_SYMBOLS
,
246 OPTION_WEAKEN_SYMBOLS
,
247 OPTION_RENAME_SECTION
,
248 OPTION_ALT_MACH_CODE
,
249 OPTION_PREFIX_SYMBOLS
,
250 OPTION_PREFIX_SECTIONS
,
251 OPTION_PREFIX_ALLOC_SECTIONS
,
253 OPTION_ADD_GNU_DEBUGLINK
,
254 OPTION_ONLY_KEEP_DEBUG
,
255 OPTION_KEEP_FILE_SYMBOLS
,
256 OPTION_READONLY_TEXT
,
257 OPTION_WRITABLE_TEXT
,
262 /* Options to handle if running as "strip". */
264 static struct option strip_options
[] =
266 {"discard-all", no_argument
, 0, 'x'},
267 {"discard-locals", no_argument
, 0, 'X'},
268 {"format", required_argument
, 0, 'F'}, /* Obsolete */
269 {"help", no_argument
, 0, 'h'},
270 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
271 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
272 {"input-target", required_argument
, 0, 'I'},
273 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
274 {"keep-symbol", required_argument
, 0, 'K'},
275 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
276 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
277 {"output-target", required_argument
, 0, 'O'},
278 {"output-file", required_argument
, 0, 'o'},
279 {"preserve-dates", no_argument
, 0, 'p'},
280 {"remove-section", required_argument
, 0, 'R'},
281 {"strip-all", no_argument
, 0, 's'},
282 {"strip-debug", no_argument
, 0, 'S'},
283 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
284 {"strip-symbol", required_argument
, 0, 'N'},
285 {"target", required_argument
, 0, 'F'},
286 {"verbose", no_argument
, 0, 'v'},
287 {"version", no_argument
, 0, 'V'},
288 {"wildcard", no_argument
, 0, 'w'},
289 {0, no_argument
, 0, 0}
292 /* Options to handle if running as "objcopy". */
294 static struct option copy_options
[] =
296 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
297 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
298 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
299 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
300 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
301 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
302 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
303 {"binary-architecture", required_argument
, 0, 'B'},
304 {"byte", required_argument
, 0, 'b'},
305 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
306 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
307 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
308 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
309 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
310 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
311 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
312 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
313 {"discard-all", no_argument
, 0, 'x'},
314 {"discard-locals", no_argument
, 0, 'X'},
315 {"format", required_argument
, 0, 'F'}, /* Obsolete */
316 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
317 {"globalize-symbol", required_argument
, 0, OPTION_GLOBALIZE_SYMBOL
},
318 {"globalize-symbols", required_argument
, 0, OPTION_GLOBALIZE_SYMBOLS
},
319 {"help", no_argument
, 0, 'h'},
320 {"impure", no_argument
, 0, OPTION_IMPURE
},
321 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
322 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
323 {"input-target", required_argument
, 0, 'I'},
324 {"interleave", required_argument
, 0, 'i'},
325 {"keep-file-symbols", no_argument
, 0, OPTION_KEEP_FILE_SYMBOLS
},
326 {"keep-global-symbol", required_argument
, 0, 'G'},
327 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
328 {"keep-symbol", required_argument
, 0, 'K'},
329 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
330 {"localize-symbol", required_argument
, 0, 'L'},
331 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
332 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
333 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
334 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
335 {"only-section", required_argument
, 0, 'j'},
336 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
337 {"output-target", required_argument
, 0, 'O'},
338 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
339 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
340 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
341 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
342 {"preserve-dates", no_argument
, 0, 'p'},
343 {"pure", no_argument
, 0, OPTION_PURE
},
344 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
345 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
346 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
347 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
348 {"remove-section", required_argument
, 0, 'R'},
349 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
350 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
351 {"set-start", required_argument
, 0, OPTION_SET_START
},
352 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
353 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
354 {"strip-all", no_argument
, 0, 'S'},
355 {"strip-debug", no_argument
, 0, 'g'},
356 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
357 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
358 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
359 {"strip-symbol", required_argument
, 0, 'N'},
360 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
361 {"target", required_argument
, 0, 'F'},
362 {"verbose", no_argument
, 0, 'v'},
363 {"version", no_argument
, 0, 'V'},
364 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
365 {"weaken-symbol", required_argument
, 0, 'W'},
366 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
367 {"wildcard", no_argument
, 0, 'w'},
368 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
369 {0, no_argument
, 0, 0}
373 extern char *program_name
;
375 /* This flag distinguishes between strip and objcopy:
376 1 means this is 'strip'; 0 means this is 'objcopy'.
377 -1 means if we should use argv[0] to decide. */
380 /* The maximum length of an S record. This variable is declared in srec.c
381 and can be modified by the --srec-len parameter. */
382 extern unsigned int Chunk
;
384 /* Restrict the generation of Srecords to type S3 only.
385 This variable is declare in bfd/srec.c and can be toggled
386 on by the --srec-forceS3 command line switch. */
387 extern bfd_boolean S3Forced
;
389 /* Defined in bfd/binary.c. Used to set architecture and machine of input
391 extern enum bfd_architecture bfd_external_binary_architecture
;
392 extern unsigned long bfd_external_machine
;
394 /* Forward declarations. */
395 static void setup_section (bfd
*, asection
*, void *);
396 static void setup_bfd_headers (bfd
*, bfd
*);
397 static void copy_section (bfd
*, asection
*, void *);
398 static void get_sections (bfd
*, asection
*, void *);
399 static int compare_section_lma (const void *, const void *);
400 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
401 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
402 static const char *lookup_sym_redefinition (const char *);
405 copy_usage (FILE *stream
, int exit_status
)
407 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
408 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
409 fprintf (stream
, _(" The options are:\n"));
410 fprintf (stream
, _("\
411 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
412 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
413 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
414 -F --target <bfdname> Set both input and output format to <bfdname>\n\
415 --debugging Convert debugging information, if possible\n\
416 -p --preserve-dates Copy modified/access timestamps to the output\n\
417 -j --only-section <name> Only copy section <name> into the output\n\
418 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
419 -R --remove-section <name> Remove section <name> from the output\n\
420 -S --strip-all Remove all symbol and relocation information\n\
421 -g --strip-debug Remove all debugging symbols & sections\n\
422 --strip-unneeded Remove all symbols not needed by relocations\n\
423 -N --strip-symbol <name> Do not copy symbol <name>\n\
424 --strip-unneeded-symbol <name>\n\
425 Do not copy symbol <name> unless needed by\n\
427 --only-keep-debug Strip everything but the debug information\n\
428 -K --keep-symbol <name> Do not strip symbol <name>\n\
429 --keep-file-symbols Do not strip file symbol(s)\n\
430 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
431 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
432 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
433 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
434 --weaken Force all global symbols to be marked as weak\n\
435 -w --wildcard Permit wildcard in symbol comparison\n\
436 -x --discard-all Remove all non-global symbols\n\
437 -X --discard-locals Remove any compiler-generated symbols\n\
438 -i --interleave <number> Only copy one out of every <number> bytes\n\
439 -b --byte <num> Select byte <num> in every interleaved block\n\
440 --gap-fill <val> Fill gaps between sections with <val>\n\
441 --pad-to <addr> Pad the last section up to address <addr>\n\
442 --set-start <addr> Set the start address to <addr>\n\
443 {--change-start|--adjust-start} <incr>\n\
444 Add <incr> to the start address\n\
445 {--change-addresses|--adjust-vma} <incr>\n\
446 Add <incr> to LMA, VMA and start addresses\n\
447 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
448 Change LMA and VMA of section <name> by <val>\n\
449 --change-section-lma <name>{=|+|-}<val>\n\
450 Change the LMA of section <name> by <val>\n\
451 --change-section-vma <name>{=|+|-}<val>\n\
452 Change the VMA of section <name> by <val>\n\
453 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
454 Warn if a named section does not exist\n\
455 --set-section-flags <name>=<flags>\n\
456 Set section <name>'s properties to <flags>\n\
457 --add-section <name>=<file> Add section <name> found in <file> to output\n\
458 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
459 --change-leading-char Force output format's leading character style\n\
460 --remove-leading-char Remove leading character from global symbols\n\
461 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
462 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
464 --srec-len <number> Restrict the length of generated Srecords\n\
465 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
466 --strip-symbols <file> -N for all symbols listed in <file>\n\
467 --strip-unneeded-symbols <file>\n\
468 --strip-unneeded-symbol for all symbols listed\n\
470 --keep-symbols <file> -K for all symbols listed in <file>\n\
471 --localize-symbols <file> -L for all symbols listed in <file>\n\
472 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
473 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
474 --weaken-symbols <file> -W for all symbols listed in <file>\n\
475 --alt-machine-code <index> Use alternate machine code for output\n\
476 --writable-text Mark the output text as writable\n\
477 --readonly-text Make the output text write protected\n\
478 --pure Mark the output file as demand paged\n\
479 --impure Mark the output file as impure\n\
480 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
481 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
482 --prefix-alloc-sections <prefix>\n\
483 Add <prefix> to start of every allocatable\n\
485 -v --verbose List all object files modified\n\
486 @<file> Read options from <file>\n\
487 -V --version Display this program's version number\n\
488 -h --help Display this output\n\
489 --info List object formats & architectures supported\n\
491 list_supported_targets (program_name
, stream
);
492 if (exit_status
== 0)
493 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
498 strip_usage (FILE *stream
, int exit_status
)
500 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
501 fprintf (stream
, _(" Removes symbols and sections from files\n"));
502 fprintf (stream
, _(" The options are:\n"));
503 fprintf (stream
, _("\
504 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
505 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
506 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
507 -p --preserve-dates Copy modified/access timestamps to the output\n\
508 -R --remove-section=<name> Remove section <name> from the output\n\
509 -s --strip-all Remove all symbol and relocation information\n\
510 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
511 --strip-unneeded Remove all symbols not needed by relocations\n\
512 --only-keep-debug Strip everything but the debug information\n\
513 -N --strip-symbol=<name> Do not copy symbol <name>\n\
514 -K --keep-symbol=<name> Do not strip symbol <name>\n\
515 --keep-file-symbols Do not strip file symbol(s)\n\
516 -w --wildcard Permit wildcard in symbol comparison\n\
517 -x --discard-all Remove all non-global symbols\n\
518 -X --discard-locals Remove any compiler-generated symbols\n\
519 -v --verbose List all object files modified\n\
520 -V --version Display this program's version number\n\
521 -h --help Display this output\n\
522 --info List object formats & architectures supported\n\
523 -o <file> Place stripped output into <file>\n\
526 list_supported_targets (program_name
, stream
);
527 if (exit_status
== 0)
528 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
532 /* Parse section flags into a flagword, with a fatal error if the
533 string can't be parsed. */
536 parse_flags (const char *s
)
546 snext
= strchr (s
, ',');
556 #define PARSE_FLAG(fname,fval) \
557 else if (strncasecmp (fname, s, len) == 0) ret |= fval
558 PARSE_FLAG ("alloc", SEC_ALLOC
);
559 PARSE_FLAG ("load", SEC_LOAD
);
560 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
561 PARSE_FLAG ("readonly", SEC_READONLY
);
562 PARSE_FLAG ("debug", SEC_DEBUGGING
);
563 PARSE_FLAG ("code", SEC_CODE
);
564 PARSE_FLAG ("data", SEC_DATA
);
565 PARSE_FLAG ("rom", SEC_ROM
);
566 PARSE_FLAG ("share", SEC_COFF_SHARED
);
567 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
573 copy
= xmalloc (len
+ 1);
574 strncpy (copy
, s
, len
);
576 non_fatal (_("unrecognized section flag `%s'"), copy
);
577 fatal (_("supported flags: %s"),
578 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
588 /* Find and optionally add an entry in the change_sections list. */
590 static struct section_list
*
591 find_section_list (const char *name
, bfd_boolean add
)
593 struct section_list
*p
;
595 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
596 if (strcmp (p
->name
, name
) == 0)
602 p
= xmalloc (sizeof (struct section_list
));
607 p
->change_vma
= CHANGE_IGNORE
;
608 p
->change_lma
= CHANGE_IGNORE
;
611 p
->set_flags
= FALSE
;
614 p
->next
= change_sections
;
620 /* Add a symbol to strip_specific_list. */
623 add_specific_symbol (const char *name
, struct symlist
**list
)
625 struct symlist
*tmp_list
;
627 tmp_list
= xmalloc (sizeof (struct symlist
));
628 tmp_list
->name
= name
;
629 tmp_list
->next
= *list
;
633 /* Add symbols listed in `filename' to strip_specific_list. */
635 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
636 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
639 add_specific_symbols (const char *filename
, struct symlist
**list
)
645 unsigned int line_count
;
647 size
= get_file_size (filename
);
651 buffer
= xmalloc (size
+ 2);
652 f
= fopen (filename
, FOPEN_RT
);
654 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
656 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
657 fatal (_("%s: fread failed"), filename
);
660 buffer
[size
] = '\n';
661 buffer
[size
+ 1] = '\0';
665 for (line
= buffer
; * line
!= '\0'; line
++)
670 int finished
= FALSE
;
672 for (eol
= line
;; eol
++)
678 /* Cope with \n\r. */
686 /* Cope with \r\n. */
697 /* Line comment, Terminate the line here, in case a
698 name is present and then allow the rest of the
699 loop to find the real end of the line. */
711 /* A name may now exist somewhere between 'line' and 'eol'.
712 Strip off leading whitespace and trailing whitespace,
713 then add it to the list. */
714 for (name
= line
; IS_WHITESPACE (* name
); name
++)
716 for (name_end
= name
;
717 (! IS_WHITESPACE (* name_end
))
718 && (! IS_LINE_TERMINATOR (* name_end
));
722 if (! IS_LINE_TERMINATOR (* name_end
))
726 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
729 if (! IS_LINE_TERMINATOR (* extra
))
730 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
731 filename
, line_count
);
737 add_specific_symbol (name
, list
);
739 /* Advance line pointer to end of line. The 'eol ++' in the for
740 loop above will then advance us to the start of the next line. */
746 /* See whether a symbol should be stripped or kept based on
747 strip_specific_list and keep_symbols. */
750 is_specified_symbol (const char *name
, struct symlist
*list
)
752 struct symlist
*tmp_list
;
756 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
757 if (*(tmp_list
->name
) != '!')
759 if (!fnmatch (tmp_list
->name
, name
, 0))
764 if (fnmatch (tmp_list
->name
+ 1, name
, 0))
770 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
771 if (strcmp (name
, tmp_list
->name
) == 0)
778 /* See if a section is being removed. */
781 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
783 if (sections_removed
|| sections_copied
)
785 struct section_list
*p
;
787 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
789 if (sections_removed
&& p
!= NULL
&& p
->remove
)
791 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
795 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
797 if (strip_symbols
== STRIP_DEBUG
798 || strip_symbols
== STRIP_UNNEEDED
799 || strip_symbols
== STRIP_ALL
800 || discard_locals
== LOCALS_ALL
801 || convert_debugging
)
804 if (strip_symbols
== STRIP_NONDEBUG
)
811 /* Choose which symbol entries to copy; put the result in OSYMS.
812 We don't copy in place, because that confuses the relocs.
813 Return the number of symbols to print. */
816 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
817 asymbol
**isyms
, long symcount
)
819 asymbol
**from
= isyms
, **to
= osyms
;
820 long src_count
= 0, dst_count
= 0;
821 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
824 for (; src_count
< symcount
; src_count
++)
826 asymbol
*sym
= from
[src_count
];
827 flagword flags
= sym
->flags
;
828 char *name
= (char *) bfd_asymbol_name (sym
);
830 bfd_boolean undefined
;
831 bfd_boolean rem_leading_char
;
832 bfd_boolean add_leading_char
;
834 undefined
= bfd_is_und_section (bfd_get_section (sym
));
836 if (redefine_sym_list
)
838 char *old_name
, *new_name
;
840 old_name
= (char *) bfd_asymbol_name (sym
);
841 new_name
= (char *) lookup_sym_redefinition (old_name
);
842 bfd_asymbol_name (sym
) = new_name
;
846 /* Check if we will remove the current leading character. */
848 (name
[0] == bfd_get_symbol_leading_char (abfd
))
849 && (change_leading_char
850 || (remove_leading_char
851 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
853 || bfd_is_com_section (bfd_get_section (sym
)))));
855 /* Check if we will add a new leading character. */
858 && (bfd_get_symbol_leading_char (obfd
) != '\0')
859 && (bfd_get_symbol_leading_char (abfd
) == '\0'
860 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
862 /* Short circuit for change_leading_char if we can do it in-place. */
863 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
865 name
[0] = bfd_get_symbol_leading_char (obfd
);
866 bfd_asymbol_name (sym
) = name
;
867 rem_leading_char
= FALSE
;
868 add_leading_char
= FALSE
;
871 /* Remove leading char. */
872 if (rem_leading_char
)
873 bfd_asymbol_name (sym
) = ++name
;
875 /* Add new leading char and/or prefix. */
876 if (add_leading_char
|| prefix_symbols_string
)
880 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
881 + strlen (name
) + 1);
882 if (add_leading_char
)
883 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
885 if (prefix_symbols_string
)
887 strcpy (ptr
, prefix_symbols_string
);
888 ptr
+= strlen (prefix_symbols_string
);
892 bfd_asymbol_name (sym
) = n
;
896 if (strip_symbols
== STRIP_ALL
)
898 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
899 || ((flags
& BSF_SECTION_SYM
) != 0
900 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
903 else if (relocatable
/* Relocatable file. */
904 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
906 else if (bfd_decode_symclass (sym
) == 'I')
907 /* Global symbols in $idata sections need to be retained
908 even if relocatable is FALSE. External users of the
909 library containing the $idata section may reference these
912 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
913 || (flags
& BSF_WEAK
) != 0
915 || bfd_is_com_section (bfd_get_section (sym
)))
916 keep
= strip_symbols
!= STRIP_UNNEEDED
;
917 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
918 keep
= (strip_symbols
!= STRIP_DEBUG
919 && strip_symbols
!= STRIP_UNNEEDED
920 && ! convert_debugging
);
921 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
922 /* COMDAT sections store special information in local
923 symbols, so we cannot risk stripping any of them. */
925 else /* Local symbol. */
926 keep
= (strip_symbols
!= STRIP_UNNEEDED
927 && (discard_locals
!= LOCALS_ALL
928 && (discard_locals
!= LOCALS_START_L
929 || ! bfd_is_local_label (abfd
, sym
))));
931 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
934 && !(flags
& BSF_KEEP
)
935 && is_specified_symbol (name
, strip_unneeded_list
))
938 && ((keep_file_symbols
&& (flags
& BSF_FILE
))
939 || is_specified_symbol (name
, keep_specific_list
)))
941 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
946 if ((flags
& BSF_GLOBAL
) != 0
947 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
949 sym
->flags
&= ~ BSF_GLOBAL
;
950 sym
->flags
|= BSF_WEAK
;
954 && (flags
& (BSF_GLOBAL
| BSF_WEAK
))
955 && (is_specified_symbol (name
, localize_specific_list
)
956 || (keepglobal_specific_list
!= NULL
957 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
959 sym
->flags
&= ~ (BSF_GLOBAL
| BSF_WEAK
);
960 sym
->flags
|= BSF_LOCAL
;
964 && (flags
& BSF_LOCAL
)
965 && is_specified_symbol (name
, globalize_specific_list
))
967 sym
->flags
&= ~ BSF_LOCAL
;
968 sym
->flags
|= BSF_GLOBAL
;
971 to
[dst_count
++] = sym
;
975 to
[dst_count
] = NULL
;
980 /* Find the redefined name of symbol SOURCE. */
983 lookup_sym_redefinition (const char *source
)
985 struct redefine_node
*list
;
987 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
988 if (strcmp (source
, list
->source
) == 0)
994 /* Add a node to a symbol redefine list. */
997 redefine_list_append (const char *cause
, const char *source
, const char *target
)
999 struct redefine_node
**p
;
1000 struct redefine_node
*list
;
1001 struct redefine_node
*new_node
;
1003 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
1005 if (strcmp (source
, list
->source
) == 0)
1006 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1009 if (strcmp (target
, list
->target
) == 0)
1010 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1014 new_node
= xmalloc (sizeof (struct redefine_node
));
1016 new_node
->source
= strdup (source
);
1017 new_node
->target
= strdup (target
);
1018 new_node
->next
= NULL
;
1023 /* Handle the --redefine-syms option. Read lines containing "old new"
1024 from the file, and add them to the symbol redefine list. */
1027 add_redefine_syms_file (const char *filename
)
1036 file
= fopen (filename
, "r");
1038 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1039 filename
, strerror (errno
));
1042 buf
= xmalloc (bufsize
);
1050 /* Collect the input symbol name. */
1051 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1059 buf
= xrealloc (buf
, bufsize
);
1067 /* Eat white space between the symbol names. */
1068 while (IS_WHITESPACE (c
))
1070 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1075 /* Collect the output symbol name. */
1077 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1085 buf
= xrealloc (buf
, bufsize
);
1093 /* Eat white space at end of line. */
1094 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1099 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1100 || c
== '\n' || c
== EOF
)
1103 /* Append the redefinition to the list. */
1105 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1116 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1118 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1119 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1122 /* Eat the rest of the line and finish it. */
1123 while (c
!= '\n' && c
!= EOF
)
1129 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1134 /* Copy unkown object file IBFD onto OBFD.
1135 Returns TRUE upon success, FALSE otherwise. */
1138 copy_unknown_object (bfd
*ibfd
, bfd
*obfd
)
1146 if (bfd_stat_arch_elt (ibfd
, &buf
) != 0)
1148 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1155 non_fatal (_("stat returns negative size for `%s'"),
1156 bfd_get_archive_filename (ibfd
));
1160 if (bfd_seek (ibfd
, (file_ptr
) 0, SEEK_SET
) != 0)
1162 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1167 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1168 bfd_get_archive_filename (ibfd
), bfd_get_filename (obfd
));
1170 cbuf
= xmalloc (BUFSIZE
);
1172 while (ncopied
< size
)
1174 tocopy
= size
- ncopied
;
1175 if (tocopy
> BUFSIZE
)
1178 if (bfd_bread (cbuf
, (bfd_size_type
) tocopy
, ibfd
)
1179 != (bfd_size_type
) tocopy
)
1181 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1186 if (bfd_bwrite (cbuf
, (bfd_size_type
) tocopy
, obfd
)
1187 != (bfd_size_type
) tocopy
)
1189 bfd_nonfatal (bfd_get_filename (obfd
));
1197 chmod (bfd_get_filename (obfd
), buf
.st_mode
);
1202 /* Copy object file IBFD onto OBFD.
1203 Returns TRUE upon success, FALSE otherwise. */
1206 copy_object (bfd
*ibfd
, bfd
*obfd
)
1210 asection
**osections
= NULL
;
1211 asection
*gnu_debuglink_section
= NULL
;
1212 bfd_size_type
*gaps
= NULL
;
1213 bfd_size_type max_gap
= 0;
1216 enum bfd_architecture iarch
;
1219 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1220 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1221 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1222 fatal (_("Unable to change endianness of input file(s)"));
1224 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1226 bfd_nonfatal (bfd_get_filename (obfd
));
1231 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1232 bfd_get_archive_filename (ibfd
), bfd_get_target (ibfd
),
1233 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1238 start
= bfd_get_start_address (ibfd
);
1239 start
+= change_start
;
1241 /* Neither the start address nor the flags
1242 need to be set for a core file. */
1243 if (bfd_get_format (obfd
) != bfd_core
)
1247 flags
= bfd_get_file_flags (ibfd
);
1248 flags
|= bfd_flags_to_set
;
1249 flags
&= ~bfd_flags_to_clear
;
1250 flags
&= bfd_applicable_file_flags (obfd
);
1252 if (!bfd_set_start_address (obfd
, start
)
1253 || !bfd_set_file_flags (obfd
, flags
))
1255 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1260 /* Copy architecture of input file to output file. */
1261 iarch
= bfd_get_arch (ibfd
);
1262 imach
= bfd_get_mach (ibfd
);
1263 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1264 && (ibfd
->target_defaulted
1265 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1267 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1268 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1269 bfd_get_archive_filename (ibfd
));
1271 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1272 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1273 bfd_get_mach (ibfd
)));
1277 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1279 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1286 if (osympp
!= isympp
)
1289 /* BFD mandates that all output sections be created and sizes set before
1290 any output is done. Thus, we traverse all sections multiple times. */
1291 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1293 setup_bfd_headers (ibfd
, obfd
);
1295 if (add_sections
!= NULL
)
1297 struct section_add
*padd
;
1298 struct section_list
*pset
;
1300 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1304 pset
= find_section_list (padd
->name
, FALSE
);
1308 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1309 if (pset
!= NULL
&& pset
->set_flags
)
1310 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1312 padd
->section
= bfd_make_section_with_flags (obfd
, padd
->name
, flags
);
1313 if (padd
->section
== NULL
)
1315 non_fatal (_("can't create section `%s': %s"),
1316 padd
->name
, bfd_errmsg (bfd_get_error ()));
1320 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1322 bfd_nonfatal (bfd_get_filename (obfd
));
1328 if (pset
->change_vma
!= CHANGE_IGNORE
)
1329 if (! bfd_set_section_vma (obfd
, padd
->section
,
1332 bfd_nonfatal (bfd_get_filename (obfd
));
1336 if (pset
->change_lma
!= CHANGE_IGNORE
)
1338 padd
->section
->lma
= pset
->lma_val
;
1340 if (! bfd_set_section_alignment
1341 (obfd
, padd
->section
,
1342 bfd_section_alignment (obfd
, padd
->section
)))
1344 bfd_nonfatal (bfd_get_filename (obfd
));
1352 if (gnu_debuglink_filename
!= NULL
)
1354 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1355 (obfd
, gnu_debuglink_filename
);
1357 if (gnu_debuglink_section
== NULL
)
1359 bfd_nonfatal (gnu_debuglink_filename
);
1364 if (bfd_count_sections (obfd
) == 0)
1366 non_fatal (_("there are no sections to be copied!"));
1370 if (gap_fill_set
|| pad_to_set
)
1375 /* We must fill in gaps between the sections and/or we must pad
1376 the last section to a specified address. We do this by
1377 grabbing a list of the sections, sorting them by VMA, and
1378 increasing the section sizes as required to fill the gaps.
1379 We write out the gap contents below. */
1381 c
= bfd_count_sections (obfd
);
1382 osections
= xmalloc (c
* sizeof (asection
*));
1384 bfd_map_over_sections (obfd
, get_sections
, &set
);
1386 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1388 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1389 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1393 for (i
= 0; i
< c
- 1; i
++)
1397 bfd_vma gap_start
, gap_stop
;
1399 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1400 if ((flags
& SEC_HAS_CONTENTS
) == 0
1401 || (flags
& SEC_LOAD
) == 0)
1404 size
= bfd_section_size (obfd
, osections
[i
]);
1405 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1406 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1407 if (gap_start
< gap_stop
)
1409 if (! bfd_set_section_size (obfd
, osections
[i
],
1410 size
+ (gap_stop
- gap_start
)))
1412 non_fatal (_("Can't fill gap after %s: %s"),
1413 bfd_get_section_name (obfd
, osections
[i
]),
1414 bfd_errmsg (bfd_get_error ()));
1418 gaps
[i
] = gap_stop
- gap_start
;
1419 if (max_gap
< gap_stop
- gap_start
)
1420 max_gap
= gap_stop
- gap_start
;
1430 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1431 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1432 if (lma
+ size
< pad_to
)
1434 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1437 non_fatal (_("Can't add padding to %s: %s"),
1438 bfd_get_section_name (obfd
, osections
[c
- 1]),
1439 bfd_errmsg (bfd_get_error ()));
1444 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1445 if (max_gap
< pad_to
- (lma
+ size
))
1446 max_gap
= pad_to
- (lma
+ size
);
1452 /* Symbol filtering must happen after the output sections
1453 have been created, but before their contents are set. */
1455 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1458 bfd_nonfatal (bfd_get_archive_filename (ibfd
));
1462 osympp
= isympp
= xmalloc (symsize
);
1463 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1466 bfd_nonfatal (bfd_get_filename (ibfd
));
1470 if (convert_debugging
)
1471 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1473 if (strip_symbols
== STRIP_DEBUG
1474 || strip_symbols
== STRIP_ALL
1475 || strip_symbols
== STRIP_UNNEEDED
1476 || strip_symbols
== STRIP_NONDEBUG
1477 || discard_locals
!= LOCALS_UNDEF
1478 || strip_specific_list
!= NULL
1479 || keep_specific_list
!= NULL
1480 || localize_specific_list
!= NULL
1481 || globalize_specific_list
!= NULL
1482 || keepglobal_specific_list
!= NULL
1483 || weaken_specific_list
!= NULL
1484 || prefix_symbols_string
1487 || convert_debugging
1488 || change_leading_char
1489 || remove_leading_char
1490 || redefine_sym_list
1493 /* Mark symbols used in output relocations so that they
1494 are kept, even if they are local labels or static symbols.
1496 Note we iterate over the input sections examining their
1497 relocations since the relocations for the output sections
1498 haven't been set yet. mark_symbols_used_in_relocations will
1499 ignore input sections which have no corresponding output
1501 if (strip_symbols
!= STRIP_ALL
)
1502 bfd_map_over_sections (ibfd
,
1503 mark_symbols_used_in_relocations
,
1505 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1506 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1509 if (convert_debugging
&& dhandle
!= NULL
)
1511 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1518 bfd_set_symtab (obfd
, osympp
, symcount
);
1520 /* This has to happen after the symbol table has been set. */
1521 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1523 if (add_sections
!= NULL
)
1525 struct section_add
*padd
;
1527 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1529 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1532 bfd_nonfatal (bfd_get_filename (obfd
));
1538 if (gnu_debuglink_filename
!= NULL
)
1540 if (! bfd_fill_in_gnu_debuglink_section
1541 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1543 bfd_nonfatal (gnu_debuglink_filename
);
1548 if (gap_fill_set
|| pad_to_set
)
1553 /* Fill in the gaps. */
1556 buf
= xmalloc (max_gap
);
1557 memset (buf
, gap_fill
, max_gap
);
1559 c
= bfd_count_sections (obfd
);
1560 for (i
= 0; i
< c
; i
++)
1568 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1579 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1582 bfd_nonfatal (bfd_get_filename (obfd
));
1593 /* Allow the BFD backend to copy any private data it understands
1594 from the input BFD to the output BFD. This is done last to
1595 permit the routine to look at the filtered symbol table, which is
1596 important for the ECOFF code at least. */
1597 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1598 && strip_symbols
== STRIP_NONDEBUG
)
1599 /* Do not copy the private data when creating an ELF format
1600 debug info file. We do not want the program headers. */
1602 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1604 non_fatal (_("%s: error copying private BFD data: %s"),
1605 bfd_get_filename (obfd
),
1606 bfd_errmsg (bfd_get_error ()));
1610 /* Switch to the alternate machine code. We have to do this at the
1611 very end, because we only initialize the header when we create
1612 the first section. */
1613 if (use_alt_mach_code
!= 0
1614 && ! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1615 non_fatal (_("unknown alternate machine code, ignored"));
1621 #if defined (_WIN32) && !defined (__CYGWIN32__)
1622 #define MKDIR(DIR, MODE) mkdir (DIR)
1624 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1627 /* Read each archive element in turn from IBFD, copy the
1628 contents to temp file, and keep the temp file handle. */
1631 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1635 struct name_list
*next
;
1639 bfd
**ptr
= &obfd
->archive_head
;
1641 char *dir
= make_tempname (bfd_get_filename (obfd
));
1643 /* Make a temp directory to hold the contents. */
1644 if (MKDIR (dir
, 0700) != 0)
1645 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1646 dir
, strerror (errno
));
1648 obfd
->has_armap
= ibfd
->has_armap
;
1652 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1654 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1655 RETURN_NONFATAL (bfd_get_filename (obfd
));
1657 while (!status
&& this_element
!= NULL
)
1663 int stat_status
= 0;
1664 bfd_boolean
delete = TRUE
;
1666 /* Create an output file for this member. */
1667 output_name
= concat (dir
, "/",
1668 bfd_get_filename (this_element
), (char *) 0);
1670 /* If the file already exists, make another temp dir. */
1671 if (stat (output_name
, &buf
) >= 0)
1673 output_name
= make_tempname (output_name
);
1674 if (MKDIR (output_name
, 0700) != 0)
1675 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1676 output_name
, strerror (errno
));
1678 l
= xmalloc (sizeof (struct name_list
));
1679 l
->name
= output_name
;
1683 output_name
= concat (output_name
, "/",
1684 bfd_get_filename (this_element
), (char *) 0);
1687 output_bfd
= bfd_openw (output_name
, output_target
);
1690 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1692 if (stat_status
!= 0)
1693 non_fatal (_("internal stat error on %s"),
1694 bfd_get_filename (this_element
));
1697 l
= xmalloc (sizeof (struct name_list
));
1698 l
->name
= output_name
;
1703 if (output_bfd
== NULL
)
1704 RETURN_NONFATAL (output_name
);
1706 if (bfd_check_format (this_element
, bfd_object
))
1708 delete = ! copy_object (this_element
, output_bfd
);
1711 || bfd_get_arch (this_element
) != bfd_arch_unknown
)
1713 if (!bfd_close (output_bfd
))
1715 bfd_nonfatal (bfd_get_filename (output_bfd
));
1716 /* Error in new object file. Don't change archive. */
1721 goto copy_unknown_element
;
1725 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1726 bfd_get_archive_filename (this_element
));
1728 copy_unknown_element
:
1729 delete = !copy_unknown_object (this_element
, output_bfd
);
1730 if (!bfd_close_all_done (output_bfd
))
1732 bfd_nonfatal (bfd_get_filename (output_bfd
));
1733 /* Error in new object file. Don't change archive. */
1740 unlink (output_name
);
1745 if (preserve_dates
&& stat_status
== 0)
1746 set_times (output_name
, &buf
);
1748 /* Open the newly output file and attach to our list. */
1749 output_bfd
= bfd_openr (output_name
, output_target
);
1751 l
->obfd
= output_bfd
;
1754 ptr
= &output_bfd
->next
;
1756 last_element
= this_element
;
1758 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1760 bfd_close (last_element
);
1765 if (!bfd_close (obfd
))
1766 RETURN_NONFATAL (bfd_get_filename (obfd
));
1768 if (!bfd_close (ibfd
))
1769 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1771 /* Delete all the files that we opened. */
1772 for (l
= list
; l
!= NULL
; l
= l
->next
)
1774 if (l
->obfd
== NULL
)
1778 bfd_close (l
->obfd
);
1785 /* The top-level control. */
1788 copy_file (const char *input_filename
, const char *output_filename
,
1789 const char *input_target
, const char *output_target
)
1792 char **obj_matching
;
1793 char **core_matching
;
1795 if (get_file_size (input_filename
) < 1)
1797 non_fatal (_("error: the input file '%s' is empty"), input_filename
);
1802 /* To allow us to do "strip *" without dying on the first
1803 non-object file, failures are nonfatal. */
1804 ibfd
= bfd_openr (input_filename
, input_target
);
1806 RETURN_NONFATAL (input_filename
);
1808 if (bfd_check_format (ibfd
, bfd_archive
))
1812 /* bfd_get_target does not return the correct value until
1813 bfd_check_format succeeds. */
1814 if (output_target
== NULL
)
1815 output_target
= bfd_get_target (ibfd
);
1817 obfd
= bfd_openw (output_filename
, output_target
);
1819 RETURN_NONFATAL (output_filename
);
1821 copy_archive (ibfd
, obfd
, output_target
);
1823 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1828 /* bfd_get_target does not return the correct value until
1829 bfd_check_format succeeds. */
1830 if (output_target
== NULL
)
1831 output_target
= bfd_get_target (ibfd
);
1833 obfd
= bfd_openw (output_filename
, output_target
);
1835 RETURN_NONFATAL (output_filename
);
1837 if (! copy_object (ibfd
, obfd
))
1840 if (!bfd_close (obfd
))
1841 RETURN_NONFATAL (output_filename
);
1843 if (!bfd_close (ibfd
))
1844 RETURN_NONFATAL (input_filename
);
1849 bfd_error_type obj_error
= bfd_get_error ();
1850 bfd_error_type core_error
;
1852 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1854 /* This probably can't happen.. */
1855 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1856 free (obj_matching
);
1860 core_error
= bfd_get_error ();
1861 /* Report the object error in preference to the core error. */
1862 if (obj_error
!= core_error
)
1863 bfd_set_error (obj_error
);
1865 bfd_nonfatal (input_filename
);
1867 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1869 list_matching_formats (obj_matching
);
1870 free (obj_matching
);
1872 if (core_error
== bfd_error_file_ambiguously_recognized
)
1874 list_matching_formats (core_matching
);
1875 free (core_matching
);
1882 /* Add a name to the section renaming list. */
1885 add_section_rename (const char * old_name
, const char * new_name
,
1888 section_rename
* rename
;
1890 /* Check for conflicts first. */
1891 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1892 if (strcmp (rename
->old_name
, old_name
) == 0)
1894 /* Silently ignore duplicate definitions. */
1895 if (strcmp (rename
->new_name
, new_name
) == 0
1896 && rename
->flags
== flags
)
1899 fatal (_("Multiple renames of section %s"), old_name
);
1902 rename
= xmalloc (sizeof (* rename
));
1904 rename
->old_name
= old_name
;
1905 rename
->new_name
= new_name
;
1906 rename
->flags
= flags
;
1907 rename
->next
= section_rename_list
;
1909 section_rename_list
= rename
;
1912 /* Check the section rename list for a new name of the input section
1913 ISECTION. Return the new name if one is found.
1914 Also set RETURNED_FLAGS to the flags to be used for this section. */
1917 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
1918 flagword
* returned_flags
)
1920 const char * old_name
= bfd_section_name (ibfd
, isection
);
1921 section_rename
* rename
;
1923 /* Default to using the flags of the input section. */
1924 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1926 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1927 if (strcmp (rename
->old_name
, old_name
) == 0)
1929 if (rename
->flags
!= (flagword
) -1)
1930 * returned_flags
= rename
->flags
;
1932 return rename
->new_name
;
1938 /* Once each of the sections is copied, we may still need to do some
1939 finalization work for private section headers. Do that here. */
1942 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
1946 /* Allow the BFD backend to copy any private data it understands
1947 from the input section to the output section. */
1948 if (! bfd_copy_private_header_data (ibfd
, obfd
))
1950 err
= _("private header data");
1954 /* All went well. */
1958 non_fatal (_("%s: error in %s: %s"),
1959 bfd_get_filename (ibfd
),
1960 err
, bfd_errmsg (bfd_get_error ()));
1964 /* Create a section in OBFD with the same
1965 name and attributes as ISECTION in IBFD. */
1968 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1970 bfd
*obfd
= obfdarg
;
1971 struct section_list
*p
;
1979 char *prefix
= NULL
;
1981 if (is_strip_section (ibfd
, isection
))
1984 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1988 /* Get the, possibly new, name of the output section. */
1989 name
= find_section_rename (ibfd
, isection
, & flags
);
1991 /* Prefix sections. */
1992 if ((prefix_alloc_sections_string
)
1993 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1994 prefix
= prefix_alloc_sections_string
;
1995 else if (prefix_sections_string
)
1996 prefix
= prefix_sections_string
;
2002 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
2008 if (p
!= NULL
&& p
->set_flags
)
2009 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
2010 else if (strip_symbols
== STRIP_NONDEBUG
&& (flags
& SEC_ALLOC
) != 0)
2011 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
2013 osection
= bfd_make_section_anyway_with_flags (obfd
, name
, flags
);
2015 if (osection
== NULL
)
2021 if (strip_symbols
== STRIP_NONDEBUG
2022 && obfd
->xvec
->flavour
== bfd_target_elf_flavour
2023 && (flags
& SEC_ALLOC
) != 0
2024 && (p
== NULL
|| !p
->set_flags
))
2025 elf_section_type (osection
) = SHT_NOBITS
;
2027 size
= bfd_section_size (ibfd
, isection
);
2029 size
= (size
+ interleave
- 1) / interleave
;
2030 if (! bfd_set_section_size (obfd
, osection
, size
))
2036 vma
= bfd_section_vma (ibfd
, isection
);
2037 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
2039 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
2042 vma
+= change_section_address
;
2044 if (! bfd_set_section_vma (obfd
, osection
, vma
))
2050 lma
= isection
->lma
;
2051 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
2053 if (p
->change_lma
== CHANGE_MODIFY
)
2055 else if (p
->change_lma
== CHANGE_SET
)
2061 lma
+= change_section_address
;
2063 osection
->lma
= lma
;
2065 /* FIXME: This is probably not enough. If we change the LMA we
2066 may have to recompute the header for the file as well. */
2067 if (!bfd_set_section_alignment (obfd
,
2069 bfd_section_alignment (ibfd
, isection
)))
2071 err
= _("alignment");
2075 /* Copy merge entity size. */
2076 osection
->entsize
= isection
->entsize
;
2078 /* This used to be mangle_section; we do here to avoid using
2079 bfd_get_section_by_name since some formats allow multiple
2080 sections with the same name. */
2081 isection
->output_section
= osection
;
2082 isection
->output_offset
= 0;
2084 /* Allow the BFD backend to copy any private data it understands
2085 from the input section to the output section. */
2086 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
2087 && strip_symbols
== STRIP_NONDEBUG
)
2088 /* Do not copy the private data when creating an ELF format
2089 debug info file. We do not want the program headers. */
2091 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
2093 err
= _("private data");
2097 /* All went well. */
2101 non_fatal (_("%s: section `%s': error in %s: %s"),
2102 bfd_get_filename (ibfd
),
2103 bfd_section_name (ibfd
, isection
),
2104 err
, bfd_errmsg (bfd_get_error ()));
2108 /* Copy the data of input section ISECTION of IBFD
2109 to an output section with the same name in OBFD.
2110 If stripping then don't copy any relocation info. */
2113 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2115 bfd
*obfd
= obfdarg
;
2116 struct section_list
*p
;
2124 /* If we have already failed earlier on,
2125 do not keep on generating complaints now. */
2129 if (is_strip_section (ibfd
, isection
))
2132 flags
= bfd_get_section_flags (ibfd
, isection
);
2133 if ((flags
& SEC_GROUP
) != 0)
2136 osection
= isection
->output_section
;
2137 size
= bfd_get_section_size (isection
);
2139 if (size
== 0 || osection
== 0)
2142 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2144 /* Core files do not need to be relocated. */
2145 if (bfd_get_format (obfd
) == bfd_core
)
2149 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2153 /* Do not complain if the target does not support relocations. */
2154 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2157 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2162 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2165 relpp
= xmalloc (relsize
);
2166 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2168 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2170 if (strip_symbols
== STRIP_ALL
)
2172 /* Remove relocations which are not in
2173 keep_strip_specific_list. */
2174 arelent
**temp_relpp
;
2175 long temp_relcount
= 0;
2178 temp_relpp
= xmalloc (relsize
);
2179 for (i
= 0; i
< relcount
; i
++)
2180 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2181 keep_specific_list
))
2182 temp_relpp
[temp_relcount
++] = relpp
[i
];
2183 relcount
= temp_relcount
;
2188 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2193 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2194 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2196 void *memhunk
= xmalloc (size
);
2198 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2199 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2203 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2204 char *from
= (char *) memhunk
+ copy_byte
;
2206 char *end
= (char *) memhunk
+ size
;
2208 for (; from
< end
; from
+= interleave
)
2211 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2212 osection
->lma
/= interleave
;
2215 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2216 RETURN_NONFATAL (bfd_get_filename (obfd
));
2220 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2222 void *memhunk
= xmalloc (size
);
2224 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2225 flag--they can just remove the section entirely and add it
2226 back again. However, we do permit them to turn on the
2227 SEC_HAS_CONTENTS flag, and take it to mean that the section
2228 contents should be zeroed out. */
2230 memset (memhunk
, 0, size
);
2231 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2232 RETURN_NONFATAL (bfd_get_filename (obfd
));
2237 /* Get all the sections. This is used when --gap-fill or --pad-to is
2241 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2243 asection
***secppp
= secppparg
;
2245 **secppp
= osection
;
2249 /* Sort sections by VMA. This is called via qsort, and is used when
2250 --gap-fill or --pad-to is used. We force non loadable or empty
2251 sections to the front, where they are easier to ignore. */
2254 compare_section_lma (const void *arg1
, const void *arg2
)
2256 const asection
*const *sec1
= arg1
;
2257 const asection
*const *sec2
= arg2
;
2258 flagword flags1
, flags2
;
2260 /* Sort non loadable sections to the front. */
2261 flags1
= (*sec1
)->flags
;
2262 flags2
= (*sec2
)->flags
;
2263 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2264 || (flags1
& SEC_LOAD
) == 0)
2266 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2267 && (flags2
& SEC_LOAD
) != 0)
2272 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2273 || (flags2
& SEC_LOAD
) == 0)
2277 /* Sort sections by LMA. */
2278 if ((*sec1
)->lma
> (*sec2
)->lma
)
2280 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2283 /* Sort sections with the same LMA by size. */
2284 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2286 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2292 /* Mark all the symbols which will be used in output relocations with
2293 the BSF_KEEP flag so that those symbols will not be stripped.
2295 Ignore relocations which will not appear in the output file. */
2298 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2300 asymbol
**symbols
= symbolsarg
;
2305 /* Ignore an input section with no corresponding output section. */
2306 if (isection
->output_section
== NULL
)
2309 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2312 /* Do not complain if the target does not support relocations. */
2313 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2315 bfd_fatal (bfd_get_filename (ibfd
));
2321 relpp
= xmalloc (relsize
);
2322 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2324 bfd_fatal (bfd_get_filename (ibfd
));
2326 /* Examine each symbol used in a relocation. If it's not one of the
2327 special bfd section symbols, then mark it with BSF_KEEP. */
2328 for (i
= 0; i
< relcount
; i
++)
2330 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2331 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2332 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2333 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2340 /* Write out debugging information. */
2343 write_debugging_info (bfd
*obfd
, void *dhandle
,
2344 long *symcountp ATTRIBUTE_UNUSED
,
2345 asymbol
***symppp ATTRIBUTE_UNUSED
)
2347 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2348 return write_ieee_debugging_info (obfd
, dhandle
);
2350 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2351 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2353 bfd_byte
*syms
, *strings
;
2354 bfd_size_type symsize
, stringsize
;
2355 asection
*stabsec
, *stabstrsec
;
2358 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2363 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
2364 stabsec
= bfd_make_section_with_flags (obfd
, ".stab", flags
);
2365 stabstrsec
= bfd_make_section_with_flags (obfd
, ".stabstr", flags
);
2367 || stabstrsec
== NULL
2368 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2369 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2370 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2371 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0))
2373 non_fatal (_("%s: can't create debugging section: %s"),
2374 bfd_get_filename (obfd
),
2375 bfd_errmsg (bfd_get_error ()));
2379 /* We can get away with setting the section contents now because
2380 the next thing the caller is going to do is copy over the
2381 real sections. We may someday have to split the contents
2382 setting out of this function. */
2383 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2384 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2387 non_fatal (_("%s: can't set debugging section contents: %s"),
2388 bfd_get_filename (obfd
),
2389 bfd_errmsg (bfd_get_error ()));
2396 non_fatal (_("%s: don't know how to write debugging information for %s"),
2397 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2402 strip_main (int argc
, char *argv
[])
2404 char *input_target
= NULL
;
2405 char *output_target
= NULL
;
2406 bfd_boolean show_version
= FALSE
;
2407 bfd_boolean formats_info
= FALSE
;
2410 struct section_list
*p
;
2411 char *output_file
= NULL
;
2413 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2414 strip_options
, (int *) 0)) != EOF
)
2419 input_target
= optarg
;
2422 output_target
= optarg
;
2425 input_target
= output_target
= optarg
;
2428 p
= find_section_list (optarg
, TRUE
);
2430 sections_removed
= TRUE
;
2433 strip_symbols
= STRIP_ALL
;
2437 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2438 strip_symbols
= STRIP_DEBUG
;
2440 case OPTION_STRIP_UNNEEDED
:
2441 strip_symbols
= STRIP_UNNEEDED
;
2444 add_specific_symbol (optarg
, &keep_specific_list
);
2447 add_specific_symbol (optarg
, &strip_specific_list
);
2450 output_file
= optarg
;
2453 preserve_dates
= TRUE
;
2456 discard_locals
= LOCALS_ALL
;
2459 discard_locals
= LOCALS_START_L
;
2465 show_version
= TRUE
;
2467 case OPTION_FORMATS_INFO
:
2468 formats_info
= TRUE
;
2470 case OPTION_ONLY_KEEP_DEBUG
:
2471 strip_symbols
= STRIP_NONDEBUG
;
2473 case OPTION_KEEP_FILE_SYMBOLS
:
2474 keep_file_symbols
= 1;
2477 /* We've been given a long option. */
2484 strip_usage (stdout
, 0);
2486 strip_usage (stderr
, 1);
2497 print_version ("strip");
2499 /* Default is to strip all symbols. */
2500 if (strip_symbols
== STRIP_UNDEF
2501 && discard_locals
== LOCALS_UNDEF
2502 && strip_specific_list
== NULL
)
2503 strip_symbols
= STRIP_ALL
;
2505 if (output_target
== NULL
)
2506 output_target
= input_target
;
2510 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2511 strip_usage (stderr
, 1);
2513 for (; i
< argc
; i
++)
2515 int hold_status
= status
;
2516 struct stat statbuf
;
2519 if (get_file_size (argv
[i
]) < 1)
2523 /* No need to check the return value of stat().
2524 It has already been checked in get_file_size(). */
2525 stat (argv
[i
], &statbuf
);
2527 if (output_file
!= NULL
)
2528 tmpname
= output_file
;
2530 tmpname
= make_tempname (argv
[i
]);
2533 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2537 set_times (tmpname
, &statbuf
);
2538 if (output_file
== NULL
)
2539 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2540 status
= hold_status
;
2543 unlink_if_ordinary (tmpname
);
2544 if (output_file
== NULL
)
2552 copy_main (int argc
, char *argv
[])
2554 char * binary_architecture
= NULL
;
2555 char *input_filename
= NULL
;
2556 char *output_filename
= NULL
;
2557 char *input_target
= NULL
;
2558 char *output_target
= NULL
;
2559 bfd_boolean show_version
= FALSE
;
2560 bfd_boolean change_warn
= TRUE
;
2561 bfd_boolean formats_info
= FALSE
;
2563 struct section_list
*p
;
2564 struct stat statbuf
;
2566 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2567 copy_options
, (int *) 0)) != EOF
)
2572 copy_byte
= atoi (optarg
);
2574 fatal (_("byte number must be non-negative"));
2578 binary_architecture
= optarg
;
2582 interleave
= atoi (optarg
);
2584 fatal (_("interleave must be positive"));
2588 case 's': /* "source" - 'I' is preferred */
2589 input_target
= optarg
;
2593 case 'd': /* "destination" - 'O' is preferred */
2594 output_target
= optarg
;
2598 input_target
= output_target
= optarg
;
2602 p
= find_section_list (optarg
, TRUE
);
2604 fatal (_("%s both copied and removed"), optarg
);
2606 sections_copied
= TRUE
;
2610 p
= find_section_list (optarg
, TRUE
);
2612 fatal (_("%s both copied and removed"), optarg
);
2614 sections_removed
= TRUE
;
2618 strip_symbols
= STRIP_ALL
;
2622 strip_symbols
= STRIP_DEBUG
;
2625 case OPTION_STRIP_UNNEEDED
:
2626 strip_symbols
= STRIP_UNNEEDED
;
2629 case OPTION_ONLY_KEEP_DEBUG
:
2630 strip_symbols
= STRIP_NONDEBUG
;
2633 case OPTION_KEEP_FILE_SYMBOLS
:
2634 keep_file_symbols
= 1;
2637 case OPTION_ADD_GNU_DEBUGLINK
:
2638 gnu_debuglink_filename
= optarg
;
2642 add_specific_symbol (optarg
, &keep_specific_list
);
2646 add_specific_symbol (optarg
, &strip_specific_list
);
2649 case OPTION_STRIP_UNNEEDED_SYMBOL
:
2650 add_specific_symbol (optarg
, &strip_unneeded_list
);
2654 add_specific_symbol (optarg
, &localize_specific_list
);
2657 case OPTION_GLOBALIZE_SYMBOL
:
2658 add_specific_symbol (optarg
, &globalize_specific_list
);
2662 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2666 add_specific_symbol (optarg
, &weaken_specific_list
);
2670 preserve_dates
= TRUE
;
2678 discard_locals
= LOCALS_ALL
;
2682 discard_locals
= LOCALS_START_L
;
2690 show_version
= TRUE
;
2693 case OPTION_FORMATS_INFO
:
2694 formats_info
= TRUE
;
2701 case OPTION_ADD_SECTION
:
2705 struct section_add
*pa
;
2710 s
= strchr (optarg
, '=');
2713 fatal (_("bad format for %s"), "--add-section");
2715 size
= get_file_size (s
+ 1);
2719 pa
= xmalloc (sizeof (struct section_add
));
2722 name
= xmalloc (len
+ 1);
2723 strncpy (name
, optarg
, len
);
2727 pa
->filename
= s
+ 1;
2729 pa
->contents
= xmalloc (size
);
2731 f
= fopen (pa
->filename
, FOPEN_RB
);
2734 fatal (_("cannot open: %s: %s"),
2735 pa
->filename
, strerror (errno
));
2737 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2739 fatal (_("%s: fread failed"), pa
->filename
);
2743 pa
->next
= add_sections
;
2748 case OPTION_CHANGE_START
:
2749 change_start
= parse_vma (optarg
, "--change-start");
2752 case OPTION_CHANGE_SECTION_ADDRESS
:
2753 case OPTION_CHANGE_SECTION_LMA
:
2754 case OPTION_CHANGE_SECTION_VMA
:
2759 char *option
= NULL
;
2761 enum change_action what
= CHANGE_IGNORE
;
2765 case OPTION_CHANGE_SECTION_ADDRESS
:
2766 option
= "--change-section-address";
2768 case OPTION_CHANGE_SECTION_LMA
:
2769 option
= "--change-section-lma";
2771 case OPTION_CHANGE_SECTION_VMA
:
2772 option
= "--change-section-vma";
2776 s
= strchr (optarg
, '=');
2779 s
= strchr (optarg
, '+');
2782 s
= strchr (optarg
, '-');
2784 fatal (_("bad format for %s"), option
);
2789 name
= xmalloc (len
+ 1);
2790 strncpy (name
, optarg
, len
);
2793 p
= find_section_list (name
, TRUE
);
2795 val
= parse_vma (s
+ 1, option
);
2799 case '=': what
= CHANGE_SET
; break;
2800 case '-': val
= - val
; /* Drop through. */
2801 case '+': what
= CHANGE_MODIFY
; break;
2806 case OPTION_CHANGE_SECTION_ADDRESS
:
2807 p
->change_vma
= what
;
2811 case OPTION_CHANGE_SECTION_LMA
:
2812 p
->change_lma
= what
;
2816 case OPTION_CHANGE_SECTION_VMA
:
2817 p
->change_vma
= what
;
2824 case OPTION_CHANGE_ADDRESSES
:
2825 change_section_address
= parse_vma (optarg
, "--change-addresses");
2826 change_start
= change_section_address
;
2829 case OPTION_CHANGE_WARNINGS
:
2833 case OPTION_CHANGE_LEADING_CHAR
:
2834 change_leading_char
= TRUE
;
2837 case OPTION_DEBUGGING
:
2838 convert_debugging
= TRUE
;
2841 case OPTION_GAP_FILL
:
2843 bfd_vma gap_fill_vma
;
2845 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2846 gap_fill
= (bfd_byte
) gap_fill_vma
;
2847 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2851 sprintf_vma (buff
, gap_fill_vma
);
2853 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2856 gap_fill_set
= TRUE
;
2860 case OPTION_NO_CHANGE_WARNINGS
:
2861 change_warn
= FALSE
;
2865 pad_to
= parse_vma (optarg
, "--pad-to");
2869 case OPTION_REMOVE_LEADING_CHAR
:
2870 remove_leading_char
= TRUE
;
2873 case OPTION_REDEFINE_SYM
:
2875 /* Push this redefinition onto redefine_symbol_list. */
2879 const char *nextarg
;
2880 char *source
, *target
;
2882 s
= strchr (optarg
, '=');
2884 fatal (_("bad format for %s"), "--redefine-sym");
2887 source
= xmalloc (len
+ 1);
2888 strncpy (source
, optarg
, len
);
2892 len
= strlen (nextarg
);
2893 target
= xmalloc (len
+ 1);
2894 strcpy (target
, nextarg
);
2896 redefine_list_append ("--redefine-sym", source
, target
);
2903 case OPTION_REDEFINE_SYMS
:
2904 add_redefine_syms_file (optarg
);
2907 case OPTION_SET_SECTION_FLAGS
:
2913 s
= strchr (optarg
, '=');
2915 fatal (_("bad format for %s"), "--set-section-flags");
2918 name
= xmalloc (len
+ 1);
2919 strncpy (name
, optarg
, len
);
2922 p
= find_section_list (name
, TRUE
);
2924 p
->set_flags
= TRUE
;
2925 p
->flags
= parse_flags (s
+ 1);
2929 case OPTION_RENAME_SECTION
:
2932 const char *eq
, *fl
;
2937 eq
= strchr (optarg
, '=');
2939 fatal (_("bad format for %s"), "--rename-section");
2943 fatal (_("bad format for %s"), "--rename-section");
2945 old_name
= xmalloc (len
+ 1);
2946 strncpy (old_name
, optarg
, len
);
2950 fl
= strchr (eq
, ',');
2953 flags
= parse_flags (fl
+ 1);
2963 fatal (_("bad format for %s"), "--rename-section");
2965 new_name
= xmalloc (len
+ 1);
2966 strncpy (new_name
, eq
, len
);
2969 add_section_rename (old_name
, new_name
, flags
);
2973 case OPTION_SET_START
:
2974 set_start
= parse_vma (optarg
, "--set-start");
2975 set_start_set
= TRUE
;
2978 case OPTION_SREC_LEN
:
2979 Chunk
= parse_vma (optarg
, "--srec-len");
2982 case OPTION_SREC_FORCES3
:
2986 case OPTION_STRIP_SYMBOLS
:
2987 add_specific_symbols (optarg
, &strip_specific_list
);
2990 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
2991 add_specific_symbols (optarg
, &strip_unneeded_list
);
2994 case OPTION_KEEP_SYMBOLS
:
2995 add_specific_symbols (optarg
, &keep_specific_list
);
2998 case OPTION_LOCALIZE_SYMBOLS
:
2999 add_specific_symbols (optarg
, &localize_specific_list
);
3002 case OPTION_GLOBALIZE_SYMBOLS
:
3003 add_specific_symbols (optarg
, &globalize_specific_list
);
3006 case OPTION_KEEPGLOBAL_SYMBOLS
:
3007 add_specific_symbols (optarg
, &keepglobal_specific_list
);
3010 case OPTION_WEAKEN_SYMBOLS
:
3011 add_specific_symbols (optarg
, &weaken_specific_list
);
3014 case OPTION_ALT_MACH_CODE
:
3015 use_alt_mach_code
= atoi (optarg
);
3016 if (use_alt_mach_code
<= 0)
3017 fatal (_("alternate machine code index must be positive"));
3020 case OPTION_PREFIX_SYMBOLS
:
3021 prefix_symbols_string
= optarg
;
3024 case OPTION_PREFIX_SECTIONS
:
3025 prefix_sections_string
= optarg
;
3028 case OPTION_PREFIX_ALLOC_SECTIONS
:
3029 prefix_alloc_sections_string
= optarg
;
3032 case OPTION_READONLY_TEXT
:
3033 bfd_flags_to_set
|= WP_TEXT
;
3034 bfd_flags_to_clear
&= ~WP_TEXT
;
3037 case OPTION_WRITABLE_TEXT
:
3038 bfd_flags_to_clear
|= WP_TEXT
;
3039 bfd_flags_to_set
&= ~WP_TEXT
;
3043 bfd_flags_to_set
|= D_PAGED
;
3044 bfd_flags_to_clear
&= ~D_PAGED
;
3048 bfd_flags_to_clear
|= D_PAGED
;
3049 bfd_flags_to_set
&= ~D_PAGED
;
3053 /* We've been given a long option. */
3058 copy_usage (stdout
, 0);
3061 copy_usage (stderr
, 1);
3072 print_version ("objcopy");
3074 if (copy_byte
>= interleave
)
3075 fatal (_("byte number must be less than interleave"));
3077 if (optind
== argc
|| optind
+ 2 < argc
)
3078 copy_usage (stderr
, 1);
3080 input_filename
= argv
[optind
];
3081 if (optind
+ 1 < argc
)
3082 output_filename
= argv
[optind
+ 1];
3084 /* Default is to strip no symbols. */
3085 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
3086 strip_symbols
= STRIP_NONE
;
3088 if (output_target
== NULL
)
3089 output_target
= input_target
;
3091 if (binary_architecture
!= NULL
)
3093 if (input_target
&& strcmp (input_target
, "binary") == 0)
3095 const bfd_arch_info_type
* temp_arch_info
;
3097 temp_arch_info
= bfd_scan_arch (binary_architecture
);
3099 if (temp_arch_info
!= NULL
)
3101 bfd_external_binary_architecture
= temp_arch_info
->arch
;
3102 bfd_external_machine
= temp_arch_info
->mach
;
3105 fatal (_("architecture %s unknown"), binary_architecture
);
3109 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3110 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3115 if (stat (input_filename
, & statbuf
) < 0)
3116 fatal (_("warning: could not locate '%s'. System error message: %s"),
3117 input_filename
, strerror (errno
));
3119 /* If there is no destination file, or the source and destination files
3120 are the same, then create a temp and rename the result into the input. */
3121 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3123 char *tmpname
= make_tempname (input_filename
);
3125 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3129 set_times (tmpname
, &statbuf
);
3130 smart_rename (tmpname
, input_filename
, preserve_dates
);
3137 copy_file (input_filename
, output_filename
, input_target
, output_target
);
3139 if (status
== 0 && preserve_dates
)
3140 set_times (output_filename
, &statbuf
);
3141 else if (status
!= 0)
3142 unlink_if_ordinary (output_filename
);
3147 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3151 if (p
->change_vma
!= CHANGE_IGNORE
)
3155 sprintf_vma (buff
, p
->vma_val
);
3157 /* xgettext:c-format */
3158 non_fatal (_("%s %s%c0x%s never used"),
3159 "--change-section-vma",
3161 p
->change_vma
== CHANGE_SET
? '=' : '+',
3165 if (p
->change_lma
!= CHANGE_IGNORE
)
3169 sprintf_vma (buff
, p
->lma_val
);
3171 /* xgettext:c-format */
3172 non_fatal (_("%s %s%c0x%s never used"),
3173 "--change-section-lma",
3175 p
->change_lma
== CHANGE_SET
? '=' : '+',
3186 main (int argc
, char *argv
[])
3188 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3189 setlocale (LC_MESSAGES
, "");
3191 #if defined (HAVE_SETLOCALE)
3192 setlocale (LC_CTYPE
, "");
3194 bindtextdomain (PACKAGE
, LOCALEDIR
);
3195 textdomain (PACKAGE
);
3197 program_name
= argv
[0];
3198 xmalloc_set_program_name (program_name
);
3200 START_PROGRESS (program_name
, 0);
3202 expandargv (&argc
, &argv
);
3204 strip_symbols
= STRIP_UNDEF
;
3205 discard_locals
= LOCALS_UNDEF
;
3208 set_default_bfd_target ();
3212 int i
= strlen (program_name
);
3213 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3214 /* Drop the .exe suffix, if any. */
3215 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3218 program_name
[i
] = '\0';
3221 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3225 strip_main (argc
, argv
);
3227 copy_main (argc
, argv
);
3229 END_PROGRESS (program_name
);