1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "libiberty.h"
29 #include "filenames.h"
32 /* A list of symbols to explicitly strip out, or to keep. A linked
33 list is good enough for a small number from the command line, but
34 this will slow things down a lot if many symbols are being
43 /* A list to support redefine_sym. */
48 struct redefine_node *next;
51 typedef struct section_rename
53 const char * old_name;
54 const char * new_name;
56 struct section_rename * next;
60 /* List of sections to be renamed. */
61 static section_rename * section_rename_list;
63 static void copy_usage PARAMS ((FILE *, int));
64 static void strip_usage PARAMS ((FILE *, int));
65 static flagword parse_flags PARAMS ((const char *));
66 static struct section_list *find_section_list PARAMS ((const char *, boolean));
67 static void setup_section PARAMS ((bfd *, asection *, PTR));
68 static void copy_section PARAMS ((bfd *, asection *, PTR));
69 static void get_sections PARAMS ((bfd *, asection *, PTR));
70 static int compare_section_lma PARAMS ((const PTR, const PTR));
71 static void add_specific_symbol PARAMS ((const char *, struct symlist **));
72 static void add_specific_symbols PARAMS ((const char *, struct symlist **));
73 static boolean is_specified_symbol PARAMS ((const char *, struct symlist *));
74 static boolean is_strip_section PARAMS ((bfd *, asection *));
75 static unsigned int filter_symbols
76 PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long));
77 static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
78 static void filter_bytes PARAMS ((char *, bfd_size_type *));
79 static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***));
80 static void copy_object PARAMS ((bfd *, bfd *));
81 static void copy_archive PARAMS ((bfd *, bfd *, const char *));
83 PARAMS ((const char *, const char *, const char *, const char *));
84 static int strip_main PARAMS ((int, char **));
85 static int copy_main PARAMS ((int, char **));
86 static const char *lookup_sym_redefinition PARAMS((const char *));
87 static void redefine_list_append PARAMS ((const char *, const char *));
88 static const char * find_section_rename PARAMS ((bfd *, sec_ptr, flagword *));
89 static void add_section_rename PARAMS ((const char *, const char *, flagword));
91 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
93 static asymbol **isympp = NULL; /* Input symbols */
94 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
96 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
97 static int copy_byte = -1;
98 static int interleave = 4;
100 static boolean verbose; /* Print file and target names. */
101 static boolean preserve_dates; /* Preserve input file timestamp. */
102 static int status = 0; /* Exit status. */
107 STRIP_NONE, /* don't strip */
108 STRIP_DEBUG, /* strip all debugger symbols */
109 STRIP_UNNEEDED, /* strip unnecessary symbols */
110 STRIP_ALL /* strip all symbols */
113 /* Which symbols to remove. */
114 static enum strip_action strip_symbols;
119 LOCALS_START_L, /* discard locals starting with L */
120 LOCALS_ALL /* discard all locals */
123 /* Which local symbols to remove. Overrides STRIP_ALL. */
124 static enum locals_action discard_locals;
126 /* What kind of change to perform. */
134 /* Structure used to hold lists of sections and actions to take. */
137 struct section_list * next; /* Next section to change. */
138 const char * name; /* Section name. */
139 boolean used; /* Whether this entry was used. */
140 boolean remove; /* Whether to remove this section. */
141 boolean copy; /* Whether to copy this section. */
142 enum change_action change_vma;/* Whether to change or set VMA. */
143 bfd_vma vma_val; /* Amount to change by or set to. */
144 enum change_action change_lma;/* Whether to change or set LMA. */
145 bfd_vma lma_val; /* Amount to change by or set to. */
146 boolean set_flags; /* Whether to set the section flags. */
147 flagword flags; /* What to set the section flags to. */
150 static struct section_list *change_sections;
152 /* True if some sections are to be removed. */
153 static boolean sections_removed;
155 /* True if only some sections are to be copied. */
156 static boolean sections_copied;
158 /* Changes to the start address. */
159 static bfd_vma change_start = 0;
160 static boolean set_start_set = false;
161 static bfd_vma set_start;
163 /* Changes to section addresses. */
164 static bfd_vma change_section_address = 0;
166 /* Filling gaps between sections. */
167 static boolean gap_fill_set = false;
168 static bfd_byte gap_fill = 0;
170 /* Pad to a given address. */
171 static boolean pad_to_set = false;
172 static bfd_vma pad_to;
174 /* List of sections to add. */
177 /* Next section to add. */
178 struct section_add *next;
179 /* Name of section to add. */
181 /* Name of file holding section contents. */
182 const char *filename;
185 /* Contents of file. */
187 /* BFD section, after it has been added. */
191 /* List of sections to add to the output BFD. */
192 static struct section_add *add_sections;
194 /* Whether to convert debugging information. */
195 static boolean convert_debugging = false;
197 /* Whether to change the leading character in symbol names. */
198 static boolean change_leading_char = false;
200 /* Whether to remove the leading character from global symbol names. */
201 static boolean remove_leading_char = false;
203 /* List of symbols to strip, keep, localize, keep-global, weaken,
205 static struct symlist *strip_specific_list = NULL;
206 static struct symlist *keep_specific_list = NULL;
207 static struct symlist *localize_specific_list = NULL;
208 static struct symlist *keepglobal_specific_list = NULL;
209 static struct symlist *weaken_specific_list = NULL;
210 static struct redefine_node *redefine_sym_list = NULL;
212 /* If this is true, we weaken global symbols (set BSF_WEAK). */
213 static boolean weaken = false;
215 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
217 #define OPTION_ADD_SECTION 150
218 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
219 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
220 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
221 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
222 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
223 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
224 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
225 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
226 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
227 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
228 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
229 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
230 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
231 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
232 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
233 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
234 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
235 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYM + 1)
236 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
237 #define OPTION_STRIP_SYMBOLS (OPTION_SREC_FORCES3 + 1)
238 #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1)
239 #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
240 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
241 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
242 #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
244 /* Options to handle if running as "strip". */
246 static struct option strip_options[] =
248 {"discard-all", no_argument, 0, 'x'},
249 {"discard-locals", no_argument, 0, 'X'},
250 {"format", required_argument, 0, 'F'}, /* Obsolete */
251 {"help", no_argument, 0, 'h'},
252 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
253 {"input-target", required_argument, 0, 'I'},
254 {"keep-symbol", required_argument, 0, 'K'},
255 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
256 {"output-target", required_argument, 0, 'O'},
257 {"output-file", required_argument, 0, 'o'},
258 {"preserve-dates", no_argument, 0, 'p'},
259 {"remove-section", required_argument, 0, 'R'},
260 {"strip-all", no_argument, 0, 's'},
261 {"strip-debug", no_argument, 0, 'S'},
262 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
263 {"strip-symbol", required_argument, 0, 'N'},
264 {"target", required_argument, 0, 'F'},
265 {"verbose", no_argument, 0, 'v'},
266 {"version", no_argument, 0, 'V'},
267 {0, no_argument, 0, 0}
270 /* Options to handle if running as "objcopy". */
272 static struct option copy_options[] =
274 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
275 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
276 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
277 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
278 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
279 {"binary-architecture", required_argument, 0, 'B'},
280 {"byte", required_argument, 0, 'b'},
281 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
282 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
283 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
284 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
285 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
286 {"change-start", required_argument, 0, OPTION_CHANGE_START},
287 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
288 {"debugging", no_argument, 0, OPTION_DEBUGGING},
289 {"discard-all", no_argument, 0, 'x'},
290 {"discard-locals", no_argument, 0, 'X'},
291 {"only-section", required_argument, 0, 'j'},
292 {"format", required_argument, 0, 'F'}, /* Obsolete */
293 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
294 {"help", no_argument, 0, 'h'},
295 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
296 {"input-target", required_argument, 0, 'I'},
297 {"interleave", required_argument, 0, 'i'},
298 {"keep-symbol", required_argument, 0, 'K'},
299 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
300 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
301 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
302 {"output-target", required_argument, 0, 'O'},
303 {"pad-to", required_argument, 0, OPTION_PAD_TO},
304 {"preserve-dates", no_argument, 0, 'p'},
305 {"localize-symbol", required_argument, 0, 'L'},
306 {"keep-global-symbol", required_argument, 0, 'G'},
307 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
308 {"remove-section", required_argument, 0, 'R'},
309 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
310 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
311 {"set-start", required_argument, 0, OPTION_SET_START},
312 {"strip-all", no_argument, 0, 'S'},
313 {"strip-debug", no_argument, 0, 'g'},
314 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
315 {"strip-symbol", required_argument, 0, 'N'},
316 {"target", required_argument, 0, 'F'},
317 {"verbose", no_argument, 0, 'v'},
318 {"version", no_argument, 0, 'V'},
319 {"weaken", no_argument, 0, OPTION_WEAKEN},
320 {"weaken-symbol", required_argument, 0, 'W'},
321 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
322 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
323 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
324 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
325 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
326 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
327 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
328 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
329 {0, no_argument, 0, 0}
333 extern char *program_name;
335 /* This flag distinguishes between strip and objcopy:
336 1 means this is 'strip'; 0 means this is 'objcopy'.
337 -1 means if we should use argv[0] to decide. */
340 /* The maximum length of an S record. This variable is declared in srec.c
341 and can be modified by the --srec-len parameter. */
342 extern unsigned int Chunk;
344 /* Restrict the generation of Srecords to type S3 only.
345 This variable is declare in bfd/srec.c and can be toggled
346 on by the --srec-forceS3 command line switch. */
347 extern boolean S3Forced;
349 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
350 extern enum bfd_architecture bfd_external_binary_architecture;
354 copy_usage (stream, exit_status)
358 fprintf (stream, _("Usage: %s <switches> in-file [out-file]\n"), program_name);
359 fprintf (stream, _(" The switches are:\n"));
360 fprintf (stream, _("\
361 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
362 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
363 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
364 -F --target <bfdname> Set both input and output format to <bfdname>\n\
365 --debugging Convert debugging information, if possible\n\
366 -p --preserve-dates Copy modified/access timestamps to the output\n\
367 -j --only-section <name> Only copy section <name> into the output\n\
368 -R --remove-section <name> Remove section <name> from the output\n\
369 -S --strip-all Remove all symbol and relocation information\n\
370 -g --strip-debug Remove all debugging symbols\n\
371 --strip-unneeded Remove all symbols not needed by relocations\n\
372 -N --strip-symbol <name> Do not copy symbol <name>\n\
373 -K --keep-symbol <name> Only copy symbol <name>\n\
374 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
375 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
376 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
377 --weaken Force all global symbols to be marked as weak\n\
378 -x --discard-all Remove all non-global symbols\n\
379 -X --discard-locals Remove any compiler-generated symbols\n\
380 -i --interleave <number> Only copy one out of every <number> bytes\n\
381 -b --byte <num> Select byte <num> in every interleaved block\n\
382 --gap-fill <val> Fill gaps between sections with <val>\n\
383 --pad-to <addr> Pad the last section up to address <addr>\n\
384 --set-start <addr> Set the start address to <addr>\n\
385 {--change-start|--adjust-start} <incr>\n\
386 Add <incr> to the start address\n\
387 {--change-addresses|--adjust-vma} <incr>\n\
388 Add <incr> to LMA, VMA and start addresses\n\
389 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
390 Change LMA and VMA of section <name> by <val>\n\
391 --change-section-lma <name>{=|+|-}<val>\n\
392 Change the LMA of section <name> by <val>\n\
393 --change-section-vma <name>{=|+|-}<val>\n\
394 Change the VMA of section <name> by <val>\n\
395 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
396 Warn if a named section does not exist\n\
397 --set-section-flags <name>=<flags>\n\
398 Set section <name>'s properties to <flags>\n\
399 --add-section <name>=<file> Add section <name> found in <file> to output\n\
400 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
401 --change-leading-char Force output format's leading character style\n\
402 --remove-leading-char Remove leading character from global symbols\n\
403 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
404 --srec-len <number> Restrict the length of generated Srecords\n\
405 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
406 --strip-symbols <file> -N for all symbols listed in <file>\n\
407 --keep-symbols <file> -K for all symbols listed in <file>\n\
408 --localize-symbols <file> -L for all symbols listed in <file>\n\
409 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
410 --weaken-symbols <file> -W for all symbols listed in <file>\n\
411 -v --verbose List all object files modified\n\
412 -V --version Display this program's version number\n\
413 -h --help Display this output\n\
415 list_supported_targets (program_name, stream);
416 if (exit_status == 0)
417 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
422 strip_usage (stream, exit_status)
426 fprintf (stream, _("Usage: %s <switches> in-file(s)\n"), program_name);
427 fprintf (stream, _(" The switches are:\n"));
428 fprintf (stream, _("\
429 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
430 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
431 -F --target <bfdname> Set both input and output format to <bfdname>\n\
432 -p --preserve-dates Copy modified/access timestamps to the output\n\
433 -R --remove-section <name> Remove section <name> from the output\n\
434 -s --strip-all Remove all symbol and relocation information\n\
435 -g -S --strip-debug Remove all debugging symbols\n\
436 --strip-unneeded Remove all symbols not needed by relocations\n\
437 -N --strip-symbol <name> Do not copy symbol <name>\n\
438 -K --keep-symbol <name> Only copy symbol <name>\n\
439 -x --discard-all Remove all non-global symbols\n\
440 -X --discard-locals Remove any compiler-generated symbols\n\
441 -v --verbose List all object files modified\n\
442 -V --version Display this program's version number\n\
443 -h --help Display this output\n\
444 -o <file> Place stripped output into <file>\n\
447 list_supported_targets (program_name, stream);
448 if (exit_status == 0)
449 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
453 /* Parse section flags into a flagword, with a fatal error if the
454 string can't be parsed. */
468 snext = strchr (s, ',');
478 #define PARSE_FLAG(fname,fval) \
479 else if (strncasecmp (fname, s, len) == 0) ret |= fval
480 PARSE_FLAG ("alloc", SEC_ALLOC);
481 PARSE_FLAG ("load", SEC_LOAD);
482 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
483 PARSE_FLAG ("readonly", SEC_READONLY);
484 PARSE_FLAG ("debug", SEC_DEBUGGING);
485 PARSE_FLAG ("code", SEC_CODE);
486 PARSE_FLAG ("data", SEC_DATA);
487 PARSE_FLAG ("rom", SEC_ROM);
488 PARSE_FLAG ("share", SEC_SHARED);
489 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
495 copy = xmalloc (len + 1);
496 strncpy (copy, s, len);
498 non_fatal (_("unrecognized section flag `%s'"), copy);
499 fatal (_("supported flags: %s"),
500 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
510 /* Find and optionally add an entry in the change_sections list. */
512 static struct section_list *
513 find_section_list (name, add)
517 register struct section_list *p;
519 for (p = change_sections; p != NULL; p = p->next)
520 if (strcmp (p->name, name) == 0)
526 p = (struct section_list *) xmalloc (sizeof (struct section_list));
531 p->change_vma = CHANGE_IGNORE;
532 p->change_lma = CHANGE_IGNORE;
535 p->set_flags = false;
538 p->next = change_sections;
544 /* Add a symbol to strip_specific_list. */
547 add_specific_symbol (name, list)
549 struct symlist **list;
551 struct symlist *tmp_list;
553 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
554 tmp_list->name = name;
555 tmp_list->next = *list;
559 /* Add symbols listed in `filename' to strip_specific_list. */
561 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
562 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
565 add_specific_symbols (filename, list)
566 const char *filename;
567 struct symlist **list;
573 unsigned int line_count;
575 if (stat (filename, & st) < 0)
576 fatal (_("cannot stat: %s: %s"), filename, strerror (errno));
580 buffer = (char *) xmalloc (st.st_size + 2);
581 f = fopen (filename, FOPEN_RT);
583 fatal (_("cannot open: %s: %s"), filename, strerror (errno));
585 if (fread (buffer, 1, st.st_size, f) == 0 || ferror (f))
586 fatal (_("%s: fread failed"), filename);
589 buffer [st.st_size] = '\n';
590 buffer [st.st_size + 1] = '\0';
594 for (line = buffer; * line != '\0'; line ++)
599 int finished = false;
601 for (eol = line;; eol ++)
607 /* Cope with \n\r. */
615 /* Cope with \r\n. */
626 /* Line comment, Terminate the line here, in case a
627 name is present and then allow the rest of the
628 loop to find the real end of the line. */
640 /* A name may now exist somewhere between 'line' and 'eol'.
641 Strip off leading whitespace and trailing whitespace,
642 then add it to the list. */
643 for (name = line; IS_WHITESPACE (* name); name ++)
645 for (name_end = name;
646 (! IS_WHITESPACE (* name_end))
647 && (! IS_LINE_TERMINATOR (* name_end));
651 if (! IS_LINE_TERMINATOR (* name_end))
655 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
658 if (! IS_LINE_TERMINATOR (* extra))
659 non_fatal (_("Ignoring rubbish found on line %d of %s"),
660 line_count, filename);
666 add_specific_symbol (name, list);
668 /* Advance line pointer to end of line. The 'eol ++' in the for
669 loop above will then advance us to the start of the next line. */
675 /* See whether a symbol should be stripped or kept based on
676 strip_specific_list and keep_symbols. */
679 is_specified_symbol (name, list)
681 struct symlist *list;
683 struct symlist *tmp_list;
685 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
686 if (strcmp (name, tmp_list->name) == 0)
692 /* See if a section is being removed. */
695 is_strip_section (abfd, sec)
696 bfd *abfd ATTRIBUTE_UNUSED;
699 struct section_list *p;
701 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
702 && (strip_symbols == STRIP_DEBUG
703 || strip_symbols == STRIP_UNNEEDED
704 || strip_symbols == STRIP_ALL
705 || discard_locals == LOCALS_ALL
706 || convert_debugging))
709 if (! sections_removed && ! sections_copied)
712 p = find_section_list (bfd_get_section_name (abfd, sec), false);
713 if (sections_removed && p != NULL && p->remove)
715 if (sections_copied && (p == NULL || ! p->copy))
720 /* Choose which symbol entries to copy; put the result in OSYMS.
721 We don't copy in place, because that confuses the relocs.
722 Return the number of symbols to print. */
725 filter_symbols (abfd, obfd, osyms, isyms, symcount)
728 asymbol **osyms, **isyms;
731 register asymbol **from = isyms, **to = osyms;
732 long src_count = 0, dst_count = 0;
733 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
736 for (; src_count < symcount; src_count++)
738 asymbol *sym = from[src_count];
739 flagword flags = sym->flags;
740 const char *name = bfd_asymbol_name (sym);
744 if (redefine_sym_list)
746 const char *old_name, *new_name;
748 old_name = bfd_asymbol_name (sym);
749 new_name = lookup_sym_redefinition (old_name);
750 name = bfd_asymbol_name (sym) = new_name;
753 if (change_leading_char
754 && (bfd_get_symbol_leading_char (abfd)
755 != bfd_get_symbol_leading_char (obfd))
756 && (bfd_get_symbol_leading_char (abfd) == '\0'
757 || (name[0] == bfd_get_symbol_leading_char (abfd))))
759 if (bfd_get_symbol_leading_char (obfd) == '\0')
760 name = bfd_asymbol_name (sym) = name + 1;
765 n = xmalloc (strlen (name) + 2);
766 n[0] = bfd_get_symbol_leading_char (obfd);
767 if (bfd_get_symbol_leading_char (abfd) == '\0')
768 strcpy (n + 1, name);
770 strcpy (n + 1, name + 1);
771 name = bfd_asymbol_name (sym) = n;
775 undefined = bfd_is_und_section (bfd_get_section (sym));
777 if (remove_leading_char
778 && ((flags & BSF_GLOBAL) != 0
779 || (flags & BSF_WEAK) != 0
781 || bfd_is_com_section (bfd_get_section (sym)))
782 && name[0] == bfd_get_symbol_leading_char (abfd))
783 name = bfd_asymbol_name (sym) = name + 1;
785 if (strip_symbols == STRIP_ALL)
787 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
788 || ((flags & BSF_SECTION_SYM) != 0
789 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
792 else if (relocatable /* Relocatable file. */
793 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
795 else if (bfd_decode_symclass (sym) == 'I')
796 /* Global symbols in $idata sections need to be retained
797 even if relocatable is false. External users of the
798 library containing the $idata section may reference these
801 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
802 || (flags & BSF_WEAK) != 0
804 || bfd_is_com_section (bfd_get_section (sym)))
805 keep = strip_symbols != STRIP_UNNEEDED;
806 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
807 keep = (strip_symbols != STRIP_DEBUG
808 && strip_symbols != STRIP_UNNEEDED
809 && ! convert_debugging);
810 else if (bfd_get_section (sym)->comdat)
811 /* COMDAT sections store special information in local
812 symbols, so we cannot risk stripping any of them. */
814 else /* Local symbol. */
815 keep = (strip_symbols != STRIP_UNNEEDED
816 && (discard_locals != LOCALS_ALL
817 && (discard_locals != LOCALS_START_L
818 || ! bfd_is_local_label (abfd, sym))));
820 if (keep && is_specified_symbol (name, strip_specific_list))
822 if (!keep && is_specified_symbol (name, keep_specific_list))
824 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
827 if (keep && (flags & BSF_GLOBAL) != 0
828 && (weaken || is_specified_symbol (name, weaken_specific_list)))
830 sym->flags &=~ BSF_GLOBAL;
831 sym->flags |= BSF_WEAK;
833 if (keep && !undefined && (flags & (BSF_GLOBAL | BSF_WEAK))
834 && (is_specified_symbol (name, localize_specific_list)
835 || (keepglobal_specific_list != NULL
836 && ! is_specified_symbol (name, keepglobal_specific_list))))
838 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
839 sym->flags |= BSF_LOCAL;
843 to[dst_count++] = sym;
846 to[dst_count] = NULL;
851 /* Find the redefined name of symbol SOURCE. */
854 lookup_sym_redefinition (source)
857 struct redefine_node *list;
859 for (list = redefine_sym_list; list != NULL; list = list->next)
860 if (strcmp (source, list->source) == 0)
866 /* Add a node to a symbol redefine list. */
869 redefine_list_append (source, target)
873 struct redefine_node **p;
874 struct redefine_node *list;
875 struct redefine_node *new_node;
877 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
879 if (strcmp (source, list->source) == 0)
880 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
884 if (strcmp (target, list->target) == 0)
885 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
890 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
892 new_node->source = strdup (source);
893 new_node->target = strdup (target);
894 new_node->next = NULL;
899 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
903 filter_bytes (memhunk, size)
907 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
909 for (; from < end; from += interleave)
912 if (*size % interleave > (bfd_size_type) copy_byte)
913 *size = (*size / interleave) + 1;
918 /* Copy object file IBFD onto OBFD. */
921 copy_object (ibfd, obfd)
927 asection **osections = NULL;
928 bfd_size_type *gaps = NULL;
929 bfd_size_type max_gap = 0;
933 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
934 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
935 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
937 fatal (_("Unable to change endianness of input file(s)"));
941 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
942 RETURN_NONFATAL (bfd_get_filename (obfd));
945 printf (_("copy from %s(%s) to %s(%s)\n"),
946 bfd_get_filename (ibfd), bfd_get_target (ibfd),
947 bfd_get_filename (obfd), bfd_get_target (obfd));
952 start = bfd_get_start_address (ibfd);
953 start += change_start;
955 if (!bfd_set_start_address (obfd, start)
956 || !bfd_set_file_flags (obfd,
957 (bfd_get_file_flags (ibfd)
958 & bfd_applicable_file_flags (obfd))))
959 RETURN_NONFATAL (bfd_get_filename (ibfd));
961 /* Copy architecture of input file to output file. */
962 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
963 bfd_get_mach (ibfd)))
964 non_fatal (_("Warning: Output file cannot represent architecture %s"),
965 bfd_printable_arch_mach (bfd_get_arch (ibfd),
966 bfd_get_mach (ibfd)));
968 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
969 RETURN_NONFATAL (bfd_get_filename (ibfd));
974 if (osympp != isympp)
977 /* BFD mandates that all output sections be created and sizes set before
978 any output is done. Thus, we traverse all sections multiple times. */
979 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
981 if (add_sections != NULL)
983 struct section_add *padd;
984 struct section_list *pset;
986 for (padd = add_sections; padd != NULL; padd = padd->next)
988 padd->section = bfd_make_section (obfd, padd->name);
989 if (padd->section == NULL)
991 non_fatal (_("can't create section `%s': %s"),
992 padd->name, bfd_errmsg (bfd_get_error ()));
1000 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1001 RETURN_NONFATAL (bfd_get_filename (obfd));
1003 pset = find_section_list (padd->name, false);
1007 if (pset != NULL && pset->set_flags)
1008 flags = pset->flags | SEC_HAS_CONTENTS;
1010 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1012 if (! bfd_set_section_flags (obfd, padd->section, flags))
1013 RETURN_NONFATAL (bfd_get_filename (obfd));
1017 if (pset->change_vma != CHANGE_IGNORE)
1018 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
1019 RETURN_NONFATAL (bfd_get_filename (obfd));
1021 if (pset->change_lma != CHANGE_IGNORE)
1023 padd->section->lma = pset->lma_val;
1025 if (! bfd_set_section_alignment
1026 (obfd, padd->section,
1027 bfd_section_alignment (obfd, padd->section)))
1028 RETURN_NONFATAL (bfd_get_filename (obfd));
1035 if (gap_fill_set || pad_to_set)
1040 /* We must fill in gaps between the sections and/or we must pad
1041 the last section to a specified address. We do this by
1042 grabbing a list of the sections, sorting them by VMA, and
1043 increasing the section sizes as required to fill the gaps.
1044 We write out the gap contents below. */
1046 c = bfd_count_sections (obfd);
1047 osections = (asection **) xmalloc (c * sizeof (asection *));
1049 bfd_map_over_sections (obfd, get_sections, (void *) &set);
1051 qsort (osections, c, sizeof (asection *), compare_section_lma);
1053 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
1054 memset (gaps, 0, c * sizeof (bfd_size_type));
1058 for (i = 0; i < c - 1; i++)
1062 bfd_vma gap_start, gap_stop;
1064 flags = bfd_get_section_flags (obfd, osections[i]);
1065 if ((flags & SEC_HAS_CONTENTS) == 0
1066 || (flags & SEC_LOAD) == 0)
1069 size = bfd_section_size (obfd, osections[i]);
1070 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1071 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1072 if (gap_start < gap_stop)
1074 if (! bfd_set_section_size (obfd, osections[i],
1075 size + (gap_stop - gap_start)))
1077 non_fatal (_("Can't fill gap after %s: %s"),
1078 bfd_get_section_name (obfd, osections[i]),
1079 bfd_errmsg (bfd_get_error ()));
1083 gaps[i] = gap_stop - gap_start;
1084 if (max_gap < gap_stop - gap_start)
1085 max_gap = gap_stop - gap_start;
1095 lma = bfd_section_lma (obfd, osections[c - 1]);
1096 size = bfd_section_size (obfd, osections[c - 1]);
1097 if (lma + size < pad_to)
1099 if (! bfd_set_section_size (obfd, osections[c - 1],
1102 non_fatal (_("Can't add padding to %s: %s"),
1103 bfd_get_section_name (obfd, osections[c - 1]),
1104 bfd_errmsg (bfd_get_error ()));
1109 gaps[c - 1] = pad_to - (lma + size);
1110 if (max_gap < pad_to - (lma + size))
1111 max_gap = pad_to - (lma + size);
1117 /* Symbol filtering must happen after the output sections
1118 have been created, but before their contents are set. */
1120 symsize = bfd_get_symtab_upper_bound (ibfd);
1122 RETURN_NONFATAL (bfd_get_filename (ibfd));
1124 osympp = isympp = (asymbol **) xmalloc (symsize);
1125 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1127 RETURN_NONFATAL (bfd_get_filename (ibfd));
1129 if (convert_debugging)
1130 dhandle = read_debugging_info (ibfd, isympp, symcount);
1132 if (strip_symbols == STRIP_DEBUG
1133 || strip_symbols == STRIP_ALL
1134 || strip_symbols == STRIP_UNNEEDED
1135 || discard_locals != LOCALS_UNDEF
1136 || strip_specific_list != NULL
1137 || keep_specific_list != NULL
1138 || localize_specific_list != NULL
1139 || keepglobal_specific_list != NULL
1140 || weaken_specific_list != NULL
1143 || convert_debugging
1144 || change_leading_char
1145 || remove_leading_char
1146 || redefine_sym_list
1149 /* Mark symbols used in output relocations so that they
1150 are kept, even if they are local labels or static symbols.
1152 Note we iterate over the input sections examining their
1153 relocations since the relocations for the output sections
1154 haven't been set yet. mark_symbols_used_in_relocations will
1155 ignore input sections which have no corresponding output
1157 if (strip_symbols != STRIP_ALL)
1158 bfd_map_over_sections (ibfd,
1159 mark_symbols_used_in_relocations,
1161 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1162 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1165 if (convert_debugging && dhandle != NULL)
1167 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1174 bfd_set_symtab (obfd, osympp, symcount);
1176 /* This has to happen after the symbol table has been set. */
1177 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
1179 if (add_sections != NULL)
1181 struct section_add *padd;
1183 for (padd = add_sections; padd != NULL; padd = padd->next)
1185 if (! bfd_set_section_contents (obfd, padd->section,
1186 (PTR) padd->contents,
1188 (bfd_size_type) padd->size))
1189 RETURN_NONFATAL (bfd_get_filename (obfd));
1193 if (gap_fill_set || pad_to_set)
1198 /* Fill in the gaps. */
1201 buf = (bfd_byte *) xmalloc (max_gap);
1202 memset (buf, gap_fill, (size_t) max_gap);
1204 c = bfd_count_sections (obfd);
1205 for (i = 0; i < c; i++)
1213 off = bfd_section_size (obfd, osections[i]) - left;
1224 if (! bfd_set_section_contents (obfd, osections[i], buf,
1226 RETURN_NONFATAL (bfd_get_filename (obfd));
1235 /* Allow the BFD backend to copy any private data it understands
1236 from the input BFD to the output BFD. This is done last to
1237 permit the routine to look at the filtered symbol table, which is
1238 important for the ECOFF code at least. */
1239 if (! bfd_copy_private_bfd_data (ibfd, obfd))
1241 non_fatal (_("%s: error copying private BFD data: %s"),
1242 bfd_get_filename (obfd),
1243 bfd_errmsg (bfd_get_error ()));
1249 /* Read each archive element in turn from IBFD, copy the
1250 contents to temp file, and keep the temp file handle. */
1253 copy_archive (ibfd, obfd, output_target)
1256 const char *output_target;
1260 struct name_list *next;
1264 bfd **ptr = &obfd->archive_head;
1266 char *dir = make_tempname (bfd_get_filename (obfd));
1268 /* Make a temp directory to hold the contents. */
1269 #if defined (_WIN32) && !defined (__CYGWIN32__)
1270 if (mkdir (dir) != 0)
1272 if (mkdir (dir, 0700) != 0)
1275 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1276 dir, strerror (errno));
1278 obfd->has_armap = ibfd->has_armap;
1282 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1284 while (!status && this_element != (bfd *) NULL)
1286 /* Create an output file for this member. */
1287 char *output_name = concat (dir, "/", bfd_get_filename (this_element),
1289 bfd *output_bfd = bfd_openw (output_name, output_target);
1292 int stat_status = 0;
1296 stat_status = bfd_stat_arch_elt (this_element, &buf);
1298 if (stat_status != 0)
1299 non_fatal (_("internal stat error on %s"),
1300 bfd_get_filename (this_element));
1303 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1304 l->name = output_name;
1308 if (output_bfd == (bfd *) NULL)
1309 RETURN_NONFATAL (output_name);
1311 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1312 RETURN_NONFATAL (bfd_get_filename (obfd));
1314 if (bfd_check_format (this_element, bfd_object) == true)
1315 copy_object (this_element, output_bfd);
1317 if (!bfd_close (output_bfd))
1319 bfd_nonfatal (bfd_get_filename (output_bfd));
1320 /* Error in new object file. Don't change archive. */
1324 if (preserve_dates && stat_status == 0)
1325 set_times (output_name, &buf);
1327 /* Open the newly output file and attach to our list. */
1328 output_bfd = bfd_openr (output_name, output_target);
1330 l->obfd = output_bfd;
1333 ptr = &output_bfd->next;
1335 last_element = this_element;
1337 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1339 bfd_close (last_element);
1341 *ptr = (bfd *) NULL;
1343 if (!bfd_close (obfd))
1344 RETURN_NONFATAL (bfd_get_filename (obfd));
1346 if (!bfd_close (ibfd))
1347 RETURN_NONFATAL (bfd_get_filename (ibfd));
1349 /* Delete all the files that we opened. */
1350 for (l = list; l != NULL; l = l->next)
1352 bfd_close (l->obfd);
1358 /* The top-level control. */
1361 copy_file (input_filename, output_filename, input_target, output_target)
1362 const char *input_filename;
1363 const char *output_filename;
1364 const char *input_target;
1365 const char *output_target;
1370 /* To allow us to do "strip *" without dying on the first
1371 non-object file, failures are nonfatal. */
1372 ibfd = bfd_openr (input_filename, input_target);
1374 RETURN_NONFATAL (input_filename);
1376 if (bfd_check_format (ibfd, bfd_archive))
1380 /* bfd_get_target does not return the correct value until
1381 bfd_check_format succeeds. */
1382 if (output_target == NULL)
1383 output_target = bfd_get_target (ibfd);
1385 obfd = bfd_openw (output_filename, output_target);
1387 RETURN_NONFATAL (output_filename);
1389 copy_archive (ibfd, obfd, output_target);
1391 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1395 /* bfd_get_target does not return the correct value until
1396 bfd_check_format succeeds. */
1397 if (output_target == NULL)
1398 output_target = bfd_get_target (ibfd);
1400 obfd = bfd_openw (output_filename, output_target);
1402 RETURN_NONFATAL (output_filename);
1404 copy_object (ibfd, obfd);
1406 if (!bfd_close (obfd))
1407 RETURN_NONFATAL (output_filename);
1409 if (!bfd_close (ibfd))
1410 RETURN_NONFATAL (input_filename);
1414 bfd_nonfatal (input_filename);
1416 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1418 list_matching_formats (matching);
1426 /* Add a name to the section renaming list. */
1429 add_section_rename (old_name, new_name, flags)
1430 const char * old_name;
1431 const char * new_name;
1434 section_rename * rename;
1436 /* Check for conflicts first. */
1437 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1438 if (strcmp (rename->old_name, old_name) == 0)
1440 /* Silently ignore duplicate definitions. */
1441 if (strcmp (rename->new_name, new_name) == 0
1442 && rename->flags == flags)
1445 fatal (_("Multiple renames of section %s"), old_name);
1448 rename = (section_rename *) xmalloc (sizeof (* rename));
1450 rename->old_name = old_name;
1451 rename->new_name = new_name;
1452 rename->flags = flags;
1453 rename->next = section_rename_list;
1455 section_rename_list = rename;
1458 /* Check the section rename list for a new name of the input section
1459 ISECTION. Return the new name if one is found.
1460 Also set RETURNED_FLAGS to the flags to be used for this section. */
1463 find_section_rename (ibfd, isection, returned_flags)
1464 bfd * ibfd ATTRIBUTE_UNUSED;
1466 flagword * returned_flags;
1468 const char * old_name = bfd_section_name (ibfd, isection);
1469 section_rename * rename;
1471 /* Default to using the flags of the input section. */
1472 * returned_flags = bfd_get_section_flags (ibfd, isection);
1474 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1475 if (strcmp (rename->old_name, old_name) == 0)
1477 if (rename->flags != (flagword) -1)
1478 * returned_flags = rename->flags;
1480 return rename->new_name;
1486 /* Create a section in OBFD with the same
1487 name and attributes as ISECTION in IBFD. */
1490 setup_section (ibfd, isection, obfdarg)
1495 bfd *obfd = (bfd *) obfdarg;
1496 struct section_list *p;
1505 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1506 && (strip_symbols == STRIP_DEBUG
1507 || strip_symbols == STRIP_UNNEEDED
1508 || strip_symbols == STRIP_ALL
1509 || discard_locals == LOCALS_ALL
1510 || convert_debugging))
1513 p = find_section_list (bfd_section_name (ibfd, isection), false);
1517 if (sections_removed && p != NULL && p->remove)
1519 if (sections_copied && (p == NULL || ! p->copy))
1522 /* Get the, possibly new, name of the output section. */
1523 name = find_section_rename (ibfd, isection, & flags);
1525 osection = bfd_make_section_anyway (obfd, name);
1527 if (osection == NULL)
1533 size = bfd_section_size (ibfd, isection);
1535 size = (size + interleave - 1) / interleave;
1536 if (! bfd_set_section_size (obfd, osection, size))
1542 vma = bfd_section_vma (ibfd, isection);
1543 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1545 else if (p != NULL && p->change_vma == CHANGE_SET)
1548 vma += change_section_address;
1550 if (! bfd_set_section_vma (obfd, osection, vma))
1556 lma = isection->lma;
1557 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1559 if (p->change_lma == CHANGE_MODIFY)
1561 else if (p->change_lma == CHANGE_SET)
1567 lma += change_section_address;
1569 osection->lma = lma;
1571 /* FIXME: This is probably not enough. If we change the LMA we
1572 may have to recompute the header for the file as well. */
1573 if (bfd_set_section_alignment (obfd,
1575 bfd_section_alignment (ibfd, isection))
1578 err = _("alignment");
1582 if (p != NULL && p->set_flags)
1583 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1584 if (!bfd_set_section_flags (obfd, osection, flags))
1590 /* This used to be mangle_section; we do here to avoid using
1591 bfd_get_section_by_name since some formats allow multiple
1592 sections with the same name. */
1593 isection->output_section = osection;
1594 isection->output_offset = 0;
1596 /* Allow the BFD backend to copy any private data it understands
1597 from the input section to the output section. */
1598 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1600 err = _("private data");
1604 /* All went well. */
1608 non_fatal (_("%s: section `%s': error in %s: %s"),
1609 bfd_get_filename (ibfd),
1610 bfd_section_name (ibfd, isection),
1611 err, bfd_errmsg (bfd_get_error ()));
1615 /* Copy the data of input section ISECTION of IBFD
1616 to an output section with the same name in OBFD.
1617 If stripping then don't copy any relocation info. */
1620 copy_section (ibfd, isection, obfdarg)
1625 bfd *obfd = (bfd *) obfdarg;
1626 struct section_list *p;
1633 /* If we have already failed earlier on,
1634 do not keep on generating complaints now. */
1638 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1639 && (strip_symbols == STRIP_DEBUG
1640 || strip_symbols == STRIP_UNNEEDED
1641 || strip_symbols == STRIP_ALL
1642 || discard_locals == LOCALS_ALL
1643 || convert_debugging))
1646 p = find_section_list (bfd_section_name (ibfd, isection), false);
1648 if (sections_removed && p != NULL && p->remove)
1650 if (sections_copied && (p == NULL || ! p->copy))
1653 osection = isection->output_section;
1654 size = bfd_get_section_size_before_reloc (isection);
1656 if (size == 0 || osection == 0)
1659 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1661 RETURN_NONFATAL (bfd_get_filename (ibfd));
1664 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1667 relpp = (arelent **) xmalloc (relsize);
1668 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1670 RETURN_NONFATAL (bfd_get_filename (ibfd));
1672 if (strip_symbols == STRIP_ALL)
1674 /* Remove relocations which are not in
1675 keep_strip_specific_list. */
1676 arelent **temp_relpp;
1677 long temp_relcount = 0;
1680 temp_relpp = (arelent **) xmalloc (relsize);
1681 for (i = 0; i < relcount; i++)
1682 if (is_specified_symbol
1683 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1684 keep_specific_list))
1685 temp_relpp [temp_relcount++] = relpp [i];
1686 relcount = temp_relcount;
1691 bfd_set_reloc (obfd, osection,
1692 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1695 isection->_cooked_size = isection->_raw_size;
1696 isection->reloc_done = true;
1698 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1700 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1702 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1704 RETURN_NONFATAL (bfd_get_filename (ibfd));
1707 filter_bytes (memhunk, &size);
1709 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1711 RETURN_NONFATAL (bfd_get_filename (obfd));
1715 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1717 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1719 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1720 flag--they can just remove the section entirely and add it
1721 back again. However, we do permit them to turn on the
1722 SEC_HAS_CONTENTS flag, and take it to mean that the section
1723 contents should be zeroed out. */
1725 memset (memhunk, 0, size);
1726 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1728 RETURN_NONFATAL (bfd_get_filename (obfd));
1733 /* Get all the sections. This is used when --gap-fill or --pad-to is
1737 get_sections (obfd, osection, secppparg)
1738 bfd *obfd ATTRIBUTE_UNUSED;
1742 asection ***secppp = (asection ***) secppparg;
1744 **secppp = osection;
1748 /* Sort sections by VMA. This is called via qsort, and is used when
1749 --gap-fill or --pad-to is used. We force non loadable or empty
1750 sections to the front, where they are easier to ignore. */
1753 compare_section_lma (arg1, arg2)
1757 const asection **sec1 = (const asection **) arg1;
1758 const asection **sec2 = (const asection **) arg2;
1759 flagword flags1, flags2;
1761 /* Sort non loadable sections to the front. */
1762 flags1 = (*sec1)->flags;
1763 flags2 = (*sec2)->flags;
1764 if ((flags1 & SEC_HAS_CONTENTS) == 0
1765 || (flags1 & SEC_LOAD) == 0)
1767 if ((flags2 & SEC_HAS_CONTENTS) != 0
1768 && (flags2 & SEC_LOAD) != 0)
1773 if ((flags2 & SEC_HAS_CONTENTS) == 0
1774 || (flags2 & SEC_LOAD) == 0)
1778 /* Sort sections by LMA. */
1779 if ((*sec1)->lma > (*sec2)->lma)
1781 else if ((*sec1)->lma < (*sec2)->lma)
1784 /* Sort sections with the same LMA by size. */
1785 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1787 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1793 /* Mark all the symbols which will be used in output relocations with
1794 the BSF_KEEP flag so that those symbols will not be stripped.
1796 Ignore relocations which will not appear in the output file. */
1799 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1804 asymbol **symbols = (asymbol **) symbolsarg;
1809 /* Ignore an input section with no corresponding output section. */
1810 if (isection->output_section == NULL)
1813 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1815 bfd_fatal (bfd_get_filename (ibfd));
1820 relpp = (arelent **) xmalloc (relsize);
1821 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1823 bfd_fatal (bfd_get_filename (ibfd));
1825 /* Examine each symbol used in a relocation. If it's not one of the
1826 special bfd section symbols, then mark it with BSF_KEEP. */
1827 for (i = 0; i < relcount; i++)
1829 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1830 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1831 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1832 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1839 /* Write out debugging information. */
1842 write_debugging_info (obfd, dhandle, symcountp, symppp)
1845 long *symcountp ATTRIBUTE_UNUSED;
1846 asymbol ***symppp ATTRIBUTE_UNUSED;
1848 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1849 return write_ieee_debugging_info (obfd, dhandle);
1851 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1852 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1854 bfd_byte *syms, *strings;
1855 bfd_size_type symsize, stringsize;
1856 asection *stabsec, *stabstrsec;
1858 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1863 stabsec = bfd_make_section (obfd, ".stab");
1864 stabstrsec = bfd_make_section (obfd, ".stabstr");
1866 || stabstrsec == NULL
1867 || ! bfd_set_section_size (obfd, stabsec, symsize)
1868 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1869 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1870 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1871 || ! bfd_set_section_flags (obfd, stabsec,
1875 || ! bfd_set_section_flags (obfd, stabstrsec,
1880 non_fatal (_("%s: can't create debugging section: %s"),
1881 bfd_get_filename (obfd),
1882 bfd_errmsg (bfd_get_error ()));
1886 /* We can get away with setting the section contents now because
1887 the next thing the caller is going to do is copy over the
1888 real sections. We may someday have to split the contents
1889 setting out of this function. */
1890 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1892 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1893 (file_ptr) 0, stringsize))
1895 non_fatal (_("%s: can't set debugging section contents: %s"),
1896 bfd_get_filename (obfd),
1897 bfd_errmsg (bfd_get_error ()));
1904 non_fatal (_("%s: don't know how to write debugging information for %s"),
1905 bfd_get_filename (obfd), bfd_get_target (obfd));
1910 strip_main (argc, argv)
1914 char *input_target = NULL, *output_target = NULL;
1915 boolean show_version = false;
1917 struct section_list *p;
1918 char *output_file = NULL;
1920 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXVv",
1921 strip_options, (int *) 0)) != EOF)
1926 input_target = optarg;
1929 output_target = optarg;
1932 input_target = output_target = optarg;
1935 p = find_section_list (optarg, true);
1937 sections_removed = true;
1940 strip_symbols = STRIP_ALL;
1944 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
1945 strip_symbols = STRIP_DEBUG;
1947 case OPTION_STRIP_UNNEEDED:
1948 strip_symbols = STRIP_UNNEEDED;
1951 add_specific_symbol (optarg, &keep_specific_list);
1954 add_specific_symbol (optarg, &strip_specific_list);
1957 output_file = optarg;
1960 preserve_dates = true;
1963 discard_locals = LOCALS_ALL;
1966 discard_locals = LOCALS_START_L;
1972 show_version = true;
1975 /* We've been given a long option. */
1978 strip_usage (stdout, 0);
1980 strip_usage (stderr, 1);
1985 print_version ("strip");
1987 /* Default is to strip all symbols. */
1988 if (strip_symbols == STRIP_UNDEF
1989 && discard_locals == LOCALS_UNDEF
1990 && strip_specific_list == NULL)
1991 strip_symbols = STRIP_ALL;
1993 if (output_target == (char *) NULL)
1994 output_target = input_target;
1998 || (output_file != NULL && (i + 1) < argc))
1999 strip_usage (stderr, 1);
2001 for (; i < argc; i++)
2003 int hold_status = status;
2004 struct stat statbuf;
2009 if (stat (argv[i], &statbuf) < 0)
2011 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
2016 if (output_file != NULL)
2017 tmpname = output_file;
2019 tmpname = make_tempname (argv[i]);
2022 copy_file (argv[i], tmpname, input_target, output_target);
2026 set_times (tmpname, &statbuf);
2027 if (output_file == NULL)
2028 smart_rename (tmpname, argv[i], preserve_dates);
2029 status = hold_status;
2033 if (output_file == NULL)
2041 copy_main (argc, argv)
2045 char * binary_architecture = NULL;
2046 char *input_filename = NULL, *output_filename = NULL;
2047 char *input_target = NULL, *output_target = NULL;
2048 boolean show_version = false;
2049 boolean change_warn = true;
2051 struct section_list *p;
2052 struct stat statbuf;
2054 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXVvW:",
2055 copy_options, (int *) 0)) != EOF)
2060 copy_byte = atoi (optarg);
2062 fatal (_("byte number must be non-negative"));
2066 binary_architecture = optarg;
2070 interleave = atoi (optarg);
2072 fatal (_("interleave must be positive"));
2076 case 's': /* "source" - 'I' is preferred */
2077 input_target = optarg;
2081 case 'd': /* "destination" - 'O' is preferred */
2082 output_target = optarg;
2086 input_target = output_target = optarg;
2090 p = find_section_list (optarg, true);
2092 fatal (_("%s both copied and removed"), optarg);
2094 sections_copied = true;
2098 p = find_section_list (optarg, true);
2100 fatal (_("%s both copied and removed"), optarg);
2102 sections_removed = true;
2106 strip_symbols = STRIP_ALL;
2110 strip_symbols = STRIP_DEBUG;
2113 case OPTION_STRIP_UNNEEDED:
2114 strip_symbols = STRIP_UNNEEDED;
2118 add_specific_symbol (optarg, &keep_specific_list);
2122 add_specific_symbol (optarg, &strip_specific_list);
2126 add_specific_symbol (optarg, &localize_specific_list);
2130 add_specific_symbol (optarg, &keepglobal_specific_list);
2134 add_specific_symbol (optarg, &weaken_specific_list);
2138 preserve_dates = true;
2142 discard_locals = LOCALS_ALL;
2146 discard_locals = LOCALS_START_L;
2154 show_version = true;
2161 case OPTION_ADD_SECTION:
2165 struct section_add *pa;
2170 s = strchr (optarg, '=');
2173 fatal (_("bad format for %s"), "--add-section");
2175 if (stat (s + 1, & st) < 0)
2176 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
2178 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
2181 name = (char *) xmalloc (len + 1);
2182 strncpy (name, optarg, len);
2186 pa->filename = s + 1;
2188 pa->size = st.st_size;
2190 pa->contents = (bfd_byte *) xmalloc (pa->size);
2191 f = fopen (pa->filename, FOPEN_RB);
2194 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
2196 if (fread (pa->contents, 1, pa->size, f) == 0
2198 fatal (_("%s: fread failed"), pa->filename);
2202 pa->next = add_sections;
2207 case OPTION_CHANGE_START:
2208 change_start = parse_vma (optarg, "--change-start");
2211 case OPTION_CHANGE_SECTION_ADDRESS:
2212 case OPTION_CHANGE_SECTION_LMA:
2213 case OPTION_CHANGE_SECTION_VMA:
2218 char *option = NULL;
2220 enum change_action what = CHANGE_IGNORE;
2224 case OPTION_CHANGE_SECTION_ADDRESS:
2225 option = "--change-section-address";
2227 case OPTION_CHANGE_SECTION_LMA:
2228 option = "--change-section-lma";
2230 case OPTION_CHANGE_SECTION_VMA:
2231 option = "--change-section-vma";
2235 s = strchr (optarg, '=');
2238 s = strchr (optarg, '+');
2241 s = strchr (optarg, '-');
2243 fatal (_("bad format for %s"), option);
2248 name = (char *) xmalloc (len + 1);
2249 strncpy (name, optarg, len);
2252 p = find_section_list (name, true);
2254 val = parse_vma (s + 1, option);
2258 case '=': what = CHANGE_SET; break;
2259 case '-': val = - val; /* Drop through. */
2260 case '+': what = CHANGE_MODIFY; break;
2265 case OPTION_CHANGE_SECTION_ADDRESS:
2266 p->change_vma = what;
2270 case OPTION_CHANGE_SECTION_LMA:
2271 p->change_lma = what;
2275 case OPTION_CHANGE_SECTION_VMA:
2276 p->change_vma = what;
2283 case OPTION_CHANGE_ADDRESSES:
2284 change_section_address = parse_vma (optarg, "--change-addresses");
2285 change_start = change_section_address;
2288 case OPTION_CHANGE_WARNINGS:
2292 case OPTION_CHANGE_LEADING_CHAR:
2293 change_leading_char = true;
2296 case OPTION_DEBUGGING:
2297 convert_debugging = true;
2300 case OPTION_GAP_FILL:
2302 bfd_vma gap_fill_vma;
2304 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2305 gap_fill = (bfd_byte) gap_fill_vma;
2306 if ((bfd_vma) gap_fill != gap_fill_vma)
2310 sprintf_vma (buff, gap_fill_vma);
2312 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2315 gap_fill_set = true;
2319 case OPTION_NO_CHANGE_WARNINGS:
2320 change_warn = false;
2324 pad_to = parse_vma (optarg, "--pad-to");
2328 case OPTION_REMOVE_LEADING_CHAR:
2329 remove_leading_char = true;
2332 case OPTION_REDEFINE_SYM:
2334 /* Push this redefinition onto redefine_symbol_list. */
2338 const char *nextarg;
2339 char *source, *target;
2341 s = strchr (optarg, '=');
2343 fatal (_("bad format for %s"), "--redefine-sym");
2346 source = (char *) xmalloc (len + 1);
2347 strncpy (source, optarg, len);
2351 len = strlen (nextarg);
2352 target = (char *) xmalloc (len + 1);
2353 strcpy (target, nextarg);
2355 redefine_list_append (source, target);
2362 case OPTION_SET_SECTION_FLAGS:
2368 s = strchr (optarg, '=');
2370 fatal (_("bad format for %s"), "--set-section-flags");
2373 name = (char *) xmalloc (len + 1);
2374 strncpy (name, optarg, len);
2377 p = find_section_list (name, true);
2379 p->set_flags = true;
2380 p->flags = parse_flags (s + 1);
2384 case OPTION_RENAME_SECTION:
2392 s = strchr (optarg, '=');
2394 fatal (_("bad format for %s"), "--rename-section");
2398 fatal (_("no old name is %s"), "--rename-section");
2400 old_name = (char *) xmalloc (len + 1);
2401 strncpy (old_name, optarg, len);
2404 s = strchr (optarg + len, ',');
2407 unsigned int new_len;
2409 flags = parse_flags (s + 1);
2410 new_len = s - (optarg + len);
2412 fatal (_("no new name in %s"), "--rename-section");
2413 new_name = (char *) xmalloc (new_len + 1);
2414 strncpy (new_name, optarg + len, new_len);
2415 new_name [new_len] = 0;
2422 fatal (_("no new name in %s"), "--rename-section");
2423 new_name = (char *) xmalloc (len + 1);
2424 strcpy (new_name, s);
2428 add_section_rename (old_name, new_name, flags);
2432 case OPTION_SET_START:
2433 set_start = parse_vma (optarg, "--set-start");
2434 set_start_set = true;
2437 case OPTION_SREC_LEN:
2438 Chunk = parse_vma (optarg, "--srec-len");
2441 case OPTION_SREC_FORCES3:
2445 case OPTION_STRIP_SYMBOLS:
2446 add_specific_symbols (optarg, &strip_specific_list);
2449 case OPTION_KEEP_SYMBOLS:
2450 add_specific_symbols (optarg, &keep_specific_list);
2453 case OPTION_LOCALIZE_SYMBOLS:
2454 add_specific_symbols (optarg, &localize_specific_list);
2457 case OPTION_KEEPGLOBAL_SYMBOLS:
2458 add_specific_symbols (optarg, &keepglobal_specific_list);
2461 case OPTION_WEAKEN_SYMBOLS:
2462 add_specific_symbols (optarg, &weaken_specific_list);
2466 break; /* we've been given a long option */
2469 copy_usage (stdout, 0);
2472 copy_usage (stderr, 1);
2477 print_version ("objcopy");
2479 if (copy_byte >= interleave)
2480 fatal (_("byte number must be less than interleave"));
2482 if (optind == argc || optind + 2 < argc)
2483 copy_usage (stderr, 1);
2485 input_filename = argv[optind];
2486 if (optind + 1 < argc)
2487 output_filename = argv[optind + 1];
2489 /* Default is to strip no symbols. */
2490 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2491 strip_symbols = STRIP_NONE;
2493 if (output_target == (char *) NULL)
2494 output_target = input_target;
2496 if (binary_architecture != (char *) NULL)
2498 if (input_target && strcmp (input_target, "binary") == 0)
2500 const bfd_arch_info_type * temp_arch_info;
2502 temp_arch_info = bfd_scan_arch (binary_architecture);
2504 if (temp_arch_info != NULL)
2505 bfd_external_binary_architecture = temp_arch_info->arch;
2507 fatal (_("architecture %s unknown"), binary_architecture);
2511 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2512 non_fatal (_(" Argument %s ignored"), binary_architecture);
2517 if (stat (input_filename, & statbuf) < 0)
2518 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2520 /* If there is no destination file then create a temp and rename
2521 the result into the input. */
2523 if (output_filename == (char *) NULL)
2525 char *tmpname = make_tempname (input_filename);
2527 copy_file (input_filename, tmpname, input_target, output_target);
2531 set_times (tmpname, &statbuf);
2532 smart_rename (tmpname, input_filename, preserve_dates);
2539 copy_file (input_filename, output_filename, input_target, output_target);
2541 if (status == 0 && preserve_dates)
2542 set_times (output_filename, &statbuf);
2547 for (p = change_sections; p != NULL; p = p->next)
2551 if (p->change_vma != CHANGE_IGNORE)
2555 sprintf_vma (buff, p->vma_val);
2557 /* xgettext:c-format */
2558 non_fatal (_("%s %s%c0x%s never used"),
2559 "--change-section-vma",
2561 p->change_vma == CHANGE_SET ? '=' : '+',
2565 if (p->change_lma != CHANGE_IGNORE)
2569 sprintf_vma (buff, p->lma_val);
2571 /* xgettext:c-format */
2572 non_fatal (_("%s %s%c0x%s never used"),
2573 "--change-section-lma",
2575 p->change_lma == CHANGE_SET ? '=' : '+',
2590 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2591 setlocale (LC_MESSAGES, "");
2593 bindtextdomain (PACKAGE, LOCALEDIR);
2594 textdomain (PACKAGE);
2596 program_name = argv[0];
2597 xmalloc_set_program_name (program_name);
2599 START_PROGRESS (program_name, 0);
2601 strip_symbols = STRIP_UNDEF;
2602 discard_locals = LOCALS_UNDEF;
2605 set_default_bfd_target ();
2609 int i = strlen (program_name);
2610 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2611 /* Drop the .exe suffix, if any. */
2612 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
2615 program_name[i] = '\0';
2618 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
2622 strip_main (argc, argv);
2624 copy_main (argc, argv);
2626 END_PROGRESS (program_name);