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"
33 /* A list of symbols to explicitly strip out, or to keep. A linked
34 list is good enough for a small number from the command line, but
35 this will slow things down a lot if many symbols are being
44 /* A list to support redefine_sym. */
49 struct redefine_node
*next
;
52 typedef struct section_rename
54 const char * old_name
;
55 const char * new_name
;
57 struct section_rename
* next
;
61 /* List of sections to be renamed. */
62 static section_rename
*section_rename_list
;
64 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
66 static asymbol
**isympp
= NULL
; /* Input symbols. */
67 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
69 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
70 static int copy_byte
= -1;
71 static int interleave
= 4;
73 static bfd_boolean verbose
; /* Print file and target names. */
74 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
75 static int status
= 0; /* Exit status. */
80 STRIP_NONE
, /* Don't strip. */
81 STRIP_DEBUG
, /* Strip all debugger symbols. */
82 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
83 STRIP_NONDEBUG
, /* Strip everything but debug info. */
84 STRIP_ALL
/* Strip all symbols. */
87 /* Which symbols to remove. */
88 static enum strip_action strip_symbols
;
93 LOCALS_START_L
, /* Discard locals starting with L. */
94 LOCALS_ALL
/* Discard all locals. */
97 /* Which local symbols to remove. Overrides STRIP_ALL. */
98 static enum locals_action discard_locals
;
100 /* What kind of change to perform. */
108 /* Structure used to hold lists of sections and actions to take. */
111 struct section_list
* next
; /* Next section to change. */
112 const char * name
; /* Section name. */
113 bfd_boolean used
; /* Whether this entry was used. */
114 bfd_boolean remove
; /* Whether to remove this section. */
115 bfd_boolean copy
; /* Whether to copy this section. */
116 enum change_action change_vma
;/* Whether to change or set VMA. */
117 bfd_vma vma_val
; /* Amount to change by or set to. */
118 enum change_action change_lma
;/* Whether to change or set LMA. */
119 bfd_vma lma_val
; /* Amount to change by or set to. */
120 bfd_boolean set_flags
; /* Whether to set the section flags. */
121 flagword flags
; /* What to set the section flags to. */
124 static struct section_list
*change_sections
;
126 /* TRUE if some sections are to be removed. */
127 static bfd_boolean sections_removed
;
129 /* TRUE if only some sections are to be copied. */
130 static bfd_boolean sections_copied
;
132 /* Changes to the start address. */
133 static bfd_vma change_start
= 0;
134 static bfd_boolean set_start_set
= FALSE
;
135 static bfd_vma set_start
;
137 /* Changes to section addresses. */
138 static bfd_vma change_section_address
= 0;
140 /* Filling gaps between sections. */
141 static bfd_boolean gap_fill_set
= FALSE
;
142 static bfd_byte gap_fill
= 0;
144 /* Pad to a given address. */
145 static bfd_boolean pad_to_set
= FALSE
;
146 static bfd_vma pad_to
;
148 /* Use alternate machine code? */
149 static int use_alt_mach_code
= 0;
151 /* List of sections to add. */
154 /* Next section to add. */
155 struct section_add
*next
;
156 /* Name of section to add. */
158 /* Name of file holding section contents. */
159 const char *filename
;
162 /* Contents of file. */
164 /* BFD section, after it has been added. */
168 /* List of sections to add to the output BFD. */
169 static struct section_add
*add_sections
;
171 /* If non-NULL the argument to --add-gnu-debuglink.
172 This should be the filename to store in the .gnu_debuglink section. */
173 static const char * gnu_debuglink_filename
= NULL
;
175 /* Whether to convert debugging information. */
176 static bfd_boolean convert_debugging
= FALSE
;
178 /* Whether to change the leading character in symbol names. */
179 static bfd_boolean change_leading_char
= FALSE
;
181 /* Whether to remove the leading character from global symbol names. */
182 static bfd_boolean remove_leading_char
= FALSE
;
184 /* Whether to permit wildcard in symbol comparasion. */
185 static bfd_boolean wildcard
= FALSE
;
187 /* List of symbols to strip, keep, localize, keep-global, weaken,
189 static struct symlist
*strip_specific_list
= NULL
;
190 static struct symlist
*keep_specific_list
= NULL
;
191 static struct symlist
*localize_specific_list
= NULL
;
192 static struct symlist
*keepglobal_specific_list
= NULL
;
193 static struct symlist
*weaken_specific_list
= NULL
;
194 static struct redefine_node
*redefine_sym_list
= NULL
;
196 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
197 static bfd_boolean weaken
= FALSE
;
199 /* Prefix symbols/sections. */
200 static char *prefix_symbols_string
= 0;
201 static char *prefix_sections_string
= 0;
202 static char *prefix_alloc_sections_string
= 0;
204 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
205 enum command_line_switch
207 OPTION_ADD_SECTION
=150,
208 OPTION_CHANGE_ADDRESSES
,
209 OPTION_CHANGE_LEADING_CHAR
,
211 OPTION_CHANGE_SECTION_ADDRESS
,
212 OPTION_CHANGE_SECTION_LMA
,
213 OPTION_CHANGE_SECTION_VMA
,
214 OPTION_CHANGE_WARNINGS
,
217 OPTION_NO_CHANGE_WARNINGS
,
219 OPTION_REMOVE_LEADING_CHAR
,
220 OPTION_SET_SECTION_FLAGS
,
222 OPTION_STRIP_UNNEEDED
,
225 OPTION_REDEFINE_SYMS
,
228 OPTION_STRIP_SYMBOLS
,
230 OPTION_LOCALIZE_SYMBOLS
,
231 OPTION_KEEPGLOBAL_SYMBOLS
,
232 OPTION_WEAKEN_SYMBOLS
,
233 OPTION_RENAME_SECTION
,
234 OPTION_ALT_MACH_CODE
,
235 OPTION_PREFIX_SYMBOLS
,
236 OPTION_PREFIX_SECTIONS
,
237 OPTION_PREFIX_ALLOC_SECTIONS
,
239 OPTION_ADD_GNU_DEBUGLINK
,
240 OPTION_ONLY_KEEP_DEBUG
243 /* Options to handle if running as "strip". */
245 static struct option strip_options
[] =
247 {"discard-all", no_argument
, 0, 'x'},
248 {"discard-locals", no_argument
, 0, 'X'},
249 {"format", required_argument
, 0, 'F'}, /* Obsolete */
250 {"help", no_argument
, 0, 'h'},
251 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
252 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
253 {"input-target", required_argument
, 0, 'I'},
254 {"keep-symbol", required_argument
, 0, 'K'},
255 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
256 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
257 {"output-target", required_argument
, 0, 'O'},
258 {"output-file", required_argument
, 0, 'o'},
259 {"preserve-dates", no_argument
, 0, 'p'},
260 {"remove-section", required_argument
, 0, 'R'},
261 {"strip-all", no_argument
, 0, 's'},
262 {"strip-debug", no_argument
, 0, 'S'},
263 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
264 {"strip-symbol", required_argument
, 0, 'N'},
265 {"target", required_argument
, 0, 'F'},
266 {"verbose", no_argument
, 0, 'v'},
267 {"version", no_argument
, 0, 'V'},
268 {"wildcard", no_argument
, 0, 'w'},
269 {0, no_argument
, 0, 0}
272 /* Options to handle if running as "objcopy". */
274 static struct option copy_options
[] =
276 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
277 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
278 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
279 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
280 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
281 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
282 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
283 {"binary-architecture", required_argument
, 0, 'B'},
284 {"byte", required_argument
, 0, 'b'},
285 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
286 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
287 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
288 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
289 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
290 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
291 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
292 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
293 {"discard-all", no_argument
, 0, 'x'},
294 {"discard-locals", no_argument
, 0, 'X'},
295 {"format", required_argument
, 0, 'F'}, /* Obsolete */
296 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
297 {"help", no_argument
, 0, 'h'},
298 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
299 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
300 {"input-target", required_argument
, 0, 'I'},
301 {"interleave", required_argument
, 0, 'i'},
302 {"keep-global-symbol", required_argument
, 0, 'G'},
303 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
304 {"keep-symbol", required_argument
, 0, 'K'},
305 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
306 {"localize-symbol", required_argument
, 0, 'L'},
307 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
308 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
309 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
310 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
311 {"only-section", required_argument
, 0, 'j'},
312 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
313 {"output-target", required_argument
, 0, 'O'},
314 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
315 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
316 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
317 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
318 {"preserve-dates", no_argument
, 0, 'p'},
319 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
320 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
321 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
322 {"remove-section", required_argument
, 0, 'R'},
323 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
324 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
325 {"set-start", required_argument
, 0, OPTION_SET_START
},
326 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
327 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
328 {"strip-all", no_argument
, 0, 'S'},
329 {"strip-debug", no_argument
, 0, 'g'},
330 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
331 {"strip-symbol", required_argument
, 0, 'N'},
332 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
333 {"target", required_argument
, 0, 'F'},
334 {"verbose", no_argument
, 0, 'v'},
335 {"version", no_argument
, 0, 'V'},
336 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
337 {"weaken-symbol", required_argument
, 0, 'W'},
338 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
339 {"wildcard", no_argument
, 0, 'w'},
340 {0, no_argument
, 0, 0}
344 extern char *program_name
;
346 /* This flag distinguishes between strip and objcopy:
347 1 means this is 'strip'; 0 means this is 'objcopy'.
348 -1 means if we should use argv[0] to decide. */
351 /* The maximum length of an S record. This variable is declared in srec.c
352 and can be modified by the --srec-len parameter. */
353 extern unsigned int Chunk
;
355 /* Restrict the generation of Srecords to type S3 only.
356 This variable is declare in bfd/srec.c and can be toggled
357 on by the --srec-forceS3 command line switch. */
358 extern bfd_boolean S3Forced
;
360 /* Defined in bfd/binary.c. Used to set architecture and machine of input
362 extern enum bfd_architecture bfd_external_binary_architecture
;
363 extern unsigned long bfd_external_machine
;
365 /* Forward declarations. */
366 static void setup_section (bfd
*, asection
*, void *);
367 static void copy_section (bfd
*, asection
*, void *);
368 static void get_sections (bfd
*, asection
*, void *);
369 static int compare_section_lma (const void *, const void *);
370 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
371 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
372 static const char *lookup_sym_redefinition (const char *);
375 copy_usage (FILE *stream
, int exit_status
)
377 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
378 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
379 fprintf (stream
, _(" The options are:\n"));
380 fprintf (stream
, _("\
381 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
382 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
383 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
384 -F --target <bfdname> Set both input and output format to <bfdname>\n\
385 --debugging Convert debugging information, if possible\n\
386 -p --preserve-dates Copy modified/access timestamps to the output\n\
387 -j --only-section <name> Only copy section <name> into the output\n\
388 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
389 -R --remove-section <name> Remove section <name> from the output\n\
390 -S --strip-all Remove all symbol and relocation information\n\
391 -g --strip-debug Remove all debugging symbols & sections\n\
392 --strip-unneeded Remove all symbols not needed by relocations\n\
393 -N --strip-symbol <name> Do not copy symbol <name>\n\
394 -K --keep-symbol <name> Only copy symbol <name>\n\
395 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
396 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
397 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
398 --weaken Force all global symbols to be marked as weak\n\
399 -w --wildcard Permit wildcard in symbol comparasion\n\
400 -x --discard-all Remove all non-global symbols\n\
401 -X --discard-locals Remove any compiler-generated symbols\n\
402 -i --interleave <number> Only copy one out of every <number> bytes\n\
403 -b --byte <num> Select byte <num> in every interleaved block\n\
404 --gap-fill <val> Fill gaps between sections with <val>\n\
405 --pad-to <addr> Pad the last section up to address <addr>\n\
406 --set-start <addr> Set the start address to <addr>\n\
407 {--change-start|--adjust-start} <incr>\n\
408 Add <incr> to the start address\n\
409 {--change-addresses|--adjust-vma} <incr>\n\
410 Add <incr> to LMA, VMA and start addresses\n\
411 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
412 Change LMA and VMA of section <name> by <val>\n\
413 --change-section-lma <name>{=|+|-}<val>\n\
414 Change the LMA of section <name> by <val>\n\
415 --change-section-vma <name>{=|+|-}<val>\n\
416 Change the VMA of section <name> by <val>\n\
417 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
418 Warn if a named section does not exist\n\
419 --set-section-flags <name>=<flags>\n\
420 Set section <name>'s properties to <flags>\n\
421 --add-section <name>=<file> Add section <name> found in <file> to output\n\
422 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
423 --change-leading-char Force output format's leading character style\n\
424 --remove-leading-char Remove leading character from global symbols\n\
425 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
426 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
428 --srec-len <number> Restrict the length of generated Srecords\n\
429 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
430 --strip-symbols <file> -N for all symbols listed in <file>\n\
431 --keep-symbols <file> -K for all symbols listed in <file>\n\
432 --localize-symbols <file> -L for all symbols listed in <file>\n\
433 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
434 --weaken-symbols <file> -W for all symbols listed in <file>\n\
435 --alt-machine-code <index> Use alternate machine code for output\n\
436 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
437 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
438 --prefix-alloc-sections <prefix>\n\
439 Add <prefix> to start of every allocatable\n\
441 -v --verbose List all object files modified\n\
442 -V --version Display this program's version number\n\
443 -h --help Display this output\n\
444 --info List object formats & architectures supported\n\
446 list_supported_targets (program_name
, stream
);
447 if (exit_status
== 0)
448 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
453 strip_usage (FILE *stream
, int exit_status
)
455 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
456 fprintf (stream
, _(" Removes symbols and sections from files\n"));
457 fprintf (stream
, _(" The options are:\n"));
458 fprintf (stream
, _("\
459 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
460 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
461 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
462 -p --preserve-dates Copy modified/access timestamps to the output\n\
463 -R --remove-section=<name> Remove section <name> from the output\n\
464 -s --strip-all Remove all symbol and relocation information\n\
465 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
466 --strip-unneeded Remove all symbols not needed by relocations\n\
467 -N --strip-symbol=<name> Do not copy symbol <name>\n\
468 -K --keep-symbol=<name> Only copy symbol <name>\n\
469 -w --wildcard Permit wildcard in symbol comparasion\n\
470 -x --discard-all Remove all non-global symbols\n\
471 -X --discard-locals Remove any compiler-generated symbols\n\
472 -v --verbose List all object files modified\n\
473 -V --version Display this program's version number\n\
474 -h --help Display this output\n\
475 --info List object formats & architectures supported\n\
476 -o <file> Place stripped output into <file>\n\
479 list_supported_targets (program_name
, stream
);
480 if (exit_status
== 0)
481 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
485 /* Parse section flags into a flagword, with a fatal error if the
486 string can't be parsed. */
489 parse_flags (const char *s
)
499 snext
= strchr (s
, ',');
509 #define PARSE_FLAG(fname,fval) \
510 else if (strncasecmp (fname, s, len) == 0) ret |= fval
511 PARSE_FLAG ("alloc", SEC_ALLOC
);
512 PARSE_FLAG ("load", SEC_LOAD
);
513 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
514 PARSE_FLAG ("readonly", SEC_READONLY
);
515 PARSE_FLAG ("debug", SEC_DEBUGGING
);
516 PARSE_FLAG ("code", SEC_CODE
);
517 PARSE_FLAG ("data", SEC_DATA
);
518 PARSE_FLAG ("rom", SEC_ROM
);
519 PARSE_FLAG ("share", SEC_SHARED
);
520 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
526 copy
= xmalloc (len
+ 1);
527 strncpy (copy
, s
, len
);
529 non_fatal (_("unrecognized section flag `%s'"), copy
);
530 fatal (_("supported flags: %s"),
531 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
541 /* Find and optionally add an entry in the change_sections list. */
543 static struct section_list
*
544 find_section_list (const char *name
, bfd_boolean add
)
546 struct section_list
*p
;
548 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
549 if (strcmp (p
->name
, name
) == 0)
555 p
= xmalloc (sizeof (struct section_list
));
560 p
->change_vma
= CHANGE_IGNORE
;
561 p
->change_lma
= CHANGE_IGNORE
;
564 p
->set_flags
= FALSE
;
567 p
->next
= change_sections
;
573 /* Add a symbol to strip_specific_list. */
576 add_specific_symbol (const char *name
, struct symlist
**list
)
578 struct symlist
*tmp_list
;
580 tmp_list
= xmalloc (sizeof (struct symlist
));
581 tmp_list
->name
= name
;
582 tmp_list
->next
= *list
;
586 /* Add symbols listed in `filename' to strip_specific_list. */
588 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
589 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
592 add_specific_symbols (const char *filename
, struct symlist
**list
)
598 unsigned int line_count
;
600 size
= get_file_size (filename
);
604 buffer
= xmalloc (size
+ 2);
605 f
= fopen (filename
, FOPEN_RT
);
607 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
609 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
610 fatal (_("%s: fread failed"), filename
);
613 buffer
[size
] = '\n';
614 buffer
[size
+ 1] = '\0';
618 for (line
= buffer
; * line
!= '\0'; line
++)
623 int finished
= FALSE
;
625 for (eol
= line
;; eol
++)
631 /* Cope with \n\r. */
639 /* Cope with \r\n. */
650 /* Line comment, Terminate the line here, in case a
651 name is present and then allow the rest of the
652 loop to find the real end of the line. */
664 /* A name may now exist somewhere between 'line' and 'eol'.
665 Strip off leading whitespace and trailing whitespace,
666 then add it to the list. */
667 for (name
= line
; IS_WHITESPACE (* name
); name
++)
669 for (name_end
= name
;
670 (! IS_WHITESPACE (* name_end
))
671 && (! IS_LINE_TERMINATOR (* name_end
));
675 if (! IS_LINE_TERMINATOR (* name_end
))
679 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
682 if (! IS_LINE_TERMINATOR (* extra
))
683 non_fatal (_("Ignoring rubbish found on line %d of %s"),
684 line_count
, filename
);
690 add_specific_symbol (name
, list
);
692 /* Advance line pointer to end of line. The 'eol ++' in the for
693 loop above will then advance us to the start of the next line. */
699 /* See whether a symbol should be stripped or kept based on
700 strip_specific_list and keep_symbols. */
703 is_specified_symbol (const char *name
, struct symlist
*list
)
705 struct symlist
*tmp_list
;
709 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
710 if (*(tmp_list
->name
) != '!')
712 if (!fnmatch (tmp_list
->name
, name
, 0))
717 if (fnmatch (tmp_list
->name
+ 1, name
, 0))
723 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
724 if (strcmp (name
, tmp_list
->name
) == 0)
731 /* See if a section is being removed. */
734 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
736 if (sections_removed
|| sections_copied
)
738 struct section_list
*p
;
740 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
742 if (sections_removed
&& p
!= NULL
&& p
->remove
)
744 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
748 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
750 if (strip_symbols
== STRIP_DEBUG
751 || strip_symbols
== STRIP_UNNEEDED
752 || strip_symbols
== STRIP_ALL
753 || discard_locals
== LOCALS_ALL
754 || convert_debugging
)
757 if (strip_symbols
== STRIP_NONDEBUG
)
761 return strip_symbols
== STRIP_NONDEBUG
? TRUE
: FALSE
;
764 /* Choose which symbol entries to copy; put the result in OSYMS.
765 We don't copy in place, because that confuses the relocs.
766 Return the number of symbols to print. */
769 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
770 asymbol
**isyms
, long symcount
)
772 asymbol
**from
= isyms
, **to
= osyms
;
773 long src_count
= 0, dst_count
= 0;
774 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
777 for (; src_count
< symcount
; src_count
++)
779 asymbol
*sym
= from
[src_count
];
780 flagword flags
= sym
->flags
;
781 char *name
= (char *) bfd_asymbol_name (sym
);
783 bfd_boolean undefined
;
784 bfd_boolean rem_leading_char
;
785 bfd_boolean add_leading_char
;
787 undefined
= bfd_is_und_section (bfd_get_section (sym
));
789 if (redefine_sym_list
)
791 char *old_name
, *new_name
;
793 old_name
= (char *) bfd_asymbol_name (sym
);
794 new_name
= (char *) lookup_sym_redefinition (old_name
);
795 bfd_asymbol_name (sym
) = new_name
;
799 /* Check if we will remove the current leading character. */
801 (name
[0] == bfd_get_symbol_leading_char (abfd
))
802 && (change_leading_char
803 || (remove_leading_char
804 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
806 || bfd_is_com_section (bfd_get_section (sym
)))));
808 /* Check if we will add a new leading character. */
811 && (bfd_get_symbol_leading_char (obfd
) != '\0')
812 && (bfd_get_symbol_leading_char (abfd
) == '\0'
813 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
815 /* Short circuit for change_leading_char if we can do it in-place. */
816 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
818 name
[0] = bfd_get_symbol_leading_char (obfd
);
819 bfd_asymbol_name (sym
) = name
;
820 rem_leading_char
= FALSE
;
821 add_leading_char
= FALSE
;
824 /* Remove leading char. */
825 if (rem_leading_char
)
826 bfd_asymbol_name (sym
) = ++name
;
828 /* Add new leading char and/or prefix. */
829 if (add_leading_char
|| prefix_symbols_string
)
833 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
834 + strlen (name
) + 1);
835 if (add_leading_char
)
836 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
838 if (prefix_symbols_string
)
840 strcpy (ptr
, prefix_symbols_string
);
841 ptr
+= strlen (prefix_symbols_string
);
845 bfd_asymbol_name (sym
) = n
;
849 if (strip_symbols
== STRIP_ALL
)
851 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
852 || ((flags
& BSF_SECTION_SYM
) != 0
853 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
856 else if (relocatable
/* Relocatable file. */
857 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
859 else if (bfd_decode_symclass (sym
) == 'I')
860 /* Global symbols in $idata sections need to be retained
861 even if relocatable is FALSE. External users of the
862 library containing the $idata section may reference these
865 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
866 || (flags
& BSF_WEAK
) != 0
868 || bfd_is_com_section (bfd_get_section (sym
)))
869 keep
= strip_symbols
!= STRIP_UNNEEDED
;
870 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
871 keep
= (strip_symbols
!= STRIP_DEBUG
872 && strip_symbols
!= STRIP_UNNEEDED
873 && ! convert_debugging
);
874 else if (bfd_get_section (sym
)->comdat
)
875 /* COMDAT sections store special information in local
876 symbols, so we cannot risk stripping any of them. */
878 else /* Local symbol. */
879 keep
= (strip_symbols
!= STRIP_UNNEEDED
880 && (discard_locals
!= LOCALS_ALL
881 && (discard_locals
!= LOCALS_START_L
882 || ! bfd_is_local_label (abfd
, sym
))));
884 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
886 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
888 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
891 if (keep
&& (flags
& BSF_GLOBAL
) != 0
892 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
894 sym
->flags
&=~ BSF_GLOBAL
;
895 sym
->flags
|= BSF_WEAK
;
897 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
898 && (is_specified_symbol (name
, localize_specific_list
)
899 || (keepglobal_specific_list
!= NULL
900 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
902 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
903 sym
->flags
|= BSF_LOCAL
;
907 to
[dst_count
++] = sym
;
910 to
[dst_count
] = NULL
;
915 /* Find the redefined name of symbol SOURCE. */
918 lookup_sym_redefinition (const char *source
)
920 struct redefine_node
*list
;
922 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
923 if (strcmp (source
, list
->source
) == 0)
929 /* Add a node to a symbol redefine list. */
932 redefine_list_append (const char *cause
, const char *source
, const char *target
)
934 struct redefine_node
**p
;
935 struct redefine_node
*list
;
936 struct redefine_node
*new_node
;
938 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
940 if (strcmp (source
, list
->source
) == 0)
941 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
944 if (strcmp (target
, list
->target
) == 0)
945 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
949 new_node
= xmalloc (sizeof (struct redefine_node
));
951 new_node
->source
= strdup (source
);
952 new_node
->target
= strdup (target
);
953 new_node
->next
= NULL
;
958 /* Handle the --redefine-syms option. Read lines containing "old new"
959 from the file, and add them to the symbol redefine list. */
962 add_redefine_syms_file (const char *filename
)
971 file
= fopen (filename
, "r");
973 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
974 filename
, strerror (errno
));
977 buf
= xmalloc (bufsize
);
985 /* Collect the input symbol name. */
986 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
994 buf
= xrealloc (buf
, bufsize
);
1002 /* Eat white space between the symbol names. */
1003 while (IS_WHITESPACE (c
))
1005 if (c
== '#' || IS_LINE_TERMINATOR (c
))
1010 /* Collect the output symbol name. */
1012 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
1020 buf
= xrealloc (buf
, bufsize
);
1028 /* Eat white space at end of line. */
1029 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1034 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1035 || c
== '\n' || c
== EOF
)
1038 /* Append the redefinition to the list. */
1040 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1051 fatal (_("%s: garbage at end of line %d"), filename
, lineno
);
1053 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1054 fatal (_("%s: missing new symbol name at line %d"), filename
, lineno
);
1057 /* Eat the rest of the line and finish it. */
1058 while (c
!= '\n' && c
!= EOF
)
1064 fatal (_("%s: premature end of file at line %d"), filename
, lineno
);
1069 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
1073 filter_bytes (char *memhunk
, bfd_size_type
*size
)
1075 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
1077 for (; from
< end
; from
+= interleave
)
1080 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
1081 *size
= (*size
/ interleave
) + 1;
1083 *size
/= interleave
;
1086 /* Copy object file IBFD onto OBFD. */
1089 copy_object (bfd
*ibfd
, bfd
*obfd
)
1093 asection
**osections
= NULL
;
1094 asection
*gnu_debuglink_section
= NULL
;
1095 bfd_size_type
*gaps
= NULL
;
1096 bfd_size_type max_gap
= 0;
1099 enum bfd_architecture iarch
;
1102 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1103 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1104 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1106 fatal (_("Unable to change endianness of input file(s)"));
1110 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1111 RETURN_NONFATAL (bfd_get_filename (obfd
));
1114 printf (_("copy from %s(%s) to %s(%s)\n"),
1115 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
1116 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1121 start
= bfd_get_start_address (ibfd
);
1122 start
+= change_start
;
1124 /* Neither the start address nor the flags
1125 need to be set for a core file. */
1126 if (bfd_get_format (obfd
) != bfd_core
)
1128 if (!bfd_set_start_address (obfd
, start
)
1129 || !bfd_set_file_flags (obfd
,
1130 (bfd_get_file_flags (ibfd
)
1131 & bfd_applicable_file_flags (obfd
))))
1132 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1135 /* Copy architecture of input file to output file. */
1136 iarch
= bfd_get_arch (ibfd
);
1137 imach
= bfd_get_mach (ibfd
);
1138 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1139 && (ibfd
->target_defaulted
1140 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1141 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1142 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1143 bfd_get_mach (ibfd
)));
1145 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1146 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1151 if (osympp
!= isympp
)
1154 /* BFD mandates that all output sections be created and sizes set before
1155 any output is done. Thus, we traverse all sections multiple times. */
1156 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1158 if (add_sections
!= NULL
)
1160 struct section_add
*padd
;
1161 struct section_list
*pset
;
1163 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1167 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1168 if (padd
->section
== NULL
)
1170 non_fatal (_("can't create section `%s': %s"),
1171 padd
->name
, bfd_errmsg (bfd_get_error ()));
1176 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1177 RETURN_NONFATAL (bfd_get_filename (obfd
));
1179 pset
= find_section_list (padd
->name
, FALSE
);
1183 if (pset
!= NULL
&& pset
->set_flags
)
1184 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1186 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1188 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1189 RETURN_NONFATAL (bfd_get_filename (obfd
));
1193 if (pset
->change_vma
!= CHANGE_IGNORE
)
1194 if (! bfd_set_section_vma (obfd
, padd
->section
,
1196 RETURN_NONFATAL (bfd_get_filename (obfd
));
1198 if (pset
->change_lma
!= CHANGE_IGNORE
)
1200 padd
->section
->lma
= pset
->lma_val
;
1202 if (! bfd_set_section_alignment
1203 (obfd
, padd
->section
,
1204 bfd_section_alignment (obfd
, padd
->section
)))
1205 RETURN_NONFATAL (bfd_get_filename (obfd
));
1211 if (gnu_debuglink_filename
!= NULL
)
1213 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1214 (obfd
, gnu_debuglink_filename
);
1216 if (gnu_debuglink_section
== NULL
)
1217 RETURN_NONFATAL (gnu_debuglink_filename
);
1220 if (gap_fill_set
|| pad_to_set
)
1225 /* We must fill in gaps between the sections and/or we must pad
1226 the last section to a specified address. We do this by
1227 grabbing a list of the sections, sorting them by VMA, and
1228 increasing the section sizes as required to fill the gaps.
1229 We write out the gap contents below. */
1231 c
= bfd_count_sections (obfd
);
1232 osections
= xmalloc (c
* sizeof (asection
*));
1234 bfd_map_over_sections (obfd
, get_sections
, &set
);
1236 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1238 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1239 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1243 for (i
= 0; i
< c
- 1; i
++)
1247 bfd_vma gap_start
, gap_stop
;
1249 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1250 if ((flags
& SEC_HAS_CONTENTS
) == 0
1251 || (flags
& SEC_LOAD
) == 0)
1254 size
= bfd_section_size (obfd
, osections
[i
]);
1255 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1256 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1257 if (gap_start
< gap_stop
)
1259 if (! bfd_set_section_size (obfd
, osections
[i
],
1260 size
+ (gap_stop
- gap_start
)))
1262 non_fatal (_("Can't fill gap after %s: %s"),
1263 bfd_get_section_name (obfd
, osections
[i
]),
1264 bfd_errmsg (bfd_get_error ()));
1268 gaps
[i
] = gap_stop
- gap_start
;
1269 if (max_gap
< gap_stop
- gap_start
)
1270 max_gap
= gap_stop
- gap_start
;
1280 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1281 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1282 if (lma
+ size
< pad_to
)
1284 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1287 non_fatal (_("Can't add padding to %s: %s"),
1288 bfd_get_section_name (obfd
, osections
[c
- 1]),
1289 bfd_errmsg (bfd_get_error ()));
1294 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1295 if (max_gap
< pad_to
- (lma
+ size
))
1296 max_gap
= pad_to
- (lma
+ size
);
1302 /* Symbol filtering must happen after the output sections
1303 have been created, but before their contents are set. */
1305 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1307 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1309 osympp
= isympp
= xmalloc (symsize
);
1310 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1312 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1314 if (convert_debugging
)
1315 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1317 if (strip_symbols
== STRIP_DEBUG
1318 || strip_symbols
== STRIP_ALL
1319 || strip_symbols
== STRIP_UNNEEDED
1320 || strip_symbols
== STRIP_NONDEBUG
1321 || discard_locals
!= LOCALS_UNDEF
1322 || strip_specific_list
!= NULL
1323 || keep_specific_list
!= NULL
1324 || localize_specific_list
!= NULL
1325 || keepglobal_specific_list
!= NULL
1326 || weaken_specific_list
!= NULL
1327 || prefix_symbols_string
1330 || convert_debugging
1331 || change_leading_char
1332 || remove_leading_char
1333 || redefine_sym_list
1336 /* Mark symbols used in output relocations so that they
1337 are kept, even if they are local labels or static symbols.
1339 Note we iterate over the input sections examining their
1340 relocations since the relocations for the output sections
1341 haven't been set yet. mark_symbols_used_in_relocations will
1342 ignore input sections which have no corresponding output
1344 if (strip_symbols
!= STRIP_ALL
)
1345 bfd_map_over_sections (ibfd
,
1346 mark_symbols_used_in_relocations
,
1348 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1349 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1352 if (convert_debugging
&& dhandle
!= NULL
)
1354 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1361 bfd_set_symtab (obfd
, osympp
, symcount
);
1363 /* This has to happen after the symbol table has been set. */
1364 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1366 if (add_sections
!= NULL
)
1368 struct section_add
*padd
;
1370 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1372 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1374 RETURN_NONFATAL (bfd_get_filename (obfd
));
1378 if (gnu_debuglink_filename
!= NULL
)
1380 if (! bfd_fill_in_gnu_debuglink_section
1381 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1382 RETURN_NONFATAL (gnu_debuglink_filename
);
1385 if (gap_fill_set
|| pad_to_set
)
1390 /* Fill in the gaps. */
1393 buf
= xmalloc (max_gap
);
1394 memset (buf
, gap_fill
, max_gap
);
1396 c
= bfd_count_sections (obfd
);
1397 for (i
= 0; i
< c
; i
++)
1405 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1416 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1418 RETURN_NONFATAL (bfd_get_filename (obfd
));
1427 /* Allow the BFD backend to copy any private data it understands
1428 from the input BFD to the output BFD. This is done last to
1429 permit the routine to look at the filtered symbol table, which is
1430 important for the ECOFF code at least. */
1431 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1432 && strip_symbols
== STRIP_NONDEBUG
)
1433 /* Do not copy the private data when creating an ELF format
1434 debug info file. We do not want the program headers. */
1436 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1438 non_fatal (_("%s: error copying private BFD data: %s"),
1439 bfd_get_filename (obfd
),
1440 bfd_errmsg (bfd_get_error ()));
1445 /* Switch to the alternate machine code. We have to do this at the
1446 very end, because we only initialize the header when we create
1447 the first section. */
1448 if (use_alt_mach_code
!= 0)
1450 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1451 non_fatal (_("unknown alternate machine code, ignored"));
1456 #if defined (_WIN32) && !defined (__CYGWIN32__)
1457 #define MKDIR(DIR, MODE) mkdir (DIR)
1459 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1462 /* Read each archive element in turn from IBFD, copy the
1463 contents to temp file, and keep the temp file handle. */
1466 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1470 struct name_list
*next
;
1474 bfd
**ptr
= &obfd
->archive_head
;
1476 char *dir
= make_tempname (bfd_get_filename (obfd
));
1478 /* Make a temp directory to hold the contents. */
1479 if (MKDIR (dir
, 0700) != 0)
1480 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1481 dir
, strerror (errno
));
1483 obfd
->has_armap
= ibfd
->has_armap
;
1487 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1489 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1490 RETURN_NONFATAL (bfd_get_filename (obfd
));
1492 while (!status
&& this_element
!= NULL
)
1498 int stat_status
= 0;
1500 /* Create an output file for this member. */
1501 output_name
= concat (dir
, "/",
1502 bfd_get_filename (this_element
), (char *) 0);
1504 /* If the file already exists, make another temp dir. */
1505 if (stat (output_name
, &buf
) >= 0)
1507 output_name
= make_tempname (output_name
);
1508 if (MKDIR (output_name
, 0700) != 0)
1509 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1510 output_name
, strerror (errno
));
1512 l
= xmalloc (sizeof (struct name_list
));
1513 l
->name
= output_name
;
1517 output_name
= concat (output_name
, "/",
1518 bfd_get_filename (this_element
), (char *) 0);
1521 output_bfd
= bfd_openw (output_name
, output_target
);
1524 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1526 if (stat_status
!= 0)
1527 non_fatal (_("internal stat error on %s"),
1528 bfd_get_filename (this_element
));
1531 l
= xmalloc (sizeof (struct name_list
));
1532 l
->name
= output_name
;
1536 if (output_bfd
== NULL
)
1537 RETURN_NONFATAL (output_name
);
1539 if (bfd_check_format (this_element
, bfd_object
))
1540 copy_object (this_element
, output_bfd
);
1542 if (!bfd_close (output_bfd
))
1544 bfd_nonfatal (bfd_get_filename (output_bfd
));
1545 /* Error in new object file. Don't change archive. */
1549 if (preserve_dates
&& stat_status
== 0)
1550 set_times (output_name
, &buf
);
1552 /* Open the newly output file and attach to our list. */
1553 output_bfd
= bfd_openr (output_name
, output_target
);
1555 l
->obfd
= output_bfd
;
1558 ptr
= &output_bfd
->next
;
1560 last_element
= this_element
;
1562 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1564 bfd_close (last_element
);
1568 if (!bfd_close (obfd
))
1569 RETURN_NONFATAL (bfd_get_filename (obfd
));
1571 if (!bfd_close (ibfd
))
1572 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1574 /* Delete all the files that we opened. */
1575 for (l
= list
; l
!= NULL
; l
= l
->next
)
1577 if (l
->obfd
== NULL
)
1581 bfd_close (l
->obfd
);
1588 /* The top-level control. */
1591 copy_file (const char *input_filename
, const char *output_filename
,
1592 const char *input_target
, const char *output_target
)
1595 char **obj_matching
;
1596 char **core_matching
;
1598 if (get_file_size (input_filename
) < 1)
1604 /* To allow us to do "strip *" without dying on the first
1605 non-object file, failures are nonfatal. */
1606 ibfd
= bfd_openr (input_filename
, input_target
);
1608 RETURN_NONFATAL (input_filename
);
1610 if (bfd_check_format (ibfd
, bfd_archive
))
1614 /* bfd_get_target does not return the correct value until
1615 bfd_check_format succeeds. */
1616 if (output_target
== NULL
)
1617 output_target
= bfd_get_target (ibfd
);
1619 obfd
= bfd_openw (output_filename
, output_target
);
1621 RETURN_NONFATAL (output_filename
);
1623 copy_archive (ibfd
, obfd
, output_target
);
1625 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1629 /* bfd_get_target does not return the correct value until
1630 bfd_check_format succeeds. */
1631 if (output_target
== NULL
)
1632 output_target
= bfd_get_target (ibfd
);
1634 obfd
= bfd_openw (output_filename
, output_target
);
1636 RETURN_NONFATAL (output_filename
);
1638 copy_object (ibfd
, obfd
);
1640 if (!bfd_close (obfd
))
1641 RETURN_NONFATAL (output_filename
);
1643 if (!bfd_close (ibfd
))
1644 RETURN_NONFATAL (input_filename
);
1648 bfd_error_type obj_error
= bfd_get_error ();
1649 bfd_error_type core_error
;
1651 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1653 /* This probably can't happen.. */
1654 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1655 free (obj_matching
);
1659 core_error
= bfd_get_error ();
1660 /* Report the object error in preference to the core error. */
1661 if (obj_error
!= core_error
)
1662 bfd_set_error (obj_error
);
1664 bfd_nonfatal (input_filename
);
1666 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1668 list_matching_formats (obj_matching
);
1669 free (obj_matching
);
1671 if (core_error
== bfd_error_file_ambiguously_recognized
)
1673 list_matching_formats (core_matching
);
1674 free (core_matching
);
1681 /* Add a name to the section renaming list. */
1684 add_section_rename (const char * old_name
, const char * new_name
,
1687 section_rename
* rename
;
1689 /* Check for conflicts first. */
1690 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1691 if (strcmp (rename
->old_name
, old_name
) == 0)
1693 /* Silently ignore duplicate definitions. */
1694 if (strcmp (rename
->new_name
, new_name
) == 0
1695 && rename
->flags
== flags
)
1698 fatal (_("Multiple renames of section %s"), old_name
);
1701 rename
= xmalloc (sizeof (* rename
));
1703 rename
->old_name
= old_name
;
1704 rename
->new_name
= new_name
;
1705 rename
->flags
= flags
;
1706 rename
->next
= section_rename_list
;
1708 section_rename_list
= rename
;
1711 /* Check the section rename list for a new name of the input section
1712 ISECTION. Return the new name if one is found.
1713 Also set RETURNED_FLAGS to the flags to be used for this section. */
1716 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
1717 flagword
* returned_flags
)
1719 const char * old_name
= bfd_section_name (ibfd
, isection
);
1720 section_rename
* rename
;
1722 /* Default to using the flags of the input section. */
1723 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1725 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1726 if (strcmp (rename
->old_name
, old_name
) == 0)
1728 if (rename
->flags
!= (flagword
) -1)
1729 * returned_flags
= rename
->flags
;
1731 return rename
->new_name
;
1737 /* Create a section in OBFD with the same
1738 name and attributes as ISECTION in IBFD. */
1741 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1743 bfd
*obfd
= obfdarg
;
1744 struct section_list
*p
;
1752 char *prefix
= NULL
;
1754 if (is_strip_section (ibfd
, isection
))
1757 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1761 /* Get the, possibly new, name of the output section. */
1762 name
= find_section_rename (ibfd
, isection
, & flags
);
1764 /* Prefix sections. */
1765 if ((prefix_alloc_sections_string
)
1766 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1767 prefix
= prefix_alloc_sections_string
;
1768 else if (prefix_sections_string
)
1769 prefix
= prefix_sections_string
;
1775 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1781 osection
= bfd_make_section_anyway (obfd
, name
);
1783 if (osection
== NULL
)
1789 size
= bfd_section_size (ibfd
, isection
);
1791 size
= (size
+ interleave
- 1) / interleave
;
1792 if (! bfd_set_section_size (obfd
, osection
, size
))
1798 vma
= bfd_section_vma (ibfd
, isection
);
1799 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1801 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1804 vma
+= change_section_address
;
1806 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1812 lma
= isection
->lma
;
1813 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1815 if (p
->change_lma
== CHANGE_MODIFY
)
1817 else if (p
->change_lma
== CHANGE_SET
)
1823 lma
+= change_section_address
;
1825 osection
->lma
= lma
;
1827 /* FIXME: This is probably not enough. If we change the LMA we
1828 may have to recompute the header for the file as well. */
1829 if (!bfd_set_section_alignment (obfd
,
1831 bfd_section_alignment (ibfd
, isection
)))
1833 err
= _("alignment");
1837 if (p
!= NULL
&& p
->set_flags
)
1838 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1839 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1845 /* Copy merge entity size. */
1846 osection
->entsize
= isection
->entsize
;
1848 /* This used to be mangle_section; we do here to avoid using
1849 bfd_get_section_by_name since some formats allow multiple
1850 sections with the same name. */
1851 isection
->output_section
= osection
;
1852 isection
->output_offset
= 0;
1854 /* Allow the BFD backend to copy any private data it understands
1855 from the input section to the output section. */
1856 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1857 && strip_symbols
== STRIP_NONDEBUG
)
1858 /* Do not copy the private data when creating an ELF format
1859 debug info file. We do not want the program headers. */
1861 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1863 err
= _("private data");
1867 /* All went well. */
1871 non_fatal (_("%s: section `%s': error in %s: %s"),
1872 bfd_get_filename (ibfd
),
1873 bfd_section_name (ibfd
, isection
),
1874 err
, bfd_errmsg (bfd_get_error ()));
1878 /* Copy the data of input section ISECTION of IBFD
1879 to an output section with the same name in OBFD.
1880 If stripping then don't copy any relocation info. */
1883 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1885 bfd
*obfd
= obfdarg
;
1886 struct section_list
*p
;
1894 /* If we have already failed earlier on,
1895 do not keep on generating complaints now. */
1899 if (is_strip_section (ibfd
, isection
))
1902 flags
= bfd_get_section_flags (ibfd
, isection
);
1903 if ((flags
& SEC_GROUP
) != 0)
1906 osection
= isection
->output_section
;
1907 size
= bfd_get_section_size_before_reloc (isection
);
1909 if (size
== 0 || osection
== 0)
1912 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
1914 /* Core files do not need to be relocated. */
1915 if (bfd_get_format (obfd
) == bfd_core
)
1919 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1923 /* Do not complain if the target does not support relocations. */
1924 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
1927 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1932 bfd_set_reloc (obfd
, osection
, NULL
, 0);
1935 relpp
= xmalloc (relsize
);
1936 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1938 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1940 if (strip_symbols
== STRIP_ALL
)
1942 /* Remove relocations which are not in
1943 keep_strip_specific_list. */
1944 arelent
**temp_relpp
;
1945 long temp_relcount
= 0;
1948 temp_relpp
= xmalloc (relsize
);
1949 for (i
= 0; i
< relcount
; i
++)
1950 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1951 keep_specific_list
))
1952 temp_relpp
[temp_relcount
++] = relpp
[i
];
1953 relcount
= temp_relcount
;
1958 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
1961 isection
->_cooked_size
= isection
->_raw_size
;
1962 isection
->reloc_done
= TRUE
;
1964 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
1965 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
1967 void *memhunk
= xmalloc (size
);
1969 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
1970 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1973 filter_bytes (memhunk
, &size
);
1975 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
1976 RETURN_NONFATAL (bfd_get_filename (obfd
));
1980 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1982 void *memhunk
= xmalloc (size
);
1984 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1985 flag--they can just remove the section entirely and add it
1986 back again. However, we do permit them to turn on the
1987 SEC_HAS_CONTENTS flag, and take it to mean that the section
1988 contents should be zeroed out. */
1990 memset (memhunk
, 0, size
);
1991 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
1992 RETURN_NONFATAL (bfd_get_filename (obfd
));
1997 /* Get all the sections. This is used when --gap-fill or --pad-to is
2001 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2003 asection
***secppp
= secppparg
;
2005 **secppp
= osection
;
2009 /* Sort sections by VMA. This is called via qsort, and is used when
2010 --gap-fill or --pad-to is used. We force non loadable or empty
2011 sections to the front, where they are easier to ignore. */
2014 compare_section_lma (const void *arg1
, const void *arg2
)
2016 const asection
*const *sec1
= arg1
;
2017 const asection
*const *sec2
= arg2
;
2018 flagword flags1
, flags2
;
2020 /* Sort non loadable sections to the front. */
2021 flags1
= (*sec1
)->flags
;
2022 flags2
= (*sec2
)->flags
;
2023 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2024 || (flags1
& SEC_LOAD
) == 0)
2026 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2027 && (flags2
& SEC_LOAD
) != 0)
2032 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2033 || (flags2
& SEC_LOAD
) == 0)
2037 /* Sort sections by LMA. */
2038 if ((*sec1
)->lma
> (*sec2
)->lma
)
2040 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2043 /* Sort sections with the same LMA by size. */
2044 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
2046 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
2052 /* Mark all the symbols which will be used in output relocations with
2053 the BSF_KEEP flag so that those symbols will not be stripped.
2055 Ignore relocations which will not appear in the output file. */
2058 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2060 asymbol
**symbols
= symbolsarg
;
2065 /* Ignore an input section with no corresponding output section. */
2066 if (isection
->output_section
== NULL
)
2069 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2072 /* Do not complain if the target does not support relocations. */
2073 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2075 bfd_fatal (bfd_get_filename (ibfd
));
2081 relpp
= xmalloc (relsize
);
2082 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2084 bfd_fatal (bfd_get_filename (ibfd
));
2086 /* Examine each symbol used in a relocation. If it's not one of the
2087 special bfd section symbols, then mark it with BSF_KEEP. */
2088 for (i
= 0; i
< relcount
; i
++)
2090 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2091 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2092 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2093 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2100 /* Write out debugging information. */
2103 write_debugging_info (bfd
*obfd
, void *dhandle
,
2104 long *symcountp ATTRIBUTE_UNUSED
,
2105 asymbol
***symppp ATTRIBUTE_UNUSED
)
2107 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2108 return write_ieee_debugging_info (obfd
, dhandle
);
2110 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2111 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2113 bfd_byte
*syms
, *strings
;
2114 bfd_size_type symsize
, stringsize
;
2115 asection
*stabsec
, *stabstrsec
;
2117 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2122 stabsec
= bfd_make_section (obfd
, ".stab");
2123 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2125 || stabstrsec
== NULL
2126 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2127 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2128 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2129 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2130 || ! bfd_set_section_flags (obfd
, stabsec
,
2134 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2139 non_fatal (_("%s: can't create debugging section: %s"),
2140 bfd_get_filename (obfd
),
2141 bfd_errmsg (bfd_get_error ()));
2145 /* We can get away with setting the section contents now because
2146 the next thing the caller is going to do is copy over the
2147 real sections. We may someday have to split the contents
2148 setting out of this function. */
2149 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2150 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2153 non_fatal (_("%s: can't set debugging section contents: %s"),
2154 bfd_get_filename (obfd
),
2155 bfd_errmsg (bfd_get_error ()));
2162 non_fatal (_("%s: don't know how to write debugging information for %s"),
2163 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2168 strip_main (int argc
, char *argv
[])
2170 char *input_target
= NULL
;
2171 char *output_target
= NULL
;
2172 bfd_boolean show_version
= FALSE
;
2173 bfd_boolean formats_info
= FALSE
;
2176 struct section_list
*p
;
2177 char *output_file
= NULL
;
2179 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2180 strip_options
, (int *) 0)) != EOF
)
2185 input_target
= optarg
;
2188 output_target
= optarg
;
2191 input_target
= output_target
= optarg
;
2194 p
= find_section_list (optarg
, TRUE
);
2196 sections_removed
= TRUE
;
2199 strip_symbols
= STRIP_ALL
;
2203 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2204 strip_symbols
= STRIP_DEBUG
;
2206 case OPTION_STRIP_UNNEEDED
:
2207 strip_symbols
= STRIP_UNNEEDED
;
2210 add_specific_symbol (optarg
, &keep_specific_list
);
2213 add_specific_symbol (optarg
, &strip_specific_list
);
2216 output_file
= optarg
;
2219 preserve_dates
= TRUE
;
2222 discard_locals
= LOCALS_ALL
;
2225 discard_locals
= LOCALS_START_L
;
2231 show_version
= TRUE
;
2233 case OPTION_FORMATS_INFO
:
2234 formats_info
= TRUE
;
2236 case OPTION_ONLY_KEEP_DEBUG
:
2237 strip_symbols
= STRIP_NONDEBUG
;
2240 /* We've been given a long option. */
2247 strip_usage (stdout
, 0);
2249 strip_usage (stderr
, 1);
2260 print_version ("strip");
2262 /* Default is to strip all symbols. */
2263 if (strip_symbols
== STRIP_UNDEF
2264 && discard_locals
== LOCALS_UNDEF
2265 && strip_specific_list
== NULL
)
2266 strip_symbols
= STRIP_ALL
;
2268 if (output_target
== NULL
)
2269 output_target
= input_target
;
2273 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2274 strip_usage (stderr
, 1);
2276 for (; i
< argc
; i
++)
2278 int hold_status
= status
;
2279 struct stat statbuf
;
2282 if (get_file_size (argv
[i
]) < 1)
2286 /* No need to check the return value of stat().
2287 It has already been checked in get_file_size(). */
2288 stat (argv
[i
], &statbuf
);
2290 if (output_file
!= NULL
)
2291 tmpname
= output_file
;
2293 tmpname
= make_tempname (argv
[i
]);
2296 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2300 set_times (tmpname
, &statbuf
);
2301 if (output_file
== NULL
)
2302 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2303 status
= hold_status
;
2307 if (output_file
== NULL
)
2315 copy_main (int argc
, char *argv
[])
2317 char * binary_architecture
= NULL
;
2318 char *input_filename
= NULL
;
2319 char *output_filename
= NULL
;
2320 char *input_target
= NULL
;
2321 char *output_target
= NULL
;
2322 bfd_boolean show_version
= FALSE
;
2323 bfd_boolean change_warn
= TRUE
;
2324 bfd_boolean formats_info
= FALSE
;
2326 struct section_list
*p
;
2327 struct stat statbuf
;
2329 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2330 copy_options
, (int *) 0)) != EOF
)
2335 copy_byte
= atoi (optarg
);
2337 fatal (_("byte number must be non-negative"));
2341 binary_architecture
= optarg
;
2345 interleave
= atoi (optarg
);
2347 fatal (_("interleave must be positive"));
2351 case 's': /* "source" - 'I' is preferred */
2352 input_target
= optarg
;
2356 case 'd': /* "destination" - 'O' is preferred */
2357 output_target
= optarg
;
2361 input_target
= output_target
= optarg
;
2365 p
= find_section_list (optarg
, TRUE
);
2367 fatal (_("%s both copied and removed"), optarg
);
2369 sections_copied
= TRUE
;
2373 p
= find_section_list (optarg
, TRUE
);
2375 fatal (_("%s both copied and removed"), optarg
);
2377 sections_removed
= TRUE
;
2381 strip_symbols
= STRIP_ALL
;
2385 strip_symbols
= STRIP_DEBUG
;
2388 case OPTION_STRIP_UNNEEDED
:
2389 strip_symbols
= STRIP_UNNEEDED
;
2392 case OPTION_ONLY_KEEP_DEBUG
:
2393 strip_symbols
= STRIP_NONDEBUG
;
2396 case OPTION_ADD_GNU_DEBUGLINK
:
2397 gnu_debuglink_filename
= optarg
;
2401 add_specific_symbol (optarg
, &keep_specific_list
);
2405 add_specific_symbol (optarg
, &strip_specific_list
);
2409 add_specific_symbol (optarg
, &localize_specific_list
);
2413 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2417 add_specific_symbol (optarg
, &weaken_specific_list
);
2421 preserve_dates
= TRUE
;
2429 discard_locals
= LOCALS_ALL
;
2433 discard_locals
= LOCALS_START_L
;
2441 show_version
= TRUE
;
2444 case OPTION_FORMATS_INFO
:
2445 formats_info
= TRUE
;
2452 case OPTION_ADD_SECTION
:
2456 struct section_add
*pa
;
2461 s
= strchr (optarg
, '=');
2464 fatal (_("bad format for %s"), "--add-section");
2466 size
= get_file_size (s
+ 1);
2470 pa
= xmalloc (sizeof (struct section_add
));
2473 name
= xmalloc (len
+ 1);
2474 strncpy (name
, optarg
, len
);
2478 pa
->filename
= s
+ 1;
2480 pa
->contents
= xmalloc (size
);
2482 f
= fopen (pa
->filename
, FOPEN_RB
);
2485 fatal (_("cannot open: %s: %s"),
2486 pa
->filename
, strerror (errno
));
2488 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2490 fatal (_("%s: fread failed"), pa
->filename
);
2494 pa
->next
= add_sections
;
2499 case OPTION_CHANGE_START
:
2500 change_start
= parse_vma (optarg
, "--change-start");
2503 case OPTION_CHANGE_SECTION_ADDRESS
:
2504 case OPTION_CHANGE_SECTION_LMA
:
2505 case OPTION_CHANGE_SECTION_VMA
:
2510 char *option
= NULL
;
2512 enum change_action what
= CHANGE_IGNORE
;
2516 case OPTION_CHANGE_SECTION_ADDRESS
:
2517 option
= "--change-section-address";
2519 case OPTION_CHANGE_SECTION_LMA
:
2520 option
= "--change-section-lma";
2522 case OPTION_CHANGE_SECTION_VMA
:
2523 option
= "--change-section-vma";
2527 s
= strchr (optarg
, '=');
2530 s
= strchr (optarg
, '+');
2533 s
= strchr (optarg
, '-');
2535 fatal (_("bad format for %s"), option
);
2540 name
= xmalloc (len
+ 1);
2541 strncpy (name
, optarg
, len
);
2544 p
= find_section_list (name
, TRUE
);
2546 val
= parse_vma (s
+ 1, option
);
2550 case '=': what
= CHANGE_SET
; break;
2551 case '-': val
= - val
; /* Drop through. */
2552 case '+': what
= CHANGE_MODIFY
; break;
2557 case OPTION_CHANGE_SECTION_ADDRESS
:
2558 p
->change_vma
= what
;
2562 case OPTION_CHANGE_SECTION_LMA
:
2563 p
->change_lma
= what
;
2567 case OPTION_CHANGE_SECTION_VMA
:
2568 p
->change_vma
= what
;
2575 case OPTION_CHANGE_ADDRESSES
:
2576 change_section_address
= parse_vma (optarg
, "--change-addresses");
2577 change_start
= change_section_address
;
2580 case OPTION_CHANGE_WARNINGS
:
2584 case OPTION_CHANGE_LEADING_CHAR
:
2585 change_leading_char
= TRUE
;
2588 case OPTION_DEBUGGING
:
2589 convert_debugging
= TRUE
;
2592 case OPTION_GAP_FILL
:
2594 bfd_vma gap_fill_vma
;
2596 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2597 gap_fill
= (bfd_byte
) gap_fill_vma
;
2598 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2602 sprintf_vma (buff
, gap_fill_vma
);
2604 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2607 gap_fill_set
= TRUE
;
2611 case OPTION_NO_CHANGE_WARNINGS
:
2612 change_warn
= FALSE
;
2616 pad_to
= parse_vma (optarg
, "--pad-to");
2620 case OPTION_REMOVE_LEADING_CHAR
:
2621 remove_leading_char
= TRUE
;
2624 case OPTION_REDEFINE_SYM
:
2626 /* Push this redefinition onto redefine_symbol_list. */
2630 const char *nextarg
;
2631 char *source
, *target
;
2633 s
= strchr (optarg
, '=');
2635 fatal (_("bad format for %s"), "--redefine-sym");
2638 source
= xmalloc (len
+ 1);
2639 strncpy (source
, optarg
, len
);
2643 len
= strlen (nextarg
);
2644 target
= xmalloc (len
+ 1);
2645 strcpy (target
, nextarg
);
2647 redefine_list_append ("--redefine-sym", source
, target
);
2654 case OPTION_REDEFINE_SYMS
:
2655 add_redefine_syms_file (optarg
);
2658 case OPTION_SET_SECTION_FLAGS
:
2664 s
= strchr (optarg
, '=');
2666 fatal (_("bad format for %s"), "--set-section-flags");
2669 name
= xmalloc (len
+ 1);
2670 strncpy (name
, optarg
, len
);
2673 p
= find_section_list (name
, TRUE
);
2675 p
->set_flags
= TRUE
;
2676 p
->flags
= parse_flags (s
+ 1);
2680 case OPTION_RENAME_SECTION
:
2683 const char *eq
, *fl
;
2688 eq
= strchr (optarg
, '=');
2690 fatal (_("bad format for %s"), "--rename-section");
2694 fatal (_("bad format for %s"), "--rename-section");
2696 old_name
= xmalloc (len
+ 1);
2697 strncpy (old_name
, optarg
, len
);
2701 fl
= strchr (eq
, ',');
2704 flags
= parse_flags (fl
+ 1);
2714 fatal (_("bad format for %s"), "--rename-section");
2716 new_name
= xmalloc (len
+ 1);
2717 strncpy (new_name
, eq
, len
);
2720 add_section_rename (old_name
, new_name
, flags
);
2724 case OPTION_SET_START
:
2725 set_start
= parse_vma (optarg
, "--set-start");
2726 set_start_set
= TRUE
;
2729 case OPTION_SREC_LEN
:
2730 Chunk
= parse_vma (optarg
, "--srec-len");
2733 case OPTION_SREC_FORCES3
:
2737 case OPTION_STRIP_SYMBOLS
:
2738 add_specific_symbols (optarg
, &strip_specific_list
);
2741 case OPTION_KEEP_SYMBOLS
:
2742 add_specific_symbols (optarg
, &keep_specific_list
);
2745 case OPTION_LOCALIZE_SYMBOLS
:
2746 add_specific_symbols (optarg
, &localize_specific_list
);
2749 case OPTION_KEEPGLOBAL_SYMBOLS
:
2750 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2753 case OPTION_WEAKEN_SYMBOLS
:
2754 add_specific_symbols (optarg
, &weaken_specific_list
);
2757 case OPTION_ALT_MACH_CODE
:
2758 use_alt_mach_code
= atoi (optarg
);
2759 if (use_alt_mach_code
<= 0)
2760 fatal (_("alternate machine code index must be positive"));
2763 case OPTION_PREFIX_SYMBOLS
:
2764 prefix_symbols_string
= optarg
;
2767 case OPTION_PREFIX_SECTIONS
:
2768 prefix_sections_string
= optarg
;
2771 case OPTION_PREFIX_ALLOC_SECTIONS
:
2772 prefix_alloc_sections_string
= optarg
;
2776 /* We've been given a long option. */
2781 copy_usage (stdout
, 0);
2784 copy_usage (stderr
, 1);
2795 print_version ("objcopy");
2797 if (copy_byte
>= interleave
)
2798 fatal (_("byte number must be less than interleave"));
2800 if (optind
== argc
|| optind
+ 2 < argc
)
2801 copy_usage (stderr
, 1);
2803 input_filename
= argv
[optind
];
2804 if (optind
+ 1 < argc
)
2805 output_filename
= argv
[optind
+ 1];
2807 /* Default is to strip no symbols. */
2808 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2809 strip_symbols
= STRIP_NONE
;
2811 if (output_target
== NULL
)
2812 output_target
= input_target
;
2814 if (binary_architecture
!= NULL
)
2816 if (input_target
&& strcmp (input_target
, "binary") == 0)
2818 const bfd_arch_info_type
* temp_arch_info
;
2820 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2822 if (temp_arch_info
!= NULL
)
2824 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2825 bfd_external_machine
= temp_arch_info
->mach
;
2828 fatal (_("architecture %s unknown"), binary_architecture
);
2832 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2833 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2838 if (stat (input_filename
, & statbuf
) < 0)
2839 fatal (_("warning: could not locate '%s'. System error message: %s"),
2840 input_filename
, strerror (errno
));
2842 /* If there is no destination file, or the source and destination files
2843 are the same, then create a temp and rename the result into the input. */
2844 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
2846 char *tmpname
= make_tempname (input_filename
);
2848 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2852 set_times (tmpname
, &statbuf
);
2853 smart_rename (tmpname
, input_filename
, preserve_dates
);
2860 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2862 if (status
== 0 && preserve_dates
)
2863 set_times (output_filename
, &statbuf
);
2868 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2872 if (p
->change_vma
!= CHANGE_IGNORE
)
2876 sprintf_vma (buff
, p
->vma_val
);
2878 /* xgettext:c-format */
2879 non_fatal (_("%s %s%c0x%s never used"),
2880 "--change-section-vma",
2882 p
->change_vma
== CHANGE_SET
? '=' : '+',
2886 if (p
->change_lma
!= CHANGE_IGNORE
)
2890 sprintf_vma (buff
, p
->lma_val
);
2892 /* xgettext:c-format */
2893 non_fatal (_("%s %s%c0x%s never used"),
2894 "--change-section-lma",
2896 p
->change_lma
== CHANGE_SET
? '=' : '+',
2907 main (int argc
, char *argv
[])
2909 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2910 setlocale (LC_MESSAGES
, "");
2912 #if defined (HAVE_SETLOCALE)
2913 setlocale (LC_CTYPE
, "");
2915 bindtextdomain (PACKAGE
, LOCALEDIR
);
2916 textdomain (PACKAGE
);
2918 program_name
= argv
[0];
2919 xmalloc_set_program_name (program_name
);
2921 START_PROGRESS (program_name
, 0);
2923 strip_symbols
= STRIP_UNDEF
;
2924 discard_locals
= LOCALS_UNDEF
;
2927 set_default_bfd_target ();
2931 int i
= strlen (program_name
);
2932 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2933 /* Drop the .exe suffix, if any. */
2934 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2937 program_name
[i
] = '\0';
2940 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2944 strip_main (argc
, argv
);
2946 copy_main (argc
, argv
);
2948 END_PROGRESS (program_name
);