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 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
65 static asymbol
**isympp
= NULL
; /* Input symbols. */
66 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping. */
68 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
69 static int copy_byte
= -1;
70 static int interleave
= 4;
72 static bfd_boolean verbose
; /* Print file and target names. */
73 static bfd_boolean preserve_dates
; /* Preserve input file timestamp. */
74 static int status
= 0; /* Exit status. */
79 STRIP_NONE
, /* Don't strip. */
80 STRIP_DEBUG
, /* Strip all debugger symbols. */
81 STRIP_UNNEEDED
, /* Strip unnecessary symbols. */
82 STRIP_NONDEBUG
, /* Strip everything but debug info. */
83 STRIP_ALL
/* Strip all symbols. */
86 /* Which symbols to remove. */
87 static enum strip_action strip_symbols
;
92 LOCALS_START_L
, /* Discard locals starting with L. */
93 LOCALS_ALL
/* Discard all locals. */
96 /* Which local symbols to remove. Overrides STRIP_ALL. */
97 static enum locals_action discard_locals
;
99 /* What kind of change to perform. */
107 /* Structure used to hold lists of sections and actions to take. */
110 struct section_list
* next
; /* Next section to change. */
111 const char * name
; /* Section name. */
112 bfd_boolean used
; /* Whether this entry was used. */
113 bfd_boolean remove
; /* Whether to remove this section. */
114 bfd_boolean copy
; /* Whether to copy this section. */
115 enum change_action change_vma
;/* Whether to change or set VMA. */
116 bfd_vma vma_val
; /* Amount to change by or set to. */
117 enum change_action change_lma
;/* Whether to change or set LMA. */
118 bfd_vma lma_val
; /* Amount to change by or set to. */
119 bfd_boolean set_flags
; /* Whether to set the section flags. */
120 flagword flags
; /* What to set the section flags to. */
123 static struct section_list
*change_sections
;
125 /* TRUE if some sections are to be removed. */
126 static bfd_boolean sections_removed
;
128 /* TRUE if only some sections are to be copied. */
129 static bfd_boolean sections_copied
;
131 /* Changes to the start address. */
132 static bfd_vma change_start
= 0;
133 static bfd_boolean set_start_set
= FALSE
;
134 static bfd_vma set_start
;
136 /* Changes to section addresses. */
137 static bfd_vma change_section_address
= 0;
139 /* Filling gaps between sections. */
140 static bfd_boolean gap_fill_set
= FALSE
;
141 static bfd_byte gap_fill
= 0;
143 /* Pad to a given address. */
144 static bfd_boolean pad_to_set
= FALSE
;
145 static bfd_vma pad_to
;
147 /* Use alternate machine code? */
148 static int use_alt_mach_code
= 0;
150 /* List of sections to add. */
153 /* Next section to add. */
154 struct section_add
*next
;
155 /* Name of section to add. */
157 /* Name of file holding section contents. */
158 const char *filename
;
161 /* Contents of file. */
163 /* BFD section, after it has been added. */
167 /* List of sections to add to the output BFD. */
168 static struct section_add
*add_sections
;
170 /* If non-NULL the argument to --add-gnu-debuglink.
171 This should be the filename to store in the .gnu_debuglink section. */
172 static const char * gnu_debuglink_filename
= NULL
;
174 /* Whether to convert debugging information. */
175 static bfd_boolean convert_debugging
= FALSE
;
177 /* Whether to change the leading character in symbol names. */
178 static bfd_boolean change_leading_char
= FALSE
;
180 /* Whether to remove the leading character from global symbol names. */
181 static bfd_boolean remove_leading_char
= FALSE
;
183 /* List of symbols to strip, keep, localize, keep-global, weaken,
185 static struct symlist
*strip_specific_list
= NULL
;
186 static struct symlist
*keep_specific_list
= NULL
;
187 static struct symlist
*localize_specific_list
= NULL
;
188 static struct symlist
*keepglobal_specific_list
= NULL
;
189 static struct symlist
*weaken_specific_list
= NULL
;
190 static struct redefine_node
*redefine_sym_list
= NULL
;
192 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
193 static bfd_boolean weaken
= FALSE
;
195 /* Prefix symbols/sections. */
196 static char *prefix_symbols_string
= 0;
197 static char *prefix_sections_string
= 0;
198 static char *prefix_alloc_sections_string
= 0;
200 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
201 enum command_line_switch
203 OPTION_ADD_SECTION
=150,
204 OPTION_CHANGE_ADDRESSES
,
205 OPTION_CHANGE_LEADING_CHAR
,
207 OPTION_CHANGE_SECTION_ADDRESS
,
208 OPTION_CHANGE_SECTION_LMA
,
209 OPTION_CHANGE_SECTION_VMA
,
210 OPTION_CHANGE_WARNINGS
,
213 OPTION_NO_CHANGE_WARNINGS
,
215 OPTION_REMOVE_LEADING_CHAR
,
216 OPTION_SET_SECTION_FLAGS
,
218 OPTION_STRIP_UNNEEDED
,
221 OPTION_REDEFINE_SYMS
,
224 OPTION_STRIP_SYMBOLS
,
226 OPTION_LOCALIZE_SYMBOLS
,
227 OPTION_KEEPGLOBAL_SYMBOLS
,
228 OPTION_WEAKEN_SYMBOLS
,
229 OPTION_RENAME_SECTION
,
230 OPTION_ALT_MACH_CODE
,
231 OPTION_PREFIX_SYMBOLS
,
232 OPTION_PREFIX_SECTIONS
,
233 OPTION_PREFIX_ALLOC_SECTIONS
,
235 OPTION_ADD_GNU_DEBUGLINK
,
236 OPTION_ONLY_KEEP_DEBUG
239 /* Options to handle if running as "strip". */
241 static struct option strip_options
[] =
243 {"discard-all", no_argument
, 0, 'x'},
244 {"discard-locals", no_argument
, 0, 'X'},
245 {"format", required_argument
, 0, 'F'}, /* Obsolete */
246 {"help", no_argument
, 0, 'h'},
247 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
248 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
249 {"input-target", required_argument
, 0, 'I'},
250 {"keep-symbol", required_argument
, 0, 'K'},
251 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
252 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
253 {"output-target", required_argument
, 0, 'O'},
254 {"output-file", required_argument
, 0, 'o'},
255 {"preserve-dates", no_argument
, 0, 'p'},
256 {"remove-section", required_argument
, 0, 'R'},
257 {"strip-all", no_argument
, 0, 's'},
258 {"strip-debug", no_argument
, 0, 'S'},
259 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
260 {"strip-symbol", required_argument
, 0, 'N'},
261 {"target", required_argument
, 0, 'F'},
262 {"verbose", no_argument
, 0, 'v'},
263 {"version", no_argument
, 0, 'V'},
264 {0, no_argument
, 0, 0}
267 /* Options to handle if running as "objcopy". */
269 static struct option copy_options
[] =
271 {"add-gnu-debuglink", required_argument
, 0, OPTION_ADD_GNU_DEBUGLINK
},
272 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
273 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
274 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
275 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
276 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
277 {"alt-machine-code", required_argument
, 0, OPTION_ALT_MACH_CODE
},
278 {"binary-architecture", required_argument
, 0, 'B'},
279 {"byte", required_argument
, 0, 'b'},
280 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
281 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
282 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
283 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
284 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
285 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
286 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
287 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
288 {"discard-all", no_argument
, 0, 'x'},
289 {"discard-locals", no_argument
, 0, 'X'},
290 {"format", required_argument
, 0, 'F'}, /* Obsolete */
291 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
292 {"help", no_argument
, 0, 'h'},
293 {"info", no_argument
, 0, OPTION_FORMATS_INFO
},
294 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
295 {"input-target", required_argument
, 0, 'I'},
296 {"interleave", required_argument
, 0, 'i'},
297 {"keep-global-symbol", required_argument
, 0, 'G'},
298 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
299 {"keep-symbol", required_argument
, 0, 'K'},
300 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
301 {"localize-symbol", required_argument
, 0, 'L'},
302 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
303 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
304 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
305 {"only-keep-debug", no_argument
, 0, OPTION_ONLY_KEEP_DEBUG
},
306 {"only-section", required_argument
, 0, 'j'},
307 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
308 {"output-target", required_argument
, 0, 'O'},
309 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
310 {"prefix-symbols", required_argument
, 0, OPTION_PREFIX_SYMBOLS
},
311 {"prefix-sections", required_argument
, 0, OPTION_PREFIX_SECTIONS
},
312 {"prefix-alloc-sections", required_argument
, 0, OPTION_PREFIX_ALLOC_SECTIONS
},
313 {"preserve-dates", no_argument
, 0, 'p'},
314 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
315 {"redefine-syms", required_argument
, 0, OPTION_REDEFINE_SYMS
},
316 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
317 {"remove-section", required_argument
, 0, 'R'},
318 {"rename-section", required_argument
, 0, OPTION_RENAME_SECTION
},
319 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
320 {"set-start", required_argument
, 0, OPTION_SET_START
},
321 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
322 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
323 {"strip-all", no_argument
, 0, 'S'},
324 {"strip-debug", no_argument
, 0, 'g'},
325 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
326 {"strip-symbol", required_argument
, 0, 'N'},
327 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
328 {"target", required_argument
, 0, 'F'},
329 {"verbose", no_argument
, 0, 'v'},
330 {"version", no_argument
, 0, 'V'},
331 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
332 {"weaken-symbol", required_argument
, 0, 'W'},
333 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
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 bfd_boolean S3Forced
;
354 /* Defined in bfd/binary.c. Used to set architecture and machine of input
356 extern enum bfd_architecture bfd_external_binary_architecture
;
357 extern unsigned long bfd_external_machine
;
359 /* Forward declarations. */
360 static void setup_section (bfd
*, asection
*, void *);
361 static void copy_section (bfd
*, asection
*, void *);
362 static void get_sections (bfd
*, asection
*, void *);
363 static int compare_section_lma (const void *, const void *);
364 static void mark_symbols_used_in_relocations (bfd
*, asection
*, void *);
365 static bfd_boolean
write_debugging_info (bfd
*, void *, long *, asymbol
***);
366 static const char *lookup_sym_redefinition (const char *);
369 copy_usage (FILE *stream
, int exit_status
)
371 fprintf (stream
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
372 fprintf (stream
, _(" Copies a binary file, possibly transforming it in the process\n"));
373 fprintf (stream
, _(" The options are:\n"));
374 fprintf (stream
, _("\
375 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
376 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
377 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
378 -F --target <bfdname> Set both input and output format to <bfdname>\n\
379 --debugging Convert debugging information, if possible\n\
380 -p --preserve-dates Copy modified/access timestamps to the output\n\
381 -j --only-section <name> Only copy section <name> into the output\n\
382 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
383 -R --remove-section <name> Remove section <name> from the output\n\
384 -S --strip-all Remove all symbol and relocation information\n\
385 -g --strip-debug Remove all debugging symbols & sections\n\
386 --strip-unneeded Remove all symbols not needed by relocations\n\
387 -N --strip-symbol <name> Do not copy symbol <name>\n\
388 -K --keep-symbol <name> Only copy symbol <name>\n\
389 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
390 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
391 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
392 --weaken Force all global symbols to be marked as weak\n\
393 -x --discard-all Remove all non-global symbols\n\
394 -X --discard-locals Remove any compiler-generated symbols\n\
395 -i --interleave <number> Only copy one out of every <number> bytes\n\
396 -b --byte <num> Select byte <num> in every interleaved block\n\
397 --gap-fill <val> Fill gaps between sections with <val>\n\
398 --pad-to <addr> Pad the last section up to address <addr>\n\
399 --set-start <addr> Set the start address to <addr>\n\
400 {--change-start|--adjust-start} <incr>\n\
401 Add <incr> to the start address\n\
402 {--change-addresses|--adjust-vma} <incr>\n\
403 Add <incr> to LMA, VMA and start addresses\n\
404 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
405 Change LMA and VMA of section <name> by <val>\n\
406 --change-section-lma <name>{=|+|-}<val>\n\
407 Change the LMA of section <name> by <val>\n\
408 --change-section-vma <name>{=|+|-}<val>\n\
409 Change the VMA of section <name> by <val>\n\
410 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
411 Warn if a named section does not exist\n\
412 --set-section-flags <name>=<flags>\n\
413 Set section <name>'s properties to <flags>\n\
414 --add-section <name>=<file> Add section <name> found in <file> to output\n\
415 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
416 --change-leading-char Force output format's leading character style\n\
417 --remove-leading-char Remove leading character from global symbols\n\
418 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
419 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
421 --srec-len <number> Restrict the length of generated Srecords\n\
422 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
423 --strip-symbols <file> -N for all symbols listed in <file>\n\
424 --keep-symbols <file> -K for all symbols listed in <file>\n\
425 --localize-symbols <file> -L for all symbols listed in <file>\n\
426 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
427 --weaken-symbols <file> -W for all symbols listed in <file>\n\
428 --alt-machine-code <index> Use alternate machine code for output\n\
429 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
430 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
431 --prefix-alloc-sections <prefix>\n\
432 Add <prefix> to start of every allocatable\n\
434 -v --verbose List all object files modified\n\
435 -V --version Display this program's version number\n\
436 -h --help Display this output\n\
437 --info List object formats & architectures supported\n\
439 list_supported_targets (program_name
, stream
);
440 if (exit_status
== 0)
441 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
446 strip_usage (FILE *stream
, int exit_status
)
448 fprintf (stream
, _("Usage: %s <option(s)> in-file(s)\n"), program_name
);
449 fprintf (stream
, _(" Removes symbols and sections from files\n"));
450 fprintf (stream
, _(" The options are:\n"));
451 fprintf (stream
, _("\
452 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
453 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
454 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
455 -p --preserve-dates Copy modified/access timestamps to the output\n\
456 -R --remove-section=<name> Remove section <name> from the output\n\
457 -s --strip-all Remove all symbol and relocation information\n\
458 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
459 --strip-unneeded Remove all symbols not needed by relocations\n\
460 -N --strip-symbol=<name> Do not copy symbol <name>\n\
461 -K --keep-symbol=<name> Only copy symbol <name>\n\
462 -x --discard-all Remove all non-global symbols\n\
463 -X --discard-locals Remove any compiler-generated symbols\n\
464 -v --verbose List all object files modified\n\
465 -V --version Display this program's version number\n\
466 -h --help Display this output\n\
467 --info List object formats & architectures supported\n\
468 -o <file> Place stripped output into <file>\n\
471 list_supported_targets (program_name
, stream
);
472 if (exit_status
== 0)
473 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
477 /* Parse section flags into a flagword, with a fatal error if the
478 string can't be parsed. */
481 parse_flags (const char *s
)
491 snext
= strchr (s
, ',');
501 #define PARSE_FLAG(fname,fval) \
502 else if (strncasecmp (fname, s, len) == 0) ret |= fval
503 PARSE_FLAG ("alloc", SEC_ALLOC
);
504 PARSE_FLAG ("load", SEC_LOAD
);
505 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
506 PARSE_FLAG ("readonly", SEC_READONLY
);
507 PARSE_FLAG ("debug", SEC_DEBUGGING
);
508 PARSE_FLAG ("code", SEC_CODE
);
509 PARSE_FLAG ("data", SEC_DATA
);
510 PARSE_FLAG ("rom", SEC_ROM
);
511 PARSE_FLAG ("share", SEC_SHARED
);
512 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
518 copy
= xmalloc (len
+ 1);
519 strncpy (copy
, s
, len
);
521 non_fatal (_("unrecognized section flag `%s'"), copy
);
522 fatal (_("supported flags: %s"),
523 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
533 /* Find and optionally add an entry in the change_sections list. */
535 static struct section_list
*
536 find_section_list (const char *name
, bfd_boolean add
)
538 struct section_list
*p
;
540 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
541 if (strcmp (p
->name
, name
) == 0)
547 p
= xmalloc (sizeof (struct section_list
));
552 p
->change_vma
= CHANGE_IGNORE
;
553 p
->change_lma
= CHANGE_IGNORE
;
556 p
->set_flags
= FALSE
;
559 p
->next
= change_sections
;
565 /* Add a symbol to strip_specific_list. */
568 add_specific_symbol (const char *name
, struct symlist
**list
)
570 struct symlist
*tmp_list
;
572 tmp_list
= xmalloc (sizeof (struct symlist
));
573 tmp_list
->name
= name
;
574 tmp_list
->next
= *list
;
578 /* Add symbols listed in `filename' to strip_specific_list. */
580 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
581 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
584 add_specific_symbols (const char *filename
, struct symlist
**list
)
590 unsigned int line_count
;
592 if (stat (filename
, & st
) < 0)
593 fatal (_("cannot stat: %s: %s"), filename
, strerror (errno
));
597 buffer
= xmalloc (st
.st_size
+ 2);
598 f
= fopen (filename
, FOPEN_RT
);
600 fatal (_("cannot open: %s: %s"), filename
, strerror (errno
));
602 if (fread (buffer
, 1, st
.st_size
, f
) == 0 || ferror (f
))
603 fatal (_("%s: fread failed"), filename
);
606 buffer
[st
.st_size
] = '\n';
607 buffer
[st
.st_size
+ 1] = '\0';
611 for (line
= buffer
; * line
!= '\0'; line
++)
616 int finished
= FALSE
;
618 for (eol
= line
;; eol
++)
624 /* Cope with \n\r. */
632 /* Cope with \r\n. */
643 /* Line comment, Terminate the line here, in case a
644 name is present and then allow the rest of the
645 loop to find the real end of the line. */
657 /* A name may now exist somewhere between 'line' and 'eol'.
658 Strip off leading whitespace and trailing whitespace,
659 then add it to the list. */
660 for (name
= line
; IS_WHITESPACE (* name
); name
++)
662 for (name_end
= name
;
663 (! IS_WHITESPACE (* name_end
))
664 && (! IS_LINE_TERMINATOR (* name_end
));
668 if (! IS_LINE_TERMINATOR (* name_end
))
672 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
675 if (! IS_LINE_TERMINATOR (* extra
))
676 non_fatal (_("Ignoring rubbish found on line %d of %s"),
677 line_count
, filename
);
683 add_specific_symbol (name
, list
);
685 /* Advance line pointer to end of line. The 'eol ++' in the for
686 loop above will then advance us to the start of the next line. */
692 /* See whether a symbol should be stripped or kept based on
693 strip_specific_list and keep_symbols. */
696 is_specified_symbol (const char *name
, struct symlist
*list
)
698 struct symlist
*tmp_list
;
700 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
701 if (strcmp (name
, tmp_list
->name
) == 0)
707 /* See if a section is being removed. */
710 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
712 if (sections_removed
|| sections_copied
)
714 struct section_list
*p
;
716 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
718 if (sections_removed
&& p
!= NULL
&& p
->remove
)
720 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
724 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
726 if (strip_symbols
== STRIP_DEBUG
727 || strip_symbols
== STRIP_UNNEEDED
728 || strip_symbols
== STRIP_ALL
729 || discard_locals
== LOCALS_ALL
730 || convert_debugging
)
733 if (strip_symbols
== STRIP_NONDEBUG
)
737 return strip_symbols
== STRIP_NONDEBUG
? TRUE
: FALSE
;
740 /* Choose which symbol entries to copy; put the result in OSYMS.
741 We don't copy in place, because that confuses the relocs.
742 Return the number of symbols to print. */
745 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
746 asymbol
**isyms
, long symcount
)
748 asymbol
**from
= isyms
, **to
= osyms
;
749 long src_count
= 0, dst_count
= 0;
750 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
753 for (; src_count
< symcount
; src_count
++)
755 asymbol
*sym
= from
[src_count
];
756 flagword flags
= sym
->flags
;
757 char *name
= (char *) bfd_asymbol_name (sym
);
759 bfd_boolean undefined
;
760 bfd_boolean rem_leading_char
;
761 bfd_boolean add_leading_char
;
763 undefined
= bfd_is_und_section (bfd_get_section (sym
));
765 if (redefine_sym_list
)
767 char *old_name
, *new_name
;
769 old_name
= (char *) bfd_asymbol_name (sym
);
770 new_name
= (char *) lookup_sym_redefinition (old_name
);
771 bfd_asymbol_name (sym
) = new_name
;
775 /* Check if we will remove the current leading character. */
777 (name
[0] == bfd_get_symbol_leading_char (abfd
))
778 && (change_leading_char
779 || (remove_leading_char
780 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
782 || bfd_is_com_section (bfd_get_section (sym
)))));
784 /* Check if we will add a new leading character. */
787 && (bfd_get_symbol_leading_char (obfd
) != '\0')
788 && (bfd_get_symbol_leading_char (abfd
) == '\0'
789 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
791 /* Short circuit for change_leading_char if we can do it in-place. */
792 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
794 name
[0] = bfd_get_symbol_leading_char (obfd
);
795 bfd_asymbol_name (sym
) = name
;
796 rem_leading_char
= FALSE
;
797 add_leading_char
= FALSE
;
800 /* Remove leading char. */
801 if (rem_leading_char
)
802 bfd_asymbol_name (sym
) = ++name
;
804 /* Add new leading char and/or prefix. */
805 if (add_leading_char
|| prefix_symbols_string
)
809 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
810 + strlen (name
) + 1);
811 if (add_leading_char
)
812 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
814 if (prefix_symbols_string
)
816 strcpy (ptr
, prefix_symbols_string
);
817 ptr
+= strlen (prefix_symbols_string
);
821 bfd_asymbol_name (sym
) = n
;
825 if (strip_symbols
== STRIP_ALL
)
827 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
828 || ((flags
& BSF_SECTION_SYM
) != 0
829 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
832 else if (relocatable
/* Relocatable file. */
833 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
835 else if (bfd_decode_symclass (sym
) == 'I')
836 /* Global symbols in $idata sections need to be retained
837 even if relocatable is FALSE. External users of the
838 library containing the $idata section may reference these
841 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
842 || (flags
& BSF_WEAK
) != 0
844 || bfd_is_com_section (bfd_get_section (sym
)))
845 keep
= strip_symbols
!= STRIP_UNNEEDED
;
846 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
847 keep
= (strip_symbols
!= STRIP_DEBUG
848 && strip_symbols
!= STRIP_UNNEEDED
849 && ! convert_debugging
);
850 else if (bfd_get_section (sym
)->comdat
)
851 /* COMDAT sections store special information in local
852 symbols, so we cannot risk stripping any of them. */
854 else /* Local symbol. */
855 keep
= (strip_symbols
!= STRIP_UNNEEDED
856 && (discard_locals
!= LOCALS_ALL
857 && (discard_locals
!= LOCALS_START_L
858 || ! bfd_is_local_label (abfd
, sym
))));
860 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
862 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
864 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
867 if (keep
&& (flags
& BSF_GLOBAL
) != 0
868 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
870 sym
->flags
&=~ BSF_GLOBAL
;
871 sym
->flags
|= BSF_WEAK
;
873 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
874 && (is_specified_symbol (name
, localize_specific_list
)
875 || (keepglobal_specific_list
!= NULL
876 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
878 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
879 sym
->flags
|= BSF_LOCAL
;
883 to
[dst_count
++] = sym
;
886 to
[dst_count
] = NULL
;
891 /* Find the redefined name of symbol SOURCE. */
894 lookup_sym_redefinition (const char *source
)
896 struct redefine_node
*list
;
898 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
899 if (strcmp (source
, list
->source
) == 0)
905 /* Add a node to a symbol redefine list. */
908 redefine_list_append (const char *cause
, const char *source
, const char *target
)
910 struct redefine_node
**p
;
911 struct redefine_node
*list
;
912 struct redefine_node
*new_node
;
914 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
916 if (strcmp (source
, list
->source
) == 0)
917 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
920 if (strcmp (target
, list
->target
) == 0)
921 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
925 new_node
= xmalloc (sizeof (struct redefine_node
));
927 new_node
->source
= strdup (source
);
928 new_node
->target
= strdup (target
);
929 new_node
->next
= NULL
;
934 /* Handle the --redefine-syms option. Read lines containing "old new"
935 from the file, and add them to the symbol redefine list. */
938 add_redefine_syms_file (const char *filename
)
947 file
= fopen (filename
, "r");
949 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
950 filename
, strerror (errno
));
953 buf
= xmalloc (bufsize
);
961 /* Collect the input symbol name. */
962 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
970 buf
= xrealloc (buf
, bufsize
);
978 /* Eat white space between the symbol names. */
979 while (IS_WHITESPACE (c
))
981 if (c
== '#' || IS_LINE_TERMINATOR (c
))
986 /* Collect the output symbol name. */
988 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
996 buf
= xrealloc (buf
, bufsize
);
1004 /* Eat white space at end of line. */
1005 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1010 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1011 || c
== '\n' || c
== EOF
)
1014 /* Append the redefinition to the list. */
1016 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1027 fatal (_("%s: garbage at end of line %d"), filename
, lineno
);
1029 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1030 fatal (_("%s: missing new symbol name at line %d"), filename
, lineno
);
1033 /* Eat the rest of the line and finish it. */
1034 while (c
!= '\n' && c
!= EOF
)
1040 fatal (_("%s: premature end of file at line %d"), filename
, lineno
);
1045 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
1049 filter_bytes (char *memhunk
, bfd_size_type
*size
)
1051 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
1053 for (; from
< end
; from
+= interleave
)
1056 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
1057 *size
= (*size
/ interleave
) + 1;
1059 *size
/= interleave
;
1062 /* Copy object file IBFD onto OBFD. */
1065 copy_object (bfd
*ibfd
, bfd
*obfd
)
1069 asection
**osections
= NULL
;
1070 asection
*gnu_debuglink_section
= NULL
;
1071 bfd_size_type
*gaps
= NULL
;
1072 bfd_size_type max_gap
= 0;
1075 enum bfd_architecture iarch
;
1078 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1079 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1080 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1082 fatal (_("Unable to change endianness of input file(s)"));
1086 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1087 RETURN_NONFATAL (bfd_get_filename (obfd
));
1090 printf (_("copy from %s(%s) to %s(%s)\n"),
1091 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
1092 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1097 start
= bfd_get_start_address (ibfd
);
1098 start
+= change_start
;
1100 /* Neither the start address nor the flags
1101 need to be set for a core file. */
1102 if (bfd_get_format (obfd
) != bfd_core
)
1104 if (!bfd_set_start_address (obfd
, start
)
1105 || !bfd_set_file_flags (obfd
,
1106 (bfd_get_file_flags (ibfd
)
1107 & bfd_applicable_file_flags (obfd
))))
1108 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1111 /* Copy architecture of input file to output file. */
1112 iarch
= bfd_get_arch (ibfd
);
1113 imach
= bfd_get_mach (ibfd
);
1114 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1115 && (ibfd
->target_defaulted
1116 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1117 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1118 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1119 bfd_get_mach (ibfd
)));
1121 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1122 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1127 if (osympp
!= isympp
)
1130 /* BFD mandates that all output sections be created and sizes set before
1131 any output is done. Thus, we traverse all sections multiple times. */
1132 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1134 if (add_sections
!= NULL
)
1136 struct section_add
*padd
;
1137 struct section_list
*pset
;
1139 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1143 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1144 if (padd
->section
== NULL
)
1146 non_fatal (_("can't create section `%s': %s"),
1147 padd
->name
, bfd_errmsg (bfd_get_error ()));
1152 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1153 RETURN_NONFATAL (bfd_get_filename (obfd
));
1155 pset
= find_section_list (padd
->name
, FALSE
);
1159 if (pset
!= NULL
&& pset
->set_flags
)
1160 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1162 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1164 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1165 RETURN_NONFATAL (bfd_get_filename (obfd
));
1169 if (pset
->change_vma
!= CHANGE_IGNORE
)
1170 if (! bfd_set_section_vma (obfd
, padd
->section
,
1172 RETURN_NONFATAL (bfd_get_filename (obfd
));
1174 if (pset
->change_lma
!= CHANGE_IGNORE
)
1176 padd
->section
->lma
= pset
->lma_val
;
1178 if (! bfd_set_section_alignment
1179 (obfd
, padd
->section
,
1180 bfd_section_alignment (obfd
, padd
->section
)))
1181 RETURN_NONFATAL (bfd_get_filename (obfd
));
1187 if (gnu_debuglink_filename
!= NULL
)
1189 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1190 (obfd
, gnu_debuglink_filename
);
1192 if (gnu_debuglink_section
== NULL
)
1193 RETURN_NONFATAL (gnu_debuglink_filename
);
1196 if (gap_fill_set
|| pad_to_set
)
1201 /* We must fill in gaps between the sections and/or we must pad
1202 the last section to a specified address. We do this by
1203 grabbing a list of the sections, sorting them by VMA, and
1204 increasing the section sizes as required to fill the gaps.
1205 We write out the gap contents below. */
1207 c
= bfd_count_sections (obfd
);
1208 osections
= xmalloc (c
* sizeof (asection
*));
1210 bfd_map_over_sections (obfd
, get_sections
, &set
);
1212 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1214 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1215 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1219 for (i
= 0; i
< c
- 1; i
++)
1223 bfd_vma gap_start
, gap_stop
;
1225 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1226 if ((flags
& SEC_HAS_CONTENTS
) == 0
1227 || (flags
& SEC_LOAD
) == 0)
1230 size
= bfd_section_size (obfd
, osections
[i
]);
1231 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1232 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1233 if (gap_start
< gap_stop
)
1235 if (! bfd_set_section_size (obfd
, osections
[i
],
1236 size
+ (gap_stop
- gap_start
)))
1238 non_fatal (_("Can't fill gap after %s: %s"),
1239 bfd_get_section_name (obfd
, osections
[i
]),
1240 bfd_errmsg (bfd_get_error ()));
1244 gaps
[i
] = gap_stop
- gap_start
;
1245 if (max_gap
< gap_stop
- gap_start
)
1246 max_gap
= gap_stop
- gap_start
;
1256 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1257 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1258 if (lma
+ size
< pad_to
)
1260 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1263 non_fatal (_("Can't add padding to %s: %s"),
1264 bfd_get_section_name (obfd
, osections
[c
- 1]),
1265 bfd_errmsg (bfd_get_error ()));
1270 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1271 if (max_gap
< pad_to
- (lma
+ size
))
1272 max_gap
= pad_to
- (lma
+ size
);
1278 /* Symbol filtering must happen after the output sections
1279 have been created, but before their contents are set. */
1281 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1283 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1285 osympp
= isympp
= xmalloc (symsize
);
1286 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1288 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1290 if (convert_debugging
)
1291 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1293 if (strip_symbols
== STRIP_DEBUG
1294 || strip_symbols
== STRIP_ALL
1295 || strip_symbols
== STRIP_UNNEEDED
1296 || strip_symbols
== STRIP_NONDEBUG
1297 || discard_locals
!= LOCALS_UNDEF
1298 || strip_specific_list
!= NULL
1299 || keep_specific_list
!= NULL
1300 || localize_specific_list
!= NULL
1301 || keepglobal_specific_list
!= NULL
1302 || weaken_specific_list
!= NULL
1303 || prefix_symbols_string
1306 || convert_debugging
1307 || change_leading_char
1308 || remove_leading_char
1309 || redefine_sym_list
1312 /* Mark symbols used in output relocations so that they
1313 are kept, even if they are local labels or static symbols.
1315 Note we iterate over the input sections examining their
1316 relocations since the relocations for the output sections
1317 haven't been set yet. mark_symbols_used_in_relocations will
1318 ignore input sections which have no corresponding output
1320 if (strip_symbols
!= STRIP_ALL
)
1321 bfd_map_over_sections (ibfd
,
1322 mark_symbols_used_in_relocations
,
1324 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1325 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1328 if (convert_debugging
&& dhandle
!= NULL
)
1330 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1337 bfd_set_symtab (obfd
, osympp
, symcount
);
1339 /* This has to happen after the symbol table has been set. */
1340 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1342 if (add_sections
!= NULL
)
1344 struct section_add
*padd
;
1346 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1348 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1350 RETURN_NONFATAL (bfd_get_filename (obfd
));
1354 if (gnu_debuglink_filename
!= NULL
)
1356 if (! bfd_fill_in_gnu_debuglink_section
1357 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1358 RETURN_NONFATAL (gnu_debuglink_filename
);
1361 if (gap_fill_set
|| pad_to_set
)
1366 /* Fill in the gaps. */
1369 buf
= xmalloc (max_gap
);
1370 memset (buf
, gap_fill
, max_gap
);
1372 c
= bfd_count_sections (obfd
);
1373 for (i
= 0; i
< c
; i
++)
1381 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1392 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1394 RETURN_NONFATAL (bfd_get_filename (obfd
));
1403 /* Allow the BFD backend to copy any private data it understands
1404 from the input BFD to the output BFD. This is done last to
1405 permit the routine to look at the filtered symbol table, which is
1406 important for the ECOFF code at least. */
1407 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1408 && strip_symbols
== STRIP_NONDEBUG
)
1409 /* Do not copy the private data when creating an ELF format
1410 debug info file. We do not want the program headers. */
1412 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1414 non_fatal (_("%s: error copying private BFD data: %s"),
1415 bfd_get_filename (obfd
),
1416 bfd_errmsg (bfd_get_error ()));
1421 /* Switch to the alternate machine code. We have to do this at the
1422 very end, because we only initialize the header when we create
1423 the first section. */
1424 if (use_alt_mach_code
!= 0)
1426 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1427 non_fatal (_("unknown alternate machine code, ignored"));
1432 #if defined (_WIN32) && !defined (__CYGWIN32__)
1433 #define MKDIR(DIR, MODE) mkdir (DIR)
1435 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1438 /* Read each archive element in turn from IBFD, copy the
1439 contents to temp file, and keep the temp file handle. */
1442 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1446 struct name_list
*next
;
1450 bfd
**ptr
= &obfd
->archive_head
;
1452 char *dir
= make_tempname (bfd_get_filename (obfd
));
1454 /* Make a temp directory to hold the contents. */
1455 if (MKDIR (dir
, 0700) != 0)
1456 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1457 dir
, strerror (errno
));
1459 obfd
->has_armap
= ibfd
->has_armap
;
1463 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1465 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1466 RETURN_NONFATAL (bfd_get_filename (obfd
));
1468 while (!status
&& this_element
!= NULL
)
1474 int stat_status
= 0;
1476 /* Create an output file for this member. */
1477 output_name
= concat (dir
, "/",
1478 bfd_get_filename (this_element
), (char *) 0);
1480 /* If the file already exists, make another temp dir. */
1481 if (stat (output_name
, &buf
) >= 0)
1483 output_name
= make_tempname (output_name
);
1484 if (MKDIR (output_name
, 0700) != 0)
1485 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1486 output_name
, strerror (errno
));
1488 l
= xmalloc (sizeof (struct name_list
));
1489 l
->name
= output_name
;
1493 output_name
= concat (output_name
, "/",
1494 bfd_get_filename (this_element
), (char *) 0);
1497 output_bfd
= bfd_openw (output_name
, output_target
);
1500 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1502 if (stat_status
!= 0)
1503 non_fatal (_("internal stat error on %s"),
1504 bfd_get_filename (this_element
));
1507 l
= xmalloc (sizeof (struct name_list
));
1508 l
->name
= output_name
;
1512 if (output_bfd
== NULL
)
1513 RETURN_NONFATAL (output_name
);
1515 if (bfd_check_format (this_element
, bfd_object
))
1516 copy_object (this_element
, output_bfd
);
1518 if (!bfd_close (output_bfd
))
1520 bfd_nonfatal (bfd_get_filename (output_bfd
));
1521 /* Error in new object file. Don't change archive. */
1525 if (preserve_dates
&& stat_status
== 0)
1526 set_times (output_name
, &buf
);
1528 /* Open the newly output file and attach to our list. */
1529 output_bfd
= bfd_openr (output_name
, output_target
);
1531 l
->obfd
= output_bfd
;
1534 ptr
= &output_bfd
->next
;
1536 last_element
= this_element
;
1538 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1540 bfd_close (last_element
);
1544 if (!bfd_close (obfd
))
1545 RETURN_NONFATAL (bfd_get_filename (obfd
));
1547 if (!bfd_close (ibfd
))
1548 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1550 /* Delete all the files that we opened. */
1551 for (l
= list
; l
!= NULL
; l
= l
->next
)
1553 if (l
->obfd
== NULL
)
1557 bfd_close (l
->obfd
);
1564 /* The top-level control. */
1567 copy_file (const char *input_filename
, const char *output_filename
,
1568 const char *input_target
, const char *output_target
)
1571 char **obj_matching
;
1572 char **core_matching
;
1574 /* To allow us to do "strip *" without dying on the first
1575 non-object file, failures are nonfatal. */
1576 ibfd
= bfd_openr (input_filename
, input_target
);
1578 RETURN_NONFATAL (input_filename
);
1580 if (bfd_check_format (ibfd
, bfd_archive
))
1584 /* bfd_get_target does not return the correct value until
1585 bfd_check_format succeeds. */
1586 if (output_target
== NULL
)
1587 output_target
= bfd_get_target (ibfd
);
1589 obfd
= bfd_openw (output_filename
, output_target
);
1591 RETURN_NONFATAL (output_filename
);
1593 copy_archive (ibfd
, obfd
, output_target
);
1595 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1599 /* bfd_get_target does not return the correct value until
1600 bfd_check_format succeeds. */
1601 if (output_target
== NULL
)
1602 output_target
= bfd_get_target (ibfd
);
1604 obfd
= bfd_openw (output_filename
, output_target
);
1606 RETURN_NONFATAL (output_filename
);
1608 copy_object (ibfd
, obfd
);
1610 if (!bfd_close (obfd
))
1611 RETURN_NONFATAL (output_filename
);
1613 if (!bfd_close (ibfd
))
1614 RETURN_NONFATAL (input_filename
);
1618 bfd_error_type obj_error
= bfd_get_error ();
1619 bfd_error_type core_error
;
1621 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1623 /* This probably can't happen.. */
1624 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1625 free (obj_matching
);
1629 core_error
= bfd_get_error ();
1630 /* Report the object error in preference to the core error. */
1631 if (obj_error
!= core_error
)
1632 bfd_set_error (obj_error
);
1634 bfd_nonfatal (input_filename
);
1636 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1638 list_matching_formats (obj_matching
);
1639 free (obj_matching
);
1641 if (core_error
== bfd_error_file_ambiguously_recognized
)
1643 list_matching_formats (core_matching
);
1644 free (core_matching
);
1651 /* Add a name to the section renaming list. */
1654 add_section_rename (const char * old_name
, const char * new_name
,
1657 section_rename
* rename
;
1659 /* Check for conflicts first. */
1660 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1661 if (strcmp (rename
->old_name
, old_name
) == 0)
1663 /* Silently ignore duplicate definitions. */
1664 if (strcmp (rename
->new_name
, new_name
) == 0
1665 && rename
->flags
== flags
)
1668 fatal (_("Multiple renames of section %s"), old_name
);
1671 rename
= xmalloc (sizeof (* rename
));
1673 rename
->old_name
= old_name
;
1674 rename
->new_name
= new_name
;
1675 rename
->flags
= flags
;
1676 rename
->next
= section_rename_list
;
1678 section_rename_list
= rename
;
1681 /* Check the section rename list for a new name of the input section
1682 ISECTION. Return the new name if one is found.
1683 Also set RETURNED_FLAGS to the flags to be used for this section. */
1686 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
1687 flagword
* returned_flags
)
1689 const char * old_name
= bfd_section_name (ibfd
, isection
);
1690 section_rename
* rename
;
1692 /* Default to using the flags of the input section. */
1693 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1695 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1696 if (strcmp (rename
->old_name
, old_name
) == 0)
1698 if (rename
->flags
!= (flagword
) -1)
1699 * returned_flags
= rename
->flags
;
1701 return rename
->new_name
;
1707 /* Create a section in OBFD with the same
1708 name and attributes as ISECTION in IBFD. */
1711 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1713 bfd
*obfd
= obfdarg
;
1714 struct section_list
*p
;
1722 char *prefix
= NULL
;
1724 if (is_strip_section (ibfd
, isection
))
1727 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1731 /* Get the, possibly new, name of the output section. */
1732 name
= find_section_rename (ibfd
, isection
, & flags
);
1734 /* Prefix sections. */
1735 if ((prefix_alloc_sections_string
)
1736 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1737 prefix
= prefix_alloc_sections_string
;
1738 else if (prefix_sections_string
)
1739 prefix
= prefix_sections_string
;
1745 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1751 osection
= bfd_make_section_anyway (obfd
, name
);
1753 if (osection
== NULL
)
1759 size
= bfd_section_size (ibfd
, isection
);
1761 size
= (size
+ interleave
- 1) / interleave
;
1762 if (! bfd_set_section_size (obfd
, osection
, size
))
1768 vma
= bfd_section_vma (ibfd
, isection
);
1769 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1771 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1774 vma
+= change_section_address
;
1776 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1782 lma
= isection
->lma
;
1783 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1785 if (p
->change_lma
== CHANGE_MODIFY
)
1787 else if (p
->change_lma
== CHANGE_SET
)
1793 lma
+= change_section_address
;
1795 osection
->lma
= lma
;
1797 /* FIXME: This is probably not enough. If we change the LMA we
1798 may have to recompute the header for the file as well. */
1799 if (!bfd_set_section_alignment (obfd
,
1801 bfd_section_alignment (ibfd
, isection
)))
1803 err
= _("alignment");
1807 if (p
!= NULL
&& p
->set_flags
)
1808 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1809 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1815 /* Copy merge entity size. */
1816 osection
->entsize
= isection
->entsize
;
1818 /* This used to be mangle_section; we do here to avoid using
1819 bfd_get_section_by_name since some formats allow multiple
1820 sections with the same name. */
1821 isection
->output_section
= osection
;
1822 isection
->output_offset
= 0;
1824 /* Allow the BFD backend to copy any private data it understands
1825 from the input section to the output section. */
1826 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1827 && strip_symbols
== STRIP_NONDEBUG
)
1828 /* Do not copy the private data when creating an ELF format
1829 debug info file. We do not want the program headers. */
1831 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1833 err
= _("private data");
1837 /* All went well. */
1841 non_fatal (_("%s: section `%s': error in %s: %s"),
1842 bfd_get_filename (ibfd
),
1843 bfd_section_name (ibfd
, isection
),
1844 err
, bfd_errmsg (bfd_get_error ()));
1848 /* Copy the data of input section ISECTION of IBFD
1849 to an output section with the same name in OBFD.
1850 If stripping then don't copy any relocation info. */
1853 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1855 bfd
*obfd
= obfdarg
;
1856 struct section_list
*p
;
1864 /* If we have already failed earlier on,
1865 do not keep on generating complaints now. */
1869 if (is_strip_section (ibfd
, isection
))
1872 flags
= bfd_get_section_flags (ibfd
, isection
);
1873 if ((flags
& SEC_GROUP
) != 0)
1876 osection
= isection
->output_section
;
1877 size
= bfd_get_section_size_before_reloc (isection
);
1879 if (size
== 0 || osection
== 0)
1882 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
1884 /* Core files do not need to be relocated. */
1885 if (bfd_get_format (obfd
) == bfd_core
)
1888 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1891 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1894 bfd_set_reloc (obfd
, osection
, NULL
, 0);
1897 relpp
= xmalloc (relsize
);
1898 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1900 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1902 if (strip_symbols
== STRIP_ALL
)
1904 /* Remove relocations which are not in
1905 keep_strip_specific_list. */
1906 arelent
**temp_relpp
;
1907 long temp_relcount
= 0;
1910 temp_relpp
= xmalloc (relsize
);
1911 for (i
= 0; i
< relcount
; i
++)
1912 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1913 keep_specific_list
))
1914 temp_relpp
[temp_relcount
++] = relpp
[i
];
1915 relcount
= temp_relcount
;
1920 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
1923 isection
->_cooked_size
= isection
->_raw_size
;
1924 isection
->reloc_done
= TRUE
;
1926 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
1927 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
1929 void *memhunk
= xmalloc (size
);
1931 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
1932 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1935 filter_bytes (memhunk
, &size
);
1937 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
1938 RETURN_NONFATAL (bfd_get_filename (obfd
));
1942 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1944 void *memhunk
= xmalloc (size
);
1946 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1947 flag--they can just remove the section entirely and add it
1948 back again. However, we do permit them to turn on the
1949 SEC_HAS_CONTENTS flag, and take it to mean that the section
1950 contents should be zeroed out. */
1952 memset (memhunk
, 0, size
);
1953 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
1954 RETURN_NONFATAL (bfd_get_filename (obfd
));
1959 /* Get all the sections. This is used when --gap-fill or --pad-to is
1963 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
1965 asection
***secppp
= secppparg
;
1967 **secppp
= osection
;
1971 /* Sort sections by VMA. This is called via qsort, and is used when
1972 --gap-fill or --pad-to is used. We force non loadable or empty
1973 sections to the front, where they are easier to ignore. */
1976 compare_section_lma (const void *arg1
, const void *arg2
)
1978 const asection
*const *sec1
= arg1
;
1979 const asection
*const *sec2
= arg2
;
1980 flagword flags1
, flags2
;
1982 /* Sort non loadable sections to the front. */
1983 flags1
= (*sec1
)->flags
;
1984 flags2
= (*sec2
)->flags
;
1985 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1986 || (flags1
& SEC_LOAD
) == 0)
1988 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1989 && (flags2
& SEC_LOAD
) != 0)
1994 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1995 || (flags2
& SEC_LOAD
) == 0)
1999 /* Sort sections by LMA. */
2000 if ((*sec1
)->lma
> (*sec2
)->lma
)
2002 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2005 /* Sort sections with the same LMA by size. */
2006 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
2008 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
2014 /* Mark all the symbols which will be used in output relocations with
2015 the BSF_KEEP flag so that those symbols will not be stripped.
2017 Ignore relocations which will not appear in the output file. */
2020 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2022 asymbol
**symbols
= symbolsarg
;
2027 /* Ignore an input section with no corresponding output section. */
2028 if (isection
->output_section
== NULL
)
2031 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2033 bfd_fatal (bfd_get_filename (ibfd
));
2038 relpp
= xmalloc (relsize
);
2039 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2041 bfd_fatal (bfd_get_filename (ibfd
));
2043 /* Examine each symbol used in a relocation. If it's not one of the
2044 special bfd section symbols, then mark it with BSF_KEEP. */
2045 for (i
= 0; i
< relcount
; i
++)
2047 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2048 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2049 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2050 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2057 /* Write out debugging information. */
2060 write_debugging_info (bfd
*obfd
, void *dhandle
,
2061 long *symcountp ATTRIBUTE_UNUSED
,
2062 asymbol
***symppp ATTRIBUTE_UNUSED
)
2064 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2065 return write_ieee_debugging_info (obfd
, dhandle
);
2067 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2068 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2070 bfd_byte
*syms
, *strings
;
2071 bfd_size_type symsize
, stringsize
;
2072 asection
*stabsec
, *stabstrsec
;
2074 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2079 stabsec
= bfd_make_section (obfd
, ".stab");
2080 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2082 || stabstrsec
== NULL
2083 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2084 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2085 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2086 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2087 || ! bfd_set_section_flags (obfd
, stabsec
,
2091 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2096 non_fatal (_("%s: can't create debugging section: %s"),
2097 bfd_get_filename (obfd
),
2098 bfd_errmsg (bfd_get_error ()));
2102 /* We can get away with setting the section contents now because
2103 the next thing the caller is going to do is copy over the
2104 real sections. We may someday have to split the contents
2105 setting out of this function. */
2106 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2107 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2110 non_fatal (_("%s: can't set debugging section contents: %s"),
2111 bfd_get_filename (obfd
),
2112 bfd_errmsg (bfd_get_error ()));
2119 non_fatal (_("%s: don't know how to write debugging information for %s"),
2120 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2125 strip_main (int argc
, char *argv
[])
2127 char *input_target
= NULL
;
2128 char *output_target
= NULL
;
2129 bfd_boolean show_version
= FALSE
;
2130 bfd_boolean formats_info
= FALSE
;
2133 struct section_list
*p
;
2134 char *output_file
= NULL
;
2136 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVv",
2137 strip_options
, (int *) 0)) != EOF
)
2142 input_target
= optarg
;
2145 output_target
= optarg
;
2148 input_target
= output_target
= optarg
;
2151 p
= find_section_list (optarg
, TRUE
);
2153 sections_removed
= TRUE
;
2156 strip_symbols
= STRIP_ALL
;
2160 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2161 strip_symbols
= STRIP_DEBUG
;
2163 case OPTION_STRIP_UNNEEDED
:
2164 strip_symbols
= STRIP_UNNEEDED
;
2167 add_specific_symbol (optarg
, &keep_specific_list
);
2170 add_specific_symbol (optarg
, &strip_specific_list
);
2173 output_file
= optarg
;
2176 preserve_dates
= TRUE
;
2179 discard_locals
= LOCALS_ALL
;
2182 discard_locals
= LOCALS_START_L
;
2188 show_version
= TRUE
;
2190 case OPTION_FORMATS_INFO
:
2191 formats_info
= TRUE
;
2193 case OPTION_ONLY_KEEP_DEBUG
:
2194 strip_symbols
= STRIP_NONDEBUG
;
2197 /* We've been given a long option. */
2201 strip_usage (stdout
, 0);
2203 strip_usage (stderr
, 1);
2214 print_version ("strip");
2216 /* Default is to strip all symbols. */
2217 if (strip_symbols
== STRIP_UNDEF
2218 && discard_locals
== LOCALS_UNDEF
2219 && strip_specific_list
== NULL
)
2220 strip_symbols
= STRIP_ALL
;
2222 if (output_target
== NULL
)
2223 output_target
= input_target
;
2227 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2228 strip_usage (stderr
, 1);
2230 for (; i
< argc
; i
++)
2232 int hold_status
= status
;
2233 struct stat statbuf
;
2238 if (stat (argv
[i
], &statbuf
) < 0)
2240 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
2245 if (output_file
!= NULL
)
2246 tmpname
= output_file
;
2248 tmpname
= make_tempname (argv
[i
]);
2251 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2255 set_times (tmpname
, &statbuf
);
2256 if (output_file
== NULL
)
2257 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2258 status
= hold_status
;
2262 if (output_file
== NULL
)
2270 copy_main (int argc
, char *argv
[])
2272 char * binary_architecture
= NULL
;
2273 char *input_filename
= NULL
;
2274 char *output_filename
= NULL
;
2275 char *input_target
= NULL
;
2276 char *output_target
= NULL
;
2277 bfd_boolean show_version
= FALSE
;
2278 bfd_boolean change_warn
= TRUE
;
2279 bfd_boolean formats_info
= FALSE
;
2281 struct section_list
*p
;
2282 struct stat statbuf
;
2284 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2285 copy_options
, (int *) 0)) != EOF
)
2290 copy_byte
= atoi (optarg
);
2292 fatal (_("byte number must be non-negative"));
2296 binary_architecture
= optarg
;
2300 interleave
= atoi (optarg
);
2302 fatal (_("interleave must be positive"));
2306 case 's': /* "source" - 'I' is preferred */
2307 input_target
= optarg
;
2311 case 'd': /* "destination" - 'O' is preferred */
2312 output_target
= optarg
;
2316 input_target
= output_target
= optarg
;
2320 p
= find_section_list (optarg
, TRUE
);
2322 fatal (_("%s both copied and removed"), optarg
);
2324 sections_copied
= TRUE
;
2328 p
= find_section_list (optarg
, TRUE
);
2330 fatal (_("%s both copied and removed"), optarg
);
2332 sections_removed
= TRUE
;
2336 strip_symbols
= STRIP_ALL
;
2340 strip_symbols
= STRIP_DEBUG
;
2343 case OPTION_STRIP_UNNEEDED
:
2344 strip_symbols
= STRIP_UNNEEDED
;
2347 case OPTION_ONLY_KEEP_DEBUG
:
2348 strip_symbols
= STRIP_NONDEBUG
;
2351 case OPTION_ADD_GNU_DEBUGLINK
:
2352 gnu_debuglink_filename
= optarg
;
2356 add_specific_symbol (optarg
, &keep_specific_list
);
2360 add_specific_symbol (optarg
, &strip_specific_list
);
2364 add_specific_symbol (optarg
, &localize_specific_list
);
2368 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2372 add_specific_symbol (optarg
, &weaken_specific_list
);
2376 preserve_dates
= TRUE
;
2380 discard_locals
= LOCALS_ALL
;
2384 discard_locals
= LOCALS_START_L
;
2392 show_version
= TRUE
;
2395 case OPTION_FORMATS_INFO
:
2396 formats_info
= TRUE
;
2403 case OPTION_ADD_SECTION
:
2407 struct section_add
*pa
;
2412 s
= strchr (optarg
, '=');
2415 fatal (_("bad format for %s"), "--add-section");
2417 if (stat (s
+ 1, & st
) < 0)
2418 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
2420 pa
= xmalloc (sizeof (struct section_add
));
2423 name
= xmalloc (len
+ 1);
2424 strncpy (name
, optarg
, len
);
2428 pa
->filename
= s
+ 1;
2430 pa
->size
= st
.st_size
;
2432 pa
->contents
= xmalloc (pa
->size
);
2433 f
= fopen (pa
->filename
, FOPEN_RB
);
2436 fatal (_("cannot open: %s: %s"),
2437 pa
->filename
, strerror (errno
));
2439 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2441 fatal (_("%s: fread failed"), pa
->filename
);
2445 pa
->next
= add_sections
;
2450 case OPTION_CHANGE_START
:
2451 change_start
= parse_vma (optarg
, "--change-start");
2454 case OPTION_CHANGE_SECTION_ADDRESS
:
2455 case OPTION_CHANGE_SECTION_LMA
:
2456 case OPTION_CHANGE_SECTION_VMA
:
2461 char *option
= NULL
;
2463 enum change_action what
= CHANGE_IGNORE
;
2467 case OPTION_CHANGE_SECTION_ADDRESS
:
2468 option
= "--change-section-address";
2470 case OPTION_CHANGE_SECTION_LMA
:
2471 option
= "--change-section-lma";
2473 case OPTION_CHANGE_SECTION_VMA
:
2474 option
= "--change-section-vma";
2478 s
= strchr (optarg
, '=');
2481 s
= strchr (optarg
, '+');
2484 s
= strchr (optarg
, '-');
2486 fatal (_("bad format for %s"), option
);
2491 name
= xmalloc (len
+ 1);
2492 strncpy (name
, optarg
, len
);
2495 p
= find_section_list (name
, TRUE
);
2497 val
= parse_vma (s
+ 1, option
);
2501 case '=': what
= CHANGE_SET
; break;
2502 case '-': val
= - val
; /* Drop through. */
2503 case '+': what
= CHANGE_MODIFY
; break;
2508 case OPTION_CHANGE_SECTION_ADDRESS
:
2509 p
->change_vma
= what
;
2513 case OPTION_CHANGE_SECTION_LMA
:
2514 p
->change_lma
= what
;
2518 case OPTION_CHANGE_SECTION_VMA
:
2519 p
->change_vma
= what
;
2526 case OPTION_CHANGE_ADDRESSES
:
2527 change_section_address
= parse_vma (optarg
, "--change-addresses");
2528 change_start
= change_section_address
;
2531 case OPTION_CHANGE_WARNINGS
:
2535 case OPTION_CHANGE_LEADING_CHAR
:
2536 change_leading_char
= TRUE
;
2539 case OPTION_DEBUGGING
:
2540 convert_debugging
= TRUE
;
2543 case OPTION_GAP_FILL
:
2545 bfd_vma gap_fill_vma
;
2547 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2548 gap_fill
= (bfd_byte
) gap_fill_vma
;
2549 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2553 sprintf_vma (buff
, gap_fill_vma
);
2555 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2558 gap_fill_set
= TRUE
;
2562 case OPTION_NO_CHANGE_WARNINGS
:
2563 change_warn
= FALSE
;
2567 pad_to
= parse_vma (optarg
, "--pad-to");
2571 case OPTION_REMOVE_LEADING_CHAR
:
2572 remove_leading_char
= TRUE
;
2575 case OPTION_REDEFINE_SYM
:
2577 /* Push this redefinition onto redefine_symbol_list. */
2581 const char *nextarg
;
2582 char *source
, *target
;
2584 s
= strchr (optarg
, '=');
2586 fatal (_("bad format for %s"), "--redefine-sym");
2589 source
= xmalloc (len
+ 1);
2590 strncpy (source
, optarg
, len
);
2594 len
= strlen (nextarg
);
2595 target
= xmalloc (len
+ 1);
2596 strcpy (target
, nextarg
);
2598 redefine_list_append ("--redefine-sym", source
, target
);
2605 case OPTION_REDEFINE_SYMS
:
2606 add_redefine_syms_file (optarg
);
2609 case OPTION_SET_SECTION_FLAGS
:
2615 s
= strchr (optarg
, '=');
2617 fatal (_("bad format for %s"), "--set-section-flags");
2620 name
= xmalloc (len
+ 1);
2621 strncpy (name
, optarg
, len
);
2624 p
= find_section_list (name
, TRUE
);
2626 p
->set_flags
= TRUE
;
2627 p
->flags
= parse_flags (s
+ 1);
2631 case OPTION_RENAME_SECTION
:
2634 const char *eq
, *fl
;
2639 eq
= strchr (optarg
, '=');
2641 fatal (_("bad format for %s"), "--rename-section");
2645 fatal (_("bad format for %s"), "--rename-section");
2647 old_name
= xmalloc (len
+ 1);
2648 strncpy (old_name
, optarg
, len
);
2652 fl
= strchr (eq
, ',');
2655 flags
= parse_flags (fl
+ 1);
2665 fatal (_("bad format for %s"), "--rename-section");
2667 new_name
= xmalloc (len
+ 1);
2668 strncpy (new_name
, eq
, len
);
2671 add_section_rename (old_name
, new_name
, flags
);
2675 case OPTION_SET_START
:
2676 set_start
= parse_vma (optarg
, "--set-start");
2677 set_start_set
= TRUE
;
2680 case OPTION_SREC_LEN
:
2681 Chunk
= parse_vma (optarg
, "--srec-len");
2684 case OPTION_SREC_FORCES3
:
2688 case OPTION_STRIP_SYMBOLS
:
2689 add_specific_symbols (optarg
, &strip_specific_list
);
2692 case OPTION_KEEP_SYMBOLS
:
2693 add_specific_symbols (optarg
, &keep_specific_list
);
2696 case OPTION_LOCALIZE_SYMBOLS
:
2697 add_specific_symbols (optarg
, &localize_specific_list
);
2700 case OPTION_KEEPGLOBAL_SYMBOLS
:
2701 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2704 case OPTION_WEAKEN_SYMBOLS
:
2705 add_specific_symbols (optarg
, &weaken_specific_list
);
2708 case OPTION_ALT_MACH_CODE
:
2709 use_alt_mach_code
= atoi (optarg
);
2710 if (use_alt_mach_code
<= 0)
2711 fatal (_("alternate machine code index must be positive"));
2714 case OPTION_PREFIX_SYMBOLS
:
2715 prefix_symbols_string
= optarg
;
2718 case OPTION_PREFIX_SECTIONS
:
2719 prefix_sections_string
= optarg
;
2722 case OPTION_PREFIX_ALLOC_SECTIONS
:
2723 prefix_alloc_sections_string
= optarg
;
2727 /* We've been given a long option. */
2732 copy_usage (stdout
, 0);
2735 copy_usage (stderr
, 1);
2746 print_version ("objcopy");
2748 if (copy_byte
>= interleave
)
2749 fatal (_("byte number must be less than interleave"));
2751 if (optind
== argc
|| optind
+ 2 < argc
)
2752 copy_usage (stderr
, 1);
2754 input_filename
= argv
[optind
];
2755 if (optind
+ 1 < argc
)
2756 output_filename
= argv
[optind
+ 1];
2758 /* Default is to strip no symbols. */
2759 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2760 strip_symbols
= STRIP_NONE
;
2762 if (output_target
== NULL
)
2763 output_target
= input_target
;
2765 if (binary_architecture
!= NULL
)
2767 if (input_target
&& strcmp (input_target
, "binary") == 0)
2769 const bfd_arch_info_type
* temp_arch_info
;
2771 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2773 if (temp_arch_info
!= NULL
)
2775 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2776 bfd_external_machine
= temp_arch_info
->mach
;
2779 fatal (_("architecture %s unknown"), binary_architecture
);
2783 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2784 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2789 if (stat (input_filename
, & statbuf
) < 0)
2790 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2792 /* If there is no destination file, or the source and destination files
2793 are the same, then create a temp and rename the result into the input. */
2794 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
2796 char *tmpname
= make_tempname (input_filename
);
2798 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2802 set_times (tmpname
, &statbuf
);
2803 smart_rename (tmpname
, input_filename
, preserve_dates
);
2810 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2812 if (status
== 0 && preserve_dates
)
2813 set_times (output_filename
, &statbuf
);
2818 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2822 if (p
->change_vma
!= CHANGE_IGNORE
)
2826 sprintf_vma (buff
, p
->vma_val
);
2828 /* xgettext:c-format */
2829 non_fatal (_("%s %s%c0x%s never used"),
2830 "--change-section-vma",
2832 p
->change_vma
== CHANGE_SET
? '=' : '+',
2836 if (p
->change_lma
!= CHANGE_IGNORE
)
2840 sprintf_vma (buff
, p
->lma_val
);
2842 /* xgettext:c-format */
2843 non_fatal (_("%s %s%c0x%s never used"),
2844 "--change-section-lma",
2846 p
->change_lma
== CHANGE_SET
? '=' : '+',
2857 main (int argc
, char *argv
[])
2859 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2860 setlocale (LC_MESSAGES
, "");
2862 #if defined (HAVE_SETLOCALE)
2863 setlocale (LC_CTYPE
, "");
2865 bindtextdomain (PACKAGE
, LOCALEDIR
);
2866 textdomain (PACKAGE
);
2868 program_name
= argv
[0];
2869 xmalloc_set_program_name (program_name
);
2871 START_PROGRESS (program_name
, 0);
2873 strip_symbols
= STRIP_UNDEF
;
2874 discard_locals
= LOCALS_UNDEF
;
2877 set_default_bfd_target ();
2881 int i
= strlen (program_name
);
2882 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2883 /* Drop the .exe suffix, if any. */
2884 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2887 program_name
[i
] = '\0';
2890 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2894 strip_main (argc
, argv
);
2896 copy_main (argc
, argv
);
2898 END_PROGRESS (program_name
);