Applied Bob Wilson's documentation fixes.
[binutils.git] / binutils / objcopy.c
blobf62ed694ee411c00ff98659f9ebeb117ce30e75b
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,
3 2001, 2002, 2003
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
21 02111-1307, USA. */
23 #include "bfd.h"
24 #include "progress.h"
25 #include "bucomm.h"
26 #include "getopt.h"
27 #include "libiberty.h"
28 #include "budbg.h"
29 #include "filenames.h"
30 #include <sys/stat.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
35 deleted. */
37 struct symlist
39 const char *name;
40 struct symlist *next;
43 /* A list to support redefine_sym. */
44 struct redefine_node
46 char *source;
47 char *target;
48 struct redefine_node *next;
51 typedef struct section_rename
53 const char * old_name;
54 const char * new_name;
55 flagword flags;
56 struct section_rename * next;
58 section_rename;
60 /* List of sections to be renamed. */
61 static section_rename * section_rename_list;
63 static void copy_usage
64 PARAMS ((FILE *, int));
65 static void strip_usage
66 PARAMS ((FILE *, int));
67 static flagword parse_flags
68 PARAMS ((const char *));
69 static struct section_list *find_section_list
70 PARAMS ((const char *, bfd_boolean));
71 static void setup_section
72 PARAMS ((bfd *, asection *, PTR));
73 static void copy_section
74 PARAMS ((bfd *, asection *, PTR));
75 static void get_sections
76 PARAMS ((bfd *, asection *, PTR));
77 static int compare_section_lma
78 PARAMS ((const PTR, const PTR));
79 static void add_specific_symbol
80 PARAMS ((const char *, struct symlist **));
81 static void add_specific_symbols
82 PARAMS ((const char *, struct symlist **));
83 static bfd_boolean is_specified_symbol
84 PARAMS ((const char *, struct symlist *));
85 static bfd_boolean is_strip_section
86 PARAMS ((bfd *, asection *));
87 static unsigned int filter_symbols
88 PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long));
89 static void mark_symbols_used_in_relocations
90 PARAMS ((bfd *, asection *, PTR));
91 static void filter_bytes
92 PARAMS ((char *, bfd_size_type *));
93 static bfd_boolean write_debugging_info
94 PARAMS ((bfd *, PTR, long *, asymbol ***));
95 static void copy_object
96 PARAMS ((bfd *, bfd *));
97 static void copy_archive
98 PARAMS ((bfd *, bfd *, const char *));
99 static void copy_file
100 PARAMS ((const char *, const char *, const char *, const char *));
101 static int strip_main
102 PARAMS ((int, char **));
103 static int copy_main
104 PARAMS ((int, char **));
105 static const char *lookup_sym_redefinition
106 PARAMS((const char *));
107 static void redefine_list_append
108 PARAMS ((const char *, const char *));
109 static const char * find_section_rename
110 PARAMS ((bfd *, sec_ptr, flagword *));
111 static void add_section_rename
112 PARAMS ((const char *, const char *, flagword));
114 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
116 static asymbol **isympp = NULL; /* Input symbols */
117 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
119 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
120 static int copy_byte = -1;
121 static int interleave = 4;
123 static bfd_boolean verbose; /* Print file and target names. */
124 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
125 static int status = 0; /* Exit status. */
127 enum strip_action
129 STRIP_UNDEF,
130 STRIP_NONE, /* don't strip */
131 STRIP_DEBUG, /* strip all debugger symbols */
132 STRIP_UNNEEDED, /* strip unnecessary symbols */
133 STRIP_ALL /* strip all symbols */
136 /* Which symbols to remove. */
137 static enum strip_action strip_symbols;
139 enum locals_action
141 LOCALS_UNDEF,
142 LOCALS_START_L, /* discard locals starting with L */
143 LOCALS_ALL /* discard all locals */
146 /* Which local symbols to remove. Overrides STRIP_ALL. */
147 static enum locals_action discard_locals;
149 /* What kind of change to perform. */
150 enum change_action
152 CHANGE_IGNORE,
153 CHANGE_MODIFY,
154 CHANGE_SET
157 /* Structure used to hold lists of sections and actions to take. */
158 struct section_list
160 struct section_list * next; /* Next section to change. */
161 const char * name; /* Section name. */
162 bfd_boolean used; /* Whether this entry was used. */
163 bfd_boolean remove; /* Whether to remove this section. */
164 bfd_boolean copy; /* Whether to copy this section. */
165 enum change_action change_vma;/* Whether to change or set VMA. */
166 bfd_vma vma_val; /* Amount to change by or set to. */
167 enum change_action change_lma;/* Whether to change or set LMA. */
168 bfd_vma lma_val; /* Amount to change by or set to. */
169 bfd_boolean set_flags; /* Whether to set the section flags. */
170 flagword flags; /* What to set the section flags to. */
173 static struct section_list *change_sections;
175 /* TRUE if some sections are to be removed. */
176 static bfd_boolean sections_removed;
178 /* TRUE if only some sections are to be copied. */
179 static bfd_boolean sections_copied;
181 /* Changes to the start address. */
182 static bfd_vma change_start = 0;
183 static bfd_boolean set_start_set = FALSE;
184 static bfd_vma set_start;
186 /* Changes to section addresses. */
187 static bfd_vma change_section_address = 0;
189 /* Filling gaps between sections. */
190 static bfd_boolean gap_fill_set = FALSE;
191 static bfd_byte gap_fill = 0;
193 /* Pad to a given address. */
194 static bfd_boolean pad_to_set = FALSE;
195 static bfd_vma pad_to;
197 /* Use alternate machine code? */
198 static int use_alt_mach_code = 0;
200 /* List of sections to add. */
201 struct section_add
203 /* Next section to add. */
204 struct section_add *next;
205 /* Name of section to add. */
206 const char *name;
207 /* Name of file holding section contents. */
208 const char *filename;
209 /* Size of file. */
210 size_t size;
211 /* Contents of file. */
212 bfd_byte *contents;
213 /* BFD section, after it has been added. */
214 asection *section;
217 /* List of sections to add to the output BFD. */
218 static struct section_add *add_sections;
220 /* Whether to convert debugging information. */
221 static bfd_boolean convert_debugging = FALSE;
223 /* Whether to change the leading character in symbol names. */
224 static bfd_boolean change_leading_char = FALSE;
226 /* Whether to remove the leading character from global symbol names. */
227 static bfd_boolean remove_leading_char = FALSE;
229 /* List of symbols to strip, keep, localize, keep-global, weaken,
230 or redefine. */
231 static struct symlist *strip_specific_list = NULL;
232 static struct symlist *keep_specific_list = NULL;
233 static struct symlist *localize_specific_list = NULL;
234 static struct symlist *keepglobal_specific_list = NULL;
235 static struct symlist *weaken_specific_list = NULL;
236 static struct redefine_node *redefine_sym_list = NULL;
238 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
239 static bfd_boolean weaken = FALSE;
241 /* Prefix symbols/sections. */
242 static char *prefix_symbols_string = 0;
243 static char *prefix_sections_string = 0;
244 static char *prefix_alloc_sections_string = 0;
246 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
248 #define OPTION_ADD_SECTION 150
249 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
250 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
251 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
252 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
253 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
254 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
255 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
256 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
257 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
258 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
259 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
260 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
261 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
262 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
263 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
264 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
265 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
266 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYM + 1)
267 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
268 #define OPTION_STRIP_SYMBOLS (OPTION_SREC_FORCES3 + 1)
269 #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1)
270 #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
271 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
272 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
273 #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
274 #define OPTION_ALT_MACH_CODE (OPTION_RENAME_SECTION + 1)
275 #define OPTION_PREFIX_SYMBOLS (OPTION_ALT_MACH_CODE + 1)
276 #define OPTION_PREFIX_SECTIONS (OPTION_PREFIX_SYMBOLS + 1)
277 #define OPTION_PREFIX_ALLOC_SECTIONS (OPTION_PREFIX_SECTIONS + 1)
279 /* Options to handle if running as "strip". */
281 static struct option strip_options[] =
283 {"discard-all", no_argument, 0, 'x'},
284 {"discard-locals", no_argument, 0, 'X'},
285 {"format", required_argument, 0, 'F'}, /* Obsolete */
286 {"help", no_argument, 0, 'h'},
287 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
288 {"input-target", required_argument, 0, 'I'},
289 {"keep-symbol", required_argument, 0, 'K'},
290 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
291 {"output-target", required_argument, 0, 'O'},
292 {"output-file", required_argument, 0, 'o'},
293 {"preserve-dates", no_argument, 0, 'p'},
294 {"remove-section", required_argument, 0, 'R'},
295 {"strip-all", no_argument, 0, 's'},
296 {"strip-debug", no_argument, 0, 'S'},
297 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
298 {"strip-symbol", required_argument, 0, 'N'},
299 {"target", required_argument, 0, 'F'},
300 {"verbose", no_argument, 0, 'v'},
301 {"version", no_argument, 0, 'V'},
302 {0, no_argument, 0, 0}
305 /* Options to handle if running as "objcopy". */
307 static struct option copy_options[] =
309 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
310 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
311 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
312 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
313 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
314 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
315 {"binary-architecture", required_argument, 0, 'B'},
316 {"byte", required_argument, 0, 'b'},
317 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
318 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
319 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
320 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
321 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
322 {"change-start", required_argument, 0, OPTION_CHANGE_START},
323 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
324 {"debugging", no_argument, 0, OPTION_DEBUGGING},
325 {"discard-all", no_argument, 0, 'x'},
326 {"discard-locals", no_argument, 0, 'X'},
327 {"format", required_argument, 0, 'F'}, /* Obsolete */
328 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
329 {"help", no_argument, 0, 'h'},
330 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
331 {"input-target", required_argument, 0, 'I'},
332 {"interleave", required_argument, 0, 'i'},
333 {"keep-global-symbol", required_argument, 0, 'G'},
334 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
335 {"keep-symbol", required_argument, 0, 'K'},
336 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
337 {"localize-symbol", required_argument, 0, 'L'},
338 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
339 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
340 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
341 {"only-section", required_argument, 0, 'j'},
342 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
343 {"output-target", required_argument, 0, 'O'},
344 {"pad-to", required_argument, 0, OPTION_PAD_TO},
345 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
346 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
347 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
348 {"preserve-dates", no_argument, 0, 'p'},
349 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
350 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
351 {"remove-section", required_argument, 0, 'R'},
352 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
353 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
354 {"set-start", required_argument, 0, OPTION_SET_START},
355 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
356 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
357 {"strip-all", no_argument, 0, 'S'},
358 {"strip-debug", no_argument, 0, 'g'},
359 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
360 {"strip-symbol", required_argument, 0, 'N'},
361 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
362 {"target", required_argument, 0, 'F'},
363 {"verbose", no_argument, 0, 'v'},
364 {"version", no_argument, 0, 'V'},
365 {"weaken", no_argument, 0, OPTION_WEAKEN},
366 {"weaken-symbol", required_argument, 0, 'W'},
367 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
368 {0, no_argument, 0, 0}
371 /* IMPORTS */
372 extern char *program_name;
374 /* This flag distinguishes between strip and objcopy:
375 1 means this is 'strip'; 0 means this is 'objcopy'.
376 -1 means if we should use argv[0] to decide. */
377 extern int is_strip;
379 /* The maximum length of an S record. This variable is declared in srec.c
380 and can be modified by the --srec-len parameter. */
381 extern unsigned int Chunk;
383 /* Restrict the generation of Srecords to type S3 only.
384 This variable is declare in bfd/srec.c and can be toggled
385 on by the --srec-forceS3 command line switch. */
386 extern bfd_boolean S3Forced;
388 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
389 extern enum bfd_architecture bfd_external_binary_architecture;
392 static void
393 copy_usage (stream, exit_status)
394 FILE *stream;
395 int exit_status;
397 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
398 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
399 fprintf (stream, _(" The options are:\n"));
400 fprintf (stream, _("\
401 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
402 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
403 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
404 -F --target <bfdname> Set both input and output format to <bfdname>\n\
405 --debugging Convert debugging information, if possible\n\
406 -p --preserve-dates Copy modified/access timestamps to the output\n\
407 -j --only-section <name> Only copy section <name> into the output\n\
408 -R --remove-section <name> Remove section <name> from the output\n\
409 -S --strip-all Remove all symbol and relocation information\n\
410 -g --strip-debug Remove all debugging symbols\n\
411 --strip-unneeded Remove all symbols not needed by relocations\n\
412 -N --strip-symbol <name> Do not copy symbol <name>\n\
413 -K --keep-symbol <name> Only copy symbol <name>\n\
414 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
415 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
416 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
417 --weaken Force all global symbols to be marked as weak\n\
418 -x --discard-all Remove all non-global symbols\n\
419 -X --discard-locals Remove any compiler-generated symbols\n\
420 -i --interleave <number> Only copy one out of every <number> bytes\n\
421 -b --byte <num> Select byte <num> in every interleaved block\n\
422 --gap-fill <val> Fill gaps between sections with <val>\n\
423 --pad-to <addr> Pad the last section up to address <addr>\n\
424 --set-start <addr> Set the start address to <addr>\n\
425 {--change-start|--adjust-start} <incr>\n\
426 Add <incr> to the start address\n\
427 {--change-addresses|--adjust-vma} <incr>\n\
428 Add <incr> to LMA, VMA and start addresses\n\
429 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
430 Change LMA and VMA of section <name> by <val>\n\
431 --change-section-lma <name>{=|+|-}<val>\n\
432 Change the LMA of section <name> by <val>\n\
433 --change-section-vma <name>{=|+|-}<val>\n\
434 Change the VMA of section <name> by <val>\n\
435 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
436 Warn if a named section does not exist\n\
437 --set-section-flags <name>=<flags>\n\
438 Set section <name>'s properties to <flags>\n\
439 --add-section <name>=<file> Add section <name> found in <file> to output\n\
440 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
441 --change-leading-char Force output format's leading character style\n\
442 --remove-leading-char Remove leading character from global symbols\n\
443 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
444 --srec-len <number> Restrict the length of generated Srecords\n\
445 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
446 --strip-symbols <file> -N for all symbols listed in <file>\n\
447 --keep-symbols <file> -K for all symbols listed in <file>\n\
448 --localize-symbols <file> -L for all symbols listed in <file>\n\
449 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
450 --weaken-symbols <file> -W for all symbols listed in <file>\n\
451 --alt-machine-code <index> Use alternate machine code for output\n\
452 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
453 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
454 --prefix-alloc-sections <prefix>\n\
455 Add <prefix> to start of every allocatable\n\
456 section name\n\
457 -v --verbose List all object files modified\n\
458 -V --version Display this program's version number\n\
459 -h --help Display this output\n\
460 "));
461 list_supported_targets (program_name, stream);
462 if (exit_status == 0)
463 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
464 exit (exit_status);
467 static void
468 strip_usage (stream, exit_status)
469 FILE *stream;
470 int exit_status;
472 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
473 fprintf (stream, _(" Removes symbols and sections from files\n"));
474 fprintf (stream, _(" The options are:\n"));
475 fprintf (stream, _("\
476 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
477 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
478 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
479 -p --preserve-dates Copy modified/access timestamps to the output\n\
480 -R --remove-section=<name> Remove section <name> from the output\n\
481 -s --strip-all Remove all symbol and relocation information\n\
482 -g -S -d --strip-debug Remove all debugging symbols\n\
483 --strip-unneeded Remove all symbols not needed by relocations\n\
484 -N --strip-symbol=<name> Do not copy symbol <name>\n\
485 -K --keep-symbol=<name> Only copy symbol <name>\n\
486 -x --discard-all Remove all non-global symbols\n\
487 -X --discard-locals Remove any compiler-generated symbols\n\
488 -v --verbose List all object files modified\n\
489 -V --version Display this program's version number\n\
490 -h --help Display this output\n\
491 -o <file> Place stripped output into <file>\n\
492 "));
494 list_supported_targets (program_name, stream);
495 if (exit_status == 0)
496 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
497 exit (exit_status);
500 /* Parse section flags into a flagword, with a fatal error if the
501 string can't be parsed. */
503 static flagword
504 parse_flags (s)
505 const char *s;
507 flagword ret;
508 const char *snext;
509 int len;
511 ret = SEC_NO_FLAGS;
515 snext = strchr (s, ',');
516 if (snext == NULL)
517 len = strlen (s);
518 else
520 len = snext - s;
521 ++snext;
524 if (0) ;
525 #define PARSE_FLAG(fname,fval) \
526 else if (strncasecmp (fname, s, len) == 0) ret |= fval
527 PARSE_FLAG ("alloc", SEC_ALLOC);
528 PARSE_FLAG ("load", SEC_LOAD);
529 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
530 PARSE_FLAG ("readonly", SEC_READONLY);
531 PARSE_FLAG ("debug", SEC_DEBUGGING);
532 PARSE_FLAG ("code", SEC_CODE);
533 PARSE_FLAG ("data", SEC_DATA);
534 PARSE_FLAG ("rom", SEC_ROM);
535 PARSE_FLAG ("share", SEC_SHARED);
536 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
537 #undef PARSE_FLAG
538 else
540 char *copy;
542 copy = xmalloc (len + 1);
543 strncpy (copy, s, len);
544 copy[len] = '\0';
545 non_fatal (_("unrecognized section flag `%s'"), copy);
546 fatal (_("supported flags: %s"),
547 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
550 s = snext;
552 while (s != NULL);
554 return ret;
557 /* Find and optionally add an entry in the change_sections list. */
559 static struct section_list *
560 find_section_list (name, add)
561 const char *name;
562 bfd_boolean add;
564 register struct section_list *p;
566 for (p = change_sections; p != NULL; p = p->next)
567 if (strcmp (p->name, name) == 0)
568 return p;
570 if (! add)
571 return NULL;
573 p = (struct section_list *) xmalloc (sizeof (struct section_list));
574 p->name = name;
575 p->used = FALSE;
576 p->remove = FALSE;
577 p->copy = FALSE;
578 p->change_vma = CHANGE_IGNORE;
579 p->change_lma = CHANGE_IGNORE;
580 p->vma_val = 0;
581 p->lma_val = 0;
582 p->set_flags = FALSE;
583 p->flags = 0;
585 p->next = change_sections;
586 change_sections = p;
588 return p;
591 /* Add a symbol to strip_specific_list. */
593 static void
594 add_specific_symbol (name, list)
595 const char *name;
596 struct symlist **list;
598 struct symlist *tmp_list;
600 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
601 tmp_list->name = name;
602 tmp_list->next = *list;
603 *list = tmp_list;
606 /* Add symbols listed in `filename' to strip_specific_list. */
608 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
609 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
611 static void
612 add_specific_symbols (filename, list)
613 const char *filename;
614 struct symlist **list;
616 struct stat st;
617 FILE * f;
618 char * line;
619 char * buffer;
620 unsigned int line_count;
622 if (stat (filename, & st) < 0)
623 fatal (_("cannot stat: %s: %s"), filename, strerror (errno));
624 if (st.st_size == 0)
625 return;
627 buffer = (char *) xmalloc (st.st_size + 2);
628 f = fopen (filename, FOPEN_RT);
629 if (f == NULL)
630 fatal (_("cannot open: %s: %s"), filename, strerror (errno));
632 if (fread (buffer, 1, st.st_size, f) == 0 || ferror (f))
633 fatal (_("%s: fread failed"), filename);
635 fclose (f);
636 buffer [st.st_size] = '\n';
637 buffer [st.st_size + 1] = '\0';
639 line_count = 1;
641 for (line = buffer; * line != '\0'; line ++)
643 char * eol;
644 char * name;
645 char * name_end;
646 int finished = FALSE;
648 for (eol = line;; eol ++)
650 switch (* eol)
652 case '\n':
653 * eol = '\0';
654 /* Cope with \n\r. */
655 if (eol[1] == '\r')
656 ++ eol;
657 finished = TRUE;
658 break;
660 case '\r':
661 * eol = '\0';
662 /* Cope with \r\n. */
663 if (eol[1] == '\n')
664 ++ eol;
665 finished = TRUE;
666 break;
668 case 0:
669 finished = TRUE;
670 break;
672 case '#':
673 /* Line comment, Terminate the line here, in case a
674 name is present and then allow the rest of the
675 loop to find the real end of the line. */
676 * eol = '\0';
677 break;
679 default:
680 break;
683 if (finished)
684 break;
687 /* A name may now exist somewhere between 'line' and 'eol'.
688 Strip off leading whitespace and trailing whitespace,
689 then add it to the list. */
690 for (name = line; IS_WHITESPACE (* name); name ++)
692 for (name_end = name;
693 (! IS_WHITESPACE (* name_end))
694 && (! IS_LINE_TERMINATOR (* name_end));
695 name_end ++)
698 if (! IS_LINE_TERMINATOR (* name_end))
700 char * extra;
702 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
705 if (! IS_LINE_TERMINATOR (* extra))
706 non_fatal (_("Ignoring rubbish found on line %d of %s"),
707 line_count, filename);
710 * name_end = '\0';
712 if (name_end > name)
713 add_specific_symbol (name, list);
715 /* Advance line pointer to end of line. The 'eol ++' in the for
716 loop above will then advance us to the start of the next line. */
717 line = eol;
718 line_count ++;
722 /* See whether a symbol should be stripped or kept based on
723 strip_specific_list and keep_symbols. */
725 static bfd_boolean
726 is_specified_symbol (name, list)
727 const char *name;
728 struct symlist *list;
730 struct symlist *tmp_list;
732 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
733 if (strcmp (name, tmp_list->name) == 0)
734 return TRUE;
736 return FALSE;
739 /* See if a section is being removed. */
741 static bfd_boolean
742 is_strip_section (abfd, sec)
743 bfd *abfd ATTRIBUTE_UNUSED;
744 asection *sec;
746 struct section_list *p;
748 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
749 && (strip_symbols == STRIP_DEBUG
750 || strip_symbols == STRIP_UNNEEDED
751 || strip_symbols == STRIP_ALL
752 || discard_locals == LOCALS_ALL
753 || convert_debugging))
754 return TRUE;
756 if (! sections_removed && ! sections_copied)
757 return FALSE;
759 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
760 if (sections_removed && p != NULL && p->remove)
761 return TRUE;
762 if (sections_copied && (p == NULL || ! p->copy))
763 return TRUE;
764 return FALSE;
767 /* Choose which symbol entries to copy; put the result in OSYMS.
768 We don't copy in place, because that confuses the relocs.
769 Return the number of symbols to print. */
771 static unsigned int
772 filter_symbols (abfd, obfd, osyms, isyms, symcount)
773 bfd *abfd;
774 bfd *obfd;
775 asymbol **osyms, **isyms;
776 long symcount;
778 register asymbol **from = isyms, **to = osyms;
779 long src_count = 0, dst_count = 0;
780 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
781 == HAS_RELOC;
783 for (; src_count < symcount; src_count++)
785 asymbol *sym = from[src_count];
786 flagword flags = sym->flags;
787 char *name = (char *) bfd_asymbol_name (sym);
788 int keep;
789 bfd_boolean undefined;
790 bfd_boolean rem_leading_char;
791 bfd_boolean add_leading_char;
793 undefined = bfd_is_und_section (bfd_get_section (sym));
795 if (redefine_sym_list)
797 char *old_name, *new_name;
799 old_name = (char *) bfd_asymbol_name (sym);
800 new_name = (char *) lookup_sym_redefinition (old_name);
801 bfd_asymbol_name (sym) = new_name;
802 name = new_name;
805 /* Check if we will remove the current leading character. */
806 rem_leading_char =
807 (name[0] == bfd_get_symbol_leading_char (abfd))
808 && (change_leading_char
809 || (remove_leading_char
810 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
811 || undefined
812 || bfd_is_com_section (bfd_get_section (sym)))));
814 /* Check if we will add a new leading character. */
815 add_leading_char =
816 change_leading_char
817 && (bfd_get_symbol_leading_char (obfd) != '\0')
818 && (bfd_get_symbol_leading_char (abfd) == '\0'
819 || (name[0] == bfd_get_symbol_leading_char (abfd)));
821 /* Short circuit for change_leading_char if we can do it in-place. */
822 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
824 name[0] = bfd_get_symbol_leading_char (obfd);
825 bfd_asymbol_name (sym) = name;
826 rem_leading_char = FALSE;
827 add_leading_char = FALSE;
830 /* Remove leading char. */
831 if (rem_leading_char)
832 bfd_asymbol_name (sym) = ++name;
834 /* Add new leading char and/or prefix. */
835 if (add_leading_char || prefix_symbols_string)
837 char *n, *ptr;
839 ptr = n = xmalloc (1 + strlen (prefix_symbols_string) + strlen (name) + 1);
840 if (add_leading_char)
841 *ptr++ = bfd_get_symbol_leading_char (obfd);
843 if (prefix_symbols_string)
845 strcpy (ptr, prefix_symbols_string);
846 ptr += strlen (prefix_symbols_string);
849 strcpy (ptr, name);
850 bfd_asymbol_name (sym) = n;
851 name = n;
854 if (strip_symbols == STRIP_ALL)
855 keep = 0;
856 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
857 || ((flags & BSF_SECTION_SYM) != 0
858 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
859 & BSF_KEEP) != 0))
860 keep = 1;
861 else if (relocatable /* Relocatable file. */
862 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
863 keep = 1;
864 else if (bfd_decode_symclass (sym) == 'I')
865 /* Global symbols in $idata sections need to be retained
866 even if relocatable is FALSE. External users of the
867 library containing the $idata section may reference these
868 symbols. */
869 keep = 1;
870 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
871 || (flags & BSF_WEAK) != 0
872 || undefined
873 || bfd_is_com_section (bfd_get_section (sym)))
874 keep = strip_symbols != STRIP_UNNEEDED;
875 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
876 keep = (strip_symbols != STRIP_DEBUG
877 && strip_symbols != STRIP_UNNEEDED
878 && ! convert_debugging);
879 else if (bfd_get_section (sym)->comdat)
880 /* COMDAT sections store special information in local
881 symbols, so we cannot risk stripping any of them. */
882 keep = 1;
883 else /* Local symbol. */
884 keep = (strip_symbols != STRIP_UNNEEDED
885 && (discard_locals != LOCALS_ALL
886 && (discard_locals != LOCALS_START_L
887 || ! bfd_is_local_label (abfd, sym))));
889 if (keep && is_specified_symbol (name, strip_specific_list))
890 keep = 0;
891 if (!keep && is_specified_symbol (name, keep_specific_list))
892 keep = 1;
893 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
894 keep = 0;
896 if (keep && (flags & BSF_GLOBAL) != 0
897 && (weaken || is_specified_symbol (name, weaken_specific_list)))
899 sym->flags &=~ BSF_GLOBAL;
900 sym->flags |= BSF_WEAK;
902 if (keep && !undefined && (flags & (BSF_GLOBAL | BSF_WEAK))
903 && (is_specified_symbol (name, localize_specific_list)
904 || (keepglobal_specific_list != NULL
905 && ! is_specified_symbol (name, keepglobal_specific_list))))
907 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
908 sym->flags |= BSF_LOCAL;
911 if (keep)
912 to[dst_count++] = sym;
915 to[dst_count] = NULL;
917 return dst_count;
920 /* Find the redefined name of symbol SOURCE. */
922 static const char *
923 lookup_sym_redefinition (source)
924 const char *source;
926 struct redefine_node *list;
928 for (list = redefine_sym_list; list != NULL; list = list->next)
929 if (strcmp (source, list->source) == 0)
930 return list->target;
932 return source;
935 /* Add a node to a symbol redefine list. */
937 static void
938 redefine_list_append (source, target)
939 const char *source;
940 const char *target;
942 struct redefine_node **p;
943 struct redefine_node *list;
944 struct redefine_node *new_node;
946 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
948 if (strcmp (source, list->source) == 0)
949 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
950 "--redefine-sym",
951 source);
953 if (strcmp (target, list->target) == 0)
954 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
955 "--redefine-sym",
956 target);
959 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
961 new_node->source = strdup (source);
962 new_node->target = strdup (target);
963 new_node->next = NULL;
965 *p = new_node;
968 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
969 Adjust *SIZE. */
971 static void
972 filter_bytes (memhunk, size)
973 char *memhunk;
974 bfd_size_type *size;
976 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
978 for (; from < end; from += interleave)
979 *to++ = *from;
981 if (*size % interleave > (bfd_size_type) copy_byte)
982 *size = (*size / interleave) + 1;
983 else
984 *size /= interleave;
987 /* Copy object file IBFD onto OBFD. */
989 static void
990 copy_object (ibfd, obfd)
991 bfd *ibfd;
992 bfd *obfd;
994 bfd_vma start;
995 long symcount;
996 asection **osections = NULL;
997 bfd_size_type *gaps = NULL;
998 bfd_size_type max_gap = 0;
999 long symsize;
1000 PTR dhandle;
1001 enum bfd_architecture iarch;
1002 unsigned int imach;
1004 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1005 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1006 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1008 fatal (_("Unable to change endianness of input file(s)"));
1009 return;
1012 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1013 RETURN_NONFATAL (bfd_get_filename (obfd));
1015 if (verbose)
1016 printf (_("copy from %s(%s) to %s(%s)\n"),
1017 bfd_get_filename (ibfd), bfd_get_target (ibfd),
1018 bfd_get_filename (obfd), bfd_get_target (obfd));
1020 if (set_start_set)
1021 start = set_start;
1022 else
1023 start = bfd_get_start_address (ibfd);
1024 start += change_start;
1026 /* Neither the start address nor the flags
1027 need to be set for a core file. */
1028 if (bfd_get_format (obfd) != bfd_core)
1030 if (!bfd_set_start_address (obfd, start)
1031 || !bfd_set_file_flags (obfd,
1032 (bfd_get_file_flags (ibfd)
1033 & bfd_applicable_file_flags (obfd))))
1034 RETURN_NONFATAL (bfd_get_filename (ibfd));
1037 /* Copy architecture of input file to output file. */
1038 iarch = bfd_get_arch (ibfd);
1039 imach = bfd_get_mach (ibfd);
1040 if (!bfd_set_arch_mach (obfd, iarch, imach)
1041 && (ibfd->target_defaulted
1042 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1043 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1044 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1045 bfd_get_mach (ibfd)));
1047 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1048 RETURN_NONFATAL (bfd_get_filename (ibfd));
1050 if (isympp)
1051 free (isympp);
1053 if (osympp != isympp)
1054 free (osympp);
1056 /* BFD mandates that all output sections be created and sizes set before
1057 any output is done. Thus, we traverse all sections multiple times. */
1058 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
1060 if (add_sections != NULL)
1062 struct section_add *padd;
1063 struct section_list *pset;
1065 for (padd = add_sections; padd != NULL; padd = padd->next)
1067 padd->section = bfd_make_section (obfd, padd->name);
1068 if (padd->section == NULL)
1070 non_fatal (_("can't create section `%s': %s"),
1071 padd->name, bfd_errmsg (bfd_get_error ()));
1072 status = 1;
1073 return;
1075 else
1077 flagword flags;
1079 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1080 RETURN_NONFATAL (bfd_get_filename (obfd));
1082 pset = find_section_list (padd->name, FALSE);
1083 if (pset != NULL)
1084 pset->used = TRUE;
1086 if (pset != NULL && pset->set_flags)
1087 flags = pset->flags | SEC_HAS_CONTENTS;
1088 else
1089 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1091 if (! bfd_set_section_flags (obfd, padd->section, flags))
1092 RETURN_NONFATAL (bfd_get_filename (obfd));
1094 if (pset != NULL)
1096 if (pset->change_vma != CHANGE_IGNORE)
1097 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
1098 RETURN_NONFATAL (bfd_get_filename (obfd));
1100 if (pset->change_lma != CHANGE_IGNORE)
1102 padd->section->lma = pset->lma_val;
1104 if (! bfd_set_section_alignment
1105 (obfd, padd->section,
1106 bfd_section_alignment (obfd, padd->section)))
1107 RETURN_NONFATAL (bfd_get_filename (obfd));
1114 if (gap_fill_set || pad_to_set)
1116 asection **set;
1117 unsigned int c, i;
1119 /* We must fill in gaps between the sections and/or we must pad
1120 the last section to a specified address. We do this by
1121 grabbing a list of the sections, sorting them by VMA, and
1122 increasing the section sizes as required to fill the gaps.
1123 We write out the gap contents below. */
1125 c = bfd_count_sections (obfd);
1126 osections = (asection **) xmalloc (c * sizeof (asection *));
1127 set = osections;
1128 bfd_map_over_sections (obfd, get_sections, (void *) &set);
1130 qsort (osections, c, sizeof (asection *), compare_section_lma);
1132 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
1133 memset (gaps, 0, c * sizeof (bfd_size_type));
1135 if (gap_fill_set)
1137 for (i = 0; i < c - 1; i++)
1139 flagword flags;
1140 bfd_size_type size;
1141 bfd_vma gap_start, gap_stop;
1143 flags = bfd_get_section_flags (obfd, osections[i]);
1144 if ((flags & SEC_HAS_CONTENTS) == 0
1145 || (flags & SEC_LOAD) == 0)
1146 continue;
1148 size = bfd_section_size (obfd, osections[i]);
1149 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1150 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1151 if (gap_start < gap_stop)
1153 if (! bfd_set_section_size (obfd, osections[i],
1154 size + (gap_stop - gap_start)))
1156 non_fatal (_("Can't fill gap after %s: %s"),
1157 bfd_get_section_name (obfd, osections[i]),
1158 bfd_errmsg (bfd_get_error ()));
1159 status = 1;
1160 break;
1162 gaps[i] = gap_stop - gap_start;
1163 if (max_gap < gap_stop - gap_start)
1164 max_gap = gap_stop - gap_start;
1169 if (pad_to_set)
1171 bfd_vma lma;
1172 bfd_size_type size;
1174 lma = bfd_section_lma (obfd, osections[c - 1]);
1175 size = bfd_section_size (obfd, osections[c - 1]);
1176 if (lma + size < pad_to)
1178 if (! bfd_set_section_size (obfd, osections[c - 1],
1179 pad_to - lma))
1181 non_fatal (_("Can't add padding to %s: %s"),
1182 bfd_get_section_name (obfd, osections[c - 1]),
1183 bfd_errmsg (bfd_get_error ()));
1184 status = 1;
1186 else
1188 gaps[c - 1] = pad_to - (lma + size);
1189 if (max_gap < pad_to - (lma + size))
1190 max_gap = pad_to - (lma + size);
1196 /* Symbol filtering must happen after the output sections
1197 have been created, but before their contents are set. */
1198 dhandle = NULL;
1199 symsize = bfd_get_symtab_upper_bound (ibfd);
1200 if (symsize < 0)
1201 RETURN_NONFATAL (bfd_get_filename (ibfd));
1203 osympp = isympp = (asymbol **) xmalloc (symsize);
1204 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1205 if (symcount < 0)
1206 RETURN_NONFATAL (bfd_get_filename (ibfd));
1208 if (convert_debugging)
1209 dhandle = read_debugging_info (ibfd, isympp, symcount);
1211 if (strip_symbols == STRIP_DEBUG
1212 || strip_symbols == STRIP_ALL
1213 || strip_symbols == STRIP_UNNEEDED
1214 || discard_locals != LOCALS_UNDEF
1215 || strip_specific_list != NULL
1216 || keep_specific_list != NULL
1217 || localize_specific_list != NULL
1218 || keepglobal_specific_list != NULL
1219 || weaken_specific_list != NULL
1220 || prefix_symbols_string
1221 || sections_removed
1222 || sections_copied
1223 || convert_debugging
1224 || change_leading_char
1225 || remove_leading_char
1226 || redefine_sym_list
1227 || weaken)
1229 /* Mark symbols used in output relocations so that they
1230 are kept, even if they are local labels or static symbols.
1232 Note we iterate over the input sections examining their
1233 relocations since the relocations for the output sections
1234 haven't been set yet. mark_symbols_used_in_relocations will
1235 ignore input sections which have no corresponding output
1236 section. */
1237 if (strip_symbols != STRIP_ALL)
1238 bfd_map_over_sections (ibfd,
1239 mark_symbols_used_in_relocations,
1240 (PTR)isympp);
1241 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1242 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1245 if (convert_debugging && dhandle != NULL)
1247 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1249 status = 1;
1250 return;
1254 bfd_set_symtab (obfd, osympp, symcount);
1256 /* This has to happen after the symbol table has been set. */
1257 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
1259 if (add_sections != NULL)
1261 struct section_add *padd;
1263 for (padd = add_sections; padd != NULL; padd = padd->next)
1265 if (! bfd_set_section_contents (obfd, padd->section,
1266 (PTR) padd->contents,
1267 (file_ptr) 0,
1268 (bfd_size_type) padd->size))
1269 RETURN_NONFATAL (bfd_get_filename (obfd));
1273 if (gap_fill_set || pad_to_set)
1275 bfd_byte *buf;
1276 int c, i;
1278 /* Fill in the gaps. */
1279 if (max_gap > 8192)
1280 max_gap = 8192;
1281 buf = (bfd_byte *) xmalloc (max_gap);
1282 memset (buf, gap_fill, (size_t) max_gap);
1284 c = bfd_count_sections (obfd);
1285 for (i = 0; i < c; i++)
1287 if (gaps[i] != 0)
1289 bfd_size_type left;
1290 file_ptr off;
1292 left = gaps[i];
1293 off = bfd_section_size (obfd, osections[i]) - left;
1295 while (left > 0)
1297 bfd_size_type now;
1299 if (left > 8192)
1300 now = 8192;
1301 else
1302 now = left;
1304 if (! bfd_set_section_contents (obfd, osections[i], buf,
1305 off, now))
1306 RETURN_NONFATAL (bfd_get_filename (obfd));
1308 left -= now;
1309 off += now;
1315 /* Allow the BFD backend to copy any private data it understands
1316 from the input BFD to the output BFD. This is done last to
1317 permit the routine to look at the filtered symbol table, which is
1318 important for the ECOFF code at least. */
1319 if (! bfd_copy_private_bfd_data (ibfd, obfd))
1321 non_fatal (_("%s: error copying private BFD data: %s"),
1322 bfd_get_filename (obfd),
1323 bfd_errmsg (bfd_get_error ()));
1324 status = 1;
1325 return;
1328 /* Switch to the alternate machine code. We have to do this at the
1329 very end, because we only initialize the header when we create
1330 the first section. */
1331 if (use_alt_mach_code != 0)
1333 if (!bfd_alt_mach_code (obfd, use_alt_mach_code))
1334 non_fatal (_("unknown alternate machine code, ignored"));
1338 #undef MKDIR
1339 #if defined (_WIN32) && !defined (__CYGWIN32__)
1340 #define MKDIR(DIR, MODE) mkdir (DIR)
1341 #else
1342 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1343 #endif
1345 /* Read each archive element in turn from IBFD, copy the
1346 contents to temp file, and keep the temp file handle. */
1348 static void
1349 copy_archive (ibfd, obfd, output_target)
1350 bfd *ibfd;
1351 bfd *obfd;
1352 const char *output_target;
1354 struct name_list
1356 struct name_list *next;
1357 const char *name;
1358 bfd *obfd;
1359 } *list, *l;
1360 bfd **ptr = &obfd->archive_head;
1361 bfd *this_element;
1362 char *dir = make_tempname (bfd_get_filename (obfd));
1364 /* Make a temp directory to hold the contents. */
1365 if (MKDIR (dir, 0700) != 0)
1367 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1368 dir, strerror (errno));
1370 obfd->has_armap = ibfd->has_armap;
1372 list = NULL;
1374 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1376 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1377 RETURN_NONFATAL (bfd_get_filename (obfd));
1379 while (!status && this_element != (bfd *) NULL)
1381 char *output_name;
1382 bfd *output_bfd;
1383 bfd *last_element;
1384 struct stat buf;
1385 int stat_status = 0;
1387 /* Create an output file for this member. */
1388 output_name = concat (dir, "/",
1389 bfd_get_filename (this_element), (char *) 0);
1391 /* If the file already exists, make another temp dir. */
1392 if (stat (output_name, &buf) >= 0)
1394 output_name = make_tempname (output_name);
1395 if (MKDIR (output_name, 0700) != 0)
1397 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1398 output_name, strerror (errno));
1400 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1401 l->name = output_name;
1402 l->next = list;
1403 l->obfd = NULL;
1404 list = l;
1405 output_name = concat (output_name, "/",
1406 bfd_get_filename (this_element), (char *) 0);
1409 output_bfd = bfd_openw (output_name, output_target);
1410 if (preserve_dates)
1412 stat_status = bfd_stat_arch_elt (this_element, &buf);
1414 if (stat_status != 0)
1415 non_fatal (_("internal stat error on %s"),
1416 bfd_get_filename (this_element));
1419 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1420 l->name = output_name;
1421 l->next = list;
1422 list = l;
1424 if (output_bfd == (bfd *) NULL)
1425 RETURN_NONFATAL (output_name);
1427 if (bfd_check_format (this_element, bfd_object))
1428 copy_object (this_element, output_bfd);
1430 if (!bfd_close (output_bfd))
1432 bfd_nonfatal (bfd_get_filename (output_bfd));
1433 /* Error in new object file. Don't change archive. */
1434 status = 1;
1437 if (preserve_dates && stat_status == 0)
1438 set_times (output_name, &buf);
1440 /* Open the newly output file and attach to our list. */
1441 output_bfd = bfd_openr (output_name, output_target);
1443 l->obfd = output_bfd;
1445 *ptr = output_bfd;
1446 ptr = &output_bfd->next;
1448 last_element = this_element;
1450 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1452 bfd_close (last_element);
1454 *ptr = (bfd *) NULL;
1456 if (!bfd_close (obfd))
1457 RETURN_NONFATAL (bfd_get_filename (obfd));
1459 if (!bfd_close (ibfd))
1460 RETURN_NONFATAL (bfd_get_filename (ibfd));
1462 /* Delete all the files that we opened. */
1463 for (l = list; l != NULL; l = l->next)
1465 if (l->obfd == NULL)
1466 rmdir (l->name);
1467 else
1469 bfd_close (l->obfd);
1470 unlink (l->name);
1473 rmdir (dir);
1476 /* The top-level control. */
1478 static void
1479 copy_file (input_filename, output_filename, input_target, output_target)
1480 const char *input_filename;
1481 const char *output_filename;
1482 const char *input_target;
1483 const char *output_target;
1485 bfd *ibfd;
1486 char **obj_matching;
1487 char **core_matching;
1489 /* To allow us to do "strip *" without dying on the first
1490 non-object file, failures are nonfatal. */
1491 ibfd = bfd_openr (input_filename, input_target);
1492 if (ibfd == NULL)
1493 RETURN_NONFATAL (input_filename);
1495 if (bfd_check_format (ibfd, bfd_archive))
1497 bfd *obfd;
1499 /* bfd_get_target does not return the correct value until
1500 bfd_check_format succeeds. */
1501 if (output_target == NULL)
1502 output_target = bfd_get_target (ibfd);
1504 obfd = bfd_openw (output_filename, output_target);
1505 if (obfd == NULL)
1506 RETURN_NONFATAL (output_filename);
1508 copy_archive (ibfd, obfd, output_target);
1510 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
1512 bfd *obfd;
1513 do_copy:
1514 /* bfd_get_target does not return the correct value until
1515 bfd_check_format succeeds. */
1516 if (output_target == NULL)
1517 output_target = bfd_get_target (ibfd);
1519 obfd = bfd_openw (output_filename, output_target);
1520 if (obfd == NULL)
1521 RETURN_NONFATAL (output_filename);
1523 copy_object (ibfd, obfd);
1525 if (!bfd_close (obfd))
1526 RETURN_NONFATAL (output_filename);
1528 if (!bfd_close (ibfd))
1529 RETURN_NONFATAL (input_filename);
1531 else
1533 bfd_error_type obj_error = bfd_get_error ();
1534 bfd_error_type core_error;
1536 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1538 /* This probably can't happen.. */
1539 if (obj_error == bfd_error_file_ambiguously_recognized)
1540 free (obj_matching);
1541 goto do_copy;
1544 core_error = bfd_get_error ();
1545 /* Report the object error in preference to the core error. */
1546 if (obj_error != core_error)
1547 bfd_set_error (obj_error);
1549 bfd_nonfatal (input_filename);
1551 if (obj_error == bfd_error_file_ambiguously_recognized)
1553 list_matching_formats (obj_matching);
1554 free (obj_matching);
1556 if (core_error == bfd_error_file_ambiguously_recognized)
1558 list_matching_formats (core_matching);
1559 free (core_matching);
1562 status = 1;
1566 /* Add a name to the section renaming list. */
1568 static void
1569 add_section_rename (old_name, new_name, flags)
1570 const char * old_name;
1571 const char * new_name;
1572 flagword flags;
1574 section_rename * rename;
1576 /* Check for conflicts first. */
1577 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1578 if (strcmp (rename->old_name, old_name) == 0)
1580 /* Silently ignore duplicate definitions. */
1581 if (strcmp (rename->new_name, new_name) == 0
1582 && rename->flags == flags)
1583 return;
1585 fatal (_("Multiple renames of section %s"), old_name);
1588 rename = (section_rename *) xmalloc (sizeof (* rename));
1590 rename->old_name = old_name;
1591 rename->new_name = new_name;
1592 rename->flags = flags;
1593 rename->next = section_rename_list;
1595 section_rename_list = rename;
1598 /* Check the section rename list for a new name of the input section
1599 ISECTION. Return the new name if one is found.
1600 Also set RETURNED_FLAGS to the flags to be used for this section. */
1602 static const char *
1603 find_section_rename (ibfd, isection, returned_flags)
1604 bfd * ibfd ATTRIBUTE_UNUSED;
1605 sec_ptr isection;
1606 flagword * returned_flags;
1608 const char * old_name = bfd_section_name (ibfd, isection);
1609 section_rename * rename;
1611 /* Default to using the flags of the input section. */
1612 * returned_flags = bfd_get_section_flags (ibfd, isection);
1614 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1615 if (strcmp (rename->old_name, old_name) == 0)
1617 if (rename->flags != (flagword) -1)
1618 * returned_flags = rename->flags;
1620 return rename->new_name;
1623 return old_name;
1626 /* Create a section in OBFD with the same
1627 name and attributes as ISECTION in IBFD. */
1629 static void
1630 setup_section (ibfd, isection, obfdarg)
1631 bfd *ibfd;
1632 sec_ptr isection;
1633 PTR obfdarg;
1635 bfd *obfd = (bfd *) obfdarg;
1636 struct section_list *p;
1637 sec_ptr osection;
1638 bfd_size_type size;
1639 bfd_vma vma;
1640 bfd_vma lma;
1641 flagword flags;
1642 const char *err;
1643 const char * name;
1644 char *prefix = NULL;
1646 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1647 && (strip_symbols == STRIP_DEBUG
1648 || strip_symbols == STRIP_UNNEEDED
1649 || strip_symbols == STRIP_ALL
1650 || discard_locals == LOCALS_ALL
1651 || convert_debugging))
1652 return;
1654 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
1655 if (p != NULL)
1656 p->used = TRUE;
1658 if (sections_removed && p != NULL && p->remove)
1659 return;
1660 if (sections_copied && (p == NULL || ! p->copy))
1661 return;
1663 /* Get the, possibly new, name of the output section. */
1664 name = find_section_rename (ibfd, isection, & flags);
1666 /* Prefix sections. */
1667 if ((prefix_alloc_sections_string) && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
1668 prefix = prefix_alloc_sections_string;
1669 else if (prefix_sections_string)
1670 prefix = prefix_sections_string;
1672 if (prefix)
1674 char *n;
1676 n = xmalloc (strlen (prefix) + strlen (name) + 1);
1677 strcpy (n, prefix);
1678 strcat (n, name);
1679 name = n;
1682 osection = bfd_make_section_anyway (obfd, name);
1684 if (osection == NULL)
1686 err = _("making");
1687 goto loser;
1690 size = bfd_section_size (ibfd, isection);
1691 if (copy_byte >= 0)
1692 size = (size + interleave - 1) / interleave;
1693 if (! bfd_set_section_size (obfd, osection, size))
1695 err = _("size");
1696 goto loser;
1699 vma = bfd_section_vma (ibfd, isection);
1700 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1701 vma += p->vma_val;
1702 else if (p != NULL && p->change_vma == CHANGE_SET)
1703 vma = p->vma_val;
1704 else
1705 vma += change_section_address;
1707 if (! bfd_set_section_vma (obfd, osection, vma))
1709 err = _("vma");
1710 goto loser;
1713 lma = isection->lma;
1714 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1716 if (p->change_lma == CHANGE_MODIFY)
1717 lma += p->lma_val;
1718 else if (p->change_lma == CHANGE_SET)
1719 lma = p->lma_val;
1720 else
1721 abort ();
1723 else
1724 lma += change_section_address;
1726 osection->lma = lma;
1728 /* FIXME: This is probably not enough. If we change the LMA we
1729 may have to recompute the header for the file as well. */
1730 if (!bfd_set_section_alignment (obfd,
1731 osection,
1732 bfd_section_alignment (ibfd, isection)))
1734 err = _("alignment");
1735 goto loser;
1738 if (p != NULL && p->set_flags)
1739 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
1740 if (!bfd_set_section_flags (obfd, osection, flags))
1742 err = _("flags");
1743 goto loser;
1746 /* Copy merge entity size. */
1747 osection->entsize = isection->entsize;
1749 /* This used to be mangle_section; we do here to avoid using
1750 bfd_get_section_by_name since some formats allow multiple
1751 sections with the same name. */
1752 isection->output_section = osection;
1753 isection->output_offset = 0;
1755 /* Allow the BFD backend to copy any private data it understands
1756 from the input section to the output section. */
1757 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1759 err = _("private data");
1760 goto loser;
1763 /* All went well. */
1764 return;
1766 loser:
1767 non_fatal (_("%s: section `%s': error in %s: %s"),
1768 bfd_get_filename (ibfd),
1769 bfd_section_name (ibfd, isection),
1770 err, bfd_errmsg (bfd_get_error ()));
1771 status = 1;
1774 /* Copy the data of input section ISECTION of IBFD
1775 to an output section with the same name in OBFD.
1776 If stripping then don't copy any relocation info. */
1778 static void
1779 copy_section (ibfd, isection, obfdarg)
1780 bfd *ibfd;
1781 sec_ptr isection;
1782 PTR obfdarg;
1784 bfd *obfd = (bfd *) obfdarg;
1785 struct section_list *p;
1786 arelent **relpp;
1787 long relcount;
1788 sec_ptr osection;
1789 bfd_size_type size;
1790 long relsize;
1791 flagword flags;
1793 /* If we have already failed earlier on,
1794 do not keep on generating complaints now. */
1795 if (status != 0)
1796 return;
1798 flags = bfd_get_section_flags (ibfd, isection);
1799 if ((flags & SEC_DEBUGGING) != 0
1800 && (strip_symbols == STRIP_DEBUG
1801 || strip_symbols == STRIP_UNNEEDED
1802 || strip_symbols == STRIP_ALL
1803 || discard_locals == LOCALS_ALL
1804 || convert_debugging))
1805 return;
1807 if ((flags & SEC_GROUP) != 0)
1808 return;
1810 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
1812 if (sections_removed && p != NULL && p->remove)
1813 return;
1814 if (sections_copied && (p == NULL || ! p->copy))
1815 return;
1817 osection = isection->output_section;
1818 size = bfd_get_section_size_before_reloc (isection);
1820 if (size == 0 || osection == 0)
1821 return;
1823 /* Core files do not need to be relocated. */
1824 if (bfd_get_format (obfd) == bfd_core)
1825 relsize = 0;
1826 else
1827 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1829 if (relsize < 0)
1830 RETURN_NONFATAL (bfd_get_filename (ibfd));
1832 if (relsize == 0)
1833 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1834 else
1836 relpp = (arelent **) xmalloc (relsize);
1837 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1838 if (relcount < 0)
1839 RETURN_NONFATAL (bfd_get_filename (ibfd));
1841 if (strip_symbols == STRIP_ALL)
1843 /* Remove relocations which are not in
1844 keep_strip_specific_list. */
1845 arelent **temp_relpp;
1846 long temp_relcount = 0;
1847 long i;
1849 temp_relpp = (arelent **) xmalloc (relsize);
1850 for (i = 0; i < relcount; i++)
1851 if (is_specified_symbol
1852 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1853 keep_specific_list))
1854 temp_relpp [temp_relcount++] = relpp [i];
1855 relcount = temp_relcount;
1856 free (relpp);
1857 relpp = temp_relpp;
1860 bfd_set_reloc (obfd, osection,
1861 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1864 isection->_cooked_size = isection->_raw_size;
1865 isection->reloc_done = TRUE;
1867 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
1868 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
1870 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1872 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1873 size))
1874 RETURN_NONFATAL (bfd_get_filename (ibfd));
1876 if (copy_byte >= 0)
1877 filter_bytes (memhunk, &size);
1879 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1880 size))
1881 RETURN_NONFATAL (bfd_get_filename (obfd));
1883 free (memhunk);
1885 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1887 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1889 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1890 flag--they can just remove the section entirely and add it
1891 back again. However, we do permit them to turn on the
1892 SEC_HAS_CONTENTS flag, and take it to mean that the section
1893 contents should be zeroed out. */
1895 memset (memhunk, 0, size);
1896 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1897 size))
1898 RETURN_NONFATAL (bfd_get_filename (obfd));
1899 free (memhunk);
1903 /* Get all the sections. This is used when --gap-fill or --pad-to is
1904 used. */
1906 static void
1907 get_sections (obfd, osection, secppparg)
1908 bfd *obfd ATTRIBUTE_UNUSED;
1909 asection *osection;
1910 PTR secppparg;
1912 asection ***secppp = (asection ***) secppparg;
1914 **secppp = osection;
1915 ++(*secppp);
1918 /* Sort sections by VMA. This is called via qsort, and is used when
1919 --gap-fill or --pad-to is used. We force non loadable or empty
1920 sections to the front, where they are easier to ignore. */
1922 static int
1923 compare_section_lma (arg1, arg2)
1924 const PTR arg1;
1925 const PTR arg2;
1927 const asection **sec1 = (const asection **) arg1;
1928 const asection **sec2 = (const asection **) arg2;
1929 flagword flags1, flags2;
1931 /* Sort non loadable sections to the front. */
1932 flags1 = (*sec1)->flags;
1933 flags2 = (*sec2)->flags;
1934 if ((flags1 & SEC_HAS_CONTENTS) == 0
1935 || (flags1 & SEC_LOAD) == 0)
1937 if ((flags2 & SEC_HAS_CONTENTS) != 0
1938 && (flags2 & SEC_LOAD) != 0)
1939 return -1;
1941 else
1943 if ((flags2 & SEC_HAS_CONTENTS) == 0
1944 || (flags2 & SEC_LOAD) == 0)
1945 return 1;
1948 /* Sort sections by LMA. */
1949 if ((*sec1)->lma > (*sec2)->lma)
1950 return 1;
1951 else if ((*sec1)->lma < (*sec2)->lma)
1952 return -1;
1954 /* Sort sections with the same LMA by size. */
1955 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1956 return 1;
1957 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1958 return -1;
1960 return 0;
1963 /* Mark all the symbols which will be used in output relocations with
1964 the BSF_KEEP flag so that those symbols will not be stripped.
1966 Ignore relocations which will not appear in the output file. */
1968 static void
1969 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1970 bfd *ibfd;
1971 sec_ptr isection;
1972 PTR symbolsarg;
1974 asymbol **symbols = (asymbol **) symbolsarg;
1975 long relsize;
1976 arelent **relpp;
1977 long relcount, i;
1979 /* Ignore an input section with no corresponding output section. */
1980 if (isection->output_section == NULL)
1981 return;
1983 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1984 if (relsize < 0)
1985 bfd_fatal (bfd_get_filename (ibfd));
1987 if (relsize == 0)
1988 return;
1990 relpp = (arelent **) xmalloc (relsize);
1991 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1992 if (relcount < 0)
1993 bfd_fatal (bfd_get_filename (ibfd));
1995 /* Examine each symbol used in a relocation. If it's not one of the
1996 special bfd section symbols, then mark it with BSF_KEEP. */
1997 for (i = 0; i < relcount; i++)
1999 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2000 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2001 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2002 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2005 if (relpp != NULL)
2006 free (relpp);
2009 /* Write out debugging information. */
2011 static bfd_boolean
2012 write_debugging_info (obfd, dhandle, symcountp, symppp)
2013 bfd *obfd;
2014 PTR dhandle;
2015 long *symcountp ATTRIBUTE_UNUSED;
2016 asymbol ***symppp ATTRIBUTE_UNUSED;
2018 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2019 return write_ieee_debugging_info (obfd, dhandle);
2021 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2022 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2024 bfd_byte *syms, *strings;
2025 bfd_size_type symsize, stringsize;
2026 asection *stabsec, *stabstrsec;
2028 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2029 &symsize, &strings,
2030 &stringsize))
2031 return FALSE;
2033 stabsec = bfd_make_section (obfd, ".stab");
2034 stabstrsec = bfd_make_section (obfd, ".stabstr");
2035 if (stabsec == NULL
2036 || stabstrsec == NULL
2037 || ! bfd_set_section_size (obfd, stabsec, symsize)
2038 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2039 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2040 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
2041 || ! bfd_set_section_flags (obfd, stabsec,
2042 (SEC_HAS_CONTENTS
2043 | SEC_READONLY
2044 | SEC_DEBUGGING))
2045 || ! bfd_set_section_flags (obfd, stabstrsec,
2046 (SEC_HAS_CONTENTS
2047 | SEC_READONLY
2048 | SEC_DEBUGGING)))
2050 non_fatal (_("%s: can't create debugging section: %s"),
2051 bfd_get_filename (obfd),
2052 bfd_errmsg (bfd_get_error ()));
2053 return FALSE;
2056 /* We can get away with setting the section contents now because
2057 the next thing the caller is going to do is copy over the
2058 real sections. We may someday have to split the contents
2059 setting out of this function. */
2060 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
2061 symsize)
2062 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
2063 (file_ptr) 0, stringsize))
2065 non_fatal (_("%s: can't set debugging section contents: %s"),
2066 bfd_get_filename (obfd),
2067 bfd_errmsg (bfd_get_error ()));
2068 return FALSE;
2071 return TRUE;
2074 non_fatal (_("%s: don't know how to write debugging information for %s"),
2075 bfd_get_filename (obfd), bfd_get_target (obfd));
2076 return FALSE;
2079 static int
2080 strip_main (argc, argv)
2081 int argc;
2082 char *argv[];
2084 char *input_target = NULL, *output_target = NULL;
2085 bfd_boolean show_version = FALSE;
2086 int c, i;
2087 struct section_list *p;
2088 char *output_file = NULL;
2090 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVv",
2091 strip_options, (int *) 0)) != EOF)
2093 switch (c)
2095 case 'I':
2096 input_target = optarg;
2097 break;
2098 case 'O':
2099 output_target = optarg;
2100 break;
2101 case 'F':
2102 input_target = output_target = optarg;
2103 break;
2104 case 'R':
2105 p = find_section_list (optarg, TRUE);
2106 p->remove = TRUE;
2107 sections_removed = TRUE;
2108 break;
2109 case 's':
2110 strip_symbols = STRIP_ALL;
2111 break;
2112 case 'S':
2113 case 'g':
2114 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2115 strip_symbols = STRIP_DEBUG;
2116 break;
2117 case OPTION_STRIP_UNNEEDED:
2118 strip_symbols = STRIP_UNNEEDED;
2119 break;
2120 case 'K':
2121 add_specific_symbol (optarg, &keep_specific_list);
2122 break;
2123 case 'N':
2124 add_specific_symbol (optarg, &strip_specific_list);
2125 break;
2126 case 'o':
2127 output_file = optarg;
2128 break;
2129 case 'p':
2130 preserve_dates = TRUE;
2131 break;
2132 case 'x':
2133 discard_locals = LOCALS_ALL;
2134 break;
2135 case 'X':
2136 discard_locals = LOCALS_START_L;
2137 break;
2138 case 'v':
2139 verbose = TRUE;
2140 break;
2141 case 'V':
2142 show_version = TRUE;
2143 break;
2144 case 0:
2145 /* We've been given a long option. */
2146 break;
2147 case 'H':
2148 case 'h':
2149 strip_usage (stdout, 0);
2150 default:
2151 strip_usage (stderr, 1);
2155 if (show_version)
2156 print_version ("strip");
2158 /* Default is to strip all symbols. */
2159 if (strip_symbols == STRIP_UNDEF
2160 && discard_locals == LOCALS_UNDEF
2161 && strip_specific_list == NULL)
2162 strip_symbols = STRIP_ALL;
2164 if (output_target == (char *) NULL)
2165 output_target = input_target;
2167 i = optind;
2168 if (i == argc
2169 || (output_file != NULL && (i + 1) < argc))
2170 strip_usage (stderr, 1);
2172 for (; i < argc; i++)
2174 int hold_status = status;
2175 struct stat statbuf;
2176 char *tmpname;
2178 if (preserve_dates)
2180 if (stat (argv[i], &statbuf) < 0)
2182 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
2183 continue;
2187 if (output_file != NULL)
2188 tmpname = output_file;
2189 else
2190 tmpname = make_tempname (argv[i]);
2191 status = 0;
2193 copy_file (argv[i], tmpname, input_target, output_target);
2194 if (status == 0)
2196 if (preserve_dates)
2197 set_times (tmpname, &statbuf);
2198 if (output_file == NULL)
2199 smart_rename (tmpname, argv[i], preserve_dates);
2200 status = hold_status;
2202 else
2203 unlink (tmpname);
2204 if (output_file == NULL)
2205 free (tmpname);
2208 return 0;
2211 static int
2212 copy_main (argc, argv)
2213 int argc;
2214 char *argv[];
2216 char * binary_architecture = NULL;
2217 char *input_filename = NULL, *output_filename = NULL;
2218 char *input_target = NULL, *output_target = NULL;
2219 bfd_boolean show_version = FALSE;
2220 bfd_boolean change_warn = TRUE;
2221 int c;
2222 struct section_list *p;
2223 struct stat statbuf;
2225 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2226 copy_options, (int *) 0)) != EOF)
2228 switch (c)
2230 case 'b':
2231 copy_byte = atoi (optarg);
2232 if (copy_byte < 0)
2233 fatal (_("byte number must be non-negative"));
2234 break;
2236 case 'B':
2237 binary_architecture = optarg;
2238 break;
2240 case 'i':
2241 interleave = atoi (optarg);
2242 if (interleave < 1)
2243 fatal (_("interleave must be positive"));
2244 break;
2246 case 'I':
2247 case 's': /* "source" - 'I' is preferred */
2248 input_target = optarg;
2249 break;
2251 case 'O':
2252 case 'd': /* "destination" - 'O' is preferred */
2253 output_target = optarg;
2254 break;
2256 case 'F':
2257 input_target = output_target = optarg;
2258 break;
2260 case 'j':
2261 p = find_section_list (optarg, TRUE);
2262 if (p->remove)
2263 fatal (_("%s both copied and removed"), optarg);
2264 p->copy = TRUE;
2265 sections_copied = TRUE;
2266 break;
2268 case 'R':
2269 p = find_section_list (optarg, TRUE);
2270 if (p->copy)
2271 fatal (_("%s both copied and removed"), optarg);
2272 p->remove = TRUE;
2273 sections_removed = TRUE;
2274 break;
2276 case 'S':
2277 strip_symbols = STRIP_ALL;
2278 break;
2280 case 'g':
2281 strip_symbols = STRIP_DEBUG;
2282 break;
2284 case OPTION_STRIP_UNNEEDED:
2285 strip_symbols = STRIP_UNNEEDED;
2286 break;
2288 case 'K':
2289 add_specific_symbol (optarg, &keep_specific_list);
2290 break;
2292 case 'N':
2293 add_specific_symbol (optarg, &strip_specific_list);
2294 break;
2296 case 'L':
2297 add_specific_symbol (optarg, &localize_specific_list);
2298 break;
2300 case 'G':
2301 add_specific_symbol (optarg, &keepglobal_specific_list);
2302 break;
2304 case 'W':
2305 add_specific_symbol (optarg, &weaken_specific_list);
2306 break;
2308 case 'p':
2309 preserve_dates = TRUE;
2310 break;
2312 case 'x':
2313 discard_locals = LOCALS_ALL;
2314 break;
2316 case 'X':
2317 discard_locals = LOCALS_START_L;
2318 break;
2320 case 'v':
2321 verbose = TRUE;
2322 break;
2324 case 'V':
2325 show_version = TRUE;
2326 break;
2328 case OPTION_WEAKEN:
2329 weaken = TRUE;
2330 break;
2332 case OPTION_ADD_SECTION:
2334 const char *s;
2335 struct stat st;
2336 struct section_add *pa;
2337 int len;
2338 char *name;
2339 FILE *f;
2341 s = strchr (optarg, '=');
2343 if (s == NULL)
2344 fatal (_("bad format for %s"), "--add-section");
2346 if (stat (s + 1, & st) < 0)
2347 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
2349 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
2351 len = s - optarg;
2352 name = (char *) xmalloc (len + 1);
2353 strncpy (name, optarg, len);
2354 name[len] = '\0';
2355 pa->name = name;
2357 pa->filename = s + 1;
2359 pa->size = st.st_size;
2361 pa->contents = (bfd_byte *) xmalloc (pa->size);
2362 f = fopen (pa->filename, FOPEN_RB);
2364 if (f == NULL)
2365 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
2367 if (fread (pa->contents, 1, pa->size, f) == 0
2368 || ferror (f))
2369 fatal (_("%s: fread failed"), pa->filename);
2371 fclose (f);
2373 pa->next = add_sections;
2374 add_sections = pa;
2376 break;
2378 case OPTION_CHANGE_START:
2379 change_start = parse_vma (optarg, "--change-start");
2380 break;
2382 case OPTION_CHANGE_SECTION_ADDRESS:
2383 case OPTION_CHANGE_SECTION_LMA:
2384 case OPTION_CHANGE_SECTION_VMA:
2386 const char *s;
2387 int len;
2388 char *name;
2389 char *option = NULL;
2390 bfd_vma val;
2391 enum change_action what = CHANGE_IGNORE;
2393 switch (c)
2395 case OPTION_CHANGE_SECTION_ADDRESS:
2396 option = "--change-section-address";
2397 break;
2398 case OPTION_CHANGE_SECTION_LMA:
2399 option = "--change-section-lma";
2400 break;
2401 case OPTION_CHANGE_SECTION_VMA:
2402 option = "--change-section-vma";
2403 break;
2406 s = strchr (optarg, '=');
2407 if (s == NULL)
2409 s = strchr (optarg, '+');
2410 if (s == NULL)
2412 s = strchr (optarg, '-');
2413 if (s == NULL)
2414 fatal (_("bad format for %s"), option);
2418 len = s - optarg;
2419 name = (char *) xmalloc (len + 1);
2420 strncpy (name, optarg, len);
2421 name[len] = '\0';
2423 p = find_section_list (name, TRUE);
2425 val = parse_vma (s + 1, option);
2427 switch (*s)
2429 case '=': what = CHANGE_SET; break;
2430 case '-': val = - val; /* Drop through. */
2431 case '+': what = CHANGE_MODIFY; break;
2434 switch (c)
2436 case OPTION_CHANGE_SECTION_ADDRESS:
2437 p->change_vma = what;
2438 p->vma_val = val;
2439 /* Drop through. */
2441 case OPTION_CHANGE_SECTION_LMA:
2442 p->change_lma = what;
2443 p->lma_val = val;
2444 break;
2446 case OPTION_CHANGE_SECTION_VMA:
2447 p->change_vma = what;
2448 p->vma_val = val;
2449 break;
2452 break;
2454 case OPTION_CHANGE_ADDRESSES:
2455 change_section_address = parse_vma (optarg, "--change-addresses");
2456 change_start = change_section_address;
2457 break;
2459 case OPTION_CHANGE_WARNINGS:
2460 change_warn = TRUE;
2461 break;
2463 case OPTION_CHANGE_LEADING_CHAR:
2464 change_leading_char = TRUE;
2465 break;
2467 case OPTION_DEBUGGING:
2468 convert_debugging = TRUE;
2469 break;
2471 case OPTION_GAP_FILL:
2473 bfd_vma gap_fill_vma;
2475 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2476 gap_fill = (bfd_byte) gap_fill_vma;
2477 if ((bfd_vma) gap_fill != gap_fill_vma)
2479 char buff[20];
2481 sprintf_vma (buff, gap_fill_vma);
2483 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2484 buff, gap_fill);
2486 gap_fill_set = TRUE;
2488 break;
2490 case OPTION_NO_CHANGE_WARNINGS:
2491 change_warn = FALSE;
2492 break;
2494 case OPTION_PAD_TO:
2495 pad_to = parse_vma (optarg, "--pad-to");
2496 pad_to_set = TRUE;
2497 break;
2499 case OPTION_REMOVE_LEADING_CHAR:
2500 remove_leading_char = TRUE;
2501 break;
2503 case OPTION_REDEFINE_SYM:
2505 /* Push this redefinition onto redefine_symbol_list. */
2507 int len;
2508 const char *s;
2509 const char *nextarg;
2510 char *source, *target;
2512 s = strchr (optarg, '=');
2513 if (s == NULL)
2514 fatal (_("bad format for %s"), "--redefine-sym");
2516 len = s - optarg;
2517 source = (char *) xmalloc (len + 1);
2518 strncpy (source, optarg, len);
2519 source[len] = '\0';
2521 nextarg = s + 1;
2522 len = strlen (nextarg);
2523 target = (char *) xmalloc (len + 1);
2524 strcpy (target, nextarg);
2526 redefine_list_append (source, target);
2528 free (source);
2529 free (target);
2531 break;
2533 case OPTION_SET_SECTION_FLAGS:
2535 const char *s;
2536 int len;
2537 char *name;
2539 s = strchr (optarg, '=');
2540 if (s == NULL)
2541 fatal (_("bad format for %s"), "--set-section-flags");
2543 len = s - optarg;
2544 name = (char *) xmalloc (len + 1);
2545 strncpy (name, optarg, len);
2546 name[len] = '\0';
2548 p = find_section_list (name, TRUE);
2550 p->set_flags = TRUE;
2551 p->flags = parse_flags (s + 1);
2553 break;
2555 case OPTION_RENAME_SECTION:
2557 flagword flags;
2558 const char *eq, *fl;
2559 char *old_name;
2560 char *new_name;
2561 unsigned int len;
2563 eq = strchr (optarg, '=');
2564 if (eq == NULL)
2565 fatal (_("bad format for %s"), "--rename-section");
2567 len = eq - optarg;
2568 if (len == 0)
2569 fatal (_("bad format for %s"), "--rename-section");
2571 old_name = (char *) xmalloc (len + 1);
2572 strncpy (old_name, optarg, len);
2573 old_name[len] = 0;
2575 eq++;
2576 fl = strchr (eq, ',');
2577 if (fl)
2579 flags = parse_flags (fl + 1);
2580 len = fl - eq;
2582 else
2584 flags = -1;
2585 len = strlen (eq);
2588 if (len == 0)
2589 fatal (_("bad format for %s"), "--rename-section");
2591 new_name = (char *) xmalloc (len + 1);
2592 strncpy (new_name, eq, len);
2593 new_name[len] = 0;
2595 add_section_rename (old_name, new_name, flags);
2597 break;
2599 case OPTION_SET_START:
2600 set_start = parse_vma (optarg, "--set-start");
2601 set_start_set = TRUE;
2602 break;
2604 case OPTION_SREC_LEN:
2605 Chunk = parse_vma (optarg, "--srec-len");
2606 break;
2608 case OPTION_SREC_FORCES3:
2609 S3Forced = TRUE;
2610 break;
2612 case OPTION_STRIP_SYMBOLS:
2613 add_specific_symbols (optarg, &strip_specific_list);
2614 break;
2616 case OPTION_KEEP_SYMBOLS:
2617 add_specific_symbols (optarg, &keep_specific_list);
2618 break;
2620 case OPTION_LOCALIZE_SYMBOLS:
2621 add_specific_symbols (optarg, &localize_specific_list);
2622 break;
2624 case OPTION_KEEPGLOBAL_SYMBOLS:
2625 add_specific_symbols (optarg, &keepglobal_specific_list);
2626 break;
2628 case OPTION_WEAKEN_SYMBOLS:
2629 add_specific_symbols (optarg, &weaken_specific_list);
2630 break;
2632 case OPTION_ALT_MACH_CODE:
2633 use_alt_mach_code = atoi (optarg);
2634 if (use_alt_mach_code <= 0)
2635 fatal (_("alternate machine code index must be positive"));
2636 break;
2638 case OPTION_PREFIX_SYMBOLS:
2639 prefix_symbols_string = optarg;
2640 break;
2642 case OPTION_PREFIX_SECTIONS:
2643 prefix_sections_string = optarg;
2644 break;
2646 case OPTION_PREFIX_ALLOC_SECTIONS:
2647 prefix_alloc_sections_string = optarg;
2648 break;
2650 case 0:
2651 break; /* we've been given a long option */
2653 case 'H':
2654 case 'h':
2655 copy_usage (stdout, 0);
2657 default:
2658 copy_usage (stderr, 1);
2662 if (show_version)
2663 print_version ("objcopy");
2665 if (copy_byte >= interleave)
2666 fatal (_("byte number must be less than interleave"));
2668 if (optind == argc || optind + 2 < argc)
2669 copy_usage (stderr, 1);
2671 input_filename = argv[optind];
2672 if (optind + 1 < argc)
2673 output_filename = argv[optind + 1];
2675 /* Default is to strip no symbols. */
2676 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2677 strip_symbols = STRIP_NONE;
2679 if (output_target == (char *) NULL)
2680 output_target = input_target;
2682 if (binary_architecture != (char *) NULL)
2684 if (input_target && strcmp (input_target, "binary") == 0)
2686 const bfd_arch_info_type * temp_arch_info;
2688 temp_arch_info = bfd_scan_arch (binary_architecture);
2690 if (temp_arch_info != NULL)
2691 bfd_external_binary_architecture = temp_arch_info->arch;
2692 else
2693 fatal (_("architecture %s unknown"), binary_architecture);
2695 else
2697 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2698 non_fatal (_(" Argument %s ignored"), binary_architecture);
2702 if (preserve_dates)
2703 if (stat (input_filename, & statbuf) < 0)
2704 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2706 /* If there is no destination file then create a temp and rename
2707 the result into the input. */
2709 if (output_filename == (char *) NULL)
2711 char *tmpname = make_tempname (input_filename);
2713 copy_file (input_filename, tmpname, input_target, output_target);
2714 if (status == 0)
2716 if (preserve_dates)
2717 set_times (tmpname, &statbuf);
2718 smart_rename (tmpname, input_filename, preserve_dates);
2720 else
2721 unlink (tmpname);
2723 else
2725 copy_file (input_filename, output_filename, input_target, output_target);
2727 if (status == 0 && preserve_dates)
2728 set_times (output_filename, &statbuf);
2731 if (change_warn)
2733 for (p = change_sections; p != NULL; p = p->next)
2735 if (! p->used)
2737 if (p->change_vma != CHANGE_IGNORE)
2739 char buff [20];
2741 sprintf_vma (buff, p->vma_val);
2743 /* xgettext:c-format */
2744 non_fatal (_("%s %s%c0x%s never used"),
2745 "--change-section-vma",
2746 p->name,
2747 p->change_vma == CHANGE_SET ? '=' : '+',
2748 buff);
2751 if (p->change_lma != CHANGE_IGNORE)
2753 char buff [20];
2755 sprintf_vma (buff, p->lma_val);
2757 /* xgettext:c-format */
2758 non_fatal (_("%s %s%c0x%s never used"),
2759 "--change-section-lma",
2760 p->name,
2761 p->change_lma == CHANGE_SET ? '=' : '+',
2762 buff);
2768 return 0;
2771 int main PARAMS ((int, char **));
2774 main (argc, argv)
2775 int argc;
2776 char *argv[];
2778 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2779 setlocale (LC_MESSAGES, "");
2780 #endif
2781 #if defined (HAVE_SETLOCALE)
2782 setlocale (LC_CTYPE, "");
2783 #endif
2784 bindtextdomain (PACKAGE, LOCALEDIR);
2785 textdomain (PACKAGE);
2787 program_name = argv[0];
2788 xmalloc_set_program_name (program_name);
2790 START_PROGRESS (program_name, 0);
2792 strip_symbols = STRIP_UNDEF;
2793 discard_locals = LOCALS_UNDEF;
2795 bfd_init ();
2796 set_default_bfd_target ();
2798 if (is_strip < 0)
2800 int i = strlen (program_name);
2801 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2802 /* Drop the .exe suffix, if any. */
2803 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
2805 i -= 4;
2806 program_name[i] = '\0';
2808 #endif
2809 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
2812 if (is_strip)
2813 strip_main (argc, argv);
2814 else
2815 copy_main (argc, argv);
2817 END_PROGRESS (program_name);
2819 return status;