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"
34 /* A list of symbols to explicitly strip out, or to keep. A linked
35 list is good enough for a small number from the command line, but
36 this will slow things down a lot if many symbols are being
45 /* A list to support redefine_sym. */
50 struct redefine_node
*next
;
53 typedef struct section_rename
55 const char * old_name
;
56 const char * new_name
;
58 struct section_rename
* next
;
62 /* List of sections to be renamed. */
63 static section_rename
*section_rename_list
;
65 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
67 static asymbol
**isympp
= NULL
; /* Input symbols. */
68 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
70 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
71 static int copy_byte
= -1;
72 static int interleave
= 4;
74 static bfd_boolean verbose
; /* Print file and target names. */
75 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
76 static int status
= 0; /* Exit status. */
81 STRIP_NONE
, /* Don't strip. */
82 STRIP_DEBUG
, /* Strip all debugger symbols. */
83 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
84 STRIP_NONDEBUG
, /* Strip everything but debug info. */
85 STRIP_ALL
/* Strip all symbols. */
88 /* Which symbols to remove. */
89 static enum strip_action strip_symbols
;
94 LOCALS_START_L
, /* Discard locals starting with L. */
95 LOCALS_ALL
/* Discard all locals. */
98 /* Which local symbols to remove. Overrides STRIP_ALL. */
99 static enum locals_action discard_locals
;
101 /* What kind of change to perform. */
109 /* Structure used to hold lists of sections and actions to take. */
112 struct section_list
* next
; /* Next section to change. */
113 const char * name
; /* Section name. */
114 bfd_boolean used
; /* Whether this entry was used. */
115 bfd_boolean remove
; /* Whether to remove this section. */
116 bfd_boolean copy
; /* Whether to copy this section. */
117 enum change_action change_vma
;/* Whether to change or set VMA. */
118 bfd_vma vma_val
; /* Amount to change by or set to. */
119 enum change_action change_lma
;/* Whether to change or set LMA. */
120 bfd_vma lma_val
; /* Amount to change by or set to. */
121 bfd_boolean set_flags
; /* Whether to set the section flags. */
122 flagword flags
; /* What to set the section flags to. */
125 static struct section_list
*change_sections
;
127 /* TRUE if some sections are to be removed. */
128 static bfd_boolean sections_removed
;
130 /* TRUE if only some sections are to be copied. */
131 static bfd_boolean sections_copied
;
133 /* Changes to the start address. */
134 static bfd_vma change_start
= 0;
135 static bfd_boolean set_start_set
= FALSE
;
136 static bfd_vma set_start
;
138 /* Changes to section addresses. */
139 static bfd_vma change_section_address
= 0;
141 /* Filling gaps between sections. */
142 static bfd_boolean gap_fill_set
= FALSE
;
143 static bfd_byte gap_fill
= 0;
145 /* Pad to a given address. */
146 static bfd_boolean pad_to_set
= FALSE
;
147 static bfd_vma pad_to
;
149 /* Use alternate machine code? */
150 static int use_alt_mach_code
= 0;
152 /* Output BFD flags user wants to set or clear */
153 static flagword bfd_flags_to_set
;
154 static flagword bfd_flags_to_clear
;
156 /* List of sections to add. */
159 /* Next section to add. */
160 struct section_add
*next
;
161 /* Name of section to add. */
163 /* Name of file holding section contents. */
164 const char *filename
;
167 /* Contents of file. */
169 /* BFD section, after it has been added. */
173 /* List of sections to add to the output BFD. */
174 static struct section_add
*add_sections
;
176 /* If non-NULL the argument to --add-gnu-debuglink.
177 This should be the filename to store in the .gnu_debuglink section. */
178 static const char * gnu_debuglink_filename
= NULL
;
180 /* Whether to convert debugging information. */
181 static bfd_boolean convert_debugging
= FALSE
;
183 /* Whether to change the leading character in symbol names. */
184 static bfd_boolean change_leading_char
= FALSE
;
186 /* Whether to remove the leading character from global symbol names. */
187 static bfd_boolean remove_leading_char
= FALSE
;
189 /* Whether to permit wildcard in symbol comparison. */
190 static bfd_boolean wildcard
= FALSE
;
192 /* List of symbols to strip, keep, localize, keep-global, weaken,
194 static struct symlist
*strip_specific_list
= NULL
;
195 static struct symlist
*strip_unneeded_list
= NULL
;
196 static struct symlist
*keep_specific_list
= NULL
;
197 static struct symlist
*localize_specific_list
= NULL
;
198 static struct symlist
*keepglobal_specific_list
= NULL
;
199 static struct symlist
*weaken_specific_list
= NULL
;
200 static struct redefine_node
*redefine_sym_list
= NULL
;
202 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
203 static bfd_boolean weaken
= FALSE
;
205 /* Prefix symbols/sections. */
206 static char *prefix_symbols_string
= 0;
207 static char *prefix_sections_string
= 0;
208 static char *prefix_alloc_sections_string
= 0;
210 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
211 enum command_line_switch
213 OPTION_ADD_SECTION
=150,
214 OPTION_CHANGE_ADDRESSES
,
215 OPTION_CHANGE_LEADING_CHAR
,
217 OPTION_CHANGE_SECTION_ADDRESS
,
218 OPTION_CHANGE_SECTION_LMA
,
219 OPTION_CHANGE_SECTION_VMA
,
220 OPTION_CHANGE_WARNINGS
,
223 OPTION_NO_CHANGE_WARNINGS
,
225 OPTION_REMOVE_LEADING_CHAR
,
226 OPTION_SET_SECTION_FLAGS
,
228 OPTION_STRIP_UNNEEDED
,
231 OPTION_REDEFINE_SYMS
,
234 OPTION_STRIP_SYMBOLS
,
235 OPTION_STRIP_UNNEEDED_SYMBOL
,
236 OPTION_STRIP_UNNEEDED_SYMBOLS
,
238 OPTION_LOCALIZE_SYMBOLS
,
239 OPTION_KEEPGLOBAL_SYMBOLS
,
240 OPTION_WEAKEN_SYMBOLS
,
241 OPTION_RENAME_SECTION
,
242 OPTION_ALT_MACH_CODE
,
243 OPTION_PREFIX_SYMBOLS
,
244 OPTION_PREFIX_SECTIONS
,
245 OPTION_PREFIX_ALLOC_SECTIONS
,
247 OPTION_ADD_GNU_DEBUGLINK
,
248 OPTION_ONLY_KEEP_DEBUG
,
249 OPTION_READONLY_TEXT
,
250 OPTION_WRITABLE_TEXT
,
255 /* Options to handle if running as "strip". */
257 static struct option strip_options
[] =
259 {"discard-all", no_argument
, 0, 'x'},
260 {"discard-locals", no_argument
, 0, 'X'},
261 {"format", required_argument
, 0, 'F'}, /* Obsolete */
262 {"help", no_argument
, 0, 'h'},
263 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
264 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
265 {"input-target", required_argument
, 0, 'I'},
266 {"keep-symbol", required_argument
, 0, 'K'},
267 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
268 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
269 {"output-target", required_argument
, 0, 'O'},
270 {"output-file", required_argument
, 0, 'o'},
271 {"preserve-dates", no_argument
, 0, 'p'},
272 {"remove-section", required_argument
, 0, 'R'},
273 {"strip-all", no_argument
, 0, 's'},
274 {"strip-debug", no_argument
, 0, 'S'},
275 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
276 {"strip-symbol", required_argument
, 0, 'N'},
277 {"target", required_argument
, 0, 'F'},
278 {"verbose", no_argument
, 0, 'v'},
279 {"version", no_argument
, 0, 'V'},
280 {"wildcard", no_argument
, 0, 'w'},
281 {0, no_argument
, 0, 0}
284 /* Options to handle if running as "objcopy". */
286 static struct option copy_options
[] =
288 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
289 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
290 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
291 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
292 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
293 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
294 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
295 {"binary-architecture", required_argument
, 0, 'B'},
296 {"byte", required_argument
, 0, 'b'},
297 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
298 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
299 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
300 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
301 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
302 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
303 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
304 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
305 {"discard-all", no_argument
, 0, 'x'},
306 {"discard-locals", no_argument
, 0, 'X'},
307 {"format", required_argument
, 0, 'F'}, /* Obsolete */
308 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
309 {"help", no_argument
, 0, 'h'},
310 {"impure", no_argument
, 0, OPTION_IMPURE
},
311 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
312 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
313 {"input-target", required_argument
, 0, 'I'},
314 {"interleave", required_argument
, 0, 'i'},
315 {"keep-global-symbol", required_argument
, 0, 'G'},
316 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
317 {"keep-symbol", required_argument
, 0, 'K'},
318 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
319 {"localize-symbol", required_argument
, 0, 'L'},
320 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
321 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
322 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
323 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
324 {"only-section", required_argument
, 0, 'j'},
325 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
326 {"output-target", required_argument
, 0, 'O'},
327 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
328 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
329 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
330 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
331 {"preserve-dates", no_argument
, 0, 'p'},
332 {"pure", no_argument
, 0, OPTION_PURE
},
333 {"readonly-text", no_argument
, 0, OPTION_READONLY_TEXT
},
334 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
335 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
336 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
337 {"remove-section", required_argument
, 0, 'R'},
338 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
339 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
340 {"set-start", required_argument
, 0, OPTION_SET_START
},
341 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
342 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
343 {"strip-all", no_argument
, 0, 'S'},
344 {"strip-debug", no_argument
, 0, 'g'},
345 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
346 {"strip-unneeded-symbol", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOL
},
347 {"strip-unneeded-symbols", required_argument
, 0, OPTION_STRIP_UNNEEDED_SYMBOLS
},
348 {"strip-symbol", required_argument
, 0, 'N'},
349 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
350 {"target", required_argument
, 0, 'F'},
351 {"verbose", no_argument
, 0, 'v'},
352 {"version", no_argument
, 0, 'V'},
353 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
354 {"weaken-symbol", required_argument
, 0, 'W'},
355 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
356 {"wildcard", no_argument
, 0, 'w'},
357 {"writable-text", no_argument
, 0, OPTION_WRITABLE_TEXT
},
358 {0, no_argument
, 0, 0}
362 extern char *program_name
;
364 /* This flag distinguishes between strip and objcopy:
365 1 means this is 'strip'; 0 means this is 'objcopy'.
366 -1 means if we should use argv[0] to decide. */
369 /* The maximum length of an S record. This variable is declared in srec.c
370 and can be modified by the --srec-len parameter. */
371 extern unsigned int Chunk
;
373 /* Restrict the generation of Srecords to type S3 only.
374 This variable is declare in bfd/srec.c and can be toggled
375 on by the --srec-forceS3 command line switch. */
376 extern bfd_boolean S3Forced
;
378 /* Defined in bfd/binary.c. Used to set architecture and machine of input
380 extern enum bfd_architecture bfd_external_binary_architecture
;
381 extern unsigned long bfd_external_machine
;
383 /* Forward declarations. */
384 static void setup_section (bfd
*, asection
*, void *);
385 static void setup_bfd_headers (bfd
*, bfd
*);
386 static void copy_section (bfd
*, asection
*, void *);
387 static void get_sections (bfd
*, asection
*, void *);
388 static int compare_section_lma (const void *, const void *);
389 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
390 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
391 static const char *lookup_sym_redefinition (const char *);
394 copy_usage (FILE *stream
, int exit_status
)
396 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
397 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
398 fprintf (stream
, _(" The options are:\n"));
399 fprintf (stream
, _("\
400 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
401 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
402 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
403 -F --target <bfdname> Set both input and output format to <bfdname>\n\
404 --debugging Convert debugging information, if possible\n\
405 -p --preserve-dates Copy modified/access timestamps to the output\n\
406 -j --only-section <name> Only copy section <name> into the output\n\
407 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
408 -R --remove-section <name> Remove section <name> from the output\n\
409 -S --strip-all Remove all symbol and relocation information\n\
410 -g --strip-debug Remove all debugging symbols & sections\n\
411 --strip-unneeded Remove all symbols not needed by relocations\n\
412 -N --strip-symbol <name> Do not copy symbol <name>\n\
413 --strip-unneeded-symbol <name>\n\
414 Do not copy symbol <name> unless needed by\n\
416 --only-keep-debug Strip everything but the debug information\n\
417 -K --keep-symbol <name> Only copy symbol <name>\n\
418 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
419 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
420 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
421 --weaken Force all global symbols to be marked as weak\n\
422 -w --wildcard Permit wildcard in symbol comparison\n\
423 -x --discard-all Remove all non-global symbols\n\
424 -X --discard-locals Remove any compiler-generated symbols\n\
425 -i --interleave <number> Only copy one out of every <number> bytes\n\
426 -b --byte <num> Select byte <num> in every interleaved block\n\
427 --gap-fill <val> Fill gaps between sections with <val>\n\
428 --pad-to <addr> Pad the last section up to address <addr>\n\
429 --set-start <addr> Set the start address to <addr>\n\
430 {--change-start|--adjust-start} <incr>\n\
431 Add <incr> to the start address\n\
432 {--change-addresses|--adjust-vma} <incr>\n\
433 Add <incr> to LMA, VMA and start addresses\n\
434 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
435 Change LMA and VMA of section <name> by <val>\n\
436 --change-section-lma <name>{=|+|-}<val>\n\
437 Change the LMA of section <name> by <val>\n\
438 --change-section-vma <name>{=|+|-}<val>\n\
439 Change the VMA of section <name> by <val>\n\
440 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
441 Warn if a named section does not exist\n\
442 --set-section-flags <name>=<flags>\n\
443 Set section <name>'s properties to <flags>\n\
444 --add-section <name>=<file> Add section <name> found in <file> to output\n\
445 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
446 --change-leading-char Force output format's leading character style\n\
447 --remove-leading-char Remove leading character from global symbols\n\
448 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
449 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
451 --srec-len <number> Restrict the length of generated Srecords\n\
452 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
453 --strip-symbols <file> -N for all symbols listed in <file>\n\
454 --strip-unneeded-symbols <file>\n\
455 --strip-unneeded-symbol for all symbols listed\n\
457 --keep-symbols <file> -K for all symbols listed in <file>\n\
458 --localize-symbols <file> -L for all symbols listed in <file>\n\
459 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
460 --weaken-symbols <file> -W for all symbols listed in <file>\n\
461 --alt-machine-code <index> Use alternate machine code for output\n\
462 --writable-text Mark the output text as writable\n\
463 --readonly-text Make the output text write protected\n\
464 --pure Mark the output file as demand paged\n\
465 --impure Mark the output file as impure\n\
466 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
467 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
468 --prefix-alloc-sections <prefix>\n\
469 Add <prefix> to start of every allocatable\n\
471 -v --verbose List all object files modified\n\
472 -V --version Display this program's version number\n\
473 -h --help Display this output\n\
474 --info List object formats & architectures supported\n\
476 list_supported_targets (program_name
, stream
);
477 if (exit_status
== 0)
478 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
483 strip_usage (FILE *stream
, int exit_status
)
485 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
486 fprintf (stream
, _(" Removes symbols and sections from files\n"));
487 fprintf (stream
, _(" The options are:\n"));
488 fprintf (stream
, _("\
489 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
490 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
491 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
492 -p --preserve-dates Copy modified/access timestamps to the output\n\
493 -R --remove-section=<name> Remove section <name> from the output\n\
494 -s --strip-all Remove all symbol and relocation information\n\
495 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
496 --strip-unneeded Remove all symbols not needed by relocations\n\
497 --only-keep-debug Strip everything but the debug information\n\
498 -N --strip-symbol=<name> Do not copy symbol <name>\n\
499 -K --keep-symbol=<name> Only copy symbol <name>\n\
500 -w --wildcard Permit wildcard in symbol comparison\n\
501 -x --discard-all Remove all non-global symbols\n\
502 -X --discard-locals Remove any compiler-generated symbols\n\
503 -v --verbose List all object files modified\n\
504 -V --version Display this program's version number\n\
505 -h --help Display this output\n\
506 --info List object formats & architectures supported\n\
507 -o <file> Place stripped output into <file>\n\
510 list_supported_targets (program_name
, stream
);
511 if (exit_status
== 0)
512 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
516 /* Parse section flags into a flagword, with a fatal error if the
517 string can't be parsed. */
520 parse_flags (const char *s
)
530 snext
= strchr (s
, ',');
540 #define PARSE_FLAG(fname,fval) \
541 else if (strncasecmp (fname, s, len) == 0) ret |= fval
542 PARSE_FLAG ("alloc", SEC_ALLOC
);
543 PARSE_FLAG ("load", SEC_LOAD
);
544 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
545 PARSE_FLAG ("readonly", SEC_READONLY
);
546 PARSE_FLAG ("debug", SEC_DEBUGGING
);
547 PARSE_FLAG ("code", SEC_CODE
);
548 PARSE_FLAG ("data", SEC_DATA
);
549 PARSE_FLAG ("rom", SEC_ROM
);
550 PARSE_FLAG ("share", SEC_SHARED
);
551 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
557 copy
= xmalloc (len
+ 1);
558 strncpy (copy
, s
, len
);
560 non_fatal (_("unrecognized section flag `%s'"), copy
);
561 fatal (_("supported flags: %s"),
562 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
572 /* Find and optionally add an entry in the change_sections list. */
574 static struct section_list
*
575 find_section_list (const char *name
, bfd_boolean add
)
577 struct section_list
*p
;
579 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
580 if (strcmp (p
->name
, name
) == 0)
586 p
= xmalloc (sizeof (struct section_list
));
591 p
->change_vma
= CHANGE_IGNORE
;
592 p
->change_lma
= CHANGE_IGNORE
;
595 p
->set_flags
= FALSE
;
598 p
->next
= change_sections
;
604 /* Add a symbol to strip_specific_list. */
607 add_specific_symbol (const char *name
, struct symlist
**list
)
609 struct symlist
*tmp_list
;
611 tmp_list
= xmalloc (sizeof (struct symlist
));
612 tmp_list
->name
= name
;
613 tmp_list
->next
= *list
;
617 /* Add symbols listed in `filename' to strip_specific_list. */
619 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
620 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
623 add_specific_symbols (const char *filename
, struct symlist
**list
)
629 unsigned int line_count
;
631 size
= get_file_size (filename
);
635 buffer
= xmalloc (size
+ 2);
636 f
= fopen (filename
, FOPEN_RT
);
638 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
640 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
641 fatal (_("%s: fread failed"), filename
);
644 buffer
[size
] = '\n';
645 buffer
[size
+ 1] = '\0';
649 for (line
= buffer
; * line
!= '\0'; line
++)
654 int finished
= FALSE
;
656 for (eol
= line
;; eol
++)
662 /* Cope with \n\r. */
670 /* Cope with \r\n. */
681 /* Line comment, Terminate the line here, in case a
682 name is present and then allow the rest of the
683 loop to find the real end of the line. */
695 /* A name may now exist somewhere between 'line' and 'eol'.
696 Strip off leading whitespace and trailing whitespace,
697 then add it to the list. */
698 for (name
= line
; IS_WHITESPACE (* name
); name
++)
700 for (name_end
= name
;
701 (! IS_WHITESPACE (* name_end
))
702 && (! IS_LINE_TERMINATOR (* name_end
));
706 if (! IS_LINE_TERMINATOR (* name_end
))
710 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
713 if (! IS_LINE_TERMINATOR (* extra
))
714 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
715 filename
, line_count
);
721 add_specific_symbol (name
, list
);
723 /* Advance line pointer to end of line. The 'eol ++' in the for
724 loop above will then advance us to the start of the next line. */
730 /* See whether a symbol should be stripped or kept based on
731 strip_specific_list and keep_symbols. */
734 is_specified_symbol (const char *name
, struct symlist
*list
)
736 struct symlist
*tmp_list
;
740 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
741 if (*(tmp_list
->name
) != '!')
743 if (!fnmatch (tmp_list
->name
, name
, 0))
748 if (fnmatch (tmp_list
->name
+ 1, name
, 0))
754 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
755 if (strcmp (name
, tmp_list
->name
) == 0)
762 /* See if a section is being removed. */
765 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
767 if (sections_removed
|| sections_copied
)
769 struct section_list
*p
;
771 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
773 if (sections_removed
&& p
!= NULL
&& p
->remove
)
775 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
779 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
781 if (strip_symbols
== STRIP_DEBUG
782 || strip_symbols
== STRIP_UNNEEDED
783 || strip_symbols
== STRIP_ALL
784 || discard_locals
== LOCALS_ALL
785 || convert_debugging
)
788 if (strip_symbols
== STRIP_NONDEBUG
)
795 /* Choose which symbol entries to copy; put the result in OSYMS.
796 We don't copy in place, because that confuses the relocs.
797 Return the number of symbols to print. */
800 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
801 asymbol
**isyms
, long symcount
)
803 asymbol
**from
= isyms
, **to
= osyms
;
804 long src_count
= 0, dst_count
= 0;
805 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
808 for (; src_count
< symcount
; src_count
++)
810 asymbol
*sym
= from
[src_count
];
811 flagword flags
= sym
->flags
;
812 char *name
= (char *) bfd_asymbol_name (sym
);
814 bfd_boolean undefined
;
815 bfd_boolean rem_leading_char
;
816 bfd_boolean add_leading_char
;
818 undefined
= bfd_is_und_section (bfd_get_section (sym
));
820 if (redefine_sym_list
)
822 char *old_name
, *new_name
;
824 old_name
= (char *) bfd_asymbol_name (sym
);
825 new_name
= (char *) lookup_sym_redefinition (old_name
);
826 bfd_asymbol_name (sym
) = new_name
;
830 /* Check if we will remove the current leading character. */
832 (name
[0] == bfd_get_symbol_leading_char (abfd
))
833 && (change_leading_char
834 || (remove_leading_char
835 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
837 || bfd_is_com_section (bfd_get_section (sym
)))));
839 /* Check if we will add a new leading character. */
842 && (bfd_get_symbol_leading_char (obfd
) != '\0')
843 && (bfd_get_symbol_leading_char (abfd
) == '\0'
844 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
846 /* Short circuit for change_leading_char if we can do it in-place. */
847 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
849 name
[0] = bfd_get_symbol_leading_char (obfd
);
850 bfd_asymbol_name (sym
) = name
;
851 rem_leading_char
= FALSE
;
852 add_leading_char
= FALSE
;
855 /* Remove leading char. */
856 if (rem_leading_char
)
857 bfd_asymbol_name (sym
) = ++name
;
859 /* Add new leading char and/or prefix. */
860 if (add_leading_char
|| prefix_symbols_string
)
864 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
865 + strlen (name
) + 1);
866 if (add_leading_char
)
867 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
869 if (prefix_symbols_string
)
871 strcpy (ptr
, prefix_symbols_string
);
872 ptr
+= strlen (prefix_symbols_string
);
876 bfd_asymbol_name (sym
) = n
;
880 if (strip_symbols
== STRIP_ALL
)
882 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
883 || ((flags
& BSF_SECTION_SYM
) != 0
884 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
887 else if (relocatable
/* Relocatable file. */
888 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
890 else if (bfd_decode_symclass (sym
) == 'I')
891 /* Global symbols in $idata sections need to be retained
892 even if relocatable is FALSE. External users of the
893 library containing the $idata section may reference these
896 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
897 || (flags
& BSF_WEAK
) != 0
899 || bfd_is_com_section (bfd_get_section (sym
)))
900 keep
= strip_symbols
!= STRIP_UNNEEDED
;
901 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
902 keep
= (strip_symbols
!= STRIP_DEBUG
903 && strip_symbols
!= STRIP_UNNEEDED
904 && ! convert_debugging
);
905 else if (bfd_coff_get_comdat_section (abfd
, bfd_get_section (sym
)))
906 /* COMDAT sections store special information in local
907 symbols, so we cannot risk stripping any of them. */
909 else /* Local symbol. */
910 keep
= (strip_symbols
!= STRIP_UNNEEDED
911 && (discard_locals
!= LOCALS_ALL
912 && (discard_locals
!= LOCALS_START_L
913 || ! bfd_is_local_label (abfd
, sym
))));
915 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
918 && !(flags
& BSF_KEEP
)
919 && is_specified_symbol (name
, strip_unneeded_list
))
921 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
923 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
926 if (keep
&& (flags
& BSF_GLOBAL
) != 0
927 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
929 sym
->flags
&=~ BSF_GLOBAL
;
930 sym
->flags
|= BSF_WEAK
;
932 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
933 && (is_specified_symbol (name
, localize_specific_list
)
934 || (keepglobal_specific_list
!= NULL
935 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
937 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
938 sym
->flags
|= BSF_LOCAL
;
942 to
[dst_count
++] = sym
;
945 to
[dst_count
] = NULL
;
950 /* Find the redefined name of symbol SOURCE. */
953 lookup_sym_redefinition (const char *source
)
955 struct redefine_node
*list
;
957 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
958 if (strcmp (source
, list
->source
) == 0)
964 /* Add a node to a symbol redefine list. */
967 redefine_list_append (const char *cause
, const char *source
, const char *target
)
969 struct redefine_node
**p
;
970 struct redefine_node
*list
;
971 struct redefine_node
*new_node
;
973 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
975 if (strcmp (source
, list
->source
) == 0)
976 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
979 if (strcmp (target
, list
->target
) == 0)
980 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
984 new_node
= xmalloc (sizeof (struct redefine_node
));
986 new_node
->source
= strdup (source
);
987 new_node
->target
= strdup (target
);
988 new_node
->next
= NULL
;
993 /* Handle the --redefine-syms option. Read lines containing "old new"
994 from the file, and add them to the symbol redefine list. */
997 add_redefine_syms_file (const char *filename
)
1006 file
= fopen (filename
, "r");
1008 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1009 filename
, strerror (errno
));
1012 buf
= xmalloc (bufsize
);
1020 /* Collect the input symbol name. */
1021 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1029 buf
= xrealloc (buf
, bufsize
);
1037 /* Eat white space between the symbol names. */
1038 while (IS_WHITESPACE (c
))
1040 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1045 /* Collect the output symbol name. */
1047 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1055 buf
= xrealloc (buf
, bufsize
);
1063 /* Eat white space at end of line. */
1064 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1069 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1070 || c
== '\n' || c
== EOF
)
1073 /* Append the redefinition to the list. */
1075 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1086 fatal (_("%s:%d: garbage found at end of line"), filename
, lineno
);
1088 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1089 fatal (_("%s:%d: missing new symbol name"), filename
, lineno
);
1092 /* Eat the rest of the line and finish it. */
1093 while (c
!= '\n' && c
!= EOF
)
1099 fatal (_("%s:%d: premature end of file"), filename
, lineno
);
1104 /* Copy object file IBFD onto OBFD.
1105 Returns TRUE upon success, FALSE otherwise. */
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
)
1124 fatal (_("Unable to change endianness of input file(s)"));
1126 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1128 bfd_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
)
1149 flags
= bfd_get_file_flags (ibfd
);
1150 flags
|= bfd_flags_to_set
;
1151 flags
&= ~bfd_flags_to_clear
;
1152 flags
&= bfd_applicable_file_flags (obfd
);
1154 if (!bfd_set_start_address (obfd
, start
)
1155 || !bfd_set_file_flags (obfd
, flags
))
1157 bfd_nonfatal (bfd_get_filename (ibfd
));
1162 /* Copy architecture of input file to output file. */
1163 iarch
= bfd_get_arch (ibfd
);
1164 imach
= bfd_get_mach (ibfd
);
1165 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1166 && (ibfd
->target_defaulted
1167 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1169 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1170 fatal (_("Unable to recognise the format of the input file %s"),
1171 bfd_get_filename (ibfd
));
1174 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1175 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1176 bfd_get_mach (ibfd
)));
1181 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1183 bfd_nonfatal (bfd_get_filename (ibfd
));
1190 if (osympp
!= isympp
)
1193 /* BFD mandates that all output sections be created and sizes set before
1194 any output is done. Thus, we traverse all sections multiple times. */
1195 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1197 setup_bfd_headers (ibfd
, obfd
);
1199 if (add_sections
!= NULL
)
1201 struct section_add
*padd
;
1202 struct section_list
*pset
;
1204 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1208 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1209 if (padd
->section
== NULL
)
1211 non_fatal (_("can't create section `%s': %s"),
1212 padd
->name
, bfd_errmsg (bfd_get_error ()));
1216 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1218 bfd_nonfatal (bfd_get_filename (obfd
));
1222 pset
= find_section_list (padd
->name
, FALSE
);
1226 if (pset
!= NULL
&& pset
->set_flags
)
1227 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1229 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1231 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1233 bfd_nonfatal (bfd_get_filename (obfd
));
1239 if (pset
->change_vma
!= CHANGE_IGNORE
)
1240 if (! bfd_set_section_vma (obfd
, padd
->section
,
1243 bfd_nonfatal (bfd_get_filename (obfd
));
1247 if (pset
->change_lma
!= CHANGE_IGNORE
)
1249 padd
->section
->lma
= pset
->lma_val
;
1251 if (! bfd_set_section_alignment
1252 (obfd
, padd
->section
,
1253 bfd_section_alignment (obfd
, padd
->section
)))
1255 bfd_nonfatal (bfd_get_filename (obfd
));
1263 if (gnu_debuglink_filename
!= NULL
)
1265 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1266 (obfd
, gnu_debuglink_filename
);
1268 if (gnu_debuglink_section
== NULL
)
1270 bfd_nonfatal (gnu_debuglink_filename
);
1275 if (bfd_count_sections (obfd
) == 0)
1277 non_fatal (_("there are no sections to be copied!"));
1281 if (gap_fill_set
|| pad_to_set
)
1286 /* We must fill in gaps between the sections and/or we must pad
1287 the last section to a specified address. We do this by
1288 grabbing a list of the sections, sorting them by VMA, and
1289 increasing the section sizes as required to fill the gaps.
1290 We write out the gap contents below. */
1292 c
= bfd_count_sections (obfd
);
1293 osections
= xmalloc (c
* sizeof (asection
*));
1295 bfd_map_over_sections (obfd
, get_sections
, &set
);
1297 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1299 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1300 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1304 for (i
= 0; i
< c
- 1; i
++)
1308 bfd_vma gap_start
, gap_stop
;
1310 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1311 if ((flags
& SEC_HAS_CONTENTS
) == 0
1312 || (flags
& SEC_LOAD
) == 0)
1315 size
= bfd_section_size (obfd
, osections
[i
]);
1316 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1317 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1318 if (gap_start
< gap_stop
)
1320 if (! bfd_set_section_size (obfd
, osections
[i
],
1321 size
+ (gap_stop
- gap_start
)))
1323 non_fatal (_("Can't fill gap after %s: %s"),
1324 bfd_get_section_name (obfd
, osections
[i
]),
1325 bfd_errmsg (bfd_get_error ()));
1329 gaps
[i
] = gap_stop
- gap_start
;
1330 if (max_gap
< gap_stop
- gap_start
)
1331 max_gap
= gap_stop
- gap_start
;
1341 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1342 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1343 if (lma
+ size
< pad_to
)
1345 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1348 non_fatal (_("Can't add padding to %s: %s"),
1349 bfd_get_section_name (obfd
, osections
[c
- 1]),
1350 bfd_errmsg (bfd_get_error ()));
1355 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1356 if (max_gap
< pad_to
- (lma
+ size
))
1357 max_gap
= pad_to
- (lma
+ size
);
1363 /* Symbol filtering must happen after the output sections
1364 have been created, but before their contents are set. */
1366 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1369 bfd_nonfatal (bfd_get_filename (ibfd
));
1373 osympp
= isympp
= xmalloc (symsize
);
1374 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1377 bfd_nonfatal (bfd_get_filename (ibfd
));
1381 if (convert_debugging
)
1382 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1384 if (strip_symbols
== STRIP_DEBUG
1385 || strip_symbols
== STRIP_ALL
1386 || strip_symbols
== STRIP_UNNEEDED
1387 || strip_symbols
== STRIP_NONDEBUG
1388 || discard_locals
!= LOCALS_UNDEF
1389 || strip_specific_list
!= NULL
1390 || keep_specific_list
!= NULL
1391 || localize_specific_list
!= NULL
1392 || keepglobal_specific_list
!= NULL
1393 || weaken_specific_list
!= NULL
1394 || prefix_symbols_string
1397 || convert_debugging
1398 || change_leading_char
1399 || remove_leading_char
1400 || redefine_sym_list
1403 /* Mark symbols used in output relocations so that they
1404 are kept, even if they are local labels or static symbols.
1406 Note we iterate over the input sections examining their
1407 relocations since the relocations for the output sections
1408 haven't been set yet. mark_symbols_used_in_relocations will
1409 ignore input sections which have no corresponding output
1411 if (strip_symbols
!= STRIP_ALL
)
1412 bfd_map_over_sections (ibfd
,
1413 mark_symbols_used_in_relocations
,
1415 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1416 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1419 if (convert_debugging
&& dhandle
!= NULL
)
1421 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1428 bfd_set_symtab (obfd
, osympp
, symcount
);
1430 /* This has to happen after the symbol table has been set. */
1431 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1433 if (add_sections
!= NULL
)
1435 struct section_add
*padd
;
1437 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1439 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1442 bfd_nonfatal (bfd_get_filename (obfd
));
1448 if (gnu_debuglink_filename
!= NULL
)
1450 if (! bfd_fill_in_gnu_debuglink_section
1451 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1453 bfd_nonfatal (gnu_debuglink_filename
);
1458 if (gap_fill_set
|| pad_to_set
)
1463 /* Fill in the gaps. */
1466 buf
= xmalloc (max_gap
);
1467 memset (buf
, gap_fill
, max_gap
);
1469 c
= bfd_count_sections (obfd
);
1470 for (i
= 0; i
< c
; i
++)
1478 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1489 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1492 bfd_nonfatal (bfd_get_filename (obfd
));
1503 /* Allow the BFD backend to copy any private data it understands
1504 from the input BFD to the output BFD. This is done last to
1505 permit the routine to look at the filtered symbol table, which is
1506 important for the ECOFF code at least. */
1507 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1508 && strip_symbols
== STRIP_NONDEBUG
)
1509 /* Do not copy the private data when creating an ELF format
1510 debug info file. We do not want the program headers. */
1512 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1514 non_fatal (_("%s: error copying private BFD data: %s"),
1515 bfd_get_filename (obfd
),
1516 bfd_errmsg (bfd_get_error ()));
1520 /* Switch to the alternate machine code. We have to do this at the
1521 very end, because we only initialize the header when we create
1522 the first section. */
1523 if (use_alt_mach_code
!= 0
1524 && ! bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1525 non_fatal (_("unknown alternate machine code, ignored"));
1531 #if defined (_WIN32) && !defined (__CYGWIN32__)
1532 #define MKDIR(DIR, MODE) mkdir (DIR)
1534 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1537 /* Read each archive element in turn from IBFD, copy the
1538 contents to temp file, and keep the temp file handle. */
1541 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1545 struct name_list
*next
;
1549 bfd
**ptr
= &obfd
->archive_head
;
1551 char *dir
= make_tempname (bfd_get_filename (obfd
));
1553 /* Make a temp directory to hold the contents. */
1554 if (MKDIR (dir
, 0700) != 0)
1555 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1556 dir
, strerror (errno
));
1558 obfd
->has_armap
= ibfd
->has_armap
;
1562 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1564 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1565 RETURN_NONFATAL (bfd_get_filename (obfd
));
1567 while (!status
&& this_element
!= NULL
)
1573 int stat_status
= 0;
1574 bfd_boolean
delete = TRUE
;
1576 /* Create an output file for this member. */
1577 output_name
= concat (dir
, "/",
1578 bfd_get_filename (this_element
), (char *) 0);
1580 /* If the file already exists, make another temp dir. */
1581 if (stat (output_name
, &buf
) >= 0)
1583 output_name
= make_tempname (output_name
);
1584 if (MKDIR (output_name
, 0700) != 0)
1585 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1586 output_name
, strerror (errno
));
1588 l
= xmalloc (sizeof (struct name_list
));
1589 l
->name
= output_name
;
1593 output_name
= concat (output_name
, "/",
1594 bfd_get_filename (this_element
), (char *) 0);
1597 output_bfd
= bfd_openw (output_name
, output_target
);
1600 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1602 if (stat_status
!= 0)
1603 non_fatal (_("internal stat error on %s"),
1604 bfd_get_filename (this_element
));
1607 l
= xmalloc (sizeof (struct name_list
));
1608 l
->name
= output_name
;
1612 if (output_bfd
== NULL
)
1613 RETURN_NONFATAL (output_name
);
1615 if (bfd_check_format (this_element
, bfd_object
))
1616 delete = ! copy_object (this_element
, output_bfd
);
1618 if (!bfd_close (output_bfd
))
1620 bfd_nonfatal (bfd_get_filename (output_bfd
));
1621 /* Error in new object file. Don't change archive. */
1627 unlink (output_name
);
1632 if (preserve_dates
&& stat_status
== 0)
1633 set_times (output_name
, &buf
);
1635 /* Open the newly output file and attach to our list. */
1636 output_bfd
= bfd_openr (output_name
, output_target
);
1638 l
->obfd
= output_bfd
;
1641 ptr
= &output_bfd
->next
;
1643 last_element
= this_element
;
1645 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1647 bfd_close (last_element
);
1652 if (!bfd_close (obfd
))
1653 RETURN_NONFATAL (bfd_get_filename (obfd
));
1655 if (!bfd_close (ibfd
))
1656 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1658 /* Delete all the files that we opened. */
1659 for (l
= list
; l
!= NULL
; l
= l
->next
)
1661 if (l
->obfd
== NULL
)
1665 bfd_close (l
->obfd
);
1672 /* The top-level control. */
1675 copy_file (const char *input_filename
, const char *output_filename
,
1676 const char *input_target
, const char *output_target
)
1679 char **obj_matching
;
1680 char **core_matching
;
1682 if (get_file_size (input_filename
) < 1)
1688 /* To allow us to do "strip *" without dying on the first
1689 non-object file, failures are nonfatal. */
1690 ibfd
= bfd_openr (input_filename
, input_target
);
1692 RETURN_NONFATAL (input_filename
);
1694 if (bfd_check_format (ibfd
, bfd_archive
))
1698 /* bfd_get_target does not return the correct value until
1699 bfd_check_format succeeds. */
1700 if (output_target
== NULL
)
1701 output_target
= bfd_get_target (ibfd
);
1703 obfd
= bfd_openw (output_filename
, output_target
);
1705 RETURN_NONFATAL (output_filename
);
1707 copy_archive (ibfd
, obfd
, output_target
);
1709 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1715 /* bfd_get_target does not return the correct value until
1716 bfd_check_format succeeds. */
1717 if (output_target
== NULL
)
1718 output_target
= bfd_get_target (ibfd
);
1720 obfd
= bfd_openw (output_filename
, output_target
);
1722 RETURN_NONFATAL (output_filename
);
1724 delete = ! copy_object (ibfd
, obfd
);
1726 if (!bfd_close (obfd
))
1727 RETURN_NONFATAL (output_filename
);
1729 if (!bfd_close (ibfd
))
1730 RETURN_NONFATAL (input_filename
);
1734 unlink (output_filename
);
1740 bfd_error_type obj_error
= bfd_get_error ();
1741 bfd_error_type core_error
;
1743 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1745 /* This probably can't happen.. */
1746 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1747 free (obj_matching
);
1751 core_error
= bfd_get_error ();
1752 /* Report the object error in preference to the core error. */
1753 if (obj_error
!= core_error
)
1754 bfd_set_error (obj_error
);
1756 bfd_nonfatal (input_filename
);
1758 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1760 list_matching_formats (obj_matching
);
1761 free (obj_matching
);
1763 if (core_error
== bfd_error_file_ambiguously_recognized
)
1765 list_matching_formats (core_matching
);
1766 free (core_matching
);
1773 /* Add a name to the section renaming list. */
1776 add_section_rename (const char * old_name
, const char * new_name
,
1779 section_rename
* rename
;
1781 /* Check for conflicts first. */
1782 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1783 if (strcmp (rename
->old_name
, old_name
) == 0)
1785 /* Silently ignore duplicate definitions. */
1786 if (strcmp (rename
->new_name
, new_name
) == 0
1787 && rename
->flags
== flags
)
1790 fatal (_("Multiple renames of section %s"), old_name
);
1793 rename
= xmalloc (sizeof (* rename
));
1795 rename
->old_name
= old_name
;
1796 rename
->new_name
= new_name
;
1797 rename
->flags
= flags
;
1798 rename
->next
= section_rename_list
;
1800 section_rename_list
= rename
;
1803 /* Check the section rename list for a new name of the input section
1804 ISECTION. Return the new name if one is found.
1805 Also set RETURNED_FLAGS to the flags to be used for this section. */
1808 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
1809 flagword
* returned_flags
)
1811 const char * old_name
= bfd_section_name (ibfd
, isection
);
1812 section_rename
* rename
;
1814 /* Default to using the flags of the input section. */
1815 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1817 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1818 if (strcmp (rename
->old_name
, old_name
) == 0)
1820 if (rename
->flags
!= (flagword
) -1)
1821 * returned_flags
= rename
->flags
;
1823 return rename
->new_name
;
1829 /* Once each of the sections is copied, we may still need to do some
1830 finalization work for private section headers. Do that here. */
1833 setup_bfd_headers (bfd
*ibfd
, bfd
*obfd
)
1837 /* Allow the BFD backend to copy any private data it understands
1838 from the input section to the output section. */
1839 if (! bfd_copy_private_header_data (ibfd
, obfd
))
1841 err
= _("private header data");
1845 /* All went well. */
1849 non_fatal (_("%s: error in %s: %s"),
1850 bfd_get_filename (ibfd
),
1851 err
, bfd_errmsg (bfd_get_error ()));
1855 /* Create a section in OBFD with the same
1856 name and attributes as ISECTION in IBFD. */
1859 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1861 bfd
*obfd
= obfdarg
;
1862 struct section_list
*p
;
1870 char *prefix
= NULL
;
1872 if (is_strip_section (ibfd
, isection
))
1875 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1879 /* Get the, possibly new, name of the output section. */
1880 name
= find_section_rename (ibfd
, isection
, & flags
);
1882 /* Prefix sections. */
1883 if ((prefix_alloc_sections_string
)
1884 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1885 prefix
= prefix_alloc_sections_string
;
1886 else if (prefix_sections_string
)
1887 prefix
= prefix_sections_string
;
1893 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1899 osection
= bfd_make_section_anyway (obfd
, name
);
1901 if (osection
== NULL
)
1907 size
= bfd_section_size (ibfd
, isection
);
1909 size
= (size
+ interleave
- 1) / interleave
;
1910 if (! bfd_set_section_size (obfd
, osection
, size
))
1916 vma
= bfd_section_vma (ibfd
, isection
);
1917 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1919 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1922 vma
+= change_section_address
;
1924 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1930 lma
= isection
->lma
;
1931 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1933 if (p
->change_lma
== CHANGE_MODIFY
)
1935 else if (p
->change_lma
== CHANGE_SET
)
1941 lma
+= change_section_address
;
1943 osection
->lma
= lma
;
1945 /* FIXME: This is probably not enough. If we change the LMA we
1946 may have to recompute the header for the file as well. */
1947 if (!bfd_set_section_alignment (obfd
,
1949 bfd_section_alignment (ibfd
, isection
)))
1951 err
= _("alignment");
1955 if (p
!= NULL
&& p
->set_flags
)
1956 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1957 else if (strip_symbols
== STRIP_NONDEBUG
&& (flags
& SEC_ALLOC
) != 0)
1959 flags
&= ~(SEC_HAS_CONTENTS
| SEC_LOAD
);
1960 if (obfd
->xvec
->flavour
== bfd_target_elf_flavour
)
1961 elf_section_type (osection
) = SHT_NOBITS
;
1964 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1970 /* Copy merge entity size. */
1971 osection
->entsize
= isection
->entsize
;
1973 /* This used to be mangle_section; we do here to avoid using
1974 bfd_get_section_by_name since some formats allow multiple
1975 sections with the same name. */
1976 isection
->output_section
= osection
;
1977 isection
->output_offset
= 0;
1979 /* Allow the BFD backend to copy any private data it understands
1980 from the input section to the output section. */
1981 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1982 && strip_symbols
== STRIP_NONDEBUG
)
1983 /* Do not copy the private data when creating an ELF format
1984 debug info file. We do not want the program headers. */
1986 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1988 err
= _("private data");
1992 /* All went well. */
1996 non_fatal (_("%s: section `%s': error in %s: %s"),
1997 bfd_get_filename (ibfd
),
1998 bfd_section_name (ibfd
, isection
),
1999 err
, bfd_errmsg (bfd_get_error ()));
2003 /* Copy the data of input section ISECTION of IBFD
2004 to an output section with the same name in OBFD.
2005 If stripping then don't copy any relocation info. */
2008 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
2010 bfd
*obfd
= obfdarg
;
2011 struct section_list
*p
;
2019 /* If we have already failed earlier on,
2020 do not keep on generating complaints now. */
2024 if (is_strip_section (ibfd
, isection
))
2027 flags
= bfd_get_section_flags (ibfd
, isection
);
2028 if ((flags
& SEC_GROUP
) != 0)
2031 osection
= isection
->output_section
;
2032 size
= bfd_get_section_size (isection
);
2034 if (size
== 0 || osection
== 0)
2037 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
2039 /* Core files do not need to be relocated. */
2040 if (bfd_get_format (obfd
) == bfd_core
)
2044 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2048 /* Do not complain if the target does not support relocations. */
2049 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2052 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2057 bfd_set_reloc (obfd
, osection
, NULL
, 0);
2060 relpp
= xmalloc (relsize
);
2061 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
2063 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2065 if (strip_symbols
== STRIP_ALL
)
2067 /* Remove relocations which are not in
2068 keep_strip_specific_list. */
2069 arelent
**temp_relpp
;
2070 long temp_relcount
= 0;
2073 temp_relpp
= xmalloc (relsize
);
2074 for (i
= 0; i
< relcount
; i
++)
2075 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
2076 keep_specific_list
))
2077 temp_relpp
[temp_relcount
++] = relpp
[i
];
2078 relcount
= temp_relcount
;
2083 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
2088 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
2089 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
2091 void *memhunk
= xmalloc (size
);
2093 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
2094 RETURN_NONFATAL (bfd_get_filename (ibfd
));
2098 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2099 char *from
= (char *) memhunk
+ copy_byte
;
2101 char *end
= (char *) memhunk
+ size
;
2103 for (; from
< end
; from
+= interleave
)
2106 size
= (size
+ interleave
- 1 - copy_byte
) / interleave
;
2107 osection
->lma
/= interleave
;
2110 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2111 RETURN_NONFATAL (bfd_get_filename (obfd
));
2115 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
2117 void *memhunk
= xmalloc (size
);
2119 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2120 flag--they can just remove the section entirely and add it
2121 back again. However, we do permit them to turn on the
2122 SEC_HAS_CONTENTS flag, and take it to mean that the section
2123 contents should be zeroed out. */
2125 memset (memhunk
, 0, size
);
2126 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2127 RETURN_NONFATAL (bfd_get_filename (obfd
));
2132 /* Get all the sections. This is used when --gap-fill or --pad-to is
2136 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2138 asection
***secppp
= secppparg
;
2140 **secppp
= osection
;
2144 /* Sort sections by VMA. This is called via qsort, and is used when
2145 --gap-fill or --pad-to is used. We force non loadable or empty
2146 sections to the front, where they are easier to ignore. */
2149 compare_section_lma (const void *arg1
, const void *arg2
)
2151 const asection
*const *sec1
= arg1
;
2152 const asection
*const *sec2
= arg2
;
2153 flagword flags1
, flags2
;
2155 /* Sort non loadable sections to the front. */
2156 flags1
= (*sec1
)->flags
;
2157 flags2
= (*sec2
)->flags
;
2158 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2159 || (flags1
& SEC_LOAD
) == 0)
2161 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2162 && (flags2
& SEC_LOAD
) != 0)
2167 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2168 || (flags2
& SEC_LOAD
) == 0)
2172 /* Sort sections by LMA. */
2173 if ((*sec1
)->lma
> (*sec2
)->lma
)
2175 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2178 /* Sort sections with the same LMA by size. */
2179 if (bfd_get_section_size (*sec1
) > bfd_get_section_size (*sec2
))
2181 else if (bfd_get_section_size (*sec1
) < bfd_get_section_size (*sec2
))
2187 /* Mark all the symbols which will be used in output relocations with
2188 the BSF_KEEP flag so that those symbols will not be stripped.
2190 Ignore relocations which will not appear in the output file. */
2193 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2195 asymbol
**symbols
= symbolsarg
;
2200 /* Ignore an input section with no corresponding output section. */
2201 if (isection
->output_section
== NULL
)
2204 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2207 /* Do not complain if the target does not support relocations. */
2208 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2210 bfd_fatal (bfd_get_filename (ibfd
));
2216 relpp
= xmalloc (relsize
);
2217 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2219 bfd_fatal (bfd_get_filename (ibfd
));
2221 /* Examine each symbol used in a relocation. If it's not one of the
2222 special bfd section symbols, then mark it with BSF_KEEP. */
2223 for (i
= 0; i
< relcount
; i
++)
2225 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2226 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2227 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2228 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2235 /* Write out debugging information. */
2238 write_debugging_info (bfd
*obfd
, void *dhandle
,
2239 long *symcountp ATTRIBUTE_UNUSED
,
2240 asymbol
***symppp ATTRIBUTE_UNUSED
)
2242 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2243 return write_ieee_debugging_info (obfd
, dhandle
);
2245 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2246 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2248 bfd_byte
*syms
, *strings
;
2249 bfd_size_type symsize
, stringsize
;
2250 asection
*stabsec
, *stabstrsec
;
2252 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2257 stabsec
= bfd_make_section (obfd
, ".stab");
2258 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2260 || stabstrsec
== NULL
2261 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2262 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2263 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2264 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2265 || ! bfd_set_section_flags (obfd
, stabsec
,
2269 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2274 non_fatal (_("%s: can't create debugging section: %s"),
2275 bfd_get_filename (obfd
),
2276 bfd_errmsg (bfd_get_error ()));
2280 /* We can get away with setting the section contents now because
2281 the next thing the caller is going to do is copy over the
2282 real sections. We may someday have to split the contents
2283 setting out of this function. */
2284 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2285 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2288 non_fatal (_("%s: can't set debugging section contents: %s"),
2289 bfd_get_filename (obfd
),
2290 bfd_errmsg (bfd_get_error ()));
2297 non_fatal (_("%s: don't know how to write debugging information for %s"),
2298 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2303 strip_main (int argc
, char *argv
[])
2305 char *input_target
= NULL
;
2306 char *output_target
= NULL
;
2307 bfd_boolean show_version
= FALSE
;
2308 bfd_boolean formats_info
= FALSE
;
2311 struct section_list
*p
;
2312 char *output_file
= NULL
;
2314 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2315 strip_options
, (int *) 0)) != EOF
)
2320 input_target
= optarg
;
2323 output_target
= optarg
;
2326 input_target
= output_target
= optarg
;
2329 p
= find_section_list (optarg
, TRUE
);
2331 sections_removed
= TRUE
;
2334 strip_symbols
= STRIP_ALL
;
2338 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2339 strip_symbols
= STRIP_DEBUG
;
2341 case OPTION_STRIP_UNNEEDED
:
2342 strip_symbols
= STRIP_UNNEEDED
;
2345 add_specific_symbol (optarg
, &keep_specific_list
);
2348 add_specific_symbol (optarg
, &strip_specific_list
);
2351 output_file
= optarg
;
2354 preserve_dates
= TRUE
;
2357 discard_locals
= LOCALS_ALL
;
2360 discard_locals
= LOCALS_START_L
;
2366 show_version
= TRUE
;
2368 case OPTION_FORMATS_INFO
:
2369 formats_info
= TRUE
;
2371 case OPTION_ONLY_KEEP_DEBUG
:
2372 strip_symbols
= STRIP_NONDEBUG
;
2375 /* We've been given a long option. */
2382 strip_usage (stdout
, 0);
2384 strip_usage (stderr
, 1);
2395 print_version ("strip");
2397 /* Default is to strip all symbols. */
2398 if (strip_symbols
== STRIP_UNDEF
2399 && discard_locals
== LOCALS_UNDEF
2400 && strip_specific_list
== NULL
)
2401 strip_symbols
= STRIP_ALL
;
2403 if (output_target
== NULL
)
2404 output_target
= input_target
;
2408 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2409 strip_usage (stderr
, 1);
2411 for (; i
< argc
; i
++)
2413 int hold_status
= status
;
2414 struct stat statbuf
;
2417 if (get_file_size (argv
[i
]) < 1)
2421 /* No need to check the return value of stat().
2422 It has already been checked in get_file_size(). */
2423 stat (argv
[i
], &statbuf
);
2425 if (output_file
!= NULL
)
2426 tmpname
= output_file
;
2428 tmpname
= make_tempname (argv
[i
]);
2431 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2435 set_times (tmpname
, &statbuf
);
2436 if (output_file
== NULL
)
2437 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2438 status
= hold_status
;
2442 if (output_file
== NULL
)
2450 copy_main (int argc
, char *argv
[])
2452 char * binary_architecture
= NULL
;
2453 char *input_filename
= NULL
;
2454 char *output_filename
= NULL
;
2455 char *input_target
= NULL
;
2456 char *output_target
= NULL
;
2457 bfd_boolean show_version
= FALSE
;
2458 bfd_boolean change_warn
= TRUE
;
2459 bfd_boolean formats_info
= FALSE
;
2461 struct section_list
*p
;
2462 struct stat statbuf
;
2464 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2465 copy_options
, (int *) 0)) != EOF
)
2470 copy_byte
= atoi (optarg
);
2472 fatal (_("byte number must be non-negative"));
2476 binary_architecture
= optarg
;
2480 interleave
= atoi (optarg
);
2482 fatal (_("interleave must be positive"));
2486 case 's': /* "source" - 'I' is preferred */
2487 input_target
= optarg
;
2491 case 'd': /* "destination" - 'O' is preferred */
2492 output_target
= optarg
;
2496 input_target
= output_target
= optarg
;
2500 p
= find_section_list (optarg
, TRUE
);
2502 fatal (_("%s both copied and removed"), optarg
);
2504 sections_copied
= TRUE
;
2508 p
= find_section_list (optarg
, TRUE
);
2510 fatal (_("%s both copied and removed"), optarg
);
2512 sections_removed
= TRUE
;
2516 strip_symbols
= STRIP_ALL
;
2520 strip_symbols
= STRIP_DEBUG
;
2523 case OPTION_STRIP_UNNEEDED
:
2524 strip_symbols
= STRIP_UNNEEDED
;
2527 case OPTION_ONLY_KEEP_DEBUG
:
2528 strip_symbols
= STRIP_NONDEBUG
;
2531 case OPTION_ADD_GNU_DEBUGLINK
:
2532 gnu_debuglink_filename
= optarg
;
2536 add_specific_symbol (optarg
, &keep_specific_list
);
2540 add_specific_symbol (optarg
, &strip_specific_list
);
2543 case OPTION_STRIP_UNNEEDED_SYMBOL
:
2544 add_specific_symbol (optarg
, &strip_unneeded_list
);
2548 add_specific_symbol (optarg
, &localize_specific_list
);
2552 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2556 add_specific_symbol (optarg
, &weaken_specific_list
);
2560 preserve_dates
= TRUE
;
2568 discard_locals
= LOCALS_ALL
;
2572 discard_locals
= LOCALS_START_L
;
2580 show_version
= TRUE
;
2583 case OPTION_FORMATS_INFO
:
2584 formats_info
= TRUE
;
2591 case OPTION_ADD_SECTION
:
2595 struct section_add
*pa
;
2600 s
= strchr (optarg
, '=');
2603 fatal (_("bad format for %s"), "--add-section");
2605 size
= get_file_size (s
+ 1);
2609 pa
= xmalloc (sizeof (struct section_add
));
2612 name
= xmalloc (len
+ 1);
2613 strncpy (name
, optarg
, len
);
2617 pa
->filename
= s
+ 1;
2619 pa
->contents
= xmalloc (size
);
2621 f
= fopen (pa
->filename
, FOPEN_RB
);
2624 fatal (_("cannot open: %s: %s"),
2625 pa
->filename
, strerror (errno
));
2627 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2629 fatal (_("%s: fread failed"), pa
->filename
);
2633 pa
->next
= add_sections
;
2638 case OPTION_CHANGE_START
:
2639 change_start
= parse_vma (optarg
, "--change-start");
2642 case OPTION_CHANGE_SECTION_ADDRESS
:
2643 case OPTION_CHANGE_SECTION_LMA
:
2644 case OPTION_CHANGE_SECTION_VMA
:
2649 char *option
= NULL
;
2651 enum change_action what
= CHANGE_IGNORE
;
2655 case OPTION_CHANGE_SECTION_ADDRESS
:
2656 option
= "--change-section-address";
2658 case OPTION_CHANGE_SECTION_LMA
:
2659 option
= "--change-section-lma";
2661 case OPTION_CHANGE_SECTION_VMA
:
2662 option
= "--change-section-vma";
2666 s
= strchr (optarg
, '=');
2669 s
= strchr (optarg
, '+');
2672 s
= strchr (optarg
, '-');
2674 fatal (_("bad format for %s"), option
);
2679 name
= xmalloc (len
+ 1);
2680 strncpy (name
, optarg
, len
);
2683 p
= find_section_list (name
, TRUE
);
2685 val
= parse_vma (s
+ 1, option
);
2689 case '=': what
= CHANGE_SET
; break;
2690 case '-': val
= - val
; /* Drop through. */
2691 case '+': what
= CHANGE_MODIFY
; break;
2696 case OPTION_CHANGE_SECTION_ADDRESS
:
2697 p
->change_vma
= what
;
2701 case OPTION_CHANGE_SECTION_LMA
:
2702 p
->change_lma
= what
;
2706 case OPTION_CHANGE_SECTION_VMA
:
2707 p
->change_vma
= what
;
2714 case OPTION_CHANGE_ADDRESSES
:
2715 change_section_address
= parse_vma (optarg
, "--change-addresses");
2716 change_start
= change_section_address
;
2719 case OPTION_CHANGE_WARNINGS
:
2723 case OPTION_CHANGE_LEADING_CHAR
:
2724 change_leading_char
= TRUE
;
2727 case OPTION_DEBUGGING
:
2728 convert_debugging
= TRUE
;
2731 case OPTION_GAP_FILL
:
2733 bfd_vma gap_fill_vma
;
2735 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2736 gap_fill
= (bfd_byte
) gap_fill_vma
;
2737 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2741 sprintf_vma (buff
, gap_fill_vma
);
2743 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2746 gap_fill_set
= TRUE
;
2750 case OPTION_NO_CHANGE_WARNINGS
:
2751 change_warn
= FALSE
;
2755 pad_to
= parse_vma (optarg
, "--pad-to");
2759 case OPTION_REMOVE_LEADING_CHAR
:
2760 remove_leading_char
= TRUE
;
2763 case OPTION_REDEFINE_SYM
:
2765 /* Push this redefinition onto redefine_symbol_list. */
2769 const char *nextarg
;
2770 char *source
, *target
;
2772 s
= strchr (optarg
, '=');
2774 fatal (_("bad format for %s"), "--redefine-sym");
2777 source
= xmalloc (len
+ 1);
2778 strncpy (source
, optarg
, len
);
2782 len
= strlen (nextarg
);
2783 target
= xmalloc (len
+ 1);
2784 strcpy (target
, nextarg
);
2786 redefine_list_append ("--redefine-sym", source
, target
);
2793 case OPTION_REDEFINE_SYMS
:
2794 add_redefine_syms_file (optarg
);
2797 case OPTION_SET_SECTION_FLAGS
:
2803 s
= strchr (optarg
, '=');
2805 fatal (_("bad format for %s"), "--set-section-flags");
2808 name
= xmalloc (len
+ 1);
2809 strncpy (name
, optarg
, len
);
2812 p
= find_section_list (name
, TRUE
);
2814 p
->set_flags
= TRUE
;
2815 p
->flags
= parse_flags (s
+ 1);
2819 case OPTION_RENAME_SECTION
:
2822 const char *eq
, *fl
;
2827 eq
= strchr (optarg
, '=');
2829 fatal (_("bad format for %s"), "--rename-section");
2833 fatal (_("bad format for %s"), "--rename-section");
2835 old_name
= xmalloc (len
+ 1);
2836 strncpy (old_name
, optarg
, len
);
2840 fl
= strchr (eq
, ',');
2843 flags
= parse_flags (fl
+ 1);
2853 fatal (_("bad format for %s"), "--rename-section");
2855 new_name
= xmalloc (len
+ 1);
2856 strncpy (new_name
, eq
, len
);
2859 add_section_rename (old_name
, new_name
, flags
);
2863 case OPTION_SET_START
:
2864 set_start
= parse_vma (optarg
, "--set-start");
2865 set_start_set
= TRUE
;
2868 case OPTION_SREC_LEN
:
2869 Chunk
= parse_vma (optarg
, "--srec-len");
2872 case OPTION_SREC_FORCES3
:
2876 case OPTION_STRIP_SYMBOLS
:
2877 add_specific_symbols (optarg
, &strip_specific_list
);
2880 case OPTION_STRIP_UNNEEDED_SYMBOLS
:
2881 add_specific_symbols (optarg
, &strip_unneeded_list
);
2884 case OPTION_KEEP_SYMBOLS
:
2885 add_specific_symbols (optarg
, &keep_specific_list
);
2888 case OPTION_LOCALIZE_SYMBOLS
:
2889 add_specific_symbols (optarg
, &localize_specific_list
);
2892 case OPTION_KEEPGLOBAL_SYMBOLS
:
2893 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2896 case OPTION_WEAKEN_SYMBOLS
:
2897 add_specific_symbols (optarg
, &weaken_specific_list
);
2900 case OPTION_ALT_MACH_CODE
:
2901 use_alt_mach_code
= atoi (optarg
);
2902 if (use_alt_mach_code
<= 0)
2903 fatal (_("alternate machine code index must be positive"));
2906 case OPTION_PREFIX_SYMBOLS
:
2907 prefix_symbols_string
= optarg
;
2910 case OPTION_PREFIX_SECTIONS
:
2911 prefix_sections_string
= optarg
;
2914 case OPTION_PREFIX_ALLOC_SECTIONS
:
2915 prefix_alloc_sections_string
= optarg
;
2918 case OPTION_READONLY_TEXT
:
2919 bfd_flags_to_set
|= WP_TEXT
;
2920 bfd_flags_to_clear
&= ~WP_TEXT
;
2923 case OPTION_WRITABLE_TEXT
:
2924 bfd_flags_to_clear
|= WP_TEXT
;
2925 bfd_flags_to_set
&= ~WP_TEXT
;
2929 bfd_flags_to_set
|= D_PAGED
;
2930 bfd_flags_to_clear
&= ~D_PAGED
;
2934 bfd_flags_to_clear
|= D_PAGED
;
2935 bfd_flags_to_set
&= ~D_PAGED
;
2939 /* We've been given a long option. */
2944 copy_usage (stdout
, 0);
2947 copy_usage (stderr
, 1);
2958 print_version ("objcopy");
2960 if (copy_byte
>= interleave
)
2961 fatal (_("byte number must be less than interleave"));
2963 if (optind
== argc
|| optind
+ 2 < argc
)
2964 copy_usage (stderr
, 1);
2966 input_filename
= argv
[optind
];
2967 if (optind
+ 1 < argc
)
2968 output_filename
= argv
[optind
+ 1];
2970 /* Default is to strip no symbols. */
2971 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2972 strip_symbols
= STRIP_NONE
;
2974 if (output_target
== NULL
)
2975 output_target
= input_target
;
2977 if (binary_architecture
!= NULL
)
2979 if (input_target
&& strcmp (input_target
, "binary") == 0)
2981 const bfd_arch_info_type
* temp_arch_info
;
2983 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2985 if (temp_arch_info
!= NULL
)
2987 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2988 bfd_external_machine
= temp_arch_info
->mach
;
2991 fatal (_("architecture %s unknown"), binary_architecture
);
2995 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2996 non_fatal (_(" Argument %s ignored"), binary_architecture
);
3001 if (stat (input_filename
, & statbuf
) < 0)
3002 fatal (_("warning: could not locate '%s'. System error message: %s"),
3003 input_filename
, strerror (errno
));
3005 /* If there is no destination file, or the source and destination files
3006 are the same, then create a temp and rename the result into the input. */
3007 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
3009 char *tmpname
= make_tempname (input_filename
);
3011 copy_file (input_filename
, tmpname
, input_target
, output_target
);
3015 set_times (tmpname
, &statbuf
);
3016 smart_rename (tmpname
, input_filename
, preserve_dates
);
3023 copy_file (input_filename
, output_filename
, input_target
, output_target
);
3025 if (status
== 0 && preserve_dates
)
3026 set_times (output_filename
, &statbuf
);
3031 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
3035 if (p
->change_vma
!= CHANGE_IGNORE
)
3039 sprintf_vma (buff
, p
->vma_val
);
3041 /* xgettext:c-format */
3042 non_fatal (_("%s %s%c0x%s never used"),
3043 "--change-section-vma",
3045 p
->change_vma
== CHANGE_SET
? '=' : '+',
3049 if (p
->change_lma
!= CHANGE_IGNORE
)
3053 sprintf_vma (buff
, p
->lma_val
);
3055 /* xgettext:c-format */
3056 non_fatal (_("%s %s%c0x%s never used"),
3057 "--change-section-lma",
3059 p
->change_lma
== CHANGE_SET
? '=' : '+',
3070 main (int argc
, char *argv
[])
3072 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3073 setlocale (LC_MESSAGES
, "");
3075 #if defined (HAVE_SETLOCALE)
3076 setlocale (LC_CTYPE
, "");
3078 bindtextdomain (PACKAGE
, LOCALEDIR
);
3079 textdomain (PACKAGE
);
3081 program_name
= argv
[0];
3082 xmalloc_set_program_name (program_name
);
3084 START_PROGRESS (program_name
, 0);
3086 strip_symbols
= STRIP_UNDEF
;
3087 discard_locals
= LOCALS_UNDEF
;
3090 set_default_bfd_target ();
3094 int i
= strlen (program_name
);
3095 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3096 /* Drop the .exe suffix, if any. */
3097 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
3100 program_name
[i
] = '\0';
3103 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
3107 strip_main (argc
, argv
);
3109 copy_main (argc
, argv
);
3111 END_PROGRESS (program_name
);