1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "libiberty.h"
30 /* A list of symbols to explicitly strip out, or to keep. A linked
31 list is good enough for a small number from the command line, but
32 this will slow things down a lot if many symbols are being
41 /* A list to support redefine_sym. */
46 struct redefine_node *next;
49 static void copy_usage PARAMS ((FILE *, int));
50 static void strip_usage PARAMS ((FILE *, int));
51 static flagword parse_flags PARAMS ((const char *));
52 static struct section_list *find_section_list PARAMS ((const char *, boolean));
53 static void setup_section PARAMS ((bfd *, asection *, PTR));
54 static void copy_section PARAMS ((bfd *, asection *, PTR));
55 static void get_sections PARAMS ((bfd *, asection *, PTR));
56 static int compare_section_lma PARAMS ((const PTR, const PTR));
57 static void add_specific_symbol PARAMS ((const char *, struct symlist **));
58 static boolean is_specified_symbol PARAMS ((const char *, struct symlist *));
59 static boolean is_strip_section PARAMS ((bfd *, asection *));
60 static unsigned int filter_symbols
61 PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long));
62 static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
63 static void filter_bytes PARAMS ((char *, bfd_size_type *));
64 static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***));
65 static void copy_object PARAMS ((bfd *, bfd *));
66 static void copy_archive PARAMS ((bfd *, bfd *, const char *));
68 PARAMS ((const char *, const char *, const char *, const char *));
69 static int strip_main PARAMS ((int, char **));
70 static int copy_main PARAMS ((int, char **));
71 static const char *lookup_sym_redefinition PARAMS((const char *));
72 static void redefine_list_append PARAMS ((const char *, const char *));
74 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
76 static asymbol **isympp = NULL; /* Input symbols */
77 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
79 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
80 static int copy_byte = -1;
81 static int interleave = 4;
83 static boolean verbose; /* Print file and target names. */
84 static boolean preserve_dates; /* Preserve input file timestamp. */
85 static int status = 0; /* Exit status. */
90 STRIP_NONE, /* don't strip */
91 STRIP_DEBUG, /* strip all debugger symbols */
92 STRIP_UNNEEDED, /* strip unnecessary symbols */
93 STRIP_ALL /* strip all symbols */
96 /* Which symbols to remove. */
97 static enum strip_action strip_symbols;
102 LOCALS_START_L, /* discard locals starting with L */
103 LOCALS_ALL /* discard all locals */
106 /* Which local symbols to remove. Overrides STRIP_ALL. */
107 static enum locals_action discard_locals;
109 /* What kind of change to perform. */
117 /* Structure used to hold lists of sections and actions to take. */
120 struct section_list * next; /* Next section to change. */
121 const char * name; /* Section name. */
122 boolean used; /* Whether this entry was used. */
123 boolean remove; /* Whether to remove this section. */
124 boolean copy; /* Whether to copy this section. */
125 enum change_action change_vma;/* Whether to change or set VMA. */
126 bfd_vma vma_val; /* Amount to change by or set to. */
127 enum change_action change_lma;/* Whether to change or set LMA. */
128 bfd_vma lma_val; /* Amount to change by or set to. */
129 boolean set_flags; /* Whether to set the section flags. */
130 flagword flags; /* What to set the section flags to. */
133 static struct section_list *change_sections;
134 static boolean sections_removed;
135 static boolean sections_copied;
137 /* Changes to the start address. */
138 static bfd_vma change_start = 0;
139 static boolean set_start_set = false;
140 static bfd_vma set_start;
142 /* Changes to section addresses. */
143 static bfd_vma change_section_address = 0;
145 /* Filling gaps between sections. */
146 static boolean gap_fill_set = false;
147 static bfd_byte gap_fill = 0;
149 /* Pad to a given address. */
150 static boolean pad_to_set = false;
151 static bfd_vma pad_to;
153 /* List of sections to add. */
157 /* Next section to add. */
158 struct section_add *next;
159 /* Name of section to add. */
161 /* Name of file holding section contents. */
162 const char *filename;
165 /* Contents of file. */
167 /* BFD section, after it has been added. */
171 static struct section_add *add_sections;
173 /* Whether to convert debugging information. */
175 static boolean convert_debugging = false;
177 /* Whether to change the leading character in symbol names. */
179 static boolean change_leading_char = false;
181 /* Whether to remove the leading character from global symbol names. */
183 static boolean remove_leading_char = false;
185 /* List of symbols to strip, keep, localize, weaken, or redefine. */
187 static struct symlist *strip_specific_list = NULL;
188 static struct symlist *keep_specific_list = NULL;
189 static struct symlist *localize_specific_list = NULL;
190 static struct symlist *weaken_specific_list = NULL;
191 static struct redefine_node *redefine_sym_list = NULL;
193 /* If this is true, we weaken global symbols (set BSF_WEAK). */
195 static boolean weaken = false;
197 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
199 #define OPTION_ADD_SECTION 150
200 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
201 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
202 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
203 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
204 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
205 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
206 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
207 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
208 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
209 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
210 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
211 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
212 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
213 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
214 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
215 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
216 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
218 /* Options to handle if running as "strip". */
220 static struct option strip_options[] =
222 {"discard-all", no_argument, 0, 'x'},
223 {"discard-locals", no_argument, 0, 'X'},
224 {"format", required_argument, 0, 'F'}, /* Obsolete */
225 {"help", no_argument, 0, 'h'},
226 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
227 {"input-target", required_argument, 0, 'I'},
228 {"keep-symbol", required_argument, 0, 'K'},
229 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
230 {"output-target", required_argument, 0, 'O'},
231 {"preserve-dates", no_argument, 0, 'p'},
232 {"remove-section", required_argument, 0, 'R'},
233 {"strip-all", no_argument, 0, 's'},
234 {"strip-debug", no_argument, 0, 'S'},
235 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
236 {"strip-symbol", required_argument, 0, 'N'},
237 {"target", required_argument, 0, 'F'},
238 {"verbose", no_argument, 0, 'v'},
239 {"version", no_argument, 0, 'V'},
240 {0, no_argument, 0, 0}
243 /* Options to handle if running as "objcopy". */
245 static struct option copy_options[] =
247 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
248 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
249 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
250 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
251 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
252 {"byte", required_argument, 0, 'b'},
253 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
254 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
255 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
256 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
257 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
258 {"change-start", required_argument, 0, OPTION_CHANGE_START},
259 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
260 {"debugging", no_argument, 0, OPTION_DEBUGGING},
261 {"discard-all", no_argument, 0, 'x'},
262 {"discard-locals", no_argument, 0, 'X'},
263 {"only-section", required_argument, 0, 'j'},
264 {"format", required_argument, 0, 'F'}, /* Obsolete */
265 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
266 {"help", no_argument, 0, 'h'},
267 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
268 {"input-target", required_argument, 0, 'I'},
269 {"interleave", required_argument, 0, 'i'},
270 {"keep-symbol", required_argument, 0, 'K'},
271 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
272 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
273 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
274 {"output-target", required_argument, 0, 'O'},
275 {"pad-to", required_argument, 0, OPTION_PAD_TO},
276 {"preserve-dates", no_argument, 0, 'p'},
277 {"localize-symbol", required_argument, 0, 'L'},
278 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
279 {"remove-section", required_argument, 0, 'R'},
280 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
281 {"set-start", required_argument, 0, OPTION_SET_START},
282 {"strip-all", no_argument, 0, 'S'},
283 {"strip-debug", no_argument, 0, 'g'},
284 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
285 {"strip-symbol", required_argument, 0, 'N'},
286 {"target", required_argument, 0, 'F'},
287 {"verbose", no_argument, 0, 'v'},
288 {"version", no_argument, 0, 'V'},
289 {"weaken", no_argument, 0, OPTION_WEAKEN},
290 {"weaken-symbol", required_argument, 0, 'W'},
291 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
292 {0, no_argument, 0, 0}
296 extern char *program_name;
298 /* This flag distinguishes between strip and objcopy:
299 1 means this is 'strip'; 0 means this is 'objcopy'.
300 -1 means if we should use argv[0] to decide. */
305 copy_usage (stream, exit_status)
309 fprintf (stream, _("Usage: %s <switches> in-file [out-file]\n"), program_name);
310 fprintf (stream, _(" The switches are:\n"));
311 fprintf (stream, _("\
312 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
313 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
314 -F --target <bfdname> Set both input and output format to <bfdname>\n\
315 --debugging Convert debugging information, if possible\n\
316 -p --preserve-dates Copy modified/access timestamps to the output\n\
317 -j --only-section <name> Only copy section <name> into the output\n\
318 -R --remove-section <name> Remove section <name> from the output\n\
319 -S --strip-all Remove all symbol and relocation information\n\
320 -g --strip-debug Remove all debugging symbols\n\
321 --strip-unneeded Remove all symbols not needed by relocations\n\
322 -N --strip-symbol <name> Do not copy symbol <name>\n\
323 -K --keep-symbol <name> Only copy symbol <name>\n\
324 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
325 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
326 --weaken Force all global symbols to be marked as weak\n\
327 -x --discard-all Remove all non-global symbols\n\
328 -X --discard-locals Remove any compiler-generated symbols\n\
329 -i --interleave <number> Only copy one out of every <number> bytes\n\
330 -b --byte <num> Select byte <num> in every interleaved block\n\
331 --gap-fill <val> Fill gaps between sections with <val>\n\
332 --pad-to <addr> Pad the last section up to address <addr>\n\
333 --set-start <addr> Set the start address to <addr>\n\
334 {--change-start|--adjust-start} <incr>\n\
335 Add <incr> to the start address\n\
336 {--change-addresses|--adjust-vma} <incr>\n\
337 Add <incr> to LMA, VMA and start addresses\n\
338 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
339 Change LMA and VMA of section <name> by <val>\n\
340 --change-section-lma <name>{=|+|-}<val>\n\
341 Change the LMA of section <name> by <val>\n\
342 --change-section-vma <name>{=|+|-}<val>\n\
343 Change the VMA of section <name> by <val>\n\
344 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
345 Warn if a named section does not exist\n\
346 --set-section-flags <name>=<flags>\n\
347 Set section <name>'s properties to <flags>\n\
348 --add-section <name>=<file> Add section <name> found in <file> to output\n\
349 --change-leading-char Force output format's leading character style\n\
350 --remove-leading-char Remove leading character from global symbols\n\
351 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
352 -v --verbose List all object files modified\n\
353 -V --version Display this program's version number\n\
354 -h --help Display this output\n\
356 list_supported_targets (program_name, stream);
357 if (exit_status == 0)
358 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
363 strip_usage (stream, exit_status)
367 fprintf (stream, _("Usage: %s <switches> in-file(s)\n"), program_name);
368 fprintf (stream, _(" The switches are:\n"));
369 fprintf (stream, _("\
370 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
371 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
372 -F --target <bfdname> Set both input and output format to <bfdname>\n\
373 -p --preserve-dates Copy modified/access timestamps to the output\n\
374 -R --remove-section <name> Remove section <name> from the output\n\
375 -s --strip-all Remove all symbol and relocation information\n\
376 -g -S --strip-debug Remove all debugging symbols\n\
377 --strip-unneeded Remove all symbols not needed by relocations\n\
378 -N --strip-symbol <name> Do not copy symbol <name>\n\
379 -K --keep-symbol <name> Only copy symbol <name>\n\
380 -x --discard-all Remove all non-global symbols\n\
381 -X --discard-locals Remove any compiler-generated symbols\n\
382 -v --verbose List all object files modified\n\
383 -V --version Display this program's version number\n\
384 -h --help Display this output\n\
385 -o <file> Place stripped output into <file>\n\
388 list_supported_targets (program_name, stream);
389 if (exit_status == 0)
390 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
394 /* Parse section flags into a flagword, with a fatal error if the
395 string can't be parsed. */
409 snext = strchr (s, ',');
419 #define PARSE_FLAG(fname,fval) \
420 else if (strncasecmp (fname, s, len) == 0) ret |= fval
421 PARSE_FLAG ("alloc", SEC_ALLOC);
422 PARSE_FLAG ("load", SEC_LOAD);
423 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
424 PARSE_FLAG ("readonly", SEC_READONLY);
425 PARSE_FLAG ("debug", SEC_DEBUGGING);
426 PARSE_FLAG ("code", SEC_CODE);
427 PARSE_FLAG ("data", SEC_DATA);
428 PARSE_FLAG ("rom", SEC_ROM);
429 PARSE_FLAG ("share", SEC_SHARED);
430 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
436 copy = xmalloc (len + 1);
437 strncpy (copy, s, len);
439 non_fatal (_("unrecognized section flag `%s'"), copy);
440 fatal (_("supported flags: %s"),
441 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
451 /* Find and optionally add an entry in the change_sections list. */
453 static struct section_list *
454 find_section_list (name, add)
458 register struct section_list *p;
460 for (p = change_sections; p != NULL; p = p->next)
461 if (strcmp (p->name, name) == 0)
467 p = (struct section_list *) xmalloc (sizeof (struct section_list));
472 p->change_vma = CHANGE_IGNORE;
473 p->change_lma = CHANGE_IGNORE;
476 p->set_flags = false;
479 p->next = change_sections;
485 /* Add a symbol to strip_specific_list. */
488 add_specific_symbol (name, list)
490 struct symlist **list;
492 struct symlist *tmp_list;
494 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
495 tmp_list->name = name;
496 tmp_list->next = *list;
500 /* See whether a symbol should be stripped or kept based on
501 strip_specific_list and keep_symbols. */
504 is_specified_symbol (name, list)
506 struct symlist *list;
508 struct symlist *tmp_list;
510 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
512 if (strcmp (name, tmp_list->name) == 0)
518 /* See if a section is being removed. */
521 is_strip_section (abfd, sec)
522 bfd *abfd ATTRIBUTE_UNUSED;
525 struct section_list *p;
527 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
528 && (strip_symbols == STRIP_DEBUG
529 || strip_symbols == STRIP_UNNEEDED
530 || strip_symbols == STRIP_ALL
531 || discard_locals == LOCALS_ALL
532 || convert_debugging))
535 if (! sections_removed && ! sections_copied)
538 p = find_section_list (bfd_get_section_name (abfd, sec), false);
539 if (sections_removed && p != NULL && p->remove)
541 if (sections_copied && (p == NULL || ! p->copy))
546 /* Choose which symbol entries to copy; put the result in OSYMS.
547 We don't copy in place, because that confuses the relocs.
548 Return the number of symbols to print. */
551 filter_symbols (abfd, obfd, osyms, isyms, symcount)
554 asymbol **osyms, **isyms;
557 register asymbol **from = isyms, **to = osyms;
558 long src_count = 0, dst_count = 0;
559 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
562 for (; src_count < symcount; src_count++)
564 asymbol *sym = from[src_count];
565 flagword flags = sym->flags;
566 const char *name = bfd_asymbol_name (sym);
569 if (redefine_sym_list)
571 const char *old_name, *new_name;
573 old_name = bfd_asymbol_name (sym);
574 new_name = lookup_sym_redefinition (old_name);
575 name = bfd_asymbol_name (sym) = new_name;
578 if (change_leading_char
579 && (bfd_get_symbol_leading_char (abfd)
580 != bfd_get_symbol_leading_char (obfd))
581 && (bfd_get_symbol_leading_char (abfd) == '\0'
582 || (name[0] == bfd_get_symbol_leading_char (abfd))))
584 if (bfd_get_symbol_leading_char (obfd) == '\0')
585 name = bfd_asymbol_name (sym) = name + 1;
590 n = xmalloc (strlen (name) + 2);
591 n[0] = bfd_get_symbol_leading_char (obfd);
592 if (bfd_get_symbol_leading_char (abfd) == '\0')
593 strcpy (n + 1, name);
595 strcpy (n + 1, name + 1);
596 name = bfd_asymbol_name (sym) = n;
600 if (remove_leading_char
601 && ((flags & BSF_GLOBAL) != 0
602 || (flags & BSF_WEAK) != 0
603 || bfd_is_und_section (bfd_get_section (sym))
604 || bfd_is_com_section (bfd_get_section (sym)))
605 && name[0] == bfd_get_symbol_leading_char (abfd))
606 name = bfd_asymbol_name (sym) = name + 1;
608 if (strip_symbols == STRIP_ALL)
610 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
611 || ((flags & BSF_SECTION_SYM) != 0
612 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
615 else if (relocatable /* Relocatable file. */
616 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
618 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
619 || (flags & BSF_WEAK) != 0
620 || bfd_is_und_section (bfd_get_section (sym))
621 || bfd_is_com_section (bfd_get_section (sym)))
622 keep = strip_symbols != STRIP_UNNEEDED;
623 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
624 keep = (strip_symbols != STRIP_DEBUG
625 && strip_symbols != STRIP_UNNEEDED
626 && ! convert_debugging);
627 else /* Local symbol. */
628 keep = (strip_symbols != STRIP_UNNEEDED
629 && (discard_locals != LOCALS_ALL
630 && (discard_locals != LOCALS_START_L
631 || ! bfd_is_local_label (abfd, sym))));
633 if (keep && is_specified_symbol (name, strip_specific_list))
635 if (!keep && is_specified_symbol (name, keep_specific_list))
637 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
640 if (keep && (flags & BSF_GLOBAL) != 0
641 && (weaken || is_specified_symbol (name, weaken_specific_list)))
643 sym->flags &=~ BSF_GLOBAL;
644 sym->flags |= BSF_WEAK;
646 if (keep && (flags & (BSF_GLOBAL | BSF_WEAK))
647 && is_specified_symbol (name, localize_specific_list))
649 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
650 sym->flags |= BSF_LOCAL;
654 to[dst_count++] = sym;
657 to[dst_count] = NULL;
663 lookup_sym_redefinition (source)
667 struct redefine_node *list;
671 for (list = redefine_sym_list; list != NULL; list = list->next)
673 if (strcmp (source, list->source) == 0)
675 result = list->target;
682 /* Add a node to a symbol redefine list */
685 redefine_list_append (source, target)
689 struct redefine_node **p;
690 struct redefine_node *list;
691 struct redefine_node *new_node;
693 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
695 if (strcmp (source, list->source) == 0)
697 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
702 if (strcmp (target, list->target) == 0)
704 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
710 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
712 new_node->source = strdup (source);
713 new_node->target = strdup (target);
714 new_node->next = NULL;
720 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
724 filter_bytes (memhunk, size)
728 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
730 for (; from < end; from += interleave)
732 if (*size % interleave > (bfd_size_type) copy_byte)
733 *size = (*size / interleave) + 1;
738 /* Copy object file IBFD onto OBFD. */
741 copy_object (ibfd, obfd)
747 asection **osections = NULL;
748 bfd_size_type *gaps = NULL;
749 bfd_size_type max_gap = 0;
754 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
755 RETURN_NONFATAL (bfd_get_filename (obfd));
758 printf (_("copy from %s(%s) to %s(%s)\n"),
759 bfd_get_filename (ibfd), bfd_get_target (ibfd),
760 bfd_get_filename (obfd), bfd_get_target (obfd));
765 start = bfd_get_start_address (ibfd);
766 start += change_start;
768 if (!bfd_set_start_address (obfd, start)
769 || !bfd_set_file_flags (obfd,
770 (bfd_get_file_flags (ibfd)
771 & bfd_applicable_file_flags (obfd))))
772 RETURN_NONFATAL (bfd_get_filename (ibfd));
774 /* Copy architecture of input file to output file */
775 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
776 bfd_get_mach (ibfd)))
777 non_fatal (_("Warning: Output file cannot represent architecture %s"),
778 bfd_printable_arch_mach (bfd_get_arch (ibfd),
779 bfd_get_mach (ibfd)));
781 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
782 RETURN_NONFATAL (bfd_get_filename (ibfd));
787 if (osympp != isympp)
790 /* BFD mandates that all output sections be created and sizes set before
791 any output is done. Thus, we traverse all sections multiple times. */
792 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
794 if (add_sections != NULL)
796 struct section_add *padd;
797 struct section_list *pset;
799 for (padd = add_sections; padd != NULL; padd = padd->next)
801 padd->section = bfd_make_section (obfd, padd->name);
802 if (padd->section == NULL)
804 non_fatal (_("can't create section `%s': %s"),
805 padd->name, bfd_errmsg (bfd_get_error ()));
813 if (! bfd_set_section_size (obfd, padd->section, padd->size))
814 RETURN_NONFATAL (bfd_get_filename (obfd));
816 pset = find_section_list (padd->name, false);
820 if (pset != NULL && pset->set_flags)
821 flags = pset->flags | SEC_HAS_CONTENTS;
823 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
825 if (! bfd_set_section_flags (obfd, padd->section, flags))
826 RETURN_NONFATAL (bfd_get_filename (obfd));
830 if (pset->change_vma != CHANGE_IGNORE)
831 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
832 RETURN_NONFATAL (bfd_get_filename (obfd));
834 if (pset->change_lma != CHANGE_IGNORE)
836 padd->section->lma = pset->lma_val;
838 if (! bfd_set_section_alignment
839 (obfd, padd->section,
840 bfd_section_alignment (obfd, padd->section)))
841 RETURN_NONFATAL (bfd_get_filename (obfd));
848 if (gap_fill_set || pad_to_set)
853 /* We must fill in gaps between the sections and/or we must pad
854 the last section to a specified address. We do this by
855 grabbing a list of the sections, sorting them by VMA, and
856 increasing the section sizes as required to fill the gaps.
857 We write out the gap contents below. */
859 c = bfd_count_sections (obfd);
860 osections = (asection **) xmalloc (c * sizeof (asection *));
862 bfd_map_over_sections (obfd, get_sections, (void *) &set);
864 qsort (osections, c, sizeof (asection *), compare_section_lma);
866 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
867 memset (gaps, 0, c * sizeof (bfd_size_type));
871 for (i = 0; i < c - 1; i++)
875 bfd_vma gap_start, gap_stop;
877 flags = bfd_get_section_flags (obfd, osections[i]);
878 if ((flags & SEC_HAS_CONTENTS) == 0
879 || (flags & SEC_LOAD) == 0)
882 size = bfd_section_size (obfd, osections[i]);
883 gap_start = bfd_section_lma (obfd, osections[i]) + size;
884 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
885 if (gap_start < gap_stop)
887 if (! bfd_set_section_size (obfd, osections[i],
888 size + (gap_stop - gap_start)))
890 non_fatal (_("Can't fill gap after %s: %s"),
891 bfd_get_section_name (obfd, osections[i]),
892 bfd_errmsg (bfd_get_error ()));
896 gaps[i] = gap_stop - gap_start;
897 if (max_gap < gap_stop - gap_start)
898 max_gap = gap_stop - gap_start;
908 lma = bfd_section_lma (obfd, osections[c - 1]);
909 size = bfd_section_size (obfd, osections[c - 1]);
910 if (lma + size < pad_to)
912 if (! bfd_set_section_size (obfd, osections[c - 1],
915 non_fatal (_("Can't add padding to %s: %s"),
916 bfd_get_section_name (obfd, osections[c - 1]),
917 bfd_errmsg (bfd_get_error ()));
922 gaps[c - 1] = pad_to - (lma + size);
923 if (max_gap < pad_to - (lma + size))
924 max_gap = pad_to - (lma + size);
930 /* Symbol filtering must happen after the output sections have
931 been created, but before their contents are set. */
933 symsize = bfd_get_symtab_upper_bound (ibfd);
935 RETURN_NONFATAL (bfd_get_filename (ibfd));
937 osympp = isympp = (asymbol **) xmalloc (symsize);
938 symcount = bfd_canonicalize_symtab (ibfd, isympp);
940 RETURN_NONFATAL (bfd_get_filename (ibfd));
942 if (convert_debugging)
943 dhandle = read_debugging_info (ibfd, isympp, symcount);
945 if (strip_symbols == STRIP_DEBUG
946 || strip_symbols == STRIP_ALL
947 || strip_symbols == STRIP_UNNEEDED
948 || discard_locals != LOCALS_UNDEF
949 || strip_specific_list != NULL
950 || keep_specific_list != NULL
951 || localize_specific_list != NULL
952 || weaken_specific_list != NULL
956 || change_leading_char
957 || remove_leading_char
961 /* Mark symbols used in output relocations so that they
962 are kept, even if they are local labels or static symbols.
964 Note we iterate over the input sections examining their
965 relocations since the relocations for the output sections
966 haven't been set yet. mark_symbols_used_in_relocations will
967 ignore input sections which have no corresponding output
969 if (strip_symbols != STRIP_ALL)
970 bfd_map_over_sections (ibfd,
971 mark_symbols_used_in_relocations,
973 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
974 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
977 if (convert_debugging && dhandle != NULL)
979 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
986 bfd_set_symtab (obfd, osympp, symcount);
988 /* This has to happen after the symbol table has been set. */
989 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
991 if (add_sections != NULL)
993 struct section_add *padd;
995 for (padd = add_sections; padd != NULL; padd = padd->next)
997 if (! bfd_set_section_contents (obfd, padd->section,
998 (PTR) padd->contents,
1000 (bfd_size_type) padd->size))
1001 RETURN_NONFATAL (bfd_get_filename (obfd));
1005 if (gap_fill_set || pad_to_set)
1010 /* Fill in the gaps. */
1014 buf = (bfd_byte *) xmalloc (max_gap);
1015 memset (buf, gap_fill, (size_t) max_gap);
1017 c = bfd_count_sections (obfd);
1018 for (i = 0; i < c; i++)
1026 off = bfd_section_size (obfd, osections[i]) - left;
1036 if (! bfd_set_section_contents (obfd, osections[i], buf,
1038 RETURN_NONFATAL (bfd_get_filename (obfd));
1047 /* Allow the BFD backend to copy any private data it understands
1048 from the input BFD to the output BFD. This is done last to
1049 permit the routine to look at the filtered symbol table, which is
1050 important for the ECOFF code at least. */
1051 if (!bfd_copy_private_bfd_data (ibfd, obfd))
1053 non_fatal (_("%s: error copying private BFD data: %s"),
1054 bfd_get_filename (obfd),
1055 bfd_errmsg (bfd_get_error ()));
1061 /* Read each archive element in turn from IBFD, copy the
1062 contents to temp file, and keep the temp file handle. */
1065 copy_archive (ibfd, obfd, output_target)
1068 const char *output_target;
1072 struct name_list *next;
1076 bfd **ptr = &obfd->archive_head;
1078 char *dir = make_tempname (bfd_get_filename (obfd));
1080 /* Make a temp directory to hold the contents. */
1081 #if defined (_WIN32) && !defined (__CYGWIN32__)
1082 if (mkdir (dir) != 0)
1084 if (mkdir (dir, 0700) != 0)
1087 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1088 dir, strerror (errno));
1090 obfd->has_armap = ibfd->has_armap;
1094 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1095 while (!status && this_element != (bfd *) NULL)
1097 /* Create an output file for this member. */
1098 char *output_name = concat (dir, "/", bfd_get_filename (this_element),
1100 bfd *output_bfd = bfd_openw (output_name, output_target);
1103 int stat_status = 0;
1107 stat_status = bfd_stat_arch_elt (this_element, &buf);
1108 if (stat_status != 0)
1109 non_fatal (_("internal stat error on %s"),
1110 bfd_get_filename (this_element));
1113 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1114 l->name = output_name;
1118 if (output_bfd == (bfd *) NULL)
1119 RETURN_NONFATAL (output_name);
1121 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1122 RETURN_NONFATAL (bfd_get_filename (obfd));
1124 if (bfd_check_format (this_element, bfd_object) == true)
1125 copy_object (this_element, output_bfd);
1127 if (!bfd_close (output_bfd))
1129 bfd_nonfatal (bfd_get_filename (output_bfd));
1130 /* Error in new object file. Don't change archive. */
1134 if (preserve_dates && stat_status == 0)
1135 set_times (output_name, &buf);
1137 /* Open the newly output file and attach to our list. */
1138 output_bfd = bfd_openr (output_name, output_target);
1140 l->obfd = output_bfd;
1143 ptr = &output_bfd->next;
1145 last_element = this_element;
1147 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1149 bfd_close (last_element);
1151 *ptr = (bfd *) NULL;
1153 if (!bfd_close (obfd))
1154 RETURN_NONFATAL (bfd_get_filename (obfd));
1156 if (!bfd_close (ibfd))
1157 RETURN_NONFATAL (bfd_get_filename (ibfd));
1159 /* Delete all the files that we opened. */
1160 for (l = list; l != NULL; l = l->next)
1162 bfd_close (l->obfd);
1168 /* The top-level control. */
1171 copy_file (input_filename, output_filename, input_target, output_target)
1172 const char *input_filename;
1173 const char *output_filename;
1174 const char *input_target;
1175 const char *output_target;
1180 /* To allow us to do "strip *" without dying on the first
1181 non-object file, failures are nonfatal. */
1183 ibfd = bfd_openr (input_filename, input_target);
1185 RETURN_NONFATAL (input_filename);
1187 if (bfd_check_format (ibfd, bfd_archive))
1191 /* bfd_get_target does not return the correct value until
1192 bfd_check_format succeeds. */
1193 if (output_target == NULL)
1194 output_target = bfd_get_target (ibfd);
1196 obfd = bfd_openw (output_filename, output_target);
1198 RETURN_NONFATAL (output_filename);
1200 copy_archive (ibfd, obfd, output_target);
1202 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1206 /* bfd_get_target does not return the correct value until
1207 bfd_check_format succeeds. */
1208 if (output_target == NULL)
1209 output_target = bfd_get_target (ibfd);
1211 obfd = bfd_openw (output_filename, output_target);
1213 RETURN_NONFATAL (output_filename);
1215 copy_object (ibfd, obfd);
1217 if (!bfd_close (obfd))
1218 RETURN_NONFATAL (output_filename);
1220 if (!bfd_close (ibfd))
1221 RETURN_NONFATAL (input_filename);
1225 bfd_nonfatal (input_filename);
1227 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1229 list_matching_formats (matching);
1237 /* Create a section in OBFD with the same name and attributes
1238 as ISECTION in IBFD. */
1241 setup_section (ibfd, isection, obfdarg)
1246 bfd *obfd = (bfd *) obfdarg;
1247 struct section_list *p;
1255 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1256 && (strip_symbols == STRIP_DEBUG
1257 || strip_symbols == STRIP_UNNEEDED
1258 || strip_symbols == STRIP_ALL
1259 || discard_locals == LOCALS_ALL
1260 || convert_debugging))
1263 p = find_section_list (bfd_section_name (ibfd, isection), false);
1267 if (sections_removed && p != NULL && p->remove)
1269 if (sections_copied && (p == NULL || ! p->copy))
1272 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1274 if (osection == NULL)
1280 size = bfd_section_size (ibfd, isection);
1282 size = (size + interleave - 1) / interleave;
1283 if (! bfd_set_section_size (obfd, osection, size))
1289 vma = bfd_section_vma (ibfd, isection);
1290 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1292 else if (p != NULL && p->change_vma == CHANGE_SET)
1295 vma += change_section_address;
1297 if (! bfd_set_section_vma (obfd, osection, vma))
1303 lma = isection->lma;
1304 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1306 if (p->change_lma == CHANGE_MODIFY)
1308 else if (p->change_lma == CHANGE_SET)
1314 lma += change_section_address;
1316 osection->lma = lma;
1318 /* FIXME: This is probably not enough. If we change the LMA we
1319 may have to recompute the header for the file as well. */
1320 if (bfd_set_section_alignment (obfd,
1322 bfd_section_alignment (ibfd, isection))
1325 err = _("alignment");
1329 flags = bfd_get_section_flags (ibfd, isection);
1330 if (p != NULL && p->set_flags)
1331 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1332 if (!bfd_set_section_flags (obfd, osection, flags))
1338 /* This used to be mangle_section; we do here to avoid using
1339 bfd_get_section_by_name since some formats allow multiple
1340 sections with the same name. */
1341 isection->output_section = osection;
1342 isection->output_offset = 0;
1344 /* Allow the BFD backend to copy any private data it understands
1345 from the input section to the output section. */
1346 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1348 err = _("private data");
1356 non_fatal (_("%s: section `%s': error in %s: %s"),
1357 bfd_get_filename (ibfd),
1358 bfd_section_name (ibfd, isection),
1359 err, bfd_errmsg (bfd_get_error ()));
1363 /* Copy the data of input section ISECTION of IBFD
1364 to an output section with the same name in OBFD.
1365 If stripping then don't copy any relocation info. */
1368 copy_section (ibfd, isection, obfdarg)
1373 bfd *obfd = (bfd *) obfdarg;
1374 struct section_list *p;
1381 /* If we have already failed earlier on, do not keep on generating
1386 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1387 && (strip_symbols == STRIP_DEBUG
1388 || strip_symbols == STRIP_UNNEEDED
1389 || strip_symbols == STRIP_ALL
1390 || discard_locals == LOCALS_ALL
1391 || convert_debugging))
1396 p = find_section_list (bfd_section_name (ibfd, isection), false);
1398 if (sections_removed && p != NULL && p->remove)
1400 if (sections_copied && (p == NULL || ! p->copy))
1403 osection = isection->output_section;
1404 size = bfd_get_section_size_before_reloc (isection);
1406 if (size == 0 || osection == 0)
1410 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1412 RETURN_NONFATAL (bfd_get_filename (ibfd));
1415 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1418 relpp = (arelent **) xmalloc (relsize);
1419 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1421 RETURN_NONFATAL (bfd_get_filename (ibfd));
1423 if (strip_symbols == STRIP_ALL)
1425 /* Remove relocations which are not in
1426 keep_strip_specific_list. */
1427 arelent **temp_relpp;
1428 long temp_relcount = 0;
1431 temp_relpp = (arelent **) xmalloc (relsize);
1432 for (i = 0; i < relcount; i++)
1433 if (is_specified_symbol
1434 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1435 keep_specific_list))
1436 temp_relpp [temp_relcount++] = relpp [i];
1437 relcount = temp_relcount;
1441 bfd_set_reloc (obfd, osection,
1442 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1445 isection->_cooked_size = isection->_raw_size;
1446 isection->reloc_done = true;
1448 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1450 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1452 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1454 RETURN_NONFATAL (bfd_get_filename (ibfd));
1457 filter_bytes (memhunk, &size);
1459 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1461 RETURN_NONFATAL (bfd_get_filename (obfd));
1465 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1467 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1469 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1470 flag--they can just remove the section entirely and add it
1471 back again. However, we do permit them to turn on the
1472 SEC_HAS_CONTENTS flag, and take it to mean that the section
1473 contents should be zeroed out. */
1475 memset (memhunk, 0, size);
1476 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1478 RETURN_NONFATAL (bfd_get_filename (obfd));
1483 /* Get all the sections. This is used when --gap-fill or --pad-to is
1487 get_sections (obfd, osection, secppparg)
1488 bfd *obfd ATTRIBUTE_UNUSED;
1492 asection ***secppp = (asection ***) secppparg;
1494 **secppp = osection;
1498 /* Sort sections by VMA. This is called via qsort, and is used when
1499 --gap-fill or --pad-to is used. We force non loadable or empty
1500 sections to the front, where they are easier to ignore. */
1503 compare_section_lma (arg1, arg2)
1507 const asection **sec1 = (const asection **) arg1;
1508 const asection **sec2 = (const asection **) arg2;
1509 flagword flags1, flags2;
1511 /* Sort non loadable sections to the front. */
1512 flags1 = (*sec1)->flags;
1513 flags2 = (*sec2)->flags;
1514 if ((flags1 & SEC_HAS_CONTENTS) == 0
1515 || (flags1 & SEC_LOAD) == 0)
1517 if ((flags2 & SEC_HAS_CONTENTS) != 0
1518 && (flags2 & SEC_LOAD) != 0)
1523 if ((flags2 & SEC_HAS_CONTENTS) == 0
1524 || (flags2 & SEC_LOAD) == 0)
1528 /* Sort sections by LMA. */
1529 if ((*sec1)->lma > (*sec2)->lma)
1531 else if ((*sec1)->lma < (*sec2)->lma)
1534 /* Sort sections with the same LMA by size. */
1535 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1537 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1543 /* Mark all the symbols which will be used in output relocations with
1544 the BSF_KEEP flag so that those symbols will not be stripped.
1546 Ignore relocations which will not appear in the output file. */
1549 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1554 asymbol **symbols = (asymbol **) symbolsarg;
1559 /* Ignore an input section with no corresponding output section. */
1560 if (isection->output_section == NULL)
1563 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1565 bfd_fatal (bfd_get_filename (ibfd));
1570 relpp = (arelent **) xmalloc (relsize);
1571 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1573 bfd_fatal (bfd_get_filename (ibfd));
1575 /* Examine each symbol used in a relocation. If it's not one of the
1576 special bfd section symbols, then mark it with BSF_KEEP. */
1577 for (i = 0; i < relcount; i++)
1579 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1580 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1581 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1582 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1589 /* Write out debugging information. */
1592 write_debugging_info (obfd, dhandle, symcountp, symppp)
1595 long *symcountp ATTRIBUTE_UNUSED;
1596 asymbol ***symppp ATTRIBUTE_UNUSED;
1598 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1599 return write_ieee_debugging_info (obfd, dhandle);
1601 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1602 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1604 bfd_byte *syms, *strings;
1605 bfd_size_type symsize, stringsize;
1606 asection *stabsec, *stabstrsec;
1608 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1613 stabsec = bfd_make_section (obfd, ".stab");
1614 stabstrsec = bfd_make_section (obfd, ".stabstr");
1616 || stabstrsec == NULL
1617 || ! bfd_set_section_size (obfd, stabsec, symsize)
1618 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1619 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1620 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1621 || ! bfd_set_section_flags (obfd, stabsec,
1625 || ! bfd_set_section_flags (obfd, stabstrsec,
1630 non_fatal (_("%s: can't create debugging section: %s"),
1631 bfd_get_filename (obfd),
1632 bfd_errmsg (bfd_get_error ()));
1636 /* We can get away with setting the section contents now because
1637 the next thing the caller is going to do is copy over the
1638 real sections. We may someday have to split the contents
1639 setting out of this function. */
1640 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1642 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1643 (file_ptr) 0, stringsize))
1645 non_fatal (_("%s: can't set debugging section contents: %s"),
1646 bfd_get_filename (obfd),
1647 bfd_errmsg (bfd_get_error ()));
1654 non_fatal (_("%s: don't know how to write debugging information for %s"),
1655 bfd_get_filename (obfd), bfd_get_target (obfd));
1660 strip_main (argc, argv)
1664 char *input_target = NULL, *output_target = NULL;
1665 boolean show_version = false;
1667 struct section_list *p;
1668 char *output_file = NULL;
1670 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv",
1671 strip_options, (int *) 0)) != EOF)
1676 input_target = optarg;
1679 output_target = optarg;
1682 input_target = output_target = optarg;
1685 p = find_section_list (optarg, true);
1687 sections_removed = true;
1690 strip_symbols = STRIP_ALL;
1694 strip_symbols = STRIP_DEBUG;
1696 case OPTION_STRIP_UNNEEDED:
1697 strip_symbols = STRIP_UNNEEDED;
1700 add_specific_symbol (optarg, &keep_specific_list);
1703 add_specific_symbol (optarg, &strip_specific_list);
1706 output_file = optarg;
1709 preserve_dates = true;
1712 discard_locals = LOCALS_ALL;
1715 discard_locals = LOCALS_START_L;
1721 show_version = true;
1724 break; /* we've been given a long option */
1726 strip_usage (stdout, 0);
1728 strip_usage (stderr, 1);
1733 print_version ("strip");
1735 /* Default is to strip all symbols. */
1736 if (strip_symbols == STRIP_UNDEF
1737 && discard_locals == LOCALS_UNDEF
1738 && strip_specific_list == NULL)
1739 strip_symbols = STRIP_ALL;
1741 if (output_target == (char *) NULL)
1742 output_target = input_target;
1746 || (output_file != NULL && (i + 1) < argc))
1747 strip_usage (stderr, 1);
1749 for (; i < argc; i++)
1751 int hold_status = status;
1752 struct stat statbuf;
1757 if (stat (argv[i], &statbuf) < 0)
1759 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
1764 if (output_file != NULL)
1765 tmpname = output_file;
1767 tmpname = make_tempname (argv[i]);
1770 copy_file (argv[i], tmpname, input_target, output_target);
1774 set_times (tmpname, &statbuf);
1775 if (output_file == NULL)
1776 smart_rename (tmpname, argv[i], preserve_dates);
1777 status = hold_status;
1781 if (output_file == NULL)
1789 copy_main (argc, argv)
1793 char *input_filename = NULL, *output_filename = NULL;
1794 char *input_target = NULL, *output_target = NULL;
1795 boolean show_version = false;
1796 boolean change_warn = true;
1798 struct section_list *p;
1799 struct stat statbuf;
1801 while ((c = getopt_long (argc, argv, "b:i:I:j:K:N:s:O:d:F:L:R:SpgxXVvW:",
1802 copy_options, (int *) 0)) != EOF)
1807 copy_byte = atoi (optarg);
1809 fatal (_("byte number must be non-negative"));
1813 interleave = atoi (optarg);
1815 fatal (_("interleave must be positive"));
1819 case 's': /* "source" - 'I' is preferred */
1820 input_target = optarg;
1824 case 'd': /* "destination" - 'O' is preferred */
1825 output_target = optarg;
1829 input_target = output_target = optarg;
1833 p = find_section_list (optarg, true);
1835 fatal (_("%s both copied and removed"), optarg);
1837 sections_copied = true;
1841 p = find_section_list (optarg, true);
1843 fatal (_("%s both copied and removed"), optarg);
1845 sections_removed = true;
1849 strip_symbols = STRIP_ALL;
1853 strip_symbols = STRIP_DEBUG;
1856 case OPTION_STRIP_UNNEEDED:
1857 strip_symbols = STRIP_UNNEEDED;
1861 add_specific_symbol (optarg, &keep_specific_list);
1865 add_specific_symbol (optarg, &strip_specific_list);
1869 add_specific_symbol (optarg, &localize_specific_list);
1873 add_specific_symbol (optarg, &weaken_specific_list);
1877 preserve_dates = true;
1881 discard_locals = LOCALS_ALL;
1885 discard_locals = LOCALS_START_L;
1893 show_version = true;
1900 case OPTION_ADD_SECTION:
1904 struct section_add *pa;
1909 s = strchr (optarg, '=');
1912 fatal (_("bad format for %s"), "--add-section");
1914 if (stat (s + 1, & st) < 0)
1915 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
1917 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1920 name = (char *) xmalloc (len + 1);
1921 strncpy (name, optarg, len);
1925 pa->filename = s + 1;
1927 pa->size = st.st_size;
1929 pa->contents = (bfd_byte *) xmalloc (pa->size);
1930 f = fopen (pa->filename, FOPEN_RB);
1933 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
1935 if (fread (pa->contents, 1, pa->size, f) == 0
1937 fatal (_("%s: fread failed"), pa->filename);
1941 pa->next = add_sections;
1946 case OPTION_CHANGE_START:
1947 change_start = parse_vma (optarg, "--change-start");
1950 case OPTION_CHANGE_SECTION_ADDRESS:
1951 case OPTION_CHANGE_SECTION_LMA:
1952 case OPTION_CHANGE_SECTION_VMA:
1957 char *option = NULL;
1959 enum change_action what = CHANGE_IGNORE;
1963 case OPTION_CHANGE_SECTION_ADDRESS:
1964 option = "--change-section-address";
1966 case OPTION_CHANGE_SECTION_LMA:
1967 option = "--change-section-lma";
1969 case OPTION_CHANGE_SECTION_VMA:
1970 option = "--change-section-vma";
1974 s = strchr (optarg, '=');
1977 s = strchr (optarg, '+');
1980 s = strchr (optarg, '-');
1982 fatal (_("bad format for %s"), option);
1987 name = (char *) xmalloc (len + 1);
1988 strncpy (name, optarg, len);
1991 p = find_section_list (name, true);
1993 val = parse_vma (s + 1, option);
1997 case '=': what = CHANGE_SET; break;
1998 case '-': val = - val; /* Drop through. */
1999 case '+': what = CHANGE_MODIFY; break;
2004 case OPTION_CHANGE_SECTION_ADDRESS:
2005 p->change_vma = what;
2009 case OPTION_CHANGE_SECTION_LMA:
2010 p->change_lma = what;
2014 case OPTION_CHANGE_SECTION_VMA:
2015 p->change_vma = what;
2022 case OPTION_CHANGE_ADDRESSES:
2023 change_section_address = parse_vma (optarg, "--change-addresses");
2024 change_start = change_section_address;
2027 case OPTION_CHANGE_WARNINGS:
2031 case OPTION_CHANGE_LEADING_CHAR:
2032 change_leading_char = true;
2035 case OPTION_DEBUGGING:
2036 convert_debugging = true;
2039 case OPTION_GAP_FILL:
2041 bfd_vma gap_fill_vma;
2043 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2044 gap_fill = (bfd_byte) gap_fill_vma;
2045 if ((bfd_vma) gap_fill != gap_fill_vma)
2049 sprintf_vma (buff, gap_fill_vma);
2051 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2054 gap_fill_set = true;
2058 case OPTION_NO_CHANGE_WARNINGS:
2059 change_warn = false;
2063 pad_to = parse_vma (optarg, "--pad-to");
2067 case OPTION_REMOVE_LEADING_CHAR:
2068 remove_leading_char = true;
2071 case OPTION_REDEFINE_SYM:
2073 /* Push this redefinition onto redefine_symbol_list. */
2077 const char *nextarg;
2078 char *source, *target;
2080 s = strchr (optarg, '=');
2083 fatal (_("bad format for %s"), "--redefine-sym");
2087 source = (char *) xmalloc (len + 1);
2088 strncpy (source, optarg, len);
2092 len = strlen (nextarg);
2093 target = (char *) xmalloc (len + 1);
2094 strcpy (target, nextarg);
2096 redefine_list_append (source, target);
2103 case OPTION_SET_SECTION_FLAGS:
2109 s = strchr (optarg, '=');
2111 fatal (_("bad format for %s"), "--set-section-flags");
2114 name = (char *) xmalloc (len + 1);
2115 strncpy (name, optarg, len);
2118 p = find_section_list (name, true);
2120 p->set_flags = true;
2121 p->flags = parse_flags (s + 1);
2125 case OPTION_SET_START:
2126 set_start = parse_vma (optarg, "--set-start");
2127 set_start_set = true;
2131 break; /* we've been given a long option */
2134 copy_usage (stdout, 0);
2137 copy_usage (stderr, 1);
2142 print_version ("objcopy");
2144 if (copy_byte >= interleave)
2145 fatal (_("byte number must be less than interleave"));
2147 if (optind == argc || optind + 2 < argc)
2148 copy_usage (stderr, 1);
2150 input_filename = argv[optind];
2151 if (optind + 1 < argc)
2152 output_filename = argv[optind + 1];
2154 /* Default is to strip no symbols. */
2155 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2156 strip_symbols = STRIP_NONE;
2158 if (output_target == (char *) NULL)
2159 output_target = input_target;
2163 if (stat (input_filename, &statbuf) < 0)
2164 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2167 /* If there is no destination file then create a temp and rename
2168 the result into the input. */
2170 if (output_filename == (char *) NULL)
2172 char *tmpname = make_tempname (input_filename);
2174 copy_file (input_filename, tmpname, input_target, output_target);
2178 set_times (tmpname, &statbuf);
2179 smart_rename (tmpname, input_filename, preserve_dates);
2186 copy_file (input_filename, output_filename, input_target, output_target);
2187 if (status == 0 && preserve_dates)
2188 set_times (output_filename, &statbuf);
2193 for (p = change_sections; p != NULL; p = p->next)
2197 if (p->change_vma != CHANGE_IGNORE)
2201 sprintf_vma (buff, p->vma_val);
2203 /* xgettext:c-format */
2204 non_fatal (_("%s %s%c0x%s never used"),
2205 "--change-section-vma",
2207 p->change_vma == CHANGE_SET ? '=' : '+',
2211 if (p->change_lma != CHANGE_IGNORE)
2215 sprintf_vma (buff, p->lma_val);
2217 /* xgettext:c-format */
2218 non_fatal (_("%s %s%c0x%s never used"),
2219 "--change-section-lma",
2221 p->change_lma == CHANGE_SET ? '=' : '+',
2236 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2237 setlocale (LC_MESSAGES, "");
2239 bindtextdomain (PACKAGE, LOCALEDIR);
2240 textdomain (PACKAGE);
2242 program_name = argv[0];
2243 xmalloc_set_program_name (program_name);
2245 START_PROGRESS (program_name, 0);
2247 strip_symbols = STRIP_UNDEF;
2248 discard_locals = LOCALS_UNDEF;
2251 set_default_bfd_target ();
2255 int i = strlen (program_name);
2256 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
2260 strip_main (argc, argv);
2262 copy_main (argc, argv);
2264 END_PROGRESS (program_name);