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"
34 /* A list of symbols to explicitly strip out, or to keep. A linked
35 list is good enough for a small number from the command line, but
36 this will slow things down a lot if many symbols are being
45 /* A list to support redefine_sym. */
50 struct redefine_node *next;
53 typedef struct section_rename
55 const char * old_name;
56 const char * new_name;
58 struct section_rename * next;
62 /* List of sections to be renamed. */
63 static section_rename *section_rename_list;
65 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
67 static asymbol **isympp = NULL; /* Input symbols. */
68 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
70 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
71 static int copy_byte = -1;
72 static int interleave = 4;
74 static bfd_boolean verbose; /* Print file and target names. */
75 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
76 static int status = 0; /* Exit status. */
81 STRIP_NONE, /* Don't strip. */
82 STRIP_DEBUG, /* Strip all debugger symbols. */
83 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
84 STRIP_NONDEBUG, /* Strip everything but debug info. */
85 STRIP_ALL /* Strip all symbols. */
88 /* Which symbols to remove. */
89 static enum strip_action strip_symbols;
94 LOCALS_START_L, /* Discard locals starting with L. */
95 LOCALS_ALL /* Discard all locals. */
98 /* Which local symbols to remove. Overrides STRIP_ALL. */
99 static enum locals_action discard_locals;
101 /* What kind of change to perform. */
109 /* Structure used to hold lists of sections and actions to take. */
112 struct section_list * next; /* Next section to change. */
113 const char * name; /* Section name. */
114 bfd_boolean used; /* Whether this entry was used. */
115 bfd_boolean remove; /* Whether to remove this section. */
116 bfd_boolean copy; /* Whether to copy this section. */
117 enum change_action change_vma;/* Whether to change or set VMA. */
118 bfd_vma vma_val; /* Amount to change by or set to. */
119 enum change_action change_lma;/* Whether to change or set LMA. */
120 bfd_vma lma_val; /* Amount to change by or set to. */
121 bfd_boolean set_flags; /* Whether to set the section flags. */
122 flagword flags; /* What to set the section flags to. */
125 static struct section_list *change_sections;
127 /* TRUE if some sections are to be removed. */
128 static bfd_boolean sections_removed;
130 /* TRUE if only some sections are to be copied. */
131 static bfd_boolean sections_copied;
133 /* Changes to the start address. */
134 static bfd_vma change_start = 0;
135 static bfd_boolean set_start_set = FALSE;
136 static bfd_vma set_start;
138 /* Changes to section addresses. */
139 static bfd_vma change_section_address = 0;
141 /* Filling gaps between sections. */
142 static bfd_boolean gap_fill_set = FALSE;
143 static bfd_byte gap_fill = 0;
145 /* Pad to a given address. */
146 static bfd_boolean pad_to_set = FALSE;
147 static bfd_vma pad_to;
149 /* Use alternate machine code? */
150 static int use_alt_mach_code = 0;
152 /* Output BFD flags user wants to set or clear */
153 static flagword bfd_flags_to_set;
154 static flagword bfd_flags_to_clear;
156 /* List of sections to add. */
159 /* Next section to add. */
160 struct section_add *next;
161 /* Name of section to add. */
163 /* Name of file holding section contents. */
164 const char *filename;
167 /* Contents of file. */
169 /* BFD section, after it has been added. */
173 /* List of sections to add to the output BFD. */
174 static struct section_add *add_sections;
176 /* If non-NULL the argument to --add-gnu-debuglink.
177 This should be the filename to store in the .gnu_debuglink section. */
178 static const char * gnu_debuglink_filename = NULL;
180 /* Whether to convert debugging information. */
181 static bfd_boolean convert_debugging = FALSE;
183 /* Whether to change the leading character in symbol names. */
184 static bfd_boolean change_leading_char = FALSE;
186 /* Whether to remove the leading character from global symbol names. */
187 static bfd_boolean remove_leading_char = FALSE;
189 /* Whether to permit wildcard in symbol comparison. */
190 static bfd_boolean wildcard = FALSE;
192 /* List of symbols to strip, keep, localize, keep-global, weaken,
194 static struct symlist *strip_specific_list = NULL;
195 static struct symlist *keep_specific_list = NULL;
196 static struct symlist *localize_specific_list = NULL;
197 static struct symlist *keepglobal_specific_list = NULL;
198 static struct symlist *weaken_specific_list = NULL;
199 static struct redefine_node *redefine_sym_list = NULL;
201 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
202 static bfd_boolean weaken = FALSE;
204 /* Prefix symbols/sections. */
205 static char *prefix_symbols_string = 0;
206 static char *prefix_sections_string = 0;
207 static char *prefix_alloc_sections_string = 0;
209 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
210 enum command_line_switch
212 OPTION_ADD_SECTION=150,
213 OPTION_CHANGE_ADDRESSES,
214 OPTION_CHANGE_LEADING_CHAR,
216 OPTION_CHANGE_SECTION_ADDRESS,
217 OPTION_CHANGE_SECTION_LMA,
218 OPTION_CHANGE_SECTION_VMA,
219 OPTION_CHANGE_WARNINGS,
222 OPTION_NO_CHANGE_WARNINGS,
224 OPTION_REMOVE_LEADING_CHAR,
225 OPTION_SET_SECTION_FLAGS,
227 OPTION_STRIP_UNNEEDED,
230 OPTION_REDEFINE_SYMS,
233 OPTION_STRIP_SYMBOLS,
235 OPTION_LOCALIZE_SYMBOLS,
236 OPTION_KEEPGLOBAL_SYMBOLS,
237 OPTION_WEAKEN_SYMBOLS,
238 OPTION_RENAME_SECTION,
239 OPTION_ALT_MACH_CODE,
240 OPTION_PREFIX_SYMBOLS,
241 OPTION_PREFIX_SECTIONS,
242 OPTION_PREFIX_ALLOC_SECTIONS,
244 OPTION_ADD_GNU_DEBUGLINK,
245 OPTION_ONLY_KEEP_DEBUG,
246 OPTION_READONLY_TEXT,
247 OPTION_WRITABLE_TEXT,
252 /* Options to handle if running as "strip". */
254 static struct option strip_options[] =
256 {"discard-all", no_argument, 0, 'x'},
257 {"discard-locals", no_argument, 0, 'X'},
258 {"format", required_argument, 0, 'F'}, /* Obsolete */
259 {"help", no_argument, 0, 'h'},
260 {"info", no_argument, 0, OPTION_FORMATS_INFO},
261 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
262 {"input-target", required_argument, 0, 'I'},
263 {"keep-symbol", required_argument, 0, 'K'},
264 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
265 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
266 {"output-target", required_argument, 0, 'O'},
267 {"output-file", required_argument, 0, 'o'},
268 {"preserve-dates", no_argument, 0, 'p'},
269 {"remove-section", required_argument, 0, 'R'},
270 {"strip-all", no_argument, 0, 's'},
271 {"strip-debug", no_argument, 0, 'S'},
272 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
273 {"strip-symbol", required_argument, 0, 'N'},
274 {"target", required_argument, 0, 'F'},
275 {"verbose", no_argument, 0, 'v'},
276 {"version", no_argument, 0, 'V'},
277 {"wildcard", no_argument, 0, 'w'},
278 {0, no_argument, 0, 0}
281 /* Options to handle if running as "objcopy". */
283 static struct option copy_options[] =
285 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
286 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
287 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
288 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
289 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
290 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
291 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
292 {"binary-architecture", required_argument, 0, 'B'},
293 {"byte", required_argument, 0, 'b'},
294 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
295 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
296 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
297 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
298 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
299 {"change-start", required_argument, 0, OPTION_CHANGE_START},
300 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
301 {"debugging", no_argument, 0, OPTION_DEBUGGING},
302 {"discard-all", no_argument, 0, 'x'},
303 {"discard-locals", no_argument, 0, 'X'},
304 {"format", required_argument, 0, 'F'}, /* Obsolete */
305 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
306 {"help", no_argument, 0, 'h'},
307 {"impure", no_argument, 0, OPTION_IMPURE},
308 {"info", no_argument, 0, OPTION_FORMATS_INFO},
309 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
310 {"input-target", required_argument, 0, 'I'},
311 {"interleave", required_argument, 0, 'i'},
312 {"keep-global-symbol", required_argument, 0, 'G'},
313 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
314 {"keep-symbol", required_argument, 0, 'K'},
315 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
316 {"localize-symbol", required_argument, 0, 'L'},
317 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
318 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
319 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
320 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
321 {"only-section", required_argument, 0, 'j'},
322 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
323 {"output-target", required_argument, 0, 'O'},
324 {"pad-to", required_argument, 0, OPTION_PAD_TO},
325 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
326 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
327 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
328 {"preserve-dates", no_argument, 0, 'p'},
329 {"pure", no_argument, 0, OPTION_PURE},
330 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
331 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
332 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
333 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
334 {"remove-section", required_argument, 0, 'R'},
335 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
336 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
337 {"set-start", required_argument, 0, OPTION_SET_START},
338 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
339 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
340 {"strip-all", no_argument, 0, 'S'},
341 {"strip-debug", no_argument, 0, 'g'},
342 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
343 {"strip-symbol", required_argument, 0, 'N'},
344 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
345 {"target", required_argument, 0, 'F'},
346 {"verbose", no_argument, 0, 'v'},
347 {"version", no_argument, 0, 'V'},
348 {"weaken", no_argument, 0, OPTION_WEAKEN},
349 {"weaken-symbol", required_argument, 0, 'W'},
350 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
351 {"wildcard", no_argument, 0, 'w'},
352 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
353 {0, no_argument, 0, 0}
357 extern char *program_name;
359 /* This flag distinguishes between strip and objcopy:
360 1 means this is 'strip'; 0 means this is 'objcopy'.
361 -1 means if we should use argv[0] to decide. */
364 /* The maximum length of an S record. This variable is declared in srec.c
365 and can be modified by the --srec-len parameter. */
366 extern unsigned int Chunk;
368 /* Restrict the generation of Srecords to type S3 only.
369 This variable is declare in bfd/srec.c and can be toggled
370 on by the --srec-forceS3 command line switch. */
371 extern bfd_boolean S3Forced;
373 /* Defined in bfd/binary.c. Used to set architecture and machine of input
375 extern enum bfd_architecture bfd_external_binary_architecture;
376 extern unsigned long bfd_external_machine;
378 /* Forward declarations. */
379 static void setup_section (bfd *, asection *, void *);
380 static void copy_section (bfd *, asection *, void *);
381 static void get_sections (bfd *, asection *, void *);
382 static int compare_section_lma (const void *, const void *);
383 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
384 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
385 static const char *lookup_sym_redefinition (const char *);
388 copy_usage (FILE *stream, int exit_status)
390 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
391 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
392 fprintf (stream, _(" The options are:\n"));
393 fprintf (stream, _("\
394 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
395 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
396 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
397 -F --target <bfdname> Set both input and output format to <bfdname>\n\
398 --debugging Convert debugging information, if possible\n\
399 -p --preserve-dates Copy modified/access timestamps to the output\n\
400 -j --only-section <name> Only copy section <name> into the output\n\
401 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
402 -R --remove-section <name> Remove section <name> from the output\n\
403 -S --strip-all Remove all symbol and relocation information\n\
404 -g --strip-debug Remove all debugging symbols & sections\n\
405 --strip-unneeded Remove all symbols not needed by relocations\n\
406 -N --strip-symbol <name> Do not copy symbol <name>\n\
407 --only-keep-debug Strip everything but the debug information\n\
408 -K --keep-symbol <name> Only copy symbol <name>\n\
409 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
410 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
411 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
412 --weaken Force all global symbols to be marked as weak\n\
413 -w --wildcard Permit wildcard in symbol comparasion\n\
414 -x --discard-all Remove all non-global symbols\n\
415 -X --discard-locals Remove any compiler-generated symbols\n\
416 -i --interleave <number> Only copy one out of every <number> bytes\n\
417 -b --byte <num> Select byte <num> in every interleaved block\n\
418 --gap-fill <val> Fill gaps between sections with <val>\n\
419 --pad-to <addr> Pad the last section up to address <addr>\n\
420 --set-start <addr> Set the start address to <addr>\n\
421 {--change-start|--adjust-start} <incr>\n\
422 Add <incr> to the start address\n\
423 {--change-addresses|--adjust-vma} <incr>\n\
424 Add <incr> to LMA, VMA and start addresses\n\
425 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
426 Change LMA and VMA of section <name> by <val>\n\
427 --change-section-lma <name>{=|+|-}<val>\n\
428 Change the LMA of section <name> by <val>\n\
429 --change-section-vma <name>{=|+|-}<val>\n\
430 Change the VMA of section <name> by <val>\n\
431 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
432 Warn if a named section does not exist\n\
433 --set-section-flags <name>=<flags>\n\
434 Set section <name>'s properties to <flags>\n\
435 --add-section <name>=<file> Add section <name> found in <file> to output\n\
436 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
437 --change-leading-char Force output format's leading character style\n\
438 --remove-leading-char Remove leading character from global symbols\n\
439 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
440 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
442 --srec-len <number> Restrict the length of generated Srecords\n\
443 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
444 --strip-symbols <file> -N for all symbols listed in <file>\n\
445 --keep-symbols <file> -K for all symbols listed in <file>\n\
446 --localize-symbols <file> -L for all symbols listed in <file>\n\
447 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
448 --weaken-symbols <file> -W for all symbols listed in <file>\n\
449 --alt-machine-code <index> Use alternate machine code for output\n\
450 --writable-text Mark the output text as writable\n\
451 --readonly-text Make the output text write protected\n\
452 --pure Mark the output file as demand paged\n\
453 --impure Mark the output file as impure\n\
454 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
455 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
456 --prefix-alloc-sections <prefix>\n\
457 Add <prefix> to start of every allocatable\n\
459 -v --verbose List all object files modified\n\
460 -V --version Display this program's version number\n\
461 -h --help Display this output\n\
462 --info List object formats & architectures supported\n\
464 list_supported_targets (program_name, stream);
465 if (exit_status == 0)
466 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
471 strip_usage (FILE *stream, int exit_status)
473 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
474 fprintf (stream, _(" Removes symbols and sections from files\n"));
475 fprintf (stream, _(" The options are:\n"));
476 fprintf (stream, _("\
477 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
478 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
479 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
480 -p --preserve-dates Copy modified/access timestamps to the output\n\
481 -R --remove-section=<name> Remove section <name> from the output\n\
482 -s --strip-all Remove all symbol and relocation information\n\
483 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
484 --strip-unneeded Remove all symbols not needed by relocations\n\
485 --only-keep-debug Strip everything but the debug information\n\
486 -N --strip-symbol=<name> Do not copy symbol <name>\n\
487 -K --keep-symbol=<name> Only copy symbol <name>\n\
488 -w --wildcard Permit wildcard in symbol comparasion\n\
489 -x --discard-all Remove all non-global symbols\n\
490 -X --discard-locals Remove any compiler-generated symbols\n\
491 -v --verbose List all object files modified\n\
492 -V --version Display this program's version number\n\
493 -h --help Display this output\n\
494 --info List object formats & architectures supported\n\
495 -o <file> Place stripped output into <file>\n\
498 list_supported_targets (program_name, stream);
499 if (exit_status == 0)
500 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
504 /* Parse section flags into a flagword, with a fatal error if the
505 string can't be parsed. */
508 parse_flags (const char *s)
518 snext = strchr (s, ',');
528 #define PARSE_FLAG(fname,fval) \
529 else if (strncasecmp (fname, s, len) == 0) ret |= fval
530 PARSE_FLAG ("alloc", SEC_ALLOC);
531 PARSE_FLAG ("load", SEC_LOAD);
532 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
533 PARSE_FLAG ("readonly", SEC_READONLY);
534 PARSE_FLAG ("debug", SEC_DEBUGGING);
535 PARSE_FLAG ("code", SEC_CODE);
536 PARSE_FLAG ("data", SEC_DATA);
537 PARSE_FLAG ("rom", SEC_ROM);
538 PARSE_FLAG ("share", SEC_SHARED);
539 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
545 copy = xmalloc (len + 1);
546 strncpy (copy, s, len);
548 non_fatal (_("unrecognized section flag `%s'"), copy);
549 fatal (_("supported flags: %s"),
550 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
560 /* Find and optionally add an entry in the change_sections list. */
562 static struct section_list *
563 find_section_list (const char *name, bfd_boolean add)
565 struct section_list *p;
567 for (p = change_sections; p != NULL; p = p->next)
568 if (strcmp (p->name, name) == 0)
574 p = xmalloc (sizeof (struct section_list));
579 p->change_vma = CHANGE_IGNORE;
580 p->change_lma = CHANGE_IGNORE;
583 p->set_flags = FALSE;
586 p->next = change_sections;
592 /* Add a symbol to strip_specific_list. */
595 add_specific_symbol (const char *name, struct symlist **list)
597 struct symlist *tmp_list;
599 tmp_list = xmalloc (sizeof (struct symlist));
600 tmp_list->name = name;
601 tmp_list->next = *list;
605 /* Add symbols listed in `filename' to strip_specific_list. */
607 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
608 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
611 add_specific_symbols (const char *filename, struct symlist **list)
617 unsigned int line_count;
619 size = get_file_size (filename);
623 buffer = xmalloc (size + 2);
624 f = fopen (filename, FOPEN_RT);
626 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
628 if (fread (buffer, 1, size, f) == 0 || ferror (f))
629 fatal (_("%s: fread failed"), filename);
632 buffer [size] = '\n';
633 buffer [size + 1] = '\0';
637 for (line = buffer; * line != '\0'; line ++)
642 int finished = FALSE;
644 for (eol = line;; eol ++)
650 /* Cope with \n\r. */
658 /* Cope with \r\n. */
669 /* Line comment, Terminate the line here, in case a
670 name is present and then allow the rest of the
671 loop to find the real end of the line. */
683 /* A name may now exist somewhere between 'line' and 'eol'.
684 Strip off leading whitespace and trailing whitespace,
685 then add it to the list. */
686 for (name = line; IS_WHITESPACE (* name); name ++)
688 for (name_end = name;
689 (! IS_WHITESPACE (* name_end))
690 && (! IS_LINE_TERMINATOR (* name_end));
694 if (! IS_LINE_TERMINATOR (* name_end))
698 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
701 if (! IS_LINE_TERMINATOR (* extra))
702 non_fatal (_("Ignoring rubbish found on line %d of %s"),
703 line_count, filename);
709 add_specific_symbol (name, list);
711 /* Advance line pointer to end of line. The 'eol ++' in the for
712 loop above will then advance us to the start of the next line. */
718 /* See whether a symbol should be stripped or kept based on
719 strip_specific_list and keep_symbols. */
722 is_specified_symbol (const char *name, struct symlist *list)
724 struct symlist *tmp_list;
728 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
729 if (*(tmp_list->name) != '!')
731 if (!fnmatch (tmp_list->name, name, 0))
736 if (fnmatch (tmp_list->name + 1, name, 0))
742 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
743 if (strcmp (name, tmp_list->name) == 0)
750 /* See if a section is being removed. */
753 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
755 if (sections_removed || sections_copied)
757 struct section_list *p;
759 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
761 if (sections_removed && p != NULL && p->remove)
763 if (sections_copied && (p == NULL || ! p->copy))
767 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
769 if (strip_symbols == STRIP_DEBUG
770 || strip_symbols == STRIP_UNNEEDED
771 || strip_symbols == STRIP_ALL
772 || discard_locals == LOCALS_ALL
773 || convert_debugging)
776 if (strip_symbols == STRIP_NONDEBUG)
783 /* Choose which symbol entries to copy; put the result in OSYMS.
784 We don't copy in place, because that confuses the relocs.
785 Return the number of symbols to print. */
788 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
789 asymbol **isyms, long symcount)
791 asymbol **from = isyms, **to = osyms;
792 long src_count = 0, dst_count = 0;
793 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
796 for (; src_count < symcount; src_count++)
798 asymbol *sym = from[src_count];
799 flagword flags = sym->flags;
800 char *name = (char *) bfd_asymbol_name (sym);
802 bfd_boolean undefined;
803 bfd_boolean rem_leading_char;
804 bfd_boolean add_leading_char;
806 undefined = bfd_is_und_section (bfd_get_section (sym));
808 if (redefine_sym_list)
810 char *old_name, *new_name;
812 old_name = (char *) bfd_asymbol_name (sym);
813 new_name = (char *) lookup_sym_redefinition (old_name);
814 bfd_asymbol_name (sym) = new_name;
818 /* Check if we will remove the current leading character. */
820 (name[0] == bfd_get_symbol_leading_char (abfd))
821 && (change_leading_char
822 || (remove_leading_char
823 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
825 || bfd_is_com_section (bfd_get_section (sym)))));
827 /* Check if we will add a new leading character. */
830 && (bfd_get_symbol_leading_char (obfd) != '\0')
831 && (bfd_get_symbol_leading_char (abfd) == '\0'
832 || (name[0] == bfd_get_symbol_leading_char (abfd)));
834 /* Short circuit for change_leading_char if we can do it in-place. */
835 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
837 name[0] = bfd_get_symbol_leading_char (obfd);
838 bfd_asymbol_name (sym) = name;
839 rem_leading_char = FALSE;
840 add_leading_char = FALSE;
843 /* Remove leading char. */
844 if (rem_leading_char)
845 bfd_asymbol_name (sym) = ++name;
847 /* Add new leading char and/or prefix. */
848 if (add_leading_char || prefix_symbols_string)
852 ptr = n = xmalloc (1 + strlen (prefix_symbols_string)
853 + strlen (name) + 1);
854 if (add_leading_char)
855 *ptr++ = bfd_get_symbol_leading_char (obfd);
857 if (prefix_symbols_string)
859 strcpy (ptr, prefix_symbols_string);
860 ptr += strlen (prefix_symbols_string);
864 bfd_asymbol_name (sym) = n;
868 if (strip_symbols == STRIP_ALL)
870 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
871 || ((flags & BSF_SECTION_SYM) != 0
872 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
875 else if (relocatable /* Relocatable file. */
876 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
878 else if (bfd_decode_symclass (sym) == 'I')
879 /* Global symbols in $idata sections need to be retained
880 even if relocatable is FALSE. External users of the
881 library containing the $idata section may reference these
884 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
885 || (flags & BSF_WEAK) != 0
887 || bfd_is_com_section (bfd_get_section (sym)))
888 keep = strip_symbols != STRIP_UNNEEDED;
889 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
890 keep = (strip_symbols != STRIP_DEBUG
891 && strip_symbols != STRIP_UNNEEDED
892 && ! convert_debugging);
893 else if (bfd_get_section (sym)->comdat)
894 /* COMDAT sections store special information in local
895 symbols, so we cannot risk stripping any of them. */
897 else /* Local symbol. */
898 keep = (strip_symbols != STRIP_UNNEEDED
899 && (discard_locals != LOCALS_ALL
900 && (discard_locals != LOCALS_START_L
901 || ! bfd_is_local_label (abfd, sym))));
903 if (keep && is_specified_symbol (name, strip_specific_list))
905 if (!keep && is_specified_symbol (name, keep_specific_list))
907 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
910 if (keep && (flags & BSF_GLOBAL) != 0
911 && (weaken || is_specified_symbol (name, weaken_specific_list)))
913 sym->flags &=~ BSF_GLOBAL;
914 sym->flags |= BSF_WEAK;
916 if (keep && !undefined && (flags & (BSF_GLOBAL | BSF_WEAK))
917 && (is_specified_symbol (name, localize_specific_list)
918 || (keepglobal_specific_list != NULL
919 && ! is_specified_symbol (name, keepglobal_specific_list))))
921 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
922 sym->flags |= BSF_LOCAL;
926 to[dst_count++] = sym;
929 to[dst_count] = NULL;
934 /* Find the redefined name of symbol SOURCE. */
937 lookup_sym_redefinition (const char *source)
939 struct redefine_node *list;
941 for (list = redefine_sym_list; list != NULL; list = list->next)
942 if (strcmp (source, list->source) == 0)
948 /* Add a node to a symbol redefine list. */
951 redefine_list_append (const char *cause, const char *source, const char *target)
953 struct redefine_node **p;
954 struct redefine_node *list;
955 struct redefine_node *new_node;
957 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
959 if (strcmp (source, list->source) == 0)
960 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
963 if (strcmp (target, list->target) == 0)
964 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
968 new_node = xmalloc (sizeof (struct redefine_node));
970 new_node->source = strdup (source);
971 new_node->target = strdup (target);
972 new_node->next = NULL;
977 /* Handle the --redefine-syms option. Read lines containing "old new"
978 from the file, and add them to the symbol redefine list. */
981 add_redefine_syms_file (const char *filename)
990 file = fopen (filename, "r");
992 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
993 filename, strerror (errno));
996 buf = xmalloc (bufsize);
1004 /* Collect the input symbol name. */
1005 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1013 buf = xrealloc (buf, bufsize);
1021 /* Eat white space between the symbol names. */
1022 while (IS_WHITESPACE (c))
1024 if (c == '#' || IS_LINE_TERMINATOR (c))
1029 /* Collect the output symbol name. */
1031 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1039 buf = xrealloc (buf, bufsize);
1047 /* Eat white space at end of line. */
1048 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1053 if ((c == '\r' && (c = getc (file)) == '\n')
1054 || c == '\n' || c == EOF)
1057 /* Append the redefinition to the list. */
1059 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1070 fatal (_("%s: garbage at end of line %d"), filename, lineno);
1072 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1073 fatal (_("%s: missing new symbol name at line %d"), filename, lineno);
1076 /* Eat the rest of the line and finish it. */
1077 while (c != '\n' && c != EOF)
1083 fatal (_("%s: premature end of file at line %d"), filename, lineno);
1088 /* Copy object file IBFD onto OBFD.
1089 Returns TRUE upon success, FALSE otherwise. */
1092 copy_object (bfd *ibfd, bfd *obfd)
1096 asection **osections = NULL;
1097 asection *gnu_debuglink_section = NULL;
1098 bfd_size_type *gaps = NULL;
1099 bfd_size_type max_gap = 0;
1102 enum bfd_architecture iarch;
1105 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1106 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1107 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1108 fatal (_("Unable to change endianness of input file(s)"));
1110 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1112 bfd_nonfatal (bfd_get_filename (obfd));
1117 printf (_("copy from %s(%s) to %s(%s)\n"),
1118 bfd_get_filename (ibfd), bfd_get_target (ibfd),
1119 bfd_get_filename (obfd), bfd_get_target (obfd));
1124 start = bfd_get_start_address (ibfd);
1125 start += change_start;
1127 /* Neither the start address nor the flags
1128 need to be set for a core file. */
1129 if (bfd_get_format (obfd) != bfd_core)
1133 flags = bfd_get_file_flags (ibfd);
1134 flags |= bfd_flags_to_set;
1135 flags &= ~bfd_flags_to_clear;
1136 flags &= bfd_applicable_file_flags (obfd);
1138 if (!bfd_set_start_address (obfd, start)
1139 || !bfd_set_file_flags (obfd, flags))
1141 bfd_nonfatal (bfd_get_filename (ibfd));
1146 /* Copy architecture of input file to output file. */
1147 iarch = bfd_get_arch (ibfd);
1148 imach = bfd_get_mach (ibfd);
1149 if (!bfd_set_arch_mach (obfd, iarch, imach)
1150 && (ibfd->target_defaulted
1151 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1153 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1154 fatal (_("Unable to recognise the format of the input file %s"),
1155 bfd_get_filename (ibfd));
1158 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1159 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1160 bfd_get_mach (ibfd)));
1165 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1167 bfd_nonfatal (bfd_get_filename (ibfd));
1174 if (osympp != isympp)
1177 /* BFD mandates that all output sections be created and sizes set before
1178 any output is done. Thus, we traverse all sections multiple times. */
1179 bfd_map_over_sections (ibfd, setup_section, obfd);
1181 if (add_sections != NULL)
1183 struct section_add *padd;
1184 struct section_list *pset;
1186 for (padd = add_sections; padd != NULL; padd = padd->next)
1190 padd->section = bfd_make_section (obfd, padd->name);
1191 if (padd->section == NULL)
1193 non_fatal (_("can't create section `%s': %s"),
1194 padd->name, bfd_errmsg (bfd_get_error ()));
1198 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1200 bfd_nonfatal (bfd_get_filename (obfd));
1204 pset = find_section_list (padd->name, FALSE);
1208 if (pset != NULL && pset->set_flags)
1209 flags = pset->flags | SEC_HAS_CONTENTS;
1211 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1213 if (! bfd_set_section_flags (obfd, padd->section, flags))
1215 bfd_nonfatal (bfd_get_filename (obfd));
1221 if (pset->change_vma != CHANGE_IGNORE)
1222 if (! bfd_set_section_vma (obfd, padd->section,
1225 bfd_nonfatal (bfd_get_filename (obfd));
1229 if (pset->change_lma != CHANGE_IGNORE)
1231 padd->section->lma = pset->lma_val;
1233 if (! bfd_set_section_alignment
1234 (obfd, padd->section,
1235 bfd_section_alignment (obfd, padd->section)))
1237 bfd_nonfatal (bfd_get_filename (obfd));
1245 if (gnu_debuglink_filename != NULL)
1247 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1248 (obfd, gnu_debuglink_filename);
1250 if (gnu_debuglink_section == NULL)
1252 bfd_nonfatal (gnu_debuglink_filename);
1257 if (bfd_count_sections (obfd) == 0)
1259 non_fatal (_("there are no sections to be copied!"));
1263 if (gap_fill_set || pad_to_set)
1268 /* We must fill in gaps between the sections and/or we must pad
1269 the last section to a specified address. We do this by
1270 grabbing a list of the sections, sorting them by VMA, and
1271 increasing the section sizes as required to fill the gaps.
1272 We write out the gap contents below. */
1274 c = bfd_count_sections (obfd);
1275 osections = xmalloc (c * sizeof (asection *));
1277 bfd_map_over_sections (obfd, get_sections, &set);
1279 qsort (osections, c, sizeof (asection *), compare_section_lma);
1281 gaps = xmalloc (c * sizeof (bfd_size_type));
1282 memset (gaps, 0, c * sizeof (bfd_size_type));
1286 for (i = 0; i < c - 1; i++)
1290 bfd_vma gap_start, gap_stop;
1292 flags = bfd_get_section_flags (obfd, osections[i]);
1293 if ((flags & SEC_HAS_CONTENTS) == 0
1294 || (flags & SEC_LOAD) == 0)
1297 size = bfd_section_size (obfd, osections[i]);
1298 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1299 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1300 if (gap_start < gap_stop)
1302 if (! bfd_set_section_size (obfd, osections[i],
1303 size + (gap_stop - gap_start)))
1305 non_fatal (_("Can't fill gap after %s: %s"),
1306 bfd_get_section_name (obfd, osections[i]),
1307 bfd_errmsg (bfd_get_error ()));
1311 gaps[i] = gap_stop - gap_start;
1312 if (max_gap < gap_stop - gap_start)
1313 max_gap = gap_stop - gap_start;
1323 lma = bfd_section_lma (obfd, osections[c - 1]);
1324 size = bfd_section_size (obfd, osections[c - 1]);
1325 if (lma + size < pad_to)
1327 if (! bfd_set_section_size (obfd, osections[c - 1],
1330 non_fatal (_("Can't add padding to %s: %s"),
1331 bfd_get_section_name (obfd, osections[c - 1]),
1332 bfd_errmsg (bfd_get_error ()));
1337 gaps[c - 1] = pad_to - (lma + size);
1338 if (max_gap < pad_to - (lma + size))
1339 max_gap = pad_to - (lma + size);
1345 /* Symbol filtering must happen after the output sections
1346 have been created, but before their contents are set. */
1348 symsize = bfd_get_symtab_upper_bound (ibfd);
1351 bfd_nonfatal (bfd_get_filename (ibfd));
1355 osympp = isympp = xmalloc (symsize);
1356 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1359 bfd_nonfatal (bfd_get_filename (ibfd));
1363 if (convert_debugging)
1364 dhandle = read_debugging_info (ibfd, isympp, symcount);
1366 if (strip_symbols == STRIP_DEBUG
1367 || strip_symbols == STRIP_ALL
1368 || strip_symbols == STRIP_UNNEEDED
1369 || strip_symbols == STRIP_NONDEBUG
1370 || discard_locals != LOCALS_UNDEF
1371 || strip_specific_list != NULL
1372 || keep_specific_list != NULL
1373 || localize_specific_list != NULL
1374 || keepglobal_specific_list != NULL
1375 || weaken_specific_list != NULL
1376 || prefix_symbols_string
1379 || convert_debugging
1380 || change_leading_char
1381 || remove_leading_char
1382 || redefine_sym_list
1385 /* Mark symbols used in output relocations so that they
1386 are kept, even if they are local labels or static symbols.
1388 Note we iterate over the input sections examining their
1389 relocations since the relocations for the output sections
1390 haven't been set yet. mark_symbols_used_in_relocations will
1391 ignore input sections which have no corresponding output
1393 if (strip_symbols != STRIP_ALL)
1394 bfd_map_over_sections (ibfd,
1395 mark_symbols_used_in_relocations,
1397 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1398 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1401 if (convert_debugging && dhandle != NULL)
1403 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1410 bfd_set_symtab (obfd, osympp, symcount);
1412 /* This has to happen after the symbol table has been set. */
1413 bfd_map_over_sections (ibfd, copy_section, obfd);
1415 if (add_sections != NULL)
1417 struct section_add *padd;
1419 for (padd = add_sections; padd != NULL; padd = padd->next)
1421 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1424 bfd_nonfatal (bfd_get_filename (obfd));
1430 if (gnu_debuglink_filename != NULL)
1432 if (! bfd_fill_in_gnu_debuglink_section
1433 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1435 bfd_nonfatal (gnu_debuglink_filename);
1440 if (gap_fill_set || pad_to_set)
1445 /* Fill in the gaps. */
1448 buf = xmalloc (max_gap);
1449 memset (buf, gap_fill, max_gap);
1451 c = bfd_count_sections (obfd);
1452 for (i = 0; i < c; i++)
1460 off = bfd_section_size (obfd, osections[i]) - left;
1471 if (! bfd_set_section_contents (obfd, osections[i], buf,
1474 bfd_nonfatal (bfd_get_filename (obfd));
1485 /* Allow the BFD backend to copy any private data it understands
1486 from the input BFD to the output BFD. This is done last to
1487 permit the routine to look at the filtered symbol table, which is
1488 important for the ECOFF code at least. */
1489 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1490 && strip_symbols == STRIP_NONDEBUG)
1491 /* Do not copy the private data when creating an ELF format
1492 debug info file. We do not want the program headers. */
1494 else if (! bfd_copy_private_bfd_data (ibfd, obfd))
1496 non_fatal (_("%s: error copying private BFD data: %s"),
1497 bfd_get_filename (obfd),
1498 bfd_errmsg (bfd_get_error ()));
1502 /* Switch to the alternate machine code. We have to do this at the
1503 very end, because we only initialize the header when we create
1504 the first section. */
1505 if (use_alt_mach_code != 0
1506 && ! bfd_alt_mach_code (obfd, use_alt_mach_code))
1507 non_fatal (_("unknown alternate machine code, ignored"));
1513 #if defined (_WIN32) && !defined (__CYGWIN32__)
1514 #define MKDIR(DIR, MODE) mkdir (DIR)
1516 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1519 /* Read each archive element in turn from IBFD, copy the
1520 contents to temp file, and keep the temp file handle. */
1523 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target)
1527 struct name_list *next;
1531 bfd **ptr = &obfd->archive_head;
1533 char *dir = make_tempname (bfd_get_filename (obfd));
1535 /* Make a temp directory to hold the contents. */
1536 if (MKDIR (dir, 0700) != 0)
1537 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1538 dir, strerror (errno));
1540 obfd->has_armap = ibfd->has_armap;
1544 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1546 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1547 RETURN_NONFATAL (bfd_get_filename (obfd));
1549 while (!status && this_element != NULL)
1555 int stat_status = 0;
1556 bfd_boolean delete = TRUE;
1558 /* Create an output file for this member. */
1559 output_name = concat (dir, "/",
1560 bfd_get_filename (this_element), (char *) 0);
1562 /* If the file already exists, make another temp dir. */
1563 if (stat (output_name, &buf) >= 0)
1565 output_name = make_tempname (output_name);
1566 if (MKDIR (output_name, 0700) != 0)
1567 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1568 output_name, strerror (errno));
1570 l = xmalloc (sizeof (struct name_list));
1571 l->name = output_name;
1575 output_name = concat (output_name, "/",
1576 bfd_get_filename (this_element), (char *) 0);
1579 output_bfd = bfd_openw (output_name, output_target);
1582 stat_status = bfd_stat_arch_elt (this_element, &buf);
1584 if (stat_status != 0)
1585 non_fatal (_("internal stat error on %s"),
1586 bfd_get_filename (this_element));
1589 l = xmalloc (sizeof (struct name_list));
1590 l->name = output_name;
1594 if (output_bfd == NULL)
1595 RETURN_NONFATAL (output_name);
1597 if (bfd_check_format (this_element, bfd_object))
1598 delete = ! copy_object (this_element, output_bfd);
1600 if (!bfd_close (output_bfd))
1602 bfd_nonfatal (bfd_get_filename (output_bfd));
1603 /* Error in new object file. Don't change archive. */
1609 unlink (output_name);
1614 if (preserve_dates && stat_status == 0)
1615 set_times (output_name, &buf);
1617 /* Open the newly output file and attach to our list. */
1618 output_bfd = bfd_openr (output_name, output_target);
1620 l->obfd = output_bfd;
1623 ptr = &output_bfd->next;
1625 last_element = this_element;
1627 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1629 bfd_close (last_element);
1634 if (!bfd_close (obfd))
1635 RETURN_NONFATAL (bfd_get_filename (obfd));
1637 if (!bfd_close (ibfd))
1638 RETURN_NONFATAL (bfd_get_filename (ibfd));
1640 /* Delete all the files that we opened. */
1641 for (l = list; l != NULL; l = l->next)
1643 if (l->obfd == NULL)
1647 bfd_close (l->obfd);
1654 /* The top-level control. */
1657 copy_file (const char *input_filename, const char *output_filename,
1658 const char *input_target, const char *output_target)
1661 char **obj_matching;
1662 char **core_matching;
1664 if (get_file_size (input_filename) < 1)
1670 /* To allow us to do "strip *" without dying on the first
1671 non-object file, failures are nonfatal. */
1672 ibfd = bfd_openr (input_filename, input_target);
1674 RETURN_NONFATAL (input_filename);
1676 if (bfd_check_format (ibfd, bfd_archive))
1680 /* bfd_get_target does not return the correct value until
1681 bfd_check_format succeeds. */
1682 if (output_target == NULL)
1683 output_target = bfd_get_target (ibfd);
1685 obfd = bfd_openw (output_filename, output_target);
1687 RETURN_NONFATAL (output_filename);
1689 copy_archive (ibfd, obfd, output_target);
1691 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
1697 /* bfd_get_target does not return the correct value until
1698 bfd_check_format succeeds. */
1699 if (output_target == NULL)
1700 output_target = bfd_get_target (ibfd);
1702 obfd = bfd_openw (output_filename, output_target);
1704 RETURN_NONFATAL (output_filename);
1706 delete = ! copy_object (ibfd, obfd);
1708 if (!bfd_close (obfd))
1709 RETURN_NONFATAL (output_filename);
1711 if (!bfd_close (ibfd))
1712 RETURN_NONFATAL (input_filename);
1716 unlink (output_filename);
1722 bfd_error_type obj_error = bfd_get_error ();
1723 bfd_error_type core_error;
1725 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1727 /* This probably can't happen.. */
1728 if (obj_error == bfd_error_file_ambiguously_recognized)
1729 free (obj_matching);
1733 core_error = bfd_get_error ();
1734 /* Report the object error in preference to the core error. */
1735 if (obj_error != core_error)
1736 bfd_set_error (obj_error);
1738 bfd_nonfatal (input_filename);
1740 if (obj_error == bfd_error_file_ambiguously_recognized)
1742 list_matching_formats (obj_matching);
1743 free (obj_matching);
1745 if (core_error == bfd_error_file_ambiguously_recognized)
1747 list_matching_formats (core_matching);
1748 free (core_matching);
1755 /* Add a name to the section renaming list. */
1758 add_section_rename (const char * old_name, const char * new_name,
1761 section_rename * rename;
1763 /* Check for conflicts first. */
1764 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1765 if (strcmp (rename->old_name, old_name) == 0)
1767 /* Silently ignore duplicate definitions. */
1768 if (strcmp (rename->new_name, new_name) == 0
1769 && rename->flags == flags)
1772 fatal (_("Multiple renames of section %s"), old_name);
1775 rename = xmalloc (sizeof (* rename));
1777 rename->old_name = old_name;
1778 rename->new_name = new_name;
1779 rename->flags = flags;
1780 rename->next = section_rename_list;
1782 section_rename_list = rename;
1785 /* Check the section rename list for a new name of the input section
1786 ISECTION. Return the new name if one is found.
1787 Also set RETURNED_FLAGS to the flags to be used for this section. */
1790 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
1791 flagword * returned_flags)
1793 const char * old_name = bfd_section_name (ibfd, isection);
1794 section_rename * rename;
1796 /* Default to using the flags of the input section. */
1797 * returned_flags = bfd_get_section_flags (ibfd, isection);
1799 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1800 if (strcmp (rename->old_name, old_name) == 0)
1802 if (rename->flags != (flagword) -1)
1803 * returned_flags = rename->flags;
1805 return rename->new_name;
1811 /* Create a section in OBFD with the same
1812 name and attributes as ISECTION in IBFD. */
1815 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
1817 bfd *obfd = obfdarg;
1818 struct section_list *p;
1826 char *prefix = NULL;
1828 if (is_strip_section (ibfd, isection))
1831 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
1835 /* Get the, possibly new, name of the output section. */
1836 name = find_section_rename (ibfd, isection, & flags);
1838 /* Prefix sections. */
1839 if ((prefix_alloc_sections_string)
1840 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
1841 prefix = prefix_alloc_sections_string;
1842 else if (prefix_sections_string)
1843 prefix = prefix_sections_string;
1849 n = xmalloc (strlen (prefix) + strlen (name) + 1);
1855 osection = bfd_make_section_anyway (obfd, name);
1857 if (osection == NULL)
1863 size = bfd_section_size (ibfd, isection);
1865 size = (size + interleave - 1) / interleave;
1866 if (! bfd_set_section_size (obfd, osection, size))
1872 vma = bfd_section_vma (ibfd, isection);
1873 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1875 else if (p != NULL && p->change_vma == CHANGE_SET)
1878 vma += change_section_address;
1880 if (! bfd_set_section_vma (obfd, osection, vma))
1886 lma = isection->lma;
1887 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1889 if (p->change_lma == CHANGE_MODIFY)
1891 else if (p->change_lma == CHANGE_SET)
1897 lma += change_section_address;
1899 osection->lma = lma;
1901 /* FIXME: This is probably not enough. If we change the LMA we
1902 may have to recompute the header for the file as well. */
1903 if (!bfd_set_section_alignment (obfd,
1905 bfd_section_alignment (ibfd, isection)))
1907 err = _("alignment");
1911 if (p != NULL && p->set_flags)
1912 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
1913 else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
1915 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
1916 if (obfd->xvec->flavour == bfd_target_elf_flavour)
1917 elf_section_type (osection) = SHT_NOBITS;
1920 if (!bfd_set_section_flags (obfd, osection, flags))
1926 /* Copy merge entity size. */
1927 osection->entsize = isection->entsize;
1929 /* This used to be mangle_section; we do here to avoid using
1930 bfd_get_section_by_name since some formats allow multiple
1931 sections with the same name. */
1932 isection->output_section = osection;
1933 isection->output_offset = 0;
1935 /* Allow the BFD backend to copy any private data it understands
1936 from the input section to the output section. */
1937 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1938 && strip_symbols == STRIP_NONDEBUG)
1939 /* Do not copy the private data when creating an ELF format
1940 debug info file. We do not want the program headers. */
1942 else if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1944 err = _("private data");
1948 /* All went well. */
1952 non_fatal (_("%s: section `%s': error in %s: %s"),
1953 bfd_get_filename (ibfd),
1954 bfd_section_name (ibfd, isection),
1955 err, bfd_errmsg (bfd_get_error ()));
1959 /* Copy the data of input section ISECTION of IBFD
1960 to an output section with the same name in OBFD.
1961 If stripping then don't copy any relocation info. */
1964 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
1966 bfd *obfd = obfdarg;
1967 struct section_list *p;
1975 /* If we have already failed earlier on,
1976 do not keep on generating complaints now. */
1980 if (is_strip_section (ibfd, isection))
1983 flags = bfd_get_section_flags (ibfd, isection);
1984 if ((flags & SEC_GROUP) != 0)
1987 osection = isection->output_section;
1988 size = bfd_get_section_size_before_reloc (isection);
1990 if (size == 0 || osection == 0)
1993 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
1995 /* Core files do not need to be relocated. */
1996 if (bfd_get_format (obfd) == bfd_core)
2000 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2004 /* Do not complain if the target does not support relocations. */
2005 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2008 RETURN_NONFATAL (bfd_get_filename (ibfd));
2013 bfd_set_reloc (obfd, osection, NULL, 0);
2016 relpp = xmalloc (relsize);
2017 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2019 RETURN_NONFATAL (bfd_get_filename (ibfd));
2021 if (strip_symbols == STRIP_ALL)
2023 /* Remove relocations which are not in
2024 keep_strip_specific_list. */
2025 arelent **temp_relpp;
2026 long temp_relcount = 0;
2029 temp_relpp = xmalloc (relsize);
2030 for (i = 0; i < relcount; i++)
2031 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2032 keep_specific_list))
2033 temp_relpp [temp_relcount++] = relpp [i];
2034 relcount = temp_relcount;
2039 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2044 isection->_cooked_size = isection->_raw_size;
2045 isection->reloc_done = TRUE;
2047 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2048 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2050 void *memhunk = xmalloc (size);
2052 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2053 RETURN_NONFATAL (bfd_get_filename (ibfd));
2057 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2058 char *from = (char *) memhunk + copy_byte;
2060 char *end = (char *) memhunk + size;
2062 for (; from < end; from += interleave)
2065 size = (size + interleave - 1 - copy_byte) / interleave;
2066 osection->lma /= interleave;
2069 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2070 RETURN_NONFATAL (bfd_get_filename (obfd));
2074 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2076 void *memhunk = xmalloc (size);
2078 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2079 flag--they can just remove the section entirely and add it
2080 back again. However, we do permit them to turn on the
2081 SEC_HAS_CONTENTS flag, and take it to mean that the section
2082 contents should be zeroed out. */
2084 memset (memhunk, 0, size);
2085 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2086 RETURN_NONFATAL (bfd_get_filename (obfd));
2091 /* Get all the sections. This is used when --gap-fill or --pad-to is
2095 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2097 asection ***secppp = secppparg;
2099 **secppp = osection;
2103 /* Sort sections by VMA. This is called via qsort, and is used when
2104 --gap-fill or --pad-to is used. We force non loadable or empty
2105 sections to the front, where they are easier to ignore. */
2108 compare_section_lma (const void *arg1, const void *arg2)
2110 const asection *const *sec1 = arg1;
2111 const asection *const *sec2 = arg2;
2112 flagword flags1, flags2;
2114 /* Sort non loadable sections to the front. */
2115 flags1 = (*sec1)->flags;
2116 flags2 = (*sec2)->flags;
2117 if ((flags1 & SEC_HAS_CONTENTS) == 0
2118 || (flags1 & SEC_LOAD) == 0)
2120 if ((flags2 & SEC_HAS_CONTENTS) != 0
2121 && (flags2 & SEC_LOAD) != 0)
2126 if ((flags2 & SEC_HAS_CONTENTS) == 0
2127 || (flags2 & SEC_LOAD) == 0)
2131 /* Sort sections by LMA. */
2132 if ((*sec1)->lma > (*sec2)->lma)
2134 else if ((*sec1)->lma < (*sec2)->lma)
2137 /* Sort sections with the same LMA by size. */
2138 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
2140 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
2146 /* Mark all the symbols which will be used in output relocations with
2147 the BSF_KEEP flag so that those symbols will not be stripped.
2149 Ignore relocations which will not appear in the output file. */
2152 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2154 asymbol **symbols = symbolsarg;
2159 /* Ignore an input section with no corresponding output section. */
2160 if (isection->output_section == NULL)
2163 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2166 /* Do not complain if the target does not support relocations. */
2167 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2169 bfd_fatal (bfd_get_filename (ibfd));
2175 relpp = xmalloc (relsize);
2176 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2178 bfd_fatal (bfd_get_filename (ibfd));
2180 /* Examine each symbol used in a relocation. If it's not one of the
2181 special bfd section symbols, then mark it with BSF_KEEP. */
2182 for (i = 0; i < relcount; i++)
2184 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2185 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2186 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2187 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2194 /* Write out debugging information. */
2197 write_debugging_info (bfd *obfd, void *dhandle,
2198 long *symcountp ATTRIBUTE_UNUSED,
2199 asymbol ***symppp ATTRIBUTE_UNUSED)
2201 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2202 return write_ieee_debugging_info (obfd, dhandle);
2204 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2205 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2207 bfd_byte *syms, *strings;
2208 bfd_size_type symsize, stringsize;
2209 asection *stabsec, *stabstrsec;
2211 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2216 stabsec = bfd_make_section (obfd, ".stab");
2217 stabstrsec = bfd_make_section (obfd, ".stabstr");
2219 || stabstrsec == NULL
2220 || ! bfd_set_section_size (obfd, stabsec, symsize)
2221 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2222 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2223 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
2224 || ! bfd_set_section_flags (obfd, stabsec,
2228 || ! bfd_set_section_flags (obfd, stabstrsec,
2233 non_fatal (_("%s: can't create debugging section: %s"),
2234 bfd_get_filename (obfd),
2235 bfd_errmsg (bfd_get_error ()));
2239 /* We can get away with setting the section contents now because
2240 the next thing the caller is going to do is copy over the
2241 real sections. We may someday have to split the contents
2242 setting out of this function. */
2243 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2244 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2247 non_fatal (_("%s: can't set debugging section contents: %s"),
2248 bfd_get_filename (obfd),
2249 bfd_errmsg (bfd_get_error ()));
2256 non_fatal (_("%s: don't know how to write debugging information for %s"),
2257 bfd_get_filename (obfd), bfd_get_target (obfd));
2262 strip_main (int argc, char *argv[])
2264 char *input_target = NULL;
2265 char *output_target = NULL;
2266 bfd_boolean show_version = FALSE;
2267 bfd_boolean formats_info = FALSE;
2270 struct section_list *p;
2271 char *output_file = NULL;
2273 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2274 strip_options, (int *) 0)) != EOF)
2279 input_target = optarg;
2282 output_target = optarg;
2285 input_target = output_target = optarg;
2288 p = find_section_list (optarg, TRUE);
2290 sections_removed = TRUE;
2293 strip_symbols = STRIP_ALL;
2297 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2298 strip_symbols = STRIP_DEBUG;
2300 case OPTION_STRIP_UNNEEDED:
2301 strip_symbols = STRIP_UNNEEDED;
2304 add_specific_symbol (optarg, &keep_specific_list);
2307 add_specific_symbol (optarg, &strip_specific_list);
2310 output_file = optarg;
2313 preserve_dates = TRUE;
2316 discard_locals = LOCALS_ALL;
2319 discard_locals = LOCALS_START_L;
2325 show_version = TRUE;
2327 case OPTION_FORMATS_INFO:
2328 formats_info = TRUE;
2330 case OPTION_ONLY_KEEP_DEBUG:
2331 strip_symbols = STRIP_NONDEBUG;
2334 /* We've been given a long option. */
2341 strip_usage (stdout, 0);
2343 strip_usage (stderr, 1);
2354 print_version ("strip");
2356 /* Default is to strip all symbols. */
2357 if (strip_symbols == STRIP_UNDEF
2358 && discard_locals == LOCALS_UNDEF
2359 && strip_specific_list == NULL)
2360 strip_symbols = STRIP_ALL;
2362 if (output_target == NULL)
2363 output_target = input_target;
2367 || (output_file != NULL && (i + 1) < argc))
2368 strip_usage (stderr, 1);
2370 for (; i < argc; i++)
2372 int hold_status = status;
2373 struct stat statbuf;
2376 if (get_file_size (argv[i]) < 1)
2380 /* No need to check the return value of stat().
2381 It has already been checked in get_file_size(). */
2382 stat (argv[i], &statbuf);
2384 if (output_file != NULL)
2385 tmpname = output_file;
2387 tmpname = make_tempname (argv[i]);
2390 copy_file (argv[i], tmpname, input_target, output_target);
2394 set_times (tmpname, &statbuf);
2395 if (output_file == NULL)
2396 smart_rename (tmpname, argv[i], preserve_dates);
2397 status = hold_status;
2401 if (output_file == NULL)
2409 copy_main (int argc, char *argv[])
2411 char * binary_architecture = NULL;
2412 char *input_filename = NULL;
2413 char *output_filename = NULL;
2414 char *input_target = NULL;
2415 char *output_target = NULL;
2416 bfd_boolean show_version = FALSE;
2417 bfd_boolean change_warn = TRUE;
2418 bfd_boolean formats_info = FALSE;
2420 struct section_list *p;
2421 struct stat statbuf;
2423 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2424 copy_options, (int *) 0)) != EOF)
2429 copy_byte = atoi (optarg);
2431 fatal (_("byte number must be non-negative"));
2435 binary_architecture = optarg;
2439 interleave = atoi (optarg);
2441 fatal (_("interleave must be positive"));
2445 case 's': /* "source" - 'I' is preferred */
2446 input_target = optarg;
2450 case 'd': /* "destination" - 'O' is preferred */
2451 output_target = optarg;
2455 input_target = output_target = optarg;
2459 p = find_section_list (optarg, TRUE);
2461 fatal (_("%s both copied and removed"), optarg);
2463 sections_copied = TRUE;
2467 p = find_section_list (optarg, TRUE);
2469 fatal (_("%s both copied and removed"), optarg);
2471 sections_removed = TRUE;
2475 strip_symbols = STRIP_ALL;
2479 strip_symbols = STRIP_DEBUG;
2482 case OPTION_STRIP_UNNEEDED:
2483 strip_symbols = STRIP_UNNEEDED;
2486 case OPTION_ONLY_KEEP_DEBUG:
2487 strip_symbols = STRIP_NONDEBUG;
2490 case OPTION_ADD_GNU_DEBUGLINK:
2491 gnu_debuglink_filename = optarg;
2495 add_specific_symbol (optarg, &keep_specific_list);
2499 add_specific_symbol (optarg, &strip_specific_list);
2503 add_specific_symbol (optarg, &localize_specific_list);
2507 add_specific_symbol (optarg, &keepglobal_specific_list);
2511 add_specific_symbol (optarg, &weaken_specific_list);
2515 preserve_dates = TRUE;
2523 discard_locals = LOCALS_ALL;
2527 discard_locals = LOCALS_START_L;
2535 show_version = TRUE;
2538 case OPTION_FORMATS_INFO:
2539 formats_info = TRUE;
2546 case OPTION_ADD_SECTION:
2550 struct section_add *pa;
2555 s = strchr (optarg, '=');
2558 fatal (_("bad format for %s"), "--add-section");
2560 size = get_file_size (s + 1);
2564 pa = xmalloc (sizeof (struct section_add));
2567 name = xmalloc (len + 1);
2568 strncpy (name, optarg, len);
2572 pa->filename = s + 1;
2574 pa->contents = xmalloc (size);
2576 f = fopen (pa->filename, FOPEN_RB);
2579 fatal (_("cannot open: %s: %s"),
2580 pa->filename, strerror (errno));
2582 if (fread (pa->contents, 1, pa->size, f) == 0
2584 fatal (_("%s: fread failed"), pa->filename);
2588 pa->next = add_sections;
2593 case OPTION_CHANGE_START:
2594 change_start = parse_vma (optarg, "--change-start");
2597 case OPTION_CHANGE_SECTION_ADDRESS:
2598 case OPTION_CHANGE_SECTION_LMA:
2599 case OPTION_CHANGE_SECTION_VMA:
2604 char *option = NULL;
2606 enum change_action what = CHANGE_IGNORE;
2610 case OPTION_CHANGE_SECTION_ADDRESS:
2611 option = "--change-section-address";
2613 case OPTION_CHANGE_SECTION_LMA:
2614 option = "--change-section-lma";
2616 case OPTION_CHANGE_SECTION_VMA:
2617 option = "--change-section-vma";
2621 s = strchr (optarg, '=');
2624 s = strchr (optarg, '+');
2627 s = strchr (optarg, '-');
2629 fatal (_("bad format for %s"), option);
2634 name = xmalloc (len + 1);
2635 strncpy (name, optarg, len);
2638 p = find_section_list (name, TRUE);
2640 val = parse_vma (s + 1, option);
2644 case '=': what = CHANGE_SET; break;
2645 case '-': val = - val; /* Drop through. */
2646 case '+': what = CHANGE_MODIFY; break;
2651 case OPTION_CHANGE_SECTION_ADDRESS:
2652 p->change_vma = what;
2656 case OPTION_CHANGE_SECTION_LMA:
2657 p->change_lma = what;
2661 case OPTION_CHANGE_SECTION_VMA:
2662 p->change_vma = what;
2669 case OPTION_CHANGE_ADDRESSES:
2670 change_section_address = parse_vma (optarg, "--change-addresses");
2671 change_start = change_section_address;
2674 case OPTION_CHANGE_WARNINGS:
2678 case OPTION_CHANGE_LEADING_CHAR:
2679 change_leading_char = TRUE;
2682 case OPTION_DEBUGGING:
2683 convert_debugging = TRUE;
2686 case OPTION_GAP_FILL:
2688 bfd_vma gap_fill_vma;
2690 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2691 gap_fill = (bfd_byte) gap_fill_vma;
2692 if ((bfd_vma) gap_fill != gap_fill_vma)
2696 sprintf_vma (buff, gap_fill_vma);
2698 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2701 gap_fill_set = TRUE;
2705 case OPTION_NO_CHANGE_WARNINGS:
2706 change_warn = FALSE;
2710 pad_to = parse_vma (optarg, "--pad-to");
2714 case OPTION_REMOVE_LEADING_CHAR:
2715 remove_leading_char = TRUE;
2718 case OPTION_REDEFINE_SYM:
2720 /* Push this redefinition onto redefine_symbol_list. */
2724 const char *nextarg;
2725 char *source, *target;
2727 s = strchr (optarg, '=');
2729 fatal (_("bad format for %s"), "--redefine-sym");
2732 source = xmalloc (len + 1);
2733 strncpy (source, optarg, len);
2737 len = strlen (nextarg);
2738 target = xmalloc (len + 1);
2739 strcpy (target, nextarg);
2741 redefine_list_append ("--redefine-sym", source, target);
2748 case OPTION_REDEFINE_SYMS:
2749 add_redefine_syms_file (optarg);
2752 case OPTION_SET_SECTION_FLAGS:
2758 s = strchr (optarg, '=');
2760 fatal (_("bad format for %s"), "--set-section-flags");
2763 name = xmalloc (len + 1);
2764 strncpy (name, optarg, len);
2767 p = find_section_list (name, TRUE);
2769 p->set_flags = TRUE;
2770 p->flags = parse_flags (s + 1);
2774 case OPTION_RENAME_SECTION:
2777 const char *eq, *fl;
2782 eq = strchr (optarg, '=');
2784 fatal (_("bad format for %s"), "--rename-section");
2788 fatal (_("bad format for %s"), "--rename-section");
2790 old_name = xmalloc (len + 1);
2791 strncpy (old_name, optarg, len);
2795 fl = strchr (eq, ',');
2798 flags = parse_flags (fl + 1);
2808 fatal (_("bad format for %s"), "--rename-section");
2810 new_name = xmalloc (len + 1);
2811 strncpy (new_name, eq, len);
2814 add_section_rename (old_name, new_name, flags);
2818 case OPTION_SET_START:
2819 set_start = parse_vma (optarg, "--set-start");
2820 set_start_set = TRUE;
2823 case OPTION_SREC_LEN:
2824 Chunk = parse_vma (optarg, "--srec-len");
2827 case OPTION_SREC_FORCES3:
2831 case OPTION_STRIP_SYMBOLS:
2832 add_specific_symbols (optarg, &strip_specific_list);
2835 case OPTION_KEEP_SYMBOLS:
2836 add_specific_symbols (optarg, &keep_specific_list);
2839 case OPTION_LOCALIZE_SYMBOLS:
2840 add_specific_symbols (optarg, &localize_specific_list);
2843 case OPTION_KEEPGLOBAL_SYMBOLS:
2844 add_specific_symbols (optarg, &keepglobal_specific_list);
2847 case OPTION_WEAKEN_SYMBOLS:
2848 add_specific_symbols (optarg, &weaken_specific_list);
2851 case OPTION_ALT_MACH_CODE:
2852 use_alt_mach_code = atoi (optarg);
2853 if (use_alt_mach_code <= 0)
2854 fatal (_("alternate machine code index must be positive"));
2857 case OPTION_PREFIX_SYMBOLS:
2858 prefix_symbols_string = optarg;
2861 case OPTION_PREFIX_SECTIONS:
2862 prefix_sections_string = optarg;
2865 case OPTION_PREFIX_ALLOC_SECTIONS:
2866 prefix_alloc_sections_string = optarg;
2869 case OPTION_READONLY_TEXT:
2870 bfd_flags_to_set |= WP_TEXT;
2871 bfd_flags_to_clear &= ~WP_TEXT;
2874 case OPTION_WRITABLE_TEXT:
2875 bfd_flags_to_clear |= WP_TEXT;
2876 bfd_flags_to_set &= ~WP_TEXT;
2880 bfd_flags_to_set |= D_PAGED;
2881 bfd_flags_to_clear &= ~D_PAGED;
2885 bfd_flags_to_clear |= D_PAGED;
2886 bfd_flags_to_set &= ~D_PAGED;
2890 /* We've been given a long option. */
2895 copy_usage (stdout, 0);
2898 copy_usage (stderr, 1);
2909 print_version ("objcopy");
2911 if (copy_byte >= interleave)
2912 fatal (_("byte number must be less than interleave"));
2914 if (optind == argc || optind + 2 < argc)
2915 copy_usage (stderr, 1);
2917 input_filename = argv[optind];
2918 if (optind + 1 < argc)
2919 output_filename = argv[optind + 1];
2921 /* Default is to strip no symbols. */
2922 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2923 strip_symbols = STRIP_NONE;
2925 if (output_target == NULL)
2926 output_target = input_target;
2928 if (binary_architecture != NULL)
2930 if (input_target && strcmp (input_target, "binary") == 0)
2932 const bfd_arch_info_type * temp_arch_info;
2934 temp_arch_info = bfd_scan_arch (binary_architecture);
2936 if (temp_arch_info != NULL)
2938 bfd_external_binary_architecture = temp_arch_info->arch;
2939 bfd_external_machine = temp_arch_info->mach;
2942 fatal (_("architecture %s unknown"), binary_architecture);
2946 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2947 non_fatal (_(" Argument %s ignored"), binary_architecture);
2952 if (stat (input_filename, & statbuf) < 0)
2953 fatal (_("warning: could not locate '%s'. System error message: %s"),
2954 input_filename, strerror (errno));
2956 /* If there is no destination file, or the source and destination files
2957 are the same, then create a temp and rename the result into the input. */
2958 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
2960 char *tmpname = make_tempname (input_filename);
2962 copy_file (input_filename, tmpname, input_target, output_target);
2966 set_times (tmpname, &statbuf);
2967 smart_rename (tmpname, input_filename, preserve_dates);
2974 copy_file (input_filename, output_filename, input_target, output_target);
2976 if (status == 0 && preserve_dates)
2977 set_times (output_filename, &statbuf);
2982 for (p = change_sections; p != NULL; p = p->next)
2986 if (p->change_vma != CHANGE_IGNORE)
2990 sprintf_vma (buff, p->vma_val);
2992 /* xgettext:c-format */
2993 non_fatal (_("%s %s%c0x%s never used"),
2994 "--change-section-vma",
2996 p->change_vma == CHANGE_SET ? '=' : '+',
3000 if (p->change_lma != CHANGE_IGNORE)
3004 sprintf_vma (buff, p->lma_val);
3006 /* xgettext:c-format */
3007 non_fatal (_("%s %s%c0x%s never used"),
3008 "--change-section-lma",
3010 p->change_lma == CHANGE_SET ? '=' : '+',
3021 main (int argc, char *argv[])
3023 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3024 setlocale (LC_MESSAGES, "");
3026 #if defined (HAVE_SETLOCALE)
3027 setlocale (LC_CTYPE, "");
3029 bindtextdomain (PACKAGE, LOCALEDIR);
3030 textdomain (PACKAGE);
3032 program_name = argv[0];
3033 xmalloc_set_program_name (program_name);
3035 START_PROGRESS (program_name, 0);
3037 strip_symbols = STRIP_UNDEF;
3038 discard_locals = LOCALS_UNDEF;
3041 set_default_bfd_target ();
3045 int i = strlen (program_name);
3046 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3047 /* Drop the .exe suffix, if any. */
3048 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3051 program_name[i] = '\0';
3054 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3058 strip_main (argc, argv);
3060 copy_main (argc, argv);
3062 END_PROGRESS (program_name);