1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "libiberty.h"
29 #include "filenames.h"
32 /* A list of symbols to explicitly strip out, or to keep. A linked
33 list is good enough for a small number from the command line, but
34 this will slow things down a lot if many symbols are being
43 /* A list to support redefine_sym. */
48 struct redefine_node
*next
;
51 typedef struct section_rename
53 const char * old_name
;
54 const char * new_name
;
56 struct section_rename
* next
;
60 /* List of sections to be renamed. */
61 static section_rename
* section_rename_list
;
63 static void copy_usage
PARAMS ((FILE *, int));
64 static void strip_usage
PARAMS ((FILE *, int));
65 static flagword parse_flags
PARAMS ((const char *));
66 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
67 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
68 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
69 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
70 static int compare_section_lma
PARAMS ((const PTR
, const PTR
));
71 static void add_specific_symbol
PARAMS ((const char *, struct symlist
**));
72 static void add_specific_symbols
PARAMS ((const char *, struct symlist
**));
73 static boolean is_specified_symbol
PARAMS ((const char *, struct symlist
*));
74 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
75 static unsigned int filter_symbols
76 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
77 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
78 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
79 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
80 static void copy_object
PARAMS ((bfd
*, bfd
*));
81 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
83 PARAMS ((const char *, const char *, const char *, const char *));
84 static int strip_main
PARAMS ((int, char **));
85 static int copy_main
PARAMS ((int, char **));
86 static const char *lookup_sym_redefinition
PARAMS((const char *));
87 static void redefine_list_append
PARAMS ((const char *, const char *));
88 static const char * find_section_rename
PARAMS ((bfd
*, sec_ptr
, flagword
*));
89 static void add_section_rename
PARAMS ((const char *, const char *, flagword
));
91 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
93 static asymbol
**isympp
= NULL
; /* Input symbols */
94 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
96 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
97 static int copy_byte
= -1;
98 static int interleave
= 4;
100 static boolean verbose
; /* Print file and target names. */
101 static boolean preserve_dates
; /* Preserve input file timestamp. */
102 static int status
= 0; /* Exit status. */
107 STRIP_NONE
, /* don't strip */
108 STRIP_DEBUG
, /* strip all debugger symbols */
109 STRIP_UNNEEDED
, /* strip unnecessary symbols */
110 STRIP_ALL
/* strip all symbols */
113 /* Which symbols to remove. */
114 static enum strip_action strip_symbols
;
119 LOCALS_START_L
, /* discard locals starting with L */
120 LOCALS_ALL
/* discard all locals */
123 /* Which local symbols to remove. Overrides STRIP_ALL. */
124 static enum locals_action discard_locals
;
126 /* What kind of change to perform. */
134 /* Structure used to hold lists of sections and actions to take. */
137 struct section_list
* next
; /* Next section to change. */
138 const char * name
; /* Section name. */
139 boolean used
; /* Whether this entry was used. */
140 boolean remove
; /* Whether to remove this section. */
141 boolean copy
; /* Whether to copy this section. */
142 enum change_action change_vma
;/* Whether to change or set VMA. */
143 bfd_vma vma_val
; /* Amount to change by or set to. */
144 enum change_action change_lma
;/* Whether to change or set LMA. */
145 bfd_vma lma_val
; /* Amount to change by or set to. */
146 boolean set_flags
; /* Whether to set the section flags. */
147 flagword flags
; /* What to set the section flags to. */
150 static struct section_list
*change_sections
;
152 /* True if some sections are to be removed. */
153 static boolean sections_removed
;
155 /* True if only some sections are to be copied. */
156 static boolean sections_copied
;
158 /* Changes to the start address. */
159 static bfd_vma change_start
= 0;
160 static boolean set_start_set
= false;
161 static bfd_vma set_start
;
163 /* Changes to section addresses. */
164 static bfd_vma change_section_address
= 0;
166 /* Filling gaps between sections. */
167 static boolean gap_fill_set
= false;
168 static bfd_byte gap_fill
= 0;
170 /* Pad to a given address. */
171 static boolean pad_to_set
= false;
172 static bfd_vma pad_to
;
174 /* Use alternate machine code? */
175 static int use_alt_mach_code
= 0;
177 /* List of sections to add. */
180 /* Next section to add. */
181 struct section_add
*next
;
182 /* Name of section to add. */
184 /* Name of file holding section contents. */
185 const char *filename
;
188 /* Contents of file. */
190 /* BFD section, after it has been added. */
194 /* List of sections to add to the output BFD. */
195 static struct section_add
*add_sections
;
197 /* Whether to convert debugging information. */
198 static boolean convert_debugging
= false;
200 /* Whether to change the leading character in symbol names. */
201 static boolean change_leading_char
= false;
203 /* Whether to remove the leading character from global symbol names. */
204 static boolean remove_leading_char
= false;
206 /* List of symbols to strip, keep, localize, keep-global, weaken,
208 static struct symlist
*strip_specific_list
= NULL
;
209 static struct symlist
*keep_specific_list
= NULL
;
210 static struct symlist
*localize_specific_list
= NULL
;
211 static struct symlist
*keepglobal_specific_list
= NULL
;
212 static struct symlist
*weaken_specific_list
= NULL
;
213 static struct redefine_node
*redefine_sym_list
= NULL
;
215 /* If this is true, we weaken global symbols (set BSF_WEAK). */
216 static boolean weaken
= false;
218 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
220 #define OPTION_ADD_SECTION 150
221 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
222 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
223 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
224 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
225 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
226 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
227 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
228 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
229 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
230 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
231 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
232 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
233 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
234 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
235 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
236 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
237 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
238 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYM + 1)
239 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
240 #define OPTION_STRIP_SYMBOLS (OPTION_SREC_FORCES3 + 1)
241 #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1)
242 #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
243 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
244 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
245 #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1)
246 #define OPTION_ALT_MACH_CODE (OPTION_RENAME_SECTION + 1)
248 /* Options to handle if running as "strip". */
250 static struct option strip_options
[] =
252 {"discard-all", no_argument
, 0, 'x'},
253 {"discard-locals", no_argument
, 0, 'X'},
254 {"format", required_argument
, 0, 'F'}, /* Obsolete */
255 {"help", no_argument
, 0, 'h'},
256 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
257 {"input-target", required_argument
, 0, 'I'},
258 {"keep-symbol", required_argument
, 0, 'K'},
259 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
260 {"output-target", required_argument
, 0, 'O'},
261 {"output-file", required_argument
, 0, 'o'},
262 {"preserve-dates", no_argument
, 0, 'p'},
263 {"remove-section", required_argument
, 0, 'R'},
264 {"strip-all", no_argument
, 0, 's'},
265 {"strip-debug", no_argument
, 0, 'S'},
266 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
267 {"strip-symbol", required_argument
, 0, 'N'},
268 {"target", required_argument
, 0, 'F'},
269 {"verbose", no_argument
, 0, 'v'},
270 {"version", no_argument
, 0, 'V'},
271 {0, no_argument
, 0, 0}
274 /* Options to handle if running as "objcopy". */
276 static struct option copy_options
[] =
278 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
279 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
280 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
281 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
282 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
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 {"only-section", required_argument
, 0, 'j'},
296 {"format", required_argument
, 0, 'F'}, /* Obsolete */
297 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
298 {"help", no_argument
, 0, 'h'},
299 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
300 {"input-target", required_argument
, 0, 'I'},
301 {"interleave", required_argument
, 0, 'i'},
302 {"keep-symbol", required_argument
, 0, 'K'},
303 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
304 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
305 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
306 {"output-target", required_argument
, 0, 'O'},
307 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
308 {"preserve-dates", no_argument
, 0, 'p'},
309 {"localize-symbol", required_argument
, 0, 'L'},
310 {"keep-global-symbol", required_argument
, 0, 'G'},
311 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
312 {"remove-section", required_argument
, 0, 'R'},
313 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
314 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
315 {"set-start", required_argument
, 0, OPTION_SET_START
},
316 {"strip-all", no_argument
, 0, 'S'},
317 {"strip-debug", no_argument
, 0, 'g'},
318 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
319 {"strip-symbol", required_argument
, 0, 'N'},
320 {"target", required_argument
, 0, 'F'},
321 {"verbose", no_argument
, 0, 'v'},
322 {"version", no_argument
, 0, 'V'},
323 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
324 {"weaken-symbol", required_argument
, 0, 'W'},
325 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
326 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
327 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
328 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
329 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
330 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
331 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
332 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
333 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
334 {0, no_argument
, 0, 0}
338 extern char *program_name
;
340 /* This flag distinguishes between strip and objcopy:
341 1 means this is 'strip'; 0 means this is 'objcopy'.
342 -1 means if we should use argv[0] to decide. */
345 /* The maximum length of an S record. This variable is declared in srec.c
346 and can be modified by the --srec-len parameter. */
347 extern unsigned int Chunk
;
349 /* Restrict the generation of Srecords to type S3 only.
350 This variable is declare in bfd/srec.c and can be toggled
351 on by the --srec-forceS3 command line switch. */
352 extern boolean S3Forced
;
354 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
355 extern enum bfd_architecture bfd_external_binary_architecture
;
359 copy_usage (stream
, exit_status
)
363 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
364 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
365 fprintf (stream
, _(" The options are:\n"));
366 fprintf (stream
, _("\
367 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
368 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
369 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
370 -F --target <bfdname> Set both input and output format to <bfdname>\n\
371 --debugging Convert debugging information, if possible\n\
372 -p --preserve-dates Copy modified/access timestamps to the output\n\
373 -j --only-section <name> Only copy section <name> into the output\n\
374 -R --remove-section <name> Remove section <name> from the output\n\
375 -S --strip-all Remove all symbol and relocation information\n\
376 -g --strip-debug Remove all debugging symbols\n\
377 --strip-unneeded Remove all symbols not needed by relocations\n\
378 -N --strip-symbol <name> Do not copy symbol <name>\n\
379 -K --keep-symbol <name> Only copy symbol <name>\n\
380 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
381 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
382 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
383 --weaken Force all global symbols to be marked as weak\n\
384 -x --discard-all Remove all non-global symbols\n\
385 -X --discard-locals Remove any compiler-generated symbols\n\
386 -i --interleave <number> Only copy one out of every <number> bytes\n\
387 -b --byte <num> Select byte <num> in every interleaved block\n\
388 --gap-fill <val> Fill gaps between sections with <val>\n\
389 --pad-to <addr> Pad the last section up to address <addr>\n\
390 --set-start <addr> Set the start address to <addr>\n\
391 {--change-start|--adjust-start} <incr>\n\
392 Add <incr> to the start address\n\
393 {--change-addresses|--adjust-vma} <incr>\n\
394 Add <incr> to LMA, VMA and start addresses\n\
395 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
396 Change LMA and VMA of section <name> by <val>\n\
397 --change-section-lma <name>{=|+|-}<val>\n\
398 Change the LMA of section <name> by <val>\n\
399 --change-section-vma <name>{=|+|-}<val>\n\
400 Change the VMA of section <name> by <val>\n\
401 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
402 Warn if a named section does not exist\n\
403 --set-section-flags <name>=<flags>\n\
404 Set section <name>'s properties to <flags>\n\
405 --add-section <name>=<file> Add section <name> found in <file> to output\n\
406 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
407 --change-leading-char Force output format's leading character style\n\
408 --remove-leading-char Remove leading character from global symbols\n\
409 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
410 --srec-len <number> Restrict the length of generated Srecords\n\
411 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
412 --strip-symbols <file> -N for all symbols listed in <file>\n\
413 --keep-symbols <file> -K for all symbols listed in <file>\n\
414 --localize-symbols <file> -L for all symbols listed in <file>\n\
415 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
416 --weaken-symbols <file> -W for all symbols listed in <file>\n\
417 --alt-machine-code <index> Use alternate machine code for output\n\
418 -v --verbose List all object files modified\n\
419 -V --version Display this program's version number\n\
420 -h --help Display this output\n\
422 list_supported_targets (program_name
, stream
);
423 if (exit_status
== 0)
424 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
429 strip_usage (stream
, exit_status
)
433 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
434 fprintf (stream
, _(" Removes symbols and sections from files\n"));
435 fprintf (stream
, _(" The options are:\n"));
436 fprintf (stream
, _("\
437 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
438 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
439 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
440 -p --preserve-dates Copy modified/access timestamps to the output\n\
441 -R --remove-section=<name> Remove section <name> from the output\n\
442 -s --strip-all Remove all symbol and relocation information\n\
443 -g -S -d --strip-debug Remove all debugging symbols\n\
444 --strip-unneeded Remove all symbols not needed by relocations\n\
445 -N --strip-symbol=<name> Do not copy symbol <name>\n\
446 -K --keep-symbol=<name> Only copy symbol <name>\n\
447 -x --discard-all Remove all non-global symbols\n\
448 -X --discard-locals Remove any compiler-generated symbols\n\
449 -v --verbose List all object files modified\n\
450 -V --version Display this program's version number\n\
451 -h --help Display this output\n\
452 -o <file> Place stripped output into <file>\n\
455 list_supported_targets (program_name
, stream
);
456 if (exit_status
== 0)
457 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
461 /* Parse section flags into a flagword, with a fatal error if the
462 string can't be parsed. */
476 snext
= strchr (s
, ',');
486 #define PARSE_FLAG(fname,fval) \
487 else if (strncasecmp (fname, s, len) == 0) ret |= fval
488 PARSE_FLAG ("alloc", SEC_ALLOC
);
489 PARSE_FLAG ("load", SEC_LOAD
);
490 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
491 PARSE_FLAG ("readonly", SEC_READONLY
);
492 PARSE_FLAG ("debug", SEC_DEBUGGING
);
493 PARSE_FLAG ("code", SEC_CODE
);
494 PARSE_FLAG ("data", SEC_DATA
);
495 PARSE_FLAG ("rom", SEC_ROM
);
496 PARSE_FLAG ("share", SEC_SHARED
);
497 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
503 copy
= xmalloc (len
+ 1);
504 strncpy (copy
, s
, len
);
506 non_fatal (_("unrecognized section flag `%s'"), copy
);
507 fatal (_("supported flags: %s"),
508 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
518 /* Find and optionally add an entry in the change_sections list. */
520 static struct section_list
*
521 find_section_list (name
, add
)
525 register struct section_list
*p
;
527 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
528 if (strcmp (p
->name
, name
) == 0)
534 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
539 p
->change_vma
= CHANGE_IGNORE
;
540 p
->change_lma
= CHANGE_IGNORE
;
543 p
->set_flags
= false;
546 p
->next
= change_sections
;
552 /* Add a symbol to strip_specific_list. */
555 add_specific_symbol (name
, list
)
557 struct symlist
**list
;
559 struct symlist
*tmp_list
;
561 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
562 tmp_list
->name
= name
;
563 tmp_list
->next
= *list
;
567 /* Add symbols listed in `filename' to strip_specific_list. */
569 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
570 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
573 add_specific_symbols (filename
, list
)
574 const char *filename
;
575 struct symlist
**list
;
581 unsigned int line_count
;
583 if (stat (filename
, & st
) < 0)
584 fatal (_("cannot stat: %s: %s"), filename
, strerror (errno
));
588 buffer
= (char *) xmalloc (st
.st_size
+ 2);
589 f
= fopen (filename
, FOPEN_RT
);
591 fatal (_("cannot open: %s: %s"), filename
, strerror (errno
));
593 if (fread (buffer
, 1, st
.st_size
, f
) == 0 || ferror (f
))
594 fatal (_("%s: fread failed"), filename
);
597 buffer
[st
.st_size
] = '\n';
598 buffer
[st
.st_size
+ 1] = '\0';
602 for (line
= buffer
; * line
!= '\0'; line
++)
607 int finished
= false;
609 for (eol
= line
;; eol
++)
615 /* Cope with \n\r. */
623 /* Cope with \r\n. */
634 /* Line comment, Terminate the line here, in case a
635 name is present and then allow the rest of the
636 loop to find the real end of the line. */
648 /* A name may now exist somewhere between 'line' and 'eol'.
649 Strip off leading whitespace and trailing whitespace,
650 then add it to the list. */
651 for (name
= line
; IS_WHITESPACE (* name
); name
++)
653 for (name_end
= name
;
654 (! IS_WHITESPACE (* name_end
))
655 && (! IS_LINE_TERMINATOR (* name_end
));
659 if (! IS_LINE_TERMINATOR (* name_end
))
663 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
666 if (! IS_LINE_TERMINATOR (* extra
))
667 non_fatal (_("Ignoring rubbish found on line %d of %s"),
668 line_count
, filename
);
674 add_specific_symbol (name
, list
);
676 /* Advance line pointer to end of line. The 'eol ++' in the for
677 loop above will then advance us to the start of the next line. */
683 /* See whether a symbol should be stripped or kept based on
684 strip_specific_list and keep_symbols. */
687 is_specified_symbol (name
, list
)
689 struct symlist
*list
;
691 struct symlist
*tmp_list
;
693 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
694 if (strcmp (name
, tmp_list
->name
) == 0)
700 /* See if a section is being removed. */
703 is_strip_section (abfd
, sec
)
704 bfd
*abfd ATTRIBUTE_UNUSED
;
707 struct section_list
*p
;
709 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
710 && (strip_symbols
== STRIP_DEBUG
711 || strip_symbols
== STRIP_UNNEEDED
712 || strip_symbols
== STRIP_ALL
713 || discard_locals
== LOCALS_ALL
714 || convert_debugging
))
717 if (! sections_removed
&& ! sections_copied
)
720 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
721 if (sections_removed
&& p
!= NULL
&& p
->remove
)
723 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
728 /* Choose which symbol entries to copy; put the result in OSYMS.
729 We don't copy in place, because that confuses the relocs.
730 Return the number of symbols to print. */
733 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
736 asymbol
**osyms
, **isyms
;
739 register asymbol
**from
= isyms
, **to
= osyms
;
740 long src_count
= 0, dst_count
= 0;
741 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
744 for (; src_count
< symcount
; src_count
++)
746 asymbol
*sym
= from
[src_count
];
747 flagword flags
= sym
->flags
;
748 const char *name
= bfd_asymbol_name (sym
);
752 if (redefine_sym_list
)
754 const char *old_name
, *new_name
;
756 old_name
= bfd_asymbol_name (sym
);
757 new_name
= lookup_sym_redefinition (old_name
);
758 name
= bfd_asymbol_name (sym
) = new_name
;
761 if (change_leading_char
762 && (bfd_get_symbol_leading_char (abfd
)
763 != bfd_get_symbol_leading_char (obfd
))
764 && (bfd_get_symbol_leading_char (abfd
) == '\0'
765 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
767 if (bfd_get_symbol_leading_char (obfd
) == '\0')
768 name
= bfd_asymbol_name (sym
) = name
+ 1;
773 n
= xmalloc (strlen (name
) + 2);
774 n
[0] = bfd_get_symbol_leading_char (obfd
);
775 if (bfd_get_symbol_leading_char (abfd
) == '\0')
776 strcpy (n
+ 1, name
);
778 strcpy (n
+ 1, name
+ 1);
779 name
= bfd_asymbol_name (sym
) = n
;
783 undefined
= bfd_is_und_section (bfd_get_section (sym
));
785 if (remove_leading_char
786 && ((flags
& BSF_GLOBAL
) != 0
787 || (flags
& BSF_WEAK
) != 0
789 || bfd_is_com_section (bfd_get_section (sym
)))
790 && name
[0] == bfd_get_symbol_leading_char (abfd
))
791 name
= bfd_asymbol_name (sym
) = name
+ 1;
793 if (strip_symbols
== STRIP_ALL
)
795 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
796 || ((flags
& BSF_SECTION_SYM
) != 0
797 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
800 else if (relocatable
/* Relocatable file. */
801 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
803 else if (bfd_decode_symclass (sym
) == 'I')
804 /* Global symbols in $idata sections need to be retained
805 even if relocatable is false. External users of the
806 library containing the $idata section may reference these
809 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
810 || (flags
& BSF_WEAK
) != 0
812 || bfd_is_com_section (bfd_get_section (sym
)))
813 keep
= strip_symbols
!= STRIP_UNNEEDED
;
814 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
815 keep
= (strip_symbols
!= STRIP_DEBUG
816 && strip_symbols
!= STRIP_UNNEEDED
817 && ! convert_debugging
);
818 else if (bfd_get_section (sym
)->comdat
)
819 /* COMDAT sections store special information in local
820 symbols, so we cannot risk stripping any of them. */
822 else /* Local symbol. */
823 keep
= (strip_symbols
!= STRIP_UNNEEDED
824 && (discard_locals
!= LOCALS_ALL
825 && (discard_locals
!= LOCALS_START_L
826 || ! bfd_is_local_label (abfd
, sym
))));
828 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
830 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
832 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
835 if (keep
&& (flags
& BSF_GLOBAL
) != 0
836 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
838 sym
->flags
&=~ BSF_GLOBAL
;
839 sym
->flags
|= BSF_WEAK
;
841 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
842 && (is_specified_symbol (name
, localize_specific_list
)
843 || (keepglobal_specific_list
!= NULL
844 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
846 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
847 sym
->flags
|= BSF_LOCAL
;
851 to
[dst_count
++] = sym
;
854 to
[dst_count
] = NULL
;
859 /* Find the redefined name of symbol SOURCE. */
862 lookup_sym_redefinition (source
)
865 struct redefine_node
*list
;
867 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
868 if (strcmp (source
, list
->source
) == 0)
874 /* Add a node to a symbol redefine list. */
877 redefine_list_append (source
, target
)
881 struct redefine_node
**p
;
882 struct redefine_node
*list
;
883 struct redefine_node
*new_node
;
885 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
887 if (strcmp (source
, list
->source
) == 0)
888 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
892 if (strcmp (target
, list
->target
) == 0)
893 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
898 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
900 new_node
->source
= strdup (source
);
901 new_node
->target
= strdup (target
);
902 new_node
->next
= NULL
;
907 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
911 filter_bytes (memhunk
, size
)
915 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
917 for (; from
< end
; from
+= interleave
)
920 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
921 *size
= (*size
/ interleave
) + 1;
926 /* Copy object file IBFD onto OBFD. */
929 copy_object (ibfd
, obfd
)
935 asection
**osections
= NULL
;
936 bfd_size_type
*gaps
= NULL
;
937 bfd_size_type max_gap
= 0;
941 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
942 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
943 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
945 fatal (_("Unable to change endianness of input file(s)"));
949 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
950 RETURN_NONFATAL (bfd_get_filename (obfd
));
953 printf (_("copy from %s(%s) to %s(%s)\n"),
954 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
955 bfd_get_filename (obfd
), bfd_get_target (obfd
));
960 start
= bfd_get_start_address (ibfd
);
961 start
+= change_start
;
963 /* Neither the start address nor the flags
964 need to be set for a core file. */
965 if (bfd_get_format (obfd
) != bfd_core
)
967 if (!bfd_set_start_address (obfd
, start
)
968 || !bfd_set_file_flags (obfd
,
969 (bfd_get_file_flags (ibfd
)
970 & bfd_applicable_file_flags (obfd
))))
971 RETURN_NONFATAL (bfd_get_filename (ibfd
));
974 /* Copy architecture of input file to output file. */
975 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
976 bfd_get_mach (ibfd
)))
977 non_fatal (_("Warning: Output file cannot represent architecture %s"),
978 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
979 bfd_get_mach (ibfd
)));
981 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
982 RETURN_NONFATAL (bfd_get_filename (ibfd
));
987 if (osympp
!= isympp
)
990 /* BFD mandates that all output sections be created and sizes set before
991 any output is done. Thus, we traverse all sections multiple times. */
992 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
994 if (add_sections
!= NULL
)
996 struct section_add
*padd
;
997 struct section_list
*pset
;
999 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1001 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1002 if (padd
->section
== NULL
)
1004 non_fatal (_("can't create section `%s': %s"),
1005 padd
->name
, bfd_errmsg (bfd_get_error ()));
1013 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1014 RETURN_NONFATAL (bfd_get_filename (obfd
));
1016 pset
= find_section_list (padd
->name
, false);
1020 if (pset
!= NULL
&& pset
->set_flags
)
1021 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1023 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1025 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1026 RETURN_NONFATAL (bfd_get_filename (obfd
));
1030 if (pset
->change_vma
!= CHANGE_IGNORE
)
1031 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
1032 RETURN_NONFATAL (bfd_get_filename (obfd
));
1034 if (pset
->change_lma
!= CHANGE_IGNORE
)
1036 padd
->section
->lma
= pset
->lma_val
;
1038 if (! bfd_set_section_alignment
1039 (obfd
, padd
->section
,
1040 bfd_section_alignment (obfd
, padd
->section
)))
1041 RETURN_NONFATAL (bfd_get_filename (obfd
));
1048 if (gap_fill_set
|| pad_to_set
)
1053 /* We must fill in gaps between the sections and/or we must pad
1054 the last section to a specified address. We do this by
1055 grabbing a list of the sections, sorting them by VMA, and
1056 increasing the section sizes as required to fill the gaps.
1057 We write out the gap contents below. */
1059 c
= bfd_count_sections (obfd
);
1060 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
1062 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
1064 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1066 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
1067 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1071 for (i
= 0; i
< c
- 1; i
++)
1075 bfd_vma gap_start
, gap_stop
;
1077 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1078 if ((flags
& SEC_HAS_CONTENTS
) == 0
1079 || (flags
& SEC_LOAD
) == 0)
1082 size
= bfd_section_size (obfd
, osections
[i
]);
1083 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1084 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1085 if (gap_start
< gap_stop
)
1087 if (! bfd_set_section_size (obfd
, osections
[i
],
1088 size
+ (gap_stop
- gap_start
)))
1090 non_fatal (_("Can't fill gap after %s: %s"),
1091 bfd_get_section_name (obfd
, osections
[i
]),
1092 bfd_errmsg (bfd_get_error ()));
1096 gaps
[i
] = gap_stop
- gap_start
;
1097 if (max_gap
< gap_stop
- gap_start
)
1098 max_gap
= gap_stop
- gap_start
;
1108 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1109 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1110 if (lma
+ size
< pad_to
)
1112 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1115 non_fatal (_("Can't add padding to %s: %s"),
1116 bfd_get_section_name (obfd
, osections
[c
- 1]),
1117 bfd_errmsg (bfd_get_error ()));
1122 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1123 if (max_gap
< pad_to
- (lma
+ size
))
1124 max_gap
= pad_to
- (lma
+ size
);
1130 /* Symbol filtering must happen after the output sections
1131 have been created, but before their contents are set. */
1133 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1135 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1137 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1138 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1140 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1142 if (convert_debugging
)
1143 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1145 if (strip_symbols
== STRIP_DEBUG
1146 || strip_symbols
== STRIP_ALL
1147 || strip_symbols
== STRIP_UNNEEDED
1148 || discard_locals
!= LOCALS_UNDEF
1149 || strip_specific_list
!= NULL
1150 || keep_specific_list
!= NULL
1151 || localize_specific_list
!= NULL
1152 || keepglobal_specific_list
!= NULL
1153 || weaken_specific_list
!= NULL
1156 || convert_debugging
1157 || change_leading_char
1158 || remove_leading_char
1159 || redefine_sym_list
1162 /* Mark symbols used in output relocations so that they
1163 are kept, even if they are local labels or static symbols.
1165 Note we iterate over the input sections examining their
1166 relocations since the relocations for the output sections
1167 haven't been set yet. mark_symbols_used_in_relocations will
1168 ignore input sections which have no corresponding output
1170 if (strip_symbols
!= STRIP_ALL
)
1171 bfd_map_over_sections (ibfd
,
1172 mark_symbols_used_in_relocations
,
1174 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1175 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1178 if (convert_debugging
&& dhandle
!= NULL
)
1180 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1187 bfd_set_symtab (obfd
, osympp
, symcount
);
1189 /* This has to happen after the symbol table has been set. */
1190 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
1192 if (add_sections
!= NULL
)
1194 struct section_add
*padd
;
1196 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1198 if (! bfd_set_section_contents (obfd
, padd
->section
,
1199 (PTR
) padd
->contents
,
1201 (bfd_size_type
) padd
->size
))
1202 RETURN_NONFATAL (bfd_get_filename (obfd
));
1206 if (gap_fill_set
|| pad_to_set
)
1211 /* Fill in the gaps. */
1214 buf
= (bfd_byte
*) xmalloc (max_gap
);
1215 memset (buf
, gap_fill
, (size_t) max_gap
);
1217 c
= bfd_count_sections (obfd
);
1218 for (i
= 0; i
< c
; i
++)
1226 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1237 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1239 RETURN_NONFATAL (bfd_get_filename (obfd
));
1248 /* Allow the BFD backend to copy any private data it understands
1249 from the input BFD to the output BFD. This is done last to
1250 permit the routine to look at the filtered symbol table, which is
1251 important for the ECOFF code at least. */
1252 if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1254 non_fatal (_("%s: error copying private BFD data: %s"),
1255 bfd_get_filename (obfd
),
1256 bfd_errmsg (bfd_get_error ()));
1261 /* Switch to the alternate machine code. We have to do this at the
1262 very end, because we only initialize the header when we create
1263 the first section. */
1264 if (use_alt_mach_code
!= 0)
1266 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1267 non_fatal (_("unknown alternate machine code, ignored"));
1272 #if defined (_WIN32) && !defined (__CYGWIN32__)
1273 #define MKDIR(DIR, MODE) mkdir (DIR)
1275 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1278 /* Read each archive element in turn from IBFD, copy the
1279 contents to temp file, and keep the temp file handle. */
1282 copy_archive (ibfd
, obfd
, output_target
)
1285 const char *output_target
;
1289 struct name_list
*next
;
1293 bfd
**ptr
= &obfd
->archive_head
;
1295 char *dir
= make_tempname (bfd_get_filename (obfd
));
1297 /* Make a temp directory to hold the contents. */
1298 if (MKDIR (dir
, 0700) != 0)
1300 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1301 dir
, strerror (errno
));
1303 obfd
->has_armap
= ibfd
->has_armap
;
1307 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1309 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1310 RETURN_NONFATAL (bfd_get_filename (obfd
));
1312 while (!status
&& this_element
!= (bfd
*) NULL
)
1318 int stat_status
= 0;
1320 /* Create an output file for this member. */
1321 output_name
= concat (dir
, "/",
1322 bfd_get_filename (this_element
), (char *) 0);
1324 /* If the file already exists, make another temp dir. */
1325 if (stat (output_name
, &buf
) >= 0)
1327 output_name
= make_tempname (output_name
);
1328 if (MKDIR (output_name
, 0700) != 0)
1330 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1331 output_name
, strerror (errno
));
1333 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1334 l
->name
= output_name
;
1338 output_name
= concat (output_name
, "/",
1339 bfd_get_filename (this_element
), (char *) 0);
1342 output_bfd
= bfd_openw (output_name
, output_target
);
1345 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1347 if (stat_status
!= 0)
1348 non_fatal (_("internal stat error on %s"),
1349 bfd_get_filename (this_element
));
1352 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1353 l
->name
= output_name
;
1357 if (output_bfd
== (bfd
*) NULL
)
1358 RETURN_NONFATAL (output_name
);
1360 if (bfd_check_format (this_element
, bfd_object
) == true)
1361 copy_object (this_element
, output_bfd
);
1363 if (!bfd_close (output_bfd
))
1365 bfd_nonfatal (bfd_get_filename (output_bfd
));
1366 /* Error in new object file. Don't change archive. */
1370 if (preserve_dates
&& stat_status
== 0)
1371 set_times (output_name
, &buf
);
1373 /* Open the newly output file and attach to our list. */
1374 output_bfd
= bfd_openr (output_name
, output_target
);
1376 l
->obfd
= output_bfd
;
1379 ptr
= &output_bfd
->next
;
1381 last_element
= this_element
;
1383 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1385 bfd_close (last_element
);
1387 *ptr
= (bfd
*) NULL
;
1389 if (!bfd_close (obfd
))
1390 RETURN_NONFATAL (bfd_get_filename (obfd
));
1392 if (!bfd_close (ibfd
))
1393 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1395 /* Delete all the files that we opened. */
1396 for (l
= list
; l
!= NULL
; l
= l
->next
)
1398 if (l
->obfd
== NULL
)
1402 bfd_close (l
->obfd
);
1409 /* The top-level control. */
1412 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1413 const char *input_filename
;
1414 const char *output_filename
;
1415 const char *input_target
;
1416 const char *output_target
;
1421 /* To allow us to do "strip *" without dying on the first
1422 non-object file, failures are nonfatal. */
1423 ibfd
= bfd_openr (input_filename
, input_target
);
1425 RETURN_NONFATAL (input_filename
);
1427 if (bfd_check_format (ibfd
, bfd_archive
))
1431 /* bfd_get_target does not return the correct value until
1432 bfd_check_format succeeds. */
1433 if (output_target
== NULL
)
1434 output_target
= bfd_get_target (ibfd
);
1436 obfd
= bfd_openw (output_filename
, output_target
);
1438 RETURN_NONFATAL (output_filename
);
1440 copy_archive (ibfd
, obfd
, output_target
);
1442 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
)
1443 || bfd_check_format_matches (ibfd
, bfd_core
, &matching
))
1447 /* bfd_get_target does not return the correct value until
1448 bfd_check_format succeeds. */
1449 if (output_target
== NULL
)
1450 output_target
= bfd_get_target (ibfd
);
1452 obfd
= bfd_openw (output_filename
, output_target
);
1454 RETURN_NONFATAL (output_filename
);
1456 copy_object (ibfd
, obfd
);
1458 if (!bfd_close (obfd
))
1459 RETURN_NONFATAL (output_filename
);
1461 if (!bfd_close (ibfd
))
1462 RETURN_NONFATAL (input_filename
);
1466 bfd_nonfatal (input_filename
);
1468 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1470 list_matching_formats (matching
);
1478 /* Add a name to the section renaming list. */
1481 add_section_rename (old_name
, new_name
, flags
)
1482 const char * old_name
;
1483 const char * new_name
;
1486 section_rename
* rename
;
1488 /* Check for conflicts first. */
1489 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1490 if (strcmp (rename
->old_name
, old_name
) == 0)
1492 /* Silently ignore duplicate definitions. */
1493 if (strcmp (rename
->new_name
, new_name
) == 0
1494 && rename
->flags
== flags
)
1497 fatal (_("Multiple renames of section %s"), old_name
);
1500 rename
= (section_rename
*) xmalloc (sizeof (* rename
));
1502 rename
->old_name
= old_name
;
1503 rename
->new_name
= new_name
;
1504 rename
->flags
= flags
;
1505 rename
->next
= section_rename_list
;
1507 section_rename_list
= rename
;
1510 /* Check the section rename list for a new name of the input section
1511 ISECTION. Return the new name if one is found.
1512 Also set RETURNED_FLAGS to the flags to be used for this section. */
1515 find_section_rename (ibfd
, isection
, returned_flags
)
1516 bfd
* ibfd ATTRIBUTE_UNUSED
;
1518 flagword
* returned_flags
;
1520 const char * old_name
= bfd_section_name (ibfd
, isection
);
1521 section_rename
* rename
;
1523 /* Default to using the flags of the input section. */
1524 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1526 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1527 if (strcmp (rename
->old_name
, old_name
) == 0)
1529 if (rename
->flags
!= (flagword
) -1)
1530 * returned_flags
= rename
->flags
;
1532 return rename
->new_name
;
1538 /* Create a section in OBFD with the same
1539 name and attributes as ISECTION in IBFD. */
1542 setup_section (ibfd
, isection
, obfdarg
)
1547 bfd
*obfd
= (bfd
*) obfdarg
;
1548 struct section_list
*p
;
1557 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1558 && (strip_symbols
== STRIP_DEBUG
1559 || strip_symbols
== STRIP_UNNEEDED
1560 || strip_symbols
== STRIP_ALL
1561 || discard_locals
== LOCALS_ALL
1562 || convert_debugging
))
1565 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1569 if (sections_removed
&& p
!= NULL
&& p
->remove
)
1571 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
1574 /* Get the, possibly new, name of the output section. */
1575 name
= find_section_rename (ibfd
, isection
, & flags
);
1577 osection
= bfd_make_section_anyway (obfd
, name
);
1579 if (osection
== NULL
)
1585 size
= bfd_section_size (ibfd
, isection
);
1587 size
= (size
+ interleave
- 1) / interleave
;
1588 if (! bfd_set_section_size (obfd
, osection
, size
))
1594 vma
= bfd_section_vma (ibfd
, isection
);
1595 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1597 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1600 vma
+= change_section_address
;
1602 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1608 lma
= isection
->lma
;
1609 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1611 if (p
->change_lma
== CHANGE_MODIFY
)
1613 else if (p
->change_lma
== CHANGE_SET
)
1619 lma
+= change_section_address
;
1621 osection
->lma
= lma
;
1623 /* FIXME: This is probably not enough. If we change the LMA we
1624 may have to recompute the header for the file as well. */
1625 if (bfd_set_section_alignment (obfd
,
1627 bfd_section_alignment (ibfd
, isection
))
1630 err
= _("alignment");
1634 if (p
!= NULL
&& p
->set_flags
)
1635 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1636 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1642 /* Copy merge entity size. */
1643 osection
->entsize
= isection
->entsize
;
1645 /* This used to be mangle_section; we do here to avoid using
1646 bfd_get_section_by_name since some formats allow multiple
1647 sections with the same name. */
1648 isection
->output_section
= osection
;
1649 isection
->output_offset
= 0;
1651 /* Allow the BFD backend to copy any private data it understands
1652 from the input section to the output section. */
1653 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1655 err
= _("private data");
1659 /* All went well. */
1663 non_fatal (_("%s: section `%s': error in %s: %s"),
1664 bfd_get_filename (ibfd
),
1665 bfd_section_name (ibfd
, isection
),
1666 err
, bfd_errmsg (bfd_get_error ()));
1670 /* Copy the data of input section ISECTION of IBFD
1671 to an output section with the same name in OBFD.
1672 If stripping then don't copy any relocation info. */
1675 copy_section (ibfd
, isection
, obfdarg
)
1680 bfd
*obfd
= (bfd
*) obfdarg
;
1681 struct section_list
*p
;
1689 /* If we have already failed earlier on,
1690 do not keep on generating complaints now. */
1694 flags
= bfd_get_section_flags (ibfd
, isection
);
1695 if ((flags
& SEC_DEBUGGING
) != 0
1696 && (strip_symbols
== STRIP_DEBUG
1697 || strip_symbols
== STRIP_UNNEEDED
1698 || strip_symbols
== STRIP_ALL
1699 || discard_locals
== LOCALS_ALL
1700 || convert_debugging
))
1703 if ((flags
& SEC_GROUP
) != 0)
1706 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1708 if (sections_removed
&& p
!= NULL
&& p
->remove
)
1710 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
1713 osection
= isection
->output_section
;
1714 size
= bfd_get_section_size_before_reloc (isection
);
1716 if (size
== 0 || osection
== 0)
1719 /* Core files do not need to be relocated. */
1720 if (bfd_get_format (obfd
) == bfd_core
)
1723 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1726 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1729 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1732 relpp
= (arelent
**) xmalloc (relsize
);
1733 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1735 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1737 if (strip_symbols
== STRIP_ALL
)
1739 /* Remove relocations which are not in
1740 keep_strip_specific_list. */
1741 arelent
**temp_relpp
;
1742 long temp_relcount
= 0;
1745 temp_relpp
= (arelent
**) xmalloc (relsize
);
1746 for (i
= 0; i
< relcount
; i
++)
1747 if (is_specified_symbol
1748 (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1749 keep_specific_list
))
1750 temp_relpp
[temp_relcount
++] = relpp
[i
];
1751 relcount
= temp_relcount
;
1756 bfd_set_reloc (obfd
, osection
,
1757 (relcount
== 0 ? (arelent
**) NULL
: relpp
), relcount
);
1760 isection
->_cooked_size
= isection
->_raw_size
;
1761 isection
->reloc_done
= true;
1763 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
1764 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
1766 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1768 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1770 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1773 filter_bytes (memhunk
, &size
);
1775 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1777 RETURN_NONFATAL (bfd_get_filename (obfd
));
1781 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1783 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1785 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1786 flag--they can just remove the section entirely and add it
1787 back again. However, we do permit them to turn on the
1788 SEC_HAS_CONTENTS flag, and take it to mean that the section
1789 contents should be zeroed out. */
1791 memset (memhunk
, 0, size
);
1792 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1794 RETURN_NONFATAL (bfd_get_filename (obfd
));
1799 /* Get all the sections. This is used when --gap-fill or --pad-to is
1803 get_sections (obfd
, osection
, secppparg
)
1804 bfd
*obfd ATTRIBUTE_UNUSED
;
1808 asection
***secppp
= (asection
***) secppparg
;
1810 **secppp
= osection
;
1814 /* Sort sections by VMA. This is called via qsort, and is used when
1815 --gap-fill or --pad-to is used. We force non loadable or empty
1816 sections to the front, where they are easier to ignore. */
1819 compare_section_lma (arg1
, arg2
)
1823 const asection
**sec1
= (const asection
**) arg1
;
1824 const asection
**sec2
= (const asection
**) arg2
;
1825 flagword flags1
, flags2
;
1827 /* Sort non loadable sections to the front. */
1828 flags1
= (*sec1
)->flags
;
1829 flags2
= (*sec2
)->flags
;
1830 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1831 || (flags1
& SEC_LOAD
) == 0)
1833 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1834 && (flags2
& SEC_LOAD
) != 0)
1839 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1840 || (flags2
& SEC_LOAD
) == 0)
1844 /* Sort sections by LMA. */
1845 if ((*sec1
)->lma
> (*sec2
)->lma
)
1847 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1850 /* Sort sections with the same LMA by size. */
1851 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1853 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1859 /* Mark all the symbols which will be used in output relocations with
1860 the BSF_KEEP flag so that those symbols will not be stripped.
1862 Ignore relocations which will not appear in the output file. */
1865 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1870 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1875 /* Ignore an input section with no corresponding output section. */
1876 if (isection
->output_section
== NULL
)
1879 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1881 bfd_fatal (bfd_get_filename (ibfd
));
1886 relpp
= (arelent
**) xmalloc (relsize
);
1887 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1889 bfd_fatal (bfd_get_filename (ibfd
));
1891 /* Examine each symbol used in a relocation. If it's not one of the
1892 special bfd section symbols, then mark it with BSF_KEEP. */
1893 for (i
= 0; i
< relcount
; i
++)
1895 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1896 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1897 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1898 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1905 /* Write out debugging information. */
1908 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1911 long *symcountp ATTRIBUTE_UNUSED
;
1912 asymbol
***symppp ATTRIBUTE_UNUSED
;
1914 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1915 return write_ieee_debugging_info (obfd
, dhandle
);
1917 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1918 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1920 bfd_byte
*syms
, *strings
;
1921 bfd_size_type symsize
, stringsize
;
1922 asection
*stabsec
, *stabstrsec
;
1924 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1929 stabsec
= bfd_make_section (obfd
, ".stab");
1930 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1932 || stabstrsec
== NULL
1933 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1934 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1935 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1936 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1937 || ! bfd_set_section_flags (obfd
, stabsec
,
1941 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1946 non_fatal (_("%s: can't create debugging section: %s"),
1947 bfd_get_filename (obfd
),
1948 bfd_errmsg (bfd_get_error ()));
1952 /* We can get away with setting the section contents now because
1953 the next thing the caller is going to do is copy over the
1954 real sections. We may someday have to split the contents
1955 setting out of this function. */
1956 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1958 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1959 (file_ptr
) 0, stringsize
))
1961 non_fatal (_("%s: can't set debugging section contents: %s"),
1962 bfd_get_filename (obfd
),
1963 bfd_errmsg (bfd_get_error ()));
1970 non_fatal (_("%s: don't know how to write debugging information for %s"),
1971 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1976 strip_main (argc
, argv
)
1980 char *input_target
= NULL
, *output_target
= NULL
;
1981 boolean show_version
= false;
1983 struct section_list
*p
;
1984 char *output_file
= NULL
;
1986 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVv",
1987 strip_options
, (int *) 0)) != EOF
)
1992 input_target
= optarg
;
1995 output_target
= optarg
;
1998 input_target
= output_target
= optarg
;
2001 p
= find_section_list (optarg
, true);
2003 sections_removed
= true;
2006 strip_symbols
= STRIP_ALL
;
2010 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2011 strip_symbols
= STRIP_DEBUG
;
2013 case OPTION_STRIP_UNNEEDED
:
2014 strip_symbols
= STRIP_UNNEEDED
;
2017 add_specific_symbol (optarg
, &keep_specific_list
);
2020 add_specific_symbol (optarg
, &strip_specific_list
);
2023 output_file
= optarg
;
2026 preserve_dates
= true;
2029 discard_locals
= LOCALS_ALL
;
2032 discard_locals
= LOCALS_START_L
;
2038 show_version
= true;
2041 /* We've been given a long option. */
2045 strip_usage (stdout
, 0);
2047 strip_usage (stderr
, 1);
2052 print_version ("strip");
2054 /* Default is to strip all symbols. */
2055 if (strip_symbols
== STRIP_UNDEF
2056 && discard_locals
== LOCALS_UNDEF
2057 && strip_specific_list
== NULL
)
2058 strip_symbols
= STRIP_ALL
;
2060 if (output_target
== (char *) NULL
)
2061 output_target
= input_target
;
2065 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2066 strip_usage (stderr
, 1);
2068 for (; i
< argc
; i
++)
2070 int hold_status
= status
;
2071 struct stat statbuf
;
2076 if (stat (argv
[i
], &statbuf
) < 0)
2078 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
2083 if (output_file
!= NULL
)
2084 tmpname
= output_file
;
2086 tmpname
= make_tempname (argv
[i
]);
2089 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2093 set_times (tmpname
, &statbuf
);
2094 if (output_file
== NULL
)
2095 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2096 status
= hold_status
;
2100 if (output_file
== NULL
)
2108 copy_main (argc
, argv
)
2112 char * binary_architecture
= NULL
;
2113 char *input_filename
= NULL
, *output_filename
= NULL
;
2114 char *input_target
= NULL
, *output_target
= NULL
;
2115 boolean show_version
= false;
2116 boolean change_warn
= true;
2118 struct section_list
*p
;
2119 struct stat statbuf
;
2121 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2122 copy_options
, (int *) 0)) != EOF
)
2127 copy_byte
= atoi (optarg
);
2129 fatal (_("byte number must be non-negative"));
2133 binary_architecture
= optarg
;
2137 interleave
= atoi (optarg
);
2139 fatal (_("interleave must be positive"));
2143 case 's': /* "source" - 'I' is preferred */
2144 input_target
= optarg
;
2148 case 'd': /* "destination" - 'O' is preferred */
2149 output_target
= optarg
;
2153 input_target
= output_target
= optarg
;
2157 p
= find_section_list (optarg
, true);
2159 fatal (_("%s both copied and removed"), optarg
);
2161 sections_copied
= true;
2165 p
= find_section_list (optarg
, true);
2167 fatal (_("%s both copied and removed"), optarg
);
2169 sections_removed
= true;
2173 strip_symbols
= STRIP_ALL
;
2177 strip_symbols
= STRIP_DEBUG
;
2180 case OPTION_STRIP_UNNEEDED
:
2181 strip_symbols
= STRIP_UNNEEDED
;
2185 add_specific_symbol (optarg
, &keep_specific_list
);
2189 add_specific_symbol (optarg
, &strip_specific_list
);
2193 add_specific_symbol (optarg
, &localize_specific_list
);
2197 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2201 add_specific_symbol (optarg
, &weaken_specific_list
);
2205 preserve_dates
= true;
2209 discard_locals
= LOCALS_ALL
;
2213 discard_locals
= LOCALS_START_L
;
2221 show_version
= true;
2228 case OPTION_ADD_SECTION
:
2232 struct section_add
*pa
;
2237 s
= strchr (optarg
, '=');
2240 fatal (_("bad format for %s"), "--add-section");
2242 if (stat (s
+ 1, & st
) < 0)
2243 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
2245 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
2248 name
= (char *) xmalloc (len
+ 1);
2249 strncpy (name
, optarg
, len
);
2253 pa
->filename
= s
+ 1;
2255 pa
->size
= st
.st_size
;
2257 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
2258 f
= fopen (pa
->filename
, FOPEN_RB
);
2261 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
2263 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2265 fatal (_("%s: fread failed"), pa
->filename
);
2269 pa
->next
= add_sections
;
2274 case OPTION_CHANGE_START
:
2275 change_start
= parse_vma (optarg
, "--change-start");
2278 case OPTION_CHANGE_SECTION_ADDRESS
:
2279 case OPTION_CHANGE_SECTION_LMA
:
2280 case OPTION_CHANGE_SECTION_VMA
:
2285 char *option
= NULL
;
2287 enum change_action what
= CHANGE_IGNORE
;
2291 case OPTION_CHANGE_SECTION_ADDRESS
:
2292 option
= "--change-section-address";
2294 case OPTION_CHANGE_SECTION_LMA
:
2295 option
= "--change-section-lma";
2297 case OPTION_CHANGE_SECTION_VMA
:
2298 option
= "--change-section-vma";
2302 s
= strchr (optarg
, '=');
2305 s
= strchr (optarg
, '+');
2308 s
= strchr (optarg
, '-');
2310 fatal (_("bad format for %s"), option
);
2315 name
= (char *) xmalloc (len
+ 1);
2316 strncpy (name
, optarg
, len
);
2319 p
= find_section_list (name
, true);
2321 val
= parse_vma (s
+ 1, option
);
2325 case '=': what
= CHANGE_SET
; break;
2326 case '-': val
= - val
; /* Drop through. */
2327 case '+': what
= CHANGE_MODIFY
; break;
2332 case OPTION_CHANGE_SECTION_ADDRESS
:
2333 p
->change_vma
= what
;
2337 case OPTION_CHANGE_SECTION_LMA
:
2338 p
->change_lma
= what
;
2342 case OPTION_CHANGE_SECTION_VMA
:
2343 p
->change_vma
= what
;
2350 case OPTION_CHANGE_ADDRESSES
:
2351 change_section_address
= parse_vma (optarg
, "--change-addresses");
2352 change_start
= change_section_address
;
2355 case OPTION_CHANGE_WARNINGS
:
2359 case OPTION_CHANGE_LEADING_CHAR
:
2360 change_leading_char
= true;
2363 case OPTION_DEBUGGING
:
2364 convert_debugging
= true;
2367 case OPTION_GAP_FILL
:
2369 bfd_vma gap_fill_vma
;
2371 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2372 gap_fill
= (bfd_byte
) gap_fill_vma
;
2373 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2377 sprintf_vma (buff
, gap_fill_vma
);
2379 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2382 gap_fill_set
= true;
2386 case OPTION_NO_CHANGE_WARNINGS
:
2387 change_warn
= false;
2391 pad_to
= parse_vma (optarg
, "--pad-to");
2395 case OPTION_REMOVE_LEADING_CHAR
:
2396 remove_leading_char
= true;
2399 case OPTION_REDEFINE_SYM
:
2401 /* Push this redefinition onto redefine_symbol_list. */
2405 const char *nextarg
;
2406 char *source
, *target
;
2408 s
= strchr (optarg
, '=');
2410 fatal (_("bad format for %s"), "--redefine-sym");
2413 source
= (char *) xmalloc (len
+ 1);
2414 strncpy (source
, optarg
, len
);
2418 len
= strlen (nextarg
);
2419 target
= (char *) xmalloc (len
+ 1);
2420 strcpy (target
, nextarg
);
2422 redefine_list_append (source
, target
);
2429 case OPTION_SET_SECTION_FLAGS
:
2435 s
= strchr (optarg
, '=');
2437 fatal (_("bad format for %s"), "--set-section-flags");
2440 name
= (char *) xmalloc (len
+ 1);
2441 strncpy (name
, optarg
, len
);
2444 p
= find_section_list (name
, true);
2446 p
->set_flags
= true;
2447 p
->flags
= parse_flags (s
+ 1);
2451 case OPTION_RENAME_SECTION
:
2454 const char *eq
, *fl
;
2459 eq
= strchr (optarg
, '=');
2461 fatal (_("bad format for %s"), "--rename-section");
2465 fatal (_("bad format for %s"), "--rename-section");
2467 old_name
= (char *) xmalloc (len
+ 1);
2468 strncpy (old_name
, optarg
, len
);
2472 fl
= strchr (eq
, ',');
2475 flags
= parse_flags (fl
+ 1);
2485 fatal (_("bad format for %s"), "--rename-section");
2487 new_name
= (char *) xmalloc (len
+ 1);
2488 strncpy (new_name
, eq
, len
);
2491 add_section_rename (old_name
, new_name
, flags
);
2495 case OPTION_SET_START
:
2496 set_start
= parse_vma (optarg
, "--set-start");
2497 set_start_set
= true;
2500 case OPTION_SREC_LEN
:
2501 Chunk
= parse_vma (optarg
, "--srec-len");
2504 case OPTION_SREC_FORCES3
:
2508 case OPTION_STRIP_SYMBOLS
:
2509 add_specific_symbols (optarg
, &strip_specific_list
);
2512 case OPTION_KEEP_SYMBOLS
:
2513 add_specific_symbols (optarg
, &keep_specific_list
);
2516 case OPTION_LOCALIZE_SYMBOLS
:
2517 add_specific_symbols (optarg
, &localize_specific_list
);
2520 case OPTION_KEEPGLOBAL_SYMBOLS
:
2521 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2524 case OPTION_WEAKEN_SYMBOLS
:
2525 add_specific_symbols (optarg
, &weaken_specific_list
);
2528 case OPTION_ALT_MACH_CODE
:
2529 use_alt_mach_code
= atoi (optarg
);
2530 if (use_alt_mach_code
<= 0)
2531 fatal (_("alternate machine code index must be positive"));
2535 break; /* we've been given a long option */
2539 copy_usage (stdout
, 0);
2542 copy_usage (stderr
, 1);
2547 print_version ("objcopy");
2549 if (copy_byte
>= interleave
)
2550 fatal (_("byte number must be less than interleave"));
2552 if (optind
== argc
|| optind
+ 2 < argc
)
2553 copy_usage (stderr
, 1);
2555 input_filename
= argv
[optind
];
2556 if (optind
+ 1 < argc
)
2557 output_filename
= argv
[optind
+ 1];
2559 /* Default is to strip no symbols. */
2560 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2561 strip_symbols
= STRIP_NONE
;
2563 if (output_target
== (char *) NULL
)
2564 output_target
= input_target
;
2566 if (binary_architecture
!= (char *) NULL
)
2568 if (input_target
&& strcmp (input_target
, "binary") == 0)
2570 const bfd_arch_info_type
* temp_arch_info
;
2572 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2574 if (temp_arch_info
!= NULL
)
2575 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2577 fatal (_("architecture %s unknown"), binary_architecture
);
2581 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2582 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2587 if (stat (input_filename
, & statbuf
) < 0)
2588 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2590 /* If there is no destination file then create a temp and rename
2591 the result into the input. */
2593 if (output_filename
== (char *) NULL
)
2595 char *tmpname
= make_tempname (input_filename
);
2597 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2601 set_times (tmpname
, &statbuf
);
2602 smart_rename (tmpname
, input_filename
, preserve_dates
);
2609 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2611 if (status
== 0 && preserve_dates
)
2612 set_times (output_filename
, &statbuf
);
2617 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2621 if (p
->change_vma
!= CHANGE_IGNORE
)
2625 sprintf_vma (buff
, p
->vma_val
);
2627 /* xgettext:c-format */
2628 non_fatal (_("%s %s%c0x%s never used"),
2629 "--change-section-vma",
2631 p
->change_vma
== CHANGE_SET
? '=' : '+',
2635 if (p
->change_lma
!= CHANGE_IGNORE
)
2639 sprintf_vma (buff
, p
->lma_val
);
2641 /* xgettext:c-format */
2642 non_fatal (_("%s %s%c0x%s never used"),
2643 "--change-section-lma",
2645 p
->change_lma
== CHANGE_SET
? '=' : '+',
2655 int main
PARAMS ((int, char **));
2662 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2663 setlocale (LC_MESSAGES
, "");
2665 #if defined (HAVE_SETLOCALE)
2666 setlocale (LC_CTYPE
, "");
2668 bindtextdomain (PACKAGE
, LOCALEDIR
);
2669 textdomain (PACKAGE
);
2671 program_name
= argv
[0];
2672 xmalloc_set_program_name (program_name
);
2674 START_PROGRESS (program_name
, 0);
2676 strip_symbols
= STRIP_UNDEF
;
2677 discard_locals
= LOCALS_UNDEF
;
2680 set_default_bfd_target ();
2684 int i
= strlen (program_name
);
2685 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2686 /* Drop the .exe suffix, if any. */
2687 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2690 program_name
[i
] = '\0';
2693 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2697 strip_main (argc
, argv
);
2699 copy_main (argc
, argv
);
2701 END_PROGRESS (program_name
);