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 comparison. */
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
)))
1142 if (bfd_get_arch (ibfd
) == bfd_arch_unknown
)
1143 fatal (_("Unable to recognise the format of the input file %s"),
1144 bfd_get_filename (ibfd
));
1147 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1148 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1149 bfd_get_mach (ibfd
)));
1155 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1156 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1161 if (osympp
!= isympp
)
1164 /* BFD mandates that all output sections be created and sizes set before
1165 any output is done. Thus, we traverse all sections multiple times. */
1166 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1168 if (add_sections
!= NULL
)
1170 struct section_add
*padd
;
1171 struct section_list
*pset
;
1173 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1177 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1178 if (padd
->section
== NULL
)
1180 non_fatal (_("can't create section `%s': %s"),
1181 padd
->name
, bfd_errmsg (bfd_get_error ()));
1186 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1187 RETURN_NONFATAL (bfd_get_filename (obfd
));
1189 pset
= find_section_list (padd
->name
, FALSE
);
1193 if (pset
!= NULL
&& pset
->set_flags
)
1194 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1196 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1198 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1199 RETURN_NONFATAL (bfd_get_filename (obfd
));
1203 if (pset
->change_vma
!= CHANGE_IGNORE
)
1204 if (! bfd_set_section_vma (obfd
, padd
->section
,
1206 RETURN_NONFATAL (bfd_get_filename (obfd
));
1208 if (pset
->change_lma
!= CHANGE_IGNORE
)
1210 padd
->section
->lma
= pset
->lma_val
;
1212 if (! bfd_set_section_alignment
1213 (obfd
, padd
->section
,
1214 bfd_section_alignment (obfd
, padd
->section
)))
1215 RETURN_NONFATAL (bfd_get_filename (obfd
));
1221 if (gnu_debuglink_filename
!= NULL
)
1223 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1224 (obfd
, gnu_debuglink_filename
);
1226 if (gnu_debuglink_section
== NULL
)
1227 RETURN_NONFATAL (gnu_debuglink_filename
);
1230 if (gap_fill_set
|| pad_to_set
)
1235 /* We must fill in gaps between the sections and/or we must pad
1236 the last section to a specified address. We do this by
1237 grabbing a list of the sections, sorting them by VMA, and
1238 increasing the section sizes as required to fill the gaps.
1239 We write out the gap contents below. */
1241 c
= bfd_count_sections (obfd
);
1242 osections
= xmalloc (c
* sizeof (asection
*));
1244 bfd_map_over_sections (obfd
, get_sections
, &set
);
1246 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1248 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1249 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1253 for (i
= 0; i
< c
- 1; i
++)
1257 bfd_vma gap_start
, gap_stop
;
1259 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1260 if ((flags
& SEC_HAS_CONTENTS
) == 0
1261 || (flags
& SEC_LOAD
) == 0)
1264 size
= bfd_section_size (obfd
, osections
[i
]);
1265 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1266 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1267 if (gap_start
< gap_stop
)
1269 if (! bfd_set_section_size (obfd
, osections
[i
],
1270 size
+ (gap_stop
- gap_start
)))
1272 non_fatal (_("Can't fill gap after %s: %s"),
1273 bfd_get_section_name (obfd
, osections
[i
]),
1274 bfd_errmsg (bfd_get_error ()));
1278 gaps
[i
] = gap_stop
- gap_start
;
1279 if (max_gap
< gap_stop
- gap_start
)
1280 max_gap
= gap_stop
- gap_start
;
1290 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1291 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1292 if (lma
+ size
< pad_to
)
1294 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1297 non_fatal (_("Can't add padding to %s: %s"),
1298 bfd_get_section_name (obfd
, osections
[c
- 1]),
1299 bfd_errmsg (bfd_get_error ()));
1304 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1305 if (max_gap
< pad_to
- (lma
+ size
))
1306 max_gap
= pad_to
- (lma
+ size
);
1312 /* Symbol filtering must happen after the output sections
1313 have been created, but before their contents are set. */
1315 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1317 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1319 osympp
= isympp
= xmalloc (symsize
);
1320 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1322 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1324 if (convert_debugging
)
1325 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1327 if (strip_symbols
== STRIP_DEBUG
1328 || strip_symbols
== STRIP_ALL
1329 || strip_symbols
== STRIP_UNNEEDED
1330 || strip_symbols
== STRIP_NONDEBUG
1331 || discard_locals
!= LOCALS_UNDEF
1332 || strip_specific_list
!= NULL
1333 || keep_specific_list
!= NULL
1334 || localize_specific_list
!= NULL
1335 || keepglobal_specific_list
!= NULL
1336 || weaken_specific_list
!= NULL
1337 || prefix_symbols_string
1340 || convert_debugging
1341 || change_leading_char
1342 || remove_leading_char
1343 || redefine_sym_list
1346 /* Mark symbols used in output relocations so that they
1347 are kept, even if they are local labels or static symbols.
1349 Note we iterate over the input sections examining their
1350 relocations since the relocations for the output sections
1351 haven't been set yet. mark_symbols_used_in_relocations will
1352 ignore input sections which have no corresponding output
1354 if (strip_symbols
!= STRIP_ALL
)
1355 bfd_map_over_sections (ibfd
,
1356 mark_symbols_used_in_relocations
,
1358 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1359 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1362 if (convert_debugging
&& dhandle
!= NULL
)
1364 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1371 bfd_set_symtab (obfd
, osympp
, symcount
);
1373 /* This has to happen after the symbol table has been set. */
1374 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1376 if (add_sections
!= NULL
)
1378 struct section_add
*padd
;
1380 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1382 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1384 RETURN_NONFATAL (bfd_get_filename (obfd
));
1388 if (gnu_debuglink_filename
!= NULL
)
1390 if (! bfd_fill_in_gnu_debuglink_section
1391 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1392 RETURN_NONFATAL (gnu_debuglink_filename
);
1395 if (gap_fill_set
|| pad_to_set
)
1400 /* Fill in the gaps. */
1403 buf
= xmalloc (max_gap
);
1404 memset (buf
, gap_fill
, max_gap
);
1406 c
= bfd_count_sections (obfd
);
1407 for (i
= 0; i
< c
; i
++)
1415 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1426 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1428 RETURN_NONFATAL (bfd_get_filename (obfd
));
1437 /* Allow the BFD backend to copy any private data it understands
1438 from the input BFD to the output BFD. This is done last to
1439 permit the routine to look at the filtered symbol table, which is
1440 important for the ECOFF code at least. */
1441 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1442 && strip_symbols
== STRIP_NONDEBUG
)
1443 /* Do not copy the private data when creating an ELF format
1444 debug info file. We do not want the program headers. */
1446 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1448 non_fatal (_("%s: error copying private BFD data: %s"),
1449 bfd_get_filename (obfd
),
1450 bfd_errmsg (bfd_get_error ()));
1455 /* Switch to the alternate machine code. We have to do this at the
1456 very end, because we only initialize the header when we create
1457 the first section. */
1458 if (use_alt_mach_code
!= 0)
1460 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1461 non_fatal (_("unknown alternate machine code, ignored"));
1466 #if defined (_WIN32) && !defined (__CYGWIN32__)
1467 #define MKDIR(DIR, MODE) mkdir (DIR)
1469 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1472 /* Read each archive element in turn from IBFD, copy the
1473 contents to temp file, and keep the temp file handle. */
1476 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1480 struct name_list
*next
;
1484 bfd
**ptr
= &obfd
->archive_head
;
1486 char *dir
= make_tempname (bfd_get_filename (obfd
));
1488 /* Make a temp directory to hold the contents. */
1489 if (MKDIR (dir
, 0700) != 0)
1490 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1491 dir
, strerror (errno
));
1493 obfd
->has_armap
= ibfd
->has_armap
;
1497 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1499 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1500 RETURN_NONFATAL (bfd_get_filename (obfd
));
1502 while (!status
&& this_element
!= NULL
)
1508 int stat_status
= 0;
1510 /* Create an output file for this member. */
1511 output_name
= concat (dir
, "/",
1512 bfd_get_filename (this_element
), (char *) 0);
1514 /* If the file already exists, make another temp dir. */
1515 if (stat (output_name
, &buf
) >= 0)
1517 output_name
= make_tempname (output_name
);
1518 if (MKDIR (output_name
, 0700) != 0)
1519 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1520 output_name
, strerror (errno
));
1522 l
= xmalloc (sizeof (struct name_list
));
1523 l
->name
= output_name
;
1527 output_name
= concat (output_name
, "/",
1528 bfd_get_filename (this_element
), (char *) 0);
1531 output_bfd
= bfd_openw (output_name
, output_target
);
1534 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1536 if (stat_status
!= 0)
1537 non_fatal (_("internal stat error on %s"),
1538 bfd_get_filename (this_element
));
1541 l
= xmalloc (sizeof (struct name_list
));
1542 l
->name
= output_name
;
1546 if (output_bfd
== NULL
)
1547 RETURN_NONFATAL (output_name
);
1549 if (bfd_check_format (this_element
, bfd_object
))
1550 copy_object (this_element
, output_bfd
);
1552 if (!bfd_close (output_bfd
))
1554 bfd_nonfatal (bfd_get_filename (output_bfd
));
1555 /* Error in new object file. Don't change archive. */
1559 if (preserve_dates
&& stat_status
== 0)
1560 set_times (output_name
, &buf
);
1562 /* Open the newly output file and attach to our list. */
1563 output_bfd
= bfd_openr (output_name
, output_target
);
1565 l
->obfd
= output_bfd
;
1568 ptr
= &output_bfd
->next
;
1570 last_element
= this_element
;
1572 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1574 bfd_close (last_element
);
1578 if (!bfd_close (obfd
))
1579 RETURN_NONFATAL (bfd_get_filename (obfd
));
1581 if (!bfd_close (ibfd
))
1582 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1584 /* Delete all the files that we opened. */
1585 for (l
= list
; l
!= NULL
; l
= l
->next
)
1587 if (l
->obfd
== NULL
)
1591 bfd_close (l
->obfd
);
1598 /* The top-level control. */
1601 copy_file (const char *input_filename
, const char *output_filename
,
1602 const char *input_target
, const char *output_target
)
1605 char **obj_matching
;
1606 char **core_matching
;
1608 if (get_file_size (input_filename
) < 1)
1614 /* To allow us to do "strip *" without dying on the first
1615 non-object file, failures are nonfatal. */
1616 ibfd
= bfd_openr (input_filename
, input_target
);
1618 RETURN_NONFATAL (input_filename
);
1620 if (bfd_check_format (ibfd
, bfd_archive
))
1624 /* bfd_get_target does not return the correct value until
1625 bfd_check_format succeeds. */
1626 if (output_target
== NULL
)
1627 output_target
= bfd_get_target (ibfd
);
1629 obfd
= bfd_openw (output_filename
, output_target
);
1631 RETURN_NONFATAL (output_filename
);
1633 copy_archive (ibfd
, obfd
, output_target
);
1635 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1639 /* bfd_get_target does not return the correct value until
1640 bfd_check_format succeeds. */
1641 if (output_target
== NULL
)
1642 output_target
= bfd_get_target (ibfd
);
1644 obfd
= bfd_openw (output_filename
, output_target
);
1646 RETURN_NONFATAL (output_filename
);
1648 copy_object (ibfd
, obfd
);
1650 if (!bfd_close (obfd
))
1651 RETURN_NONFATAL (output_filename
);
1653 if (!bfd_close (ibfd
))
1654 RETURN_NONFATAL (input_filename
);
1658 bfd_error_type obj_error
= bfd_get_error ();
1659 bfd_error_type core_error
;
1661 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1663 /* This probably can't happen.. */
1664 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1665 free (obj_matching
);
1669 core_error
= bfd_get_error ();
1670 /* Report the object error in preference to the core error. */
1671 if (obj_error
!= core_error
)
1672 bfd_set_error (obj_error
);
1674 bfd_nonfatal (input_filename
);
1676 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1678 list_matching_formats (obj_matching
);
1679 free (obj_matching
);
1681 if (core_error
== bfd_error_file_ambiguously_recognized
)
1683 list_matching_formats (core_matching
);
1684 free (core_matching
);
1691 /* Add a name to the section renaming list. */
1694 add_section_rename (const char * old_name
, const char * new_name
,
1697 section_rename
* rename
;
1699 /* Check for conflicts first. */
1700 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1701 if (strcmp (rename
->old_name
, old_name
) == 0)
1703 /* Silently ignore duplicate definitions. */
1704 if (strcmp (rename
->new_name
, new_name
) == 0
1705 && rename
->flags
== flags
)
1708 fatal (_("Multiple renames of section %s"), old_name
);
1711 rename
= xmalloc (sizeof (* rename
));
1713 rename
->old_name
= old_name
;
1714 rename
->new_name
= new_name
;
1715 rename
->flags
= flags
;
1716 rename
->next
= section_rename_list
;
1718 section_rename_list
= rename
;
1721 /* Check the section rename list for a new name of the input section
1722 ISECTION. Return the new name if one is found.
1723 Also set RETURNED_FLAGS to the flags to be used for this section. */
1726 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
1727 flagword
* returned_flags
)
1729 const char * old_name
= bfd_section_name (ibfd
, isection
);
1730 section_rename
* rename
;
1732 /* Default to using the flags of the input section. */
1733 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1735 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1736 if (strcmp (rename
->old_name
, old_name
) == 0)
1738 if (rename
->flags
!= (flagword
) -1)
1739 * returned_flags
= rename
->flags
;
1741 return rename
->new_name
;
1747 /* Create a section in OBFD with the same
1748 name and attributes as ISECTION in IBFD. */
1751 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1753 bfd
*obfd
= obfdarg
;
1754 struct section_list
*p
;
1762 char *prefix
= NULL
;
1764 if (is_strip_section (ibfd
, isection
))
1767 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1771 /* Get the, possibly new, name of the output section. */
1772 name
= find_section_rename (ibfd
, isection
, & flags
);
1774 /* Prefix sections. */
1775 if ((prefix_alloc_sections_string
)
1776 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1777 prefix
= prefix_alloc_sections_string
;
1778 else if (prefix_sections_string
)
1779 prefix
= prefix_sections_string
;
1785 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1791 osection
= bfd_make_section_anyway (obfd
, name
);
1793 if (osection
== NULL
)
1799 size
= bfd_section_size (ibfd
, isection
);
1801 size
= (size
+ interleave
- 1) / interleave
;
1802 if (! bfd_set_section_size (obfd
, osection
, size
))
1808 vma
= bfd_section_vma (ibfd
, isection
);
1809 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1811 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1814 vma
+= change_section_address
;
1816 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1822 lma
= isection
->lma
;
1823 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1825 if (p
->change_lma
== CHANGE_MODIFY
)
1827 else if (p
->change_lma
== CHANGE_SET
)
1833 lma
+= change_section_address
;
1835 osection
->lma
= lma
;
1837 /* FIXME: This is probably not enough. If we change the LMA we
1838 may have to recompute the header for the file as well. */
1839 if (!bfd_set_section_alignment (obfd
,
1841 bfd_section_alignment (ibfd
, isection
)))
1843 err
= _("alignment");
1847 if (p
!= NULL
&& p
->set_flags
)
1848 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1849 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1855 /* Copy merge entity size. */
1856 osection
->entsize
= isection
->entsize
;
1858 /* This used to be mangle_section; we do here to avoid using
1859 bfd_get_section_by_name since some formats allow multiple
1860 sections with the same name. */
1861 isection
->output_section
= osection
;
1862 isection
->output_offset
= 0;
1864 /* Allow the BFD backend to copy any private data it understands
1865 from the input section to the output section. */
1866 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1867 && strip_symbols
== STRIP_NONDEBUG
)
1868 /* Do not copy the private data when creating an ELF format
1869 debug info file. We do not want the program headers. */
1871 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1873 err
= _("private data");
1877 /* All went well. */
1881 non_fatal (_("%s: section `%s': error in %s: %s"),
1882 bfd_get_filename (ibfd
),
1883 bfd_section_name (ibfd
, isection
),
1884 err
, bfd_errmsg (bfd_get_error ()));
1888 /* Copy the data of input section ISECTION of IBFD
1889 to an output section with the same name in OBFD.
1890 If stripping then don't copy any relocation info. */
1893 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1895 bfd
*obfd
= obfdarg
;
1896 struct section_list
*p
;
1904 /* If we have already failed earlier on,
1905 do not keep on generating complaints now. */
1909 if (is_strip_section (ibfd
, isection
))
1912 flags
= bfd_get_section_flags (ibfd
, isection
);
1913 if ((flags
& SEC_GROUP
) != 0)
1916 osection
= isection
->output_section
;
1917 size
= bfd_get_section_size_before_reloc (isection
);
1919 if (size
== 0 || osection
== 0)
1922 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
1924 /* Core files do not need to be relocated. */
1925 if (bfd_get_format (obfd
) == bfd_core
)
1929 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1933 /* Do not complain if the target does not support relocations. */
1934 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
1937 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1942 bfd_set_reloc (obfd
, osection
, NULL
, 0);
1945 relpp
= xmalloc (relsize
);
1946 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1948 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1950 if (strip_symbols
== STRIP_ALL
)
1952 /* Remove relocations which are not in
1953 keep_strip_specific_list. */
1954 arelent
**temp_relpp
;
1955 long temp_relcount
= 0;
1958 temp_relpp
= xmalloc (relsize
);
1959 for (i
= 0; i
< relcount
; i
++)
1960 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1961 keep_specific_list
))
1962 temp_relpp
[temp_relcount
++] = relpp
[i
];
1963 relcount
= temp_relcount
;
1968 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
1971 isection
->_cooked_size
= isection
->_raw_size
;
1972 isection
->reloc_done
= TRUE
;
1974 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
1975 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
1977 void *memhunk
= xmalloc (size
);
1979 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
1980 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1983 filter_bytes (memhunk
, &size
);
1985 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
1986 RETURN_NONFATAL (bfd_get_filename (obfd
));
1990 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1992 void *memhunk
= xmalloc (size
);
1994 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1995 flag--they can just remove the section entirely and add it
1996 back again. However, we do permit them to turn on the
1997 SEC_HAS_CONTENTS flag, and take it to mean that the section
1998 contents should be zeroed out. */
2000 memset (memhunk
, 0, size
);
2001 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
2002 RETURN_NONFATAL (bfd_get_filename (obfd
));
2007 /* Get all the sections. This is used when --gap-fill or --pad-to is
2011 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
2013 asection
***secppp
= secppparg
;
2015 **secppp
= osection
;
2019 /* Sort sections by VMA. This is called via qsort, and is used when
2020 --gap-fill or --pad-to is used. We force non loadable or empty
2021 sections to the front, where they are easier to ignore. */
2024 compare_section_lma (const void *arg1
, const void *arg2
)
2026 const asection
*const *sec1
= arg1
;
2027 const asection
*const *sec2
= arg2
;
2028 flagword flags1
, flags2
;
2030 /* Sort non loadable sections to the front. */
2031 flags1
= (*sec1
)->flags
;
2032 flags2
= (*sec2
)->flags
;
2033 if ((flags1
& SEC_HAS_CONTENTS
) == 0
2034 || (flags1
& SEC_LOAD
) == 0)
2036 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2037 && (flags2
& SEC_LOAD
) != 0)
2042 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2043 || (flags2
& SEC_LOAD
) == 0)
2047 /* Sort sections by LMA. */
2048 if ((*sec1
)->lma
> (*sec2
)->lma
)
2050 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2053 /* Sort sections with the same LMA by size. */
2054 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
2056 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
2062 /* Mark all the symbols which will be used in output relocations with
2063 the BSF_KEEP flag so that those symbols will not be stripped.
2065 Ignore relocations which will not appear in the output file. */
2068 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2070 asymbol
**symbols
= symbolsarg
;
2075 /* Ignore an input section with no corresponding output section. */
2076 if (isection
->output_section
== NULL
)
2079 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2082 /* Do not complain if the target does not support relocations. */
2083 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2085 bfd_fatal (bfd_get_filename (ibfd
));
2091 relpp
= xmalloc (relsize
);
2092 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2094 bfd_fatal (bfd_get_filename (ibfd
));
2096 /* Examine each symbol used in a relocation. If it's not one of the
2097 special bfd section symbols, then mark it with BSF_KEEP. */
2098 for (i
= 0; i
< relcount
; i
++)
2100 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2101 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2102 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2103 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2110 /* Write out debugging information. */
2113 write_debugging_info (bfd
*obfd
, void *dhandle
,
2114 long *symcountp ATTRIBUTE_UNUSED
,
2115 asymbol
***symppp ATTRIBUTE_UNUSED
)
2117 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2118 return write_ieee_debugging_info (obfd
, dhandle
);
2120 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2121 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2123 bfd_byte
*syms
, *strings
;
2124 bfd_size_type symsize
, stringsize
;
2125 asection
*stabsec
, *stabstrsec
;
2127 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2132 stabsec
= bfd_make_section (obfd
, ".stab");
2133 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2135 || stabstrsec
== NULL
2136 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2137 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2138 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2139 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2140 || ! bfd_set_section_flags (obfd
, stabsec
,
2144 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2149 non_fatal (_("%s: can't create debugging section: %s"),
2150 bfd_get_filename (obfd
),
2151 bfd_errmsg (bfd_get_error ()));
2155 /* We can get away with setting the section contents now because
2156 the next thing the caller is going to do is copy over the
2157 real sections. We may someday have to split the contents
2158 setting out of this function. */
2159 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2160 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2163 non_fatal (_("%s: can't set debugging section contents: %s"),
2164 bfd_get_filename (obfd
),
2165 bfd_errmsg (bfd_get_error ()));
2172 non_fatal (_("%s: don't know how to write debugging information for %s"),
2173 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2178 strip_main (int argc
, char *argv
[])
2180 char *input_target
= NULL
;
2181 char *output_target
= NULL
;
2182 bfd_boolean show_version
= FALSE
;
2183 bfd_boolean formats_info
= FALSE
;
2186 struct section_list
*p
;
2187 char *output_file
= NULL
;
2189 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2190 strip_options
, (int *) 0)) != EOF
)
2195 input_target
= optarg
;
2198 output_target
= optarg
;
2201 input_target
= output_target
= optarg
;
2204 p
= find_section_list (optarg
, TRUE
);
2206 sections_removed
= TRUE
;
2209 strip_symbols
= STRIP_ALL
;
2213 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2214 strip_symbols
= STRIP_DEBUG
;
2216 case OPTION_STRIP_UNNEEDED
:
2217 strip_symbols
= STRIP_UNNEEDED
;
2220 add_specific_symbol (optarg
, &keep_specific_list
);
2223 add_specific_symbol (optarg
, &strip_specific_list
);
2226 output_file
= optarg
;
2229 preserve_dates
= TRUE
;
2232 discard_locals
= LOCALS_ALL
;
2235 discard_locals
= LOCALS_START_L
;
2241 show_version
= TRUE
;
2243 case OPTION_FORMATS_INFO
:
2244 formats_info
= TRUE
;
2246 case OPTION_ONLY_KEEP_DEBUG
:
2247 strip_symbols
= STRIP_NONDEBUG
;
2250 /* We've been given a long option. */
2257 strip_usage (stdout
, 0);
2259 strip_usage (stderr
, 1);
2270 print_version ("strip");
2272 /* Default is to strip all symbols. */
2273 if (strip_symbols
== STRIP_UNDEF
2274 && discard_locals
== LOCALS_UNDEF
2275 && strip_specific_list
== NULL
)
2276 strip_symbols
= STRIP_ALL
;
2278 if (output_target
== NULL
)
2279 output_target
= input_target
;
2283 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2284 strip_usage (stderr
, 1);
2286 for (; i
< argc
; i
++)
2288 int hold_status
= status
;
2289 struct stat statbuf
;
2292 if (get_file_size (argv
[i
]) < 1)
2296 /* No need to check the return value of stat().
2297 It has already been checked in get_file_size(). */
2298 stat (argv
[i
], &statbuf
);
2300 if (output_file
!= NULL
)
2301 tmpname
= output_file
;
2303 tmpname
= make_tempname (argv
[i
]);
2306 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2310 set_times (tmpname
, &statbuf
);
2311 if (output_file
== NULL
)
2312 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2313 status
= hold_status
;
2317 if (output_file
== NULL
)
2325 copy_main (int argc
, char *argv
[])
2327 char * binary_architecture
= NULL
;
2328 char *input_filename
= NULL
;
2329 char *output_filename
= NULL
;
2330 char *input_target
= NULL
;
2331 char *output_target
= NULL
;
2332 bfd_boolean show_version
= FALSE
;
2333 bfd_boolean change_warn
= TRUE
;
2334 bfd_boolean formats_info
= FALSE
;
2336 struct section_list
*p
;
2337 struct stat statbuf
;
2339 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2340 copy_options
, (int *) 0)) != EOF
)
2345 copy_byte
= atoi (optarg
);
2347 fatal (_("byte number must be non-negative"));
2351 binary_architecture
= optarg
;
2355 interleave
= atoi (optarg
);
2357 fatal (_("interleave must be positive"));
2361 case 's': /* "source" - 'I' is preferred */
2362 input_target
= optarg
;
2366 case 'd': /* "destination" - 'O' is preferred */
2367 output_target
= optarg
;
2371 input_target
= output_target
= optarg
;
2375 p
= find_section_list (optarg
, TRUE
);
2377 fatal (_("%s both copied and removed"), optarg
);
2379 sections_copied
= TRUE
;
2383 p
= find_section_list (optarg
, TRUE
);
2385 fatal (_("%s both copied and removed"), optarg
);
2387 sections_removed
= TRUE
;
2391 strip_symbols
= STRIP_ALL
;
2395 strip_symbols
= STRIP_DEBUG
;
2398 case OPTION_STRIP_UNNEEDED
:
2399 strip_symbols
= STRIP_UNNEEDED
;
2402 case OPTION_ONLY_KEEP_DEBUG
:
2403 strip_symbols
= STRIP_NONDEBUG
;
2406 case OPTION_ADD_GNU_DEBUGLINK
:
2407 gnu_debuglink_filename
= optarg
;
2411 add_specific_symbol (optarg
, &keep_specific_list
);
2415 add_specific_symbol (optarg
, &strip_specific_list
);
2419 add_specific_symbol (optarg
, &localize_specific_list
);
2423 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2427 add_specific_symbol (optarg
, &weaken_specific_list
);
2431 preserve_dates
= TRUE
;
2439 discard_locals
= LOCALS_ALL
;
2443 discard_locals
= LOCALS_START_L
;
2451 show_version
= TRUE
;
2454 case OPTION_FORMATS_INFO
:
2455 formats_info
= TRUE
;
2462 case OPTION_ADD_SECTION
:
2466 struct section_add
*pa
;
2471 s
= strchr (optarg
, '=');
2474 fatal (_("bad format for %s"), "--add-section");
2476 size
= get_file_size (s
+ 1);
2480 pa
= xmalloc (sizeof (struct section_add
));
2483 name
= xmalloc (len
+ 1);
2484 strncpy (name
, optarg
, len
);
2488 pa
->filename
= s
+ 1;
2490 pa
->contents
= xmalloc (size
);
2492 f
= fopen (pa
->filename
, FOPEN_RB
);
2495 fatal (_("cannot open: %s: %s"),
2496 pa
->filename
, strerror (errno
));
2498 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2500 fatal (_("%s: fread failed"), pa
->filename
);
2504 pa
->next
= add_sections
;
2509 case OPTION_CHANGE_START
:
2510 change_start
= parse_vma (optarg
, "--change-start");
2513 case OPTION_CHANGE_SECTION_ADDRESS
:
2514 case OPTION_CHANGE_SECTION_LMA
:
2515 case OPTION_CHANGE_SECTION_VMA
:
2520 char *option
= NULL
;
2522 enum change_action what
= CHANGE_IGNORE
;
2526 case OPTION_CHANGE_SECTION_ADDRESS
:
2527 option
= "--change-section-address";
2529 case OPTION_CHANGE_SECTION_LMA
:
2530 option
= "--change-section-lma";
2532 case OPTION_CHANGE_SECTION_VMA
:
2533 option
= "--change-section-vma";
2537 s
= strchr (optarg
, '=');
2540 s
= strchr (optarg
, '+');
2543 s
= strchr (optarg
, '-');
2545 fatal (_("bad format for %s"), option
);
2550 name
= xmalloc (len
+ 1);
2551 strncpy (name
, optarg
, len
);
2554 p
= find_section_list (name
, TRUE
);
2556 val
= parse_vma (s
+ 1, option
);
2560 case '=': what
= CHANGE_SET
; break;
2561 case '-': val
= - val
; /* Drop through. */
2562 case '+': what
= CHANGE_MODIFY
; break;
2567 case OPTION_CHANGE_SECTION_ADDRESS
:
2568 p
->change_vma
= what
;
2572 case OPTION_CHANGE_SECTION_LMA
:
2573 p
->change_lma
= what
;
2577 case OPTION_CHANGE_SECTION_VMA
:
2578 p
->change_vma
= what
;
2585 case OPTION_CHANGE_ADDRESSES
:
2586 change_section_address
= parse_vma (optarg
, "--change-addresses");
2587 change_start
= change_section_address
;
2590 case OPTION_CHANGE_WARNINGS
:
2594 case OPTION_CHANGE_LEADING_CHAR
:
2595 change_leading_char
= TRUE
;
2598 case OPTION_DEBUGGING
:
2599 convert_debugging
= TRUE
;
2602 case OPTION_GAP_FILL
:
2604 bfd_vma gap_fill_vma
;
2606 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2607 gap_fill
= (bfd_byte
) gap_fill_vma
;
2608 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2612 sprintf_vma (buff
, gap_fill_vma
);
2614 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2617 gap_fill_set
= TRUE
;
2621 case OPTION_NO_CHANGE_WARNINGS
:
2622 change_warn
= FALSE
;
2626 pad_to
= parse_vma (optarg
, "--pad-to");
2630 case OPTION_REMOVE_LEADING_CHAR
:
2631 remove_leading_char
= TRUE
;
2634 case OPTION_REDEFINE_SYM
:
2636 /* Push this redefinition onto redefine_symbol_list. */
2640 const char *nextarg
;
2641 char *source
, *target
;
2643 s
= strchr (optarg
, '=');
2645 fatal (_("bad format for %s"), "--redefine-sym");
2648 source
= xmalloc (len
+ 1);
2649 strncpy (source
, optarg
, len
);
2653 len
= strlen (nextarg
);
2654 target
= xmalloc (len
+ 1);
2655 strcpy (target
, nextarg
);
2657 redefine_list_append ("--redefine-sym", source
, target
);
2664 case OPTION_REDEFINE_SYMS
:
2665 add_redefine_syms_file (optarg
);
2668 case OPTION_SET_SECTION_FLAGS
:
2674 s
= strchr (optarg
, '=');
2676 fatal (_("bad format for %s"), "--set-section-flags");
2679 name
= xmalloc (len
+ 1);
2680 strncpy (name
, optarg
, len
);
2683 p
= find_section_list (name
, TRUE
);
2685 p
->set_flags
= TRUE
;
2686 p
->flags
= parse_flags (s
+ 1);
2690 case OPTION_RENAME_SECTION
:
2693 const char *eq
, *fl
;
2698 eq
= strchr (optarg
, '=');
2700 fatal (_("bad format for %s"), "--rename-section");
2704 fatal (_("bad format for %s"), "--rename-section");
2706 old_name
= xmalloc (len
+ 1);
2707 strncpy (old_name
, optarg
, len
);
2711 fl
= strchr (eq
, ',');
2714 flags
= parse_flags (fl
+ 1);
2724 fatal (_("bad format for %s"), "--rename-section");
2726 new_name
= xmalloc (len
+ 1);
2727 strncpy (new_name
, eq
, len
);
2730 add_section_rename (old_name
, new_name
, flags
);
2734 case OPTION_SET_START
:
2735 set_start
= parse_vma (optarg
, "--set-start");
2736 set_start_set
= TRUE
;
2739 case OPTION_SREC_LEN
:
2740 Chunk
= parse_vma (optarg
, "--srec-len");
2743 case OPTION_SREC_FORCES3
:
2747 case OPTION_STRIP_SYMBOLS
:
2748 add_specific_symbols (optarg
, &strip_specific_list
);
2751 case OPTION_KEEP_SYMBOLS
:
2752 add_specific_symbols (optarg
, &keep_specific_list
);
2755 case OPTION_LOCALIZE_SYMBOLS
:
2756 add_specific_symbols (optarg
, &localize_specific_list
);
2759 case OPTION_KEEPGLOBAL_SYMBOLS
:
2760 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2763 case OPTION_WEAKEN_SYMBOLS
:
2764 add_specific_symbols (optarg
, &weaken_specific_list
);
2767 case OPTION_ALT_MACH_CODE
:
2768 use_alt_mach_code
= atoi (optarg
);
2769 if (use_alt_mach_code
<= 0)
2770 fatal (_("alternate machine code index must be positive"));
2773 case OPTION_PREFIX_SYMBOLS
:
2774 prefix_symbols_string
= optarg
;
2777 case OPTION_PREFIX_SECTIONS
:
2778 prefix_sections_string
= optarg
;
2781 case OPTION_PREFIX_ALLOC_SECTIONS
:
2782 prefix_alloc_sections_string
= optarg
;
2786 /* We've been given a long option. */
2791 copy_usage (stdout
, 0);
2794 copy_usage (stderr
, 1);
2805 print_version ("objcopy");
2807 if (copy_byte
>= interleave
)
2808 fatal (_("byte number must be less than interleave"));
2810 if (optind
== argc
|| optind
+ 2 < argc
)
2811 copy_usage (stderr
, 1);
2813 input_filename
= argv
[optind
];
2814 if (optind
+ 1 < argc
)
2815 output_filename
= argv
[optind
+ 1];
2817 /* Default is to strip no symbols. */
2818 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2819 strip_symbols
= STRIP_NONE
;
2821 if (output_target
== NULL
)
2822 output_target
= input_target
;
2824 if (binary_architecture
!= NULL
)
2826 if (input_target
&& strcmp (input_target
, "binary") == 0)
2828 const bfd_arch_info_type
* temp_arch_info
;
2830 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2832 if (temp_arch_info
!= NULL
)
2834 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2835 bfd_external_machine
= temp_arch_info
->mach
;
2838 fatal (_("architecture %s unknown"), binary_architecture
);
2842 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2843 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2848 if (stat (input_filename
, & statbuf
) < 0)
2849 fatal (_("warning: could not locate '%s'. System error message: %s"),
2850 input_filename
, strerror (errno
));
2852 /* If there is no destination file, or the source and destination files
2853 are the same, then create a temp and rename the result into the input. */
2854 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
2856 char *tmpname
= make_tempname (input_filename
);
2858 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2862 set_times (tmpname
, &statbuf
);
2863 smart_rename (tmpname
, input_filename
, preserve_dates
);
2870 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2872 if (status
== 0 && preserve_dates
)
2873 set_times (output_filename
, &statbuf
);
2878 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2882 if (p
->change_vma
!= CHANGE_IGNORE
)
2886 sprintf_vma (buff
, p
->vma_val
);
2888 /* xgettext:c-format */
2889 non_fatal (_("%s %s%c0x%s never used"),
2890 "--change-section-vma",
2892 p
->change_vma
== CHANGE_SET
? '=' : '+',
2896 if (p
->change_lma
!= CHANGE_IGNORE
)
2900 sprintf_vma (buff
, p
->lma_val
);
2902 /* xgettext:c-format */
2903 non_fatal (_("%s %s%c0x%s never used"),
2904 "--change-section-lma",
2906 p
->change_lma
== CHANGE_SET
? '=' : '+',
2917 main (int argc
, char *argv
[])
2919 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2920 setlocale (LC_MESSAGES
, "");
2922 #if defined (HAVE_SETLOCALE)
2923 setlocale (LC_CTYPE
, "");
2925 bindtextdomain (PACKAGE
, LOCALEDIR
);
2926 textdomain (PACKAGE
);
2928 program_name
= argv
[0];
2929 xmalloc_set_program_name (program_name
);
2931 START_PROGRESS (program_name
, 0);
2933 strip_symbols
= STRIP_UNDEF
;
2934 discard_locals
= LOCALS_UNDEF
;
2937 set_default_bfd_target ();
2941 int i
= strlen (program_name
);
2942 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2943 /* Drop the .exe suffix, if any. */
2944 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2947 program_name
[i
] = '\0';
2950 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2954 strip_main (argc
, argv
);
2956 copy_main (argc
, argv
);
2958 END_PROGRESS (program_name
);