2006-02-14 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / binutils / objcopy.c
bloba9fb877ec081d9201b3a9c48f14473ab68ba6a7f
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, 2004, 2005, 2006
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., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, 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 "fnmatch.h"
31 #include "elf-bfd.h"
32 #include <sys/stat.h>
33 #include "libbfd.h"
35 /* A list of symbols to explicitly strip out, or to keep. A linked
36 list is good enough for a small number from the command line, but
37 this will slow things down a lot if many symbols are being
38 deleted. */
40 struct symlist
42 const char *name;
43 struct symlist *next;
46 /* A list to support redefine_sym. */
47 struct redefine_node
49 char *source;
50 char *target;
51 struct redefine_node *next;
54 typedef struct section_rename
56 const char * old_name;
57 const char * new_name;
58 flagword flags;
59 struct section_rename * next;
61 section_rename;
63 /* List of sections to be renamed. */
64 static section_rename *section_rename_list;
66 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
68 static asymbol **isympp = NULL; /* Input symbols. */
69 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
71 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
72 static int copy_byte = -1;
73 static int interleave = 4;
75 static bfd_boolean verbose; /* Print file and target names. */
76 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
77 static int status = 0; /* Exit status. */
79 enum strip_action
81 STRIP_UNDEF,
82 STRIP_NONE, /* Don't strip. */
83 STRIP_DEBUG, /* Strip all debugger symbols. */
84 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
85 STRIP_NONDEBUG, /* Strip everything but debug info. */
86 STRIP_ALL /* Strip all symbols. */
89 /* Which symbols to remove. */
90 static enum strip_action strip_symbols;
92 enum locals_action
94 LOCALS_UNDEF,
95 LOCALS_START_L, /* Discard locals starting with L. */
96 LOCALS_ALL /* Discard all locals. */
99 /* Which local symbols to remove. Overrides STRIP_ALL. */
100 static enum locals_action discard_locals;
102 /* What kind of change to perform. */
103 enum change_action
105 CHANGE_IGNORE,
106 CHANGE_MODIFY,
107 CHANGE_SET
110 /* Structure used to hold lists of sections and actions to take. */
111 struct section_list
113 struct section_list * next; /* Next section to change. */
114 const char * name; /* Section name. */
115 bfd_boolean used; /* Whether this entry was used. */
116 bfd_boolean remove; /* Whether to remove this section. */
117 bfd_boolean copy; /* Whether to copy this section. */
118 enum change_action change_vma;/* Whether to change or set VMA. */
119 bfd_vma vma_val; /* Amount to change by or set to. */
120 enum change_action change_lma;/* Whether to change or set LMA. */
121 bfd_vma lma_val; /* Amount to change by or set to. */
122 bfd_boolean set_flags; /* Whether to set the section flags. */
123 flagword flags; /* What to set the section flags to. */
126 static struct section_list *change_sections;
128 /* TRUE if some sections are to be removed. */
129 static bfd_boolean sections_removed;
131 /* TRUE if only some sections are to be copied. */
132 static bfd_boolean sections_copied;
134 /* Changes to the start address. */
135 static bfd_vma change_start = 0;
136 static bfd_boolean set_start_set = FALSE;
137 static bfd_vma set_start;
139 /* Changes to section addresses. */
140 static bfd_vma change_section_address = 0;
142 /* Filling gaps between sections. */
143 static bfd_boolean gap_fill_set = FALSE;
144 static bfd_byte gap_fill = 0;
146 /* Pad to a given address. */
147 static bfd_boolean pad_to_set = FALSE;
148 static bfd_vma pad_to;
150 /* Use alternate machine code? */
151 static int use_alt_mach_code = 0;
153 /* Output BFD flags user wants to set or clear */
154 static flagword bfd_flags_to_set;
155 static flagword bfd_flags_to_clear;
157 /* List of sections to add. */
158 struct section_add
160 /* Next section to add. */
161 struct section_add *next;
162 /* Name of section to add. */
163 const char *name;
164 /* Name of file holding section contents. */
165 const char *filename;
166 /* Size of file. */
167 size_t size;
168 /* Contents of file. */
169 bfd_byte *contents;
170 /* BFD section, after it has been added. */
171 asection *section;
174 /* List of sections to add to the output BFD. */
175 static struct section_add *add_sections;
177 /* If non-NULL the argument to --add-gnu-debuglink.
178 This should be the filename to store in the .gnu_debuglink section. */
179 static const char * gnu_debuglink_filename = NULL;
181 /* Whether to convert debugging information. */
182 static bfd_boolean convert_debugging = FALSE;
184 /* Whether to change the leading character in symbol names. */
185 static bfd_boolean change_leading_char = FALSE;
187 /* Whether to remove the leading character from global symbol names. */
188 static bfd_boolean remove_leading_char = FALSE;
190 /* Whether to permit wildcard in symbol comparison. */
191 static bfd_boolean wildcard = FALSE;
193 /* List of symbols to strip, keep, localize, keep-global, weaken,
194 or redefine. */
195 static struct symlist *strip_specific_list = NULL;
196 static struct symlist *strip_unneeded_list = NULL;
197 static struct symlist *keep_specific_list = NULL;
198 static struct symlist *localize_specific_list = NULL;
199 static struct symlist *globalize_specific_list = NULL;
200 static struct symlist *keepglobal_specific_list = NULL;
201 static struct symlist *weaken_specific_list = NULL;
202 static struct redefine_node *redefine_sym_list = NULL;
204 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
205 static bfd_boolean weaken = FALSE;
207 /* If this is TRUE, we retain BSF_FILE symbols. */
208 static bfd_boolean keep_file_symbols = FALSE;
210 /* Prefix symbols/sections. */
211 static char *prefix_symbols_string = 0;
212 static char *prefix_sections_string = 0;
213 static char *prefix_alloc_sections_string = 0;
215 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
216 enum command_line_switch
218 OPTION_ADD_SECTION=150,
219 OPTION_CHANGE_ADDRESSES,
220 OPTION_CHANGE_LEADING_CHAR,
221 OPTION_CHANGE_START,
222 OPTION_CHANGE_SECTION_ADDRESS,
223 OPTION_CHANGE_SECTION_LMA,
224 OPTION_CHANGE_SECTION_VMA,
225 OPTION_CHANGE_WARNINGS,
226 OPTION_DEBUGGING,
227 OPTION_GAP_FILL,
228 OPTION_NO_CHANGE_WARNINGS,
229 OPTION_PAD_TO,
230 OPTION_REMOVE_LEADING_CHAR,
231 OPTION_SET_SECTION_FLAGS,
232 OPTION_SET_START,
233 OPTION_STRIP_UNNEEDED,
234 OPTION_WEAKEN,
235 OPTION_REDEFINE_SYM,
236 OPTION_REDEFINE_SYMS,
237 OPTION_SREC_LEN,
238 OPTION_SREC_FORCES3,
239 OPTION_STRIP_SYMBOLS,
240 OPTION_STRIP_UNNEEDED_SYMBOL,
241 OPTION_STRIP_UNNEEDED_SYMBOLS,
242 OPTION_KEEP_SYMBOLS,
243 OPTION_LOCALIZE_SYMBOLS,
244 OPTION_GLOBALIZE_SYMBOL,
245 OPTION_GLOBALIZE_SYMBOLS,
246 OPTION_KEEPGLOBAL_SYMBOLS,
247 OPTION_WEAKEN_SYMBOLS,
248 OPTION_RENAME_SECTION,
249 OPTION_ALT_MACH_CODE,
250 OPTION_PREFIX_SYMBOLS,
251 OPTION_PREFIX_SECTIONS,
252 OPTION_PREFIX_ALLOC_SECTIONS,
253 OPTION_FORMATS_INFO,
254 OPTION_ADD_GNU_DEBUGLINK,
255 OPTION_ONLY_KEEP_DEBUG,
256 OPTION_KEEP_FILE_SYMBOLS,
257 OPTION_READONLY_TEXT,
258 OPTION_WRITABLE_TEXT,
259 OPTION_PURE,
260 OPTION_IMPURE
263 /* Options to handle if running as "strip". */
265 static struct option strip_options[] =
267 {"discard-all", no_argument, 0, 'x'},
268 {"discard-locals", no_argument, 0, 'X'},
269 {"format", required_argument, 0, 'F'}, /* Obsolete */
270 {"help", no_argument, 0, 'h'},
271 {"info", no_argument, 0, OPTION_FORMATS_INFO},
272 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
273 {"input-target", required_argument, 0, 'I'},
274 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
275 {"keep-symbol", required_argument, 0, 'K'},
276 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
277 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
278 {"output-target", required_argument, 0, 'O'},
279 {"output-file", required_argument, 0, 'o'},
280 {"preserve-dates", no_argument, 0, 'p'},
281 {"remove-section", required_argument, 0, 'R'},
282 {"strip-all", no_argument, 0, 's'},
283 {"strip-debug", no_argument, 0, 'S'},
284 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
285 {"strip-symbol", required_argument, 0, 'N'},
286 {"target", required_argument, 0, 'F'},
287 {"verbose", no_argument, 0, 'v'},
288 {"version", no_argument, 0, 'V'},
289 {"wildcard", no_argument, 0, 'w'},
290 {0, no_argument, 0, 0}
293 /* Options to handle if running as "objcopy". */
295 static struct option copy_options[] =
297 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
298 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
299 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
300 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
301 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
302 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
303 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
304 {"binary-architecture", required_argument, 0, 'B'},
305 {"byte", required_argument, 0, 'b'},
306 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
307 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
308 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
309 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
310 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
311 {"change-start", required_argument, 0, OPTION_CHANGE_START},
312 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
313 {"debugging", no_argument, 0, OPTION_DEBUGGING},
314 {"discard-all", no_argument, 0, 'x'},
315 {"discard-locals", no_argument, 0, 'X'},
316 {"format", required_argument, 0, 'F'}, /* Obsolete */
317 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
318 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
319 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
320 {"help", no_argument, 0, 'h'},
321 {"impure", no_argument, 0, OPTION_IMPURE},
322 {"info", no_argument, 0, OPTION_FORMATS_INFO},
323 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
324 {"input-target", required_argument, 0, 'I'},
325 {"interleave", required_argument, 0, 'i'},
326 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
327 {"keep-global-symbol", required_argument, 0, 'G'},
328 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
329 {"keep-symbol", required_argument, 0, 'K'},
330 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
331 {"localize-symbol", required_argument, 0, 'L'},
332 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
333 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
334 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
335 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
336 {"only-section", required_argument, 0, 'j'},
337 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
338 {"output-target", required_argument, 0, 'O'},
339 {"pad-to", required_argument, 0, OPTION_PAD_TO},
340 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
341 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
342 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
343 {"preserve-dates", no_argument, 0, 'p'},
344 {"pure", no_argument, 0, OPTION_PURE},
345 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
346 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
347 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
348 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
349 {"remove-section", required_argument, 0, 'R'},
350 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
351 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
352 {"set-start", required_argument, 0, OPTION_SET_START},
353 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
354 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
355 {"strip-all", no_argument, 0, 'S'},
356 {"strip-debug", no_argument, 0, 'g'},
357 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
358 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
359 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
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 {"wildcard", no_argument, 0, 'w'},
369 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
370 {0, no_argument, 0, 0}
373 /* IMPORTS */
374 extern char *program_name;
376 /* This flag distinguishes between strip and objcopy:
377 1 means this is 'strip'; 0 means this is 'objcopy'.
378 -1 means if we should use argv[0] to decide. */
379 extern int is_strip;
381 /* The maximum length of an S record. This variable is declared in srec.c
382 and can be modified by the --srec-len parameter. */
383 extern unsigned int Chunk;
385 /* Restrict the generation of Srecords to type S3 only.
386 This variable is declare in bfd/srec.c and can be toggled
387 on by the --srec-forceS3 command line switch. */
388 extern bfd_boolean S3Forced;
390 /* Defined in bfd/binary.c. Used to set architecture and machine of input
391 binary files. */
392 extern enum bfd_architecture bfd_external_binary_architecture;
393 extern unsigned long bfd_external_machine;
395 /* Forward declarations. */
396 static void setup_section (bfd *, asection *, void *);
397 static void setup_bfd_headers (bfd *, bfd *);
398 static void copy_section (bfd *, asection *, void *);
399 static void get_sections (bfd *, asection *, void *);
400 static int compare_section_lma (const void *, const void *);
401 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
402 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
403 static const char *lookup_sym_redefinition (const char *);
405 static void
406 copy_usage (FILE *stream, int exit_status)
408 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
409 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
410 fprintf (stream, _(" The options are:\n"));
411 fprintf (stream, _("\
412 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
413 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
414 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
415 -F --target <bfdname> Set both input and output format to <bfdname>\n\
416 --debugging Convert debugging information, if possible\n\
417 -p --preserve-dates Copy modified/access timestamps to the output\n\
418 -j --only-section <name> Only copy section <name> into the output\n\
419 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
420 -R --remove-section <name> Remove section <name> from the output\n\
421 -S --strip-all Remove all symbol and relocation information\n\
422 -g --strip-debug Remove all debugging symbols & sections\n\
423 --strip-unneeded Remove all symbols not needed by relocations\n\
424 -N --strip-symbol <name> Do not copy symbol <name>\n\
425 --strip-unneeded-symbol <name>\n\
426 Do not copy symbol <name> unless needed by\n\
427 relocations\n\
428 --only-keep-debug Strip everything but the debug information\n\
429 -K --keep-symbol <name> Do not strip symbol <name>\n\
430 --keep-file-symbols Do not strip file symbol(s)\n\
431 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
432 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
433 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
434 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
435 --weaken Force all global symbols to be marked as weak\n\
436 -w --wildcard Permit wildcard in symbol comparison\n\
437 -x --discard-all Remove all non-global symbols\n\
438 -X --discard-locals Remove any compiler-generated symbols\n\
439 -i --interleave <number> Only copy one out of every <number> bytes\n\
440 -b --byte <num> Select byte <num> in every interleaved block\n\
441 --gap-fill <val> Fill gaps between sections with <val>\n\
442 --pad-to <addr> Pad the last section up to address <addr>\n\
443 --set-start <addr> Set the start address to <addr>\n\
444 {--change-start|--adjust-start} <incr>\n\
445 Add <incr> to the start address\n\
446 {--change-addresses|--adjust-vma} <incr>\n\
447 Add <incr> to LMA, VMA and start addresses\n\
448 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
449 Change LMA and VMA of section <name> by <val>\n\
450 --change-section-lma <name>{=|+|-}<val>\n\
451 Change the LMA of section <name> by <val>\n\
452 --change-section-vma <name>{=|+|-}<val>\n\
453 Change the VMA of section <name> by <val>\n\
454 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
455 Warn if a named section does not exist\n\
456 --set-section-flags <name>=<flags>\n\
457 Set section <name>'s properties to <flags>\n\
458 --add-section <name>=<file> Add section <name> found in <file> to output\n\
459 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
460 --change-leading-char Force output format's leading character style\n\
461 --remove-leading-char Remove leading character from global symbols\n\
462 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
463 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
464 listed in <file>\n\
465 --srec-len <number> Restrict the length of generated Srecords\n\
466 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
467 --strip-symbols <file> -N for all symbols listed in <file>\n\
468 --strip-unneeded-symbols <file>\n\
469 --strip-unneeded-symbol for all symbols listed\n\
470 in <file>\n\
471 --keep-symbols <file> -K for all symbols listed in <file>\n\
472 --localize-symbols <file> -L for all symbols listed in <file>\n\
473 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
474 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
475 --weaken-symbols <file> -W for all symbols listed in <file>\n\
476 --alt-machine-code <index> Use alternate machine code for output\n\
477 --writable-text Mark the output text as writable\n\
478 --readonly-text Make the output text write protected\n\
479 --pure Mark the output file as demand paged\n\
480 --impure Mark the output file as impure\n\
481 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
482 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
483 --prefix-alloc-sections <prefix>\n\
484 Add <prefix> to start of every allocatable\n\
485 section name\n\
486 -v --verbose List all object files modified\n\
487 @<file> Read options from <file>\n\
488 -V --version Display this program's version number\n\
489 -h --help Display this output\n\
490 --info List object formats & architectures supported\n\
491 "));
492 list_supported_targets (program_name, stream);
493 if (exit_status == 0)
494 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
495 exit (exit_status);
498 static void
499 strip_usage (FILE *stream, int exit_status)
501 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
502 fprintf (stream, _(" Removes symbols and sections from files\n"));
503 fprintf (stream, _(" The options are:\n"));
504 fprintf (stream, _("\
505 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
506 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
507 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
508 -p --preserve-dates Copy modified/access timestamps to the output\n\
509 -R --remove-section=<name> Remove section <name> from the output\n\
510 -s --strip-all Remove all symbol and relocation information\n\
511 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
512 --strip-unneeded Remove all symbols not needed by relocations\n\
513 --only-keep-debug Strip everything but the debug information\n\
514 -N --strip-symbol=<name> Do not copy symbol <name>\n\
515 -K --keep-symbol=<name> Do not strip symbol <name>\n\
516 --keep-file-symbols Do not strip file symbol(s)\n\
517 -w --wildcard Permit wildcard in symbol comparison\n\
518 -x --discard-all Remove all non-global symbols\n\
519 -X --discard-locals Remove any compiler-generated symbols\n\
520 -v --verbose List all object files modified\n\
521 -V --version Display this program's version number\n\
522 -h --help Display this output\n\
523 --info List object formats & architectures supported\n\
524 -o <file> Place stripped output into <file>\n\
525 "));
527 list_supported_targets (program_name, stream);
528 if (exit_status == 0)
529 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
530 exit (exit_status);
533 /* Parse section flags into a flagword, with a fatal error if the
534 string can't be parsed. */
536 static flagword
537 parse_flags (const char *s)
539 flagword ret;
540 const char *snext;
541 int len;
543 ret = SEC_NO_FLAGS;
547 snext = strchr (s, ',');
548 if (snext == NULL)
549 len = strlen (s);
550 else
552 len = snext - s;
553 ++snext;
556 if (0) ;
557 #define PARSE_FLAG(fname,fval) \
558 else if (strncasecmp (fname, s, len) == 0) ret |= fval
559 PARSE_FLAG ("alloc", SEC_ALLOC);
560 PARSE_FLAG ("load", SEC_LOAD);
561 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
562 PARSE_FLAG ("readonly", SEC_READONLY);
563 PARSE_FLAG ("debug", SEC_DEBUGGING);
564 PARSE_FLAG ("code", SEC_CODE);
565 PARSE_FLAG ("data", SEC_DATA);
566 PARSE_FLAG ("rom", SEC_ROM);
567 PARSE_FLAG ("share", SEC_COFF_SHARED);
568 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
569 #undef PARSE_FLAG
570 else
572 char *copy;
574 copy = xmalloc (len + 1);
575 strncpy (copy, s, len);
576 copy[len] = '\0';
577 non_fatal (_("unrecognized section flag `%s'"), copy);
578 fatal (_("supported flags: %s"),
579 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
582 s = snext;
584 while (s != NULL);
586 return ret;
589 /* Find and optionally add an entry in the change_sections list. */
591 static struct section_list *
592 find_section_list (const char *name, bfd_boolean add)
594 struct section_list *p;
596 for (p = change_sections; p != NULL; p = p->next)
597 if (strcmp (p->name, name) == 0)
598 return p;
600 if (! add)
601 return NULL;
603 p = xmalloc (sizeof (struct section_list));
604 p->name = name;
605 p->used = FALSE;
606 p->remove = FALSE;
607 p->copy = FALSE;
608 p->change_vma = CHANGE_IGNORE;
609 p->change_lma = CHANGE_IGNORE;
610 p->vma_val = 0;
611 p->lma_val = 0;
612 p->set_flags = FALSE;
613 p->flags = 0;
615 p->next = change_sections;
616 change_sections = p;
618 return p;
621 /* Add a symbol to strip_specific_list. */
623 static void
624 add_specific_symbol (const char *name, struct symlist **list)
626 struct symlist *tmp_list;
628 tmp_list = xmalloc (sizeof (struct symlist));
629 tmp_list->name = name;
630 tmp_list->next = *list;
631 *list = tmp_list;
634 /* Add symbols listed in `filename' to strip_specific_list. */
636 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
637 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
639 static void
640 add_specific_symbols (const char *filename, struct symlist **list)
642 off_t size;
643 FILE * f;
644 char * line;
645 char * buffer;
646 unsigned int line_count;
648 size = get_file_size (filename);
649 if (size == 0)
650 return;
652 buffer = xmalloc (size + 2);
653 f = fopen (filename, FOPEN_RT);
654 if (f == NULL)
655 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
657 if (fread (buffer, 1, size, f) == 0 || ferror (f))
658 fatal (_("%s: fread failed"), filename);
660 fclose (f);
661 buffer [size] = '\n';
662 buffer [size + 1] = '\0';
664 line_count = 1;
666 for (line = buffer; * line != '\0'; line ++)
668 char * eol;
669 char * name;
670 char * name_end;
671 int finished = FALSE;
673 for (eol = line;; eol ++)
675 switch (* eol)
677 case '\n':
678 * eol = '\0';
679 /* Cope with \n\r. */
680 if (eol[1] == '\r')
681 ++ eol;
682 finished = TRUE;
683 break;
685 case '\r':
686 * eol = '\0';
687 /* Cope with \r\n. */
688 if (eol[1] == '\n')
689 ++ eol;
690 finished = TRUE;
691 break;
693 case 0:
694 finished = TRUE;
695 break;
697 case '#':
698 /* Line comment, Terminate the line here, in case a
699 name is present and then allow the rest of the
700 loop to find the real end of the line. */
701 * eol = '\0';
702 break;
704 default:
705 break;
708 if (finished)
709 break;
712 /* A name may now exist somewhere between 'line' and 'eol'.
713 Strip off leading whitespace and trailing whitespace,
714 then add it to the list. */
715 for (name = line; IS_WHITESPACE (* name); name ++)
717 for (name_end = name;
718 (! IS_WHITESPACE (* name_end))
719 && (! IS_LINE_TERMINATOR (* name_end));
720 name_end ++)
723 if (! IS_LINE_TERMINATOR (* name_end))
725 char * extra;
727 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
730 if (! IS_LINE_TERMINATOR (* extra))
731 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
732 filename, line_count);
735 * name_end = '\0';
737 if (name_end > name)
738 add_specific_symbol (name, list);
740 /* Advance line pointer to end of line. The 'eol ++' in the for
741 loop above will then advance us to the start of the next line. */
742 line = eol;
743 line_count ++;
747 /* See whether a symbol should be stripped or kept based on
748 strip_specific_list and keep_symbols. */
750 static bfd_boolean
751 is_specified_symbol (const char *name, struct symlist *list)
753 struct symlist *tmp_list;
755 if (wildcard)
757 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
758 if (*(tmp_list->name) != '!')
760 if (!fnmatch (tmp_list->name, name, 0))
761 return TRUE;
763 else
765 if (fnmatch (tmp_list->name + 1, name, 0))
766 return TRUE;
769 else
771 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
772 if (strcmp (name, tmp_list->name) == 0)
773 return TRUE;
776 return FALSE;
779 /* See if a section is being removed. */
781 static bfd_boolean
782 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
784 if (sections_removed || sections_copied)
786 struct section_list *p;
788 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
790 if (sections_removed && p != NULL && p->remove)
791 return TRUE;
792 if (sections_copied && (p == NULL || ! p->copy))
793 return TRUE;
796 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
798 if (strip_symbols == STRIP_DEBUG
799 || strip_symbols == STRIP_UNNEEDED
800 || strip_symbols == STRIP_ALL
801 || discard_locals == LOCALS_ALL
802 || convert_debugging)
803 return TRUE;
805 if (strip_symbols == STRIP_NONDEBUG)
806 return FALSE;
809 return FALSE;
812 /* Choose which symbol entries to copy; put the result in OSYMS.
813 We don't copy in place, because that confuses the relocs.
814 Return the number of symbols to print. */
816 static unsigned int
817 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
818 asymbol **isyms, long symcount)
820 asymbol **from = isyms, **to = osyms;
821 long src_count = 0, dst_count = 0;
822 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
823 == HAS_RELOC;
825 for (; src_count < symcount; src_count++)
827 asymbol *sym = from[src_count];
828 flagword flags = sym->flags;
829 char *name = (char *) bfd_asymbol_name (sym);
830 int keep;
831 bfd_boolean undefined;
832 bfd_boolean rem_leading_char;
833 bfd_boolean add_leading_char;
835 undefined = bfd_is_und_section (bfd_get_section (sym));
837 if (redefine_sym_list)
839 char *old_name, *new_name;
841 old_name = (char *) bfd_asymbol_name (sym);
842 new_name = (char *) lookup_sym_redefinition (old_name);
843 bfd_asymbol_name (sym) = new_name;
844 name = new_name;
847 /* Check if we will remove the current leading character. */
848 rem_leading_char =
849 (name[0] == bfd_get_symbol_leading_char (abfd))
850 && (change_leading_char
851 || (remove_leading_char
852 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
853 || undefined
854 || bfd_is_com_section (bfd_get_section (sym)))));
856 /* Check if we will add a new leading character. */
857 add_leading_char =
858 change_leading_char
859 && (bfd_get_symbol_leading_char (obfd) != '\0')
860 && (bfd_get_symbol_leading_char (abfd) == '\0'
861 || (name[0] == bfd_get_symbol_leading_char (abfd)));
863 /* Short circuit for change_leading_char if we can do it in-place. */
864 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
866 name[0] = bfd_get_symbol_leading_char (obfd);
867 bfd_asymbol_name (sym) = name;
868 rem_leading_char = FALSE;
869 add_leading_char = FALSE;
872 /* Remove leading char. */
873 if (rem_leading_char)
874 bfd_asymbol_name (sym) = ++name;
876 /* Add new leading char and/or prefix. */
877 if (add_leading_char || prefix_symbols_string)
879 char *n, *ptr;
881 ptr = n = xmalloc (1 + strlen (prefix_symbols_string)
882 + strlen (name) + 1);
883 if (add_leading_char)
884 *ptr++ = bfd_get_symbol_leading_char (obfd);
886 if (prefix_symbols_string)
888 strcpy (ptr, prefix_symbols_string);
889 ptr += strlen (prefix_symbols_string);
892 strcpy (ptr, name);
893 bfd_asymbol_name (sym) = n;
894 name = n;
897 if (strip_symbols == STRIP_ALL)
898 keep = 0;
899 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
900 || ((flags & BSF_SECTION_SYM) != 0
901 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
902 & BSF_KEEP) != 0))
903 keep = 1;
904 else if (relocatable /* Relocatable file. */
905 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
906 keep = 1;
907 else if (bfd_decode_symclass (sym) == 'I')
908 /* Global symbols in $idata sections need to be retained
909 even if relocatable is FALSE. External users of the
910 library containing the $idata section may reference these
911 symbols. */
912 keep = 1;
913 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
914 || (flags & BSF_WEAK) != 0
915 || undefined
916 || bfd_is_com_section (bfd_get_section (sym)))
917 keep = strip_symbols != STRIP_UNNEEDED;
918 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
919 keep = (strip_symbols != STRIP_DEBUG
920 && strip_symbols != STRIP_UNNEEDED
921 && ! convert_debugging);
922 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
923 /* COMDAT sections store special information in local
924 symbols, so we cannot risk stripping any of them. */
925 keep = 1;
926 else /* Local symbol. */
927 keep = (strip_symbols != STRIP_UNNEEDED
928 && (discard_locals != LOCALS_ALL
929 && (discard_locals != LOCALS_START_L
930 || ! bfd_is_local_label (abfd, sym))));
932 if (keep && is_specified_symbol (name, strip_specific_list))
933 keep = 0;
934 if (keep
935 && !(flags & BSF_KEEP)
936 && is_specified_symbol (name, strip_unneeded_list))
937 keep = 0;
938 if (!keep
939 && ((keep_file_symbols && (flags & BSF_FILE))
940 || is_specified_symbol (name, keep_specific_list)))
941 keep = 1;
942 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
943 keep = 0;
945 if (keep)
947 if ((flags & BSF_GLOBAL) != 0
948 && (weaken || is_specified_symbol (name, weaken_specific_list)))
950 sym->flags &= ~ BSF_GLOBAL;
951 sym->flags |= BSF_WEAK;
954 if (!undefined
955 && (flags & (BSF_GLOBAL | BSF_WEAK))
956 && (is_specified_symbol (name, localize_specific_list)
957 || (keepglobal_specific_list != NULL
958 && ! is_specified_symbol (name, keepglobal_specific_list))))
960 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
961 sym->flags |= BSF_LOCAL;
964 if (!undefined
965 && (flags & BSF_LOCAL)
966 && is_specified_symbol (name, globalize_specific_list))
968 sym->flags &= ~ BSF_LOCAL;
969 sym->flags |= BSF_GLOBAL;
972 to[dst_count++] = sym;
976 to[dst_count] = NULL;
978 return dst_count;
981 /* Find the redefined name of symbol SOURCE. */
983 static const char *
984 lookup_sym_redefinition (const char *source)
986 struct redefine_node *list;
988 for (list = redefine_sym_list; list != NULL; list = list->next)
989 if (strcmp (source, list->source) == 0)
990 return list->target;
992 return source;
995 /* Add a node to a symbol redefine list. */
997 static void
998 redefine_list_append (const char *cause, const char *source, const char *target)
1000 struct redefine_node **p;
1001 struct redefine_node *list;
1002 struct redefine_node *new_node;
1004 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1006 if (strcmp (source, list->source) == 0)
1007 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1008 cause, source);
1010 if (strcmp (target, list->target) == 0)
1011 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1012 cause, target);
1015 new_node = xmalloc (sizeof (struct redefine_node));
1017 new_node->source = strdup (source);
1018 new_node->target = strdup (target);
1019 new_node->next = NULL;
1021 *p = new_node;
1024 /* Handle the --redefine-syms option. Read lines containing "old new"
1025 from the file, and add them to the symbol redefine list. */
1027 static void
1028 add_redefine_syms_file (const char *filename)
1030 FILE *file;
1031 char *buf;
1032 size_t bufsize;
1033 size_t len;
1034 size_t outsym_off;
1035 int c, lineno;
1037 file = fopen (filename, "r");
1038 if (file == NULL)
1039 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1040 filename, strerror (errno));
1042 bufsize = 100;
1043 buf = xmalloc (bufsize);
1045 lineno = 1;
1046 c = getc (file);
1047 len = 0;
1048 outsym_off = 0;
1049 while (c != EOF)
1051 /* Collect the input symbol name. */
1052 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1054 if (c == '#')
1055 goto comment;
1056 buf[len++] = c;
1057 if (len >= bufsize)
1059 bufsize *= 2;
1060 buf = xrealloc (buf, bufsize);
1062 c = getc (file);
1064 buf[len++] = '\0';
1065 if (c == EOF)
1066 break;
1068 /* Eat white space between the symbol names. */
1069 while (IS_WHITESPACE (c))
1070 c = getc (file);
1071 if (c == '#' || IS_LINE_TERMINATOR (c))
1072 goto comment;
1073 if (c == EOF)
1074 break;
1076 /* Collect the output symbol name. */
1077 outsym_off = len;
1078 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1080 if (c == '#')
1081 goto comment;
1082 buf[len++] = c;
1083 if (len >= bufsize)
1085 bufsize *= 2;
1086 buf = xrealloc (buf, bufsize);
1088 c = getc (file);
1090 buf[len++] = '\0';
1091 if (c == EOF)
1092 break;
1094 /* Eat white space at end of line. */
1095 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1096 c = getc (file);
1097 if (c == '#')
1098 goto comment;
1099 /* Handle \r\n. */
1100 if ((c == '\r' && (c = getc (file)) == '\n')
1101 || c == '\n' || c == EOF)
1103 end_of_line:
1104 /* Append the redefinition to the list. */
1105 if (buf[0] != '\0')
1106 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1108 lineno++;
1109 len = 0;
1110 outsym_off = 0;
1111 if (c == EOF)
1112 break;
1113 c = getc (file);
1114 continue;
1116 else
1117 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1118 comment:
1119 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1120 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1121 buf[len++] = '\0';
1123 /* Eat the rest of the line and finish it. */
1124 while (c != '\n' && c != EOF)
1125 c = getc (file);
1126 goto end_of_line;
1129 if (len != 0)
1130 fatal (_("%s:%d: premature end of file"), filename, lineno);
1132 free (buf);
1135 /* Copy unkown object file IBFD onto OBFD.
1136 Returns TRUE upon success, FALSE otherwise. */
1138 static bfd_boolean
1139 copy_unknown_object (bfd *ibfd, bfd *obfd)
1141 char *cbuf;
1142 int tocopy;
1143 long ncopied;
1144 long size;
1145 struct stat buf;
1147 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1149 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1150 return FALSE;
1153 size = buf.st_size;
1154 if (size < 0)
1156 non_fatal (_("stat returns negative size for `%s'"),
1157 bfd_get_archive_filename (ibfd));
1158 return FALSE;
1161 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1163 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1164 return FALSE;
1167 if (verbose)
1168 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1169 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1171 cbuf = xmalloc (BUFSIZE);
1172 ncopied = 0;
1173 while (ncopied < size)
1175 tocopy = size - ncopied;
1176 if (tocopy > BUFSIZE)
1177 tocopy = BUFSIZE;
1179 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1180 != (bfd_size_type) tocopy)
1182 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1183 free (cbuf);
1184 return FALSE;
1187 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1188 != (bfd_size_type) tocopy)
1190 bfd_nonfatal (bfd_get_filename (obfd));
1191 free (cbuf);
1192 return FALSE;
1195 ncopied += tocopy;
1198 chmod (bfd_get_filename (obfd), buf.st_mode);
1199 free (cbuf);
1200 return TRUE;
1203 /* Copy object file IBFD onto OBFD.
1204 Returns TRUE upon success, FALSE otherwise. */
1206 static bfd_boolean
1207 copy_object (bfd *ibfd, bfd *obfd)
1209 bfd_vma start;
1210 long symcount;
1211 asection **osections = NULL;
1212 asection *gnu_debuglink_section = NULL;
1213 bfd_size_type *gaps = NULL;
1214 bfd_size_type max_gap = 0;
1215 long symsize;
1216 void *dhandle;
1217 enum bfd_architecture iarch;
1218 unsigned int imach;
1220 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1221 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1222 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1223 fatal (_("Unable to change endianness of input file(s)"));
1225 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1227 bfd_nonfatal (bfd_get_filename (obfd));
1228 return FALSE;
1231 if (verbose)
1232 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1233 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1234 bfd_get_filename (obfd), bfd_get_target (obfd));
1236 if (set_start_set)
1237 start = set_start;
1238 else
1239 start = bfd_get_start_address (ibfd);
1240 start += change_start;
1242 /* Neither the start address nor the flags
1243 need to be set for a core file. */
1244 if (bfd_get_format (obfd) != bfd_core)
1246 flagword flags;
1248 flags = bfd_get_file_flags (ibfd);
1249 flags |= bfd_flags_to_set;
1250 flags &= ~bfd_flags_to_clear;
1251 flags &= bfd_applicable_file_flags (obfd);
1253 if (!bfd_set_start_address (obfd, start)
1254 || !bfd_set_file_flags (obfd, flags))
1256 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1257 return FALSE;
1261 /* Copy architecture of input file to output file. */
1262 iarch = bfd_get_arch (ibfd);
1263 imach = bfd_get_mach (ibfd);
1264 if (!bfd_set_arch_mach (obfd, iarch, imach)
1265 && (ibfd->target_defaulted
1266 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1268 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1269 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1270 bfd_get_archive_filename (ibfd));
1271 else
1272 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1273 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1274 bfd_get_mach (ibfd)));
1275 return FALSE;
1278 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1280 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1281 return FALSE;
1284 if (isympp)
1285 free (isympp);
1287 if (osympp != isympp)
1288 free (osympp);
1290 isympp = NULL;
1291 osympp = NULL;
1293 /* BFD mandates that all output sections be created and sizes set before
1294 any output is done. Thus, we traverse all sections multiple times. */
1295 bfd_map_over_sections (ibfd, setup_section, obfd);
1297 setup_bfd_headers (ibfd, obfd);
1299 if (add_sections != NULL)
1301 struct section_add *padd;
1302 struct section_list *pset;
1304 for (padd = add_sections; padd != NULL; padd = padd->next)
1306 flagword flags;
1308 pset = find_section_list (padd->name, FALSE);
1309 if (pset != NULL)
1310 pset->used = TRUE;
1312 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1313 if (pset != NULL && pset->set_flags)
1314 flags = pset->flags | SEC_HAS_CONTENTS;
1316 /* bfd_make_section_with_flags() does not return very helpful
1317 error codes, so check for the most likely user error first. */
1318 if (bfd_get_section_by_name (obfd, padd->name))
1320 non_fatal (_("can't add section '%s' - it already exists!"), padd->name);
1321 return FALSE;
1323 else
1325 padd->section = bfd_make_section_with_flags (obfd, padd->name, flags);
1326 if (padd->section == NULL)
1328 non_fatal (_("can't create section `%s': %s"),
1329 padd->name, bfd_errmsg (bfd_get_error ()));
1330 return FALSE;
1334 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1336 bfd_nonfatal (bfd_get_filename (obfd));
1337 return FALSE;
1340 if (pset != NULL)
1342 if (pset->change_vma != CHANGE_IGNORE)
1343 if (! bfd_set_section_vma (obfd, padd->section,
1344 pset->vma_val))
1346 bfd_nonfatal (bfd_get_filename (obfd));
1347 return FALSE;
1350 if (pset->change_lma != CHANGE_IGNORE)
1352 padd->section->lma = pset->lma_val;
1354 if (! bfd_set_section_alignment
1355 (obfd, padd->section,
1356 bfd_section_alignment (obfd, padd->section)))
1358 bfd_nonfatal (bfd_get_filename (obfd));
1359 return FALSE;
1366 if (gnu_debuglink_filename != NULL)
1368 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1369 (obfd, gnu_debuglink_filename);
1371 if (gnu_debuglink_section == NULL)
1373 bfd_nonfatal (gnu_debuglink_filename);
1374 return FALSE;
1377 /* Special processing for PE format files. We
1378 have no way to distinguish PE from COFF here. */
1379 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1381 bfd_vma debuglink_vma;
1382 asection * highest_section;
1383 asection * sec;
1385 /* The PE spec requires that all sections be adjacent and sorted
1386 in ascending order of VMA. It also specifies that debug
1387 sections should be last. This is despite the fact that debug
1388 sections are not loaded into memory and so in theory have no
1389 use for a VMA.
1391 This means that the debuglink section must be given a non-zero
1392 VMA which makes it contiguous with other debug sections. So
1393 walk the current section list, find the section with the
1394 highest VMA and start the debuglink section after that one. */
1395 for (sec = obfd->sections, highest_section = NULL;
1396 sec != NULL;
1397 sec = sec->next)
1398 if (sec->vma > 0
1399 && (highest_section == NULL
1400 || sec->vma > highest_section->vma))
1401 highest_section = sec;
1403 if (highest_section)
1404 debuglink_vma = BFD_ALIGN (highest_section->vma
1405 + highest_section->size,
1406 /* FIXME: We ought to be using
1407 COFF_PAGE_SIZE here or maybe
1408 bfd_get_section_alignment() (if it
1409 was set) but since this is for PE
1410 and we know the required alignment
1411 it is easier just to hard code it. */
1412 0x1000);
1413 else
1414 /* Umm, not sure what to do in this case. */
1415 debuglink_vma = 0x1000;
1417 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1421 if (bfd_count_sections (obfd) == 0)
1423 non_fatal (_("there are no sections to be copied!"));
1424 return FALSE;
1427 if (gap_fill_set || pad_to_set)
1429 asection **set;
1430 unsigned int c, i;
1432 /* We must fill in gaps between the sections and/or we must pad
1433 the last section to a specified address. We do this by
1434 grabbing a list of the sections, sorting them by VMA, and
1435 increasing the section sizes as required to fill the gaps.
1436 We write out the gap contents below. */
1438 c = bfd_count_sections (obfd);
1439 osections = xmalloc (c * sizeof (asection *));
1440 set = osections;
1441 bfd_map_over_sections (obfd, get_sections, &set);
1443 qsort (osections, c, sizeof (asection *), compare_section_lma);
1445 gaps = xmalloc (c * sizeof (bfd_size_type));
1446 memset (gaps, 0, c * sizeof (bfd_size_type));
1448 if (gap_fill_set)
1450 for (i = 0; i < c - 1; i++)
1452 flagword flags;
1453 bfd_size_type size;
1454 bfd_vma gap_start, gap_stop;
1456 flags = bfd_get_section_flags (obfd, osections[i]);
1457 if ((flags & SEC_HAS_CONTENTS) == 0
1458 || (flags & SEC_LOAD) == 0)
1459 continue;
1461 size = bfd_section_size (obfd, osections[i]);
1462 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1463 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1464 if (gap_start < gap_stop)
1466 if (! bfd_set_section_size (obfd, osections[i],
1467 size + (gap_stop - gap_start)))
1469 non_fatal (_("Can't fill gap after %s: %s"),
1470 bfd_get_section_name (obfd, osections[i]),
1471 bfd_errmsg (bfd_get_error ()));
1472 status = 1;
1473 break;
1475 gaps[i] = gap_stop - gap_start;
1476 if (max_gap < gap_stop - gap_start)
1477 max_gap = gap_stop - gap_start;
1482 if (pad_to_set)
1484 bfd_vma lma;
1485 bfd_size_type size;
1487 lma = bfd_section_lma (obfd, osections[c - 1]);
1488 size = bfd_section_size (obfd, osections[c - 1]);
1489 if (lma + size < pad_to)
1491 if (! bfd_set_section_size (obfd, osections[c - 1],
1492 pad_to - lma))
1494 non_fatal (_("Can't add padding to %s: %s"),
1495 bfd_get_section_name (obfd, osections[c - 1]),
1496 bfd_errmsg (bfd_get_error ()));
1497 status = 1;
1499 else
1501 gaps[c - 1] = pad_to - (lma + size);
1502 if (max_gap < pad_to - (lma + size))
1503 max_gap = pad_to - (lma + size);
1509 /* Symbol filtering must happen after the output sections
1510 have been created, but before their contents are set. */
1511 dhandle = NULL;
1512 symsize = bfd_get_symtab_upper_bound (ibfd);
1513 if (symsize < 0)
1515 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1516 return FALSE;
1519 osympp = isympp = xmalloc (symsize);
1520 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1521 if (symcount < 0)
1523 bfd_nonfatal (bfd_get_filename (ibfd));
1524 return FALSE;
1527 if (convert_debugging)
1528 dhandle = read_debugging_info (ibfd, isympp, symcount);
1530 if (strip_symbols == STRIP_DEBUG
1531 || strip_symbols == STRIP_ALL
1532 || strip_symbols == STRIP_UNNEEDED
1533 || strip_symbols == STRIP_NONDEBUG
1534 || discard_locals != LOCALS_UNDEF
1535 || strip_specific_list != NULL
1536 || keep_specific_list != NULL
1537 || localize_specific_list != NULL
1538 || globalize_specific_list != NULL
1539 || keepglobal_specific_list != NULL
1540 || weaken_specific_list != NULL
1541 || prefix_symbols_string
1542 || sections_removed
1543 || sections_copied
1544 || convert_debugging
1545 || change_leading_char
1546 || remove_leading_char
1547 || redefine_sym_list
1548 || weaken)
1550 /* Mark symbols used in output relocations so that they
1551 are kept, even if they are local labels or static symbols.
1553 Note we iterate over the input sections examining their
1554 relocations since the relocations for the output sections
1555 haven't been set yet. mark_symbols_used_in_relocations will
1556 ignore input sections which have no corresponding output
1557 section. */
1558 if (strip_symbols != STRIP_ALL)
1559 bfd_map_over_sections (ibfd,
1560 mark_symbols_used_in_relocations,
1561 isympp);
1562 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1563 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1566 if (convert_debugging && dhandle != NULL)
1568 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1570 status = 1;
1571 return FALSE;
1575 bfd_set_symtab (obfd, osympp, symcount);
1577 /* This has to happen after the symbol table has been set. */
1578 bfd_map_over_sections (ibfd, copy_section, obfd);
1580 if (add_sections != NULL)
1582 struct section_add *padd;
1584 for (padd = add_sections; padd != NULL; padd = padd->next)
1586 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1587 0, padd->size))
1589 bfd_nonfatal (bfd_get_filename (obfd));
1590 return FALSE;
1595 if (gnu_debuglink_filename != NULL)
1597 if (! bfd_fill_in_gnu_debuglink_section
1598 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1600 bfd_nonfatal (gnu_debuglink_filename);
1601 return FALSE;
1605 if (gap_fill_set || pad_to_set)
1607 bfd_byte *buf;
1608 int c, i;
1610 /* Fill in the gaps. */
1611 if (max_gap > 8192)
1612 max_gap = 8192;
1613 buf = xmalloc (max_gap);
1614 memset (buf, gap_fill, max_gap);
1616 c = bfd_count_sections (obfd);
1617 for (i = 0; i < c; i++)
1619 if (gaps[i] != 0)
1621 bfd_size_type left;
1622 file_ptr off;
1624 left = gaps[i];
1625 off = bfd_section_size (obfd, osections[i]) - left;
1627 while (left > 0)
1629 bfd_size_type now;
1631 if (left > 8192)
1632 now = 8192;
1633 else
1634 now = left;
1636 if (! bfd_set_section_contents (obfd, osections[i], buf,
1637 off, now))
1639 bfd_nonfatal (bfd_get_filename (obfd));
1640 return FALSE;
1643 left -= now;
1644 off += now;
1650 /* Allow the BFD backend to copy any private data it understands
1651 from the input BFD to the output BFD. This is done last to
1652 permit the routine to look at the filtered symbol table, which is
1653 important for the ECOFF code at least. */
1654 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1655 && strip_symbols == STRIP_NONDEBUG)
1656 /* Do not copy the private data when creating an ELF format
1657 debug info file. We do not want the program headers. */
1659 else if (! bfd_copy_private_bfd_data (ibfd, obfd))
1661 non_fatal (_("%s: error copying private BFD data: %s"),
1662 bfd_get_filename (obfd),
1663 bfd_errmsg (bfd_get_error ()));
1664 return FALSE;
1667 /* Switch to the alternate machine code. We have to do this at the
1668 very end, because we only initialize the header when we create
1669 the first section. */
1670 if (use_alt_mach_code != 0
1671 && ! bfd_alt_mach_code (obfd, use_alt_mach_code))
1672 non_fatal (_("unknown alternate machine code, ignored"));
1674 return TRUE;
1677 #undef MKDIR
1678 #if defined (_WIN32) && !defined (__CYGWIN32__)
1679 #define MKDIR(DIR, MODE) mkdir (DIR)
1680 #else
1681 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1682 #endif
1684 /* Read each archive element in turn from IBFD, copy the
1685 contents to temp file, and keep the temp file handle. */
1687 static void
1688 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target)
1690 struct name_list
1692 struct name_list *next;
1693 const char *name;
1694 bfd *obfd;
1695 } *list, *l;
1696 bfd **ptr = &obfd->archive_head;
1697 bfd *this_element;
1698 char *dir = make_tempname (bfd_get_filename (obfd));
1700 /* Make a temp directory to hold the contents. */
1701 if (MKDIR (dir, 0700) != 0)
1702 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1703 dir, strerror (errno));
1705 obfd->has_armap = ibfd->has_armap;
1707 list = NULL;
1709 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1711 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1712 RETURN_NONFATAL (bfd_get_filename (obfd));
1714 while (!status && this_element != NULL)
1716 char *output_name;
1717 bfd *output_bfd;
1718 bfd *last_element;
1719 struct stat buf;
1720 int stat_status = 0;
1721 bfd_boolean delete = TRUE;
1723 /* Create an output file for this member. */
1724 output_name = concat (dir, "/",
1725 bfd_get_filename (this_element), (char *) 0);
1727 /* If the file already exists, make another temp dir. */
1728 if (stat (output_name, &buf) >= 0)
1730 output_name = make_tempname (output_name);
1731 if (MKDIR (output_name, 0700) != 0)
1732 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1733 output_name, strerror (errno));
1735 l = xmalloc (sizeof (struct name_list));
1736 l->name = output_name;
1737 l->next = list;
1738 l->obfd = NULL;
1739 list = l;
1740 output_name = concat (output_name, "/",
1741 bfd_get_filename (this_element), (char *) 0);
1744 output_bfd = bfd_openw (output_name, output_target);
1745 if (preserve_dates)
1747 stat_status = bfd_stat_arch_elt (this_element, &buf);
1749 if (stat_status != 0)
1750 non_fatal (_("internal stat error on %s"),
1751 bfd_get_filename (this_element));
1754 l = xmalloc (sizeof (struct name_list));
1755 l->name = output_name;
1756 l->next = list;
1757 l->obfd = NULL;
1758 list = l;
1760 if (output_bfd == NULL)
1761 RETURN_NONFATAL (output_name);
1763 if (bfd_check_format (this_element, bfd_object))
1765 delete = ! copy_object (this_element, output_bfd);
1767 if (! delete
1768 || bfd_get_arch (this_element) != bfd_arch_unknown)
1770 if (!bfd_close (output_bfd))
1772 bfd_nonfatal (bfd_get_filename (output_bfd));
1773 /* Error in new object file. Don't change archive. */
1774 status = 1;
1777 else
1778 goto copy_unknown_element;
1780 else
1782 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1783 bfd_get_archive_filename (this_element));
1785 copy_unknown_element:
1786 delete = !copy_unknown_object (this_element, output_bfd);
1787 if (!bfd_close_all_done (output_bfd))
1789 bfd_nonfatal (bfd_get_filename (output_bfd));
1790 /* Error in new object file. Don't change archive. */
1791 status = 1;
1795 if (delete)
1797 unlink (output_name);
1798 status = 1;
1800 else
1802 if (preserve_dates && stat_status == 0)
1803 set_times (output_name, &buf);
1805 /* Open the newly output file and attach to our list. */
1806 output_bfd = bfd_openr (output_name, output_target);
1808 l->obfd = output_bfd;
1810 *ptr = output_bfd;
1811 ptr = &output_bfd->next;
1813 last_element = this_element;
1815 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1817 bfd_close (last_element);
1820 *ptr = NULL;
1822 if (!bfd_close (obfd))
1823 RETURN_NONFATAL (bfd_get_filename (obfd));
1825 if (!bfd_close (ibfd))
1826 RETURN_NONFATAL (bfd_get_filename (ibfd));
1828 /* Delete all the files that we opened. */
1829 for (l = list; l != NULL; l = l->next)
1831 if (l->obfd == NULL)
1832 rmdir (l->name);
1833 else
1835 bfd_close (l->obfd);
1836 unlink (l->name);
1839 rmdir (dir);
1842 /* The top-level control. */
1844 static void
1845 copy_file (const char *input_filename, const char *output_filename,
1846 const char *input_target, const char *output_target)
1848 bfd *ibfd;
1849 char **obj_matching;
1850 char **core_matching;
1852 if (get_file_size (input_filename) < 1)
1854 non_fatal (_("error: the input file '%s' is empty"), input_filename);
1855 status = 1;
1856 return;
1859 /* To allow us to do "strip *" without dying on the first
1860 non-object file, failures are nonfatal. */
1861 ibfd = bfd_openr (input_filename, input_target);
1862 if (ibfd == NULL)
1863 RETURN_NONFATAL (input_filename);
1865 if (bfd_check_format (ibfd, bfd_archive))
1867 bfd *obfd;
1869 /* bfd_get_target does not return the correct value until
1870 bfd_check_format succeeds. */
1871 if (output_target == NULL)
1872 output_target = bfd_get_target (ibfd);
1874 obfd = bfd_openw (output_filename, output_target);
1875 if (obfd == NULL)
1876 RETURN_NONFATAL (output_filename);
1878 copy_archive (ibfd, obfd, output_target);
1880 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
1882 bfd *obfd;
1883 do_copy:
1885 /* bfd_get_target does not return the correct value until
1886 bfd_check_format succeeds. */
1887 if (output_target == NULL)
1888 output_target = bfd_get_target (ibfd);
1890 obfd = bfd_openw (output_filename, output_target);
1891 if (obfd == NULL)
1892 RETURN_NONFATAL (output_filename);
1894 if (! copy_object (ibfd, obfd))
1895 status = 1;
1897 if (!bfd_close (obfd))
1898 RETURN_NONFATAL (output_filename);
1900 if (!bfd_close (ibfd))
1901 RETURN_NONFATAL (input_filename);
1904 else
1906 bfd_error_type obj_error = bfd_get_error ();
1907 bfd_error_type core_error;
1909 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1911 /* This probably can't happen.. */
1912 if (obj_error == bfd_error_file_ambiguously_recognized)
1913 free (obj_matching);
1914 goto do_copy;
1917 core_error = bfd_get_error ();
1918 /* Report the object error in preference to the core error. */
1919 if (obj_error != core_error)
1920 bfd_set_error (obj_error);
1922 bfd_nonfatal (input_filename);
1924 if (obj_error == bfd_error_file_ambiguously_recognized)
1926 list_matching_formats (obj_matching);
1927 free (obj_matching);
1929 if (core_error == bfd_error_file_ambiguously_recognized)
1931 list_matching_formats (core_matching);
1932 free (core_matching);
1935 status = 1;
1939 /* Add a name to the section renaming list. */
1941 static void
1942 add_section_rename (const char * old_name, const char * new_name,
1943 flagword flags)
1945 section_rename * rename;
1947 /* Check for conflicts first. */
1948 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1949 if (strcmp (rename->old_name, old_name) == 0)
1951 /* Silently ignore duplicate definitions. */
1952 if (strcmp (rename->new_name, new_name) == 0
1953 && rename->flags == flags)
1954 return;
1956 fatal (_("Multiple renames of section %s"), old_name);
1959 rename = xmalloc (sizeof (* rename));
1961 rename->old_name = old_name;
1962 rename->new_name = new_name;
1963 rename->flags = flags;
1964 rename->next = section_rename_list;
1966 section_rename_list = rename;
1969 /* Check the section rename list for a new name of the input section
1970 ISECTION. Return the new name if one is found.
1971 Also set RETURNED_FLAGS to the flags to be used for this section. */
1973 static const char *
1974 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
1975 flagword * returned_flags)
1977 const char * old_name = bfd_section_name (ibfd, isection);
1978 section_rename * rename;
1980 /* Default to using the flags of the input section. */
1981 * returned_flags = bfd_get_section_flags (ibfd, isection);
1983 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1984 if (strcmp (rename->old_name, old_name) == 0)
1986 if (rename->flags != (flagword) -1)
1987 * returned_flags = rename->flags;
1989 return rename->new_name;
1992 return old_name;
1995 /* Once each of the sections is copied, we may still need to do some
1996 finalization work for private section headers. Do that here. */
1998 static void
1999 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2001 const char *err;
2003 /* Allow the BFD backend to copy any private data it understands
2004 from the input section to the output section. */
2005 if (! bfd_copy_private_header_data (ibfd, obfd))
2007 err = _("private header data");
2008 goto loser;
2011 /* All went well. */
2012 return;
2014 loser:
2015 non_fatal (_("%s: error in %s: %s"),
2016 bfd_get_filename (ibfd),
2017 err, bfd_errmsg (bfd_get_error ()));
2018 status = 1;
2021 /* Create a section in OBFD with the same
2022 name and attributes as ISECTION in IBFD. */
2024 static void
2025 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2027 bfd *obfd = obfdarg;
2028 struct section_list *p;
2029 sec_ptr osection;
2030 bfd_size_type size;
2031 bfd_vma vma;
2032 bfd_vma lma;
2033 flagword flags;
2034 const char *err;
2035 const char * name;
2036 char *prefix = NULL;
2038 if (is_strip_section (ibfd, isection))
2039 return;
2041 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2042 if (p != NULL)
2043 p->used = TRUE;
2045 /* Get the, possibly new, name of the output section. */
2046 name = find_section_rename (ibfd, isection, & flags);
2048 /* Prefix sections. */
2049 if ((prefix_alloc_sections_string)
2050 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2051 prefix = prefix_alloc_sections_string;
2052 else if (prefix_sections_string)
2053 prefix = prefix_sections_string;
2055 if (prefix)
2057 char *n;
2059 n = xmalloc (strlen (prefix) + strlen (name) + 1);
2060 strcpy (n, prefix);
2061 strcat (n, name);
2062 name = n;
2065 if (p != NULL && p->set_flags)
2066 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2067 else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
2068 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2070 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2072 if (osection == NULL)
2074 err = _("making");
2075 goto loser;
2078 if (strip_symbols == STRIP_NONDEBUG
2079 && obfd->xvec->flavour == bfd_target_elf_flavour
2080 && (flags & SEC_ALLOC) != 0
2081 && (p == NULL || !p->set_flags))
2082 elf_section_type (osection) = SHT_NOBITS;
2084 size = bfd_section_size (ibfd, isection);
2085 if (copy_byte >= 0)
2086 size = (size + interleave - 1) / interleave;
2087 if (! bfd_set_section_size (obfd, osection, size))
2089 err = _("size");
2090 goto loser;
2093 vma = bfd_section_vma (ibfd, isection);
2094 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2095 vma += p->vma_val;
2096 else if (p != NULL && p->change_vma == CHANGE_SET)
2097 vma = p->vma_val;
2098 else
2099 vma += change_section_address;
2101 if (! bfd_set_section_vma (obfd, osection, vma))
2103 err = _("vma");
2104 goto loser;
2107 lma = isection->lma;
2108 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2110 if (p->change_lma == CHANGE_MODIFY)
2111 lma += p->lma_val;
2112 else if (p->change_lma == CHANGE_SET)
2113 lma = p->lma_val;
2114 else
2115 abort ();
2117 else
2118 lma += change_section_address;
2120 osection->lma = lma;
2122 /* FIXME: This is probably not enough. If we change the LMA we
2123 may have to recompute the header for the file as well. */
2124 if (!bfd_set_section_alignment (obfd,
2125 osection,
2126 bfd_section_alignment (ibfd, isection)))
2128 err = _("alignment");
2129 goto loser;
2132 /* Copy merge entity size. */
2133 osection->entsize = isection->entsize;
2135 /* This used to be mangle_section; we do here to avoid using
2136 bfd_get_section_by_name since some formats allow multiple
2137 sections with the same name. */
2138 isection->output_section = osection;
2139 isection->output_offset = 0;
2141 /* Allow the BFD backend to copy any private data it understands
2142 from the input section to the output section. */
2143 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
2144 && strip_symbols == STRIP_NONDEBUG)
2145 /* Do not copy the private data when creating an ELF format
2146 debug info file. We do not want the program headers. */
2148 else if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2150 err = _("private data");
2151 goto loser;
2154 /* All went well. */
2155 return;
2157 loser:
2158 non_fatal (_("%s: section `%s': error in %s: %s"),
2159 bfd_get_filename (ibfd),
2160 bfd_section_name (ibfd, isection),
2161 err, bfd_errmsg (bfd_get_error ()));
2162 status = 1;
2165 /* Copy the data of input section ISECTION of IBFD
2166 to an output section with the same name in OBFD.
2167 If stripping then don't copy any relocation info. */
2169 static void
2170 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2172 bfd *obfd = obfdarg;
2173 struct section_list *p;
2174 arelent **relpp;
2175 long relcount;
2176 sec_ptr osection;
2177 bfd_size_type size;
2178 long relsize;
2179 flagword flags;
2181 /* If we have already failed earlier on,
2182 do not keep on generating complaints now. */
2183 if (status != 0)
2184 return;
2186 if (is_strip_section (ibfd, isection))
2187 return;
2189 flags = bfd_get_section_flags (ibfd, isection);
2190 if ((flags & SEC_GROUP) != 0)
2191 return;
2193 osection = isection->output_section;
2194 size = bfd_get_section_size (isection);
2196 if (size == 0 || osection == 0)
2197 return;
2199 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2201 /* Core files do not need to be relocated. */
2202 if (bfd_get_format (obfd) == bfd_core)
2203 relsize = 0;
2204 else
2206 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2208 if (relsize < 0)
2210 /* Do not complain if the target does not support relocations. */
2211 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2212 relsize = 0;
2213 else
2214 RETURN_NONFATAL (bfd_get_filename (ibfd));
2218 if (relsize == 0)
2219 bfd_set_reloc (obfd, osection, NULL, 0);
2220 else
2222 relpp = xmalloc (relsize);
2223 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2224 if (relcount < 0)
2225 RETURN_NONFATAL (bfd_get_filename (ibfd));
2227 if (strip_symbols == STRIP_ALL)
2229 /* Remove relocations which are not in
2230 keep_strip_specific_list. */
2231 arelent **temp_relpp;
2232 long temp_relcount = 0;
2233 long i;
2235 temp_relpp = xmalloc (relsize);
2236 for (i = 0; i < relcount; i++)
2237 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2238 keep_specific_list))
2239 temp_relpp [temp_relcount++] = relpp [i];
2240 relcount = temp_relcount;
2241 free (relpp);
2242 relpp = temp_relpp;
2245 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2246 if (relcount == 0)
2247 free (relpp);
2250 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2251 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2253 void *memhunk = xmalloc (size);
2255 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2256 RETURN_NONFATAL (bfd_get_filename (ibfd));
2258 if (copy_byte >= 0)
2260 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2261 char *from = (char *) memhunk + copy_byte;
2262 char *to = memhunk;
2263 char *end = (char *) memhunk + size;
2265 for (; from < end; from += interleave)
2266 *to++ = *from;
2268 size = (size + interleave - 1 - copy_byte) / interleave;
2269 osection->lma /= interleave;
2272 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2273 RETURN_NONFATAL (bfd_get_filename (obfd));
2275 free (memhunk);
2277 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2279 void *memhunk = xmalloc (size);
2281 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2282 flag--they can just remove the section entirely and add it
2283 back again. However, we do permit them to turn on the
2284 SEC_HAS_CONTENTS flag, and take it to mean that the section
2285 contents should be zeroed out. */
2287 memset (memhunk, 0, size);
2288 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2289 RETURN_NONFATAL (bfd_get_filename (obfd));
2290 free (memhunk);
2294 /* Get all the sections. This is used when --gap-fill or --pad-to is
2295 used. */
2297 static void
2298 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2300 asection ***secppp = secppparg;
2302 **secppp = osection;
2303 ++(*secppp);
2306 /* Sort sections by VMA. This is called via qsort, and is used when
2307 --gap-fill or --pad-to is used. We force non loadable or empty
2308 sections to the front, where they are easier to ignore. */
2310 static int
2311 compare_section_lma (const void *arg1, const void *arg2)
2313 const asection *const *sec1 = arg1;
2314 const asection *const *sec2 = arg2;
2315 flagword flags1, flags2;
2317 /* Sort non loadable sections to the front. */
2318 flags1 = (*sec1)->flags;
2319 flags2 = (*sec2)->flags;
2320 if ((flags1 & SEC_HAS_CONTENTS) == 0
2321 || (flags1 & SEC_LOAD) == 0)
2323 if ((flags2 & SEC_HAS_CONTENTS) != 0
2324 && (flags2 & SEC_LOAD) != 0)
2325 return -1;
2327 else
2329 if ((flags2 & SEC_HAS_CONTENTS) == 0
2330 || (flags2 & SEC_LOAD) == 0)
2331 return 1;
2334 /* Sort sections by LMA. */
2335 if ((*sec1)->lma > (*sec2)->lma)
2336 return 1;
2337 else if ((*sec1)->lma < (*sec2)->lma)
2338 return -1;
2340 /* Sort sections with the same LMA by size. */
2341 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2342 return 1;
2343 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2344 return -1;
2346 return 0;
2349 /* Mark all the symbols which will be used in output relocations with
2350 the BSF_KEEP flag so that those symbols will not be stripped.
2352 Ignore relocations which will not appear in the output file. */
2354 static void
2355 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2357 asymbol **symbols = symbolsarg;
2358 long relsize;
2359 arelent **relpp;
2360 long relcount, i;
2362 /* Ignore an input section with no corresponding output section. */
2363 if (isection->output_section == NULL)
2364 return;
2366 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2367 if (relsize < 0)
2369 /* Do not complain if the target does not support relocations. */
2370 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2371 return;
2372 bfd_fatal (bfd_get_filename (ibfd));
2375 if (relsize == 0)
2376 return;
2378 relpp = xmalloc (relsize);
2379 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2380 if (relcount < 0)
2381 bfd_fatal (bfd_get_filename (ibfd));
2383 /* Examine each symbol used in a relocation. If it's not one of the
2384 special bfd section symbols, then mark it with BSF_KEEP. */
2385 for (i = 0; i < relcount; i++)
2387 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2388 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2389 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2390 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2393 if (relpp != NULL)
2394 free (relpp);
2397 /* Write out debugging information. */
2399 static bfd_boolean
2400 write_debugging_info (bfd *obfd, void *dhandle,
2401 long *symcountp ATTRIBUTE_UNUSED,
2402 asymbol ***symppp ATTRIBUTE_UNUSED)
2404 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2405 return write_ieee_debugging_info (obfd, dhandle);
2407 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2408 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2410 bfd_byte *syms, *strings;
2411 bfd_size_type symsize, stringsize;
2412 asection *stabsec, *stabstrsec;
2413 flagword flags;
2415 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2416 &symsize, &strings,
2417 &stringsize))
2418 return FALSE;
2420 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2421 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2422 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2423 if (stabsec == NULL
2424 || stabstrsec == NULL
2425 || ! bfd_set_section_size (obfd, stabsec, symsize)
2426 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2427 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2428 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2430 non_fatal (_("%s: can't create debugging section: %s"),
2431 bfd_get_filename (obfd),
2432 bfd_errmsg (bfd_get_error ()));
2433 return FALSE;
2436 /* We can get away with setting the section contents now because
2437 the next thing the caller is going to do is copy over the
2438 real sections. We may someday have to split the contents
2439 setting out of this function. */
2440 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2441 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2442 stringsize))
2444 non_fatal (_("%s: can't set debugging section contents: %s"),
2445 bfd_get_filename (obfd),
2446 bfd_errmsg (bfd_get_error ()));
2447 return FALSE;
2450 return TRUE;
2453 non_fatal (_("%s: don't know how to write debugging information for %s"),
2454 bfd_get_filename (obfd), bfd_get_target (obfd));
2455 return FALSE;
2458 static int
2459 strip_main (int argc, char *argv[])
2461 char *input_target = NULL;
2462 char *output_target = NULL;
2463 bfd_boolean show_version = FALSE;
2464 bfd_boolean formats_info = FALSE;
2465 int c;
2466 int i;
2467 struct section_list *p;
2468 char *output_file = NULL;
2470 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2471 strip_options, (int *) 0)) != EOF)
2473 switch (c)
2475 case 'I':
2476 input_target = optarg;
2477 break;
2478 case 'O':
2479 output_target = optarg;
2480 break;
2481 case 'F':
2482 input_target = output_target = optarg;
2483 break;
2484 case 'R':
2485 p = find_section_list (optarg, TRUE);
2486 p->remove = TRUE;
2487 sections_removed = TRUE;
2488 break;
2489 case 's':
2490 strip_symbols = STRIP_ALL;
2491 break;
2492 case 'S':
2493 case 'g':
2494 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2495 strip_symbols = STRIP_DEBUG;
2496 break;
2497 case OPTION_STRIP_UNNEEDED:
2498 strip_symbols = STRIP_UNNEEDED;
2499 break;
2500 case 'K':
2501 add_specific_symbol (optarg, &keep_specific_list);
2502 break;
2503 case 'N':
2504 add_specific_symbol (optarg, &strip_specific_list);
2505 break;
2506 case 'o':
2507 output_file = optarg;
2508 break;
2509 case 'p':
2510 preserve_dates = TRUE;
2511 break;
2512 case 'x':
2513 discard_locals = LOCALS_ALL;
2514 break;
2515 case 'X':
2516 discard_locals = LOCALS_START_L;
2517 break;
2518 case 'v':
2519 verbose = TRUE;
2520 break;
2521 case 'V':
2522 show_version = TRUE;
2523 break;
2524 case OPTION_FORMATS_INFO:
2525 formats_info = TRUE;
2526 break;
2527 case OPTION_ONLY_KEEP_DEBUG:
2528 strip_symbols = STRIP_NONDEBUG;
2529 break;
2530 case OPTION_KEEP_FILE_SYMBOLS:
2531 keep_file_symbols = 1;
2532 break;
2533 case 0:
2534 /* We've been given a long option. */
2535 break;
2536 case 'w':
2537 wildcard = TRUE;
2538 break;
2539 case 'H':
2540 case 'h':
2541 strip_usage (stdout, 0);
2542 default:
2543 strip_usage (stderr, 1);
2547 if (formats_info)
2549 display_info ();
2550 return 0;
2553 if (show_version)
2554 print_version ("strip");
2556 /* Default is to strip all symbols. */
2557 if (strip_symbols == STRIP_UNDEF
2558 && discard_locals == LOCALS_UNDEF
2559 && strip_specific_list == NULL)
2560 strip_symbols = STRIP_ALL;
2562 if (output_target == NULL)
2563 output_target = input_target;
2565 i = optind;
2566 if (i == argc
2567 || (output_file != NULL && (i + 1) < argc))
2568 strip_usage (stderr, 1);
2570 for (; i < argc; i++)
2572 int hold_status = status;
2573 struct stat statbuf;
2574 char *tmpname;
2576 if (get_file_size (argv[i]) < 1)
2577 continue;
2579 if (preserve_dates)
2580 /* No need to check the return value of stat().
2581 It has already been checked in get_file_size(). */
2582 stat (argv[i], &statbuf);
2584 if (output_file != NULL)
2585 tmpname = output_file;
2586 else
2587 tmpname = make_tempname (argv[i]);
2588 status = 0;
2590 copy_file (argv[i], tmpname, input_target, output_target);
2591 if (status == 0)
2593 if (preserve_dates)
2594 set_times (tmpname, &statbuf);
2595 if (output_file == NULL)
2596 smart_rename (tmpname, argv[i], preserve_dates);
2597 status = hold_status;
2599 else
2600 unlink_if_ordinary (tmpname);
2601 if (output_file == NULL)
2602 free (tmpname);
2605 return 0;
2608 static int
2609 copy_main (int argc, char *argv[])
2611 char * binary_architecture = NULL;
2612 char *input_filename = NULL;
2613 char *output_filename = NULL;
2614 char *input_target = NULL;
2615 char *output_target = NULL;
2616 bfd_boolean show_version = FALSE;
2617 bfd_boolean change_warn = TRUE;
2618 bfd_boolean formats_info = FALSE;
2619 int c;
2620 struct section_list *p;
2621 struct stat statbuf;
2623 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2624 copy_options, (int *) 0)) != EOF)
2626 switch (c)
2628 case 'b':
2629 copy_byte = atoi (optarg);
2630 if (copy_byte < 0)
2631 fatal (_("byte number must be non-negative"));
2632 break;
2634 case 'B':
2635 binary_architecture = optarg;
2636 break;
2638 case 'i':
2639 interleave = atoi (optarg);
2640 if (interleave < 1)
2641 fatal (_("interleave must be positive"));
2642 break;
2644 case 'I':
2645 case 's': /* "source" - 'I' is preferred */
2646 input_target = optarg;
2647 break;
2649 case 'O':
2650 case 'd': /* "destination" - 'O' is preferred */
2651 output_target = optarg;
2652 break;
2654 case 'F':
2655 input_target = output_target = optarg;
2656 break;
2658 case 'j':
2659 p = find_section_list (optarg, TRUE);
2660 if (p->remove)
2661 fatal (_("%s both copied and removed"), optarg);
2662 p->copy = TRUE;
2663 sections_copied = TRUE;
2664 break;
2666 case 'R':
2667 p = find_section_list (optarg, TRUE);
2668 if (p->copy)
2669 fatal (_("%s both copied and removed"), optarg);
2670 p->remove = TRUE;
2671 sections_removed = TRUE;
2672 break;
2674 case 'S':
2675 strip_symbols = STRIP_ALL;
2676 break;
2678 case 'g':
2679 strip_symbols = STRIP_DEBUG;
2680 break;
2682 case OPTION_STRIP_UNNEEDED:
2683 strip_symbols = STRIP_UNNEEDED;
2684 break;
2686 case OPTION_ONLY_KEEP_DEBUG:
2687 strip_symbols = STRIP_NONDEBUG;
2688 break;
2690 case OPTION_KEEP_FILE_SYMBOLS:
2691 keep_file_symbols = 1;
2692 break;
2694 case OPTION_ADD_GNU_DEBUGLINK:
2695 gnu_debuglink_filename = optarg;
2696 break;
2698 case 'K':
2699 add_specific_symbol (optarg, &keep_specific_list);
2700 break;
2702 case 'N':
2703 add_specific_symbol (optarg, &strip_specific_list);
2704 break;
2706 case OPTION_STRIP_UNNEEDED_SYMBOL:
2707 add_specific_symbol (optarg, &strip_unneeded_list);
2708 break;
2710 case 'L':
2711 add_specific_symbol (optarg, &localize_specific_list);
2712 break;
2714 case OPTION_GLOBALIZE_SYMBOL:
2715 add_specific_symbol (optarg, &globalize_specific_list);
2716 break;
2718 case 'G':
2719 add_specific_symbol (optarg, &keepglobal_specific_list);
2720 break;
2722 case 'W':
2723 add_specific_symbol (optarg, &weaken_specific_list);
2724 break;
2726 case 'p':
2727 preserve_dates = TRUE;
2728 break;
2730 case 'w':
2731 wildcard = TRUE;
2732 break;
2734 case 'x':
2735 discard_locals = LOCALS_ALL;
2736 break;
2738 case 'X':
2739 discard_locals = LOCALS_START_L;
2740 break;
2742 case 'v':
2743 verbose = TRUE;
2744 break;
2746 case 'V':
2747 show_version = TRUE;
2748 break;
2750 case OPTION_FORMATS_INFO:
2751 formats_info = TRUE;
2752 break;
2754 case OPTION_WEAKEN:
2755 weaken = TRUE;
2756 break;
2758 case OPTION_ADD_SECTION:
2760 const char *s;
2761 off_t size;
2762 struct section_add *pa;
2763 int len;
2764 char *name;
2765 FILE *f;
2767 s = strchr (optarg, '=');
2769 if (s == NULL)
2770 fatal (_("bad format for %s"), "--add-section");
2772 size = get_file_size (s + 1);
2773 if (size < 1)
2774 break;
2776 pa = xmalloc (sizeof (struct section_add));
2778 len = s - optarg;
2779 name = xmalloc (len + 1);
2780 strncpy (name, optarg, len);
2781 name[len] = '\0';
2782 pa->name = name;
2784 pa->filename = s + 1;
2785 pa->size = size;
2786 pa->contents = xmalloc (size);
2788 f = fopen (pa->filename, FOPEN_RB);
2790 if (f == NULL)
2791 fatal (_("cannot open: %s: %s"),
2792 pa->filename, strerror (errno));
2794 if (fread (pa->contents, 1, pa->size, f) == 0
2795 || ferror (f))
2796 fatal (_("%s: fread failed"), pa->filename);
2798 fclose (f);
2800 pa->next = add_sections;
2801 add_sections = pa;
2803 break;
2805 case OPTION_CHANGE_START:
2806 change_start = parse_vma (optarg, "--change-start");
2807 break;
2809 case OPTION_CHANGE_SECTION_ADDRESS:
2810 case OPTION_CHANGE_SECTION_LMA:
2811 case OPTION_CHANGE_SECTION_VMA:
2813 const char *s;
2814 int len;
2815 char *name;
2816 char *option = NULL;
2817 bfd_vma val;
2818 enum change_action what = CHANGE_IGNORE;
2820 switch (c)
2822 case OPTION_CHANGE_SECTION_ADDRESS:
2823 option = "--change-section-address";
2824 break;
2825 case OPTION_CHANGE_SECTION_LMA:
2826 option = "--change-section-lma";
2827 break;
2828 case OPTION_CHANGE_SECTION_VMA:
2829 option = "--change-section-vma";
2830 break;
2833 s = strchr (optarg, '=');
2834 if (s == NULL)
2836 s = strchr (optarg, '+');
2837 if (s == NULL)
2839 s = strchr (optarg, '-');
2840 if (s == NULL)
2841 fatal (_("bad format for %s"), option);
2845 len = s - optarg;
2846 name = xmalloc (len + 1);
2847 strncpy (name, optarg, len);
2848 name[len] = '\0';
2850 p = find_section_list (name, TRUE);
2852 val = parse_vma (s + 1, option);
2854 switch (*s)
2856 case '=': what = CHANGE_SET; break;
2857 case '-': val = - val; /* Drop through. */
2858 case '+': what = CHANGE_MODIFY; break;
2861 switch (c)
2863 case OPTION_CHANGE_SECTION_ADDRESS:
2864 p->change_vma = what;
2865 p->vma_val = val;
2866 /* Drop through. */
2868 case OPTION_CHANGE_SECTION_LMA:
2869 p->change_lma = what;
2870 p->lma_val = val;
2871 break;
2873 case OPTION_CHANGE_SECTION_VMA:
2874 p->change_vma = what;
2875 p->vma_val = val;
2876 break;
2879 break;
2881 case OPTION_CHANGE_ADDRESSES:
2882 change_section_address = parse_vma (optarg, "--change-addresses");
2883 change_start = change_section_address;
2884 break;
2886 case OPTION_CHANGE_WARNINGS:
2887 change_warn = TRUE;
2888 break;
2890 case OPTION_CHANGE_LEADING_CHAR:
2891 change_leading_char = TRUE;
2892 break;
2894 case OPTION_DEBUGGING:
2895 convert_debugging = TRUE;
2896 break;
2898 case OPTION_GAP_FILL:
2900 bfd_vma gap_fill_vma;
2902 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2903 gap_fill = (bfd_byte) gap_fill_vma;
2904 if ((bfd_vma) gap_fill != gap_fill_vma)
2906 char buff[20];
2908 sprintf_vma (buff, gap_fill_vma);
2910 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2911 buff, gap_fill);
2913 gap_fill_set = TRUE;
2915 break;
2917 case OPTION_NO_CHANGE_WARNINGS:
2918 change_warn = FALSE;
2919 break;
2921 case OPTION_PAD_TO:
2922 pad_to = parse_vma (optarg, "--pad-to");
2923 pad_to_set = TRUE;
2924 break;
2926 case OPTION_REMOVE_LEADING_CHAR:
2927 remove_leading_char = TRUE;
2928 break;
2930 case OPTION_REDEFINE_SYM:
2932 /* Push this redefinition onto redefine_symbol_list. */
2934 int len;
2935 const char *s;
2936 const char *nextarg;
2937 char *source, *target;
2939 s = strchr (optarg, '=');
2940 if (s == NULL)
2941 fatal (_("bad format for %s"), "--redefine-sym");
2943 len = s - optarg;
2944 source = xmalloc (len + 1);
2945 strncpy (source, optarg, len);
2946 source[len] = '\0';
2948 nextarg = s + 1;
2949 len = strlen (nextarg);
2950 target = xmalloc (len + 1);
2951 strcpy (target, nextarg);
2953 redefine_list_append ("--redefine-sym", source, target);
2955 free (source);
2956 free (target);
2958 break;
2960 case OPTION_REDEFINE_SYMS:
2961 add_redefine_syms_file (optarg);
2962 break;
2964 case OPTION_SET_SECTION_FLAGS:
2966 const char *s;
2967 int len;
2968 char *name;
2970 s = strchr (optarg, '=');
2971 if (s == NULL)
2972 fatal (_("bad format for %s"), "--set-section-flags");
2974 len = s - optarg;
2975 name = xmalloc (len + 1);
2976 strncpy (name, optarg, len);
2977 name[len] = '\0';
2979 p = find_section_list (name, TRUE);
2981 p->set_flags = TRUE;
2982 p->flags = parse_flags (s + 1);
2984 break;
2986 case OPTION_RENAME_SECTION:
2988 flagword flags;
2989 const char *eq, *fl;
2990 char *old_name;
2991 char *new_name;
2992 unsigned int len;
2994 eq = strchr (optarg, '=');
2995 if (eq == NULL)
2996 fatal (_("bad format for %s"), "--rename-section");
2998 len = eq - optarg;
2999 if (len == 0)
3000 fatal (_("bad format for %s"), "--rename-section");
3002 old_name = xmalloc (len + 1);
3003 strncpy (old_name, optarg, len);
3004 old_name[len] = 0;
3006 eq++;
3007 fl = strchr (eq, ',');
3008 if (fl)
3010 flags = parse_flags (fl + 1);
3011 len = fl - eq;
3013 else
3015 flags = -1;
3016 len = strlen (eq);
3019 if (len == 0)
3020 fatal (_("bad format for %s"), "--rename-section");
3022 new_name = xmalloc (len + 1);
3023 strncpy (new_name, eq, len);
3024 new_name[len] = 0;
3026 add_section_rename (old_name, new_name, flags);
3028 break;
3030 case OPTION_SET_START:
3031 set_start = parse_vma (optarg, "--set-start");
3032 set_start_set = TRUE;
3033 break;
3035 case OPTION_SREC_LEN:
3036 Chunk = parse_vma (optarg, "--srec-len");
3037 break;
3039 case OPTION_SREC_FORCES3:
3040 S3Forced = TRUE;
3041 break;
3043 case OPTION_STRIP_SYMBOLS:
3044 add_specific_symbols (optarg, &strip_specific_list);
3045 break;
3047 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3048 add_specific_symbols (optarg, &strip_unneeded_list);
3049 break;
3051 case OPTION_KEEP_SYMBOLS:
3052 add_specific_symbols (optarg, &keep_specific_list);
3053 break;
3055 case OPTION_LOCALIZE_SYMBOLS:
3056 add_specific_symbols (optarg, &localize_specific_list);
3057 break;
3059 case OPTION_GLOBALIZE_SYMBOLS:
3060 add_specific_symbols (optarg, &globalize_specific_list);
3061 break;
3063 case OPTION_KEEPGLOBAL_SYMBOLS:
3064 add_specific_symbols (optarg, &keepglobal_specific_list);
3065 break;
3067 case OPTION_WEAKEN_SYMBOLS:
3068 add_specific_symbols (optarg, &weaken_specific_list);
3069 break;
3071 case OPTION_ALT_MACH_CODE:
3072 use_alt_mach_code = atoi (optarg);
3073 if (use_alt_mach_code <= 0)
3074 fatal (_("alternate machine code index must be positive"));
3075 break;
3077 case OPTION_PREFIX_SYMBOLS:
3078 prefix_symbols_string = optarg;
3079 break;
3081 case OPTION_PREFIX_SECTIONS:
3082 prefix_sections_string = optarg;
3083 break;
3085 case OPTION_PREFIX_ALLOC_SECTIONS:
3086 prefix_alloc_sections_string = optarg;
3087 break;
3089 case OPTION_READONLY_TEXT:
3090 bfd_flags_to_set |= WP_TEXT;
3091 bfd_flags_to_clear &= ~WP_TEXT;
3092 break;
3094 case OPTION_WRITABLE_TEXT:
3095 bfd_flags_to_clear |= WP_TEXT;
3096 bfd_flags_to_set &= ~WP_TEXT;
3097 break;
3099 case OPTION_PURE:
3100 bfd_flags_to_set |= D_PAGED;
3101 bfd_flags_to_clear &= ~D_PAGED;
3102 break;
3104 case OPTION_IMPURE:
3105 bfd_flags_to_clear |= D_PAGED;
3106 bfd_flags_to_set &= ~D_PAGED;
3107 break;
3109 case 0:
3110 /* We've been given a long option. */
3111 break;
3113 case 'H':
3114 case 'h':
3115 copy_usage (stdout, 0);
3117 default:
3118 copy_usage (stderr, 1);
3122 if (formats_info)
3124 display_info ();
3125 return 0;
3128 if (show_version)
3129 print_version ("objcopy");
3131 if (copy_byte >= interleave)
3132 fatal (_("byte number must be less than interleave"));
3134 if (optind == argc || optind + 2 < argc)
3135 copy_usage (stderr, 1);
3137 input_filename = argv[optind];
3138 if (optind + 1 < argc)
3139 output_filename = argv[optind + 1];
3141 /* Default is to strip no symbols. */
3142 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3143 strip_symbols = STRIP_NONE;
3145 if (output_target == NULL)
3146 output_target = input_target;
3148 if (binary_architecture != NULL)
3150 if (input_target && strcmp (input_target, "binary") == 0)
3152 const bfd_arch_info_type * temp_arch_info;
3154 temp_arch_info = bfd_scan_arch (binary_architecture);
3156 if (temp_arch_info != NULL)
3158 bfd_external_binary_architecture = temp_arch_info->arch;
3159 bfd_external_machine = temp_arch_info->mach;
3161 else
3162 fatal (_("architecture %s unknown"), binary_architecture);
3164 else
3166 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3167 non_fatal (_(" Argument %s ignored"), binary_architecture);
3171 if (preserve_dates)
3172 if (stat (input_filename, & statbuf) < 0)
3173 fatal (_("warning: could not locate '%s'. System error message: %s"),
3174 input_filename, strerror (errno));
3176 /* If there is no destination file, or the source and destination files
3177 are the same, then create a temp and rename the result into the input. */
3178 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3180 char *tmpname = make_tempname (input_filename);
3182 copy_file (input_filename, tmpname, input_target, output_target);
3183 if (status == 0)
3185 if (preserve_dates)
3186 set_times (tmpname, &statbuf);
3187 smart_rename (tmpname, input_filename, preserve_dates);
3189 else
3190 unlink (tmpname);
3192 else
3194 copy_file (input_filename, output_filename, input_target, output_target);
3196 if (status == 0 && preserve_dates)
3197 set_times (output_filename, &statbuf);
3198 else if (status != 0)
3199 unlink_if_ordinary (output_filename);
3202 if (change_warn)
3204 for (p = change_sections; p != NULL; p = p->next)
3206 if (! p->used)
3208 if (p->change_vma != CHANGE_IGNORE)
3210 char buff [20];
3212 sprintf_vma (buff, p->vma_val);
3214 /* xgettext:c-format */
3215 non_fatal (_("%s %s%c0x%s never used"),
3216 "--change-section-vma",
3217 p->name,
3218 p->change_vma == CHANGE_SET ? '=' : '+',
3219 buff);
3222 if (p->change_lma != CHANGE_IGNORE)
3224 char buff [20];
3226 sprintf_vma (buff, p->lma_val);
3228 /* xgettext:c-format */
3229 non_fatal (_("%s %s%c0x%s never used"),
3230 "--change-section-lma",
3231 p->name,
3232 p->change_lma == CHANGE_SET ? '=' : '+',
3233 buff);
3239 return 0;
3243 main (int argc, char *argv[])
3245 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3246 setlocale (LC_MESSAGES, "");
3247 #endif
3248 #if defined (HAVE_SETLOCALE)
3249 setlocale (LC_CTYPE, "");
3250 #endif
3251 bindtextdomain (PACKAGE, LOCALEDIR);
3252 textdomain (PACKAGE);
3254 program_name = argv[0];
3255 xmalloc_set_program_name (program_name);
3257 START_PROGRESS (program_name, 0);
3259 expandargv (&argc, &argv);
3261 strip_symbols = STRIP_UNDEF;
3262 discard_locals = LOCALS_UNDEF;
3264 bfd_init ();
3265 set_default_bfd_target ();
3267 if (is_strip < 0)
3269 int i = strlen (program_name);
3270 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3271 /* Drop the .exe suffix, if any. */
3272 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3274 i -= 4;
3275 program_name[i] = '\0';
3277 #endif
3278 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3281 if (is_strip)
3282 strip_main (argc, argv);
3283 else
3284 copy_main (argc, argv);
3286 END_PROGRESS (program_name);
3288 return status;