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,
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., 59 Temple Place - Suite 330, Boston, MA
27 #include "libiberty.h"
29 #include "filenames.h"
32 /* A list of symbols to explicitly strip out, or to keep. A linked
33 list is good enough for a small number from the command line, but
34 this will slow things down a lot if many symbols are being
43 /* A list to support redefine_sym. */
48 struct redefine_node
*next
;
51 typedef struct section_rename
53 const char * old_name
;
54 const char * new_name
;
56 struct section_rename
* next
;
60 /* List of sections to be renamed. */
61 static section_rename
* section_rename_list
;
63 static void copy_usage
64 PARAMS ((FILE *, int));
65 static void strip_usage
66 PARAMS ((FILE *, int));
67 static flagword parse_flags
68 PARAMS ((const char *));
69 static struct section_list
*find_section_list
70 PARAMS ((const char *, bfd_boolean
));
71 static void setup_section
72 PARAMS ((bfd
*, asection
*, PTR
));
73 static void copy_section
74 PARAMS ((bfd
*, asection
*, PTR
));
75 static void get_sections
76 PARAMS ((bfd
*, asection
*, PTR
));
77 static int compare_section_lma
78 PARAMS ((const PTR
, const PTR
));
79 static void add_specific_symbol
80 PARAMS ((const char *, struct symlist
**));
81 static void add_specific_symbols
82 PARAMS ((const char *, struct symlist
**));
83 static bfd_boolean is_specified_symbol
84 PARAMS ((const char *, struct symlist
*));
85 static bfd_boolean is_strip_section
86 PARAMS ((bfd
*, asection
*));
87 static unsigned int filter_symbols
88 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
89 static void mark_symbols_used_in_relocations
90 PARAMS ((bfd
*, asection
*, PTR
));
91 static void filter_bytes
92 PARAMS ((char *, bfd_size_type
*));
93 static bfd_boolean write_debugging_info
94 PARAMS ((bfd
*, PTR
, long *, asymbol
***));
95 static void copy_object
96 PARAMS ((bfd
*, bfd
*));
97 static void copy_archive
98 PARAMS ((bfd
*, bfd
*, const char *));
100 PARAMS ((const char *, const char *, const char *, const char *));
101 static int strip_main
102 PARAMS ((int, char **));
104 PARAMS ((int, char **));
105 static const char *lookup_sym_redefinition
106 PARAMS((const char *));
107 static void redefine_list_append
108 PARAMS ((const char *, const char *, const char *));
109 static const char * find_section_rename
110 PARAMS ((bfd
*, sec_ptr
, flagword
*));
111 static void add_section_rename
112 PARAMS ((const char *, const char *, flagword
));
113 static void add_redefine_syms_file
114 PARAMS ((const char *));
116 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
118 static asymbol
**isympp
= NULL
; /* Input symbols */
119 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
121 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
122 static int copy_byte
= -1;
123 static int interleave
= 4;
125 static bfd_boolean verbose
; /* Print file and target names. */
126 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
127 static int status
= 0; /* Exit status. */
132 STRIP_NONE
, /* don't strip */
133 STRIP_DEBUG
, /* strip all debugger symbols */
134 STRIP_UNNEEDED
, /* strip unnecessary symbols */
135 STRIP_NONDEBUG
, /* Strip everything but debug info. */
136 STRIP_ALL
/* strip all symbols */
139 /* Which symbols to remove. */
140 static enum strip_action strip_symbols
;
145 LOCALS_START_L
, /* discard locals starting with L */
146 LOCALS_ALL
/* discard all locals */
149 /* Which local symbols to remove. Overrides STRIP_ALL. */
150 static enum locals_action discard_locals
;
152 /* What kind of change to perform. */
160 /* Structure used to hold lists of sections and actions to take. */
163 struct section_list
* next
; /* Next section to change. */
164 const char * name
; /* Section name. */
165 bfd_boolean used
; /* Whether this entry was used. */
166 bfd_boolean remove
; /* Whether to remove this section. */
167 bfd_boolean copy
; /* Whether to copy this section. */
168 enum change_action change_vma
;/* Whether to change or set VMA. */
169 bfd_vma vma_val
; /* Amount to change by or set to. */
170 enum change_action change_lma
;/* Whether to change or set LMA. */
171 bfd_vma lma_val
; /* Amount to change by or set to. */
172 bfd_boolean set_flags
; /* Whether to set the section flags. */
173 flagword flags
; /* What to set the section flags to. */
176 static struct section_list
*change_sections
;
178 /* TRUE if some sections are to be removed. */
179 static bfd_boolean sections_removed
;
181 /* TRUE if only some sections are to be copied. */
182 static bfd_boolean sections_copied
;
184 /* Changes to the start address. */
185 static bfd_vma change_start
= 0;
186 static bfd_boolean set_start_set
= FALSE
;
187 static bfd_vma set_start
;
189 /* Changes to section addresses. */
190 static bfd_vma change_section_address
= 0;
192 /* Filling gaps between sections. */
193 static bfd_boolean gap_fill_set
= FALSE
;
194 static bfd_byte gap_fill
= 0;
196 /* Pad to a given address. */
197 static bfd_boolean pad_to_set
= FALSE
;
198 static bfd_vma pad_to
;
200 /* Use alternate machine code? */
201 static int use_alt_mach_code
= 0;
203 /* List of sections to add. */
206 /* Next section to add. */
207 struct section_add
*next
;
208 /* Name of section to add. */
210 /* Name of file holding section contents. */
211 const char *filename
;
214 /* Contents of file. */
216 /* BFD section, after it has been added. */
220 /* List of sections to add to the output BFD. */
221 static struct section_add
*add_sections
;
223 /* If non-NULL the argument to --add-gnu-debuglink.
224 This should be the filename to store in the .gnu_debuglink section. */
225 static const char * gnu_debuglink_filename
= NULL
;
227 /* Whether to convert debugging information. */
228 static bfd_boolean convert_debugging
= FALSE
;
230 /* Whether to change the leading character in symbol names. */
231 static bfd_boolean change_leading_char
= FALSE
;
233 /* Whether to remove the leading character from global symbol names. */
234 static bfd_boolean remove_leading_char
= FALSE
;
236 /* List of symbols to strip, keep, localize, keep-global, weaken,
238 static struct symlist
*strip_specific_list
= NULL
;
239 static struct symlist
*keep_specific_list
= NULL
;
240 static struct symlist
*localize_specific_list
= NULL
;
241 static struct symlist
*keepglobal_specific_list
= NULL
;
242 static struct symlist
*weaken_specific_list
= NULL
;
243 static struct redefine_node
*redefine_sym_list
= NULL
;
245 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
246 static bfd_boolean weaken
= FALSE
;
248 /* Prefix symbols/sections. */
249 static char *prefix_symbols_string
= 0;
250 static char *prefix_sections_string
= 0;
251 static char *prefix_alloc_sections_string
= 0;
253 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
255 #define OPTION_ADD_SECTION 150
256 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
257 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
258 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
259 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
260 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
261 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
262 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
263 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
264 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
265 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
266 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
267 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
268 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
269 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
270 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
271 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
272 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
273 #define OPTION_REDEFINE_SYMS (OPTION_REDEFINE_SYM + 1)
274 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYMS + 1)
275 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
276 #define OPTION_STRIP_SYMBOLS (OPTION_SREC_FORCES3 + 1)
277 #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1)
278 #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
279 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
280 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
281 #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
282 #define OPTION_ALT_MACH_CODE (OPTION_RENAME_SECTION + 1)
283 #define OPTION_PREFIX_SYMBOLS (OPTION_ALT_MACH_CODE + 1)
284 #define OPTION_PREFIX_SECTIONS (OPTION_PREFIX_SYMBOLS + 1)
285 #define OPTION_PREFIX_ALLOC_SECTIONS (OPTION_PREFIX_SECTIONS + 1)
286 #define OPTION_FORMATS_INFO (OPTION_PREFIX_ALLOC_SECTIONS + 1)
287 #define OPTION_ADD_GNU_DEBUGLINK (OPTION_FORMATS_INFO + 1)
288 #define OPTION_ONLY_KEEP_DEBUG (OPTION_ADD_GNU_DEBUGLINK + 1)
290 /* Options to handle if running as "strip". */
292 static struct option strip_options
[] =
294 {"discard-all", no_argument
, 0, 'x'},
295 {"discard-locals", no_argument
, 0, 'X'},
296 {"format", required_argument
, 0, 'F'}, /* Obsolete */
297 {"help", no_argument
, 0, 'h'},
298 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
299 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
300 {"input-target", required_argument
, 0, 'I'},
301 {"keep-symbol", required_argument
, 0, 'K'},
302 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
303 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
304 {"output-target", required_argument
, 0, 'O'},
305 {"output-file", required_argument
, 0, 'o'},
306 {"preserve-dates", no_argument
, 0, 'p'},
307 {"remove-section", required_argument
, 0, 'R'},
308 {"strip-all", no_argument
, 0, 's'},
309 {"strip-debug", no_argument
, 0, 'S'},
310 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
311 {"strip-symbol", required_argument
, 0, 'N'},
312 {"target", required_argument
, 0, 'F'},
313 {"verbose", no_argument
, 0, 'v'},
314 {"version", no_argument
, 0, 'V'},
315 {0, no_argument
, 0, 0}
318 /* Options to handle if running as "objcopy". */
320 static struct option copy_options
[] =
322 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
323 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
324 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
325 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
326 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
327 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
328 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
329 {"binary-architecture", required_argument
, 0, 'B'},
330 {"byte", required_argument
, 0, 'b'},
331 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
332 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
333 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
334 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
335 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
336 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
337 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
338 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
339 {"discard-all", no_argument
, 0, 'x'},
340 {"discard-locals", no_argument
, 0, 'X'},
341 {"format", required_argument
, 0, 'F'}, /* Obsolete */
342 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
343 {"help", no_argument
, 0, 'h'},
344 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
345 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
346 {"input-target", required_argument
, 0, 'I'},
347 {"interleave", required_argument
, 0, 'i'},
348 {"keep-global-symbol", required_argument
, 0, 'G'},
349 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
350 {"keep-symbol", required_argument
, 0, 'K'},
351 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
352 {"localize-symbol", required_argument
, 0, 'L'},
353 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
354 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
355 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
356 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
357 {"only-section", required_argument
, 0, 'j'},
358 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
359 {"output-target", required_argument
, 0, 'O'},
360 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
361 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
362 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
363 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
364 {"preserve-dates", no_argument
, 0, 'p'},
365 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
366 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
367 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
368 {"remove-section", required_argument
, 0, 'R'},
369 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
370 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
371 {"set-start", required_argument
, 0, OPTION_SET_START
},
372 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
373 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
374 {"strip-all", no_argument
, 0, 'S'},
375 {"strip-debug", no_argument
, 0, 'g'},
376 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
377 {"strip-symbol", required_argument
, 0, 'N'},
378 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
379 {"target", required_argument
, 0, 'F'},
380 {"verbose", no_argument
, 0, 'v'},
381 {"version", no_argument
, 0, 'V'},
382 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
383 {"weaken-symbol", required_argument
, 0, 'W'},
384 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
385 {0, no_argument
, 0, 0}
389 extern char *program_name
;
391 /* This flag distinguishes between strip and objcopy:
392 1 means this is 'strip'; 0 means this is 'objcopy'.
393 -1 means if we should use argv[0] to decide. */
396 /* The maximum length of an S record. This variable is declared in srec.c
397 and can be modified by the --srec-len parameter. */
398 extern unsigned int Chunk
;
400 /* Restrict the generation of Srecords to type S3 only.
401 This variable is declare in bfd/srec.c and can be toggled
402 on by the --srec-forceS3 command line switch. */
403 extern bfd_boolean S3Forced
;
405 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
406 extern enum bfd_architecture bfd_external_binary_architecture
;
410 copy_usage (stream
, exit_status
)
414 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
415 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
416 fprintf (stream
, _(" The options are:\n"));
417 fprintf (stream
, _("\
418 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
419 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
420 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
421 -F --target <bfdname> Set both input and output format to <bfdname>\n\
422 --debugging Convert debugging information, if possible\n\
423 -p --preserve-dates Copy modified/access timestamps to the output\n\
424 -j --only-section <name> Only copy section <name> into the output\n\
425 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
426 -R --remove-section <name> Remove section <name> from the output\n\
427 -S --strip-all Remove all symbol and relocation information\n\
428 -g --strip-debug Remove all debugging symbols & sections\n\
429 --strip-unneeded Remove all symbols not needed by relocations\n\
430 -N --strip-symbol <name> Do not copy symbol <name>\n\
431 -K --keep-symbol <name> Only copy symbol <name>\n\
432 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
433 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
434 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
435 --weaken Force all global symbols to be marked as weak\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 --keep-symbols <file> -K for all symbols listed in <file>\n\
468 --localize-symbols <file> -L for all symbols listed in <file>\n\
469 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
470 --weaken-symbols <file> -W for all symbols listed in <file>\n\
471 --alt-machine-code <index> Use alternate machine code for output\n\
472 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
473 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
474 --prefix-alloc-sections <prefix>\n\
475 Add <prefix> to start of every allocatable\n\
477 -v --verbose List all object files modified\n\
478 -V --version Display this program's version number\n\
479 -h --help Display this output\n\
480 --info List object formats & architectures supported\n\
482 list_supported_targets (program_name
, stream
);
483 if (exit_status
== 0)
484 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
489 strip_usage (stream
, exit_status
)
493 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
494 fprintf (stream
, _(" Removes symbols and sections from files\n"));
495 fprintf (stream
, _(" The options are:\n"));
496 fprintf (stream
, _("\
497 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
498 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
499 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
500 -p --preserve-dates Copy modified/access timestamps to the output\n\
501 -R --remove-section=<name> Remove section <name> from the output\n\
502 -s --strip-all Remove all symbol and relocation information\n\
503 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
504 --strip-unneeded Remove all symbols not needed by relocations\n\
505 -N --strip-symbol=<name> Do not copy symbol <name>\n\
506 -K --keep-symbol=<name> Only copy symbol <name>\n\
507 -x --discard-all Remove all non-global symbols\n\
508 -X --discard-locals Remove any compiler-generated symbols\n\
509 -v --verbose List all object files modified\n\
510 -V --version Display this program's version number\n\
511 -h --help Display this output\n\
512 --info List object formats & architectures supported\n\
513 -o <file> Place stripped output into <file>\n\
516 list_supported_targets (program_name
, stream
);
517 if (exit_status
== 0)
518 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
522 /* Parse section flags into a flagword, with a fatal error if the
523 string can't be parsed. */
537 snext
= strchr (s
, ',');
547 #define PARSE_FLAG(fname,fval) \
548 else if (strncasecmp (fname, s, len) == 0) ret |= fval
549 PARSE_FLAG ("alloc", SEC_ALLOC
);
550 PARSE_FLAG ("load", SEC_LOAD
);
551 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
552 PARSE_FLAG ("readonly", SEC_READONLY
);
553 PARSE_FLAG ("debug", SEC_DEBUGGING
);
554 PARSE_FLAG ("code", SEC_CODE
);
555 PARSE_FLAG ("data", SEC_DATA
);
556 PARSE_FLAG ("rom", SEC_ROM
);
557 PARSE_FLAG ("share", SEC_SHARED
);
558 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
564 copy
= xmalloc (len
+ 1);
565 strncpy (copy
, s
, len
);
567 non_fatal (_("unrecognized section flag `%s'"), copy
);
568 fatal (_("supported flags: %s"),
569 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
579 /* Find and optionally add an entry in the change_sections list. */
581 static struct section_list
*
582 find_section_list (name
, add
)
586 register struct section_list
*p
;
588 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
589 if (strcmp (p
->name
, name
) == 0)
595 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
600 p
->change_vma
= CHANGE_IGNORE
;
601 p
->change_lma
= CHANGE_IGNORE
;
604 p
->set_flags
= FALSE
;
607 p
->next
= change_sections
;
613 /* Add a symbol to strip_specific_list. */
616 add_specific_symbol (name
, list
)
618 struct symlist
**list
;
620 struct symlist
*tmp_list
;
622 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
623 tmp_list
->name
= name
;
624 tmp_list
->next
= *list
;
628 /* Add symbols listed in `filename' to strip_specific_list. */
630 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
631 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
634 add_specific_symbols (filename
, list
)
635 const char *filename
;
636 struct symlist
**list
;
642 unsigned int line_count
;
644 if (stat (filename
, & st
) < 0)
645 fatal (_("cannot stat: %s: %s"), filename
, strerror (errno
));
649 buffer
= (char *) xmalloc (st
.st_size
+ 2);
650 f
= fopen (filename
, FOPEN_RT
);
652 fatal (_("cannot open: %s: %s"), filename
, strerror (errno
));
654 if (fread (buffer
, 1, st
.st_size
, f
) == 0 || ferror (f
))
655 fatal (_("%s: fread failed"), filename
);
658 buffer
[st
.st_size
] = '\n';
659 buffer
[st
.st_size
+ 1] = '\0';
663 for (line
= buffer
; * line
!= '\0'; line
++)
668 int finished
= FALSE
;
670 for (eol
= line
;; eol
++)
676 /* Cope with \n\r. */
684 /* Cope with \r\n. */
695 /* Line comment, Terminate the line here, in case a
696 name is present and then allow the rest of the
697 loop to find the real end of the line. */
709 /* A name may now exist somewhere between 'line' and 'eol'.
710 Strip off leading whitespace and trailing whitespace,
711 then add it to the list. */
712 for (name
= line
; IS_WHITESPACE (* name
); name
++)
714 for (name_end
= name
;
715 (! IS_WHITESPACE (* name_end
))
716 && (! IS_LINE_TERMINATOR (* name_end
));
720 if (! IS_LINE_TERMINATOR (* name_end
))
724 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
727 if (! IS_LINE_TERMINATOR (* extra
))
728 non_fatal (_("Ignoring rubbish found on line %d of %s"),
729 line_count
, filename
);
735 add_specific_symbol (name
, list
);
737 /* Advance line pointer to end of line. The 'eol ++' in the for
738 loop above will then advance us to the start of the next line. */
744 /* See whether a symbol should be stripped or kept based on
745 strip_specific_list and keep_symbols. */
748 is_specified_symbol (name
, list
)
750 struct symlist
*list
;
752 struct symlist
*tmp_list
;
754 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
755 if (strcmp (name
, tmp_list
->name
) == 0)
761 /* See if a section is being removed. */
764 is_strip_section (abfd
, sec
)
765 bfd
*abfd ATTRIBUTE_UNUSED
;
768 if (sections_removed
|| sections_copied
)
770 struct section_list
*p
;
772 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
774 if (sections_removed
&& p
!= NULL
&& p
->remove
)
776 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
780 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
782 if (strip_symbols
== STRIP_DEBUG
783 || strip_symbols
== STRIP_UNNEEDED
784 || strip_symbols
== STRIP_ALL
785 || discard_locals
== LOCALS_ALL
786 || convert_debugging
)
789 if (strip_symbols
== STRIP_NONDEBUG
)
793 return strip_symbols
== STRIP_NONDEBUG
? TRUE
: FALSE
;
796 /* Choose which symbol entries to copy; put the result in OSYMS.
797 We don't copy in place, because that confuses the relocs.
798 Return the number of symbols to print. */
801 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
804 asymbol
**osyms
, **isyms
;
807 register asymbol
**from
= isyms
, **to
= osyms
;
808 long src_count
= 0, dst_count
= 0;
809 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
812 for (; src_count
< symcount
; src_count
++)
814 asymbol
*sym
= from
[src_count
];
815 flagword flags
= sym
->flags
;
816 char *name
= (char *) bfd_asymbol_name (sym
);
818 bfd_boolean undefined
;
819 bfd_boolean rem_leading_char
;
820 bfd_boolean add_leading_char
;
822 undefined
= bfd_is_und_section (bfd_get_section (sym
));
824 if (redefine_sym_list
)
826 char *old_name
, *new_name
;
828 old_name
= (char *) bfd_asymbol_name (sym
);
829 new_name
= (char *) lookup_sym_redefinition (old_name
);
830 bfd_asymbol_name (sym
) = new_name
;
834 /* Check if we will remove the current leading character. */
836 (name
[0] == bfd_get_symbol_leading_char (abfd
))
837 && (change_leading_char
838 || (remove_leading_char
839 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
841 || bfd_is_com_section (bfd_get_section (sym
)))));
843 /* Check if we will add a new leading character. */
846 && (bfd_get_symbol_leading_char (obfd
) != '\0')
847 && (bfd_get_symbol_leading_char (abfd
) == '\0'
848 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
850 /* Short circuit for change_leading_char if we can do it in-place. */
851 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
853 name
[0] = bfd_get_symbol_leading_char (obfd
);
854 bfd_asymbol_name (sym
) = name
;
855 rem_leading_char
= FALSE
;
856 add_leading_char
= FALSE
;
859 /* Remove leading char. */
860 if (rem_leading_char
)
861 bfd_asymbol_name (sym
) = ++name
;
863 /* Add new leading char and/or prefix. */
864 if (add_leading_char
|| prefix_symbols_string
)
868 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
) + strlen (name
) + 1);
869 if (add_leading_char
)
870 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
872 if (prefix_symbols_string
)
874 strcpy (ptr
, prefix_symbols_string
);
875 ptr
+= strlen (prefix_symbols_string
);
879 bfd_asymbol_name (sym
) = n
;
883 if (strip_symbols
== STRIP_ALL
)
885 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
886 || ((flags
& BSF_SECTION_SYM
) != 0
887 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
890 else if (relocatable
/* Relocatable file. */
891 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
893 else if (bfd_decode_symclass (sym
) == 'I')
894 /* Global symbols in $idata sections need to be retained
895 even if relocatable is FALSE. External users of the
896 library containing the $idata section may reference these
899 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
900 || (flags
& BSF_WEAK
) != 0
902 || bfd_is_com_section (bfd_get_section (sym
)))
903 keep
= strip_symbols
!= STRIP_UNNEEDED
;
904 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
905 keep
= (strip_symbols
!= STRIP_DEBUG
906 && strip_symbols
!= STRIP_UNNEEDED
907 && ! convert_debugging
);
908 else if (bfd_get_section (sym
)->comdat
)
909 /* COMDAT sections store special information in local
910 symbols, so we cannot risk stripping any of them. */
912 else /* Local symbol. */
913 keep
= (strip_symbols
!= STRIP_UNNEEDED
914 && (discard_locals
!= LOCALS_ALL
915 && (discard_locals
!= LOCALS_START_L
916 || ! bfd_is_local_label (abfd
, sym
))));
918 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
920 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
922 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
925 if (keep
&& (flags
& BSF_GLOBAL
) != 0
926 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
928 sym
->flags
&=~ BSF_GLOBAL
;
929 sym
->flags
|= BSF_WEAK
;
931 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
932 && (is_specified_symbol (name
, localize_specific_list
)
933 || (keepglobal_specific_list
!= NULL
934 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
936 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
937 sym
->flags
|= BSF_LOCAL
;
941 to
[dst_count
++] = sym
;
944 to
[dst_count
] = NULL
;
949 /* Find the redefined name of symbol SOURCE. */
952 lookup_sym_redefinition (source
)
955 struct redefine_node
*list
;
957 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
958 if (strcmp (source
, list
->source
) == 0)
964 /* Add a node to a symbol redefine list. */
967 redefine_list_append (cause
, source
, target
)
972 struct redefine_node
**p
;
973 struct redefine_node
*list
;
974 struct redefine_node
*new_node
;
976 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
978 if (strcmp (source
, list
->source
) == 0)
979 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
982 if (strcmp (target
, list
->target
) == 0)
983 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
987 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
989 new_node
->source
= strdup (source
);
990 new_node
->target
= strdup (target
);
991 new_node
->next
= NULL
;
996 /* Handle the --redefine-syms option. Read lines containing "old new"
997 from the file, and add them to the symbol redefine list. */
1000 add_redefine_syms_file (filename
)
1001 const char *filename
;
1005 size_t bufsize
, len
, outsym_off
;
1008 file
= fopen (filename
, "r");
1009 if (file
== (FILE *) NULL
)
1010 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1011 filename
, strerror (errno
));
1014 buf
= (char *) xmalloc (bufsize
);
1022 /* Collect the input symbol name. */
1023 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1031 buf
= xrealloc (buf
, bufsize
);
1039 /* Eat white space between the symbol names. */
1040 while (IS_WHITESPACE (c
))
1042 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1047 /* Collect the output symbol name. */
1049 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1057 buf
= xrealloc (buf
, bufsize
);
1065 /* Eat white space at end of line. */
1066 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1071 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1072 || c
== '\n' || c
== EOF
)
1075 /* Append the redefinition to the list. */
1077 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1088 fatal (_("%s: garbage at end of line %d"), filename
, lineno
);
1090 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1091 fatal (_("%s: missing new symbol name at line %d"), filename
, lineno
);
1094 /* Eat the rest of the line and finish it. */
1095 while (c
!= '\n' && c
!= EOF
)
1101 fatal (_("%s: premature end of file at line %d"), filename
, lineno
);
1106 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
1110 filter_bytes (memhunk
, size
)
1112 bfd_size_type
*size
;
1114 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
1116 for (; from
< end
; from
+= interleave
)
1119 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
1120 *size
= (*size
/ interleave
) + 1;
1122 *size
/= interleave
;
1125 /* Copy object file IBFD onto OBFD. */
1128 copy_object (ibfd
, obfd
)
1134 asection
**osections
= NULL
;
1135 bfd_size_type
*gaps
= NULL
;
1136 bfd_size_type max_gap
= 0;
1139 enum bfd_architecture iarch
;
1142 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1143 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1144 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1146 fatal (_("Unable to change endianness of input file(s)"));
1150 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1151 RETURN_NONFATAL (bfd_get_filename (obfd
));
1154 printf (_("copy from %s(%s) to %s(%s)\n"),
1155 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
1156 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1161 start
= bfd_get_start_address (ibfd
);
1162 start
+= change_start
;
1164 /* Neither the start address nor the flags
1165 need to be set for a core file. */
1166 if (bfd_get_format (obfd
) != bfd_core
)
1168 if (!bfd_set_start_address (obfd
, start
)
1169 || !bfd_set_file_flags (obfd
,
1170 (bfd_get_file_flags (ibfd
)
1171 & bfd_applicable_file_flags (obfd
))))
1172 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1175 /* Copy architecture of input file to output file. */
1176 iarch
= bfd_get_arch (ibfd
);
1177 imach
= bfd_get_mach (ibfd
);
1178 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1179 && (ibfd
->target_defaulted
1180 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1181 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1182 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1183 bfd_get_mach (ibfd
)));
1185 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1186 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1191 if (osympp
!= isympp
)
1194 /* BFD mandates that all output sections be created and sizes set before
1195 any output is done. Thus, we traverse all sections multiple times. */
1196 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
1198 if (add_sections
!= NULL
)
1200 struct section_add
*padd
;
1201 struct section_list
*pset
;
1203 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1207 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1208 if (padd
->section
== NULL
)
1210 non_fatal (_("can't create section `%s': %s"),
1211 padd
->name
, bfd_errmsg (bfd_get_error ()));
1216 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1217 RETURN_NONFATAL (bfd_get_filename (obfd
));
1219 pset
= find_section_list (padd
->name
, FALSE
);
1223 if (pset
!= NULL
&& pset
->set_flags
)
1224 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1226 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1228 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1229 RETURN_NONFATAL (bfd_get_filename (obfd
));
1233 if (pset
->change_vma
!= CHANGE_IGNORE
)
1234 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
1235 RETURN_NONFATAL (bfd_get_filename (obfd
));
1237 if (pset
->change_lma
!= CHANGE_IGNORE
)
1239 padd
->section
->lma
= pset
->lma_val
;
1241 if (! bfd_set_section_alignment
1242 (obfd
, padd
->section
,
1243 bfd_section_alignment (obfd
, padd
->section
)))
1244 RETURN_NONFATAL (bfd_get_filename (obfd
));
1250 if (gnu_debuglink_filename
!= NULL
)
1252 if (! bfd_add_gnu_debuglink (obfd
, gnu_debuglink_filename
))
1253 RETURN_NONFATAL (gnu_debuglink_filename
);
1256 if (gap_fill_set
|| pad_to_set
)
1261 /* We must fill in gaps between the sections and/or we must pad
1262 the last section to a specified address. We do this by
1263 grabbing a list of the sections, sorting them by VMA, and
1264 increasing the section sizes as required to fill the gaps.
1265 We write out the gap contents below. */
1267 c
= bfd_count_sections (obfd
);
1268 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
1270 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
1272 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1274 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
1275 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1279 for (i
= 0; i
< c
- 1; i
++)
1283 bfd_vma gap_start
, gap_stop
;
1285 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1286 if ((flags
& SEC_HAS_CONTENTS
) == 0
1287 || (flags
& SEC_LOAD
) == 0)
1290 size
= bfd_section_size (obfd
, osections
[i
]);
1291 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1292 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1293 if (gap_start
< gap_stop
)
1295 if (! bfd_set_section_size (obfd
, osections
[i
],
1296 size
+ (gap_stop
- gap_start
)))
1298 non_fatal (_("Can't fill gap after %s: %s"),
1299 bfd_get_section_name (obfd
, osections
[i
]),
1300 bfd_errmsg (bfd_get_error ()));
1304 gaps
[i
] = gap_stop
- gap_start
;
1305 if (max_gap
< gap_stop
- gap_start
)
1306 max_gap
= gap_stop
- gap_start
;
1316 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1317 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1318 if (lma
+ size
< pad_to
)
1320 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1323 non_fatal (_("Can't add padding to %s: %s"),
1324 bfd_get_section_name (obfd
, osections
[c
- 1]),
1325 bfd_errmsg (bfd_get_error ()));
1330 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1331 if (max_gap
< pad_to
- (lma
+ size
))
1332 max_gap
= pad_to
- (lma
+ size
);
1338 /* Symbol filtering must happen after the output sections
1339 have been created, but before their contents are set. */
1341 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1343 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1345 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1346 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1348 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1350 if (convert_debugging
)
1351 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1353 if (strip_symbols
== STRIP_DEBUG
1354 || strip_symbols
== STRIP_ALL
1355 || strip_symbols
== STRIP_UNNEEDED
1356 || strip_symbols
== STRIP_NONDEBUG
1357 || discard_locals
!= LOCALS_UNDEF
1358 || strip_specific_list
!= NULL
1359 || keep_specific_list
!= NULL
1360 || localize_specific_list
!= NULL
1361 || keepglobal_specific_list
!= NULL
1362 || weaken_specific_list
!= NULL
1363 || prefix_symbols_string
1366 || convert_debugging
1367 || change_leading_char
1368 || remove_leading_char
1369 || redefine_sym_list
1372 /* Mark symbols used in output relocations so that they
1373 are kept, even if they are local labels or static symbols.
1375 Note we iterate over the input sections examining their
1376 relocations since the relocations for the output sections
1377 haven't been set yet. mark_symbols_used_in_relocations will
1378 ignore input sections which have no corresponding output
1380 if (strip_symbols
!= STRIP_ALL
)
1381 bfd_map_over_sections (ibfd
,
1382 mark_symbols_used_in_relocations
,
1384 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1385 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1388 if (convert_debugging
&& dhandle
!= NULL
)
1390 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1397 bfd_set_symtab (obfd
, osympp
, symcount
);
1399 /* This has to happen after the symbol table has been set. */
1400 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
1402 if (add_sections
!= NULL
)
1404 struct section_add
*padd
;
1406 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1408 if (! bfd_set_section_contents (obfd
, padd
->section
,
1409 (PTR
) padd
->contents
,
1411 (bfd_size_type
) padd
->size
))
1412 RETURN_NONFATAL (bfd_get_filename (obfd
));
1416 if (gap_fill_set
|| pad_to_set
)
1421 /* Fill in the gaps. */
1424 buf
= (bfd_byte
*) xmalloc (max_gap
);
1425 memset (buf
, gap_fill
, (size_t) max_gap
);
1427 c
= bfd_count_sections (obfd
);
1428 for (i
= 0; i
< c
; i
++)
1436 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1447 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1449 RETURN_NONFATAL (bfd_get_filename (obfd
));
1458 /* Allow the BFD backend to copy any private data it understands
1459 from the input BFD to the output BFD. This is done last to
1460 permit the routine to look at the filtered symbol table, which is
1461 important for the ECOFF code at least. */
1462 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1463 && strip_symbols
== STRIP_NONDEBUG
)
1464 /* Do not copy the private data when creating an ELF format
1465 debug info file. We do not want the program headers. */
1467 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1469 non_fatal (_("%s: error copying private BFD data: %s"),
1470 bfd_get_filename (obfd
),
1471 bfd_errmsg (bfd_get_error ()));
1476 /* Switch to the alternate machine code. We have to do this at the
1477 very end, because we only initialize the header when we create
1478 the first section. */
1479 if (use_alt_mach_code
!= 0)
1481 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1482 non_fatal (_("unknown alternate machine code, ignored"));
1487 #if defined (_WIN32) && !defined (__CYGWIN32__)
1488 #define MKDIR(DIR, MODE) mkdir (DIR)
1490 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1493 /* Read each archive element in turn from IBFD, copy the
1494 contents to temp file, and keep the temp file handle. */
1497 copy_archive (ibfd
, obfd
, output_target
)
1500 const char *output_target
;
1504 struct name_list
*next
;
1508 bfd
**ptr
= &obfd
->archive_head
;
1510 char *dir
= make_tempname (bfd_get_filename (obfd
));
1512 /* Make a temp directory to hold the contents. */
1513 if (MKDIR (dir
, 0700) != 0)
1515 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1516 dir
, strerror (errno
));
1518 obfd
->has_armap
= ibfd
->has_armap
;
1522 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1524 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1525 RETURN_NONFATAL (bfd_get_filename (obfd
));
1527 while (!status
&& this_element
!= (bfd
*) NULL
)
1533 int stat_status
= 0;
1535 /* Create an output file for this member. */
1536 output_name
= concat (dir
, "/",
1537 bfd_get_filename (this_element
), (char *) 0);
1539 /* If the file already exists, make another temp dir. */
1540 if (stat (output_name
, &buf
) >= 0)
1542 output_name
= make_tempname (output_name
);
1543 if (MKDIR (output_name
, 0700) != 0)
1545 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1546 output_name
, strerror (errno
));
1548 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1549 l
->name
= output_name
;
1553 output_name
= concat (output_name
, "/",
1554 bfd_get_filename (this_element
), (char *) 0);
1557 output_bfd
= bfd_openw (output_name
, output_target
);
1560 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1562 if (stat_status
!= 0)
1563 non_fatal (_("internal stat error on %s"),
1564 bfd_get_filename (this_element
));
1567 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1568 l
->name
= output_name
;
1572 if (output_bfd
== (bfd
*) NULL
)
1573 RETURN_NONFATAL (output_name
);
1575 if (bfd_check_format (this_element
, bfd_object
))
1576 copy_object (this_element
, output_bfd
);
1578 if (!bfd_close (output_bfd
))
1580 bfd_nonfatal (bfd_get_filename (output_bfd
));
1581 /* Error in new object file. Don't change archive. */
1585 if (preserve_dates
&& stat_status
== 0)
1586 set_times (output_name
, &buf
);
1588 /* Open the newly output file and attach to our list. */
1589 output_bfd
= bfd_openr (output_name
, output_target
);
1591 l
->obfd
= output_bfd
;
1594 ptr
= &output_bfd
->next
;
1596 last_element
= this_element
;
1598 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1600 bfd_close (last_element
);
1602 *ptr
= (bfd
*) NULL
;
1604 if (!bfd_close (obfd
))
1605 RETURN_NONFATAL (bfd_get_filename (obfd
));
1607 if (!bfd_close (ibfd
))
1608 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1610 /* Delete all the files that we opened. */
1611 for (l
= list
; l
!= NULL
; l
= l
->next
)
1613 if (l
->obfd
== NULL
)
1617 bfd_close (l
->obfd
);
1624 /* The top-level control. */
1627 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1628 const char *input_filename
;
1629 const char *output_filename
;
1630 const char *input_target
;
1631 const char *output_target
;
1634 char **obj_matching
;
1635 char **core_matching
;
1637 /* To allow us to do "strip *" without dying on the first
1638 non-object file, failures are nonfatal. */
1639 ibfd
= bfd_openr (input_filename
, input_target
);
1641 RETURN_NONFATAL (input_filename
);
1643 if (bfd_check_format (ibfd
, bfd_archive
))
1647 /* bfd_get_target does not return the correct value until
1648 bfd_check_format succeeds. */
1649 if (output_target
== NULL
)
1650 output_target
= bfd_get_target (ibfd
);
1652 obfd
= bfd_openw (output_filename
, output_target
);
1654 RETURN_NONFATAL (output_filename
);
1656 copy_archive (ibfd
, obfd
, output_target
);
1658 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1662 /* bfd_get_target does not return the correct value until
1663 bfd_check_format succeeds. */
1664 if (output_target
== NULL
)
1665 output_target
= bfd_get_target (ibfd
);
1667 obfd
= bfd_openw (output_filename
, output_target
);
1669 RETURN_NONFATAL (output_filename
);
1671 copy_object (ibfd
, obfd
);
1673 if (!bfd_close (obfd
))
1674 RETURN_NONFATAL (output_filename
);
1676 if (!bfd_close (ibfd
))
1677 RETURN_NONFATAL (input_filename
);
1681 bfd_error_type obj_error
= bfd_get_error ();
1682 bfd_error_type core_error
;
1684 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1686 /* This probably can't happen.. */
1687 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1688 free (obj_matching
);
1692 core_error
= bfd_get_error ();
1693 /* Report the object error in preference to the core error. */
1694 if (obj_error
!= core_error
)
1695 bfd_set_error (obj_error
);
1697 bfd_nonfatal (input_filename
);
1699 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1701 list_matching_formats (obj_matching
);
1702 free (obj_matching
);
1704 if (core_error
== bfd_error_file_ambiguously_recognized
)
1706 list_matching_formats (core_matching
);
1707 free (core_matching
);
1714 /* Add a name to the section renaming list. */
1717 add_section_rename (old_name
, new_name
, flags
)
1718 const char * old_name
;
1719 const char * new_name
;
1722 section_rename
* rename
;
1724 /* Check for conflicts first. */
1725 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1726 if (strcmp (rename
->old_name
, old_name
) == 0)
1728 /* Silently ignore duplicate definitions. */
1729 if (strcmp (rename
->new_name
, new_name
) == 0
1730 && rename
->flags
== flags
)
1733 fatal (_("Multiple renames of section %s"), old_name
);
1736 rename
= (section_rename
*) xmalloc (sizeof (* rename
));
1738 rename
->old_name
= old_name
;
1739 rename
->new_name
= new_name
;
1740 rename
->flags
= flags
;
1741 rename
->next
= section_rename_list
;
1743 section_rename_list
= rename
;
1746 /* Check the section rename list for a new name of the input section
1747 ISECTION. Return the new name if one is found.
1748 Also set RETURNED_FLAGS to the flags to be used for this section. */
1751 find_section_rename (ibfd
, isection
, returned_flags
)
1752 bfd
* ibfd ATTRIBUTE_UNUSED
;
1754 flagword
* returned_flags
;
1756 const char * old_name
= bfd_section_name (ibfd
, isection
);
1757 section_rename
* rename
;
1759 /* Default to using the flags of the input section. */
1760 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1762 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1763 if (strcmp (rename
->old_name
, old_name
) == 0)
1765 if (rename
->flags
!= (flagword
) -1)
1766 * returned_flags
= rename
->flags
;
1768 return rename
->new_name
;
1774 /* Create a section in OBFD with the same
1775 name and attributes as ISECTION in IBFD. */
1778 setup_section (ibfd
, isection
, obfdarg
)
1783 bfd
*obfd
= (bfd
*) obfdarg
;
1784 struct section_list
*p
;
1792 char *prefix
= NULL
;
1794 if (is_strip_section (ibfd
, isection
))
1797 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1801 /* Get the, possibly new, name of the output section. */
1802 name
= find_section_rename (ibfd
, isection
, & flags
);
1804 /* Prefix sections. */
1805 if ((prefix_alloc_sections_string
) && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1806 prefix
= prefix_alloc_sections_string
;
1807 else if (prefix_sections_string
)
1808 prefix
= prefix_sections_string
;
1814 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1820 osection
= bfd_make_section_anyway (obfd
, name
);
1822 if (osection
== NULL
)
1828 size
= bfd_section_size (ibfd
, isection
);
1830 size
= (size
+ interleave
- 1) / interleave
;
1831 if (! bfd_set_section_size (obfd
, osection
, size
))
1837 vma
= bfd_section_vma (ibfd
, isection
);
1838 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1840 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1843 vma
+= change_section_address
;
1845 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1851 lma
= isection
->lma
;
1852 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1854 if (p
->change_lma
== CHANGE_MODIFY
)
1856 else if (p
->change_lma
== CHANGE_SET
)
1862 lma
+= change_section_address
;
1864 osection
->lma
= lma
;
1866 /* FIXME: This is probably not enough. If we change the LMA we
1867 may have to recompute the header for the file as well. */
1868 if (!bfd_set_section_alignment (obfd
,
1870 bfd_section_alignment (ibfd
, isection
)))
1872 err
= _("alignment");
1876 if (p
!= NULL
&& p
->set_flags
)
1877 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1878 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1884 /* Copy merge entity size. */
1885 osection
->entsize
= isection
->entsize
;
1887 /* This used to be mangle_section; we do here to avoid using
1888 bfd_get_section_by_name since some formats allow multiple
1889 sections with the same name. */
1890 isection
->output_section
= osection
;
1891 isection
->output_offset
= 0;
1893 /* Allow the BFD backend to copy any private data it understands
1894 from the input section to the output section. */
1895 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1896 && strip_symbols
== STRIP_NONDEBUG
)
1897 /* Do not copy the private data when creating an ELF format
1898 debug info file. We do not want the program headers. */
1900 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1902 err
= _("private data");
1906 /* All went well. */
1910 non_fatal (_("%s: section `%s': error in %s: %s"),
1911 bfd_get_filename (ibfd
),
1912 bfd_section_name (ibfd
, isection
),
1913 err
, bfd_errmsg (bfd_get_error ()));
1917 /* Copy the data of input section ISECTION of IBFD
1918 to an output section with the same name in OBFD.
1919 If stripping then don't copy any relocation info. */
1922 copy_section (ibfd
, isection
, obfdarg
)
1927 bfd
*obfd
= (bfd
*) obfdarg
;
1928 struct section_list
*p
;
1936 /* If we have already failed earlier on,
1937 do not keep on generating complaints now. */
1941 if (is_strip_section (ibfd
, isection
))
1944 flags
= bfd_get_section_flags (ibfd
, isection
);
1945 if ((flags
& SEC_GROUP
) != 0)
1948 osection
= isection
->output_section
;
1949 size
= bfd_get_section_size_before_reloc (isection
);
1951 if (size
== 0 || osection
== 0)
1954 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
1956 /* Core files do not need to be relocated. */
1957 if (bfd_get_format (obfd
) == bfd_core
)
1960 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1963 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1966 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1969 relpp
= (arelent
**) xmalloc (relsize
);
1970 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1972 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1974 if (strip_symbols
== STRIP_ALL
)
1976 /* Remove relocations which are not in
1977 keep_strip_specific_list. */
1978 arelent
**temp_relpp
;
1979 long temp_relcount
= 0;
1982 temp_relpp
= (arelent
**) xmalloc (relsize
);
1983 for (i
= 0; i
< relcount
; i
++)
1984 if (is_specified_symbol
1985 (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1986 keep_specific_list
))
1987 temp_relpp
[temp_relcount
++] = relpp
[i
];
1988 relcount
= temp_relcount
;
1993 bfd_set_reloc (obfd
, osection
,
1994 (relcount
== 0 ? (arelent
**) NULL
: relpp
), relcount
);
1997 isection
->_cooked_size
= isection
->_raw_size
;
1998 isection
->reloc_done
= TRUE
;
2000 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2001 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2003 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
2005 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
2007 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2010 filter_bytes (memhunk
, &size
);
2012 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
2014 RETURN_NONFATAL (bfd_get_filename (obfd
));
2018 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2020 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
2022 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2023 flag--they can just remove the section entirely and add it
2024 back again. However, we do permit them to turn on the
2025 SEC_HAS_CONTENTS flag, and take it to mean that the section
2026 contents should be zeroed out. */
2028 memset (memhunk
, 0, size
);
2029 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
2031 RETURN_NONFATAL (bfd_get_filename (obfd
));
2036 /* Get all the sections. This is used when --gap-fill or --pad-to is
2040 get_sections (obfd
, osection
, secppparg
)
2041 bfd
*obfd ATTRIBUTE_UNUSED
;
2045 asection
***secppp
= (asection
***) secppparg
;
2047 **secppp
= osection
;
2051 /* Sort sections by VMA. This is called via qsort, and is used when
2052 --gap-fill or --pad-to is used. We force non loadable or empty
2053 sections to the front, where they are easier to ignore. */
2056 compare_section_lma (arg1
, arg2
)
2060 const asection
**sec1
= (const asection
**) arg1
;
2061 const asection
**sec2
= (const asection
**) arg2
;
2062 flagword flags1
, flags2
;
2064 /* Sort non loadable sections to the front. */
2065 flags1
= (*sec1
)->flags
;
2066 flags2
= (*sec2
)->flags
;
2067 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2068 || (flags1
& SEC_LOAD
) == 0)
2070 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2071 && (flags2
& SEC_LOAD
) != 0)
2076 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2077 || (flags2
& SEC_LOAD
) == 0)
2081 /* Sort sections by LMA. */
2082 if ((*sec1
)->lma
> (*sec2
)->lma
)
2084 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2087 /* Sort sections with the same LMA by size. */
2088 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
2090 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
2096 /* Mark all the symbols which will be used in output relocations with
2097 the BSF_KEEP flag so that those symbols will not be stripped.
2099 Ignore relocations which will not appear in the output file. */
2102 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
2107 asymbol
**symbols
= (asymbol
**) symbolsarg
;
2112 /* Ignore an input section with no corresponding output section. */
2113 if (isection
->output_section
== NULL
)
2116 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2118 bfd_fatal (bfd_get_filename (ibfd
));
2123 relpp
= (arelent
**) xmalloc (relsize
);
2124 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2126 bfd_fatal (bfd_get_filename (ibfd
));
2128 /* Examine each symbol used in a relocation. If it's not one of the
2129 special bfd section symbols, then mark it with BSF_KEEP. */
2130 for (i
= 0; i
< relcount
; i
++)
2132 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2133 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2134 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2135 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2142 /* Write out debugging information. */
2145 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
2148 long *symcountp ATTRIBUTE_UNUSED
;
2149 asymbol
***symppp ATTRIBUTE_UNUSED
;
2151 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2152 return write_ieee_debugging_info (obfd
, dhandle
);
2154 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2155 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2157 bfd_byte
*syms
, *strings
;
2158 bfd_size_type symsize
, stringsize
;
2159 asection
*stabsec
, *stabstrsec
;
2161 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2166 stabsec
= bfd_make_section (obfd
, ".stab");
2167 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2169 || stabstrsec
== NULL
2170 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2171 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2172 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2173 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2174 || ! bfd_set_section_flags (obfd
, stabsec
,
2178 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2183 non_fatal (_("%s: can't create debugging section: %s"),
2184 bfd_get_filename (obfd
),
2185 bfd_errmsg (bfd_get_error ()));
2189 /* We can get away with setting the section contents now because
2190 the next thing the caller is going to do is copy over the
2191 real sections. We may someday have to split the contents
2192 setting out of this function. */
2193 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
2195 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
2196 (file_ptr
) 0, stringsize
))
2198 non_fatal (_("%s: can't set debugging section contents: %s"),
2199 bfd_get_filename (obfd
),
2200 bfd_errmsg (bfd_get_error ()));
2207 non_fatal (_("%s: don't know how to write debugging information for %s"),
2208 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2213 strip_main (argc
, argv
)
2217 char *input_target
= NULL
;
2218 char *output_target
= NULL
;
2219 bfd_boolean show_version
= FALSE
;
2220 bfd_boolean formats_info
= FALSE
;
2223 struct section_list
*p
;
2224 char *output_file
= NULL
;
2226 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVv",
2227 strip_options
, (int *) 0)) != EOF
)
2232 input_target
= optarg
;
2235 output_target
= optarg
;
2238 input_target
= output_target
= optarg
;
2241 p
= find_section_list (optarg
, TRUE
);
2243 sections_removed
= TRUE
;
2246 strip_symbols
= STRIP_ALL
;
2250 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2251 strip_symbols
= STRIP_DEBUG
;
2253 case OPTION_STRIP_UNNEEDED
:
2254 strip_symbols
= STRIP_UNNEEDED
;
2257 add_specific_symbol (optarg
, &keep_specific_list
);
2260 add_specific_symbol (optarg
, &strip_specific_list
);
2263 output_file
= optarg
;
2266 preserve_dates
= TRUE
;
2269 discard_locals
= LOCALS_ALL
;
2272 discard_locals
= LOCALS_START_L
;
2278 show_version
= TRUE
;
2280 case OPTION_FORMATS_INFO
:
2281 formats_info
= TRUE
;
2283 case OPTION_ONLY_KEEP_DEBUG
:
2284 strip_symbols
= STRIP_NONDEBUG
;
2287 /* We've been given a long option. */
2291 strip_usage (stdout
, 0);
2293 strip_usage (stderr
, 1);
2304 print_version ("strip");
2306 /* Default is to strip all symbols. */
2307 if (strip_symbols
== STRIP_UNDEF
2308 && discard_locals
== LOCALS_UNDEF
2309 && strip_specific_list
== NULL
)
2310 strip_symbols
= STRIP_ALL
;
2312 if (output_target
== (char *) NULL
)
2313 output_target
= input_target
;
2317 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2318 strip_usage (stderr
, 1);
2320 for (; i
< argc
; i
++)
2322 int hold_status
= status
;
2323 struct stat statbuf
;
2328 if (stat (argv
[i
], &statbuf
) < 0)
2330 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
2335 if (output_file
!= NULL
)
2336 tmpname
= output_file
;
2338 tmpname
= make_tempname (argv
[i
]);
2341 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2345 set_times (tmpname
, &statbuf
);
2346 if (output_file
== NULL
)
2347 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2348 status
= hold_status
;
2352 if (output_file
== NULL
)
2360 copy_main (argc
, argv
)
2364 char * binary_architecture
= NULL
;
2365 char *input_filename
= NULL
;
2366 char *output_filename
= NULL
;
2367 char *input_target
= NULL
;
2368 char *output_target
= NULL
;
2369 bfd_boolean show_version
= FALSE
;
2370 bfd_boolean change_warn
= TRUE
;
2371 bfd_boolean formats_info
= FALSE
;
2373 struct section_list
*p
;
2374 struct stat statbuf
;
2376 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2377 copy_options
, (int *) 0)) != EOF
)
2382 copy_byte
= atoi (optarg
);
2384 fatal (_("byte number must be non-negative"));
2388 binary_architecture
= optarg
;
2392 interleave
= atoi (optarg
);
2394 fatal (_("interleave must be positive"));
2398 case 's': /* "source" - 'I' is preferred */
2399 input_target
= optarg
;
2403 case 'd': /* "destination" - 'O' is preferred */
2404 output_target
= optarg
;
2408 input_target
= output_target
= optarg
;
2412 p
= find_section_list (optarg
, TRUE
);
2414 fatal (_("%s both copied and removed"), optarg
);
2416 sections_copied
= TRUE
;
2420 p
= find_section_list (optarg
, TRUE
);
2422 fatal (_("%s both copied and removed"), optarg
);
2424 sections_removed
= TRUE
;
2428 strip_symbols
= STRIP_ALL
;
2432 strip_symbols
= STRIP_DEBUG
;
2435 case OPTION_STRIP_UNNEEDED
:
2436 strip_symbols
= STRIP_UNNEEDED
;
2439 case OPTION_ONLY_KEEP_DEBUG
:
2440 strip_symbols
= STRIP_NONDEBUG
;
2443 case OPTION_ADD_GNU_DEBUGLINK
:
2444 gnu_debuglink_filename
= optarg
;
2448 add_specific_symbol (optarg
, &keep_specific_list
);
2452 add_specific_symbol (optarg
, &strip_specific_list
);
2456 add_specific_symbol (optarg
, &localize_specific_list
);
2460 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2464 add_specific_symbol (optarg
, &weaken_specific_list
);
2468 preserve_dates
= TRUE
;
2472 discard_locals
= LOCALS_ALL
;
2476 discard_locals
= LOCALS_START_L
;
2484 show_version
= TRUE
;
2487 case OPTION_FORMATS_INFO
:
2488 formats_info
= TRUE
;
2495 case OPTION_ADD_SECTION
:
2499 struct section_add
*pa
;
2504 s
= strchr (optarg
, '=');
2507 fatal (_("bad format for %s"), "--add-section");
2509 if (stat (s
+ 1, & st
) < 0)
2510 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
2512 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
2515 name
= (char *) xmalloc (len
+ 1);
2516 strncpy (name
, optarg
, len
);
2520 pa
->filename
= s
+ 1;
2522 pa
->size
= st
.st_size
;
2524 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
2525 f
= fopen (pa
->filename
, FOPEN_RB
);
2528 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
2530 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2532 fatal (_("%s: fread failed"), pa
->filename
);
2536 pa
->next
= add_sections
;
2541 case OPTION_CHANGE_START
:
2542 change_start
= parse_vma (optarg
, "--change-start");
2545 case OPTION_CHANGE_SECTION_ADDRESS
:
2546 case OPTION_CHANGE_SECTION_LMA
:
2547 case OPTION_CHANGE_SECTION_VMA
:
2552 char *option
= NULL
;
2554 enum change_action what
= CHANGE_IGNORE
;
2558 case OPTION_CHANGE_SECTION_ADDRESS
:
2559 option
= "--change-section-address";
2561 case OPTION_CHANGE_SECTION_LMA
:
2562 option
= "--change-section-lma";
2564 case OPTION_CHANGE_SECTION_VMA
:
2565 option
= "--change-section-vma";
2569 s
= strchr (optarg
, '=');
2572 s
= strchr (optarg
, '+');
2575 s
= strchr (optarg
, '-');
2577 fatal (_("bad format for %s"), option
);
2582 name
= (char *) xmalloc (len
+ 1);
2583 strncpy (name
, optarg
, len
);
2586 p
= find_section_list (name
, TRUE
);
2588 val
= parse_vma (s
+ 1, option
);
2592 case '=': what
= CHANGE_SET
; break;
2593 case '-': val
= - val
; /* Drop through. */
2594 case '+': what
= CHANGE_MODIFY
; break;
2599 case OPTION_CHANGE_SECTION_ADDRESS
:
2600 p
->change_vma
= what
;
2604 case OPTION_CHANGE_SECTION_LMA
:
2605 p
->change_lma
= what
;
2609 case OPTION_CHANGE_SECTION_VMA
:
2610 p
->change_vma
= what
;
2617 case OPTION_CHANGE_ADDRESSES
:
2618 change_section_address
= parse_vma (optarg
, "--change-addresses");
2619 change_start
= change_section_address
;
2622 case OPTION_CHANGE_WARNINGS
:
2626 case OPTION_CHANGE_LEADING_CHAR
:
2627 change_leading_char
= TRUE
;
2630 case OPTION_DEBUGGING
:
2631 convert_debugging
= TRUE
;
2634 case OPTION_GAP_FILL
:
2636 bfd_vma gap_fill_vma
;
2638 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2639 gap_fill
= (bfd_byte
) gap_fill_vma
;
2640 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2644 sprintf_vma (buff
, gap_fill_vma
);
2646 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2649 gap_fill_set
= TRUE
;
2653 case OPTION_NO_CHANGE_WARNINGS
:
2654 change_warn
= FALSE
;
2658 pad_to
= parse_vma (optarg
, "--pad-to");
2662 case OPTION_REMOVE_LEADING_CHAR
:
2663 remove_leading_char
= TRUE
;
2666 case OPTION_REDEFINE_SYM
:
2668 /* Push this redefinition onto redefine_symbol_list. */
2672 const char *nextarg
;
2673 char *source
, *target
;
2675 s
= strchr (optarg
, '=');
2677 fatal (_("bad format for %s"), "--redefine-sym");
2680 source
= (char *) xmalloc (len
+ 1);
2681 strncpy (source
, optarg
, len
);
2685 len
= strlen (nextarg
);
2686 target
= (char *) xmalloc (len
+ 1);
2687 strcpy (target
, nextarg
);
2689 redefine_list_append ("--redefine-sym", source
, target
);
2696 case OPTION_REDEFINE_SYMS
:
2697 add_redefine_syms_file (optarg
);
2700 case OPTION_SET_SECTION_FLAGS
:
2706 s
= strchr (optarg
, '=');
2708 fatal (_("bad format for %s"), "--set-section-flags");
2711 name
= (char *) xmalloc (len
+ 1);
2712 strncpy (name
, optarg
, len
);
2715 p
= find_section_list (name
, TRUE
);
2717 p
->set_flags
= TRUE
;
2718 p
->flags
= parse_flags (s
+ 1);
2722 case OPTION_RENAME_SECTION
:
2725 const char *eq
, *fl
;
2730 eq
= strchr (optarg
, '=');
2732 fatal (_("bad format for %s"), "--rename-section");
2736 fatal (_("bad format for %s"), "--rename-section");
2738 old_name
= (char *) xmalloc (len
+ 1);
2739 strncpy (old_name
, optarg
, len
);
2743 fl
= strchr (eq
, ',');
2746 flags
= parse_flags (fl
+ 1);
2756 fatal (_("bad format for %s"), "--rename-section");
2758 new_name
= (char *) xmalloc (len
+ 1);
2759 strncpy (new_name
, eq
, len
);
2762 add_section_rename (old_name
, new_name
, flags
);
2766 case OPTION_SET_START
:
2767 set_start
= parse_vma (optarg
, "--set-start");
2768 set_start_set
= TRUE
;
2771 case OPTION_SREC_LEN
:
2772 Chunk
= parse_vma (optarg
, "--srec-len");
2775 case OPTION_SREC_FORCES3
:
2779 case OPTION_STRIP_SYMBOLS
:
2780 add_specific_symbols (optarg
, &strip_specific_list
);
2783 case OPTION_KEEP_SYMBOLS
:
2784 add_specific_symbols (optarg
, &keep_specific_list
);
2787 case OPTION_LOCALIZE_SYMBOLS
:
2788 add_specific_symbols (optarg
, &localize_specific_list
);
2791 case OPTION_KEEPGLOBAL_SYMBOLS
:
2792 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2795 case OPTION_WEAKEN_SYMBOLS
:
2796 add_specific_symbols (optarg
, &weaken_specific_list
);
2799 case OPTION_ALT_MACH_CODE
:
2800 use_alt_mach_code
= atoi (optarg
);
2801 if (use_alt_mach_code
<= 0)
2802 fatal (_("alternate machine code index must be positive"));
2805 case OPTION_PREFIX_SYMBOLS
:
2806 prefix_symbols_string
= optarg
;
2809 case OPTION_PREFIX_SECTIONS
:
2810 prefix_sections_string
= optarg
;
2813 case OPTION_PREFIX_ALLOC_SECTIONS
:
2814 prefix_alloc_sections_string
= optarg
;
2818 /* We've been given a long option. */
2823 copy_usage (stdout
, 0);
2826 copy_usage (stderr
, 1);
2837 print_version ("objcopy");
2839 if (copy_byte
>= interleave
)
2840 fatal (_("byte number must be less than interleave"));
2842 if (optind
== argc
|| optind
+ 2 < argc
)
2843 copy_usage (stderr
, 1);
2845 input_filename
= argv
[optind
];
2846 if (optind
+ 1 < argc
)
2847 output_filename
= argv
[optind
+ 1];
2849 /* Default is to strip no symbols. */
2850 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2851 strip_symbols
= STRIP_NONE
;
2853 if (output_target
== (char *) NULL
)
2854 output_target
= input_target
;
2856 if (binary_architecture
!= (char *) NULL
)
2858 if (input_target
&& strcmp (input_target
, "binary") == 0)
2860 const bfd_arch_info_type
* temp_arch_info
;
2862 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2864 if (temp_arch_info
!= NULL
)
2865 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2867 fatal (_("architecture %s unknown"), binary_architecture
);
2871 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2872 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2877 if (stat (input_filename
, & statbuf
) < 0)
2878 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2880 /* If there is no destination file, or the source and destination files
2881 are the same, then create a temp and rename the result into the input. */
2882 if ((output_filename
== (char *) NULL
) ||
2883 (strcmp (input_filename
, output_filename
) == 0))
2885 char *tmpname
= make_tempname (input_filename
);
2887 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2891 set_times (tmpname
, &statbuf
);
2892 smart_rename (tmpname
, input_filename
, preserve_dates
);
2899 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2901 if (status
== 0 && preserve_dates
)
2902 set_times (output_filename
, &statbuf
);
2907 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2911 if (p
->change_vma
!= CHANGE_IGNORE
)
2915 sprintf_vma (buff
, p
->vma_val
);
2917 /* xgettext:c-format */
2918 non_fatal (_("%s %s%c0x%s never used"),
2919 "--change-section-vma",
2921 p
->change_vma
== CHANGE_SET
? '=' : '+',
2925 if (p
->change_lma
!= CHANGE_IGNORE
)
2929 sprintf_vma (buff
, p
->lma_val
);
2931 /* xgettext:c-format */
2932 non_fatal (_("%s %s%c0x%s never used"),
2933 "--change-section-lma",
2935 p
->change_lma
== CHANGE_SET
? '=' : '+',
2945 int main
PARAMS ((int, char **));
2952 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2953 setlocale (LC_MESSAGES
, "");
2955 #if defined (HAVE_SETLOCALE)
2956 setlocale (LC_CTYPE
, "");
2958 bindtextdomain (PACKAGE
, LOCALEDIR
);
2959 textdomain (PACKAGE
);
2961 program_name
= argv
[0];
2962 xmalloc_set_program_name (program_name
);
2964 START_PROGRESS (program_name
, 0);
2966 strip_symbols
= STRIP_UNDEF
;
2967 discard_locals
= LOCALS_UNDEF
;
2970 set_default_bfd_target ();
2974 int i
= strlen (program_name
);
2975 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2976 /* Drop the .exe suffix, if any. */
2977 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2980 program_name
[i
] = '\0';
2983 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2987 strip_main (argc
, argv
);
2989 copy_main (argc
, argv
);
2991 END_PROGRESS (program_name
);