1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "libiberty.h"
29 #include "filenames.h"
32 /* A list of symbols to explicitly strip out, or to keep. A linked
33 list is good enough for a small number from the command line, but
34 this will slow things down a lot if many symbols are being
43 /* A list to support redefine_sym. */
48 struct redefine_node
*next
;
51 typedef struct section_rename
53 const char * old_name
;
54 const char * new_name
;
56 struct section_rename
* next
;
60 /* List of sections to be renamed. */
61 static section_rename
*section_rename_list
;
63 static void copy_usage
65 static void strip_usage
67 static flagword parse_flags
69 static struct section_list
*find_section_list
70 (const char *, bfd_boolean
);
71 static void setup_section
72 (bfd
*, asection
*, void *);
73 static void copy_section
74 (bfd
*, asection
*, void *);
75 static void get_sections
76 (bfd
*, asection
*, void *);
77 static int compare_section_lma
78 (const void *, const void *);
79 static void add_specific_symbol
80 (const char *, struct symlist
**);
81 static void add_specific_symbols
82 (const char *, struct symlist
**);
83 static bfd_boolean is_specified_symbol
84 (const char *, struct symlist
*);
85 static bfd_boolean is_strip_section
87 static unsigned int filter_symbols
88 (bfd
*, bfd
*, asymbol
**, asymbol
**, long);
89 static void mark_symbols_used_in_relocations
90 (bfd
*, asection
*, void *);
91 static void filter_bytes
92 (char *, bfd_size_type
*);
93 static bfd_boolean write_debugging_info
94 (bfd
*, void *, long *, asymbol
***);
95 static void copy_object
97 static void copy_archive
98 (bfd
*, bfd
*, const char *);
100 (const char *, const char *, const char *, const char *);
101 static int strip_main
105 static const char *lookup_sym_redefinition
107 static void redefine_list_append
108 (const char *, const char *, const char *);
109 static const char * find_section_rename
110 (bfd
*, sec_ptr
, flagword
*);
111 static void add_section_rename
112 (const char *, const char *, flagword
);
113 static void add_redefine_syms_file
116 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
118 static asymbol
**isympp
= NULL
; /* Input symbols. */
119 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
121 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
122 static int copy_byte
= -1;
123 static int interleave
= 4;
125 static bfd_boolean verbose
; /* Print file and target names. */
126 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
127 static int status
= 0; /* Exit status. */
132 STRIP_NONE
, /* Don't strip. */
133 STRIP_DEBUG
, /* Strip all debugger symbols. */
134 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
135 STRIP_NONDEBUG
, /* Strip everything but debug info. */
136 STRIP_ALL
/* Strip all symbols. */
139 /* Which symbols to remove. */
140 static enum strip_action strip_symbols
;
145 LOCALS_START_L
, /* Discard locals starting with L. */
146 LOCALS_ALL
/* Discard all locals. */
149 /* Which local symbols to remove. Overrides STRIP_ALL. */
150 static enum locals_action discard_locals
;
152 /* What kind of change to perform. */
160 /* Structure used to hold lists of sections and actions to take. */
163 struct section_list
* next
; /* Next section to change. */
164 const char * name
; /* Section name. */
165 bfd_boolean used
; /* Whether this entry was used. */
166 bfd_boolean remove
; /* Whether to remove this section. */
167 bfd_boolean copy
; /* Whether to copy this section. */
168 enum change_action change_vma
;/* Whether to change or set VMA. */
169 bfd_vma vma_val
; /* Amount to change by or set to. */
170 enum change_action change_lma
;/* Whether to change or set LMA. */
171 bfd_vma lma_val
; /* Amount to change by or set to. */
172 bfd_boolean set_flags
; /* Whether to set the section flags. */
173 flagword flags
; /* What to set the section flags to. */
176 static struct section_list
*change_sections
;
178 /* TRUE if some sections are to be removed. */
179 static bfd_boolean sections_removed
;
181 /* TRUE if only some sections are to be copied. */
182 static bfd_boolean sections_copied
;
184 /* Changes to the start address. */
185 static bfd_vma change_start
= 0;
186 static bfd_boolean set_start_set
= FALSE
;
187 static bfd_vma set_start
;
189 /* Changes to section addresses. */
190 static bfd_vma change_section_address
= 0;
192 /* Filling gaps between sections. */
193 static bfd_boolean gap_fill_set
= FALSE
;
194 static bfd_byte gap_fill
= 0;
196 /* Pad to a given address. */
197 static bfd_boolean pad_to_set
= FALSE
;
198 static bfd_vma pad_to
;
200 /* Use alternate machine code? */
201 static int use_alt_mach_code
= 0;
203 /* List of sections to add. */
206 /* Next section to add. */
207 struct section_add
*next
;
208 /* Name of section to add. */
210 /* Name of file holding section contents. */
211 const char *filename
;
214 /* Contents of file. */
216 /* BFD section, after it has been added. */
220 /* List of sections to add to the output BFD. */
221 static struct section_add
*add_sections
;
223 /* If non-NULL the argument to --add-gnu-debuglink.
224 This should be the filename to store in the .gnu_debuglink section. */
225 static const char * gnu_debuglink_filename
= NULL
;
227 /* Whether to convert debugging information. */
228 static bfd_boolean convert_debugging
= FALSE
;
230 /* Whether to change the leading character in symbol names. */
231 static bfd_boolean change_leading_char
= FALSE
;
233 /* Whether to remove the leading character from global symbol names. */
234 static bfd_boolean remove_leading_char
= FALSE
;
236 /* List of symbols to strip, keep, localize, keep-global, weaken,
238 static struct symlist
*strip_specific_list
= NULL
;
239 static struct symlist
*keep_specific_list
= NULL
;
240 static struct symlist
*localize_specific_list
= NULL
;
241 static struct symlist
*keepglobal_specific_list
= NULL
;
242 static struct symlist
*weaken_specific_list
= NULL
;
243 static struct redefine_node
*redefine_sym_list
= NULL
;
245 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
246 static bfd_boolean weaken
= FALSE
;
248 /* Prefix symbols/sections. */
249 static char *prefix_symbols_string
= 0;
250 static char *prefix_sections_string
= 0;
251 static char *prefix_alloc_sections_string
= 0;
253 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
254 enum command_line_switch
256 OPTION_ADD_SECTION
=150,
257 OPTION_CHANGE_ADDRESSES
,
258 OPTION_CHANGE_LEADING_CHAR
,
260 OPTION_CHANGE_SECTION_ADDRESS
,
261 OPTION_CHANGE_SECTION_LMA
,
262 OPTION_CHANGE_SECTION_VMA
,
263 OPTION_CHANGE_WARNINGS
,
266 OPTION_NO_CHANGE_WARNINGS
,
268 OPTION_REMOVE_LEADING_CHAR
,
269 OPTION_SET_SECTION_FLAGS
,
271 OPTION_STRIP_UNNEEDED
,
274 OPTION_REDEFINE_SYMS
,
277 OPTION_STRIP_SYMBOLS
,
279 OPTION_LOCALIZE_SYMBOLS
,
280 OPTION_KEEPGLOBAL_SYMBOLS
,
281 OPTION_WEAKEN_SYMBOLS
,
282 OPTION_RENAME_SECTION
,
283 OPTION_ALT_MACH_CODE
,
284 OPTION_PREFIX_SYMBOLS
,
285 OPTION_PREFIX_SECTIONS
,
286 OPTION_PREFIX_ALLOC_SECTIONS
,
288 OPTION_ADD_GNU_DEBUGLINK
,
289 OPTION_ONLY_KEEP_DEBUG
292 /* Options to handle if running as "strip". */
294 static struct option strip_options
[] =
296 {"discard-all", no_argument
, 0, 'x'},
297 {"discard-locals", no_argument
, 0, 'X'},
298 {"format", required_argument
, 0, 'F'}, /* Obsolete */
299 {"help", no_argument
, 0, 'h'},
300 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
301 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
302 {"input-target", required_argument
, 0, 'I'},
303 {"keep-symbol", required_argument
, 0, 'K'},
304 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
305 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
306 {"output-target", required_argument
, 0, 'O'},
307 {"output-file", required_argument
, 0, 'o'},
308 {"preserve-dates", no_argument
, 0, 'p'},
309 {"remove-section", required_argument
, 0, 'R'},
310 {"strip-all", no_argument
, 0, 's'},
311 {"strip-debug", no_argument
, 0, 'S'},
312 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
313 {"strip-symbol", required_argument
, 0, 'N'},
314 {"target", required_argument
, 0, 'F'},
315 {"verbose", no_argument
, 0, 'v'},
316 {"version", no_argument
, 0, 'V'},
317 {0, no_argument
, 0, 0}
320 /* Options to handle if running as "objcopy". */
322 static struct option copy_options
[] =
324 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
325 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
326 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
327 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
328 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
329 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
330 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
331 {"binary-architecture", required_argument
, 0, 'B'},
332 {"byte", required_argument
, 0, 'b'},
333 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
334 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
335 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
336 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
337 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
338 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
339 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
340 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
341 {"discard-all", no_argument
, 0, 'x'},
342 {"discard-locals", no_argument
, 0, 'X'},
343 {"format", required_argument
, 0, 'F'}, /* Obsolete */
344 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
345 {"help", no_argument
, 0, 'h'},
346 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
347 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
348 {"input-target", required_argument
, 0, 'I'},
349 {"interleave", required_argument
, 0, 'i'},
350 {"keep-global-symbol", required_argument
, 0, 'G'},
351 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
352 {"keep-symbol", required_argument
, 0, 'K'},
353 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
354 {"localize-symbol", required_argument
, 0, 'L'},
355 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
356 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
357 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
358 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
359 {"only-section", required_argument
, 0, 'j'},
360 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
361 {"output-target", required_argument
, 0, 'O'},
362 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
363 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
364 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
365 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
366 {"preserve-dates", no_argument
, 0, 'p'},
367 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
368 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
369 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
370 {"remove-section", required_argument
, 0, 'R'},
371 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
372 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
373 {"set-start", required_argument
, 0, OPTION_SET_START
},
374 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
375 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
376 {"strip-all", no_argument
, 0, 'S'},
377 {"strip-debug", no_argument
, 0, 'g'},
378 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
379 {"strip-symbol", required_argument
, 0, 'N'},
380 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
381 {"target", required_argument
, 0, 'F'},
382 {"verbose", no_argument
, 0, 'v'},
383 {"version", no_argument
, 0, 'V'},
384 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
385 {"weaken-symbol", required_argument
, 0, 'W'},
386 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
387 {0, no_argument
, 0, 0}
391 extern char *program_name
;
393 /* This flag distinguishes between strip and objcopy:
394 1 means this is 'strip'; 0 means this is 'objcopy'.
395 -1 means if we should use argv[0] to decide. */
398 /* The maximum length of an S record. This variable is declared in srec.c
399 and can be modified by the --srec-len parameter. */
400 extern unsigned int Chunk
;
402 /* Restrict the generation of Srecords to type S3 only.
403 This variable is declare in bfd/srec.c and can be toggled
404 on by the --srec-forceS3 command line switch. */
405 extern bfd_boolean S3Forced
;
407 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
408 extern enum bfd_architecture bfd_external_binary_architecture
;
412 copy_usage (FILE *stream
, int exit_status
)
414 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
415 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
416 fprintf (stream
, _(" The options are:\n"));
417 fprintf (stream
, _("\
418 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
419 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
420 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
421 -F --target <bfdname> Set both input and output format to <bfdname>\n\
422 --debugging Convert debugging information, if possible\n\
423 -p --preserve-dates Copy modified/access timestamps to the output\n\
424 -j --only-section <name> Only copy section <name> into the output\n\
425 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
426 -R --remove-section <name> Remove section <name> from the output\n\
427 -S --strip-all Remove all symbol and relocation information\n\
428 -g --strip-debug Remove all debugging symbols & sections\n\
429 --strip-unneeded Remove all symbols not needed by relocations\n\
430 -N --strip-symbol <name> Do not copy symbol <name>\n\
431 -K --keep-symbol <name> Only copy symbol <name>\n\
432 -L --localize-symbol <name> Force symbol <name> to be marked as a local\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 -x --discard-all Remove all non-global symbols\n\
437 -X --discard-locals Remove any compiler-generated symbols\n\
438 -i --interleave <number> Only copy one out of every <number> bytes\n\
439 -b --byte <num> Select byte <num> in every interleaved block\n\
440 --gap-fill <val> Fill gaps between sections with <val>\n\
441 --pad-to <addr> Pad the last section up to address <addr>\n\
442 --set-start <addr> Set the start address to <addr>\n\
443 {--change-start|--adjust-start} <incr>\n\
444 Add <incr> to the start address\n\
445 {--change-addresses|--adjust-vma} <incr>\n\
446 Add <incr> to LMA, VMA and start addresses\n\
447 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
448 Change LMA and VMA of section <name> by <val>\n\
449 --change-section-lma <name>{=|+|-}<val>\n\
450 Change the LMA of section <name> by <val>\n\
451 --change-section-vma <name>{=|+|-}<val>\n\
452 Change the VMA of section <name> by <val>\n\
453 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
454 Warn if a named section does not exist\n\
455 --set-section-flags <name>=<flags>\n\
456 Set section <name>'s properties to <flags>\n\
457 --add-section <name>=<file> Add section <name> found in <file> to output\n\
458 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
459 --change-leading-char Force output format's leading character style\n\
460 --remove-leading-char Remove leading character from global symbols\n\
461 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
462 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
464 --srec-len <number> Restrict the length of generated Srecords\n\
465 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
466 --strip-symbols <file> -N for all symbols listed in <file>\n\
467 --keep-symbols <file> -K for all symbols listed in <file>\n\
468 --localize-symbols <file> -L for all symbols listed in <file>\n\
469 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
470 --weaken-symbols <file> -W for all symbols listed in <file>\n\
471 --alt-machine-code <index> Use alternate machine code for output\n\
472 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
473 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
474 --prefix-alloc-sections <prefix>\n\
475 Add <prefix> to start of every allocatable\n\
477 -v --verbose List all object files modified\n\
478 -V --version Display this program's version number\n\
479 -h --help Display this output\n\
480 --info List object formats & architectures supported\n\
482 list_supported_targets (program_name
, stream
);
483 if (exit_status
== 0)
484 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
489 strip_usage (FILE *stream
, int exit_status
)
491 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
492 fprintf (stream
, _(" Removes symbols and sections from files\n"));
493 fprintf (stream
, _(" The options are:\n"));
494 fprintf (stream
, _("\
495 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
496 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
497 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
498 -p --preserve-dates Copy modified/access timestamps to the output\n\
499 -R --remove-section=<name> Remove section <name> from the output\n\
500 -s --strip-all Remove all symbol and relocation information\n\
501 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
502 --strip-unneeded Remove all symbols not needed by relocations\n\
503 -N --strip-symbol=<name> Do not copy symbol <name>\n\
504 -K --keep-symbol=<name> Only copy symbol <name>\n\
505 -x --discard-all Remove all non-global symbols\n\
506 -X --discard-locals Remove any compiler-generated symbols\n\
507 -v --verbose List all object files modified\n\
508 -V --version Display this program's version number\n\
509 -h --help Display this output\n\
510 --info List object formats & architectures supported\n\
511 -o <file> Place stripped output into <file>\n\
514 list_supported_targets (program_name
, stream
);
515 if (exit_status
== 0)
516 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
520 /* Parse section flags into a flagword, with a fatal error if the
521 string can't be parsed. */
524 parse_flags (const char *s
)
534 snext
= strchr (s
, ',');
544 #define PARSE_FLAG(fname,fval) \
545 else if (strncasecmp (fname, s, len) == 0) ret |= fval
546 PARSE_FLAG ("alloc", SEC_ALLOC
);
547 PARSE_FLAG ("load", SEC_LOAD
);
548 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
549 PARSE_FLAG ("readonly", SEC_READONLY
);
550 PARSE_FLAG ("debug", SEC_DEBUGGING
);
551 PARSE_FLAG ("code", SEC_CODE
);
552 PARSE_FLAG ("data", SEC_DATA
);
553 PARSE_FLAG ("rom", SEC_ROM
);
554 PARSE_FLAG ("share", SEC_SHARED
);
555 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
561 copy
= xmalloc (len
+ 1);
562 strncpy (copy
, s
, len
);
564 non_fatal (_("unrecognized section flag `%s'"), copy
);
565 fatal (_("supported flags: %s"),
566 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
576 /* Find and optionally add an entry in the change_sections list. */
578 static struct section_list
*
579 find_section_list (const char *name
, bfd_boolean add
)
581 struct section_list
*p
;
583 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
584 if (strcmp (p
->name
, name
) == 0)
590 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
595 p
->change_vma
= CHANGE_IGNORE
;
596 p
->change_lma
= CHANGE_IGNORE
;
599 p
->set_flags
= FALSE
;
602 p
->next
= change_sections
;
608 /* Add a symbol to strip_specific_list. */
611 add_specific_symbol (const char *name
, struct symlist
**list
)
613 struct symlist
*tmp_list
;
615 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
616 tmp_list
->name
= name
;
617 tmp_list
->next
= *list
;
621 /* Add symbols listed in `filename' to strip_specific_list. */
623 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
624 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
627 add_specific_symbols (const char *filename
, struct symlist
**list
)
633 unsigned int line_count
;
635 if (stat (filename
, & st
) < 0)
636 fatal (_("cannot stat: %s: %s"), filename
, strerror (errno
));
640 buffer
= (char *) xmalloc (st
.st_size
+ 2);
641 f
= fopen (filename
, FOPEN_RT
);
643 fatal (_("cannot open: %s: %s"), filename
, strerror (errno
));
645 if (fread (buffer
, 1, st
.st_size
, f
) == 0 || ferror (f
))
646 fatal (_("%s: fread failed"), filename
);
649 buffer
[st
.st_size
] = '\n';
650 buffer
[st
.st_size
+ 1] = '\0';
654 for (line
= buffer
; * line
!= '\0'; line
++)
659 int finished
= FALSE
;
661 for (eol
= line
;; eol
++)
667 /* Cope with \n\r. */
675 /* Cope with \r\n. */
686 /* Line comment, Terminate the line here, in case a
687 name is present and then allow the rest of the
688 loop to find the real end of the line. */
700 /* A name may now exist somewhere between 'line' and 'eol'.
701 Strip off leading whitespace and trailing whitespace,
702 then add it to the list. */
703 for (name
= line
; IS_WHITESPACE (* name
); name
++)
705 for (name_end
= name
;
706 (! IS_WHITESPACE (* name_end
))
707 && (! IS_LINE_TERMINATOR (* name_end
));
711 if (! IS_LINE_TERMINATOR (* name_end
))
715 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
718 if (! IS_LINE_TERMINATOR (* extra
))
719 non_fatal (_("Ignoring rubbish found on line %d of %s"),
720 line_count
, filename
);
726 add_specific_symbol (name
, list
);
728 /* Advance line pointer to end of line. The 'eol ++' in the for
729 loop above will then advance us to the start of the next line. */
735 /* See whether a symbol should be stripped or kept based on
736 strip_specific_list and keep_symbols. */
739 is_specified_symbol (const char *name
, struct symlist
*list
)
741 struct symlist
*tmp_list
;
743 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
744 if (strcmp (name
, tmp_list
->name
) == 0)
750 /* See if a section is being removed. */
753 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
755 if (sections_removed
|| sections_copied
)
757 struct section_list
*p
;
759 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
761 if (sections_removed
&& p
!= NULL
&& p
->remove
)
763 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
767 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
769 if (strip_symbols
== STRIP_DEBUG
770 || strip_symbols
== STRIP_UNNEEDED
771 || strip_symbols
== STRIP_ALL
772 || discard_locals
== LOCALS_ALL
773 || convert_debugging
)
776 if (strip_symbols
== STRIP_NONDEBUG
)
780 return strip_symbols
== STRIP_NONDEBUG
? TRUE
: FALSE
;
783 /* Choose which symbol entries to copy; put the result in OSYMS.
784 We don't copy in place, because that confuses the relocs.
785 Return the number of symbols to print. */
788 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
789 asymbol
**isyms
, long symcount
)
791 asymbol
**from
= isyms
, **to
= osyms
;
792 long src_count
= 0, dst_count
= 0;
793 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
796 for (; src_count
< symcount
; src_count
++)
798 asymbol
*sym
= from
[src_count
];
799 flagword flags
= sym
->flags
;
800 char *name
= (char *) bfd_asymbol_name (sym
);
802 bfd_boolean undefined
;
803 bfd_boolean rem_leading_char
;
804 bfd_boolean add_leading_char
;
806 undefined
= bfd_is_und_section (bfd_get_section (sym
));
808 if (redefine_sym_list
)
810 char *old_name
, *new_name
;
812 old_name
= (char *) bfd_asymbol_name (sym
);
813 new_name
= (char *) lookup_sym_redefinition (old_name
);
814 bfd_asymbol_name (sym
) = new_name
;
818 /* Check if we will remove the current leading character. */
820 (name
[0] == bfd_get_symbol_leading_char (abfd
))
821 && (change_leading_char
822 || (remove_leading_char
823 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
825 || bfd_is_com_section (bfd_get_section (sym
)))));
827 /* Check if we will add a new leading character. */
830 && (bfd_get_symbol_leading_char (obfd
) != '\0')
831 && (bfd_get_symbol_leading_char (abfd
) == '\0'
832 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
834 /* Short circuit for change_leading_char if we can do it in-place. */
835 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
837 name
[0] = bfd_get_symbol_leading_char (obfd
);
838 bfd_asymbol_name (sym
) = name
;
839 rem_leading_char
= FALSE
;
840 add_leading_char
= FALSE
;
843 /* Remove leading char. */
844 if (rem_leading_char
)
845 bfd_asymbol_name (sym
) = ++name
;
847 /* Add new leading char and/or prefix. */
848 if (add_leading_char
|| prefix_symbols_string
)
852 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
853 + strlen (name
) + 1);
854 if (add_leading_char
)
855 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
857 if (prefix_symbols_string
)
859 strcpy (ptr
, prefix_symbols_string
);
860 ptr
+= strlen (prefix_symbols_string
);
864 bfd_asymbol_name (sym
) = n
;
868 if (strip_symbols
== STRIP_ALL
)
870 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
871 || ((flags
& BSF_SECTION_SYM
) != 0
872 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
875 else if (relocatable
/* Relocatable file. */
876 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
878 else if (bfd_decode_symclass (sym
) == 'I')
879 /* Global symbols in $idata sections need to be retained
880 even if relocatable is FALSE. External users of the
881 library containing the $idata section may reference these
884 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
885 || (flags
& BSF_WEAK
) != 0
887 || bfd_is_com_section (bfd_get_section (sym
)))
888 keep
= strip_symbols
!= STRIP_UNNEEDED
;
889 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
890 keep
= (strip_symbols
!= STRIP_DEBUG
891 && strip_symbols
!= STRIP_UNNEEDED
892 && ! convert_debugging
);
893 else if (bfd_get_section (sym
)->comdat
)
894 /* COMDAT sections store special information in local
895 symbols, so we cannot risk stripping any of them. */
897 else /* Local symbol. */
898 keep
= (strip_symbols
!= STRIP_UNNEEDED
899 && (discard_locals
!= LOCALS_ALL
900 && (discard_locals
!= LOCALS_START_L
901 || ! bfd_is_local_label (abfd
, sym
))));
903 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
905 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
907 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
910 if (keep
&& (flags
& BSF_GLOBAL
) != 0
911 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
913 sym
->flags
&=~ BSF_GLOBAL
;
914 sym
->flags
|= BSF_WEAK
;
916 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
917 && (is_specified_symbol (name
, localize_specific_list
)
918 || (keepglobal_specific_list
!= NULL
919 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
921 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
922 sym
->flags
|= BSF_LOCAL
;
926 to
[dst_count
++] = sym
;
929 to
[dst_count
] = NULL
;
934 /* Find the redefined name of symbol SOURCE. */
937 lookup_sym_redefinition (const char *source
)
939 struct redefine_node
*list
;
941 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
942 if (strcmp (source
, list
->source
) == 0)
948 /* Add a node to a symbol redefine list. */
951 redefine_list_append (const char *cause
, const char *source
, const char *target
)
953 struct redefine_node
**p
;
954 struct redefine_node
*list
;
955 struct redefine_node
*new_node
;
957 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
959 if (strcmp (source
, list
->source
) == 0)
960 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
963 if (strcmp (target
, list
->target
) == 0)
964 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
968 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
970 new_node
->source
= strdup (source
);
971 new_node
->target
= strdup (target
);
972 new_node
->next
= NULL
;
977 /* Handle the --redefine-syms option. Read lines containing "old new"
978 from the file, and add them to the symbol redefine list. */
981 add_redefine_syms_file (const char *filename
)
990 file
= fopen (filename
, "r");
991 if (file
== (FILE *) NULL
)
992 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
993 filename
, strerror (errno
));
996 buf
= (char *) xmalloc (bufsize
);
1004 /* Collect the input symbol name. */
1005 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1013 buf
= xrealloc (buf
, bufsize
);
1021 /* Eat white space between the symbol names. */
1022 while (IS_WHITESPACE (c
))
1024 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1029 /* Collect the output symbol name. */
1031 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1039 buf
= xrealloc (buf
, bufsize
);
1047 /* Eat white space at end of line. */
1048 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1053 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1054 || c
== '\n' || c
== EOF
)
1057 /* Append the redefinition to the list. */
1059 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1070 fatal (_("%s: garbage at end of line %d"), filename
, lineno
);
1072 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1073 fatal (_("%s: missing new symbol name at line %d"), filename
, lineno
);
1076 /* Eat the rest of the line and finish it. */
1077 while (c
!= '\n' && c
!= EOF
)
1083 fatal (_("%s: premature end of file at line %d"), filename
, lineno
);
1088 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
1092 filter_bytes (char *memhunk
, bfd_size_type
*size
)
1094 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
1096 for (; from
< end
; from
+= interleave
)
1099 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
1100 *size
= (*size
/ interleave
) + 1;
1102 *size
/= interleave
;
1105 /* Copy object file IBFD onto OBFD. */
1108 copy_object (bfd
*ibfd
, bfd
*obfd
)
1112 asection
**osections
= NULL
;
1113 asection
*gnu_debuglink_section
= NULL
;
1114 bfd_size_type
*gaps
= NULL
;
1115 bfd_size_type max_gap
= 0;
1118 enum bfd_architecture iarch
;
1121 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1122 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1123 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1125 fatal (_("Unable to change endianness of input file(s)"));
1129 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1130 RETURN_NONFATAL (bfd_get_filename (obfd
));
1133 printf (_("copy from %s(%s) to %s(%s)\n"),
1134 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
1135 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1140 start
= bfd_get_start_address (ibfd
);
1141 start
+= change_start
;
1143 /* Neither the start address nor the flags
1144 need to be set for a core file. */
1145 if (bfd_get_format (obfd
) != bfd_core
)
1147 if (!bfd_set_start_address (obfd
, start
)
1148 || !bfd_set_file_flags (obfd
,
1149 (bfd_get_file_flags (ibfd
)
1150 & bfd_applicable_file_flags (obfd
))))
1151 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1154 /* Copy architecture of input file to output file. */
1155 iarch
= bfd_get_arch (ibfd
);
1156 imach
= bfd_get_mach (ibfd
);
1157 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1158 && (ibfd
->target_defaulted
1159 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1160 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1161 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1162 bfd_get_mach (ibfd
)));
1164 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1165 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1170 if (osympp
!= isympp
)
1173 /* BFD mandates that all output sections be created and sizes set before
1174 any output is done. Thus, we traverse all sections multiple times. */
1175 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
1177 if (add_sections
!= NULL
)
1179 struct section_add
*padd
;
1180 struct section_list
*pset
;
1182 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1186 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1187 if (padd
->section
== NULL
)
1189 non_fatal (_("can't create section `%s': %s"),
1190 padd
->name
, bfd_errmsg (bfd_get_error ()));
1195 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1196 RETURN_NONFATAL (bfd_get_filename (obfd
));
1198 pset
= find_section_list (padd
->name
, FALSE
);
1202 if (pset
!= NULL
&& pset
->set_flags
)
1203 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1205 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1207 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1208 RETURN_NONFATAL (bfd_get_filename (obfd
));
1212 if (pset
->change_vma
!= CHANGE_IGNORE
)
1213 if (! bfd_set_section_vma (obfd
, padd
->section
,
1215 RETURN_NONFATAL (bfd_get_filename (obfd
));
1217 if (pset
->change_lma
!= CHANGE_IGNORE
)
1219 padd
->section
->lma
= pset
->lma_val
;
1221 if (! bfd_set_section_alignment
1222 (obfd
, padd
->section
,
1223 bfd_section_alignment (obfd
, padd
->section
)))
1224 RETURN_NONFATAL (bfd_get_filename (obfd
));
1230 if (gnu_debuglink_filename
!= NULL
)
1232 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1233 (obfd
, gnu_debuglink_filename
);
1235 if (gnu_debuglink_section
== NULL
)
1236 RETURN_NONFATAL (gnu_debuglink_filename
);
1239 if (gap_fill_set
|| pad_to_set
)
1244 /* We must fill in gaps between the sections and/or we must pad
1245 the last section to a specified address. We do this by
1246 grabbing a list of the sections, sorting them by VMA, and
1247 increasing the section sizes as required to fill the gaps.
1248 We write out the gap contents below. */
1250 c
= bfd_count_sections (obfd
);
1251 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
1253 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
1255 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1257 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
1258 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1262 for (i
= 0; i
< c
- 1; i
++)
1266 bfd_vma gap_start
, gap_stop
;
1268 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1269 if ((flags
& SEC_HAS_CONTENTS
) == 0
1270 || (flags
& SEC_LOAD
) == 0)
1273 size
= bfd_section_size (obfd
, osections
[i
]);
1274 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1275 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1276 if (gap_start
< gap_stop
)
1278 if (! bfd_set_section_size (obfd
, osections
[i
],
1279 size
+ (gap_stop
- gap_start
)))
1281 non_fatal (_("Can't fill gap after %s: %s"),
1282 bfd_get_section_name (obfd
, osections
[i
]),
1283 bfd_errmsg (bfd_get_error ()));
1287 gaps
[i
] = gap_stop
- gap_start
;
1288 if (max_gap
< gap_stop
- gap_start
)
1289 max_gap
= gap_stop
- gap_start
;
1299 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1300 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1301 if (lma
+ size
< pad_to
)
1303 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1306 non_fatal (_("Can't add padding to %s: %s"),
1307 bfd_get_section_name (obfd
, osections
[c
- 1]),
1308 bfd_errmsg (bfd_get_error ()));
1313 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1314 if (max_gap
< pad_to
- (lma
+ size
))
1315 max_gap
= pad_to
- (lma
+ size
);
1321 /* Symbol filtering must happen after the output sections
1322 have been created, but before their contents are set. */
1324 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1326 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1328 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1329 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1331 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1333 if (convert_debugging
)
1334 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1336 if (strip_symbols
== STRIP_DEBUG
1337 || strip_symbols
== STRIP_ALL
1338 || strip_symbols
== STRIP_UNNEEDED
1339 || strip_symbols
== STRIP_NONDEBUG
1340 || discard_locals
!= LOCALS_UNDEF
1341 || strip_specific_list
!= NULL
1342 || keep_specific_list
!= NULL
1343 || localize_specific_list
!= NULL
1344 || keepglobal_specific_list
!= NULL
1345 || weaken_specific_list
!= NULL
1346 || prefix_symbols_string
1349 || convert_debugging
1350 || change_leading_char
1351 || remove_leading_char
1352 || redefine_sym_list
1355 /* Mark symbols used in output relocations so that they
1356 are kept, even if they are local labels or static symbols.
1358 Note we iterate over the input sections examining their
1359 relocations since the relocations for the output sections
1360 haven't been set yet. mark_symbols_used_in_relocations will
1361 ignore input sections which have no corresponding output
1363 if (strip_symbols
!= STRIP_ALL
)
1364 bfd_map_over_sections (ibfd
,
1365 mark_symbols_used_in_relocations
,
1367 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1368 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1371 if (convert_debugging
&& dhandle
!= NULL
)
1373 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1380 bfd_set_symtab (obfd
, osympp
, symcount
);
1382 /* This has to happen after the symbol table has been set. */
1383 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
1385 if (add_sections
!= NULL
)
1387 struct section_add
*padd
;
1389 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1391 if (! bfd_set_section_contents (obfd
, padd
->section
,
1392 (void *) padd
->contents
,
1394 (bfd_size_type
) padd
->size
))
1395 RETURN_NONFATAL (bfd_get_filename (obfd
));
1399 if (gnu_debuglink_filename
!= NULL
)
1401 if (! bfd_fill_in_gnu_debuglink_section
1402 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1403 RETURN_NONFATAL (gnu_debuglink_filename
);
1406 if (gap_fill_set
|| pad_to_set
)
1411 /* Fill in the gaps. */
1414 buf
= (bfd_byte
*) xmalloc (max_gap
);
1415 memset (buf
, gap_fill
, (size_t) max_gap
);
1417 c
= bfd_count_sections (obfd
);
1418 for (i
= 0; i
< c
; i
++)
1426 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1437 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1439 RETURN_NONFATAL (bfd_get_filename (obfd
));
1448 /* Allow the BFD backend to copy any private data it understands
1449 from the input BFD to the output BFD. This is done last to
1450 permit the routine to look at the filtered symbol table, which is
1451 important for the ECOFF code at least. */
1452 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1453 && strip_symbols
== STRIP_NONDEBUG
)
1454 /* Do not copy the private data when creating an ELF format
1455 debug info file. We do not want the program headers. */
1457 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1459 non_fatal (_("%s: error copying private BFD data: %s"),
1460 bfd_get_filename (obfd
),
1461 bfd_errmsg (bfd_get_error ()));
1466 /* Switch to the alternate machine code. We have to do this at the
1467 very end, because we only initialize the header when we create
1468 the first section. */
1469 if (use_alt_mach_code
!= 0)
1471 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1472 non_fatal (_("unknown alternate machine code, ignored"));
1477 #if defined (_WIN32) && !defined (__CYGWIN32__)
1478 #define MKDIR(DIR, MODE) mkdir (DIR)
1480 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1483 /* Read each archive element in turn from IBFD, copy the
1484 contents to temp file, and keep the temp file handle. */
1487 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1491 struct name_list
*next
;
1495 bfd
**ptr
= &obfd
->archive_head
;
1497 char *dir
= make_tempname (bfd_get_filename (obfd
));
1499 /* Make a temp directory to hold the contents. */
1500 if (MKDIR (dir
, 0700) != 0)
1501 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1502 dir
, strerror (errno
));
1504 obfd
->has_armap
= ibfd
->has_armap
;
1508 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1510 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1511 RETURN_NONFATAL (bfd_get_filename (obfd
));
1513 while (!status
&& this_element
!= (bfd
*) NULL
)
1519 int stat_status
= 0;
1521 /* Create an output file for this member. */
1522 output_name
= concat (dir
, "/",
1523 bfd_get_filename (this_element
), (char *) 0);
1525 /* If the file already exists, make another temp dir. */
1526 if (stat (output_name
, &buf
) >= 0)
1528 output_name
= make_tempname (output_name
);
1529 if (MKDIR (output_name
, 0700) != 0)
1530 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1531 output_name
, strerror (errno
));
1533 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1534 l
->name
= output_name
;
1538 output_name
= concat (output_name
, "/",
1539 bfd_get_filename (this_element
), (char *) 0);
1542 output_bfd
= bfd_openw (output_name
, output_target
);
1545 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1547 if (stat_status
!= 0)
1548 non_fatal (_("internal stat error on %s"),
1549 bfd_get_filename (this_element
));
1552 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1553 l
->name
= output_name
;
1557 if (output_bfd
== (bfd
*) NULL
)
1558 RETURN_NONFATAL (output_name
);
1560 if (bfd_check_format (this_element
, bfd_object
))
1561 copy_object (this_element
, output_bfd
);
1563 if (!bfd_close (output_bfd
))
1565 bfd_nonfatal (bfd_get_filename (output_bfd
));
1566 /* Error in new object file. Don't change archive. */
1570 if (preserve_dates
&& stat_status
== 0)
1571 set_times (output_name
, &buf
);
1573 /* Open the newly output file and attach to our list. */
1574 output_bfd
= bfd_openr (output_name
, output_target
);
1576 l
->obfd
= output_bfd
;
1579 ptr
= &output_bfd
->next
;
1581 last_element
= this_element
;
1583 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1585 bfd_close (last_element
);
1587 *ptr
= (bfd
*) NULL
;
1589 if (!bfd_close (obfd
))
1590 RETURN_NONFATAL (bfd_get_filename (obfd
));
1592 if (!bfd_close (ibfd
))
1593 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1595 /* Delete all the files that we opened. */
1596 for (l
= list
; l
!= NULL
; l
= l
->next
)
1598 if (l
->obfd
== NULL
)
1602 bfd_close (l
->obfd
);
1609 /* The top-level control. */
1612 copy_file (const char *input_filename
, const char *output_filename
,
1613 const char *input_target
, const char *output_target
)
1616 char **obj_matching
;
1617 char **core_matching
;
1619 /* To allow us to do "strip *" without dying on the first
1620 non-object file, failures are nonfatal. */
1621 ibfd
= bfd_openr (input_filename
, input_target
);
1623 RETURN_NONFATAL (input_filename
);
1625 if (bfd_check_format (ibfd
, bfd_archive
))
1629 /* bfd_get_target does not return the correct value until
1630 bfd_check_format succeeds. */
1631 if (output_target
== NULL
)
1632 output_target
= bfd_get_target (ibfd
);
1634 obfd
= bfd_openw (output_filename
, output_target
);
1636 RETURN_NONFATAL (output_filename
);
1638 copy_archive (ibfd
, obfd
, output_target
);
1640 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1644 /* bfd_get_target does not return the correct value until
1645 bfd_check_format succeeds. */
1646 if (output_target
== NULL
)
1647 output_target
= bfd_get_target (ibfd
);
1649 obfd
= bfd_openw (output_filename
, output_target
);
1651 RETURN_NONFATAL (output_filename
);
1653 copy_object (ibfd
, obfd
);
1655 if (!bfd_close (obfd
))
1656 RETURN_NONFATAL (output_filename
);
1658 if (!bfd_close (ibfd
))
1659 RETURN_NONFATAL (input_filename
);
1663 bfd_error_type obj_error
= bfd_get_error ();
1664 bfd_error_type core_error
;
1666 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1668 /* This probably can't happen.. */
1669 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1670 free (obj_matching
);
1674 core_error
= bfd_get_error ();
1675 /* Report the object error in preference to the core error. */
1676 if (obj_error
!= core_error
)
1677 bfd_set_error (obj_error
);
1679 bfd_nonfatal (input_filename
);
1681 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1683 list_matching_formats (obj_matching
);
1684 free (obj_matching
);
1686 if (core_error
== bfd_error_file_ambiguously_recognized
)
1688 list_matching_formats (core_matching
);
1689 free (core_matching
);
1696 /* Add a name to the section renaming list. */
1699 add_section_rename (const char * old_name
, const char * new_name
,
1702 section_rename
* rename
;
1704 /* Check for conflicts first. */
1705 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1706 if (strcmp (rename
->old_name
, old_name
) == 0)
1708 /* Silently ignore duplicate definitions. */
1709 if (strcmp (rename
->new_name
, new_name
) == 0
1710 && rename
->flags
== flags
)
1713 fatal (_("Multiple renames of section %s"), old_name
);
1716 rename
= (section_rename
*) xmalloc (sizeof (* rename
));
1718 rename
->old_name
= old_name
;
1719 rename
->new_name
= new_name
;
1720 rename
->flags
= flags
;
1721 rename
->next
= section_rename_list
;
1723 section_rename_list
= rename
;
1726 /* Check the section rename list for a new name of the input section
1727 ISECTION. Return the new name if one is found.
1728 Also set RETURNED_FLAGS to the flags to be used for this section. */
1731 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
1732 flagword
* returned_flags
)
1734 const char * old_name
= bfd_section_name (ibfd
, isection
);
1735 section_rename
* rename
;
1737 /* Default to using the flags of the input section. */
1738 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1740 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1741 if (strcmp (rename
->old_name
, old_name
) == 0)
1743 if (rename
->flags
!= (flagword
) -1)
1744 * returned_flags
= rename
->flags
;
1746 return rename
->new_name
;
1752 /* Create a section in OBFD with the same
1753 name and attributes as ISECTION in IBFD. */
1756 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1758 bfd
*obfd
= (bfd
*) obfdarg
;
1759 struct section_list
*p
;
1767 char *prefix
= NULL
;
1769 if (is_strip_section (ibfd
, isection
))
1772 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1776 /* Get the, possibly new, name of the output section. */
1777 name
= find_section_rename (ibfd
, isection
, & flags
);
1779 /* Prefix sections. */
1780 if ((prefix_alloc_sections_string
)
1781 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1782 prefix
= prefix_alloc_sections_string
;
1783 else if (prefix_sections_string
)
1784 prefix
= prefix_sections_string
;
1790 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1796 osection
= bfd_make_section_anyway (obfd
, name
);
1798 if (osection
== NULL
)
1804 size
= bfd_section_size (ibfd
, isection
);
1806 size
= (size
+ interleave
- 1) / interleave
;
1807 if (! bfd_set_section_size (obfd
, osection
, size
))
1813 vma
= bfd_section_vma (ibfd
, isection
);
1814 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1816 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1819 vma
+= change_section_address
;
1821 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1827 lma
= isection
->lma
;
1828 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1830 if (p
->change_lma
== CHANGE_MODIFY
)
1832 else if (p
->change_lma
== CHANGE_SET
)
1838 lma
+= change_section_address
;
1840 osection
->lma
= lma
;
1842 /* FIXME: This is probably not enough. If we change the LMA we
1843 may have to recompute the header for the file as well. */
1844 if (!bfd_set_section_alignment (obfd
,
1846 bfd_section_alignment (ibfd
, isection
)))
1848 err
= _("alignment");
1852 if (p
!= NULL
&& p
->set_flags
)
1853 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1854 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1860 /* Copy merge entity size. */
1861 osection
->entsize
= isection
->entsize
;
1863 /* This used to be mangle_section; we do here to avoid using
1864 bfd_get_section_by_name since some formats allow multiple
1865 sections with the same name. */
1866 isection
->output_section
= osection
;
1867 isection
->output_offset
= 0;
1869 /* Allow the BFD backend to copy any private data it understands
1870 from the input section to the output section. */
1871 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1872 && strip_symbols
== STRIP_NONDEBUG
)
1873 /* Do not copy the private data when creating an ELF format
1874 debug info file. We do not want the program headers. */
1876 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1878 err
= _("private data");
1882 /* All went well. */
1886 non_fatal (_("%s: section `%s': error in %s: %s"),
1887 bfd_get_filename (ibfd
),
1888 bfd_section_name (ibfd
, isection
),
1889 err
, bfd_errmsg (bfd_get_error ()));
1893 /* Copy the data of input section ISECTION of IBFD
1894 to an output section with the same name in OBFD.
1895 If stripping then don't copy any relocation info. */
1898 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1900 bfd
*obfd
= (bfd
*) obfdarg
;
1901 struct section_list
*p
;
1909 /* If we have already failed earlier on,
1910 do not keep on generating complaints now. */
1914 if (is_strip_section (ibfd
, isection
))
1917 flags
= bfd_get_section_flags (ibfd
, isection
);
1918 if ((flags
& SEC_GROUP
) != 0)
1921 osection
= isection
->output_section
;
1922 size
= bfd_get_section_size_before_reloc (isection
);
1924 if (size
== 0 || osection
== 0)
1927 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
1929 /* Core files do not need to be relocated. */
1930 if (bfd_get_format (obfd
) == bfd_core
)
1933 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1936 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1939 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1942 relpp
= (arelent
**) xmalloc (relsize
);
1943 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1945 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1947 if (strip_symbols
== STRIP_ALL
)
1949 /* Remove relocations which are not in
1950 keep_strip_specific_list. */
1951 arelent
**temp_relpp
;
1952 long temp_relcount
= 0;
1955 temp_relpp
= (arelent
**) xmalloc (relsize
);
1956 for (i
= 0; i
< relcount
; i
++)
1957 if (is_specified_symbol
1958 (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1959 keep_specific_list
))
1960 temp_relpp
[temp_relcount
++] = relpp
[i
];
1961 relcount
= temp_relcount
;
1966 bfd_set_reloc (obfd
, osection
,
1967 (relcount
== 0 ? (arelent
**) NULL
: relpp
), relcount
);
1970 isection
->_cooked_size
= isection
->_raw_size
;
1971 isection
->reloc_done
= TRUE
;
1973 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
1974 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
1976 void *memhunk
= (void *) xmalloc ((unsigned) size
);
1978 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1980 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1983 filter_bytes (memhunk
, &size
);
1985 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1987 RETURN_NONFATAL (bfd_get_filename (obfd
));
1991 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1993 void * memhunk
= (void *) xmalloc ((unsigned) size
);
1995 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1996 flag--they can just remove the section entirely and add it
1997 back again. However, we do permit them to turn on the
1998 SEC_HAS_CONTENTS flag, and take it to mean that the section
1999 contents should be zeroed out. */
2001 memset (memhunk
, 0, size
);
2002 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
2004 RETURN_NONFATAL (bfd_get_filename (obfd
));
2009 /* Get all the sections. This is used when --gap-fill or --pad-to is
2013 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2015 asection
***secppp
= (asection
***) secppparg
;
2017 **secppp
= osection
;
2021 /* Sort sections by VMA. This is called via qsort, and is used when
2022 --gap-fill or --pad-to is used. We force non loadable or empty
2023 sections to the front, where they are easier to ignore. */
2026 compare_section_lma (const void *arg1
, const void *arg2
)
2028 const asection
**sec1
= (const asection
**) arg1
;
2029 const asection
**sec2
= (const asection
**) arg2
;
2030 flagword flags1
, flags2
;
2032 /* Sort non loadable sections to the front. */
2033 flags1
= (*sec1
)->flags
;
2034 flags2
= (*sec2
)->flags
;
2035 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2036 || (flags1
& SEC_LOAD
) == 0)
2038 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2039 && (flags2
& SEC_LOAD
) != 0)
2044 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2045 || (flags2
& SEC_LOAD
) == 0)
2049 /* Sort sections by LMA. */
2050 if ((*sec1
)->lma
> (*sec2
)->lma
)
2052 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2055 /* Sort sections with the same LMA by size. */
2056 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
2058 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
2064 /* Mark all the symbols which will be used in output relocations with
2065 the BSF_KEEP flag so that those symbols will not be stripped.
2067 Ignore relocations which will not appear in the output file. */
2070 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2072 asymbol
**symbols
= (asymbol
**) symbolsarg
;
2077 /* Ignore an input section with no corresponding output section. */
2078 if (isection
->output_section
== NULL
)
2081 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2083 bfd_fatal (bfd_get_filename (ibfd
));
2088 relpp
= (arelent
**) xmalloc (relsize
);
2089 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2091 bfd_fatal (bfd_get_filename (ibfd
));
2093 /* Examine each symbol used in a relocation. If it's not one of the
2094 special bfd section symbols, then mark it with BSF_KEEP. */
2095 for (i
= 0; i
< relcount
; i
++)
2097 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2098 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2099 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2100 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2107 /* Write out debugging information. */
2110 write_debugging_info (bfd
*obfd
, void *dhandle
,
2111 long *symcountp ATTRIBUTE_UNUSED
,
2112 asymbol
***symppp ATTRIBUTE_UNUSED
)
2114 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2115 return write_ieee_debugging_info (obfd
, dhandle
);
2117 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2118 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2120 bfd_byte
*syms
, *strings
;
2121 bfd_size_type symsize
, stringsize
;
2122 asection
*stabsec
, *stabstrsec
;
2124 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2129 stabsec
= bfd_make_section (obfd
, ".stab");
2130 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2132 || stabstrsec
== NULL
2133 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2134 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2135 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2136 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2137 || ! bfd_set_section_flags (obfd
, stabsec
,
2141 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2146 non_fatal (_("%s: can't create debugging section: %s"),
2147 bfd_get_filename (obfd
),
2148 bfd_errmsg (bfd_get_error ()));
2152 /* We can get away with setting the section contents now because
2153 the next thing the caller is going to do is copy over the
2154 real sections. We may someday have to split the contents
2155 setting out of this function. */
2156 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
2158 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
2159 (file_ptr
) 0, stringsize
))
2161 non_fatal (_("%s: can't set debugging section contents: %s"),
2162 bfd_get_filename (obfd
),
2163 bfd_errmsg (bfd_get_error ()));
2170 non_fatal (_("%s: don't know how to write debugging information for %s"),
2171 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2176 strip_main (int argc
, char *argv
[])
2178 char *input_target
= NULL
;
2179 char *output_target
= NULL
;
2180 bfd_boolean show_version
= FALSE
;
2181 bfd_boolean formats_info
= FALSE
;
2184 struct section_list
*p
;
2185 char *output_file
= NULL
;
2187 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVv",
2188 strip_options
, (int *) 0)) != EOF
)
2193 input_target
= optarg
;
2196 output_target
= optarg
;
2199 input_target
= output_target
= optarg
;
2202 p
= find_section_list (optarg
, TRUE
);
2204 sections_removed
= TRUE
;
2207 strip_symbols
= STRIP_ALL
;
2211 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2212 strip_symbols
= STRIP_DEBUG
;
2214 case OPTION_STRIP_UNNEEDED
:
2215 strip_symbols
= STRIP_UNNEEDED
;
2218 add_specific_symbol (optarg
, &keep_specific_list
);
2221 add_specific_symbol (optarg
, &strip_specific_list
);
2224 output_file
= optarg
;
2227 preserve_dates
= TRUE
;
2230 discard_locals
= LOCALS_ALL
;
2233 discard_locals
= LOCALS_START_L
;
2239 show_version
= TRUE
;
2241 case OPTION_FORMATS_INFO
:
2242 formats_info
= TRUE
;
2244 case OPTION_ONLY_KEEP_DEBUG
:
2245 strip_symbols
= STRIP_NONDEBUG
;
2248 /* We've been given a long option. */
2252 strip_usage (stdout
, 0);
2254 strip_usage (stderr
, 1);
2265 print_version ("strip");
2267 /* Default is to strip all symbols. */
2268 if (strip_symbols
== STRIP_UNDEF
2269 && discard_locals
== LOCALS_UNDEF
2270 && strip_specific_list
== NULL
)
2271 strip_symbols
= STRIP_ALL
;
2273 if (output_target
== (char *) NULL
)
2274 output_target
= input_target
;
2278 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2279 strip_usage (stderr
, 1);
2281 for (; i
< argc
; i
++)
2283 int hold_status
= status
;
2284 struct stat statbuf
;
2289 if (stat (argv
[i
], &statbuf
) < 0)
2291 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
2296 if (output_file
!= NULL
)
2297 tmpname
= output_file
;
2299 tmpname
= make_tempname (argv
[i
]);
2302 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2306 set_times (tmpname
, &statbuf
);
2307 if (output_file
== NULL
)
2308 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2309 status
= hold_status
;
2313 if (output_file
== NULL
)
2321 copy_main (int argc
, char *argv
[])
2323 char * binary_architecture
= NULL
;
2324 char *input_filename
= NULL
;
2325 char *output_filename
= NULL
;
2326 char *input_target
= NULL
;
2327 char *output_target
= NULL
;
2328 bfd_boolean show_version
= FALSE
;
2329 bfd_boolean change_warn
= TRUE
;
2330 bfd_boolean formats_info
= FALSE
;
2332 struct section_list
*p
;
2333 struct stat statbuf
;
2335 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2336 copy_options
, (int *) 0)) != EOF
)
2341 copy_byte
= atoi (optarg
);
2343 fatal (_("byte number must be non-negative"));
2347 binary_architecture
= optarg
;
2351 interleave
= atoi (optarg
);
2353 fatal (_("interleave must be positive"));
2357 case 's': /* "source" - 'I' is preferred */
2358 input_target
= optarg
;
2362 case 'd': /* "destination" - 'O' is preferred */
2363 output_target
= optarg
;
2367 input_target
= output_target
= optarg
;
2371 p
= find_section_list (optarg
, TRUE
);
2373 fatal (_("%s both copied and removed"), optarg
);
2375 sections_copied
= TRUE
;
2379 p
= find_section_list (optarg
, TRUE
);
2381 fatal (_("%s both copied and removed"), optarg
);
2383 sections_removed
= TRUE
;
2387 strip_symbols
= STRIP_ALL
;
2391 strip_symbols
= STRIP_DEBUG
;
2394 case OPTION_STRIP_UNNEEDED
:
2395 strip_symbols
= STRIP_UNNEEDED
;
2398 case OPTION_ONLY_KEEP_DEBUG
:
2399 strip_symbols
= STRIP_NONDEBUG
;
2402 case OPTION_ADD_GNU_DEBUGLINK
:
2403 gnu_debuglink_filename
= optarg
;
2407 add_specific_symbol (optarg
, &keep_specific_list
);
2411 add_specific_symbol (optarg
, &strip_specific_list
);
2415 add_specific_symbol (optarg
, &localize_specific_list
);
2419 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2423 add_specific_symbol (optarg
, &weaken_specific_list
);
2427 preserve_dates
= TRUE
;
2431 discard_locals
= LOCALS_ALL
;
2435 discard_locals
= LOCALS_START_L
;
2443 show_version
= TRUE
;
2446 case OPTION_FORMATS_INFO
:
2447 formats_info
= TRUE
;
2454 case OPTION_ADD_SECTION
:
2458 struct section_add
*pa
;
2463 s
= strchr (optarg
, '=');
2466 fatal (_("bad format for %s"), "--add-section");
2468 if (stat (s
+ 1, & st
) < 0)
2469 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
2471 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
2474 name
= (char *) xmalloc (len
+ 1);
2475 strncpy (name
, optarg
, len
);
2479 pa
->filename
= s
+ 1;
2481 pa
->size
= st
.st_size
;
2483 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
2484 f
= fopen (pa
->filename
, FOPEN_RB
);
2487 fatal (_("cannot open: %s: %s"),
2488 pa
->filename
, strerror (errno
));
2490 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2492 fatal (_("%s: fread failed"), pa
->filename
);
2496 pa
->next
= add_sections
;
2501 case OPTION_CHANGE_START
:
2502 change_start
= parse_vma (optarg
, "--change-start");
2505 case OPTION_CHANGE_SECTION_ADDRESS
:
2506 case OPTION_CHANGE_SECTION_LMA
:
2507 case OPTION_CHANGE_SECTION_VMA
:
2512 char *option
= NULL
;
2514 enum change_action what
= CHANGE_IGNORE
;
2518 case OPTION_CHANGE_SECTION_ADDRESS
:
2519 option
= "--change-section-address";
2521 case OPTION_CHANGE_SECTION_LMA
:
2522 option
= "--change-section-lma";
2524 case OPTION_CHANGE_SECTION_VMA
:
2525 option
= "--change-section-vma";
2529 s
= strchr (optarg
, '=');
2532 s
= strchr (optarg
, '+');
2535 s
= strchr (optarg
, '-');
2537 fatal (_("bad format for %s"), option
);
2542 name
= (char *) xmalloc (len
+ 1);
2543 strncpy (name
, optarg
, len
);
2546 p
= find_section_list (name
, TRUE
);
2548 val
= parse_vma (s
+ 1, option
);
2552 case '=': what
= CHANGE_SET
; break;
2553 case '-': val
= - val
; /* Drop through. */
2554 case '+': what
= CHANGE_MODIFY
; break;
2559 case OPTION_CHANGE_SECTION_ADDRESS
:
2560 p
->change_vma
= what
;
2564 case OPTION_CHANGE_SECTION_LMA
:
2565 p
->change_lma
= what
;
2569 case OPTION_CHANGE_SECTION_VMA
:
2570 p
->change_vma
= what
;
2577 case OPTION_CHANGE_ADDRESSES
:
2578 change_section_address
= parse_vma (optarg
, "--change-addresses");
2579 change_start
= change_section_address
;
2582 case OPTION_CHANGE_WARNINGS
:
2586 case OPTION_CHANGE_LEADING_CHAR
:
2587 change_leading_char
= TRUE
;
2590 case OPTION_DEBUGGING
:
2591 convert_debugging
= TRUE
;
2594 case OPTION_GAP_FILL
:
2596 bfd_vma gap_fill_vma
;
2598 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2599 gap_fill
= (bfd_byte
) gap_fill_vma
;
2600 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2604 sprintf_vma (buff
, gap_fill_vma
);
2606 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2609 gap_fill_set
= TRUE
;
2613 case OPTION_NO_CHANGE_WARNINGS
:
2614 change_warn
= FALSE
;
2618 pad_to
= parse_vma (optarg
, "--pad-to");
2622 case OPTION_REMOVE_LEADING_CHAR
:
2623 remove_leading_char
= TRUE
;
2626 case OPTION_REDEFINE_SYM
:
2628 /* Push this redefinition onto redefine_symbol_list. */
2632 const char *nextarg
;
2633 char *source
, *target
;
2635 s
= strchr (optarg
, '=');
2637 fatal (_("bad format for %s"), "--redefine-sym");
2640 source
= (char *) xmalloc (len
+ 1);
2641 strncpy (source
, optarg
, len
);
2645 len
= strlen (nextarg
);
2646 target
= (char *) xmalloc (len
+ 1);
2647 strcpy (target
, nextarg
);
2649 redefine_list_append ("--redefine-sym", source
, target
);
2656 case OPTION_REDEFINE_SYMS
:
2657 add_redefine_syms_file (optarg
);
2660 case OPTION_SET_SECTION_FLAGS
:
2666 s
= strchr (optarg
, '=');
2668 fatal (_("bad format for %s"), "--set-section-flags");
2671 name
= (char *) xmalloc (len
+ 1);
2672 strncpy (name
, optarg
, len
);
2675 p
= find_section_list (name
, TRUE
);
2677 p
->set_flags
= TRUE
;
2678 p
->flags
= parse_flags (s
+ 1);
2682 case OPTION_RENAME_SECTION
:
2685 const char *eq
, *fl
;
2690 eq
= strchr (optarg
, '=');
2692 fatal (_("bad format for %s"), "--rename-section");
2696 fatal (_("bad format for %s"), "--rename-section");
2698 old_name
= (char *) xmalloc (len
+ 1);
2699 strncpy (old_name
, optarg
, len
);
2703 fl
= strchr (eq
, ',');
2706 flags
= parse_flags (fl
+ 1);
2716 fatal (_("bad format for %s"), "--rename-section");
2718 new_name
= (char *) xmalloc (len
+ 1);
2719 strncpy (new_name
, eq
, len
);
2722 add_section_rename (old_name
, new_name
, flags
);
2726 case OPTION_SET_START
:
2727 set_start
= parse_vma (optarg
, "--set-start");
2728 set_start_set
= TRUE
;
2731 case OPTION_SREC_LEN
:
2732 Chunk
= parse_vma (optarg
, "--srec-len");
2735 case OPTION_SREC_FORCES3
:
2739 case OPTION_STRIP_SYMBOLS
:
2740 add_specific_symbols (optarg
, &strip_specific_list
);
2743 case OPTION_KEEP_SYMBOLS
:
2744 add_specific_symbols (optarg
, &keep_specific_list
);
2747 case OPTION_LOCALIZE_SYMBOLS
:
2748 add_specific_symbols (optarg
, &localize_specific_list
);
2751 case OPTION_KEEPGLOBAL_SYMBOLS
:
2752 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2755 case OPTION_WEAKEN_SYMBOLS
:
2756 add_specific_symbols (optarg
, &weaken_specific_list
);
2759 case OPTION_ALT_MACH_CODE
:
2760 use_alt_mach_code
= atoi (optarg
);
2761 if (use_alt_mach_code
<= 0)
2762 fatal (_("alternate machine code index must be positive"));
2765 case OPTION_PREFIX_SYMBOLS
:
2766 prefix_symbols_string
= optarg
;
2769 case OPTION_PREFIX_SECTIONS
:
2770 prefix_sections_string
= optarg
;
2773 case OPTION_PREFIX_ALLOC_SECTIONS
:
2774 prefix_alloc_sections_string
= optarg
;
2778 /* We've been given a long option. */
2783 copy_usage (stdout
, 0);
2786 copy_usage (stderr
, 1);
2797 print_version ("objcopy");
2799 if (copy_byte
>= interleave
)
2800 fatal (_("byte number must be less than interleave"));
2802 if (optind
== argc
|| optind
+ 2 < argc
)
2803 copy_usage (stderr
, 1);
2805 input_filename
= argv
[optind
];
2806 if (optind
+ 1 < argc
)
2807 output_filename
= argv
[optind
+ 1];
2809 /* Default is to strip no symbols. */
2810 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2811 strip_symbols
= STRIP_NONE
;
2813 if (output_target
== (char *) NULL
)
2814 output_target
= input_target
;
2816 if (binary_architecture
!= (char *) NULL
)
2818 if (input_target
&& strcmp (input_target
, "binary") == 0)
2820 const bfd_arch_info_type
* temp_arch_info
;
2822 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2824 if (temp_arch_info
!= NULL
)
2825 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2827 fatal (_("architecture %s unknown"), binary_architecture
);
2831 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2832 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2837 if (stat (input_filename
, & statbuf
) < 0)
2838 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2840 /* If there is no destination file, or the source and destination files
2841 are the same, then create a temp and rename the result into the input. */
2842 if ((output_filename
== (char *) NULL
) ||
2843 (strcmp (input_filename
, output_filename
) == 0))
2845 char *tmpname
= make_tempname (input_filename
);
2847 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2851 set_times (tmpname
, &statbuf
);
2852 smart_rename (tmpname
, input_filename
, preserve_dates
);
2859 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2861 if (status
== 0 && preserve_dates
)
2862 set_times (output_filename
, &statbuf
);
2867 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2871 if (p
->change_vma
!= CHANGE_IGNORE
)
2875 sprintf_vma (buff
, p
->vma_val
);
2877 /* xgettext:c-format */
2878 non_fatal (_("%s %s%c0x%s never used"),
2879 "--change-section-vma",
2881 p
->change_vma
== CHANGE_SET
? '=' : '+',
2885 if (p
->change_lma
!= CHANGE_IGNORE
)
2889 sprintf_vma (buff
, p
->lma_val
);
2891 /* xgettext:c-format */
2892 non_fatal (_("%s %s%c0x%s never used"),
2893 "--change-section-lma",
2895 p
->change_lma
== CHANGE_SET
? '=' : '+',
2906 main (int argc
, char *argv
[])
2908 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2909 setlocale (LC_MESSAGES
, "");
2911 #if defined (HAVE_SETLOCALE)
2912 setlocale (LC_CTYPE
, "");
2914 bindtextdomain (PACKAGE
, LOCALEDIR
);
2915 textdomain (PACKAGE
);
2917 program_name
= argv
[0];
2918 xmalloc_set_program_name (program_name
);
2920 START_PROGRESS (program_name
, 0);
2922 strip_symbols
= STRIP_UNDEF
;
2923 discard_locals
= LOCALS_UNDEF
;
2926 set_default_bfd_target ();
2930 int i
= strlen (program_name
);
2931 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2932 /* Drop the .exe suffix, if any. */
2933 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2936 program_name
[i
] = '\0';
2939 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2943 strip_main (argc
, argv
);
2945 copy_main (argc
, argv
);
2947 END_PROGRESS (program_name
);