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
64 PARAMS ((FILE *, int));
65 static void strip_usage
66 PARAMS ((FILE *, int));
67 static flagword parse_flags
68 PARAMS ((const char *));
69 static struct section_list
*find_section_list
70 PARAMS ((const char *, bfd_boolean
));
71 static void setup_section
72 PARAMS ((bfd
*, asection
*, PTR
));
73 static void copy_section
74 PARAMS ((bfd
*, asection
*, PTR
));
75 static void get_sections
76 PARAMS ((bfd
*, asection
*, PTR
));
77 static int compare_section_lma
78 PARAMS ((const PTR
, const PTR
));
79 static void add_specific_symbol
80 PARAMS ((const char *, struct symlist
**));
81 static void add_specific_symbols
82 PARAMS ((const char *, struct symlist
**));
83 static bfd_boolean is_specified_symbol
84 PARAMS ((const char *, struct symlist
*));
85 static bfd_boolean is_strip_section
86 PARAMS ((bfd
*, asection
*));
87 static unsigned int filter_symbols
88 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
89 static void mark_symbols_used_in_relocations
90 PARAMS ((bfd
*, asection
*, PTR
));
91 static void filter_bytes
92 PARAMS ((char *, bfd_size_type
*));
93 static bfd_boolean write_debugging_info
94 PARAMS ((bfd
*, PTR
, long *, asymbol
***));
95 static void copy_object
96 PARAMS ((bfd
*, bfd
*));
97 static void copy_archive
98 PARAMS ((bfd
*, bfd
*, const char *));
100 PARAMS ((const char *, const char *, const char *, const char *));
101 static int strip_main
102 PARAMS ((int, char **));
104 PARAMS ((int, char **));
105 static const char *lookup_sym_redefinition
106 PARAMS((const char *));
107 static void redefine_list_append
108 PARAMS ((const char *, const char *, const char *));
109 static const char * find_section_rename
110 PARAMS ((bfd
*, sec_ptr
, flagword
*));
111 static void add_section_rename
112 PARAMS ((const char *, const char *, flagword
));
113 static void add_redefine_syms_file
114 PARAMS ((const char *));
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_ALL
/* strip all symbols */
138 /* Which symbols to remove. */
139 static enum strip_action strip_symbols
;
144 LOCALS_START_L
, /* discard locals starting with L */
145 LOCALS_ALL
/* discard all locals */
148 /* Which local symbols to remove. Overrides STRIP_ALL. */
149 static enum locals_action discard_locals
;
151 /* What kind of change to perform. */
159 /* Structure used to hold lists of sections and actions to take. */
162 struct section_list
* next
; /* Next section to change. */
163 const char * name
; /* Section name. */
164 bfd_boolean used
; /* Whether this entry was used. */
165 bfd_boolean remove
; /* Whether to remove this section. */
166 bfd_boolean copy
; /* Whether to copy this section. */
167 enum change_action change_vma
;/* Whether to change or set VMA. */
168 bfd_vma vma_val
; /* Amount to change by or set to. */
169 enum change_action change_lma
;/* Whether to change or set LMA. */
170 bfd_vma lma_val
; /* Amount to change by or set to. */
171 bfd_boolean set_flags
; /* Whether to set the section flags. */
172 flagword flags
; /* What to set the section flags to. */
175 static struct section_list
*change_sections
;
177 /* TRUE if some sections are to be removed. */
178 static bfd_boolean sections_removed
;
180 /* TRUE if only some sections are to be copied. */
181 static bfd_boolean sections_copied
;
183 /* Changes to the start address. */
184 static bfd_vma change_start
= 0;
185 static bfd_boolean set_start_set
= FALSE
;
186 static bfd_vma set_start
;
188 /* Changes to section addresses. */
189 static bfd_vma change_section_address
= 0;
191 /* Filling gaps between sections. */
192 static bfd_boolean gap_fill_set
= FALSE
;
193 static bfd_byte gap_fill
= 0;
195 /* Pad to a given address. */
196 static bfd_boolean pad_to_set
= FALSE
;
197 static bfd_vma pad_to
;
199 /* Use alternate machine code? */
200 static int use_alt_mach_code
= 0;
202 /* List of sections to add. */
205 /* Next section to add. */
206 struct section_add
*next
;
207 /* Name of section to add. */
209 /* Name of file holding section contents. */
210 const char *filename
;
213 /* Contents of file. */
215 /* BFD section, after it has been added. */
219 /* List of sections to add to the output BFD. */
220 static struct section_add
*add_sections
;
222 /* If non-NULL the argument to --add-gnu-debuglink.
223 This should be the filename to store in the .gnu_debuglink section. */
224 static const char * gnu_debuglink_filename
= NULL
;
226 /* Whether to convert debugging information. */
227 static bfd_boolean convert_debugging
= FALSE
;
229 /* Whether to change the leading character in symbol names. */
230 static bfd_boolean change_leading_char
= FALSE
;
232 /* Whether to remove the leading character from global symbol names. */
233 static bfd_boolean remove_leading_char
= FALSE
;
235 /* List of symbols to strip, keep, localize, keep-global, weaken,
237 static struct symlist
*strip_specific_list
= NULL
;
238 static struct symlist
*keep_specific_list
= NULL
;
239 static struct symlist
*localize_specific_list
= NULL
;
240 static struct symlist
*keepglobal_specific_list
= NULL
;
241 static struct symlist
*weaken_specific_list
= NULL
;
242 static struct redefine_node
*redefine_sym_list
= NULL
;
244 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
245 static bfd_boolean weaken
= FALSE
;
247 /* Prefix symbols/sections. */
248 static char *prefix_symbols_string
= 0;
249 static char *prefix_sections_string
= 0;
250 static char *prefix_alloc_sections_string
= 0;
252 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
254 #define OPTION_ADD_SECTION 150
255 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
256 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
257 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
258 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
259 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
260 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
261 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
262 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
263 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
264 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
265 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
266 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
267 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
268 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
269 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
270 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
271 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
272 #define OPTION_REDEFINE_SYMS (OPTION_REDEFINE_SYM + 1)
273 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYMS + 1)
274 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
275 #define OPTION_STRIP_SYMBOLS (OPTION_SREC_FORCES3 + 1)
276 #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1)
277 #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
278 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
279 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
280 #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
281 #define OPTION_ALT_MACH_CODE (OPTION_RENAME_SECTION + 1)
282 #define OPTION_PREFIX_SYMBOLS (OPTION_ALT_MACH_CODE + 1)
283 #define OPTION_PREFIX_SECTIONS (OPTION_PREFIX_SYMBOLS + 1)
284 #define OPTION_PREFIX_ALLOC_SECTIONS (OPTION_PREFIX_SECTIONS + 1)
285 #define OPTION_FORMATS_INFO (OPTION_PREFIX_ALLOC_SECTIONS + 1)
286 #define OPTION_ADD_GNU_DEBUGLINK (OPTION_FORMATS_INFO + 1)
288 /* Options to handle if running as "strip". */
290 static struct option strip_options
[] =
292 {"discard-all", no_argument
, 0, 'x'},
293 {"discard-locals", no_argument
, 0, 'X'},
294 {"format", required_argument
, 0, 'F'}, /* Obsolete */
295 {"help", no_argument
, 0, 'h'},
296 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
297 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
298 {"input-target", required_argument
, 0, 'I'},
299 {"keep-symbol", required_argument
, 0, 'K'},
300 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
301 {"output-target", required_argument
, 0, 'O'},
302 {"output-file", required_argument
, 0, 'o'},
303 {"preserve-dates", no_argument
, 0, 'p'},
304 {"remove-section", required_argument
, 0, 'R'},
305 {"strip-all", no_argument
, 0, 's'},
306 {"strip-debug", no_argument
, 0, 'S'},
307 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
308 {"strip-symbol", required_argument
, 0, 'N'},
309 {"target", required_argument
, 0, 'F'},
310 {"verbose", no_argument
, 0, 'v'},
311 {"version", no_argument
, 0, 'V'},
312 {0, no_argument
, 0, 0}
315 /* Options to handle if running as "objcopy". */
317 static struct option copy_options
[] =
319 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
320 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
321 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
322 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
323 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
324 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
325 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
326 {"binary-architecture", required_argument
, 0, 'B'},
327 {"byte", required_argument
, 0, 'b'},
328 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
329 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
330 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
331 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
332 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
333 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
334 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
335 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
336 {"discard-all", no_argument
, 0, 'x'},
337 {"discard-locals", no_argument
, 0, 'X'},
338 {"format", required_argument
, 0, 'F'}, /* Obsolete */
339 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
340 {"help", no_argument
, 0, 'h'},
341 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
342 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
343 {"input-target", required_argument
, 0, 'I'},
344 {"interleave", required_argument
, 0, 'i'},
345 {"keep-global-symbol", required_argument
, 0, 'G'},
346 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
347 {"keep-symbol", required_argument
, 0, 'K'},
348 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
349 {"localize-symbol", required_argument
, 0, 'L'},
350 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
351 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
352 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
353 {"only-section", required_argument
, 0, 'j'},
354 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
355 {"output-target", required_argument
, 0, 'O'},
356 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
357 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
358 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
359 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
360 {"preserve-dates", no_argument
, 0, 'p'},
361 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
362 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
363 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
364 {"remove-section", required_argument
, 0, 'R'},
365 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
366 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
367 {"set-start", required_argument
, 0, OPTION_SET_START
},
368 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
369 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
370 {"strip-all", no_argument
, 0, 'S'},
371 {"strip-debug", no_argument
, 0, 'g'},
372 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
373 {"strip-symbol", required_argument
, 0, 'N'},
374 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
375 {"target", required_argument
, 0, 'F'},
376 {"verbose", no_argument
, 0, 'v'},
377 {"version", no_argument
, 0, 'V'},
378 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
379 {"weaken-symbol", required_argument
, 0, 'W'},
380 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
381 {0, no_argument
, 0, 0}
385 extern char *program_name
;
387 /* This flag distinguishes between strip and objcopy:
388 1 means this is 'strip'; 0 means this is 'objcopy'.
389 -1 means if we should use argv[0] to decide. */
392 /* The maximum length of an S record. This variable is declared in srec.c
393 and can be modified by the --srec-len parameter. */
394 extern unsigned int Chunk
;
396 /* Restrict the generation of Srecords to type S3 only.
397 This variable is declare in bfd/srec.c and can be toggled
398 on by the --srec-forceS3 command line switch. */
399 extern bfd_boolean S3Forced
;
401 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
402 extern enum bfd_architecture bfd_external_binary_architecture
;
406 copy_usage (stream
, exit_status
)
410 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
411 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
412 fprintf (stream
, _(" The options are:\n"));
413 fprintf (stream
, _("\
414 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
415 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
416 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
417 -F --target <bfdname> Set both input and output format to <bfdname>\n\
418 --debugging Convert debugging information, if possible\n\
419 -p --preserve-dates Copy modified/access timestamps to the output\n\
420 -j --only-section <name> Only copy section <name> into the output\n\
421 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
422 -R --remove-section <name> Remove section <name> from the output\n\
423 -S --strip-all Remove all symbol and relocation information\n\
424 -g --strip-debug Remove all debugging symbols & sections\n\
425 --strip-unneeded Remove all symbols not needed by relocations\n\
426 -N --strip-symbol <name> Do not copy symbol <name>\n\
427 -K --keep-symbol <name> Only copy symbol <name>\n\
428 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
429 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
430 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
431 --weaken Force all global symbols to be marked as weak\n\
432 -x --discard-all Remove all non-global symbols\n\
433 -X --discard-locals Remove any compiler-generated symbols\n\
434 -i --interleave <number> Only copy one out of every <number> bytes\n\
435 -b --byte <num> Select byte <num> in every interleaved block\n\
436 --gap-fill <val> Fill gaps between sections with <val>\n\
437 --pad-to <addr> Pad the last section up to address <addr>\n\
438 --set-start <addr> Set the start address to <addr>\n\
439 {--change-start|--adjust-start} <incr>\n\
440 Add <incr> to the start address\n\
441 {--change-addresses|--adjust-vma} <incr>\n\
442 Add <incr> to LMA, VMA and start addresses\n\
443 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
444 Change LMA and VMA of section <name> by <val>\n\
445 --change-section-lma <name>{=|+|-}<val>\n\
446 Change the LMA of section <name> by <val>\n\
447 --change-section-vma <name>{=|+|-}<val>\n\
448 Change the VMA of section <name> by <val>\n\
449 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
450 Warn if a named section does not exist\n\
451 --set-section-flags <name>=<flags>\n\
452 Set section <name>'s properties to <flags>\n\
453 --add-section <name>=<file> Add section <name> found in <file> to output\n\
454 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
455 --change-leading-char Force output format's leading character style\n\
456 --remove-leading-char Remove leading character from global symbols\n\
457 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
458 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
460 --srec-len <number> Restrict the length of generated Srecords\n\
461 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
462 --strip-symbols <file> -N for all symbols listed in <file>\n\
463 --keep-symbols <file> -K for all symbols listed in <file>\n\
464 --localize-symbols <file> -L for all symbols listed in <file>\n\
465 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
466 --weaken-symbols <file> -W for all symbols listed in <file>\n\
467 --alt-machine-code <index> Use alternate machine code for output\n\
468 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
469 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
470 --prefix-alloc-sections <prefix>\n\
471 Add <prefix> to start of every allocatable\n\
473 -v --verbose List all object files modified\n\
474 -V --version Display this program's version number\n\
475 -h --help Display this output\n\
476 --info List object formats & architectures supported\n\
478 list_supported_targets (program_name
, stream
);
479 if (exit_status
== 0)
480 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
485 strip_usage (stream
, exit_status
)
489 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
490 fprintf (stream
, _(" Removes symbols and sections from files\n"));
491 fprintf (stream
, _(" The options are:\n"));
492 fprintf (stream
, _("\
493 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
494 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
495 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
496 -p --preserve-dates Copy modified/access timestamps to the output\n\
497 -R --remove-section=<name> Remove section <name> from the output\n\
498 -s --strip-all Remove all symbol and relocation information\n\
499 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
500 --strip-unneeded Remove all symbols not needed by relocations\n\
501 -N --strip-symbol=<name> Do not copy symbol <name>\n\
502 -K --keep-symbol=<name> Only copy symbol <name>\n\
503 -x --discard-all Remove all non-global symbols\n\
504 -X --discard-locals Remove any compiler-generated symbols\n\
505 -v --verbose List all object files modified\n\
506 -V --version Display this program's version number\n\
507 -h --help Display this output\n\
508 --info List object formats & architectures supported\n\
509 -o <file> Place stripped output into <file>\n\
512 list_supported_targets (program_name
, stream
);
513 if (exit_status
== 0)
514 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
518 /* Parse section flags into a flagword, with a fatal error if the
519 string can't be parsed. */
533 snext
= strchr (s
, ',');
543 #define PARSE_FLAG(fname,fval) \
544 else if (strncasecmp (fname, s, len) == 0) ret |= fval
545 PARSE_FLAG ("alloc", SEC_ALLOC
);
546 PARSE_FLAG ("load", SEC_LOAD
);
547 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
548 PARSE_FLAG ("readonly", SEC_READONLY
);
549 PARSE_FLAG ("debug", SEC_DEBUGGING
);
550 PARSE_FLAG ("code", SEC_CODE
);
551 PARSE_FLAG ("data", SEC_DATA
);
552 PARSE_FLAG ("rom", SEC_ROM
);
553 PARSE_FLAG ("share", SEC_SHARED
);
554 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
560 copy
= xmalloc (len
+ 1);
561 strncpy (copy
, s
, len
);
563 non_fatal (_("unrecognized section flag `%s'"), copy
);
564 fatal (_("supported flags: %s"),
565 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
575 /* Find and optionally add an entry in the change_sections list. */
577 static struct section_list
*
578 find_section_list (name
, add
)
582 register struct section_list
*p
;
584 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
585 if (strcmp (p
->name
, name
) == 0)
591 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
596 p
->change_vma
= CHANGE_IGNORE
;
597 p
->change_lma
= CHANGE_IGNORE
;
600 p
->set_flags
= FALSE
;
603 p
->next
= change_sections
;
609 /* Add a symbol to strip_specific_list. */
612 add_specific_symbol (name
, list
)
614 struct symlist
**list
;
616 struct symlist
*tmp_list
;
618 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
619 tmp_list
->name
= name
;
620 tmp_list
->next
= *list
;
624 /* Add symbols listed in `filename' to strip_specific_list. */
626 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
627 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
630 add_specific_symbols (filename
, list
)
631 const char *filename
;
632 struct symlist
**list
;
638 unsigned int line_count
;
640 if (stat (filename
, & st
) < 0)
641 fatal (_("cannot stat: %s: %s"), filename
, strerror (errno
));
645 buffer
= (char *) xmalloc (st
.st_size
+ 2);
646 f
= fopen (filename
, FOPEN_RT
);
648 fatal (_("cannot open: %s: %s"), filename
, strerror (errno
));
650 if (fread (buffer
, 1, st
.st_size
, f
) == 0 || ferror (f
))
651 fatal (_("%s: fread failed"), filename
);
654 buffer
[st
.st_size
] = '\n';
655 buffer
[st
.st_size
+ 1] = '\0';
659 for (line
= buffer
; * line
!= '\0'; line
++)
664 int finished
= FALSE
;
666 for (eol
= line
;; eol
++)
672 /* Cope with \n\r. */
680 /* Cope with \r\n. */
691 /* Line comment, Terminate the line here, in case a
692 name is present and then allow the rest of the
693 loop to find the real end of the line. */
705 /* A name may now exist somewhere between 'line' and 'eol'.
706 Strip off leading whitespace and trailing whitespace,
707 then add it to the list. */
708 for (name
= line
; IS_WHITESPACE (* name
); name
++)
710 for (name_end
= name
;
711 (! IS_WHITESPACE (* name_end
))
712 && (! IS_LINE_TERMINATOR (* name_end
));
716 if (! IS_LINE_TERMINATOR (* name_end
))
720 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
723 if (! IS_LINE_TERMINATOR (* extra
))
724 non_fatal (_("Ignoring rubbish found on line %d of %s"),
725 line_count
, filename
);
731 add_specific_symbol (name
, list
);
733 /* Advance line pointer to end of line. The 'eol ++' in the for
734 loop above will then advance us to the start of the next line. */
740 /* See whether a symbol should be stripped or kept based on
741 strip_specific_list and keep_symbols. */
744 is_specified_symbol (name
, list
)
746 struct symlist
*list
;
748 struct symlist
*tmp_list
;
750 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
751 if (strcmp (name
, tmp_list
->name
) == 0)
757 /* See if a section is being removed. */
760 is_strip_section (abfd
, sec
)
761 bfd
*abfd ATTRIBUTE_UNUSED
;
764 if (sections_removed
|| sections_copied
)
766 struct section_list
*p
;
768 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
770 if (sections_removed
&& p
!= NULL
&& p
->remove
)
772 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
776 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
778 if (strip_symbols
== STRIP_DEBUG
779 || strip_symbols
== STRIP_UNNEEDED
780 || strip_symbols
== STRIP_ALL
781 || discard_locals
== LOCALS_ALL
782 || convert_debugging
)
789 /* Choose which symbol entries to copy; put the result in OSYMS.
790 We don't copy in place, because that confuses the relocs.
791 Return the number of symbols to print. */
794 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
797 asymbol
**osyms
, **isyms
;
800 register asymbol
**from
= isyms
, **to
= osyms
;
801 long src_count
= 0, dst_count
= 0;
802 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
805 for (; src_count
< symcount
; src_count
++)
807 asymbol
*sym
= from
[src_count
];
808 flagword flags
= sym
->flags
;
809 char *name
= (char *) bfd_asymbol_name (sym
);
811 bfd_boolean undefined
;
812 bfd_boolean rem_leading_char
;
813 bfd_boolean add_leading_char
;
815 undefined
= bfd_is_und_section (bfd_get_section (sym
));
817 if (redefine_sym_list
)
819 char *old_name
, *new_name
;
821 old_name
= (char *) bfd_asymbol_name (sym
);
822 new_name
= (char *) lookup_sym_redefinition (old_name
);
823 bfd_asymbol_name (sym
) = new_name
;
827 /* Check if we will remove the current leading character. */
829 (name
[0] == bfd_get_symbol_leading_char (abfd
))
830 && (change_leading_char
831 || (remove_leading_char
832 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
834 || bfd_is_com_section (bfd_get_section (sym
)))));
836 /* Check if we will add a new leading character. */
839 && (bfd_get_symbol_leading_char (obfd
) != '\0')
840 && (bfd_get_symbol_leading_char (abfd
) == '\0'
841 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
843 /* Short circuit for change_leading_char if we can do it in-place. */
844 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
846 name
[0] = bfd_get_symbol_leading_char (obfd
);
847 bfd_asymbol_name (sym
) = name
;
848 rem_leading_char
= FALSE
;
849 add_leading_char
= FALSE
;
852 /* Remove leading char. */
853 if (rem_leading_char
)
854 bfd_asymbol_name (sym
) = ++name
;
856 /* Add new leading char and/or prefix. */
857 if (add_leading_char
|| prefix_symbols_string
)
861 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
) + strlen (name
) + 1);
862 if (add_leading_char
)
863 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
865 if (prefix_symbols_string
)
867 strcpy (ptr
, prefix_symbols_string
);
868 ptr
+= strlen (prefix_symbols_string
);
872 bfd_asymbol_name (sym
) = n
;
876 if (strip_symbols
== STRIP_ALL
)
878 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
879 || ((flags
& BSF_SECTION_SYM
) != 0
880 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
883 else if (relocatable
/* Relocatable file. */
884 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
886 else if (bfd_decode_symclass (sym
) == 'I')
887 /* Global symbols in $idata sections need to be retained
888 even if relocatable is FALSE. External users of the
889 library containing the $idata section may reference these
892 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
893 || (flags
& BSF_WEAK
) != 0
895 || bfd_is_com_section (bfd_get_section (sym
)))
896 keep
= strip_symbols
!= STRIP_UNNEEDED
;
897 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
898 keep
= (strip_symbols
!= STRIP_DEBUG
899 && strip_symbols
!= STRIP_UNNEEDED
900 && ! convert_debugging
);
901 else if (bfd_get_section (sym
)->comdat
)
902 /* COMDAT sections store special information in local
903 symbols, so we cannot risk stripping any of them. */
905 else /* Local symbol. */
906 keep
= (strip_symbols
!= STRIP_UNNEEDED
907 && (discard_locals
!= LOCALS_ALL
908 && (discard_locals
!= LOCALS_START_L
909 || ! bfd_is_local_label (abfd
, sym
))));
911 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
913 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
915 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
918 if (keep
&& (flags
& BSF_GLOBAL
) != 0
919 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
921 sym
->flags
&=~ BSF_GLOBAL
;
922 sym
->flags
|= BSF_WEAK
;
924 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
925 && (is_specified_symbol (name
, localize_specific_list
)
926 || (keepglobal_specific_list
!= NULL
927 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
929 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
930 sym
->flags
|= BSF_LOCAL
;
934 to
[dst_count
++] = sym
;
937 to
[dst_count
] = NULL
;
942 /* Find the redefined name of symbol SOURCE. */
945 lookup_sym_redefinition (source
)
948 struct redefine_node
*list
;
950 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
951 if (strcmp (source
, list
->source
) == 0)
957 /* Add a node to a symbol redefine list. */
960 redefine_list_append (cause
, source
, target
)
965 struct redefine_node
**p
;
966 struct redefine_node
*list
;
967 struct redefine_node
*new_node
;
969 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
971 if (strcmp (source
, list
->source
) == 0)
972 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
975 if (strcmp (target
, list
->target
) == 0)
976 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
980 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
982 new_node
->source
= strdup (source
);
983 new_node
->target
= strdup (target
);
984 new_node
->next
= NULL
;
989 /* Handle the --redefine-syms option. Read lines containing "old new"
990 from the file, and add them to the symbol redefine list. */
993 add_redefine_syms_file (filename
)
994 const char *filename
;
998 size_t bufsize
, len
, outsym_off
;
1001 file
= fopen (filename
, "r");
1002 if (file
== (FILE *) NULL
)
1003 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1004 filename
, strerror (errno
));
1007 buf
= (char *) xmalloc (bufsize
);
1015 /* Collect the input symbol name. */
1016 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1024 buf
= xrealloc (buf
, bufsize
);
1032 /* Eat white space between the symbol names. */
1033 while (IS_WHITESPACE (c
))
1035 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1040 /* Collect the output symbol name. */
1042 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1050 buf
= xrealloc (buf
, bufsize
);
1058 /* Eat white space at end of line. */
1059 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1064 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1065 || c
== '\n' || c
== EOF
)
1068 /* Append the redefinition to the list. */
1070 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1081 fatal (_("%s: garbage at end of line %d"), filename
, lineno
);
1083 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1084 fatal (_("%s: missing new symbol name at line %d"), filename
, lineno
);
1087 /* Eat the rest of the line and finish it. */
1088 while (c
!= '\n' && c
!= EOF
)
1094 fatal (_("%s: premature end of file at line %d"), filename
, lineno
);
1099 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
1103 filter_bytes (memhunk
, size
)
1105 bfd_size_type
*size
;
1107 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
1109 for (; from
< end
; from
+= interleave
)
1112 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
1113 *size
= (*size
/ interleave
) + 1;
1115 *size
/= interleave
;
1118 /* Copy object file IBFD onto OBFD. */
1121 copy_object (ibfd
, obfd
)
1127 asection
**osections
= NULL
;
1128 bfd_size_type
*gaps
= NULL
;
1129 bfd_size_type max_gap
= 0;
1132 enum bfd_architecture iarch
;
1135 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1136 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1137 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1139 fatal (_("Unable to change endianness of input file(s)"));
1143 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1144 RETURN_NONFATAL (bfd_get_filename (obfd
));
1147 printf (_("copy from %s(%s) to %s(%s)\n"),
1148 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
1149 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1154 start
= bfd_get_start_address (ibfd
);
1155 start
+= change_start
;
1157 /* Neither the start address nor the flags
1158 need to be set for a core file. */
1159 if (bfd_get_format (obfd
) != bfd_core
)
1161 if (!bfd_set_start_address (obfd
, start
)
1162 || !bfd_set_file_flags (obfd
,
1163 (bfd_get_file_flags (ibfd
)
1164 & bfd_applicable_file_flags (obfd
))))
1165 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1168 /* Copy architecture of input file to output file. */
1169 iarch
= bfd_get_arch (ibfd
);
1170 imach
= bfd_get_mach (ibfd
);
1171 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1172 && (ibfd
->target_defaulted
1173 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1174 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1175 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1176 bfd_get_mach (ibfd
)));
1178 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1179 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1184 if (osympp
!= isympp
)
1187 /* BFD mandates that all output sections be created and sizes set before
1188 any output is done. Thus, we traverse all sections multiple times. */
1189 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
1191 if (add_sections
!= NULL
)
1193 struct section_add
*padd
;
1194 struct section_list
*pset
;
1196 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1200 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1201 if (padd
->section
== NULL
)
1203 non_fatal (_("can't create section `%s': %s"),
1204 padd
->name
, bfd_errmsg (bfd_get_error ()));
1209 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1210 RETURN_NONFATAL (bfd_get_filename (obfd
));
1212 pset
= find_section_list (padd
->name
, FALSE
);
1216 if (pset
!= NULL
&& pset
->set_flags
)
1217 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1219 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1221 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1222 RETURN_NONFATAL (bfd_get_filename (obfd
));
1226 if (pset
->change_vma
!= CHANGE_IGNORE
)
1227 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
1228 RETURN_NONFATAL (bfd_get_filename (obfd
));
1230 if (pset
->change_lma
!= CHANGE_IGNORE
)
1232 padd
->section
->lma
= pset
->lma_val
;
1234 if (! bfd_set_section_alignment
1235 (obfd
, padd
->section
,
1236 bfd_section_alignment (obfd
, padd
->section
)))
1237 RETURN_NONFATAL (bfd_get_filename (obfd
));
1243 if (gnu_debuglink_filename
!= NULL
)
1245 if (! bfd_add_gnu_debuglink (obfd
, gnu_debuglink_filename
))
1246 RETURN_NONFATAL (gnu_debuglink_filename
);
1249 if (gap_fill_set
|| pad_to_set
)
1254 /* We must fill in gaps between the sections and/or we must pad
1255 the last section to a specified address. We do this by
1256 grabbing a list of the sections, sorting them by VMA, and
1257 increasing the section sizes as required to fill the gaps.
1258 We write out the gap contents below. */
1260 c
= bfd_count_sections (obfd
);
1261 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
1263 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
1265 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1267 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
1268 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1272 for (i
= 0; i
< c
- 1; i
++)
1276 bfd_vma gap_start
, gap_stop
;
1278 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1279 if ((flags
& SEC_HAS_CONTENTS
) == 0
1280 || (flags
& SEC_LOAD
) == 0)
1283 size
= bfd_section_size (obfd
, osections
[i
]);
1284 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1285 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1286 if (gap_start
< gap_stop
)
1288 if (! bfd_set_section_size (obfd
, osections
[i
],
1289 size
+ (gap_stop
- gap_start
)))
1291 non_fatal (_("Can't fill gap after %s: %s"),
1292 bfd_get_section_name (obfd
, osections
[i
]),
1293 bfd_errmsg (bfd_get_error ()));
1297 gaps
[i
] = gap_stop
- gap_start
;
1298 if (max_gap
< gap_stop
- gap_start
)
1299 max_gap
= gap_stop
- gap_start
;
1309 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1310 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1311 if (lma
+ size
< pad_to
)
1313 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1316 non_fatal (_("Can't add padding to %s: %s"),
1317 bfd_get_section_name (obfd
, osections
[c
- 1]),
1318 bfd_errmsg (bfd_get_error ()));
1323 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1324 if (max_gap
< pad_to
- (lma
+ size
))
1325 max_gap
= pad_to
- (lma
+ size
);
1331 /* Symbol filtering must happen after the output sections
1332 have been created, but before their contents are set. */
1334 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1336 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1338 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1339 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1341 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1343 if (convert_debugging
)
1344 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1346 if (strip_symbols
== STRIP_DEBUG
1347 || strip_symbols
== STRIP_ALL
1348 || strip_symbols
== STRIP_UNNEEDED
1349 || discard_locals
!= LOCALS_UNDEF
1350 || strip_specific_list
!= NULL
1351 || keep_specific_list
!= NULL
1352 || localize_specific_list
!= NULL
1353 || keepglobal_specific_list
!= NULL
1354 || weaken_specific_list
!= NULL
1355 || prefix_symbols_string
1358 || convert_debugging
1359 || change_leading_char
1360 || remove_leading_char
1361 || redefine_sym_list
1364 /* Mark symbols used in output relocations so that they
1365 are kept, even if they are local labels or static symbols.
1367 Note we iterate over the input sections examining their
1368 relocations since the relocations for the output sections
1369 haven't been set yet. mark_symbols_used_in_relocations will
1370 ignore input sections which have no corresponding output
1372 if (strip_symbols
!= STRIP_ALL
)
1373 bfd_map_over_sections (ibfd
,
1374 mark_symbols_used_in_relocations
,
1376 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1377 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1380 if (convert_debugging
&& dhandle
!= NULL
)
1382 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1389 bfd_set_symtab (obfd
, osympp
, symcount
);
1391 /* This has to happen after the symbol table has been set. */
1392 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
1394 if (add_sections
!= NULL
)
1396 struct section_add
*padd
;
1398 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1400 if (! bfd_set_section_contents (obfd
, padd
->section
,
1401 (PTR
) padd
->contents
,
1403 (bfd_size_type
) padd
->size
))
1404 RETURN_NONFATAL (bfd_get_filename (obfd
));
1408 if (gap_fill_set
|| pad_to_set
)
1413 /* Fill in the gaps. */
1416 buf
= (bfd_byte
*) xmalloc (max_gap
);
1417 memset (buf
, gap_fill
, (size_t) max_gap
);
1419 c
= bfd_count_sections (obfd
);
1420 for (i
= 0; i
< c
; i
++)
1428 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1439 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1441 RETURN_NONFATAL (bfd_get_filename (obfd
));
1450 /* Allow the BFD backend to copy any private data it understands
1451 from the input BFD to the output BFD. This is done last to
1452 permit the routine to look at the filtered symbol table, which is
1453 important for the ECOFF code at least. */
1454 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1456 non_fatal (_("%s: error copying private BFD data: %s"),
1457 bfd_get_filename (obfd
),
1458 bfd_errmsg (bfd_get_error ()));
1463 /* Switch to the alternate machine code. We have to do this at the
1464 very end, because we only initialize the header when we create
1465 the first section. */
1466 if (use_alt_mach_code
!= 0)
1468 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1469 non_fatal (_("unknown alternate machine code, ignored"));
1474 #if defined (_WIN32) && !defined (__CYGWIN32__)
1475 #define MKDIR(DIR, MODE) mkdir (DIR)
1477 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1480 /* Read each archive element in turn from IBFD, copy the
1481 contents to temp file, and keep the temp file handle. */
1484 copy_archive (ibfd
, obfd
, output_target
)
1487 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)
1502 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1503 dir
, strerror (errno
));
1505 obfd
->has_armap
= ibfd
->has_armap
;
1509 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1511 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1512 RETURN_NONFATAL (bfd_get_filename (obfd
));
1514 while (!status
&& this_element
!= (bfd
*) NULL
)
1520 int stat_status
= 0;
1522 /* Create an output file for this member. */
1523 output_name
= concat (dir
, "/",
1524 bfd_get_filename (this_element
), (char *) 0);
1526 /* If the file already exists, make another temp dir. */
1527 if (stat (output_name
, &buf
) >= 0)
1529 output_name
= make_tempname (output_name
);
1530 if (MKDIR (output_name
, 0700) != 0)
1532 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1533 output_name
, strerror (errno
));
1535 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1536 l
->name
= output_name
;
1540 output_name
= concat (output_name
, "/",
1541 bfd_get_filename (this_element
), (char *) 0);
1544 output_bfd
= bfd_openw (output_name
, output_target
);
1547 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1549 if (stat_status
!= 0)
1550 non_fatal (_("internal stat error on %s"),
1551 bfd_get_filename (this_element
));
1554 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1555 l
->name
= output_name
;
1559 if (output_bfd
== (bfd
*) NULL
)
1560 RETURN_NONFATAL (output_name
);
1562 if (bfd_check_format (this_element
, bfd_object
))
1563 copy_object (this_element
, output_bfd
);
1565 if (!bfd_close (output_bfd
))
1567 bfd_nonfatal (bfd_get_filename (output_bfd
));
1568 /* Error in new object file. Don't change archive. */
1572 if (preserve_dates
&& stat_status
== 0)
1573 set_times (output_name
, &buf
);
1575 /* Open the newly output file and attach to our list. */
1576 output_bfd
= bfd_openr (output_name
, output_target
);
1578 l
->obfd
= output_bfd
;
1581 ptr
= &output_bfd
->next
;
1583 last_element
= this_element
;
1585 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1587 bfd_close (last_element
);
1589 *ptr
= (bfd
*) NULL
;
1591 if (!bfd_close (obfd
))
1592 RETURN_NONFATAL (bfd_get_filename (obfd
));
1594 if (!bfd_close (ibfd
))
1595 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1597 /* Delete all the files that we opened. */
1598 for (l
= list
; l
!= NULL
; l
= l
->next
)
1600 if (l
->obfd
== NULL
)
1604 bfd_close (l
->obfd
);
1611 /* The top-level control. */
1614 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1615 const char *input_filename
;
1616 const char *output_filename
;
1617 const char *input_target
;
1618 const char *output_target
;
1621 char **obj_matching
;
1622 char **core_matching
;
1624 /* To allow us to do "strip *" without dying on the first
1625 non-object file, failures are nonfatal. */
1626 ibfd
= bfd_openr (input_filename
, input_target
);
1628 RETURN_NONFATAL (input_filename
);
1630 if (bfd_check_format (ibfd
, bfd_archive
))
1634 /* bfd_get_target does not return the correct value until
1635 bfd_check_format succeeds. */
1636 if (output_target
== NULL
)
1637 output_target
= bfd_get_target (ibfd
);
1639 obfd
= bfd_openw (output_filename
, output_target
);
1641 RETURN_NONFATAL (output_filename
);
1643 copy_archive (ibfd
, obfd
, output_target
);
1645 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1649 /* bfd_get_target does not return the correct value until
1650 bfd_check_format succeeds. */
1651 if (output_target
== NULL
)
1652 output_target
= bfd_get_target (ibfd
);
1654 obfd
= bfd_openw (output_filename
, output_target
);
1656 RETURN_NONFATAL (output_filename
);
1658 copy_object (ibfd
, obfd
);
1660 if (!bfd_close (obfd
))
1661 RETURN_NONFATAL (output_filename
);
1663 if (!bfd_close (ibfd
))
1664 RETURN_NONFATAL (input_filename
);
1668 bfd_error_type obj_error
= bfd_get_error ();
1669 bfd_error_type core_error
;
1671 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1673 /* This probably can't happen.. */
1674 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1675 free (obj_matching
);
1679 core_error
= bfd_get_error ();
1680 /* Report the object error in preference to the core error. */
1681 if (obj_error
!= core_error
)
1682 bfd_set_error (obj_error
);
1684 bfd_nonfatal (input_filename
);
1686 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1688 list_matching_formats (obj_matching
);
1689 free (obj_matching
);
1691 if (core_error
== bfd_error_file_ambiguously_recognized
)
1693 list_matching_formats (core_matching
);
1694 free (core_matching
);
1701 /* Add a name to the section renaming list. */
1704 add_section_rename (old_name
, new_name
, flags
)
1705 const char * old_name
;
1706 const char * new_name
;
1709 section_rename
* rename
;
1711 /* Check for conflicts first. */
1712 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1713 if (strcmp (rename
->old_name
, old_name
) == 0)
1715 /* Silently ignore duplicate definitions. */
1716 if (strcmp (rename
->new_name
, new_name
) == 0
1717 && rename
->flags
== flags
)
1720 fatal (_("Multiple renames of section %s"), old_name
);
1723 rename
= (section_rename
*) xmalloc (sizeof (* rename
));
1725 rename
->old_name
= old_name
;
1726 rename
->new_name
= new_name
;
1727 rename
->flags
= flags
;
1728 rename
->next
= section_rename_list
;
1730 section_rename_list
= rename
;
1733 /* Check the section rename list for a new name of the input section
1734 ISECTION. Return the new name if one is found.
1735 Also set RETURNED_FLAGS to the flags to be used for this section. */
1738 find_section_rename (ibfd
, isection
, returned_flags
)
1739 bfd
* ibfd ATTRIBUTE_UNUSED
;
1741 flagword
* returned_flags
;
1743 const char * old_name
= bfd_section_name (ibfd
, isection
);
1744 section_rename
* rename
;
1746 /* Default to using the flags of the input section. */
1747 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1749 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1750 if (strcmp (rename
->old_name
, old_name
) == 0)
1752 if (rename
->flags
!= (flagword
) -1)
1753 * returned_flags
= rename
->flags
;
1755 return rename
->new_name
;
1761 /* Create a section in OBFD with the same
1762 name and attributes as ISECTION in IBFD. */
1765 setup_section (ibfd
, isection
, obfdarg
)
1770 bfd
*obfd
= (bfd
*) obfdarg
;
1771 struct section_list
*p
;
1779 char *prefix
= NULL
;
1781 if (is_strip_section (ibfd
, isection
))
1784 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1788 /* Get the, possibly new, name of the output section. */
1789 name
= find_section_rename (ibfd
, isection
, & flags
);
1791 /* Prefix sections. */
1792 if ((prefix_alloc_sections_string
) && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1793 prefix
= prefix_alloc_sections_string
;
1794 else if (prefix_sections_string
)
1795 prefix
= prefix_sections_string
;
1801 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1807 osection
= bfd_make_section_anyway (obfd
, name
);
1809 if (osection
== NULL
)
1815 size
= bfd_section_size (ibfd
, isection
);
1817 size
= (size
+ interleave
- 1) / interleave
;
1818 if (! bfd_set_section_size (obfd
, osection
, size
))
1824 vma
= bfd_section_vma (ibfd
, isection
);
1825 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1827 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1830 vma
+= change_section_address
;
1832 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1838 lma
= isection
->lma
;
1839 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1841 if (p
->change_lma
== CHANGE_MODIFY
)
1843 else if (p
->change_lma
== CHANGE_SET
)
1849 lma
+= change_section_address
;
1851 osection
->lma
= lma
;
1853 /* FIXME: This is probably not enough. If we change the LMA we
1854 may have to recompute the header for the file as well. */
1855 if (!bfd_set_section_alignment (obfd
,
1857 bfd_section_alignment (ibfd
, isection
)))
1859 err
= _("alignment");
1863 if (p
!= NULL
&& p
->set_flags
)
1864 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1865 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1871 /* Copy merge entity size. */
1872 osection
->entsize
= isection
->entsize
;
1874 /* This used to be mangle_section; we do here to avoid using
1875 bfd_get_section_by_name since some formats allow multiple
1876 sections with the same name. */
1877 isection
->output_section
= osection
;
1878 isection
->output_offset
= 0;
1880 /* Allow the BFD backend to copy any private data it understands
1881 from the input section to the output section. */
1882 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1884 err
= _("private data");
1888 /* All went well. */
1892 non_fatal (_("%s: section `%s': error in %s: %s"),
1893 bfd_get_filename (ibfd
),
1894 bfd_section_name (ibfd
, isection
),
1895 err
, bfd_errmsg (bfd_get_error ()));
1899 /* Copy the data of input section ISECTION of IBFD
1900 to an output section with the same name in OBFD.
1901 If stripping then don't copy any relocation info. */
1904 copy_section (ibfd
, isection
, obfdarg
)
1909 bfd
*obfd
= (bfd
*) obfdarg
;
1910 struct section_list
*p
;
1918 /* If we have already failed earlier on,
1919 do not keep on generating complaints now. */
1923 if (is_strip_section (ibfd
, isection
))
1926 flags
= bfd_get_section_flags (ibfd
, isection
);
1927 if ((flags
& SEC_GROUP
) != 0)
1930 osection
= isection
->output_section
;
1931 size
= bfd_get_section_size_before_reloc (isection
);
1933 if (size
== 0 || osection
== 0)
1936 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
1938 /* Core files do not need to be relocated. */
1939 if (bfd_get_format (obfd
) == bfd_core
)
1942 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1945 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1948 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1951 relpp
= (arelent
**) xmalloc (relsize
);
1952 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1954 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1956 if (strip_symbols
== STRIP_ALL
)
1958 /* Remove relocations which are not in
1959 keep_strip_specific_list. */
1960 arelent
**temp_relpp
;
1961 long temp_relcount
= 0;
1964 temp_relpp
= (arelent
**) xmalloc (relsize
);
1965 for (i
= 0; i
< relcount
; i
++)
1966 if (is_specified_symbol
1967 (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1968 keep_specific_list
))
1969 temp_relpp
[temp_relcount
++] = relpp
[i
];
1970 relcount
= temp_relcount
;
1975 bfd_set_reloc (obfd
, osection
,
1976 (relcount
== 0 ? (arelent
**) NULL
: relpp
), relcount
);
1979 isection
->_cooked_size
= isection
->_raw_size
;
1980 isection
->reloc_done
= TRUE
;
1982 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
1983 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
1985 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1987 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1989 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1992 filter_bytes (memhunk
, &size
);
1994 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1996 RETURN_NONFATAL (bfd_get_filename (obfd
));
2000 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2002 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
2004 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2005 flag--they can just remove the section entirely and add it
2006 back again. However, we do permit them to turn on the
2007 SEC_HAS_CONTENTS flag, and take it to mean that the section
2008 contents should be zeroed out. */
2010 memset (memhunk
, 0, size
);
2011 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
2013 RETURN_NONFATAL (bfd_get_filename (obfd
));
2018 /* Get all the sections. This is used when --gap-fill or --pad-to is
2022 get_sections (obfd
, osection
, secppparg
)
2023 bfd
*obfd ATTRIBUTE_UNUSED
;
2027 asection
***secppp
= (asection
***) secppparg
;
2029 **secppp
= osection
;
2033 /* Sort sections by VMA. This is called via qsort, and is used when
2034 --gap-fill or --pad-to is used. We force non loadable or empty
2035 sections to the front, where they are easier to ignore. */
2038 compare_section_lma (arg1
, arg2
)
2042 const asection
**sec1
= (const asection
**) arg1
;
2043 const asection
**sec2
= (const asection
**) arg2
;
2044 flagword flags1
, flags2
;
2046 /* Sort non loadable sections to the front. */
2047 flags1
= (*sec1
)->flags
;
2048 flags2
= (*sec2
)->flags
;
2049 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2050 || (flags1
& SEC_LOAD
) == 0)
2052 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2053 && (flags2
& SEC_LOAD
) != 0)
2058 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2059 || (flags2
& SEC_LOAD
) == 0)
2063 /* Sort sections by LMA. */
2064 if ((*sec1
)->lma
> (*sec2
)->lma
)
2066 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2069 /* Sort sections with the same LMA by size. */
2070 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
2072 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
2078 /* Mark all the symbols which will be used in output relocations with
2079 the BSF_KEEP flag so that those symbols will not be stripped.
2081 Ignore relocations which will not appear in the output file. */
2084 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
2089 asymbol
**symbols
= (asymbol
**) symbolsarg
;
2094 /* Ignore an input section with no corresponding output section. */
2095 if (isection
->output_section
== NULL
)
2098 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2100 bfd_fatal (bfd_get_filename (ibfd
));
2105 relpp
= (arelent
**) xmalloc (relsize
);
2106 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2108 bfd_fatal (bfd_get_filename (ibfd
));
2110 /* Examine each symbol used in a relocation. If it's not one of the
2111 special bfd section symbols, then mark it with BSF_KEEP. */
2112 for (i
= 0; i
< relcount
; i
++)
2114 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2115 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2116 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2117 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2124 /* Write out debugging information. */
2127 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
2130 long *symcountp ATTRIBUTE_UNUSED
;
2131 asymbol
***symppp ATTRIBUTE_UNUSED
;
2133 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2134 return write_ieee_debugging_info (obfd
, dhandle
);
2136 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2137 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2139 bfd_byte
*syms
, *strings
;
2140 bfd_size_type symsize
, stringsize
;
2141 asection
*stabsec
, *stabstrsec
;
2143 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2148 stabsec
= bfd_make_section (obfd
, ".stab");
2149 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2151 || stabstrsec
== NULL
2152 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2153 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2154 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2155 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2156 || ! bfd_set_section_flags (obfd
, stabsec
,
2160 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2165 non_fatal (_("%s: can't create debugging section: %s"),
2166 bfd_get_filename (obfd
),
2167 bfd_errmsg (bfd_get_error ()));
2171 /* We can get away with setting the section contents now because
2172 the next thing the caller is going to do is copy over the
2173 real sections. We may someday have to split the contents
2174 setting out of this function. */
2175 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
2177 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
2178 (file_ptr
) 0, stringsize
))
2180 non_fatal (_("%s: can't set debugging section contents: %s"),
2181 bfd_get_filename (obfd
),
2182 bfd_errmsg (bfd_get_error ()));
2189 non_fatal (_("%s: don't know how to write debugging information for %s"),
2190 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2195 strip_main (argc
, argv
)
2199 char *input_target
= NULL
;
2200 char *output_target
= NULL
;
2201 bfd_boolean show_version
= FALSE
;
2202 bfd_boolean formats_info
= FALSE
;
2205 struct section_list
*p
;
2206 char *output_file
= NULL
;
2208 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVv",
2209 strip_options
, (int *) 0)) != EOF
)
2214 input_target
= optarg
;
2217 output_target
= optarg
;
2220 input_target
= output_target
= optarg
;
2223 p
= find_section_list (optarg
, TRUE
);
2225 sections_removed
= TRUE
;
2228 strip_symbols
= STRIP_ALL
;
2232 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2233 strip_symbols
= STRIP_DEBUG
;
2235 case OPTION_STRIP_UNNEEDED
:
2236 strip_symbols
= STRIP_UNNEEDED
;
2239 add_specific_symbol (optarg
, &keep_specific_list
);
2242 add_specific_symbol (optarg
, &strip_specific_list
);
2245 output_file
= optarg
;
2248 preserve_dates
= TRUE
;
2251 discard_locals
= LOCALS_ALL
;
2254 discard_locals
= LOCALS_START_L
;
2260 show_version
= TRUE
;
2262 case OPTION_FORMATS_INFO
:
2263 formats_info
= TRUE
;
2266 /* We've been given a long option. */
2270 strip_usage (stdout
, 0);
2272 strip_usage (stderr
, 1);
2283 print_version ("strip");
2285 /* Default is to strip all symbols. */
2286 if (strip_symbols
== STRIP_UNDEF
2287 && discard_locals
== LOCALS_UNDEF
2288 && strip_specific_list
== NULL
)
2289 strip_symbols
= STRIP_ALL
;
2291 if (output_target
== (char *) NULL
)
2292 output_target
= input_target
;
2296 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2297 strip_usage (stderr
, 1);
2299 for (; i
< argc
; i
++)
2301 int hold_status
= status
;
2302 struct stat statbuf
;
2307 if (stat (argv
[i
], &statbuf
) < 0)
2309 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
2314 if (output_file
!= NULL
)
2315 tmpname
= output_file
;
2317 tmpname
= make_tempname (argv
[i
]);
2320 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2324 set_times (tmpname
, &statbuf
);
2325 if (output_file
== NULL
)
2326 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2327 status
= hold_status
;
2331 if (output_file
== NULL
)
2339 copy_main (argc
, argv
)
2343 char * binary_architecture
= NULL
;
2344 char *input_filename
= NULL
;
2345 char *output_filename
= NULL
;
2346 char *input_target
= NULL
;
2347 char *output_target
= NULL
;
2348 bfd_boolean show_version
= FALSE
;
2349 bfd_boolean change_warn
= TRUE
;
2350 bfd_boolean formats_info
= FALSE
;
2352 struct section_list
*p
;
2353 struct stat statbuf
;
2355 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2356 copy_options
, (int *) 0)) != EOF
)
2361 copy_byte
= atoi (optarg
);
2363 fatal (_("byte number must be non-negative"));
2367 binary_architecture
= optarg
;
2371 interleave
= atoi (optarg
);
2373 fatal (_("interleave must be positive"));
2377 case 's': /* "source" - 'I' is preferred */
2378 input_target
= optarg
;
2382 case 'd': /* "destination" - 'O' is preferred */
2383 output_target
= optarg
;
2387 input_target
= output_target
= optarg
;
2391 p
= find_section_list (optarg
, TRUE
);
2393 fatal (_("%s both copied and removed"), optarg
);
2395 sections_copied
= TRUE
;
2399 p
= find_section_list (optarg
, TRUE
);
2401 fatal (_("%s both copied and removed"), optarg
);
2403 sections_removed
= TRUE
;
2407 strip_symbols
= STRIP_ALL
;
2411 strip_symbols
= STRIP_DEBUG
;
2414 case OPTION_STRIP_UNNEEDED
:
2415 strip_symbols
= STRIP_UNNEEDED
;
2418 case OPTION_ADD_GNU_DEBUGLINK
:
2419 gnu_debuglink_filename
= optarg
;
2423 add_specific_symbol (optarg
, &keep_specific_list
);
2427 add_specific_symbol (optarg
, &strip_specific_list
);
2431 add_specific_symbol (optarg
, &localize_specific_list
);
2435 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2439 add_specific_symbol (optarg
, &weaken_specific_list
);
2443 preserve_dates
= TRUE
;
2447 discard_locals
= LOCALS_ALL
;
2451 discard_locals
= LOCALS_START_L
;
2459 show_version
= TRUE
;
2462 case OPTION_FORMATS_INFO
:
2463 formats_info
= TRUE
;
2470 case OPTION_ADD_SECTION
:
2474 struct section_add
*pa
;
2479 s
= strchr (optarg
, '=');
2482 fatal (_("bad format for %s"), "--add-section");
2484 if (stat (s
+ 1, & st
) < 0)
2485 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
2487 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
2490 name
= (char *) xmalloc (len
+ 1);
2491 strncpy (name
, optarg
, len
);
2495 pa
->filename
= s
+ 1;
2497 pa
->size
= st
.st_size
;
2499 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
2500 f
= fopen (pa
->filename
, FOPEN_RB
);
2503 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
2505 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2507 fatal (_("%s: fread failed"), pa
->filename
);
2511 pa
->next
= add_sections
;
2516 case OPTION_CHANGE_START
:
2517 change_start
= parse_vma (optarg
, "--change-start");
2520 case OPTION_CHANGE_SECTION_ADDRESS
:
2521 case OPTION_CHANGE_SECTION_LMA
:
2522 case OPTION_CHANGE_SECTION_VMA
:
2527 char *option
= NULL
;
2529 enum change_action what
= CHANGE_IGNORE
;
2533 case OPTION_CHANGE_SECTION_ADDRESS
:
2534 option
= "--change-section-address";
2536 case OPTION_CHANGE_SECTION_LMA
:
2537 option
= "--change-section-lma";
2539 case OPTION_CHANGE_SECTION_VMA
:
2540 option
= "--change-section-vma";
2544 s
= strchr (optarg
, '=');
2547 s
= strchr (optarg
, '+');
2550 s
= strchr (optarg
, '-');
2552 fatal (_("bad format for %s"), option
);
2557 name
= (char *) xmalloc (len
+ 1);
2558 strncpy (name
, optarg
, len
);
2561 p
= find_section_list (name
, TRUE
);
2563 val
= parse_vma (s
+ 1, option
);
2567 case '=': what
= CHANGE_SET
; break;
2568 case '-': val
= - val
; /* Drop through. */
2569 case '+': what
= CHANGE_MODIFY
; break;
2574 case OPTION_CHANGE_SECTION_ADDRESS
:
2575 p
->change_vma
= what
;
2579 case OPTION_CHANGE_SECTION_LMA
:
2580 p
->change_lma
= what
;
2584 case OPTION_CHANGE_SECTION_VMA
:
2585 p
->change_vma
= what
;
2592 case OPTION_CHANGE_ADDRESSES
:
2593 change_section_address
= parse_vma (optarg
, "--change-addresses");
2594 change_start
= change_section_address
;
2597 case OPTION_CHANGE_WARNINGS
:
2601 case OPTION_CHANGE_LEADING_CHAR
:
2602 change_leading_char
= TRUE
;
2605 case OPTION_DEBUGGING
:
2606 convert_debugging
= TRUE
;
2609 case OPTION_GAP_FILL
:
2611 bfd_vma gap_fill_vma
;
2613 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2614 gap_fill
= (bfd_byte
) gap_fill_vma
;
2615 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2619 sprintf_vma (buff
, gap_fill_vma
);
2621 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2624 gap_fill_set
= TRUE
;
2628 case OPTION_NO_CHANGE_WARNINGS
:
2629 change_warn
= FALSE
;
2633 pad_to
= parse_vma (optarg
, "--pad-to");
2637 case OPTION_REMOVE_LEADING_CHAR
:
2638 remove_leading_char
= TRUE
;
2641 case OPTION_REDEFINE_SYM
:
2643 /* Push this redefinition onto redefine_symbol_list. */
2647 const char *nextarg
;
2648 char *source
, *target
;
2650 s
= strchr (optarg
, '=');
2652 fatal (_("bad format for %s"), "--redefine-sym");
2655 source
= (char *) xmalloc (len
+ 1);
2656 strncpy (source
, optarg
, len
);
2660 len
= strlen (nextarg
);
2661 target
= (char *) xmalloc (len
+ 1);
2662 strcpy (target
, nextarg
);
2664 redefine_list_append ("--redefine-sym", source
, target
);
2671 case OPTION_REDEFINE_SYMS
:
2672 add_redefine_syms_file (optarg
);
2675 case OPTION_SET_SECTION_FLAGS
:
2681 s
= strchr (optarg
, '=');
2683 fatal (_("bad format for %s"), "--set-section-flags");
2686 name
= (char *) xmalloc (len
+ 1);
2687 strncpy (name
, optarg
, len
);
2690 p
= find_section_list (name
, TRUE
);
2692 p
->set_flags
= TRUE
;
2693 p
->flags
= parse_flags (s
+ 1);
2697 case OPTION_RENAME_SECTION
:
2700 const char *eq
, *fl
;
2705 eq
= strchr (optarg
, '=');
2707 fatal (_("bad format for %s"), "--rename-section");
2711 fatal (_("bad format for %s"), "--rename-section");
2713 old_name
= (char *) xmalloc (len
+ 1);
2714 strncpy (old_name
, optarg
, len
);
2718 fl
= strchr (eq
, ',');
2721 flags
= parse_flags (fl
+ 1);
2731 fatal (_("bad format for %s"), "--rename-section");
2733 new_name
= (char *) xmalloc (len
+ 1);
2734 strncpy (new_name
, eq
, len
);
2737 add_section_rename (old_name
, new_name
, flags
);
2741 case OPTION_SET_START
:
2742 set_start
= parse_vma (optarg
, "--set-start");
2743 set_start_set
= TRUE
;
2746 case OPTION_SREC_LEN
:
2747 Chunk
= parse_vma (optarg
, "--srec-len");
2750 case OPTION_SREC_FORCES3
:
2754 case OPTION_STRIP_SYMBOLS
:
2755 add_specific_symbols (optarg
, &strip_specific_list
);
2758 case OPTION_KEEP_SYMBOLS
:
2759 add_specific_symbols (optarg
, &keep_specific_list
);
2762 case OPTION_LOCALIZE_SYMBOLS
:
2763 add_specific_symbols (optarg
, &localize_specific_list
);
2766 case OPTION_KEEPGLOBAL_SYMBOLS
:
2767 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2770 case OPTION_WEAKEN_SYMBOLS
:
2771 add_specific_symbols (optarg
, &weaken_specific_list
);
2774 case OPTION_ALT_MACH_CODE
:
2775 use_alt_mach_code
= atoi (optarg
);
2776 if (use_alt_mach_code
<= 0)
2777 fatal (_("alternate machine code index must be positive"));
2780 case OPTION_PREFIX_SYMBOLS
:
2781 prefix_symbols_string
= optarg
;
2784 case OPTION_PREFIX_SECTIONS
:
2785 prefix_sections_string
= optarg
;
2788 case OPTION_PREFIX_ALLOC_SECTIONS
:
2789 prefix_alloc_sections_string
= optarg
;
2793 /* We've been given a long option. */
2798 copy_usage (stdout
, 0);
2801 copy_usage (stderr
, 1);
2812 print_version ("objcopy");
2814 if (copy_byte
>= interleave
)
2815 fatal (_("byte number must be less than interleave"));
2817 if (optind
== argc
|| optind
+ 2 < argc
)
2818 copy_usage (stderr
, 1);
2820 input_filename
= argv
[optind
];
2821 if (optind
+ 1 < argc
)
2822 output_filename
= argv
[optind
+ 1];
2824 /* Default is to strip no symbols. */
2825 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2826 strip_symbols
= STRIP_NONE
;
2828 if (output_target
== (char *) NULL
)
2829 output_target
= input_target
;
2831 if (binary_architecture
!= (char *) NULL
)
2833 if (input_target
&& strcmp (input_target
, "binary") == 0)
2835 const bfd_arch_info_type
* temp_arch_info
;
2837 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2839 if (temp_arch_info
!= NULL
)
2840 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2842 fatal (_("architecture %s unknown"), binary_architecture
);
2846 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2847 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2852 if (stat (input_filename
, & statbuf
) < 0)
2853 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2855 /* If there is no destination file, or the source and destination files
2856 are the same, then create a temp and rename the result into the input. */
2857 if ((output_filename
== (char *) NULL
) ||
2858 (strcmp (input_filename
, output_filename
) == 0))
2860 char *tmpname
= make_tempname (input_filename
);
2862 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2866 set_times (tmpname
, &statbuf
);
2867 smart_rename (tmpname
, input_filename
, preserve_dates
);
2874 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2876 if (status
== 0 && preserve_dates
)
2877 set_times (output_filename
, &statbuf
);
2882 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2886 if (p
->change_vma
!= CHANGE_IGNORE
)
2890 sprintf_vma (buff
, p
->vma_val
);
2892 /* xgettext:c-format */
2893 non_fatal (_("%s %s%c0x%s never used"),
2894 "--change-section-vma",
2896 p
->change_vma
== CHANGE_SET
? '=' : '+',
2900 if (p
->change_lma
!= CHANGE_IGNORE
)
2904 sprintf_vma (buff
, p
->lma_val
);
2906 /* xgettext:c-format */
2907 non_fatal (_("%s %s%c0x%s never used"),
2908 "--change-section-lma",
2910 p
->change_lma
== CHANGE_SET
? '=' : '+',
2920 int main
PARAMS ((int, char **));
2927 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2928 setlocale (LC_MESSAGES
, "");
2930 #if defined (HAVE_SETLOCALE)
2931 setlocale (LC_CTYPE
, "");
2933 bindtextdomain (PACKAGE
, LOCALEDIR
);
2934 textdomain (PACKAGE
);
2936 program_name
= argv
[0];
2937 xmalloc_set_program_name (program_name
);
2939 START_PROGRESS (program_name
, 0);
2941 strip_symbols
= STRIP_UNDEF
;
2942 discard_locals
= LOCALS_UNDEF
;
2945 set_default_bfd_target ();
2949 int i
= strlen (program_name
);
2950 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2951 /* Drop the .exe suffix, if any. */
2952 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2955 program_name
[i
] = '\0';
2958 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2962 strip_main (argc
, argv
);
2964 copy_main (argc
, argv
);
2966 END_PROGRESS (program_name
);