2000-08-11 Carlo Wood <carlo@alinoe.com>
[binutils.git] / binutils / objcopy.c
blobb8a49c8a34423aa309e0aa2b70f8e325b9997b9f
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
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
20 02111-1307, USA. */
22 #include "bfd.h"
23 #include "progress.h"
24 #include "bucomm.h"
25 #include "getopt.h"
26 #include "libiberty.h"
27 #include "budbg.h"
28 #include "filenames.h"
29 #include <sys/stat.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
34 deleted. */
36 struct symlist
38 const char *name;
39 struct symlist *next;
42 /* A list to support redefine_sym. */
43 struct redefine_node
45 char *source;
46 char *target;
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 *));
68 static void copy_file
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. */
88 enum strip_action
90 STRIP_UNDEF,
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;
100 enum locals_action
102 LOCALS_UNDEF,
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. */
111 enum change_action
113 CHANGE_IGNORE,
114 CHANGE_MODIFY,
115 CHANGE_SET
118 /* Structure used to hold lists of sections and actions to take. */
119 struct section_list
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. */
156 struct section_add
158 /* Next section to add. */
159 struct section_add *next;
160 /* Name of section to add. */
161 const char *name;
162 /* Name of file holding section contents. */
163 const char *filename;
164 /* Size of file. */
165 size_t size;
166 /* Contents of file. */
167 bfd_byte *contents;
168 /* BFD section, after it has been added. */
169 asection *section;
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)
219 /* Options to handle if running as "strip". */
221 static struct option strip_options[] =
223 {"discard-all", no_argument, 0, 'x'},
224 {"discard-locals", no_argument, 0, 'X'},
225 {"format", required_argument, 0, 'F'}, /* Obsolete */
226 {"help", no_argument, 0, 'h'},
227 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
228 {"input-target", required_argument, 0, 'I'},
229 {"keep-symbol", required_argument, 0, 'K'},
230 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
231 {"output-target", required_argument, 0, 'O'},
232 {"preserve-dates", no_argument, 0, 'p'},
233 {"remove-section", required_argument, 0, 'R'},
234 {"strip-all", no_argument, 0, 's'},
235 {"strip-debug", no_argument, 0, 'S'},
236 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
237 {"strip-symbol", required_argument, 0, 'N'},
238 {"target", required_argument, 0, 'F'},
239 {"verbose", no_argument, 0, 'v'},
240 {"version", no_argument, 0, 'V'},
241 {0, no_argument, 0, 0}
244 /* Options to handle if running as "objcopy". */
246 static struct option copy_options[] =
248 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
249 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
250 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
251 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
252 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
253 {"byte", required_argument, 0, 'b'},
254 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
255 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
256 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
257 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
258 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
259 {"change-start", required_argument, 0, OPTION_CHANGE_START},
260 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
261 {"debugging", no_argument, 0, OPTION_DEBUGGING},
262 {"discard-all", no_argument, 0, 'x'},
263 {"discard-locals", no_argument, 0, 'X'},
264 {"only-section", required_argument, 0, 'j'},
265 {"format", required_argument, 0, 'F'}, /* Obsolete */
266 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
267 {"help", no_argument, 0, 'h'},
268 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
269 {"input-target", required_argument, 0, 'I'},
270 {"interleave", required_argument, 0, 'i'},
271 {"keep-symbol", required_argument, 0, 'K'},
272 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
273 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
274 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
275 {"output-target", required_argument, 0, 'O'},
276 {"pad-to", required_argument, 0, OPTION_PAD_TO},
277 {"preserve-dates", no_argument, 0, 'p'},
278 {"localize-symbol", required_argument, 0, 'L'},
279 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
280 {"remove-section", required_argument, 0, 'R'},
281 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
282 {"set-start", required_argument, 0, OPTION_SET_START},
283 {"strip-all", no_argument, 0, 'S'},
284 {"strip-debug", no_argument, 0, 'g'},
285 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
286 {"strip-symbol", required_argument, 0, 'N'},
287 {"target", required_argument, 0, 'F'},
288 {"verbose", no_argument, 0, 'v'},
289 {"version", no_argument, 0, 'V'},
290 {"weaken", no_argument, 0, OPTION_WEAKEN},
291 {"weaken-symbol", required_argument, 0, 'W'},
292 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
293 {0, no_argument, 0, 0}
296 /* IMPORTS */
297 extern char *program_name;
299 /* This flag distinguishes between strip and objcopy:
300 1 means this is 'strip'; 0 means this is 'objcopy'.
301 -1 means if we should use argv[0] to decide. */
302 extern int is_strip;
305 static void
306 copy_usage (stream, exit_status)
307 FILE *stream;
308 int exit_status;
310 fprintf (stream, _("Usage: %s <switches> in-file [out-file]\n"), program_name);
311 fprintf (stream, _(" The switches are:\n"));
312 fprintf (stream, _("\
313 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
314 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
315 -F --target <bfdname> Set both input and output format to <bfdname>\n\
316 --debugging Convert debugging information, if possible\n\
317 -p --preserve-dates Copy modified/access timestamps to the output\n\
318 -j --only-section <name> Only copy section <name> into the output\n\
319 -R --remove-section <name> Remove section <name> from the output\n\
320 -S --strip-all Remove all symbol and relocation information\n\
321 -g --strip-debug Remove all debugging symbols\n\
322 --strip-unneeded Remove all symbols not needed by relocations\n\
323 -N --strip-symbol <name> Do not copy symbol <name>\n\
324 -K --keep-symbol <name> Only copy symbol <name>\n\
325 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
326 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
327 --weaken Force all global symbols to be marked as weak\n\
328 -x --discard-all Remove all non-global symbols\n\
329 -X --discard-locals Remove any compiler-generated symbols\n\
330 -i --interleave <number> Only copy one out of every <number> bytes\n\
331 -b --byte <num> Select byte <num> in every interleaved block\n\
332 --gap-fill <val> Fill gaps between sections with <val>\n\
333 --pad-to <addr> Pad the last section up to address <addr>\n\
334 --set-start <addr> Set the start address to <addr>\n\
335 {--change-start|--adjust-start} <incr>\n\
336 Add <incr> to the start address\n\
337 {--change-addresses|--adjust-vma} <incr>\n\
338 Add <incr> to LMA, VMA and start addresses\n\
339 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
340 Change LMA and VMA of section <name> by <val>\n\
341 --change-section-lma <name>{=|+|-}<val>\n\
342 Change the LMA of section <name> by <val>\n\
343 --change-section-vma <name>{=|+|-}<val>\n\
344 Change the VMA of section <name> by <val>\n\
345 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
346 Warn if a named section does not exist\n\
347 --set-section-flags <name>=<flags>\n\
348 Set section <name>'s properties to <flags>\n\
349 --add-section <name>=<file> Add section <name> found in <file> to output\n\
350 --change-leading-char Force output format's leading character style\n\
351 --remove-leading-char Remove leading character from global symbols\n\
352 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
353 -v --verbose List all object files modified\n\
354 -V --version Display this program's version number\n\
355 -h --help Display this output\n\
356 "));
357 list_supported_targets (program_name, stream);
358 if (exit_status == 0)
359 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
360 exit (exit_status);
363 static void
364 strip_usage (stream, exit_status)
365 FILE *stream;
366 int exit_status;
368 fprintf (stream, _("Usage: %s <switches> in-file(s)\n"), program_name);
369 fprintf (stream, _(" The switches are:\n"));
370 fprintf (stream, _("\
371 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
372 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
373 -F --target <bfdname> Set both input and output format to <bfdname>\n\
374 -p --preserve-dates Copy modified/access timestamps to the output\n\
375 -R --remove-section <name> Remove section <name> from the output\n\
376 -s --strip-all Remove all symbol and relocation information\n\
377 -g -S --strip-debug Remove all debugging symbols\n\
378 --strip-unneeded Remove all symbols not needed by relocations\n\
379 -N --strip-symbol <name> Do not copy symbol <name>\n\
380 -K --keep-symbol <name> Only copy symbol <name>\n\
381 -x --discard-all Remove all non-global symbols\n\
382 -X --discard-locals Remove any compiler-generated symbols\n\
383 -v --verbose List all object files modified\n\
384 -V --version Display this program's version number\n\
385 -h --help Display this output\n\
386 -o <file> Place stripped output into <file>\n\
387 "));
389 list_supported_targets (program_name, stream);
390 if (exit_status == 0)
391 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
392 exit (exit_status);
395 /* Parse section flags into a flagword, with a fatal error if the
396 string can't be parsed. */
398 static flagword
399 parse_flags (s)
400 const char *s;
402 flagword ret;
403 const char *snext;
404 int len;
406 ret = SEC_NO_FLAGS;
410 snext = strchr (s, ',');
411 if (snext == NULL)
412 len = strlen (s);
413 else
415 len = snext - s;
416 ++snext;
419 if (0) ;
420 #define PARSE_FLAG(fname,fval) \
421 else if (strncasecmp (fname, s, len) == 0) ret |= fval
422 PARSE_FLAG ("alloc", SEC_ALLOC);
423 PARSE_FLAG ("load", SEC_LOAD);
424 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
425 PARSE_FLAG ("readonly", SEC_READONLY);
426 PARSE_FLAG ("debug", SEC_DEBUGGING);
427 PARSE_FLAG ("code", SEC_CODE);
428 PARSE_FLAG ("data", SEC_DATA);
429 PARSE_FLAG ("rom", SEC_ROM);
430 PARSE_FLAG ("share", SEC_SHARED);
431 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
432 #undef PARSE_FLAG
433 else
435 char *copy;
437 copy = xmalloc (len + 1);
438 strncpy (copy, s, len);
439 copy[len] = '\0';
440 non_fatal (_("unrecognized section flag `%s'"), copy);
441 fatal (_("supported flags: %s"),
442 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
445 s = snext;
447 while (s != NULL);
449 return ret;
452 /* Find and optionally add an entry in the change_sections list. */
454 static struct section_list *
455 find_section_list (name, add)
456 const char *name;
457 boolean add;
459 register struct section_list *p;
461 for (p = change_sections; p != NULL; p = p->next)
462 if (strcmp (p->name, name) == 0)
463 return p;
465 if (! add)
466 return NULL;
468 p = (struct section_list *) xmalloc (sizeof (struct section_list));
469 p->name = name;
470 p->used = false;
471 p->remove = false;
472 p->copy = false;
473 p->change_vma = CHANGE_IGNORE;
474 p->change_lma = CHANGE_IGNORE;
475 p->vma_val = 0;
476 p->lma_val = 0;
477 p->set_flags = false;
478 p->flags = 0;
480 p->next = change_sections;
481 change_sections = p;
483 return p;
486 /* Add a symbol to strip_specific_list. */
488 static void
489 add_specific_symbol (name, list)
490 const char *name;
491 struct symlist **list;
493 struct symlist *tmp_list;
495 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
496 tmp_list->name = name;
497 tmp_list->next = *list;
498 *list = tmp_list;
501 /* See whether a symbol should be stripped or kept based on
502 strip_specific_list and keep_symbols. */
504 static boolean
505 is_specified_symbol (name, list)
506 const char *name;
507 struct symlist *list;
509 struct symlist *tmp_list;
511 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
513 if (strcmp (name, tmp_list->name) == 0)
514 return true;
516 return false;
519 /* See if a section is being removed. */
521 static boolean
522 is_strip_section (abfd, sec)
523 bfd *abfd ATTRIBUTE_UNUSED;
524 asection *sec;
526 struct section_list *p;
528 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
529 && (strip_symbols == STRIP_DEBUG
530 || strip_symbols == STRIP_UNNEEDED
531 || strip_symbols == STRIP_ALL
532 || discard_locals == LOCALS_ALL
533 || convert_debugging))
534 return true;
536 if (! sections_removed && ! sections_copied)
537 return false;
539 p = find_section_list (bfd_get_section_name (abfd, sec), false);
540 if (sections_removed && p != NULL && p->remove)
541 return true;
542 if (sections_copied && (p == NULL || ! p->copy))
543 return true;
544 return false;
547 /* Choose which symbol entries to copy; put the result in OSYMS.
548 We don't copy in place, because that confuses the relocs.
549 Return the number of symbols to print. */
551 static unsigned int
552 filter_symbols (abfd, obfd, osyms, isyms, symcount)
553 bfd *abfd;
554 bfd *obfd;
555 asymbol **osyms, **isyms;
556 long symcount;
558 register asymbol **from = isyms, **to = osyms;
559 long src_count = 0, dst_count = 0;
560 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
561 == HAS_RELOC;
563 for (; src_count < symcount; src_count++)
565 asymbol *sym = from[src_count];
566 flagword flags = sym->flags;
567 const char *name = bfd_asymbol_name (sym);
568 int keep;
570 if (redefine_sym_list)
572 const char *old_name, *new_name;
574 old_name = bfd_asymbol_name (sym);
575 new_name = lookup_sym_redefinition (old_name);
576 name = bfd_asymbol_name (sym) = new_name;
579 if (change_leading_char
580 && (bfd_get_symbol_leading_char (abfd)
581 != bfd_get_symbol_leading_char (obfd))
582 && (bfd_get_symbol_leading_char (abfd) == '\0'
583 || (name[0] == bfd_get_symbol_leading_char (abfd))))
585 if (bfd_get_symbol_leading_char (obfd) == '\0')
586 name = bfd_asymbol_name (sym) = name + 1;
587 else
589 char *n;
591 n = xmalloc (strlen (name) + 2);
592 n[0] = bfd_get_symbol_leading_char (obfd);
593 if (bfd_get_symbol_leading_char (abfd) == '\0')
594 strcpy (n + 1, name);
595 else
596 strcpy (n + 1, name + 1);
597 name = bfd_asymbol_name (sym) = n;
601 if (remove_leading_char
602 && ((flags & BSF_GLOBAL) != 0
603 || (flags & BSF_WEAK) != 0
604 || bfd_is_und_section (bfd_get_section (sym))
605 || bfd_is_com_section (bfd_get_section (sym)))
606 && name[0] == bfd_get_symbol_leading_char (abfd))
607 name = bfd_asymbol_name (sym) = name + 1;
609 if (strip_symbols == STRIP_ALL)
610 keep = 0;
611 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
612 || ((flags & BSF_SECTION_SYM) != 0
613 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
614 & BSF_KEEP) != 0))
615 keep = 1;
616 else if (relocatable /* Relocatable file. */
617 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
618 keep = 1;
619 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
620 || (flags & BSF_WEAK) != 0
621 || bfd_is_und_section (bfd_get_section (sym))
622 || bfd_is_com_section (bfd_get_section (sym)))
623 keep = strip_symbols != STRIP_UNNEEDED;
624 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
625 keep = (strip_symbols != STRIP_DEBUG
626 && strip_symbols != STRIP_UNNEEDED
627 && ! convert_debugging);
628 else /* Local symbol. */
629 keep = (strip_symbols != STRIP_UNNEEDED
630 && (discard_locals != LOCALS_ALL
631 && (discard_locals != LOCALS_START_L
632 || ! bfd_is_local_label (abfd, sym))));
634 if (keep && is_specified_symbol (name, strip_specific_list))
635 keep = 0;
636 if (!keep && is_specified_symbol (name, keep_specific_list))
637 keep = 1;
638 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
639 keep = 0;
641 if (keep && (flags & BSF_GLOBAL) != 0
642 && (weaken || is_specified_symbol (name, weaken_specific_list)))
644 sym->flags &=~ BSF_GLOBAL;
645 sym->flags |= BSF_WEAK;
647 if (keep && (flags & (BSF_GLOBAL | BSF_WEAK))
648 && is_specified_symbol (name, localize_specific_list))
650 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
651 sym->flags |= BSF_LOCAL;
654 if (keep)
655 to[dst_count++] = sym;
658 to[dst_count] = NULL;
660 return dst_count;
663 static const char *
664 lookup_sym_redefinition (source)
665 const char *source;
667 const char *result;
668 struct redefine_node *list;
670 result = source;
672 for (list = redefine_sym_list; list != NULL; list = list->next)
674 if (strcmp (source, list->source) == 0)
676 result = list->target;
677 break;
680 return result;
683 /* Add a node to a symbol redefine list */
685 static void
686 redefine_list_append (source, target)
687 const char *source;
688 const char *target;
690 struct redefine_node **p;
691 struct redefine_node *list;
692 struct redefine_node *new_node;
694 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
696 if (strcmp (source, list->source) == 0)
698 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
699 "--redefine-sym",
700 source);
703 if (strcmp (target, list->target) == 0)
705 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
706 "--redefine-sym",
707 target);
711 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
713 new_node->source = strdup (source);
714 new_node->target = strdup (target);
715 new_node->next = NULL;
717 *p = new_node;
721 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
722 Adjust *SIZE. */
724 static void
725 filter_bytes (memhunk, size)
726 char *memhunk;
727 bfd_size_type *size;
729 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
731 for (; from < end; from += interleave)
732 *to++ = *from;
733 if (*size % interleave > (bfd_size_type) copy_byte)
734 *size = (*size / interleave) + 1;
735 else
736 *size /= interleave;
739 /* Copy object file IBFD onto OBFD. */
741 static void
742 copy_object (ibfd, obfd)
743 bfd *ibfd;
744 bfd *obfd;
746 bfd_vma start;
747 long symcount;
748 asection **osections = NULL;
749 bfd_size_type *gaps = NULL;
750 bfd_size_type max_gap = 0;
751 long symsize;
752 PTR dhandle;
755 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
756 RETURN_NONFATAL (bfd_get_filename (obfd));
758 if (verbose)
759 printf (_("copy from %s(%s) to %s(%s)\n"),
760 bfd_get_filename (ibfd), bfd_get_target (ibfd),
761 bfd_get_filename (obfd), bfd_get_target (obfd));
763 if (set_start_set)
764 start = set_start;
765 else
766 start = bfd_get_start_address (ibfd);
767 start += change_start;
769 if (!bfd_set_start_address (obfd, start)
770 || !bfd_set_file_flags (obfd,
771 (bfd_get_file_flags (ibfd)
772 & bfd_applicable_file_flags (obfd))))
773 RETURN_NONFATAL (bfd_get_filename (ibfd));
775 /* Copy architecture of input file to output file */
776 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
777 bfd_get_mach (ibfd)))
778 non_fatal (_("Warning: Output file cannot represent architecture %s"),
779 bfd_printable_arch_mach (bfd_get_arch (ibfd),
780 bfd_get_mach (ibfd)));
782 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
783 RETURN_NONFATAL (bfd_get_filename (ibfd));
785 if (isympp)
786 free (isympp);
788 if (osympp != isympp)
789 free (osympp);
791 /* BFD mandates that all output sections be created and sizes set before
792 any output is done. Thus, we traverse all sections multiple times. */
793 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
795 if (add_sections != NULL)
797 struct section_add *padd;
798 struct section_list *pset;
800 for (padd = add_sections; padd != NULL; padd = padd->next)
802 padd->section = bfd_make_section (obfd, padd->name);
803 if (padd->section == NULL)
805 non_fatal (_("can't create section `%s': %s"),
806 padd->name, bfd_errmsg (bfd_get_error ()));
807 status = 1;
808 return;
810 else
812 flagword flags;
814 if (! bfd_set_section_size (obfd, padd->section, padd->size))
815 RETURN_NONFATAL (bfd_get_filename (obfd));
817 pset = find_section_list (padd->name, false);
818 if (pset != NULL)
819 pset->used = true;
821 if (pset != NULL && pset->set_flags)
822 flags = pset->flags | SEC_HAS_CONTENTS;
823 else
824 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
826 if (! bfd_set_section_flags (obfd, padd->section, flags))
827 RETURN_NONFATAL (bfd_get_filename (obfd));
829 if (pset != NULL)
831 if (pset->change_vma != CHANGE_IGNORE)
832 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
833 RETURN_NONFATAL (bfd_get_filename (obfd));
835 if (pset->change_lma != CHANGE_IGNORE)
837 padd->section->lma = pset->lma_val;
839 if (! bfd_set_section_alignment
840 (obfd, padd->section,
841 bfd_section_alignment (obfd, padd->section)))
842 RETURN_NONFATAL (bfd_get_filename (obfd));
849 if (gap_fill_set || pad_to_set)
851 asection **set;
852 unsigned int c, i;
854 /* We must fill in gaps between the sections and/or we must pad
855 the last section to a specified address. We do this by
856 grabbing a list of the sections, sorting them by VMA, and
857 increasing the section sizes as required to fill the gaps.
858 We write out the gap contents below. */
860 c = bfd_count_sections (obfd);
861 osections = (asection **) xmalloc (c * sizeof (asection *));
862 set = osections;
863 bfd_map_over_sections (obfd, get_sections, (void *) &set);
865 qsort (osections, c, sizeof (asection *), compare_section_lma);
867 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
868 memset (gaps, 0, c * sizeof (bfd_size_type));
870 if (gap_fill_set)
872 for (i = 0; i < c - 1; i++)
874 flagword flags;
875 bfd_size_type size;
876 bfd_vma gap_start, gap_stop;
878 flags = bfd_get_section_flags (obfd, osections[i]);
879 if ((flags & SEC_HAS_CONTENTS) == 0
880 || (flags & SEC_LOAD) == 0)
881 continue;
883 size = bfd_section_size (obfd, osections[i]);
884 gap_start = bfd_section_lma (obfd, osections[i]) + size;
885 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
886 if (gap_start < gap_stop)
888 if (! bfd_set_section_size (obfd, osections[i],
889 size + (gap_stop - gap_start)))
891 non_fatal (_("Can't fill gap after %s: %s"),
892 bfd_get_section_name (obfd, osections[i]),
893 bfd_errmsg (bfd_get_error ()));
894 status = 1;
895 break;
897 gaps[i] = gap_stop - gap_start;
898 if (max_gap < gap_stop - gap_start)
899 max_gap = gap_stop - gap_start;
904 if (pad_to_set)
906 bfd_vma lma;
907 bfd_size_type size;
909 lma = bfd_section_lma (obfd, osections[c - 1]);
910 size = bfd_section_size (obfd, osections[c - 1]);
911 if (lma + size < pad_to)
913 if (! bfd_set_section_size (obfd, osections[c - 1],
914 pad_to - lma))
916 non_fatal (_("Can't add padding to %s: %s"),
917 bfd_get_section_name (obfd, osections[c - 1]),
918 bfd_errmsg (bfd_get_error ()));
919 status = 1;
921 else
923 gaps[c - 1] = pad_to - (lma + size);
924 if (max_gap < pad_to - (lma + size))
925 max_gap = pad_to - (lma + size);
931 /* Symbol filtering must happen after the output sections have
932 been created, but before their contents are set. */
933 dhandle = NULL;
934 symsize = bfd_get_symtab_upper_bound (ibfd);
935 if (symsize < 0)
936 RETURN_NONFATAL (bfd_get_filename (ibfd));
938 osympp = isympp = (asymbol **) xmalloc (symsize);
939 symcount = bfd_canonicalize_symtab (ibfd, isympp);
940 if (symcount < 0)
941 RETURN_NONFATAL (bfd_get_filename (ibfd));
943 if (convert_debugging)
944 dhandle = read_debugging_info (ibfd, isympp, symcount);
946 if (strip_symbols == STRIP_DEBUG
947 || strip_symbols == STRIP_ALL
948 || strip_symbols == STRIP_UNNEEDED
949 || discard_locals != LOCALS_UNDEF
950 || strip_specific_list != NULL
951 || keep_specific_list != NULL
952 || localize_specific_list != NULL
953 || weaken_specific_list != NULL
954 || sections_removed
955 || sections_copied
956 || convert_debugging
957 || change_leading_char
958 || remove_leading_char
959 || redefine_sym_list
960 || weaken)
962 /* Mark symbols used in output relocations so that they
963 are kept, even if they are local labels or static symbols.
965 Note we iterate over the input sections examining their
966 relocations since the relocations for the output sections
967 haven't been set yet. mark_symbols_used_in_relocations will
968 ignore input sections which have no corresponding output
969 section. */
970 if (strip_symbols != STRIP_ALL)
971 bfd_map_over_sections (ibfd,
972 mark_symbols_used_in_relocations,
973 (PTR)isympp);
974 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
975 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
978 if (convert_debugging && dhandle != NULL)
980 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
982 status = 1;
983 return;
987 bfd_set_symtab (obfd, osympp, symcount);
989 /* This has to happen after the symbol table has been set. */
990 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
992 if (add_sections != NULL)
994 struct section_add *padd;
996 for (padd = add_sections; padd != NULL; padd = padd->next)
998 if (! bfd_set_section_contents (obfd, padd->section,
999 (PTR) padd->contents,
1000 (file_ptr) 0,
1001 (bfd_size_type) padd->size))
1002 RETURN_NONFATAL (bfd_get_filename (obfd));
1006 if (gap_fill_set || pad_to_set)
1008 bfd_byte *buf;
1009 int c, i;
1011 /* Fill in the gaps. */
1013 if (max_gap > 8192)
1014 max_gap = 8192;
1015 buf = (bfd_byte *) xmalloc (max_gap);
1016 memset (buf, gap_fill, (size_t) max_gap);
1018 c = bfd_count_sections (obfd);
1019 for (i = 0; i < c; i++)
1021 if (gaps[i] != 0)
1023 bfd_size_type left;
1024 file_ptr off;
1026 left = gaps[i];
1027 off = bfd_section_size (obfd, osections[i]) - left;
1028 while (left > 0)
1030 bfd_size_type now;
1032 if (left > 8192)
1033 now = 8192;
1034 else
1035 now = left;
1037 if (! bfd_set_section_contents (obfd, osections[i], buf,
1038 off, now))
1039 RETURN_NONFATAL (bfd_get_filename (obfd));
1041 left -= now;
1042 off += now;
1048 /* Allow the BFD backend to copy any private data it understands
1049 from the input BFD to the output BFD. This is done last to
1050 permit the routine to look at the filtered symbol table, which is
1051 important for the ECOFF code at least. */
1052 if (!bfd_copy_private_bfd_data (ibfd, obfd))
1054 non_fatal (_("%s: error copying private BFD data: %s"),
1055 bfd_get_filename (obfd),
1056 bfd_errmsg (bfd_get_error ()));
1057 status = 1;
1058 return;
1062 /* Read each archive element in turn from IBFD, copy the
1063 contents to temp file, and keep the temp file handle. */
1065 static void
1066 copy_archive (ibfd, obfd, output_target)
1067 bfd *ibfd;
1068 bfd *obfd;
1069 const char *output_target;
1071 struct name_list
1073 struct name_list *next;
1074 char *name;
1075 bfd *obfd;
1076 } *list, *l;
1077 bfd **ptr = &obfd->archive_head;
1078 bfd *this_element;
1079 char *dir = make_tempname (bfd_get_filename (obfd));
1081 /* Make a temp directory to hold the contents. */
1082 #if defined (_WIN32) && !defined (__CYGWIN32__)
1083 if (mkdir (dir) != 0)
1084 #else
1085 if (mkdir (dir, 0700) != 0)
1086 #endif
1088 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1089 dir, strerror (errno));
1091 obfd->has_armap = ibfd->has_armap;
1093 list = NULL;
1095 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1096 while (!status && this_element != (bfd *) NULL)
1098 /* Create an output file for this member. */
1099 char *output_name = concat (dir, "/", bfd_get_filename (this_element),
1100 (char *) NULL);
1101 bfd *output_bfd = bfd_openw (output_name, output_target);
1102 bfd *last_element;
1103 struct stat buf;
1104 int stat_status = 0;
1106 if (preserve_dates)
1108 stat_status = bfd_stat_arch_elt (this_element, &buf);
1109 if (stat_status != 0)
1110 non_fatal (_("internal stat error on %s"),
1111 bfd_get_filename (this_element));
1114 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1115 l->name = output_name;
1116 l->next = list;
1117 list = l;
1119 if (output_bfd == (bfd *) NULL)
1120 RETURN_NONFATAL (output_name);
1122 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1123 RETURN_NONFATAL (bfd_get_filename (obfd));
1125 if (bfd_check_format (this_element, bfd_object) == true)
1126 copy_object (this_element, output_bfd);
1128 if (!bfd_close (output_bfd))
1130 bfd_nonfatal (bfd_get_filename (output_bfd));
1131 /* Error in new object file. Don't change archive. */
1132 status = 1;
1135 if (preserve_dates && stat_status == 0)
1136 set_times (output_name, &buf);
1138 /* Open the newly output file and attach to our list. */
1139 output_bfd = bfd_openr (output_name, output_target);
1141 l->obfd = output_bfd;
1143 *ptr = output_bfd;
1144 ptr = &output_bfd->next;
1146 last_element = this_element;
1148 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1150 bfd_close (last_element);
1152 *ptr = (bfd *) NULL;
1154 if (!bfd_close (obfd))
1155 RETURN_NONFATAL (bfd_get_filename (obfd));
1157 if (!bfd_close (ibfd))
1158 RETURN_NONFATAL (bfd_get_filename (ibfd));
1160 /* Delete all the files that we opened. */
1161 for (l = list; l != NULL; l = l->next)
1163 bfd_close (l->obfd);
1164 unlink (l->name);
1166 rmdir (dir);
1169 /* The top-level control. */
1171 static void
1172 copy_file (input_filename, output_filename, input_target, output_target)
1173 const char *input_filename;
1174 const char *output_filename;
1175 const char *input_target;
1176 const char *output_target;
1178 bfd *ibfd;
1179 char **matching;
1181 /* To allow us to do "strip *" without dying on the first
1182 non-object file, failures are nonfatal. */
1184 ibfd = bfd_openr (input_filename, input_target);
1185 if (ibfd == NULL)
1186 RETURN_NONFATAL (input_filename);
1188 if (bfd_check_format (ibfd, bfd_archive))
1190 bfd *obfd;
1192 /* bfd_get_target does not return the correct value until
1193 bfd_check_format succeeds. */
1194 if (output_target == NULL)
1195 output_target = bfd_get_target (ibfd);
1197 obfd = bfd_openw (output_filename, output_target);
1198 if (obfd == NULL)
1199 RETURN_NONFATAL (output_filename);
1201 copy_archive (ibfd, obfd, output_target);
1203 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1205 bfd *obfd;
1207 /* bfd_get_target does not return the correct value until
1208 bfd_check_format succeeds. */
1209 if (output_target == NULL)
1210 output_target = bfd_get_target (ibfd);
1212 obfd = bfd_openw (output_filename, output_target);
1213 if (obfd == NULL)
1214 RETURN_NONFATAL (output_filename);
1216 copy_object (ibfd, obfd);
1218 if (!bfd_close (obfd))
1219 RETURN_NONFATAL (output_filename);
1221 if (!bfd_close (ibfd))
1222 RETURN_NONFATAL (input_filename);
1224 else
1226 bfd_nonfatal (input_filename);
1228 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1230 list_matching_formats (matching);
1231 free (matching);
1234 status = 1;
1238 /* Create a section in OBFD with the same name and attributes
1239 as ISECTION in IBFD. */
1241 static void
1242 setup_section (ibfd, isection, obfdarg)
1243 bfd *ibfd;
1244 sec_ptr isection;
1245 PTR obfdarg;
1247 bfd *obfd = (bfd *) obfdarg;
1248 struct section_list *p;
1249 sec_ptr osection;
1250 bfd_size_type size;
1251 bfd_vma vma;
1252 bfd_vma lma;
1253 flagword flags;
1254 const char *err;
1256 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1257 && (strip_symbols == STRIP_DEBUG
1258 || strip_symbols == STRIP_UNNEEDED
1259 || strip_symbols == STRIP_ALL
1260 || discard_locals == LOCALS_ALL
1261 || convert_debugging))
1262 return;
1264 p = find_section_list (bfd_section_name (ibfd, isection), false);
1265 if (p != NULL)
1266 p->used = true;
1268 if (sections_removed && p != NULL && p->remove)
1269 return;
1270 if (sections_copied && (p == NULL || ! p->copy))
1271 return;
1273 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1275 if (osection == NULL)
1277 err = _("making");
1278 goto loser;
1281 size = bfd_section_size (ibfd, isection);
1282 if (copy_byte >= 0)
1283 size = (size + interleave - 1) / interleave;
1284 if (! bfd_set_section_size (obfd, osection, size))
1286 err = _("size");
1287 goto loser;
1290 vma = bfd_section_vma (ibfd, isection);
1291 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1292 vma += p->vma_val;
1293 else if (p != NULL && p->change_vma == CHANGE_SET)
1294 vma = p->vma_val;
1295 else
1296 vma += change_section_address;
1298 if (! bfd_set_section_vma (obfd, osection, vma))
1300 err = _("vma");
1301 goto loser;
1304 lma = isection->lma;
1305 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1307 if (p->change_lma == CHANGE_MODIFY)
1308 lma += p->lma_val;
1309 else if (p->change_lma == CHANGE_SET)
1310 lma = p->lma_val;
1311 else
1312 abort ();
1314 else
1315 lma += change_section_address;
1317 osection->lma = lma;
1319 /* FIXME: This is probably not enough. If we change the LMA we
1320 may have to recompute the header for the file as well. */
1321 if (bfd_set_section_alignment (obfd,
1322 osection,
1323 bfd_section_alignment (ibfd, isection))
1324 == false)
1326 err = _("alignment");
1327 goto loser;
1330 flags = bfd_get_section_flags (ibfd, isection);
1331 if (p != NULL && p->set_flags)
1332 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1333 if (!bfd_set_section_flags (obfd, osection, flags))
1335 err = _("flags");
1336 goto loser;
1339 /* This used to be mangle_section; we do here to avoid using
1340 bfd_get_section_by_name since some formats allow multiple
1341 sections with the same name. */
1342 isection->output_section = osection;
1343 isection->output_offset = 0;
1345 /* Allow the BFD backend to copy any private data it understands
1346 from the input section to the output section. */
1347 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1349 err = _("private data");
1350 goto loser;
1353 /* All went well */
1354 return;
1356 loser:
1357 non_fatal (_("%s: section `%s': error in %s: %s"),
1358 bfd_get_filename (ibfd),
1359 bfd_section_name (ibfd, isection),
1360 err, bfd_errmsg (bfd_get_error ()));
1361 status = 1;
1364 /* Copy the data of input section ISECTION of IBFD
1365 to an output section with the same name in OBFD.
1366 If stripping then don't copy any relocation info. */
1368 static void
1369 copy_section (ibfd, isection, obfdarg)
1370 bfd *ibfd;
1371 sec_ptr isection;
1372 PTR obfdarg;
1374 bfd *obfd = (bfd *) obfdarg;
1375 struct section_list *p;
1376 arelent **relpp;
1377 long relcount;
1378 sec_ptr osection;
1379 bfd_size_type size;
1380 long relsize;
1382 /* If we have already failed earlier on, do not keep on generating
1383 complaints now. */
1384 if (status != 0)
1385 return;
1387 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1388 && (strip_symbols == STRIP_DEBUG
1389 || strip_symbols == STRIP_UNNEEDED
1390 || strip_symbols == STRIP_ALL
1391 || discard_locals == LOCALS_ALL
1392 || convert_debugging))
1394 return;
1397 p = find_section_list (bfd_section_name (ibfd, isection), false);
1399 if (sections_removed && p != NULL && p->remove)
1400 return;
1401 if (sections_copied && (p == NULL || ! p->copy))
1402 return;
1404 osection = isection->output_section;
1405 size = bfd_get_section_size_before_reloc (isection);
1407 if (size == 0 || osection == 0)
1408 return;
1411 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1412 if (relsize < 0)
1413 RETURN_NONFATAL (bfd_get_filename (ibfd));
1415 if (relsize == 0)
1416 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1417 else
1419 relpp = (arelent **) xmalloc (relsize);
1420 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1421 if (relcount < 0)
1422 RETURN_NONFATAL (bfd_get_filename (ibfd));
1424 if (strip_symbols == STRIP_ALL)
1426 /* Remove relocations which are not in
1427 keep_strip_specific_list. */
1428 arelent **temp_relpp;
1429 long temp_relcount = 0;
1430 long i;
1432 temp_relpp = (arelent **) xmalloc (relsize);
1433 for (i = 0; i < relcount; i++)
1434 if (is_specified_symbol
1435 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1436 keep_specific_list))
1437 temp_relpp [temp_relcount++] = relpp [i];
1438 relcount = temp_relcount;
1439 free (relpp);
1440 relpp = temp_relpp;
1442 bfd_set_reloc (obfd, osection,
1443 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1446 isection->_cooked_size = isection->_raw_size;
1447 isection->reloc_done = true;
1449 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1451 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1453 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1454 size))
1455 RETURN_NONFATAL (bfd_get_filename (ibfd));
1457 if (copy_byte >= 0)
1458 filter_bytes (memhunk, &size);
1460 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1461 size))
1462 RETURN_NONFATAL (bfd_get_filename (obfd));
1464 free (memhunk);
1466 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1468 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1470 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1471 flag--they can just remove the section entirely and add it
1472 back again. However, we do permit them to turn on the
1473 SEC_HAS_CONTENTS flag, and take it to mean that the section
1474 contents should be zeroed out. */
1476 memset (memhunk, 0, size);
1477 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1478 size))
1479 RETURN_NONFATAL (bfd_get_filename (obfd));
1480 free (memhunk);
1484 /* Get all the sections. This is used when --gap-fill or --pad-to is
1485 used. */
1487 static void
1488 get_sections (obfd, osection, secppparg)
1489 bfd *obfd ATTRIBUTE_UNUSED;
1490 asection *osection;
1491 PTR secppparg;
1493 asection ***secppp = (asection ***) secppparg;
1495 **secppp = osection;
1496 ++(*secppp);
1499 /* Sort sections by VMA. This is called via qsort, and is used when
1500 --gap-fill or --pad-to is used. We force non loadable or empty
1501 sections to the front, where they are easier to ignore. */
1503 static int
1504 compare_section_lma (arg1, arg2)
1505 const PTR arg1;
1506 const PTR arg2;
1508 const asection **sec1 = (const asection **) arg1;
1509 const asection **sec2 = (const asection **) arg2;
1510 flagword flags1, flags2;
1512 /* Sort non loadable sections to the front. */
1513 flags1 = (*sec1)->flags;
1514 flags2 = (*sec2)->flags;
1515 if ((flags1 & SEC_HAS_CONTENTS) == 0
1516 || (flags1 & SEC_LOAD) == 0)
1518 if ((flags2 & SEC_HAS_CONTENTS) != 0
1519 && (flags2 & SEC_LOAD) != 0)
1520 return -1;
1522 else
1524 if ((flags2 & SEC_HAS_CONTENTS) == 0
1525 || (flags2 & SEC_LOAD) == 0)
1526 return 1;
1529 /* Sort sections by LMA. */
1530 if ((*sec1)->lma > (*sec2)->lma)
1531 return 1;
1532 else if ((*sec1)->lma < (*sec2)->lma)
1533 return -1;
1535 /* Sort sections with the same LMA by size. */
1536 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1537 return 1;
1538 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1539 return -1;
1541 return 0;
1544 /* Mark all the symbols which will be used in output relocations with
1545 the BSF_KEEP flag so that those symbols will not be stripped.
1547 Ignore relocations which will not appear in the output file. */
1549 static void
1550 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1551 bfd *ibfd;
1552 sec_ptr isection;
1553 PTR symbolsarg;
1555 asymbol **symbols = (asymbol **) symbolsarg;
1556 long relsize;
1557 arelent **relpp;
1558 long relcount, i;
1560 /* Ignore an input section with no corresponding output section. */
1561 if (isection->output_section == NULL)
1562 return;
1564 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1565 if (relsize < 0)
1566 bfd_fatal (bfd_get_filename (ibfd));
1568 if (relsize == 0)
1569 return;
1571 relpp = (arelent **) xmalloc (relsize);
1572 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1573 if (relcount < 0)
1574 bfd_fatal (bfd_get_filename (ibfd));
1576 /* Examine each symbol used in a relocation. If it's not one of the
1577 special bfd section symbols, then mark it with BSF_KEEP. */
1578 for (i = 0; i < relcount; i++)
1580 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1581 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1582 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1583 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1586 if (relpp != NULL)
1587 free (relpp);
1590 /* Write out debugging information. */
1592 static boolean
1593 write_debugging_info (obfd, dhandle, symcountp, symppp)
1594 bfd *obfd;
1595 PTR dhandle;
1596 long *symcountp ATTRIBUTE_UNUSED;
1597 asymbol ***symppp ATTRIBUTE_UNUSED;
1599 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1600 return write_ieee_debugging_info (obfd, dhandle);
1602 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1603 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1605 bfd_byte *syms, *strings;
1606 bfd_size_type symsize, stringsize;
1607 asection *stabsec, *stabstrsec;
1609 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1610 &symsize, &strings,
1611 &stringsize))
1612 return false;
1614 stabsec = bfd_make_section (obfd, ".stab");
1615 stabstrsec = bfd_make_section (obfd, ".stabstr");
1616 if (stabsec == NULL
1617 || stabstrsec == NULL
1618 || ! bfd_set_section_size (obfd, stabsec, symsize)
1619 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1620 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1621 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1622 || ! bfd_set_section_flags (obfd, stabsec,
1623 (SEC_HAS_CONTENTS
1624 | SEC_READONLY
1625 | SEC_DEBUGGING))
1626 || ! bfd_set_section_flags (obfd, stabstrsec,
1627 (SEC_HAS_CONTENTS
1628 | SEC_READONLY
1629 | SEC_DEBUGGING)))
1631 non_fatal (_("%s: can't create debugging section: %s"),
1632 bfd_get_filename (obfd),
1633 bfd_errmsg (bfd_get_error ()));
1634 return false;
1637 /* We can get away with setting the section contents now because
1638 the next thing the caller is going to do is copy over the
1639 real sections. We may someday have to split the contents
1640 setting out of this function. */
1641 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1642 symsize)
1643 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1644 (file_ptr) 0, stringsize))
1646 non_fatal (_("%s: can't set debugging section contents: %s"),
1647 bfd_get_filename (obfd),
1648 bfd_errmsg (bfd_get_error ()));
1649 return false;
1652 return true;
1655 non_fatal (_("%s: don't know how to write debugging information for %s"),
1656 bfd_get_filename (obfd), bfd_get_target (obfd));
1657 return false;
1660 static int
1661 strip_main (argc, argv)
1662 int argc;
1663 char *argv[];
1665 char *input_target = NULL, *output_target = NULL;
1666 boolean show_version = false;
1667 int c, i;
1668 struct section_list *p;
1669 char *output_file = NULL;
1671 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv",
1672 strip_options, (int *) 0)) != EOF)
1674 switch (c)
1676 case 'I':
1677 input_target = optarg;
1678 break;
1679 case 'O':
1680 output_target = optarg;
1681 break;
1682 case 'F':
1683 input_target = output_target = optarg;
1684 break;
1685 case 'R':
1686 p = find_section_list (optarg, true);
1687 p->remove = true;
1688 sections_removed = true;
1689 break;
1690 case 's':
1691 strip_symbols = STRIP_ALL;
1692 break;
1693 case 'S':
1694 case 'g':
1695 strip_symbols = STRIP_DEBUG;
1696 break;
1697 case OPTION_STRIP_UNNEEDED:
1698 strip_symbols = STRIP_UNNEEDED;
1699 break;
1700 case 'K':
1701 add_specific_symbol (optarg, &keep_specific_list);
1702 break;
1703 case 'N':
1704 add_specific_symbol (optarg, &strip_specific_list);
1705 break;
1706 case 'o':
1707 output_file = optarg;
1708 break;
1709 case 'p':
1710 preserve_dates = true;
1711 break;
1712 case 'x':
1713 discard_locals = LOCALS_ALL;
1714 break;
1715 case 'X':
1716 discard_locals = LOCALS_START_L;
1717 break;
1718 case 'v':
1719 verbose = true;
1720 break;
1721 case 'V':
1722 show_version = true;
1723 break;
1724 case 0:
1725 break; /* we've been given a long option */
1726 case 'h':
1727 strip_usage (stdout, 0);
1728 default:
1729 strip_usage (stderr, 1);
1733 if (show_version)
1734 print_version ("strip");
1736 /* Default is to strip all symbols. */
1737 if (strip_symbols == STRIP_UNDEF
1738 && discard_locals == LOCALS_UNDEF
1739 && strip_specific_list == NULL)
1740 strip_symbols = STRIP_ALL;
1742 if (output_target == (char *) NULL)
1743 output_target = input_target;
1745 i = optind;
1746 if (i == argc
1747 || (output_file != NULL && (i + 1) < argc))
1748 strip_usage (stderr, 1);
1750 for (; i < argc; i++)
1752 int hold_status = status;
1753 struct stat statbuf;
1754 char *tmpname;
1756 if (preserve_dates)
1758 if (stat (argv[i], &statbuf) < 0)
1760 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
1761 continue;
1765 if (output_file != NULL)
1766 tmpname = output_file;
1767 else
1768 tmpname = make_tempname (argv[i]);
1769 status = 0;
1771 copy_file (argv[i], tmpname, input_target, output_target);
1772 if (status == 0)
1774 if (preserve_dates)
1775 set_times (tmpname, &statbuf);
1776 if (output_file == NULL)
1777 smart_rename (tmpname, argv[i], preserve_dates);
1778 status = hold_status;
1780 else
1781 unlink (tmpname);
1782 if (output_file == NULL)
1783 free (tmpname);
1786 return 0;
1789 static int
1790 copy_main (argc, argv)
1791 int argc;
1792 char *argv[];
1794 char *input_filename = NULL, *output_filename = NULL;
1795 char *input_target = NULL, *output_target = NULL;
1796 boolean show_version = false;
1797 boolean change_warn = true;
1798 int c;
1799 struct section_list *p;
1800 struct stat statbuf;
1802 while ((c = getopt_long (argc, argv, "b:i:I:j:K:N:s:O:d:F:L:R:SpgxXVvW:",
1803 copy_options, (int *) 0)) != EOF)
1805 switch (c)
1807 case 'b':
1808 copy_byte = atoi (optarg);
1809 if (copy_byte < 0)
1810 fatal (_("byte number must be non-negative"));
1811 break;
1813 case 'i':
1814 interleave = atoi (optarg);
1815 if (interleave < 1)
1816 fatal (_("interleave must be positive"));
1817 break;
1819 case 'I':
1820 case 's': /* "source" - 'I' is preferred */
1821 input_target = optarg;
1822 break;
1824 case 'O':
1825 case 'd': /* "destination" - 'O' is preferred */
1826 output_target = optarg;
1827 break;
1829 case 'F':
1830 input_target = output_target = optarg;
1831 break;
1833 case 'j':
1834 p = find_section_list (optarg, true);
1835 if (p->remove)
1836 fatal (_("%s both copied and removed"), optarg);
1837 p->copy = true;
1838 sections_copied = true;
1839 break;
1841 case 'R':
1842 p = find_section_list (optarg, true);
1843 if (p->copy)
1844 fatal (_("%s both copied and removed"), optarg);
1845 p->remove = true;
1846 sections_removed = true;
1847 break;
1849 case 'S':
1850 strip_symbols = STRIP_ALL;
1851 break;
1853 case 'g':
1854 strip_symbols = STRIP_DEBUG;
1855 break;
1857 case OPTION_STRIP_UNNEEDED:
1858 strip_symbols = STRIP_UNNEEDED;
1859 break;
1861 case 'K':
1862 add_specific_symbol (optarg, &keep_specific_list);
1863 break;
1865 case 'N':
1866 add_specific_symbol (optarg, &strip_specific_list);
1867 break;
1869 case 'L':
1870 add_specific_symbol (optarg, &localize_specific_list);
1871 break;
1873 case 'W':
1874 add_specific_symbol (optarg, &weaken_specific_list);
1875 break;
1877 case 'p':
1878 preserve_dates = true;
1879 break;
1881 case 'x':
1882 discard_locals = LOCALS_ALL;
1883 break;
1885 case 'X':
1886 discard_locals = LOCALS_START_L;
1887 break;
1889 case 'v':
1890 verbose = true;
1891 break;
1893 case 'V':
1894 show_version = true;
1895 break;
1897 case OPTION_WEAKEN:
1898 weaken = true;
1899 break;
1901 case OPTION_ADD_SECTION:
1903 const char *s;
1904 struct stat st;
1905 struct section_add *pa;
1906 int len;
1907 char *name;
1908 FILE *f;
1910 s = strchr (optarg, '=');
1912 if (s == NULL)
1913 fatal (_("bad format for %s"), "--add-section");
1915 if (stat (s + 1, & st) < 0)
1916 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
1918 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1920 len = s - optarg;
1921 name = (char *) xmalloc (len + 1);
1922 strncpy (name, optarg, len);
1923 name[len] = '\0';
1924 pa->name = name;
1926 pa->filename = s + 1;
1928 pa->size = st.st_size;
1930 pa->contents = (bfd_byte *) xmalloc (pa->size);
1931 f = fopen (pa->filename, FOPEN_RB);
1933 if (f == NULL)
1934 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
1936 if (fread (pa->contents, 1, pa->size, f) == 0
1937 || ferror (f))
1938 fatal (_("%s: fread failed"), pa->filename);
1940 fclose (f);
1942 pa->next = add_sections;
1943 add_sections = pa;
1945 break;
1947 case OPTION_CHANGE_START:
1948 change_start = parse_vma (optarg, "--change-start");
1949 break;
1951 case OPTION_CHANGE_SECTION_ADDRESS:
1952 case OPTION_CHANGE_SECTION_LMA:
1953 case OPTION_CHANGE_SECTION_VMA:
1955 const char *s;
1956 int len;
1957 char *name;
1958 char *option = NULL;
1959 bfd_vma val;
1960 enum change_action what = CHANGE_IGNORE;
1962 switch (c)
1964 case OPTION_CHANGE_SECTION_ADDRESS:
1965 option = "--change-section-address";
1966 break;
1967 case OPTION_CHANGE_SECTION_LMA:
1968 option = "--change-section-lma";
1969 break;
1970 case OPTION_CHANGE_SECTION_VMA:
1971 option = "--change-section-vma";
1972 break;
1975 s = strchr (optarg, '=');
1976 if (s == NULL)
1978 s = strchr (optarg, '+');
1979 if (s == NULL)
1981 s = strchr (optarg, '-');
1982 if (s == NULL)
1983 fatal (_("bad format for %s"), option);
1987 len = s - optarg;
1988 name = (char *) xmalloc (len + 1);
1989 strncpy (name, optarg, len);
1990 name[len] = '\0';
1992 p = find_section_list (name, true);
1994 val = parse_vma (s + 1, option);
1996 switch (*s)
1998 case '=': what = CHANGE_SET; break;
1999 case '-': val = - val; /* Drop through. */
2000 case '+': what = CHANGE_MODIFY; break;
2003 switch (c)
2005 case OPTION_CHANGE_SECTION_ADDRESS:
2006 p->change_vma = what;
2007 p->vma_val = val;
2008 /* Drop through. */
2010 case OPTION_CHANGE_SECTION_LMA:
2011 p->change_lma = what;
2012 p->lma_val = val;
2013 break;
2015 case OPTION_CHANGE_SECTION_VMA:
2016 p->change_vma = what;
2017 p->vma_val = val;
2018 break;
2021 break;
2023 case OPTION_CHANGE_ADDRESSES:
2024 change_section_address = parse_vma (optarg, "--change-addresses");
2025 change_start = change_section_address;
2026 break;
2028 case OPTION_CHANGE_WARNINGS:
2029 change_warn = true;
2030 break;
2032 case OPTION_CHANGE_LEADING_CHAR:
2033 change_leading_char = true;
2034 break;
2036 case OPTION_DEBUGGING:
2037 convert_debugging = true;
2038 break;
2040 case OPTION_GAP_FILL:
2042 bfd_vma gap_fill_vma;
2044 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2045 gap_fill = (bfd_byte) gap_fill_vma;
2046 if ((bfd_vma) gap_fill != gap_fill_vma)
2048 char buff[20];
2050 sprintf_vma (buff, gap_fill_vma);
2052 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2053 buff, gap_fill);
2055 gap_fill_set = true;
2057 break;
2059 case OPTION_NO_CHANGE_WARNINGS:
2060 change_warn = false;
2061 break;
2063 case OPTION_PAD_TO:
2064 pad_to = parse_vma (optarg, "--pad-to");
2065 pad_to_set = true;
2066 break;
2068 case OPTION_REMOVE_LEADING_CHAR:
2069 remove_leading_char = true;
2070 break;
2072 case OPTION_REDEFINE_SYM:
2074 /* Push this redefinition onto redefine_symbol_list. */
2076 int len;
2077 const char *s;
2078 const char *nextarg;
2079 char *source, *target;
2081 s = strchr (optarg, '=');
2082 if (s == NULL)
2084 fatal (_("bad format for %s"), "--redefine-sym");
2087 len = s - optarg;
2088 source = (char *) xmalloc (len + 1);
2089 strncpy (source, optarg, len);
2090 source[len] = '\0';
2092 nextarg = s + 1;
2093 len = strlen (nextarg);
2094 target = (char *) xmalloc (len + 1);
2095 strcpy (target, nextarg);
2097 redefine_list_append (source, target);
2099 free (source);
2100 free (target);
2102 break;
2104 case OPTION_SET_SECTION_FLAGS:
2106 const char *s;
2107 int len;
2108 char *name;
2110 s = strchr (optarg, '=');
2111 if (s == NULL)
2112 fatal (_("bad format for %s"), "--set-section-flags");
2114 len = s - optarg;
2115 name = (char *) xmalloc (len + 1);
2116 strncpy (name, optarg, len);
2117 name[len] = '\0';
2119 p = find_section_list (name, true);
2121 p->set_flags = true;
2122 p->flags = parse_flags (s + 1);
2124 break;
2126 case OPTION_SET_START:
2127 set_start = parse_vma (optarg, "--set-start");
2128 set_start_set = true;
2129 break;
2131 case 0:
2132 break; /* we've been given a long option */
2134 case 'h':
2135 copy_usage (stdout, 0);
2137 default:
2138 copy_usage (stderr, 1);
2142 if (show_version)
2143 print_version ("objcopy");
2145 if (copy_byte >= interleave)
2146 fatal (_("byte number must be less than interleave"));
2148 if (optind == argc || optind + 2 < argc)
2149 copy_usage (stderr, 1);
2151 input_filename = argv[optind];
2152 if (optind + 1 < argc)
2153 output_filename = argv[optind + 1];
2155 /* Default is to strip no symbols. */
2156 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2157 strip_symbols = STRIP_NONE;
2159 if (output_target == (char *) NULL)
2160 output_target = input_target;
2162 if (preserve_dates)
2164 if (stat (input_filename, &statbuf) < 0)
2165 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2168 /* If there is no destination file then create a temp and rename
2169 the result into the input. */
2171 if (output_filename == (char *) NULL)
2173 char *tmpname = make_tempname (input_filename);
2175 copy_file (input_filename, tmpname, input_target, output_target);
2176 if (status == 0)
2178 if (preserve_dates)
2179 set_times (tmpname, &statbuf);
2180 smart_rename (tmpname, input_filename, preserve_dates);
2182 else
2183 unlink (tmpname);
2185 else
2187 copy_file (input_filename, output_filename, input_target, output_target);
2188 if (status == 0 && preserve_dates)
2189 set_times (output_filename, &statbuf);
2192 if (change_warn)
2194 for (p = change_sections; p != NULL; p = p->next)
2196 if (! p->used)
2198 if (p->change_vma != CHANGE_IGNORE)
2200 char buff [20];
2202 sprintf_vma (buff, p->vma_val);
2204 /* xgettext:c-format */
2205 non_fatal (_("%s %s%c0x%s never used"),
2206 "--change-section-vma",
2207 p->name,
2208 p->change_vma == CHANGE_SET ? '=' : '+',
2209 buff);
2212 if (p->change_lma != CHANGE_IGNORE)
2214 char buff [20];
2216 sprintf_vma (buff, p->lma_val);
2218 /* xgettext:c-format */
2219 non_fatal (_("%s %s%c0x%s never used"),
2220 "--change-section-lma",
2221 p->name,
2222 p->change_lma == CHANGE_SET ? '=' : '+',
2223 buff);
2229 return 0;
2233 main (argc, argv)
2234 int argc;
2235 char *argv[];
2237 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2238 setlocale (LC_MESSAGES, "");
2239 #endif
2240 bindtextdomain (PACKAGE, LOCALEDIR);
2241 textdomain (PACKAGE);
2243 program_name = argv[0];
2244 xmalloc_set_program_name (program_name);
2246 START_PROGRESS (program_name, 0);
2248 strip_symbols = STRIP_UNDEF;
2249 discard_locals = LOCALS_UNDEF;
2251 bfd_init ();
2252 set_default_bfd_target ();
2254 if (is_strip < 0)
2256 int i = strlen (program_name);
2257 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2258 /* Drop the .exe suffix, if any. */
2259 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
2261 i -= 4;
2262 program_name[i] = '\0';
2264 #endif
2265 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
2268 if (is_strip)
2269 strip_main (argc, argv);
2270 else
2271 copy_main (argc, argv);
2273 END_PROGRESS (program_name);
2275 return status;