1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001
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"
28 #include "filenames.h"
31 /* A list of symbols to explicitly strip out, or to keep. A linked
32 list is good enough for a small number from the command line, but
33 this will slow things down a lot if many symbols are being
42 /* A list to support redefine_sym. */
47 struct redefine_node *next;
50 static void copy_usage PARAMS ((FILE *, int));
51 static void strip_usage PARAMS ((FILE *, int));
52 static flagword parse_flags PARAMS ((const char *));
53 static struct section_list *find_section_list PARAMS ((const char *, boolean));
54 static void setup_section PARAMS ((bfd *, asection *, PTR));
55 static void copy_section PARAMS ((bfd *, asection *, PTR));
56 static void get_sections PARAMS ((bfd *, asection *, PTR));
57 static int compare_section_lma PARAMS ((const PTR, const PTR));
58 static void add_specific_symbol PARAMS ((const char *, struct symlist **));
59 static boolean is_specified_symbol PARAMS ((const char *, struct symlist *));
60 static boolean is_strip_section PARAMS ((bfd *, asection *));
61 static unsigned int filter_symbols
62 PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long));
63 static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
64 static void filter_bytes PARAMS ((char *, bfd_size_type *));
65 static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***));
66 static void copy_object PARAMS ((bfd *, bfd *));
67 static void copy_archive PARAMS ((bfd *, bfd *, const char *));
69 PARAMS ((const char *, const char *, const char *, const char *));
70 static int strip_main PARAMS ((int, char **));
71 static int copy_main PARAMS ((int, char **));
72 static const char *lookup_sym_redefinition PARAMS((const char *));
73 static void redefine_list_append PARAMS ((const char *, const char *));
75 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
77 static asymbol **isympp = NULL; /* Input symbols */
78 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
80 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
81 static int copy_byte = -1;
82 static int interleave = 4;
84 static boolean verbose; /* Print file and target names. */
85 static boolean preserve_dates; /* Preserve input file timestamp. */
86 static int status = 0; /* Exit status. */
91 STRIP_NONE, /* don't strip */
92 STRIP_DEBUG, /* strip all debugger symbols */
93 STRIP_UNNEEDED, /* strip unnecessary symbols */
94 STRIP_ALL /* strip all symbols */
97 /* Which symbols to remove. */
98 static enum strip_action strip_symbols;
103 LOCALS_START_L, /* discard locals starting with L */
104 LOCALS_ALL /* discard all locals */
107 /* Which local symbols to remove. Overrides STRIP_ALL. */
108 static enum locals_action discard_locals;
110 /* What kind of change to perform. */
118 /* Structure used to hold lists of sections and actions to take. */
121 struct section_list * next; /* Next section to change. */
122 const char * name; /* Section name. */
123 boolean used; /* Whether this entry was used. */
124 boolean remove; /* Whether to remove this section. */
125 boolean copy; /* Whether to copy this section. */
126 enum change_action change_vma;/* Whether to change or set VMA. */
127 bfd_vma vma_val; /* Amount to change by or set to. */
128 enum change_action change_lma;/* Whether to change or set LMA. */
129 bfd_vma lma_val; /* Amount to change by or set to. */
130 boolean set_flags; /* Whether to set the section flags. */
131 flagword flags; /* What to set the section flags to. */
134 static struct section_list *change_sections;
135 static boolean sections_removed;
136 static boolean sections_copied;
138 /* Changes to the start address. */
139 static bfd_vma change_start = 0;
140 static boolean set_start_set = false;
141 static bfd_vma set_start;
143 /* Changes to section addresses. */
144 static bfd_vma change_section_address = 0;
146 /* Filling gaps between sections. */
147 static boolean gap_fill_set = false;
148 static bfd_byte gap_fill = 0;
150 /* Pad to a given address. */
151 static boolean pad_to_set = false;
152 static bfd_vma pad_to;
154 /* List of sections to add. */
158 /* Next section to add. */
159 struct section_add *next;
160 /* Name of section to add. */
162 /* Name of file holding section contents. */
163 const char *filename;
166 /* Contents of file. */
168 /* BFD section, after it has been added. */
172 static struct section_add *add_sections;
174 /* Whether to convert debugging information. */
176 static boolean convert_debugging = false;
178 /* Whether to change the leading character in symbol names. */
180 static boolean change_leading_char = false;
182 /* Whether to remove the leading character from global symbol names. */
184 static boolean remove_leading_char = false;
186 /* List of symbols to strip, keep, localize, weaken, or redefine. */
188 static struct symlist *strip_specific_list = NULL;
189 static struct symlist *keep_specific_list = NULL;
190 static struct symlist *localize_specific_list = NULL;
191 static struct symlist *weaken_specific_list = NULL;
192 static struct redefine_node *redefine_sym_list = NULL;
194 /* If this is true, we weaken global symbols (set BSF_WEAK). */
196 static boolean weaken = false;
198 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
200 #define OPTION_ADD_SECTION 150
201 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
202 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
203 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
204 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
205 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
206 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
207 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
208 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
209 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
210 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
211 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
212 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
213 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
214 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
215 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
216 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
217 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
218 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYM + 1)
219 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
221 /* Options to handle if running as "strip". */
223 static struct option strip_options[] =
225 {"discard-all", no_argument, 0, 'x'},
226 {"discard-locals", no_argument, 0, 'X'},
227 {"format", required_argument, 0, 'F'}, /* Obsolete */
228 {"help", no_argument, 0, 'h'},
229 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
230 {"input-target", required_argument, 0, 'I'},
231 {"keep-symbol", required_argument, 0, 'K'},
232 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
233 {"output-target", required_argument, 0, 'O'},
234 {"preserve-dates", no_argument, 0, 'p'},
235 {"remove-section", required_argument, 0, 'R'},
236 {"strip-all", no_argument, 0, 's'},
237 {"strip-debug", no_argument, 0, 'S'},
238 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
239 {"strip-symbol", required_argument, 0, 'N'},
240 {"target", required_argument, 0, 'F'},
241 {"verbose", no_argument, 0, 'v'},
242 {"version", no_argument, 0, 'V'},
243 {0, no_argument, 0, 0}
246 /* Options to handle if running as "objcopy". */
248 static struct option copy_options[] =
250 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
251 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
252 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
253 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
254 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
255 {"binary-architecture", required_argument, 0, 'B'},
256 {"byte", required_argument, 0, 'b'},
257 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
258 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
259 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
260 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
261 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
262 {"change-start", required_argument, 0, OPTION_CHANGE_START},
263 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
264 {"debugging", no_argument, 0, OPTION_DEBUGGING},
265 {"discard-all", no_argument, 0, 'x'},
266 {"discard-locals", no_argument, 0, 'X'},
267 {"only-section", required_argument, 0, 'j'},
268 {"format", required_argument, 0, 'F'}, /* Obsolete */
269 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
270 {"help", no_argument, 0, 'h'},
271 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
272 {"input-target", required_argument, 0, 'I'},
273 {"interleave", required_argument, 0, 'i'},
274 {"keep-symbol", required_argument, 0, 'K'},
275 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
276 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
277 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
278 {"output-target", required_argument, 0, 'O'},
279 {"pad-to", required_argument, 0, OPTION_PAD_TO},
280 {"preserve-dates", no_argument, 0, 'p'},
281 {"localize-symbol", required_argument, 0, 'L'},
282 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
283 {"remove-section", required_argument, 0, 'R'},
284 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
285 {"set-start", required_argument, 0, OPTION_SET_START},
286 {"strip-all", no_argument, 0, 'S'},
287 {"strip-debug", no_argument, 0, 'g'},
288 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
289 {"strip-symbol", required_argument, 0, 'N'},
290 {"target", required_argument, 0, 'F'},
291 {"verbose", no_argument, 0, 'v'},
292 {"version", no_argument, 0, 'V'},
293 {"weaken", no_argument, 0, OPTION_WEAKEN},
294 {"weaken-symbol", required_argument, 0, 'W'},
295 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
296 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
297 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
298 {0, no_argument, 0, 0}
302 extern char *program_name;
304 /* This flag distinguishes between strip and objcopy:
305 1 means this is 'strip'; 0 means this is 'objcopy'.
306 -1 means if we should use argv[0] to decide. */
309 /* The maximum length of an S record. This variable is declared in srec.c
310 and can be modified by the --srec-len parameter. */
311 extern unsigned int Chunk;
313 /* Restrict the generation of Srecords to type S3 only.
314 This variable is declare in bfd/srec.c and can be toggled
315 on by the --srec-forceS3 command line switch. */
316 extern boolean S3Forced;
318 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
319 extern enum bfd_architecture bfd_external_binary_architecture;
322 copy_usage (stream, exit_status)
326 fprintf (stream, _("Usage: %s <switches> in-file [out-file]\n"), program_name);
327 fprintf (stream, _(" The switches are:\n"));
328 fprintf (stream, _("\
329 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
330 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
331 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
332 -F --target <bfdname> Set both input and output format to <bfdname>\n\
333 --debugging Convert debugging information, if possible\n\
334 -p --preserve-dates Copy modified/access timestamps to the output\n\
335 -j --only-section <name> Only copy section <name> into the output\n\
336 -R --remove-section <name> Remove section <name> from the output\n\
337 -S --strip-all Remove all symbol and relocation information\n\
338 -g --strip-debug Remove all debugging symbols\n\
339 --strip-unneeded Remove all symbols not needed by relocations\n\
340 -N --strip-symbol <name> Do not copy symbol <name>\n\
341 -K --keep-symbol <name> Only copy symbol <name>\n\
342 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
343 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
344 --weaken Force all global symbols to be marked as weak\n\
345 -x --discard-all Remove all non-global symbols\n\
346 -X --discard-locals Remove any compiler-generated symbols\n\
347 -i --interleave <number> Only copy one out of every <number> bytes\n\
348 -b --byte <num> Select byte <num> in every interleaved block\n\
349 --gap-fill <val> Fill gaps between sections with <val>\n\
350 --pad-to <addr> Pad the last section up to address <addr>\n\
351 --set-start <addr> Set the start address to <addr>\n\
352 {--change-start|--adjust-start} <incr>\n\
353 Add <incr> to the start address\n\
354 {--change-addresses|--adjust-vma} <incr>\n\
355 Add <incr> to LMA, VMA and start addresses\n\
356 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
357 Change LMA and VMA of section <name> by <val>\n\
358 --change-section-lma <name>{=|+|-}<val>\n\
359 Change the LMA of section <name> by <val>\n\
360 --change-section-vma <name>{=|+|-}<val>\n\
361 Change the VMA of section <name> by <val>\n\
362 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
363 Warn if a named section does not exist\n\
364 --set-section-flags <name>=<flags>\n\
365 Set section <name>'s properties to <flags>\n\
366 --add-section <name>=<file> Add section <name> found in <file> to output\n\
367 --change-leading-char Force output format's leading character style\n\
368 --remove-leading-char Remove leading character from global symbols\n\
369 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
370 --srec-len <number> Restrict the length of generated Srecords\n\
371 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
372 -v --verbose List all object files modified\n\
373 -V --version Display this program's version number\n\
374 -h --help Display this output\n\
376 list_supported_targets (program_name, stream);
377 if (exit_status == 0)
378 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
383 strip_usage (stream, exit_status)
387 fprintf (stream, _("Usage: %s <switches> in-file(s)\n"), program_name);
388 fprintf (stream, _(" The switches are:\n"));
389 fprintf (stream, _("\
390 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
391 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
392 -F --target <bfdname> Set both input and output format to <bfdname>\n\
393 -p --preserve-dates Copy modified/access timestamps to the output\n\
394 -R --remove-section <name> Remove section <name> from the output\n\
395 -s --strip-all Remove all symbol and relocation information\n\
396 -g -S --strip-debug Remove all debugging symbols\n\
397 --strip-unneeded Remove all symbols not needed by relocations\n\
398 -N --strip-symbol <name> Do not copy symbol <name>\n\
399 -K --keep-symbol <name> Only copy symbol <name>\n\
400 -x --discard-all Remove all non-global symbols\n\
401 -X --discard-locals Remove any compiler-generated symbols\n\
402 -v --verbose List all object files modified\n\
403 -V --version Display this program's version number\n\
404 -h --help Display this output\n\
405 -o <file> Place stripped output into <file>\n\
408 list_supported_targets (program_name, stream);
409 if (exit_status == 0)
410 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
414 /* Parse section flags into a flagword, with a fatal error if the
415 string can't be parsed. */
429 snext = strchr (s, ',');
439 #define PARSE_FLAG(fname,fval) \
440 else if (strncasecmp (fname, s, len) == 0) ret |= fval
441 PARSE_FLAG ("alloc", SEC_ALLOC);
442 PARSE_FLAG ("load", SEC_LOAD);
443 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
444 PARSE_FLAG ("readonly", SEC_READONLY);
445 PARSE_FLAG ("debug", SEC_DEBUGGING);
446 PARSE_FLAG ("code", SEC_CODE);
447 PARSE_FLAG ("data", SEC_DATA);
448 PARSE_FLAG ("rom", SEC_ROM);
449 PARSE_FLAG ("share", SEC_SHARED);
450 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
456 copy = xmalloc (len + 1);
457 strncpy (copy, s, len);
459 non_fatal (_("unrecognized section flag `%s'"), copy);
460 fatal (_("supported flags: %s"),
461 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
471 /* Find and optionally add an entry in the change_sections list. */
473 static struct section_list *
474 find_section_list (name, add)
478 register struct section_list *p;
480 for (p = change_sections; p != NULL; p = p->next)
481 if (strcmp (p->name, name) == 0)
487 p = (struct section_list *) xmalloc (sizeof (struct section_list));
492 p->change_vma = CHANGE_IGNORE;
493 p->change_lma = CHANGE_IGNORE;
496 p->set_flags = false;
499 p->next = change_sections;
505 /* Add a symbol to strip_specific_list. */
508 add_specific_symbol (name, list)
510 struct symlist **list;
512 struct symlist *tmp_list;
514 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
515 tmp_list->name = name;
516 tmp_list->next = *list;
520 /* See whether a symbol should be stripped or kept based on
521 strip_specific_list and keep_symbols. */
524 is_specified_symbol (name, list)
526 struct symlist *list;
528 struct symlist *tmp_list;
530 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
532 if (strcmp (name, tmp_list->name) == 0)
538 /* See if a section is being removed. */
541 is_strip_section (abfd, sec)
542 bfd *abfd ATTRIBUTE_UNUSED;
545 struct section_list *p;
547 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
548 && (strip_symbols == STRIP_DEBUG
549 || strip_symbols == STRIP_UNNEEDED
550 || strip_symbols == STRIP_ALL
551 || discard_locals == LOCALS_ALL
552 || convert_debugging))
555 if (! sections_removed && ! sections_copied)
558 p = find_section_list (bfd_get_section_name (abfd, sec), false);
559 if (sections_removed && p != NULL && p->remove)
561 if (sections_copied && (p == NULL || ! p->copy))
566 /* Choose which symbol entries to copy; put the result in OSYMS.
567 We don't copy in place, because that confuses the relocs.
568 Return the number of symbols to print. */
571 filter_symbols (abfd, obfd, osyms, isyms, symcount)
574 asymbol **osyms, **isyms;
577 register asymbol **from = isyms, **to = osyms;
578 long src_count = 0, dst_count = 0;
579 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
582 for (; src_count < symcount; src_count++)
584 asymbol *sym = from[src_count];
585 flagword flags = sym->flags;
586 const char *name = bfd_asymbol_name (sym);
589 if (redefine_sym_list)
591 const char *old_name, *new_name;
593 old_name = bfd_asymbol_name (sym);
594 new_name = lookup_sym_redefinition (old_name);
595 name = bfd_asymbol_name (sym) = new_name;
598 if (change_leading_char
599 && (bfd_get_symbol_leading_char (abfd)
600 != bfd_get_symbol_leading_char (obfd))
601 && (bfd_get_symbol_leading_char (abfd) == '\0'
602 || (name[0] == bfd_get_symbol_leading_char (abfd))))
604 if (bfd_get_symbol_leading_char (obfd) == '\0')
605 name = bfd_asymbol_name (sym) = name + 1;
610 n = xmalloc (strlen (name) + 2);
611 n[0] = bfd_get_symbol_leading_char (obfd);
612 if (bfd_get_symbol_leading_char (abfd) == '\0')
613 strcpy (n + 1, name);
615 strcpy (n + 1, name + 1);
616 name = bfd_asymbol_name (sym) = n;
620 if (remove_leading_char
621 && ((flags & BSF_GLOBAL) != 0
622 || (flags & BSF_WEAK) != 0
623 || bfd_is_und_section (bfd_get_section (sym))
624 || bfd_is_com_section (bfd_get_section (sym)))
625 && name[0] == bfd_get_symbol_leading_char (abfd))
626 name = bfd_asymbol_name (sym) = name + 1;
628 if (strip_symbols == STRIP_ALL)
630 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
631 || ((flags & BSF_SECTION_SYM) != 0
632 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
635 else if (relocatable /* Relocatable file. */
636 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
638 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
639 || (flags & BSF_WEAK) != 0
640 || bfd_is_und_section (bfd_get_section (sym))
641 || bfd_is_com_section (bfd_get_section (sym)))
642 keep = strip_symbols != STRIP_UNNEEDED;
643 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
644 keep = (strip_symbols != STRIP_DEBUG
645 && strip_symbols != STRIP_UNNEEDED
646 && ! convert_debugging);
647 else /* Local symbol. */
648 keep = (strip_symbols != STRIP_UNNEEDED
649 && (discard_locals != LOCALS_ALL
650 && (discard_locals != LOCALS_START_L
651 || ! bfd_is_local_label (abfd, sym))));
653 if (keep && is_specified_symbol (name, strip_specific_list))
655 if (!keep && is_specified_symbol (name, keep_specific_list))
657 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
660 if (keep && (flags & BSF_GLOBAL) != 0
661 && (weaken || is_specified_symbol (name, weaken_specific_list)))
663 sym->flags &=~ BSF_GLOBAL;
664 sym->flags |= BSF_WEAK;
666 if (keep && (flags & (BSF_GLOBAL | BSF_WEAK))
667 && is_specified_symbol (name, localize_specific_list))
669 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
670 sym->flags |= BSF_LOCAL;
674 to[dst_count++] = sym;
677 to[dst_count] = NULL;
683 lookup_sym_redefinition (source)
687 struct redefine_node *list;
691 for (list = redefine_sym_list; list != NULL; list = list->next)
693 if (strcmp (source, list->source) == 0)
695 result = list->target;
702 /* Add a node to a symbol redefine list */
705 redefine_list_append (source, target)
709 struct redefine_node **p;
710 struct redefine_node *list;
711 struct redefine_node *new_node;
713 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
715 if (strcmp (source, list->source) == 0)
717 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
722 if (strcmp (target, list->target) == 0)
724 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
730 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
732 new_node->source = strdup (source);
733 new_node->target = strdup (target);
734 new_node->next = NULL;
740 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
744 filter_bytes (memhunk, size)
748 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
750 for (; from < end; from += interleave)
752 if (*size % interleave > (bfd_size_type) copy_byte)
753 *size = (*size / interleave) + 1;
758 /* Copy object file IBFD onto OBFD. */
761 copy_object (ibfd, obfd)
767 asection **osections = NULL;
768 bfd_size_type *gaps = NULL;
769 bfd_size_type max_gap = 0;
773 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
774 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
775 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
777 fatal (_("Unable to change endianness of input file(s)"));
781 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
782 RETURN_NONFATAL (bfd_get_filename (obfd));
785 printf (_("copy from %s(%s) to %s(%s)\n"),
786 bfd_get_filename (ibfd), bfd_get_target (ibfd),
787 bfd_get_filename (obfd), bfd_get_target (obfd));
792 start = bfd_get_start_address (ibfd);
793 start += change_start;
795 if (!bfd_set_start_address (obfd, start)
796 || !bfd_set_file_flags (obfd,
797 (bfd_get_file_flags (ibfd)
798 & bfd_applicable_file_flags (obfd))))
799 RETURN_NONFATAL (bfd_get_filename (ibfd));
801 /* Copy architecture of input file to output file */
802 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
803 bfd_get_mach (ibfd)))
804 non_fatal (_("Warning: Output file cannot represent architecture %s"),
805 bfd_printable_arch_mach (bfd_get_arch (ibfd),
806 bfd_get_mach (ibfd)));
808 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
809 RETURN_NONFATAL (bfd_get_filename (ibfd));
814 if (osympp != isympp)
817 /* BFD mandates that all output sections be created and sizes set before
818 any output is done. Thus, we traverse all sections multiple times. */
819 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
821 if (add_sections != NULL)
823 struct section_add *padd;
824 struct section_list *pset;
826 for (padd = add_sections; padd != NULL; padd = padd->next)
828 padd->section = bfd_make_section (obfd, padd->name);
829 if (padd->section == NULL)
831 non_fatal (_("can't create section `%s': %s"),
832 padd->name, bfd_errmsg (bfd_get_error ()));
840 if (! bfd_set_section_size (obfd, padd->section, padd->size))
841 RETURN_NONFATAL (bfd_get_filename (obfd));
843 pset = find_section_list (padd->name, false);
847 if (pset != NULL && pset->set_flags)
848 flags = pset->flags | SEC_HAS_CONTENTS;
850 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
852 if (! bfd_set_section_flags (obfd, padd->section, flags))
853 RETURN_NONFATAL (bfd_get_filename (obfd));
857 if (pset->change_vma != CHANGE_IGNORE)
858 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
859 RETURN_NONFATAL (bfd_get_filename (obfd));
861 if (pset->change_lma != CHANGE_IGNORE)
863 padd->section->lma = pset->lma_val;
865 if (! bfd_set_section_alignment
866 (obfd, padd->section,
867 bfd_section_alignment (obfd, padd->section)))
868 RETURN_NONFATAL (bfd_get_filename (obfd));
875 if (gap_fill_set || pad_to_set)
880 /* We must fill in gaps between the sections and/or we must pad
881 the last section to a specified address. We do this by
882 grabbing a list of the sections, sorting them by VMA, and
883 increasing the section sizes as required to fill the gaps.
884 We write out the gap contents below. */
886 c = bfd_count_sections (obfd);
887 osections = (asection **) xmalloc (c * sizeof (asection *));
889 bfd_map_over_sections (obfd, get_sections, (void *) &set);
891 qsort (osections, c, sizeof (asection *), compare_section_lma);
893 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
894 memset (gaps, 0, c * sizeof (bfd_size_type));
898 for (i = 0; i < c - 1; i++)
902 bfd_vma gap_start, gap_stop;
904 flags = bfd_get_section_flags (obfd, osections[i]);
905 if ((flags & SEC_HAS_CONTENTS) == 0
906 || (flags & SEC_LOAD) == 0)
909 size = bfd_section_size (obfd, osections[i]);
910 gap_start = bfd_section_lma (obfd, osections[i]) + size;
911 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
912 if (gap_start < gap_stop)
914 if (! bfd_set_section_size (obfd, osections[i],
915 size + (gap_stop - gap_start)))
917 non_fatal (_("Can't fill gap after %s: %s"),
918 bfd_get_section_name (obfd, osections[i]),
919 bfd_errmsg (bfd_get_error ()));
923 gaps[i] = gap_stop - gap_start;
924 if (max_gap < gap_stop - gap_start)
925 max_gap = gap_stop - gap_start;
935 lma = bfd_section_lma (obfd, osections[c - 1]);
936 size = bfd_section_size (obfd, osections[c - 1]);
937 if (lma + size < pad_to)
939 if (! bfd_set_section_size (obfd, osections[c - 1],
942 non_fatal (_("Can't add padding to %s: %s"),
943 bfd_get_section_name (obfd, osections[c - 1]),
944 bfd_errmsg (bfd_get_error ()));
949 gaps[c - 1] = pad_to - (lma + size);
950 if (max_gap < pad_to - (lma + size))
951 max_gap = pad_to - (lma + size);
957 /* Symbol filtering must happen after the output sections have
958 been created, but before their contents are set. */
960 symsize = bfd_get_symtab_upper_bound (ibfd);
962 RETURN_NONFATAL (bfd_get_filename (ibfd));
964 osympp = isympp = (asymbol **) xmalloc (symsize);
965 symcount = bfd_canonicalize_symtab (ibfd, isympp);
967 RETURN_NONFATAL (bfd_get_filename (ibfd));
969 if (convert_debugging)
970 dhandle = read_debugging_info (ibfd, isympp, symcount);
972 if (strip_symbols == STRIP_DEBUG
973 || strip_symbols == STRIP_ALL
974 || strip_symbols == STRIP_UNNEEDED
975 || discard_locals != LOCALS_UNDEF
976 || strip_specific_list != NULL
977 || keep_specific_list != NULL
978 || localize_specific_list != NULL
979 || weaken_specific_list != NULL
983 || change_leading_char
984 || remove_leading_char
988 /* Mark symbols used in output relocations so that they
989 are kept, even if they are local labels or static symbols.
991 Note we iterate over the input sections examining their
992 relocations since the relocations for the output sections
993 haven't been set yet. mark_symbols_used_in_relocations will
994 ignore input sections which have no corresponding output
996 if (strip_symbols != STRIP_ALL)
997 bfd_map_over_sections (ibfd,
998 mark_symbols_used_in_relocations,
1000 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1001 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1004 if (convert_debugging && dhandle != NULL)
1006 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1013 bfd_set_symtab (obfd, osympp, symcount);
1015 /* This has to happen after the symbol table has been set. */
1016 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
1018 if (add_sections != NULL)
1020 struct section_add *padd;
1022 for (padd = add_sections; padd != NULL; padd = padd->next)
1024 if (! bfd_set_section_contents (obfd, padd->section,
1025 (PTR) padd->contents,
1027 (bfd_size_type) padd->size))
1028 RETURN_NONFATAL (bfd_get_filename (obfd));
1032 if (gap_fill_set || pad_to_set)
1037 /* Fill in the gaps. */
1041 buf = (bfd_byte *) xmalloc (max_gap);
1042 memset (buf, gap_fill, (size_t) max_gap);
1044 c = bfd_count_sections (obfd);
1045 for (i = 0; i < c; i++)
1053 off = bfd_section_size (obfd, osections[i]) - left;
1063 if (! bfd_set_section_contents (obfd, osections[i], buf,
1065 RETURN_NONFATAL (bfd_get_filename (obfd));
1074 /* Allow the BFD backend to copy any private data it understands
1075 from the input BFD to the output BFD. This is done last to
1076 permit the routine to look at the filtered symbol table, which is
1077 important for the ECOFF code at least. */
1078 if (!bfd_copy_private_bfd_data (ibfd, obfd))
1080 non_fatal (_("%s: error copying private BFD data: %s"),
1081 bfd_get_filename (obfd),
1082 bfd_errmsg (bfd_get_error ()));
1088 /* Read each archive element in turn from IBFD, copy the
1089 contents to temp file, and keep the temp file handle. */
1092 copy_archive (ibfd, obfd, output_target)
1095 const char *output_target;
1099 struct name_list *next;
1103 bfd **ptr = &obfd->archive_head;
1105 char *dir = make_tempname (bfd_get_filename (obfd));
1107 /* Make a temp directory to hold the contents. */
1108 #if defined (_WIN32) && !defined (__CYGWIN32__)
1109 if (mkdir (dir) != 0)
1111 if (mkdir (dir, 0700) != 0)
1114 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1115 dir, strerror (errno));
1117 obfd->has_armap = ibfd->has_armap;
1121 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1122 while (!status && this_element != (bfd *) NULL)
1124 /* Create an output file for this member. */
1125 char *output_name = concat (dir, "/", bfd_get_filename (this_element),
1127 bfd *output_bfd = bfd_openw (output_name, output_target);
1130 int stat_status = 0;
1134 stat_status = bfd_stat_arch_elt (this_element, &buf);
1135 if (stat_status != 0)
1136 non_fatal (_("internal stat error on %s"),
1137 bfd_get_filename (this_element));
1140 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1141 l->name = output_name;
1145 if (output_bfd == (bfd *) NULL)
1146 RETURN_NONFATAL (output_name);
1148 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1149 RETURN_NONFATAL (bfd_get_filename (obfd));
1151 if (bfd_check_format (this_element, bfd_object) == true)
1152 copy_object (this_element, output_bfd);
1154 if (!bfd_close (output_bfd))
1156 bfd_nonfatal (bfd_get_filename (output_bfd));
1157 /* Error in new object file. Don't change archive. */
1161 if (preserve_dates && stat_status == 0)
1162 set_times (output_name, &buf);
1164 /* Open the newly output file and attach to our list. */
1165 output_bfd = bfd_openr (output_name, output_target);
1167 l->obfd = output_bfd;
1170 ptr = &output_bfd->next;
1172 last_element = this_element;
1174 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1176 bfd_close (last_element);
1178 *ptr = (bfd *) NULL;
1180 if (!bfd_close (obfd))
1181 RETURN_NONFATAL (bfd_get_filename (obfd));
1183 if (!bfd_close (ibfd))
1184 RETURN_NONFATAL (bfd_get_filename (ibfd));
1186 /* Delete all the files that we opened. */
1187 for (l = list; l != NULL; l = l->next)
1189 bfd_close (l->obfd);
1195 /* The top-level control. */
1198 copy_file (input_filename, output_filename, input_target, output_target)
1199 const char *input_filename;
1200 const char *output_filename;
1201 const char *input_target;
1202 const char *output_target;
1207 /* To allow us to do "strip *" without dying on the first
1208 non-object file, failures are nonfatal. */
1210 ibfd = bfd_openr (input_filename, input_target);
1212 RETURN_NONFATAL (input_filename);
1214 if (bfd_check_format (ibfd, bfd_archive))
1218 /* bfd_get_target does not return the correct value until
1219 bfd_check_format succeeds. */
1220 if (output_target == NULL)
1221 output_target = bfd_get_target (ibfd);
1223 obfd = bfd_openw (output_filename, output_target);
1225 RETURN_NONFATAL (output_filename);
1227 copy_archive (ibfd, obfd, output_target);
1229 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1233 /* bfd_get_target does not return the correct value until
1234 bfd_check_format succeeds. */
1235 if (output_target == NULL)
1236 output_target = bfd_get_target (ibfd);
1238 obfd = bfd_openw (output_filename, output_target);
1240 RETURN_NONFATAL (output_filename);
1242 copy_object (ibfd, obfd);
1244 if (!bfd_close (obfd))
1245 RETURN_NONFATAL (output_filename);
1247 if (!bfd_close (ibfd))
1248 RETURN_NONFATAL (input_filename);
1252 bfd_nonfatal (input_filename);
1254 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1256 list_matching_formats (matching);
1264 /* Create a section in OBFD with the same name and attributes
1265 as ISECTION in IBFD. */
1268 setup_section (ibfd, isection, obfdarg)
1273 bfd *obfd = (bfd *) obfdarg;
1274 struct section_list *p;
1282 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1283 && (strip_symbols == STRIP_DEBUG
1284 || strip_symbols == STRIP_UNNEEDED
1285 || strip_symbols == STRIP_ALL
1286 || discard_locals == LOCALS_ALL
1287 || convert_debugging))
1290 p = find_section_list (bfd_section_name (ibfd, isection), false);
1294 if (sections_removed && p != NULL && p->remove)
1296 if (sections_copied && (p == NULL || ! p->copy))
1299 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1301 if (osection == NULL)
1307 size = bfd_section_size (ibfd, isection);
1309 size = (size + interleave - 1) / interleave;
1310 if (! bfd_set_section_size (obfd, osection, size))
1316 vma = bfd_section_vma (ibfd, isection);
1317 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1319 else if (p != NULL && p->change_vma == CHANGE_SET)
1322 vma += change_section_address;
1324 if (! bfd_set_section_vma (obfd, osection, vma))
1330 lma = isection->lma;
1331 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1333 if (p->change_lma == CHANGE_MODIFY)
1335 else if (p->change_lma == CHANGE_SET)
1341 lma += change_section_address;
1343 osection->lma = lma;
1345 /* FIXME: This is probably not enough. If we change the LMA we
1346 may have to recompute the header for the file as well. */
1347 if (bfd_set_section_alignment (obfd,
1349 bfd_section_alignment (ibfd, isection))
1352 err = _("alignment");
1356 flags = bfd_get_section_flags (ibfd, isection);
1357 if (p != NULL && p->set_flags)
1358 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1359 if (!bfd_set_section_flags (obfd, osection, flags))
1365 /* This used to be mangle_section; we do here to avoid using
1366 bfd_get_section_by_name since some formats allow multiple
1367 sections with the same name. */
1368 isection->output_section = osection;
1369 isection->output_offset = 0;
1371 /* Allow the BFD backend to copy any private data it understands
1372 from the input section to the output section. */
1373 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1375 err = _("private data");
1383 non_fatal (_("%s: section `%s': error in %s: %s"),
1384 bfd_get_filename (ibfd),
1385 bfd_section_name (ibfd, isection),
1386 err, bfd_errmsg (bfd_get_error ()));
1390 /* Copy the data of input section ISECTION of IBFD
1391 to an output section with the same name in OBFD.
1392 If stripping then don't copy any relocation info. */
1395 copy_section (ibfd, isection, obfdarg)
1400 bfd *obfd = (bfd *) obfdarg;
1401 struct section_list *p;
1408 /* If we have already failed earlier on, do not keep on generating
1413 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1414 && (strip_symbols == STRIP_DEBUG
1415 || strip_symbols == STRIP_UNNEEDED
1416 || strip_symbols == STRIP_ALL
1417 || discard_locals == LOCALS_ALL
1418 || convert_debugging))
1423 p = find_section_list (bfd_section_name (ibfd, isection), false);
1425 if (sections_removed && p != NULL && p->remove)
1427 if (sections_copied && (p == NULL || ! p->copy))
1430 osection = isection->output_section;
1431 size = bfd_get_section_size_before_reloc (isection);
1433 if (size == 0 || osection == 0)
1437 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1439 RETURN_NONFATAL (bfd_get_filename (ibfd));
1442 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1445 relpp = (arelent **) xmalloc (relsize);
1446 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1448 RETURN_NONFATAL (bfd_get_filename (ibfd));
1450 if (strip_symbols == STRIP_ALL)
1452 /* Remove relocations which are not in
1453 keep_strip_specific_list. */
1454 arelent **temp_relpp;
1455 long temp_relcount = 0;
1458 temp_relpp = (arelent **) xmalloc (relsize);
1459 for (i = 0; i < relcount; i++)
1460 if (is_specified_symbol
1461 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1462 keep_specific_list))
1463 temp_relpp [temp_relcount++] = relpp [i];
1464 relcount = temp_relcount;
1468 bfd_set_reloc (obfd, osection,
1469 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1472 isection->_cooked_size = isection->_raw_size;
1473 isection->reloc_done = true;
1475 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1477 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1479 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1481 RETURN_NONFATAL (bfd_get_filename (ibfd));
1484 filter_bytes (memhunk, &size);
1486 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1488 RETURN_NONFATAL (bfd_get_filename (obfd));
1492 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1494 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1496 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1497 flag--they can just remove the section entirely and add it
1498 back again. However, we do permit them to turn on the
1499 SEC_HAS_CONTENTS flag, and take it to mean that the section
1500 contents should be zeroed out. */
1502 memset (memhunk, 0, size);
1503 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1505 RETURN_NONFATAL (bfd_get_filename (obfd));
1510 /* Get all the sections. This is used when --gap-fill or --pad-to is
1514 get_sections (obfd, osection, secppparg)
1515 bfd *obfd ATTRIBUTE_UNUSED;
1519 asection ***secppp = (asection ***) secppparg;
1521 **secppp = osection;
1525 /* Sort sections by VMA. This is called via qsort, and is used when
1526 --gap-fill or --pad-to is used. We force non loadable or empty
1527 sections to the front, where they are easier to ignore. */
1530 compare_section_lma (arg1, arg2)
1534 const asection **sec1 = (const asection **) arg1;
1535 const asection **sec2 = (const asection **) arg2;
1536 flagword flags1, flags2;
1538 /* Sort non loadable sections to the front. */
1539 flags1 = (*sec1)->flags;
1540 flags2 = (*sec2)->flags;
1541 if ((flags1 & SEC_HAS_CONTENTS) == 0
1542 || (flags1 & SEC_LOAD) == 0)
1544 if ((flags2 & SEC_HAS_CONTENTS) != 0
1545 && (flags2 & SEC_LOAD) != 0)
1550 if ((flags2 & SEC_HAS_CONTENTS) == 0
1551 || (flags2 & SEC_LOAD) == 0)
1555 /* Sort sections by LMA. */
1556 if ((*sec1)->lma > (*sec2)->lma)
1558 else if ((*sec1)->lma < (*sec2)->lma)
1561 /* Sort sections with the same LMA by size. */
1562 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1564 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1570 /* Mark all the symbols which will be used in output relocations with
1571 the BSF_KEEP flag so that those symbols will not be stripped.
1573 Ignore relocations which will not appear in the output file. */
1576 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1581 asymbol **symbols = (asymbol **) symbolsarg;
1586 /* Ignore an input section with no corresponding output section. */
1587 if (isection->output_section == NULL)
1590 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1592 bfd_fatal (bfd_get_filename (ibfd));
1597 relpp = (arelent **) xmalloc (relsize);
1598 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1600 bfd_fatal (bfd_get_filename (ibfd));
1602 /* Examine each symbol used in a relocation. If it's not one of the
1603 special bfd section symbols, then mark it with BSF_KEEP. */
1604 for (i = 0; i < relcount; i++)
1606 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1607 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1608 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1609 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1616 /* Write out debugging information. */
1619 write_debugging_info (obfd, dhandle, symcountp, symppp)
1622 long *symcountp ATTRIBUTE_UNUSED;
1623 asymbol ***symppp ATTRIBUTE_UNUSED;
1625 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1626 return write_ieee_debugging_info (obfd, dhandle);
1628 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1629 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1631 bfd_byte *syms, *strings;
1632 bfd_size_type symsize, stringsize;
1633 asection *stabsec, *stabstrsec;
1635 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1640 stabsec = bfd_make_section (obfd, ".stab");
1641 stabstrsec = bfd_make_section (obfd, ".stabstr");
1643 || stabstrsec == NULL
1644 || ! bfd_set_section_size (obfd, stabsec, symsize)
1645 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1646 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1647 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1648 || ! bfd_set_section_flags (obfd, stabsec,
1652 || ! bfd_set_section_flags (obfd, stabstrsec,
1657 non_fatal (_("%s: can't create debugging section: %s"),
1658 bfd_get_filename (obfd),
1659 bfd_errmsg (bfd_get_error ()));
1663 /* We can get away with setting the section contents now because
1664 the next thing the caller is going to do is copy over the
1665 real sections. We may someday have to split the contents
1666 setting out of this function. */
1667 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1669 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1670 (file_ptr) 0, stringsize))
1672 non_fatal (_("%s: can't set debugging section contents: %s"),
1673 bfd_get_filename (obfd),
1674 bfd_errmsg (bfd_get_error ()));
1681 non_fatal (_("%s: don't know how to write debugging information for %s"),
1682 bfd_get_filename (obfd), bfd_get_target (obfd));
1687 strip_main (argc, argv)
1691 char *input_target = NULL, *output_target = NULL;
1692 boolean show_version = false;
1694 struct section_list *p;
1695 char *output_file = NULL;
1697 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXVv",
1698 strip_options, (int *) 0)) != EOF)
1703 input_target = optarg;
1706 output_target = optarg;
1709 input_target = output_target = optarg;
1712 p = find_section_list (optarg, true);
1714 sections_removed = true;
1717 strip_symbols = STRIP_ALL;
1721 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
1722 strip_symbols = STRIP_DEBUG;
1724 case OPTION_STRIP_UNNEEDED:
1725 strip_symbols = STRIP_UNNEEDED;
1728 add_specific_symbol (optarg, &keep_specific_list);
1731 add_specific_symbol (optarg, &strip_specific_list);
1734 output_file = optarg;
1737 preserve_dates = true;
1740 discard_locals = LOCALS_ALL;
1743 discard_locals = LOCALS_START_L;
1749 show_version = true;
1752 break; /* we've been given a long option */
1754 strip_usage (stdout, 0);
1756 strip_usage (stderr, 1);
1761 print_version ("strip");
1763 /* Default is to strip all symbols. */
1764 if (strip_symbols == STRIP_UNDEF
1765 && discard_locals == LOCALS_UNDEF
1766 && strip_specific_list == NULL)
1767 strip_symbols = STRIP_ALL;
1769 if (output_target == (char *) NULL)
1770 output_target = input_target;
1774 || (output_file != NULL && (i + 1) < argc))
1775 strip_usage (stderr, 1);
1777 for (; i < argc; i++)
1779 int hold_status = status;
1780 struct stat statbuf;
1785 if (stat (argv[i], &statbuf) < 0)
1787 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
1792 if (output_file != NULL)
1793 tmpname = output_file;
1795 tmpname = make_tempname (argv[i]);
1798 copy_file (argv[i], tmpname, input_target, output_target);
1802 set_times (tmpname, &statbuf);
1803 if (output_file == NULL)
1804 smart_rename (tmpname, argv[i], preserve_dates);
1805 status = hold_status;
1809 if (output_file == NULL)
1817 copy_main (argc, argv)
1821 char * binary_architecture = NULL;
1822 char *input_filename = NULL, *output_filename = NULL;
1823 char *input_target = NULL, *output_target = NULL;
1824 boolean show_version = false;
1825 boolean change_warn = true;
1827 struct section_list *p;
1828 struct stat statbuf;
1830 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:R:SpgxXVvW:",
1831 copy_options, (int *) 0)) != EOF)
1836 copy_byte = atoi (optarg);
1838 fatal (_("byte number must be non-negative"));
1842 binary_architecture = optarg;
1846 interleave = atoi (optarg);
1848 fatal (_("interleave must be positive"));
1852 case 's': /* "source" - 'I' is preferred */
1853 input_target = optarg;
1857 case 'd': /* "destination" - 'O' is preferred */
1858 output_target = optarg;
1862 input_target = output_target = optarg;
1866 p = find_section_list (optarg, true);
1868 fatal (_("%s both copied and removed"), optarg);
1870 sections_copied = true;
1874 p = find_section_list (optarg, true);
1876 fatal (_("%s both copied and removed"), optarg);
1878 sections_removed = true;
1882 strip_symbols = STRIP_ALL;
1886 strip_symbols = STRIP_DEBUG;
1889 case OPTION_STRIP_UNNEEDED:
1890 strip_symbols = STRIP_UNNEEDED;
1894 add_specific_symbol (optarg, &keep_specific_list);
1898 add_specific_symbol (optarg, &strip_specific_list);
1902 add_specific_symbol (optarg, &localize_specific_list);
1906 add_specific_symbol (optarg, &weaken_specific_list);
1910 preserve_dates = true;
1914 discard_locals = LOCALS_ALL;
1918 discard_locals = LOCALS_START_L;
1926 show_version = true;
1933 case OPTION_ADD_SECTION:
1937 struct section_add *pa;
1942 s = strchr (optarg, '=');
1945 fatal (_("bad format for %s"), "--add-section");
1947 if (stat (s + 1, & st) < 0)
1948 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
1950 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1953 name = (char *) xmalloc (len + 1);
1954 strncpy (name, optarg, len);
1958 pa->filename = s + 1;
1960 pa->size = st.st_size;
1962 pa->contents = (bfd_byte *) xmalloc (pa->size);
1963 f = fopen (pa->filename, FOPEN_RB);
1966 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
1968 if (fread (pa->contents, 1, pa->size, f) == 0
1970 fatal (_("%s: fread failed"), pa->filename);
1974 pa->next = add_sections;
1979 case OPTION_CHANGE_START:
1980 change_start = parse_vma (optarg, "--change-start");
1983 case OPTION_CHANGE_SECTION_ADDRESS:
1984 case OPTION_CHANGE_SECTION_LMA:
1985 case OPTION_CHANGE_SECTION_VMA:
1990 char *option = NULL;
1992 enum change_action what = CHANGE_IGNORE;
1996 case OPTION_CHANGE_SECTION_ADDRESS:
1997 option = "--change-section-address";
1999 case OPTION_CHANGE_SECTION_LMA:
2000 option = "--change-section-lma";
2002 case OPTION_CHANGE_SECTION_VMA:
2003 option = "--change-section-vma";
2007 s = strchr (optarg, '=');
2010 s = strchr (optarg, '+');
2013 s = strchr (optarg, '-');
2015 fatal (_("bad format for %s"), option);
2020 name = (char *) xmalloc (len + 1);
2021 strncpy (name, optarg, len);
2024 p = find_section_list (name, true);
2026 val = parse_vma (s + 1, option);
2030 case '=': what = CHANGE_SET; break;
2031 case '-': val = - val; /* Drop through. */
2032 case '+': what = CHANGE_MODIFY; break;
2037 case OPTION_CHANGE_SECTION_ADDRESS:
2038 p->change_vma = what;
2042 case OPTION_CHANGE_SECTION_LMA:
2043 p->change_lma = what;
2047 case OPTION_CHANGE_SECTION_VMA:
2048 p->change_vma = what;
2055 case OPTION_CHANGE_ADDRESSES:
2056 change_section_address = parse_vma (optarg, "--change-addresses");
2057 change_start = change_section_address;
2060 case OPTION_CHANGE_WARNINGS:
2064 case OPTION_CHANGE_LEADING_CHAR:
2065 change_leading_char = true;
2068 case OPTION_DEBUGGING:
2069 convert_debugging = true;
2072 case OPTION_GAP_FILL:
2074 bfd_vma gap_fill_vma;
2076 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2077 gap_fill = (bfd_byte) gap_fill_vma;
2078 if ((bfd_vma) gap_fill != gap_fill_vma)
2082 sprintf_vma (buff, gap_fill_vma);
2084 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2087 gap_fill_set = true;
2091 case OPTION_NO_CHANGE_WARNINGS:
2092 change_warn = false;
2096 pad_to = parse_vma (optarg, "--pad-to");
2100 case OPTION_REMOVE_LEADING_CHAR:
2101 remove_leading_char = true;
2104 case OPTION_REDEFINE_SYM:
2106 /* Push this redefinition onto redefine_symbol_list. */
2110 const char *nextarg;
2111 char *source, *target;
2113 s = strchr (optarg, '=');
2116 fatal (_("bad format for %s"), "--redefine-sym");
2120 source = (char *) xmalloc (len + 1);
2121 strncpy (source, optarg, len);
2125 len = strlen (nextarg);
2126 target = (char *) xmalloc (len + 1);
2127 strcpy (target, nextarg);
2129 redefine_list_append (source, target);
2136 case OPTION_SET_SECTION_FLAGS:
2142 s = strchr (optarg, '=');
2144 fatal (_("bad format for %s"), "--set-section-flags");
2147 name = (char *) xmalloc (len + 1);
2148 strncpy (name, optarg, len);
2151 p = find_section_list (name, true);
2153 p->set_flags = true;
2154 p->flags = parse_flags (s + 1);
2158 case OPTION_SET_START:
2159 set_start = parse_vma (optarg, "--set-start");
2160 set_start_set = true;
2163 case OPTION_SREC_LEN:
2164 Chunk = parse_vma (optarg, "--srec-len");
2167 case OPTION_SREC_FORCES3:
2172 break; /* we've been given a long option */
2175 copy_usage (stdout, 0);
2178 copy_usage (stderr, 1);
2183 print_version ("objcopy");
2185 if (copy_byte >= interleave)
2186 fatal (_("byte number must be less than interleave"));
2188 if (optind == argc || optind + 2 < argc)
2189 copy_usage (stderr, 1);
2191 input_filename = argv[optind];
2192 if (optind + 1 < argc)
2193 output_filename = argv[optind + 1];
2195 /* Default is to strip no symbols. */
2196 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2197 strip_symbols = STRIP_NONE;
2199 if (output_target == (char *) NULL)
2200 output_target = input_target;
2202 if (binary_architecture != (char *) NULL)
2204 if (input_target && strcmp (input_target, "binary") == 0)
2206 const bfd_arch_info_type * temp_arch_info;
2208 temp_arch_info = bfd_scan_arch (binary_architecture);
2210 if (temp_arch_info != NULL)
2211 bfd_external_binary_architecture = temp_arch_info->arch;
2213 fatal (_("architecture %s unknown"), binary_architecture);
2217 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2218 non_fatal (_(" Argument %s ignored"), binary_architecture);
2223 if (stat (input_filename, & statbuf) < 0)
2224 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2226 /* If there is no destination file then create a temp and rename
2227 the result into the input. */
2229 if (output_filename == (char *) NULL)
2231 char *tmpname = make_tempname (input_filename);
2233 copy_file (input_filename, tmpname, input_target, output_target);
2237 set_times (tmpname, &statbuf);
2238 smart_rename (tmpname, input_filename, preserve_dates);
2245 copy_file (input_filename, output_filename, input_target, output_target);
2246 if (status == 0 && preserve_dates)
2247 set_times (output_filename, &statbuf);
2252 for (p = change_sections; p != NULL; p = p->next)
2256 if (p->change_vma != CHANGE_IGNORE)
2260 sprintf_vma (buff, p->vma_val);
2262 /* xgettext:c-format */
2263 non_fatal (_("%s %s%c0x%s never used"),
2264 "--change-section-vma",
2266 p->change_vma == CHANGE_SET ? '=' : '+',
2270 if (p->change_lma != CHANGE_IGNORE)
2274 sprintf_vma (buff, p->lma_val);
2276 /* xgettext:c-format */
2277 non_fatal (_("%s %s%c0x%s never used"),
2278 "--change-section-lma",
2280 p->change_lma == CHANGE_SET ? '=' : '+',
2295 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2296 setlocale (LC_MESSAGES, "");
2298 bindtextdomain (PACKAGE, LOCALEDIR);
2299 textdomain (PACKAGE);
2301 program_name = argv[0];
2302 xmalloc_set_program_name (program_name);
2304 START_PROGRESS (program_name, 0);
2306 strip_symbols = STRIP_UNDEF;
2307 discard_locals = LOCALS_UNDEF;
2310 set_default_bfd_target ();
2314 int i = strlen (program_name);
2315 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2316 /* Drop the .exe suffix, if any. */
2317 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
2320 program_name[i] = '\0';
2323 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
2327 strip_main (argc, argv);
2329 copy_main (argc, argv);
2331 END_PROGRESS (program_name);