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 *));
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));
114 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
116 static asymbol **isympp = NULL; /* Input symbols */
117 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
119 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
120 static int copy_byte = -1;
121 static int interleave = 4;
123 static bfd_boolean verbose; /* Print file and target names. */
124 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
125 static int status = 0; /* Exit status. */
130 STRIP_NONE, /* don't strip */
131 STRIP_DEBUG, /* strip all debugger symbols */
132 STRIP_UNNEEDED, /* strip unnecessary symbols */
133 STRIP_ALL /* strip all symbols */
136 /* Which symbols to remove. */
137 static enum strip_action strip_symbols;
142 LOCALS_START_L, /* discard locals starting with L */
143 LOCALS_ALL /* discard all locals */
146 /* Which local symbols to remove. Overrides STRIP_ALL. */
147 static enum locals_action discard_locals;
149 /* What kind of change to perform. */
157 /* Structure used to hold lists of sections and actions to take. */
160 struct section_list * next; /* Next section to change. */
161 const char * name; /* Section name. */
162 bfd_boolean used; /* Whether this entry was used. */
163 bfd_boolean remove; /* Whether to remove this section. */
164 bfd_boolean copy; /* Whether to copy this section. */
165 enum change_action change_vma;/* Whether to change or set VMA. */
166 bfd_vma vma_val; /* Amount to change by or set to. */
167 enum change_action change_lma;/* Whether to change or set LMA. */
168 bfd_vma lma_val; /* Amount to change by or set to. */
169 bfd_boolean set_flags; /* Whether to set the section flags. */
170 flagword flags; /* What to set the section flags to. */
173 static struct section_list *change_sections;
175 /* TRUE if some sections are to be removed. */
176 static bfd_boolean sections_removed;
178 /* TRUE if only some sections are to be copied. */
179 static bfd_boolean sections_copied;
181 /* Changes to the start address. */
182 static bfd_vma change_start = 0;
183 static bfd_boolean set_start_set = FALSE;
184 static bfd_vma set_start;
186 /* Changes to section addresses. */
187 static bfd_vma change_section_address = 0;
189 /* Filling gaps between sections. */
190 static bfd_boolean gap_fill_set = FALSE;
191 static bfd_byte gap_fill = 0;
193 /* Pad to a given address. */
194 static bfd_boolean pad_to_set = FALSE;
195 static bfd_vma pad_to;
197 /* Use alternate machine code? */
198 static int use_alt_mach_code = 0;
200 /* List of sections to add. */
203 /* Next section to add. */
204 struct section_add *next;
205 /* Name of section to add. */
207 /* Name of file holding section contents. */
208 const char *filename;
211 /* Contents of file. */
213 /* BFD section, after it has been added. */
217 /* List of sections to add to the output BFD. */
218 static struct section_add *add_sections;
220 /* Whether to convert debugging information. */
221 static bfd_boolean convert_debugging = FALSE;
223 /* Whether to change the leading character in symbol names. */
224 static bfd_boolean change_leading_char = FALSE;
226 /* Whether to remove the leading character from global symbol names. */
227 static bfd_boolean remove_leading_char = FALSE;
229 /* List of symbols to strip, keep, localize, keep-global, weaken,
231 static struct symlist *strip_specific_list = NULL;
232 static struct symlist *keep_specific_list = NULL;
233 static struct symlist *localize_specific_list = NULL;
234 static struct symlist *keepglobal_specific_list = NULL;
235 static struct symlist *weaken_specific_list = NULL;
236 static struct redefine_node *redefine_sym_list = NULL;
238 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
239 static bfd_boolean weaken = FALSE;
241 /* Prefix symbols/sections. */
242 static char *prefix_symbols_string = 0;
243 static char *prefix_sections_string = 0;
244 static char *prefix_alloc_sections_string = 0;
246 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
248 #define OPTION_ADD_SECTION 150
249 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
250 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
251 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
252 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
253 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
254 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
255 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
256 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
257 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
258 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
259 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
260 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
261 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
262 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
263 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
264 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
265 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
266 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYM + 1)
267 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
268 #define OPTION_STRIP_SYMBOLS (OPTION_SREC_FORCES3 + 1)
269 #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1)
270 #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
271 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
272 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
273 #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
274 #define OPTION_ALT_MACH_CODE (OPTION_RENAME_SECTION + 1)
275 #define OPTION_PREFIX_SYMBOLS (OPTION_ALT_MACH_CODE + 1)
276 #define OPTION_PREFIX_SECTIONS (OPTION_PREFIX_SYMBOLS + 1)
277 #define OPTION_PREFIX_ALLOC_SECTIONS (OPTION_PREFIX_SECTIONS + 1)
279 /* Options to handle if running as "strip". */
281 static struct option strip_options[] =
283 {"discard-all", no_argument, 0, 'x'},
284 {"discard-locals", no_argument, 0, 'X'},
285 {"format", required_argument, 0, 'F'}, /* Obsolete */
286 {"help", no_argument, 0, 'h'},
287 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
288 {"input-target", required_argument, 0, 'I'},
289 {"keep-symbol", required_argument, 0, 'K'},
290 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
291 {"output-target", required_argument, 0, 'O'},
292 {"output-file", required_argument, 0, 'o'},
293 {"preserve-dates", no_argument, 0, 'p'},
294 {"remove-section", required_argument, 0, 'R'},
295 {"strip-all", no_argument, 0, 's'},
296 {"strip-debug", no_argument, 0, 'S'},
297 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
298 {"strip-symbol", required_argument, 0, 'N'},
299 {"target", required_argument, 0, 'F'},
300 {"verbose", no_argument, 0, 'v'},
301 {"version", no_argument, 0, 'V'},
302 {0, no_argument, 0, 0}
305 /* Options to handle if running as "objcopy". */
307 static struct option copy_options[] =
309 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
310 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
311 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
312 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
313 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
314 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
315 {"binary-architecture", required_argument, 0, 'B'},
316 {"byte", required_argument, 0, 'b'},
317 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
318 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
319 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
320 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
321 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
322 {"change-start", required_argument, 0, OPTION_CHANGE_START},
323 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
324 {"debugging", no_argument, 0, OPTION_DEBUGGING},
325 {"discard-all", no_argument, 0, 'x'},
326 {"discard-locals", no_argument, 0, 'X'},
327 {"format", required_argument, 0, 'F'}, /* Obsolete */
328 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
329 {"help", no_argument, 0, 'h'},
330 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
331 {"input-target", required_argument, 0, 'I'},
332 {"interleave", required_argument, 0, 'i'},
333 {"keep-global-symbol", required_argument, 0, 'G'},
334 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
335 {"keep-symbol", required_argument, 0, 'K'},
336 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
337 {"localize-symbol", required_argument, 0, 'L'},
338 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
339 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
340 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
341 {"only-section", required_argument, 0, 'j'},
342 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
343 {"output-target", required_argument, 0, 'O'},
344 {"pad-to", required_argument, 0, OPTION_PAD_TO},
345 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
346 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
347 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
348 {"preserve-dates", no_argument, 0, 'p'},
349 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
350 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
351 {"remove-section", required_argument, 0, 'R'},
352 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
353 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
354 {"set-start", required_argument, 0, OPTION_SET_START},
355 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
356 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
357 {"strip-all", no_argument, 0, 'S'},
358 {"strip-debug", no_argument, 0, 'g'},
359 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
360 {"strip-symbol", required_argument, 0, 'N'},
361 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
362 {"target", required_argument, 0, 'F'},
363 {"verbose", no_argument, 0, 'v'},
364 {"version", no_argument, 0, 'V'},
365 {"weaken", no_argument, 0, OPTION_WEAKEN},
366 {"weaken-symbol", required_argument, 0, 'W'},
367 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
368 {0, no_argument, 0, 0}
372 extern char *program_name;
374 /* This flag distinguishes between strip and objcopy:
375 1 means this is 'strip'; 0 means this is 'objcopy'.
376 -1 means if we should use argv[0] to decide. */
379 /* The maximum length of an S record. This variable is declared in srec.c
380 and can be modified by the --srec-len parameter. */
381 extern unsigned int Chunk;
383 /* Restrict the generation of Srecords to type S3 only.
384 This variable is declare in bfd/srec.c and can be toggled
385 on by the --srec-forceS3 command line switch. */
386 extern bfd_boolean S3Forced;
388 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
389 extern enum bfd_architecture bfd_external_binary_architecture;
393 copy_usage (stream, exit_status)
397 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
398 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
399 fprintf (stream, _(" The options are:\n"));
400 fprintf (stream, _("\
401 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
402 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
403 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
404 -F --target <bfdname> Set both input and output format to <bfdname>\n\
405 --debugging Convert debugging information, if possible\n\
406 -p --preserve-dates Copy modified/access timestamps to the output\n\
407 -j --only-section <name> Only copy section <name> into the output\n\
408 -R --remove-section <name> Remove section <name> from the output\n\
409 -S --strip-all Remove all symbol and relocation information\n\
410 -g --strip-debug Remove all debugging symbols\n\
411 --strip-unneeded Remove all symbols not needed by relocations\n\
412 -N --strip-symbol <name> Do not copy symbol <name>\n\
413 -K --keep-symbol <name> Only copy symbol <name>\n\
414 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
415 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
416 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
417 --weaken Force all global symbols to be marked as weak\n\
418 -x --discard-all Remove all non-global symbols\n\
419 -X --discard-locals Remove any compiler-generated symbols\n\
420 -i --interleave <number> Only copy one out of every <number> bytes\n\
421 -b --byte <num> Select byte <num> in every interleaved block\n\
422 --gap-fill <val> Fill gaps between sections with <val>\n\
423 --pad-to <addr> Pad the last section up to address <addr>\n\
424 --set-start <addr> Set the start address to <addr>\n\
425 {--change-start|--adjust-start} <incr>\n\
426 Add <incr> to the start address\n\
427 {--change-addresses|--adjust-vma} <incr>\n\
428 Add <incr> to LMA, VMA and start addresses\n\
429 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
430 Change LMA and VMA of section <name> by <val>\n\
431 --change-section-lma <name>{=|+|-}<val>\n\
432 Change the LMA of section <name> by <val>\n\
433 --change-section-vma <name>{=|+|-}<val>\n\
434 Change the VMA of section <name> by <val>\n\
435 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
436 Warn if a named section does not exist\n\
437 --set-section-flags <name>=<flags>\n\
438 Set section <name>'s properties to <flags>\n\
439 --add-section <name>=<file> Add section <name> found in <file> to output\n\
440 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
441 --change-leading-char Force output format's leading character style\n\
442 --remove-leading-char Remove leading character from global symbols\n\
443 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
444 --srec-len <number> Restrict the length of generated Srecords\n\
445 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
446 --strip-symbols <file> -N for all symbols listed in <file>\n\
447 --keep-symbols <file> -K for all symbols listed in <file>\n\
448 --localize-symbols <file> -L for all symbols listed in <file>\n\
449 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
450 --weaken-symbols <file> -W for all symbols listed in <file>\n\
451 --alt-machine-code <index> Use alternate machine code for output\n\
452 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
453 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
454 --prefix-alloc-sections <prefix>\n\
455 Add <prefix> to start of every allocatable\n\
457 -v --verbose List all object files modified\n\
458 -V --version Display this program's version number\n\
459 -h --help Display this output\n\
461 list_supported_targets (program_name, stream);
462 if (exit_status == 0)
463 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
468 strip_usage (stream, exit_status)
472 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
473 fprintf (stream, _(" Removes symbols and sections from files\n"));
474 fprintf (stream, _(" The options are:\n"));
475 fprintf (stream, _("\
476 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
477 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
478 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
479 -p --preserve-dates Copy modified/access timestamps to the output\n\
480 -R --remove-section=<name> Remove section <name> from the output\n\
481 -s --strip-all Remove all symbol and relocation information\n\
482 -g -S -d --strip-debug Remove all debugging symbols\n\
483 --strip-unneeded Remove all symbols not needed by relocations\n\
484 -N --strip-symbol=<name> Do not copy symbol <name>\n\
485 -K --keep-symbol=<name> Only copy symbol <name>\n\
486 -x --discard-all Remove all non-global symbols\n\
487 -X --discard-locals Remove any compiler-generated symbols\n\
488 -v --verbose List all object files modified\n\
489 -V --version Display this program's version number\n\
490 -h --help Display this output\n\
491 -o <file> Place stripped output into <file>\n\
494 list_supported_targets (program_name, stream);
495 if (exit_status == 0)
496 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
500 /* Parse section flags into a flagword, with a fatal error if the
501 string can't be parsed. */
515 snext = strchr (s, ',');
525 #define PARSE_FLAG(fname,fval) \
526 else if (strncasecmp (fname, s, len) == 0) ret |= fval
527 PARSE_FLAG ("alloc", SEC_ALLOC);
528 PARSE_FLAG ("load", SEC_LOAD);
529 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
530 PARSE_FLAG ("readonly", SEC_READONLY);
531 PARSE_FLAG ("debug", SEC_DEBUGGING);
532 PARSE_FLAG ("code", SEC_CODE);
533 PARSE_FLAG ("data", SEC_DATA);
534 PARSE_FLAG ("rom", SEC_ROM);
535 PARSE_FLAG ("share", SEC_SHARED);
536 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
542 copy = xmalloc (len + 1);
543 strncpy (copy, s, len);
545 non_fatal (_("unrecognized section flag `%s'"), copy);
546 fatal (_("supported flags: %s"),
547 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
557 /* Find and optionally add an entry in the change_sections list. */
559 static struct section_list *
560 find_section_list (name, add)
564 register struct section_list *p;
566 for (p = change_sections; p != NULL; p = p->next)
567 if (strcmp (p->name, name) == 0)
573 p = (struct section_list *) xmalloc (sizeof (struct section_list));
578 p->change_vma = CHANGE_IGNORE;
579 p->change_lma = CHANGE_IGNORE;
582 p->set_flags = FALSE;
585 p->next = change_sections;
591 /* Add a symbol to strip_specific_list. */
594 add_specific_symbol (name, list)
596 struct symlist **list;
598 struct symlist *tmp_list;
600 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
601 tmp_list->name = name;
602 tmp_list->next = *list;
606 /* Add symbols listed in `filename' to strip_specific_list. */
608 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
609 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
612 add_specific_symbols (filename, list)
613 const char *filename;
614 struct symlist **list;
620 unsigned int line_count;
622 if (stat (filename, & st) < 0)
623 fatal (_("cannot stat: %s: %s"), filename, strerror (errno));
627 buffer = (char *) xmalloc (st.st_size + 2);
628 f = fopen (filename, FOPEN_RT);
630 fatal (_("cannot open: %s: %s"), filename, strerror (errno));
632 if (fread (buffer, 1, st.st_size, f) == 0 || ferror (f))
633 fatal (_("%s: fread failed"), filename);
636 buffer [st.st_size] = '\n';
637 buffer [st.st_size + 1] = '\0';
641 for (line = buffer; * line != '\0'; line ++)
646 int finished = FALSE;
648 for (eol = line;; eol ++)
654 /* Cope with \n\r. */
662 /* Cope with \r\n. */
673 /* Line comment, Terminate the line here, in case a
674 name is present and then allow the rest of the
675 loop to find the real end of the line. */
687 /* A name may now exist somewhere between 'line' and 'eol'.
688 Strip off leading whitespace and trailing whitespace,
689 then add it to the list. */
690 for (name = line; IS_WHITESPACE (* name); name ++)
692 for (name_end = name;
693 (! IS_WHITESPACE (* name_end))
694 && (! IS_LINE_TERMINATOR (* name_end));
698 if (! IS_LINE_TERMINATOR (* name_end))
702 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
705 if (! IS_LINE_TERMINATOR (* extra))
706 non_fatal (_("Ignoring rubbish found on line %d of %s"),
707 line_count, filename);
713 add_specific_symbol (name, list);
715 /* Advance line pointer to end of line. The 'eol ++' in the for
716 loop above will then advance us to the start of the next line. */
722 /* See whether a symbol should be stripped or kept based on
723 strip_specific_list and keep_symbols. */
726 is_specified_symbol (name, list)
728 struct symlist *list;
730 struct symlist *tmp_list;
732 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
733 if (strcmp (name, tmp_list->name) == 0)
739 /* See if a section is being removed. */
742 is_strip_section (abfd, sec)
743 bfd *abfd ATTRIBUTE_UNUSED;
746 struct section_list *p;
748 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
749 && (strip_symbols == STRIP_DEBUG
750 || strip_symbols == STRIP_UNNEEDED
751 || strip_symbols == STRIP_ALL
752 || discard_locals == LOCALS_ALL
753 || convert_debugging))
756 if (! sections_removed && ! sections_copied)
759 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
760 if (sections_removed && p != NULL && p->remove)
762 if (sections_copied && (p == NULL || ! p->copy))
767 /* Choose which symbol entries to copy; put the result in OSYMS.
768 We don't copy in place, because that confuses the relocs.
769 Return the number of symbols to print. */
772 filter_symbols (abfd, obfd, osyms, isyms, symcount)
775 asymbol **osyms, **isyms;
778 register asymbol **from = isyms, **to = osyms;
779 long src_count = 0, dst_count = 0;
780 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
783 for (; src_count < symcount; src_count++)
785 asymbol *sym = from[src_count];
786 flagword flags = sym->flags;
787 char *name = (char *) bfd_asymbol_name (sym);
789 bfd_boolean undefined;
790 bfd_boolean rem_leading_char;
791 bfd_boolean add_leading_char;
793 undefined = bfd_is_und_section (bfd_get_section (sym));
795 if (redefine_sym_list)
797 char *old_name, *new_name;
799 old_name = (char *) bfd_asymbol_name (sym);
800 new_name = (char *) lookup_sym_redefinition (old_name);
801 name = (char *) bfd_asymbol_name (sym) = new_name;
804 /* Check if we will remove the current leading character. */
806 (name[0] == bfd_get_symbol_leading_char (abfd))
807 && (change_leading_char
808 || (remove_leading_char
809 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
811 || bfd_is_com_section (bfd_get_section (sym)))));
813 /* Check if we will add a new leading character. */
816 && (bfd_get_symbol_leading_char (obfd) != '\0')
817 && (bfd_get_symbol_leading_char (abfd) == '\0'
818 || (name[0] == bfd_get_symbol_leading_char (abfd)));
820 /* Short circuit for change_leading_char if we can do it in-place. */
821 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
823 name[0] = bfd_get_symbol_leading_char (obfd);
824 bfd_asymbol_name (sym) = name;
825 rem_leading_char = FALSE;
826 add_leading_char = FALSE;
829 /* Remove leading char. */
830 if (rem_leading_char)
831 name = (char *) bfd_asymbol_name (sym) = name + 1;
833 /* Add new leading char and/or prefix. */
834 if (add_leading_char || prefix_symbols_string)
838 ptr = n = xmalloc (1 + strlen (prefix_symbols_string) + strlen (name) + 1);
839 if (add_leading_char)
840 *ptr++ = bfd_get_symbol_leading_char (obfd);
842 if (prefix_symbols_string)
844 strcpy (ptr, prefix_symbols_string);
845 ptr += strlen (prefix_symbols_string);
849 name = (char *) bfd_asymbol_name (sym) = n;
852 if (strip_symbols == STRIP_ALL)
854 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
855 || ((flags & BSF_SECTION_SYM) != 0
856 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
859 else if (relocatable /* Relocatable file. */
860 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
862 else if (bfd_decode_symclass (sym) == 'I')
863 /* Global symbols in $idata sections need to be retained
864 even if relocatable is FALSE. External users of the
865 library containing the $idata section may reference these
868 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
869 || (flags & BSF_WEAK) != 0
871 || bfd_is_com_section (bfd_get_section (sym)))
872 keep = strip_symbols != STRIP_UNNEEDED;
873 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
874 keep = (strip_symbols != STRIP_DEBUG
875 && strip_symbols != STRIP_UNNEEDED
876 && ! convert_debugging);
877 else if (bfd_get_section (sym)->comdat)
878 /* COMDAT sections store special information in local
879 symbols, so we cannot risk stripping any of them. */
881 else /* Local symbol. */
882 keep = (strip_symbols != STRIP_UNNEEDED
883 && (discard_locals != LOCALS_ALL
884 && (discard_locals != LOCALS_START_L
885 || ! bfd_is_local_label (abfd, sym))));
887 if (keep && is_specified_symbol (name, strip_specific_list))
889 if (!keep && is_specified_symbol (name, keep_specific_list))
891 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
894 if (keep && (flags & BSF_GLOBAL) != 0
895 && (weaken || is_specified_symbol (name, weaken_specific_list)))
897 sym->flags &=~ BSF_GLOBAL;
898 sym->flags |= BSF_WEAK;
900 if (keep && !undefined && (flags & (BSF_GLOBAL | BSF_WEAK))
901 && (is_specified_symbol (name, localize_specific_list)
902 || (keepglobal_specific_list != NULL
903 && ! is_specified_symbol (name, keepglobal_specific_list))))
905 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
906 sym->flags |= BSF_LOCAL;
910 to[dst_count++] = sym;
913 to[dst_count] = NULL;
918 /* Find the redefined name of symbol SOURCE. */
921 lookup_sym_redefinition (source)
924 struct redefine_node *list;
926 for (list = redefine_sym_list; list != NULL; list = list->next)
927 if (strcmp (source, list->source) == 0)
933 /* Add a node to a symbol redefine list. */
936 redefine_list_append (source, target)
940 struct redefine_node **p;
941 struct redefine_node *list;
942 struct redefine_node *new_node;
944 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
946 if (strcmp (source, list->source) == 0)
947 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
951 if (strcmp (target, list->target) == 0)
952 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
957 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
959 new_node->source = strdup (source);
960 new_node->target = strdup (target);
961 new_node->next = NULL;
966 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
970 filter_bytes (memhunk, size)
974 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
976 for (; from < end; from += interleave)
979 if (*size % interleave > (bfd_size_type) copy_byte)
980 *size = (*size / interleave) + 1;
985 /* Copy object file IBFD onto OBFD. */
988 copy_object (ibfd, obfd)
994 asection **osections = NULL;
995 bfd_size_type *gaps = NULL;
996 bfd_size_type max_gap = 0;
1000 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1001 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1002 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1004 fatal (_("Unable to change endianness of input file(s)"));
1008 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1009 RETURN_NONFATAL (bfd_get_filename (obfd));
1012 printf (_("copy from %s(%s) to %s(%s)\n"),
1013 bfd_get_filename (ibfd), bfd_get_target (ibfd),
1014 bfd_get_filename (obfd), bfd_get_target (obfd));
1019 start = bfd_get_start_address (ibfd);
1020 start += change_start;
1022 /* Neither the start address nor the flags
1023 need to be set for a core file. */
1024 if (bfd_get_format (obfd) != bfd_core)
1026 if (!bfd_set_start_address (obfd, start)
1027 || !bfd_set_file_flags (obfd,
1028 (bfd_get_file_flags (ibfd)
1029 & bfd_applicable_file_flags (obfd))))
1030 RETURN_NONFATAL (bfd_get_filename (ibfd));
1033 /* Copy architecture of input file to output file. */
1034 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1035 bfd_get_mach (ibfd))
1036 && (ibfd->target_defaulted
1037 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1038 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1039 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1040 bfd_get_mach (ibfd)));
1042 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1043 RETURN_NONFATAL (bfd_get_filename (ibfd));
1048 if (osympp != isympp)
1051 /* BFD mandates that all output sections be created and sizes set before
1052 any output is done. Thus, we traverse all sections multiple times. */
1053 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
1055 if (add_sections != NULL)
1057 struct section_add *padd;
1058 struct section_list *pset;
1060 for (padd = add_sections; padd != NULL; padd = padd->next)
1062 padd->section = bfd_make_section (obfd, padd->name);
1063 if (padd->section == NULL)
1065 non_fatal (_("can't create section `%s': %s"),
1066 padd->name, bfd_errmsg (bfd_get_error ()));
1074 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1075 RETURN_NONFATAL (bfd_get_filename (obfd));
1077 pset = find_section_list (padd->name, FALSE);
1081 if (pset != NULL && pset->set_flags)
1082 flags = pset->flags | SEC_HAS_CONTENTS;
1084 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1086 if (! bfd_set_section_flags (obfd, padd->section, flags))
1087 RETURN_NONFATAL (bfd_get_filename (obfd));
1091 if (pset->change_vma != CHANGE_IGNORE)
1092 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
1093 RETURN_NONFATAL (bfd_get_filename (obfd));
1095 if (pset->change_lma != CHANGE_IGNORE)
1097 padd->section->lma = pset->lma_val;
1099 if (! bfd_set_section_alignment
1100 (obfd, padd->section,
1101 bfd_section_alignment (obfd, padd->section)))
1102 RETURN_NONFATAL (bfd_get_filename (obfd));
1109 if (gap_fill_set || pad_to_set)
1114 /* We must fill in gaps between the sections and/or we must pad
1115 the last section to a specified address. We do this by
1116 grabbing a list of the sections, sorting them by VMA, and
1117 increasing the section sizes as required to fill the gaps.
1118 We write out the gap contents below. */
1120 c = bfd_count_sections (obfd);
1121 osections = (asection **) xmalloc (c * sizeof (asection *));
1123 bfd_map_over_sections (obfd, get_sections, (void *) &set);
1125 qsort (osections, c, sizeof (asection *), compare_section_lma);
1127 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
1128 memset (gaps, 0, c * sizeof (bfd_size_type));
1132 for (i = 0; i < c - 1; i++)
1136 bfd_vma gap_start, gap_stop;
1138 flags = bfd_get_section_flags (obfd, osections[i]);
1139 if ((flags & SEC_HAS_CONTENTS) == 0
1140 || (flags & SEC_LOAD) == 0)
1143 size = bfd_section_size (obfd, osections[i]);
1144 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1145 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1146 if (gap_start < gap_stop)
1148 if (! bfd_set_section_size (obfd, osections[i],
1149 size + (gap_stop - gap_start)))
1151 non_fatal (_("Can't fill gap after %s: %s"),
1152 bfd_get_section_name (obfd, osections[i]),
1153 bfd_errmsg (bfd_get_error ()));
1157 gaps[i] = gap_stop - gap_start;
1158 if (max_gap < gap_stop - gap_start)
1159 max_gap = gap_stop - gap_start;
1169 lma = bfd_section_lma (obfd, osections[c - 1]);
1170 size = bfd_section_size (obfd, osections[c - 1]);
1171 if (lma + size < pad_to)
1173 if (! bfd_set_section_size (obfd, osections[c - 1],
1176 non_fatal (_("Can't add padding to %s: %s"),
1177 bfd_get_section_name (obfd, osections[c - 1]),
1178 bfd_errmsg (bfd_get_error ()));
1183 gaps[c - 1] = pad_to - (lma + size);
1184 if (max_gap < pad_to - (lma + size))
1185 max_gap = pad_to - (lma + size);
1191 /* Symbol filtering must happen after the output sections
1192 have been created, but before their contents are set. */
1194 symsize = bfd_get_symtab_upper_bound (ibfd);
1196 RETURN_NONFATAL (bfd_get_filename (ibfd));
1198 osympp = isympp = (asymbol **) xmalloc (symsize);
1199 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1201 RETURN_NONFATAL (bfd_get_filename (ibfd));
1203 if (convert_debugging)
1204 dhandle = read_debugging_info (ibfd, isympp, symcount);
1206 if (strip_symbols == STRIP_DEBUG
1207 || strip_symbols == STRIP_ALL
1208 || strip_symbols == STRIP_UNNEEDED
1209 || discard_locals != LOCALS_UNDEF
1210 || strip_specific_list != NULL
1211 || keep_specific_list != NULL
1212 || localize_specific_list != NULL
1213 || keepglobal_specific_list != NULL
1214 || weaken_specific_list != NULL
1215 || prefix_symbols_string
1218 || convert_debugging
1219 || change_leading_char
1220 || remove_leading_char
1221 || redefine_sym_list
1224 /* Mark symbols used in output relocations so that they
1225 are kept, even if they are local labels or static symbols.
1227 Note we iterate over the input sections examining their
1228 relocations since the relocations for the output sections
1229 haven't been set yet. mark_symbols_used_in_relocations will
1230 ignore input sections which have no corresponding output
1232 if (strip_symbols != STRIP_ALL)
1233 bfd_map_over_sections (ibfd,
1234 mark_symbols_used_in_relocations,
1236 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1237 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1240 if (convert_debugging && dhandle != NULL)
1242 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1249 bfd_set_symtab (obfd, osympp, symcount);
1251 /* This has to happen after the symbol table has been set. */
1252 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
1254 if (add_sections != NULL)
1256 struct section_add *padd;
1258 for (padd = add_sections; padd != NULL; padd = padd->next)
1260 if (! bfd_set_section_contents (obfd, padd->section,
1261 (PTR) padd->contents,
1263 (bfd_size_type) padd->size))
1264 RETURN_NONFATAL (bfd_get_filename (obfd));
1268 if (gap_fill_set || pad_to_set)
1273 /* Fill in the gaps. */
1276 buf = (bfd_byte *) xmalloc (max_gap);
1277 memset (buf, gap_fill, (size_t) max_gap);
1279 c = bfd_count_sections (obfd);
1280 for (i = 0; i < c; i++)
1288 off = bfd_section_size (obfd, osections[i]) - left;
1299 if (! bfd_set_section_contents (obfd, osections[i], buf,
1301 RETURN_NONFATAL (bfd_get_filename (obfd));
1310 /* Allow the BFD backend to copy any private data it understands
1311 from the input BFD to the output BFD. This is done last to
1312 permit the routine to look at the filtered symbol table, which is
1313 important for the ECOFF code at least. */
1314 if (! bfd_copy_private_bfd_data (ibfd, obfd))
1316 non_fatal (_("%s: error copying private BFD data: %s"),
1317 bfd_get_filename (obfd),
1318 bfd_errmsg (bfd_get_error ()));
1323 /* Switch to the alternate machine code. We have to do this at the
1324 very end, because we only initialize the header when we create
1325 the first section. */
1326 if (use_alt_mach_code != 0)
1328 if (!bfd_alt_mach_code (obfd, use_alt_mach_code))
1329 non_fatal (_("unknown alternate machine code, ignored"));
1334 #if defined (_WIN32) && !defined (__CYGWIN32__)
1335 #define MKDIR(DIR, MODE) mkdir (DIR)
1337 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1340 /* Read each archive element in turn from IBFD, copy the
1341 contents to temp file, and keep the temp file handle. */
1344 copy_archive (ibfd, obfd, output_target)
1347 const char *output_target;
1351 struct name_list *next;
1355 bfd **ptr = &obfd->archive_head;
1357 char *dir = make_tempname (bfd_get_filename (obfd));
1359 /* Make a temp directory to hold the contents. */
1360 if (MKDIR (dir, 0700) != 0)
1362 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1363 dir, strerror (errno));
1365 obfd->has_armap = ibfd->has_armap;
1369 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1371 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1372 RETURN_NONFATAL (bfd_get_filename (obfd));
1374 while (!status && this_element != (bfd *) NULL)
1380 int stat_status = 0;
1382 /* Create an output file for this member. */
1383 output_name = concat (dir, "/",
1384 bfd_get_filename (this_element), (char *) 0);
1386 /* If the file already exists, make another temp dir. */
1387 if (stat (output_name, &buf) >= 0)
1389 output_name = make_tempname (output_name);
1390 if (MKDIR (output_name, 0700) != 0)
1392 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1393 output_name, strerror (errno));
1395 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1396 l->name = output_name;
1400 output_name = concat (output_name, "/",
1401 bfd_get_filename (this_element), (char *) 0);
1404 output_bfd = bfd_openw (output_name, output_target);
1407 stat_status = bfd_stat_arch_elt (this_element, &buf);
1409 if (stat_status != 0)
1410 non_fatal (_("internal stat error on %s"),
1411 bfd_get_filename (this_element));
1414 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1415 l->name = output_name;
1419 if (output_bfd == (bfd *) NULL)
1420 RETURN_NONFATAL (output_name);
1422 if (bfd_check_format (this_element, bfd_object))
1423 copy_object (this_element, output_bfd);
1425 if (!bfd_close (output_bfd))
1427 bfd_nonfatal (bfd_get_filename (output_bfd));
1428 /* Error in new object file. Don't change archive. */
1432 if (preserve_dates && stat_status == 0)
1433 set_times (output_name, &buf);
1435 /* Open the newly output file and attach to our list. */
1436 output_bfd = bfd_openr (output_name, output_target);
1438 l->obfd = output_bfd;
1441 ptr = &output_bfd->next;
1443 last_element = this_element;
1445 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1447 bfd_close (last_element);
1449 *ptr = (bfd *) NULL;
1451 if (!bfd_close (obfd))
1452 RETURN_NONFATAL (bfd_get_filename (obfd));
1454 if (!bfd_close (ibfd))
1455 RETURN_NONFATAL (bfd_get_filename (ibfd));
1457 /* Delete all the files that we opened. */
1458 for (l = list; l != NULL; l = l->next)
1460 if (l->obfd == NULL)
1464 bfd_close (l->obfd);
1471 /* The top-level control. */
1474 copy_file (input_filename, output_filename, input_target, output_target)
1475 const char *input_filename;
1476 const char *output_filename;
1477 const char *input_target;
1478 const char *output_target;
1481 char **obj_matching;
1482 char **core_matching;
1484 /* To allow us to do "strip *" without dying on the first
1485 non-object file, failures are nonfatal. */
1486 ibfd = bfd_openr (input_filename, input_target);
1488 RETURN_NONFATAL (input_filename);
1490 if (bfd_check_format (ibfd, bfd_archive))
1494 /* bfd_get_target does not return the correct value until
1495 bfd_check_format succeeds. */
1496 if (output_target == NULL)
1497 output_target = bfd_get_target (ibfd);
1499 obfd = bfd_openw (output_filename, output_target);
1501 RETURN_NONFATAL (output_filename);
1503 copy_archive (ibfd, obfd, output_target);
1505 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
1509 /* bfd_get_target does not return the correct value until
1510 bfd_check_format succeeds. */
1511 if (output_target == NULL)
1512 output_target = bfd_get_target (ibfd);
1514 obfd = bfd_openw (output_filename, output_target);
1516 RETURN_NONFATAL (output_filename);
1518 copy_object (ibfd, obfd);
1520 if (!bfd_close (obfd))
1521 RETURN_NONFATAL (output_filename);
1523 if (!bfd_close (ibfd))
1524 RETURN_NONFATAL (input_filename);
1528 bfd_error_type obj_error = bfd_get_error ();
1529 bfd_error_type core_error;
1531 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1533 /* This probably can't happen.. */
1534 if (obj_error == bfd_error_file_ambiguously_recognized)
1535 free (obj_matching);
1539 core_error = bfd_get_error ();
1540 /* Report the object error in preference to the core error. */
1541 if (obj_error != core_error)
1542 bfd_set_error (obj_error);
1544 bfd_nonfatal (input_filename);
1546 if (obj_error == bfd_error_file_ambiguously_recognized)
1548 list_matching_formats (obj_matching);
1549 free (obj_matching);
1551 if (core_error == bfd_error_file_ambiguously_recognized)
1553 list_matching_formats (core_matching);
1554 free (core_matching);
1561 /* Add a name to the section renaming list. */
1564 add_section_rename (old_name, new_name, flags)
1565 const char * old_name;
1566 const char * new_name;
1569 section_rename * rename;
1571 /* Check for conflicts first. */
1572 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1573 if (strcmp (rename->old_name, old_name) == 0)
1575 /* Silently ignore duplicate definitions. */
1576 if (strcmp (rename->new_name, new_name) == 0
1577 && rename->flags == flags)
1580 fatal (_("Multiple renames of section %s"), old_name);
1583 rename = (section_rename *) xmalloc (sizeof (* rename));
1585 rename->old_name = old_name;
1586 rename->new_name = new_name;
1587 rename->flags = flags;
1588 rename->next = section_rename_list;
1590 section_rename_list = rename;
1593 /* Check the section rename list for a new name of the input section
1594 ISECTION. Return the new name if one is found.
1595 Also set RETURNED_FLAGS to the flags to be used for this section. */
1598 find_section_rename (ibfd, isection, returned_flags)
1599 bfd * ibfd ATTRIBUTE_UNUSED;
1601 flagword * returned_flags;
1603 const char * old_name = bfd_section_name (ibfd, isection);
1604 section_rename * rename;
1606 /* Default to using the flags of the input section. */
1607 * returned_flags = bfd_get_section_flags (ibfd, isection);
1609 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1610 if (strcmp (rename->old_name, old_name) == 0)
1612 if (rename->flags != (flagword) -1)
1613 * returned_flags = rename->flags;
1615 return rename->new_name;
1621 /* Create a section in OBFD with the same
1622 name and attributes as ISECTION in IBFD. */
1625 setup_section (ibfd, isection, obfdarg)
1630 bfd *obfd = (bfd *) obfdarg;
1631 struct section_list *p;
1639 char *prefix = NULL;
1641 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1642 && (strip_symbols == STRIP_DEBUG
1643 || strip_symbols == STRIP_UNNEEDED
1644 || strip_symbols == STRIP_ALL
1645 || discard_locals == LOCALS_ALL
1646 || convert_debugging))
1649 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
1653 if (sections_removed && p != NULL && p->remove)
1655 if (sections_copied && (p == NULL || ! p->copy))
1658 /* Get the, possibly new, name of the output section. */
1659 name = find_section_rename (ibfd, isection, & flags);
1661 /* Prefix sections. */
1662 if ((prefix_alloc_sections_string) && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
1663 prefix = prefix_alloc_sections_string;
1664 else if (prefix_sections_string)
1665 prefix = prefix_sections_string;
1671 n = xmalloc (strlen (prefix) + strlen (name) + 1);
1677 osection = bfd_make_section_anyway (obfd, name);
1679 if (osection == NULL)
1685 size = bfd_section_size (ibfd, isection);
1687 size = (size + interleave - 1) / interleave;
1688 if (! bfd_set_section_size (obfd, osection, size))
1694 vma = bfd_section_vma (ibfd, isection);
1695 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1697 else if (p != NULL && p->change_vma == CHANGE_SET)
1700 vma += change_section_address;
1702 if (! bfd_set_section_vma (obfd, osection, vma))
1708 lma = isection->lma;
1709 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1711 if (p->change_lma == CHANGE_MODIFY)
1713 else if (p->change_lma == CHANGE_SET)
1719 lma += change_section_address;
1721 osection->lma = lma;
1723 /* FIXME: This is probably not enough. If we change the LMA we
1724 may have to recompute the header for the file as well. */
1725 if (!bfd_set_section_alignment (obfd,
1727 bfd_section_alignment (ibfd, isection)))
1729 err = _("alignment");
1733 if (p != NULL && p->set_flags)
1734 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
1735 if (!bfd_set_section_flags (obfd, osection, flags))
1741 /* Copy merge entity size. */
1742 osection->entsize = isection->entsize;
1744 /* This used to be mangle_section; we do here to avoid using
1745 bfd_get_section_by_name since some formats allow multiple
1746 sections with the same name. */
1747 isection->output_section = osection;
1748 isection->output_offset = 0;
1750 /* Allow the BFD backend to copy any private data it understands
1751 from the input section to the output section. */
1752 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1754 err = _("private data");
1758 /* All went well. */
1762 non_fatal (_("%s: section `%s': error in %s: %s"),
1763 bfd_get_filename (ibfd),
1764 bfd_section_name (ibfd, isection),
1765 err, bfd_errmsg (bfd_get_error ()));
1769 /* Copy the data of input section ISECTION of IBFD
1770 to an output section with the same name in OBFD.
1771 If stripping then don't copy any relocation info. */
1774 copy_section (ibfd, isection, obfdarg)
1779 bfd *obfd = (bfd *) obfdarg;
1780 struct section_list *p;
1788 /* If we have already failed earlier on,
1789 do not keep on generating complaints now. */
1793 flags = bfd_get_section_flags (ibfd, isection);
1794 if ((flags & SEC_DEBUGGING) != 0
1795 && (strip_symbols == STRIP_DEBUG
1796 || strip_symbols == STRIP_UNNEEDED
1797 || strip_symbols == STRIP_ALL
1798 || discard_locals == LOCALS_ALL
1799 || convert_debugging))
1802 if ((flags & SEC_GROUP) != 0)
1805 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
1807 if (sections_removed && p != NULL && p->remove)
1809 if (sections_copied && (p == NULL || ! p->copy))
1812 osection = isection->output_section;
1813 size = bfd_get_section_size_before_reloc (isection);
1815 if (size == 0 || osection == 0)
1818 /* Core files do not need to be relocated. */
1819 if (bfd_get_format (obfd) == bfd_core)
1822 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1825 RETURN_NONFATAL (bfd_get_filename (ibfd));
1828 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1831 relpp = (arelent **) xmalloc (relsize);
1832 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1834 RETURN_NONFATAL (bfd_get_filename (ibfd));
1836 if (strip_symbols == STRIP_ALL)
1838 /* Remove relocations which are not in
1839 keep_strip_specific_list. */
1840 arelent **temp_relpp;
1841 long temp_relcount = 0;
1844 temp_relpp = (arelent **) xmalloc (relsize);
1845 for (i = 0; i < relcount; i++)
1846 if (is_specified_symbol
1847 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1848 keep_specific_list))
1849 temp_relpp [temp_relcount++] = relpp [i];
1850 relcount = temp_relcount;
1855 bfd_set_reloc (obfd, osection,
1856 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1859 isection->_cooked_size = isection->_raw_size;
1860 isection->reloc_done = TRUE;
1862 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
1863 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
1865 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1867 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1869 RETURN_NONFATAL (bfd_get_filename (ibfd));
1872 filter_bytes (memhunk, &size);
1874 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1876 RETURN_NONFATAL (bfd_get_filename (obfd));
1880 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1882 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1884 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1885 flag--they can just remove the section entirely and add it
1886 back again. However, we do permit them to turn on the
1887 SEC_HAS_CONTENTS flag, and take it to mean that the section
1888 contents should be zeroed out. */
1890 memset (memhunk, 0, size);
1891 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1893 RETURN_NONFATAL (bfd_get_filename (obfd));
1898 /* Get all the sections. This is used when --gap-fill or --pad-to is
1902 get_sections (obfd, osection, secppparg)
1903 bfd *obfd ATTRIBUTE_UNUSED;
1907 asection ***secppp = (asection ***) secppparg;
1909 **secppp = osection;
1913 /* Sort sections by VMA. This is called via qsort, and is used when
1914 --gap-fill or --pad-to is used. We force non loadable or empty
1915 sections to the front, where they are easier to ignore. */
1918 compare_section_lma (arg1, arg2)
1922 const asection **sec1 = (const asection **) arg1;
1923 const asection **sec2 = (const asection **) arg2;
1924 flagword flags1, flags2;
1926 /* Sort non loadable sections to the front. */
1927 flags1 = (*sec1)->flags;
1928 flags2 = (*sec2)->flags;
1929 if ((flags1 & SEC_HAS_CONTENTS) == 0
1930 || (flags1 & SEC_LOAD) == 0)
1932 if ((flags2 & SEC_HAS_CONTENTS) != 0
1933 && (flags2 & SEC_LOAD) != 0)
1938 if ((flags2 & SEC_HAS_CONTENTS) == 0
1939 || (flags2 & SEC_LOAD) == 0)
1943 /* Sort sections by LMA. */
1944 if ((*sec1)->lma > (*sec2)->lma)
1946 else if ((*sec1)->lma < (*sec2)->lma)
1949 /* Sort sections with the same LMA by size. */
1950 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1952 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1958 /* Mark all the symbols which will be used in output relocations with
1959 the BSF_KEEP flag so that those symbols will not be stripped.
1961 Ignore relocations which will not appear in the output file. */
1964 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1969 asymbol **symbols = (asymbol **) symbolsarg;
1974 /* Ignore an input section with no corresponding output section. */
1975 if (isection->output_section == NULL)
1978 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1980 bfd_fatal (bfd_get_filename (ibfd));
1985 relpp = (arelent **) xmalloc (relsize);
1986 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1988 bfd_fatal (bfd_get_filename (ibfd));
1990 /* Examine each symbol used in a relocation. If it's not one of the
1991 special bfd section symbols, then mark it with BSF_KEEP. */
1992 for (i = 0; i < relcount; i++)
1994 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1995 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1996 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1997 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2004 /* Write out debugging information. */
2007 write_debugging_info (obfd, dhandle, symcountp, symppp)
2010 long *symcountp ATTRIBUTE_UNUSED;
2011 asymbol ***symppp ATTRIBUTE_UNUSED;
2013 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2014 return write_ieee_debugging_info (obfd, dhandle);
2016 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2017 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2019 bfd_byte *syms, *strings;
2020 bfd_size_type symsize, stringsize;
2021 asection *stabsec, *stabstrsec;
2023 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2028 stabsec = bfd_make_section (obfd, ".stab");
2029 stabstrsec = bfd_make_section (obfd, ".stabstr");
2031 || stabstrsec == NULL
2032 || ! bfd_set_section_size (obfd, stabsec, symsize)
2033 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2034 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2035 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
2036 || ! bfd_set_section_flags (obfd, stabsec,
2040 || ! bfd_set_section_flags (obfd, stabstrsec,
2045 non_fatal (_("%s: can't create debugging section: %s"),
2046 bfd_get_filename (obfd),
2047 bfd_errmsg (bfd_get_error ()));
2051 /* We can get away with setting the section contents now because
2052 the next thing the caller is going to do is copy over the
2053 real sections. We may someday have to split the contents
2054 setting out of this function. */
2055 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
2057 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
2058 (file_ptr) 0, stringsize))
2060 non_fatal (_("%s: can't set debugging section contents: %s"),
2061 bfd_get_filename (obfd),
2062 bfd_errmsg (bfd_get_error ()));
2069 non_fatal (_("%s: don't know how to write debugging information for %s"),
2070 bfd_get_filename (obfd), bfd_get_target (obfd));
2075 strip_main (argc, argv)
2079 char *input_target = NULL, *output_target = NULL;
2080 bfd_boolean show_version = FALSE;
2082 struct section_list *p;
2083 char *output_file = NULL;
2085 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVv",
2086 strip_options, (int *) 0)) != EOF)
2091 input_target = optarg;
2094 output_target = optarg;
2097 input_target = output_target = optarg;
2100 p = find_section_list (optarg, TRUE);
2102 sections_removed = TRUE;
2105 strip_symbols = STRIP_ALL;
2109 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2110 strip_symbols = STRIP_DEBUG;
2112 case OPTION_STRIP_UNNEEDED:
2113 strip_symbols = STRIP_UNNEEDED;
2116 add_specific_symbol (optarg, &keep_specific_list);
2119 add_specific_symbol (optarg, &strip_specific_list);
2122 output_file = optarg;
2125 preserve_dates = TRUE;
2128 discard_locals = LOCALS_ALL;
2131 discard_locals = LOCALS_START_L;
2137 show_version = TRUE;
2140 /* We've been given a long option. */
2144 strip_usage (stdout, 0);
2146 strip_usage (stderr, 1);
2151 print_version ("strip");
2153 /* Default is to strip all symbols. */
2154 if (strip_symbols == STRIP_UNDEF
2155 && discard_locals == LOCALS_UNDEF
2156 && strip_specific_list == NULL)
2157 strip_symbols = STRIP_ALL;
2159 if (output_target == (char *) NULL)
2160 output_target = input_target;
2164 || (output_file != NULL && (i + 1) < argc))
2165 strip_usage (stderr, 1);
2167 for (; i < argc; i++)
2169 int hold_status = status;
2170 struct stat statbuf;
2175 if (stat (argv[i], &statbuf) < 0)
2177 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
2182 if (output_file != NULL)
2183 tmpname = output_file;
2185 tmpname = make_tempname (argv[i]);
2188 copy_file (argv[i], tmpname, input_target, output_target);
2192 set_times (tmpname, &statbuf);
2193 if (output_file == NULL)
2194 smart_rename (tmpname, argv[i], preserve_dates);
2195 status = hold_status;
2199 if (output_file == NULL)
2207 copy_main (argc, argv)
2211 char * binary_architecture = NULL;
2212 char *input_filename = NULL, *output_filename = NULL;
2213 char *input_target = NULL, *output_target = NULL;
2214 bfd_boolean show_version = FALSE;
2215 bfd_boolean change_warn = TRUE;
2217 struct section_list *p;
2218 struct stat statbuf;
2220 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2221 copy_options, (int *) 0)) != EOF)
2226 copy_byte = atoi (optarg);
2228 fatal (_("byte number must be non-negative"));
2232 binary_architecture = optarg;
2236 interleave = atoi (optarg);
2238 fatal (_("interleave must be positive"));
2242 case 's': /* "source" - 'I' is preferred */
2243 input_target = optarg;
2247 case 'd': /* "destination" - 'O' is preferred */
2248 output_target = optarg;
2252 input_target = output_target = optarg;
2256 p = find_section_list (optarg, TRUE);
2258 fatal (_("%s both copied and removed"), optarg);
2260 sections_copied = TRUE;
2264 p = find_section_list (optarg, TRUE);
2266 fatal (_("%s both copied and removed"), optarg);
2268 sections_removed = TRUE;
2272 strip_symbols = STRIP_ALL;
2276 strip_symbols = STRIP_DEBUG;
2279 case OPTION_STRIP_UNNEEDED:
2280 strip_symbols = STRIP_UNNEEDED;
2284 add_specific_symbol (optarg, &keep_specific_list);
2288 add_specific_symbol (optarg, &strip_specific_list);
2292 add_specific_symbol (optarg, &localize_specific_list);
2296 add_specific_symbol (optarg, &keepglobal_specific_list);
2300 add_specific_symbol (optarg, &weaken_specific_list);
2304 preserve_dates = TRUE;
2308 discard_locals = LOCALS_ALL;
2312 discard_locals = LOCALS_START_L;
2320 show_version = TRUE;
2327 case OPTION_ADD_SECTION:
2331 struct section_add *pa;
2336 s = strchr (optarg, '=');
2339 fatal (_("bad format for %s"), "--add-section");
2341 if (stat (s + 1, & st) < 0)
2342 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
2344 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
2347 name = (char *) xmalloc (len + 1);
2348 strncpy (name, optarg, len);
2352 pa->filename = s + 1;
2354 pa->size = st.st_size;
2356 pa->contents = (bfd_byte *) xmalloc (pa->size);
2357 f = fopen (pa->filename, FOPEN_RB);
2360 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
2362 if (fread (pa->contents, 1, pa->size, f) == 0
2364 fatal (_("%s: fread failed"), pa->filename);
2368 pa->next = add_sections;
2373 case OPTION_CHANGE_START:
2374 change_start = parse_vma (optarg, "--change-start");
2377 case OPTION_CHANGE_SECTION_ADDRESS:
2378 case OPTION_CHANGE_SECTION_LMA:
2379 case OPTION_CHANGE_SECTION_VMA:
2384 char *option = NULL;
2386 enum change_action what = CHANGE_IGNORE;
2390 case OPTION_CHANGE_SECTION_ADDRESS:
2391 option = "--change-section-address";
2393 case OPTION_CHANGE_SECTION_LMA:
2394 option = "--change-section-lma";
2396 case OPTION_CHANGE_SECTION_VMA:
2397 option = "--change-section-vma";
2401 s = strchr (optarg, '=');
2404 s = strchr (optarg, '+');
2407 s = strchr (optarg, '-');
2409 fatal (_("bad format for %s"), option);
2414 name = (char *) xmalloc (len + 1);
2415 strncpy (name, optarg, len);
2418 p = find_section_list (name, TRUE);
2420 val = parse_vma (s + 1, option);
2424 case '=': what = CHANGE_SET; break;
2425 case '-': val = - val; /* Drop through. */
2426 case '+': what = CHANGE_MODIFY; break;
2431 case OPTION_CHANGE_SECTION_ADDRESS:
2432 p->change_vma = what;
2436 case OPTION_CHANGE_SECTION_LMA:
2437 p->change_lma = what;
2441 case OPTION_CHANGE_SECTION_VMA:
2442 p->change_vma = what;
2449 case OPTION_CHANGE_ADDRESSES:
2450 change_section_address = parse_vma (optarg, "--change-addresses");
2451 change_start = change_section_address;
2454 case OPTION_CHANGE_WARNINGS:
2458 case OPTION_CHANGE_LEADING_CHAR:
2459 change_leading_char = TRUE;
2462 case OPTION_DEBUGGING:
2463 convert_debugging = TRUE;
2466 case OPTION_GAP_FILL:
2468 bfd_vma gap_fill_vma;
2470 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2471 gap_fill = (bfd_byte) gap_fill_vma;
2472 if ((bfd_vma) gap_fill != gap_fill_vma)
2476 sprintf_vma (buff, gap_fill_vma);
2478 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2481 gap_fill_set = TRUE;
2485 case OPTION_NO_CHANGE_WARNINGS:
2486 change_warn = FALSE;
2490 pad_to = parse_vma (optarg, "--pad-to");
2494 case OPTION_REMOVE_LEADING_CHAR:
2495 remove_leading_char = TRUE;
2498 case OPTION_REDEFINE_SYM:
2500 /* Push this redefinition onto redefine_symbol_list. */
2504 const char *nextarg;
2505 char *source, *target;
2507 s = strchr (optarg, '=');
2509 fatal (_("bad format for %s"), "--redefine-sym");
2512 source = (char *) xmalloc (len + 1);
2513 strncpy (source, optarg, len);
2517 len = strlen (nextarg);
2518 target = (char *) xmalloc (len + 1);
2519 strcpy (target, nextarg);
2521 redefine_list_append (source, target);
2528 case OPTION_SET_SECTION_FLAGS:
2534 s = strchr (optarg, '=');
2536 fatal (_("bad format for %s"), "--set-section-flags");
2539 name = (char *) xmalloc (len + 1);
2540 strncpy (name, optarg, len);
2543 p = find_section_list (name, TRUE);
2545 p->set_flags = TRUE;
2546 p->flags = parse_flags (s + 1);
2550 case OPTION_RENAME_SECTION:
2553 const char *eq, *fl;
2558 eq = strchr (optarg, '=');
2560 fatal (_("bad format for %s"), "--rename-section");
2564 fatal (_("bad format for %s"), "--rename-section");
2566 old_name = (char *) xmalloc (len + 1);
2567 strncpy (old_name, optarg, len);
2571 fl = strchr (eq, ',');
2574 flags = parse_flags (fl + 1);
2584 fatal (_("bad format for %s"), "--rename-section");
2586 new_name = (char *) xmalloc (len + 1);
2587 strncpy (new_name, eq, len);
2590 add_section_rename (old_name, new_name, flags);
2594 case OPTION_SET_START:
2595 set_start = parse_vma (optarg, "--set-start");
2596 set_start_set = TRUE;
2599 case OPTION_SREC_LEN:
2600 Chunk = parse_vma (optarg, "--srec-len");
2603 case OPTION_SREC_FORCES3:
2607 case OPTION_STRIP_SYMBOLS:
2608 add_specific_symbols (optarg, &strip_specific_list);
2611 case OPTION_KEEP_SYMBOLS:
2612 add_specific_symbols (optarg, &keep_specific_list);
2615 case OPTION_LOCALIZE_SYMBOLS:
2616 add_specific_symbols (optarg, &localize_specific_list);
2619 case OPTION_KEEPGLOBAL_SYMBOLS:
2620 add_specific_symbols (optarg, &keepglobal_specific_list);
2623 case OPTION_WEAKEN_SYMBOLS:
2624 add_specific_symbols (optarg, &weaken_specific_list);
2627 case OPTION_ALT_MACH_CODE:
2628 use_alt_mach_code = atoi (optarg);
2629 if (use_alt_mach_code <= 0)
2630 fatal (_("alternate machine code index must be positive"));
2633 case OPTION_PREFIX_SYMBOLS:
2634 prefix_symbols_string = optarg;
2637 case OPTION_PREFIX_SECTIONS:
2638 prefix_sections_string = optarg;
2641 case OPTION_PREFIX_ALLOC_SECTIONS:
2642 prefix_alloc_sections_string = optarg;
2646 break; /* we've been given a long option */
2650 copy_usage (stdout, 0);
2653 copy_usage (stderr, 1);
2658 print_version ("objcopy");
2660 if (copy_byte >= interleave)
2661 fatal (_("byte number must be less than interleave"));
2663 if (optind == argc || optind + 2 < argc)
2664 copy_usage (stderr, 1);
2666 input_filename = argv[optind];
2667 if (optind + 1 < argc)
2668 output_filename = argv[optind + 1];
2670 /* Default is to strip no symbols. */
2671 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2672 strip_symbols = STRIP_NONE;
2674 if (output_target == (char *) NULL)
2675 output_target = input_target;
2677 if (binary_architecture != (char *) NULL)
2679 if (input_target && strcmp (input_target, "binary") == 0)
2681 const bfd_arch_info_type * temp_arch_info;
2683 temp_arch_info = bfd_scan_arch (binary_architecture);
2685 if (temp_arch_info != NULL)
2686 bfd_external_binary_architecture = temp_arch_info->arch;
2688 fatal (_("architecture %s unknown"), binary_architecture);
2692 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2693 non_fatal (_(" Argument %s ignored"), binary_architecture);
2698 if (stat (input_filename, & statbuf) < 0)
2699 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2701 /* If there is no destination file then create a temp and rename
2702 the result into the input. */
2704 if (output_filename == (char *) NULL)
2706 char *tmpname = make_tempname (input_filename);
2708 copy_file (input_filename, tmpname, input_target, output_target);
2712 set_times (tmpname, &statbuf);
2713 smart_rename (tmpname, input_filename, preserve_dates);
2720 copy_file (input_filename, output_filename, input_target, output_target);
2722 if (status == 0 && preserve_dates)
2723 set_times (output_filename, &statbuf);
2728 for (p = change_sections; p != NULL; p = p->next)
2732 if (p->change_vma != CHANGE_IGNORE)
2736 sprintf_vma (buff, p->vma_val);
2738 /* xgettext:c-format */
2739 non_fatal (_("%s %s%c0x%s never used"),
2740 "--change-section-vma",
2742 p->change_vma == CHANGE_SET ? '=' : '+',
2746 if (p->change_lma != CHANGE_IGNORE)
2750 sprintf_vma (buff, p->lma_val);
2752 /* xgettext:c-format */
2753 non_fatal (_("%s %s%c0x%s never used"),
2754 "--change-section-lma",
2756 p->change_lma == CHANGE_SET ? '=' : '+',
2766 int main PARAMS ((int, char **));
2773 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2774 setlocale (LC_MESSAGES, "");
2776 #if defined (HAVE_SETLOCALE)
2777 setlocale (LC_CTYPE, "");
2779 bindtextdomain (PACKAGE, LOCALEDIR);
2780 textdomain (PACKAGE);
2782 program_name = argv[0];
2783 xmalloc_set_program_name (program_name);
2785 START_PROGRESS (program_name, 0);
2787 strip_symbols = STRIP_UNDEF;
2788 discard_locals = LOCALS_UNDEF;
2791 set_default_bfd_target ();
2795 int i = strlen (program_name);
2796 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2797 /* Drop the .exe suffix, if any. */
2798 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
2801 program_name[i] = '\0';
2804 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
2808 strip_main (argc, argv);
2810 copy_main (argc, argv);
2812 END_PROGRESS (program_name);