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 size
= get_file_size (filename
);
596 buffer
= xmalloc (size
+ 2);
597 f
= fopen (filename
, FOPEN_RT
);
599 fatal (_("cannot open '%s': %s"), filename
, strerror (errno
));
601 if (fread (buffer
, 1, size
, f
) == 0 || ferror (f
))
602 fatal (_("%s: fread failed"), filename
);
605 buffer
[size
] = '\n';
606 buffer
[size
+ 1] = '\0';
610 for (line
= buffer
; * line
!= '\0'; line
++)
615 int finished
= FALSE
;
617 for (eol
= line
;; eol
++)
623 /* Cope with \n\r. */
631 /* Cope with \r\n. */
642 /* Line comment, Terminate the line here, in case a
643 name is present and then allow the rest of the
644 loop to find the real end of the line. */
656 /* A name may now exist somewhere between 'line' and 'eol'.
657 Strip off leading whitespace and trailing whitespace,
658 then add it to the list. */
659 for (name
= line
; IS_WHITESPACE (* name
); name
++)
661 for (name_end
= name
;
662 (! IS_WHITESPACE (* name_end
))
663 && (! IS_LINE_TERMINATOR (* name_end
));
667 if (! IS_LINE_TERMINATOR (* name_end
))
671 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
674 if (! IS_LINE_TERMINATOR (* extra
))
675 non_fatal (_("Ignoring rubbish found on line %d of %s"),
676 line_count
, filename
);
682 add_specific_symbol (name
, list
);
684 /* Advance line pointer to end of line. The 'eol ++' in the for
685 loop above will then advance us to the start of the next line. */
691 /* See whether a symbol should be stripped or kept based on
692 strip_specific_list and keep_symbols. */
695 is_specified_symbol (const char *name
, struct symlist
*list
)
697 struct symlist
*tmp_list
;
699 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
700 if (strcmp (name
, tmp_list
->name
) == 0)
706 /* See if a section is being removed. */
709 is_strip_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
711 if (sections_removed
|| sections_copied
)
713 struct section_list
*p
;
715 p
= find_section_list (bfd_get_section_name (abfd
, sec
), FALSE
);
717 if (sections_removed
&& p
!= NULL
&& p
->remove
)
719 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
723 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0)
725 if (strip_symbols
== STRIP_DEBUG
726 || strip_symbols
== STRIP_UNNEEDED
727 || strip_symbols
== STRIP_ALL
728 || discard_locals
== LOCALS_ALL
729 || convert_debugging
)
732 if (strip_symbols
== STRIP_NONDEBUG
)
736 return strip_symbols
== STRIP_NONDEBUG
? TRUE
: FALSE
;
739 /* Choose which symbol entries to copy; put the result in OSYMS.
740 We don't copy in place, because that confuses the relocs.
741 Return the number of symbols to print. */
744 filter_symbols (bfd
*abfd
, bfd
*obfd
, asymbol
**osyms
,
745 asymbol
**isyms
, long symcount
)
747 asymbol
**from
= isyms
, **to
= osyms
;
748 long src_count
= 0, dst_count
= 0;
749 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
752 for (; src_count
< symcount
; src_count
++)
754 asymbol
*sym
= from
[src_count
];
755 flagword flags
= sym
->flags
;
756 char *name
= (char *) bfd_asymbol_name (sym
);
758 bfd_boolean undefined
;
759 bfd_boolean rem_leading_char
;
760 bfd_boolean add_leading_char
;
762 undefined
= bfd_is_und_section (bfd_get_section (sym
));
764 if (redefine_sym_list
)
766 char *old_name
, *new_name
;
768 old_name
= (char *) bfd_asymbol_name (sym
);
769 new_name
= (char *) lookup_sym_redefinition (old_name
);
770 bfd_asymbol_name (sym
) = new_name
;
774 /* Check if we will remove the current leading character. */
776 (name
[0] == bfd_get_symbol_leading_char (abfd
))
777 && (change_leading_char
778 || (remove_leading_char
779 && ((flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
781 || bfd_is_com_section (bfd_get_section (sym
)))));
783 /* Check if we will add a new leading character. */
786 && (bfd_get_symbol_leading_char (obfd
) != '\0')
787 && (bfd_get_symbol_leading_char (abfd
) == '\0'
788 || (name
[0] == bfd_get_symbol_leading_char (abfd
)));
790 /* Short circuit for change_leading_char if we can do it in-place. */
791 if (rem_leading_char
&& add_leading_char
&& !prefix_symbols_string
)
793 name
[0] = bfd_get_symbol_leading_char (obfd
);
794 bfd_asymbol_name (sym
) = name
;
795 rem_leading_char
= FALSE
;
796 add_leading_char
= FALSE
;
799 /* Remove leading char. */
800 if (rem_leading_char
)
801 bfd_asymbol_name (sym
) = ++name
;
803 /* Add new leading char and/or prefix. */
804 if (add_leading_char
|| prefix_symbols_string
)
808 ptr
= n
= xmalloc (1 + strlen (prefix_symbols_string
)
809 + strlen (name
) + 1);
810 if (add_leading_char
)
811 *ptr
++ = bfd_get_symbol_leading_char (obfd
);
813 if (prefix_symbols_string
)
815 strcpy (ptr
, prefix_symbols_string
);
816 ptr
+= strlen (prefix_symbols_string
);
820 bfd_asymbol_name (sym
) = n
;
824 if (strip_symbols
== STRIP_ALL
)
826 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
827 || ((flags
& BSF_SECTION_SYM
) != 0
828 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
831 else if (relocatable
/* Relocatable file. */
832 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
834 else if (bfd_decode_symclass (sym
) == 'I')
835 /* Global symbols in $idata sections need to be retained
836 even if relocatable is FALSE. External users of the
837 library containing the $idata section may reference these
840 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
841 || (flags
& BSF_WEAK
) != 0
843 || bfd_is_com_section (bfd_get_section (sym
)))
844 keep
= strip_symbols
!= STRIP_UNNEEDED
;
845 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
846 keep
= (strip_symbols
!= STRIP_DEBUG
847 && strip_symbols
!= STRIP_UNNEEDED
848 && ! convert_debugging
);
849 else if (bfd_get_section (sym
)->comdat
)
850 /* COMDAT sections store special information in local
851 symbols, so we cannot risk stripping any of them. */
853 else /* Local symbol. */
854 keep
= (strip_symbols
!= STRIP_UNNEEDED
855 && (discard_locals
!= LOCALS_ALL
856 && (discard_locals
!= LOCALS_START_L
857 || ! bfd_is_local_label (abfd
, sym
))));
859 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
861 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
863 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
866 if (keep
&& (flags
& BSF_GLOBAL
) != 0
867 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
869 sym
->flags
&=~ BSF_GLOBAL
;
870 sym
->flags
|= BSF_WEAK
;
872 if (keep
&& !undefined
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
873 && (is_specified_symbol (name
, localize_specific_list
)
874 || (keepglobal_specific_list
!= NULL
875 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
877 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
878 sym
->flags
|= BSF_LOCAL
;
882 to
[dst_count
++] = sym
;
885 to
[dst_count
] = NULL
;
890 /* Find the redefined name of symbol SOURCE. */
893 lookup_sym_redefinition (const char *source
)
895 struct redefine_node
*list
;
897 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
898 if (strcmp (source
, list
->source
) == 0)
904 /* Add a node to a symbol redefine list. */
907 redefine_list_append (const char *cause
, const char *source
, const char *target
)
909 struct redefine_node
**p
;
910 struct redefine_node
*list
;
911 struct redefine_node
*new_node
;
913 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
915 if (strcmp (source
, list
->source
) == 0)
916 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
919 if (strcmp (target
, list
->target
) == 0)
920 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
924 new_node
= xmalloc (sizeof (struct redefine_node
));
926 new_node
->source
= strdup (source
);
927 new_node
->target
= strdup (target
);
928 new_node
->next
= NULL
;
933 /* Handle the --redefine-syms option. Read lines containing "old new"
934 from the file, and add them to the symbol redefine list. */
937 add_redefine_syms_file (const char *filename
)
946 file
= fopen (filename
, "r");
948 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
949 filename
, strerror (errno
));
952 buf
= xmalloc (bufsize
);
960 /* Collect the input symbol name. */
961 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
969 buf
= xrealloc (buf
, bufsize
);
977 /* Eat white space between the symbol names. */
978 while (IS_WHITESPACE (c
))
980 if (c
== '#' || IS_LINE_TERMINATOR (c
))
985 /* Collect the output symbol name. */
987 while (! IS_WHITESPACE (c
) && ! IS_LINE_TERMINATOR (c
) && c
!= EOF
)
995 buf
= xrealloc (buf
, bufsize
);
1003 /* Eat white space at end of line. */
1004 while (! IS_LINE_TERMINATOR(c
) && c
!= EOF
&& IS_WHITESPACE (c
))
1009 if ((c
== '\r' && (c
= getc (file
)) == '\n')
1010 || c
== '\n' || c
== EOF
)
1013 /* Append the redefinition to the list. */
1015 redefine_list_append (filename
, &buf
[0], &buf
[outsym_off
]);
1026 fatal (_("%s: garbage at end of line %d"), filename
, lineno
);
1028 if (len
!= 0 && (outsym_off
== 0 || outsym_off
== len
))
1029 fatal (_("%s: missing new symbol name at line %d"), filename
, lineno
);
1032 /* Eat the rest of the line and finish it. */
1033 while (c
!= '\n' && c
!= EOF
)
1039 fatal (_("%s: premature end of file at line %d"), filename
, lineno
);
1044 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
1048 filter_bytes (char *memhunk
, bfd_size_type
*size
)
1050 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
1052 for (; from
< end
; from
+= interleave
)
1055 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
1056 *size
= (*size
/ interleave
) + 1;
1058 *size
/= interleave
;
1061 /* Copy object file IBFD onto OBFD. */
1064 copy_object (bfd
*ibfd
, bfd
*obfd
)
1068 asection
**osections
= NULL
;
1069 asection
*gnu_debuglink_section
= NULL
;
1070 bfd_size_type
*gaps
= NULL
;
1071 bfd_size_type max_gap
= 0;
1074 enum bfd_architecture iarch
;
1077 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
1078 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
1079 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
1081 fatal (_("Unable to change endianness of input file(s)"));
1085 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1086 RETURN_NONFATAL (bfd_get_filename (obfd
));
1089 printf (_("copy from %s(%s) to %s(%s)\n"),
1090 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
1091 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1096 start
= bfd_get_start_address (ibfd
);
1097 start
+= change_start
;
1099 /* Neither the start address nor the flags
1100 need to be set for a core file. */
1101 if (bfd_get_format (obfd
) != bfd_core
)
1103 if (!bfd_set_start_address (obfd
, start
)
1104 || !bfd_set_file_flags (obfd
,
1105 (bfd_get_file_flags (ibfd
)
1106 & bfd_applicable_file_flags (obfd
))))
1107 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1110 /* Copy architecture of input file to output file. */
1111 iarch
= bfd_get_arch (ibfd
);
1112 imach
= bfd_get_mach (ibfd
);
1113 if (!bfd_set_arch_mach (obfd
, iarch
, imach
)
1114 && (ibfd
->target_defaulted
1115 || bfd_get_arch (ibfd
) != bfd_get_arch (obfd
)))
1116 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1117 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
1118 bfd_get_mach (ibfd
)));
1120 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1121 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1126 if (osympp
!= isympp
)
1129 /* BFD mandates that all output sections be created and sizes set before
1130 any output is done. Thus, we traverse all sections multiple times. */
1131 bfd_map_over_sections (ibfd
, setup_section
, obfd
);
1133 if (add_sections
!= NULL
)
1135 struct section_add
*padd
;
1136 struct section_list
*pset
;
1138 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1142 padd
->section
= bfd_make_section (obfd
, padd
->name
);
1143 if (padd
->section
== NULL
)
1145 non_fatal (_("can't create section `%s': %s"),
1146 padd
->name
, bfd_errmsg (bfd_get_error ()));
1151 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
1152 RETURN_NONFATAL (bfd_get_filename (obfd
));
1154 pset
= find_section_list (padd
->name
, FALSE
);
1158 if (pset
!= NULL
&& pset
->set_flags
)
1159 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
1161 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
1163 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
1164 RETURN_NONFATAL (bfd_get_filename (obfd
));
1168 if (pset
->change_vma
!= CHANGE_IGNORE
)
1169 if (! bfd_set_section_vma (obfd
, padd
->section
,
1171 RETURN_NONFATAL (bfd_get_filename (obfd
));
1173 if (pset
->change_lma
!= CHANGE_IGNORE
)
1175 padd
->section
->lma
= pset
->lma_val
;
1177 if (! bfd_set_section_alignment
1178 (obfd
, padd
->section
,
1179 bfd_section_alignment (obfd
, padd
->section
)))
1180 RETURN_NONFATAL (bfd_get_filename (obfd
));
1186 if (gnu_debuglink_filename
!= NULL
)
1188 gnu_debuglink_section
= bfd_create_gnu_debuglink_section
1189 (obfd
, gnu_debuglink_filename
);
1191 if (gnu_debuglink_section
== NULL
)
1192 RETURN_NONFATAL (gnu_debuglink_filename
);
1195 if (gap_fill_set
|| pad_to_set
)
1200 /* We must fill in gaps between the sections and/or we must pad
1201 the last section to a specified address. We do this by
1202 grabbing a list of the sections, sorting them by VMA, and
1203 increasing the section sizes as required to fill the gaps.
1204 We write out the gap contents below. */
1206 c
= bfd_count_sections (obfd
);
1207 osections
= xmalloc (c
* sizeof (asection
*));
1209 bfd_map_over_sections (obfd
, get_sections
, &set
);
1211 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1213 gaps
= xmalloc (c
* sizeof (bfd_size_type
));
1214 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1218 for (i
= 0; i
< c
- 1; i
++)
1222 bfd_vma gap_start
, gap_stop
;
1224 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1225 if ((flags
& SEC_HAS_CONTENTS
) == 0
1226 || (flags
& SEC_LOAD
) == 0)
1229 size
= bfd_section_size (obfd
, osections
[i
]);
1230 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1231 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1232 if (gap_start
< gap_stop
)
1234 if (! bfd_set_section_size (obfd
, osections
[i
],
1235 size
+ (gap_stop
- gap_start
)))
1237 non_fatal (_("Can't fill gap after %s: %s"),
1238 bfd_get_section_name (obfd
, osections
[i
]),
1239 bfd_errmsg (bfd_get_error ()));
1243 gaps
[i
] = gap_stop
- gap_start
;
1244 if (max_gap
< gap_stop
- gap_start
)
1245 max_gap
= gap_stop
- gap_start
;
1255 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1256 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1257 if (lma
+ size
< pad_to
)
1259 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1262 non_fatal (_("Can't add padding to %s: %s"),
1263 bfd_get_section_name (obfd
, osections
[c
- 1]),
1264 bfd_errmsg (bfd_get_error ()));
1269 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1270 if (max_gap
< pad_to
- (lma
+ size
))
1271 max_gap
= pad_to
- (lma
+ size
);
1277 /* Symbol filtering must happen after the output sections
1278 have been created, but before their contents are set. */
1280 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1282 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1284 osympp
= isympp
= xmalloc (symsize
);
1285 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1287 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1289 if (convert_debugging
)
1290 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1292 if (strip_symbols
== STRIP_DEBUG
1293 || strip_symbols
== STRIP_ALL
1294 || strip_symbols
== STRIP_UNNEEDED
1295 || strip_symbols
== STRIP_NONDEBUG
1296 || discard_locals
!= LOCALS_UNDEF
1297 || strip_specific_list
!= NULL
1298 || keep_specific_list
!= NULL
1299 || localize_specific_list
!= NULL
1300 || keepglobal_specific_list
!= NULL
1301 || weaken_specific_list
!= NULL
1302 || prefix_symbols_string
1305 || convert_debugging
1306 || change_leading_char
1307 || remove_leading_char
1308 || redefine_sym_list
1311 /* Mark symbols used in output relocations so that they
1312 are kept, even if they are local labels or static symbols.
1314 Note we iterate over the input sections examining their
1315 relocations since the relocations for the output sections
1316 haven't been set yet. mark_symbols_used_in_relocations will
1317 ignore input sections which have no corresponding output
1319 if (strip_symbols
!= STRIP_ALL
)
1320 bfd_map_over_sections (ibfd
,
1321 mark_symbols_used_in_relocations
,
1323 osympp
= xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1324 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1327 if (convert_debugging
&& dhandle
!= NULL
)
1329 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1336 bfd_set_symtab (obfd
, osympp
, symcount
);
1338 /* This has to happen after the symbol table has been set. */
1339 bfd_map_over_sections (ibfd
, copy_section
, obfd
);
1341 if (add_sections
!= NULL
)
1343 struct section_add
*padd
;
1345 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1347 if (! bfd_set_section_contents (obfd
, padd
->section
, padd
->contents
,
1349 RETURN_NONFATAL (bfd_get_filename (obfd
));
1353 if (gnu_debuglink_filename
!= NULL
)
1355 if (! bfd_fill_in_gnu_debuglink_section
1356 (obfd
, gnu_debuglink_section
, gnu_debuglink_filename
))
1357 RETURN_NONFATAL (gnu_debuglink_filename
);
1360 if (gap_fill_set
|| pad_to_set
)
1365 /* Fill in the gaps. */
1368 buf
= xmalloc (max_gap
);
1369 memset (buf
, gap_fill
, max_gap
);
1371 c
= bfd_count_sections (obfd
);
1372 for (i
= 0; i
< c
; i
++)
1380 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1391 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1393 RETURN_NONFATAL (bfd_get_filename (obfd
));
1402 /* Allow the BFD backend to copy any private data it understands
1403 from the input BFD to the output BFD. This is done last to
1404 permit the routine to look at the filtered symbol table, which is
1405 important for the ECOFF code at least. */
1406 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1407 && strip_symbols
== STRIP_NONDEBUG
)
1408 /* Do not copy the private data when creating an ELF format
1409 debug info file. We do not want the program headers. */
1411 else if (! bfd_copy_private_bfd_data (ibfd
, obfd
))
1413 non_fatal (_("%s: error copying private BFD data: %s"),
1414 bfd_get_filename (obfd
),
1415 bfd_errmsg (bfd_get_error ()));
1420 /* Switch to the alternate machine code. We have to do this at the
1421 very end, because we only initialize the header when we create
1422 the first section. */
1423 if (use_alt_mach_code
!= 0)
1425 if (!bfd_alt_mach_code (obfd
, use_alt_mach_code
))
1426 non_fatal (_("unknown alternate machine code, ignored"));
1431 #if defined (_WIN32) && !defined (__CYGWIN32__)
1432 #define MKDIR(DIR, MODE) mkdir (DIR)
1434 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1437 /* Read each archive element in turn from IBFD, copy the
1438 contents to temp file, and keep the temp file handle. */
1441 copy_archive (bfd
*ibfd
, bfd
*obfd
, const char *output_target
)
1445 struct name_list
*next
;
1449 bfd
**ptr
= &obfd
->archive_head
;
1451 char *dir
= make_tempname (bfd_get_filename (obfd
));
1453 /* Make a temp directory to hold the contents. */
1454 if (MKDIR (dir
, 0700) != 0)
1455 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1456 dir
, strerror (errno
));
1458 obfd
->has_armap
= ibfd
->has_armap
;
1462 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1464 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1465 RETURN_NONFATAL (bfd_get_filename (obfd
));
1467 while (!status
&& this_element
!= NULL
)
1473 int stat_status
= 0;
1475 /* Create an output file for this member. */
1476 output_name
= concat (dir
, "/",
1477 bfd_get_filename (this_element
), (char *) 0);
1479 /* If the file already exists, make another temp dir. */
1480 if (stat (output_name
, &buf
) >= 0)
1482 output_name
= make_tempname (output_name
);
1483 if (MKDIR (output_name
, 0700) != 0)
1484 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1485 output_name
, strerror (errno
));
1487 l
= xmalloc (sizeof (struct name_list
));
1488 l
->name
= output_name
;
1492 output_name
= concat (output_name
, "/",
1493 bfd_get_filename (this_element
), (char *) 0);
1496 output_bfd
= bfd_openw (output_name
, output_target
);
1499 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1501 if (stat_status
!= 0)
1502 non_fatal (_("internal stat error on %s"),
1503 bfd_get_filename (this_element
));
1506 l
= xmalloc (sizeof (struct name_list
));
1507 l
->name
= output_name
;
1511 if (output_bfd
== NULL
)
1512 RETURN_NONFATAL (output_name
);
1514 if (bfd_check_format (this_element
, bfd_object
))
1515 copy_object (this_element
, output_bfd
);
1517 if (!bfd_close (output_bfd
))
1519 bfd_nonfatal (bfd_get_filename (output_bfd
));
1520 /* Error in new object file. Don't change archive. */
1524 if (preserve_dates
&& stat_status
== 0)
1525 set_times (output_name
, &buf
);
1527 /* Open the newly output file and attach to our list. */
1528 output_bfd
= bfd_openr (output_name
, output_target
);
1530 l
->obfd
= output_bfd
;
1533 ptr
= &output_bfd
->next
;
1535 last_element
= this_element
;
1537 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1539 bfd_close (last_element
);
1543 if (!bfd_close (obfd
))
1544 RETURN_NONFATAL (bfd_get_filename (obfd
));
1546 if (!bfd_close (ibfd
))
1547 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1549 /* Delete all the files that we opened. */
1550 for (l
= list
; l
!= NULL
; l
= l
->next
)
1552 if (l
->obfd
== NULL
)
1556 bfd_close (l
->obfd
);
1563 /* The top-level control. */
1566 copy_file (const char *input_filename
, const char *output_filename
,
1567 const char *input_target
, const char *output_target
)
1570 char **obj_matching
;
1571 char **core_matching
;
1573 if (get_file_size (input_filename
) < 1)
1579 /* To allow us to do "strip *" without dying on the first
1580 non-object file, failures are nonfatal. */
1581 ibfd
= bfd_openr (input_filename
, input_target
);
1583 RETURN_NONFATAL (input_filename
);
1585 if (bfd_check_format (ibfd
, bfd_archive
))
1589 /* bfd_get_target does not return the correct value until
1590 bfd_check_format succeeds. */
1591 if (output_target
== NULL
)
1592 output_target
= bfd_get_target (ibfd
);
1594 obfd
= bfd_openw (output_filename
, output_target
);
1596 RETURN_NONFATAL (output_filename
);
1598 copy_archive (ibfd
, obfd
, output_target
);
1600 else if (bfd_check_format_matches (ibfd
, bfd_object
, &obj_matching
))
1604 /* bfd_get_target does not return the correct value until
1605 bfd_check_format succeeds. */
1606 if (output_target
== NULL
)
1607 output_target
= bfd_get_target (ibfd
);
1609 obfd
= bfd_openw (output_filename
, output_target
);
1611 RETURN_NONFATAL (output_filename
);
1613 copy_object (ibfd
, obfd
);
1615 if (!bfd_close (obfd
))
1616 RETURN_NONFATAL (output_filename
);
1618 if (!bfd_close (ibfd
))
1619 RETURN_NONFATAL (input_filename
);
1623 bfd_error_type obj_error
= bfd_get_error ();
1624 bfd_error_type core_error
;
1626 if (bfd_check_format_matches (ibfd
, bfd_core
, &core_matching
))
1628 /* This probably can't happen.. */
1629 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1630 free (obj_matching
);
1634 core_error
= bfd_get_error ();
1635 /* Report the object error in preference to the core error. */
1636 if (obj_error
!= core_error
)
1637 bfd_set_error (obj_error
);
1639 bfd_nonfatal (input_filename
);
1641 if (obj_error
== bfd_error_file_ambiguously_recognized
)
1643 list_matching_formats (obj_matching
);
1644 free (obj_matching
);
1646 if (core_error
== bfd_error_file_ambiguously_recognized
)
1648 list_matching_formats (core_matching
);
1649 free (core_matching
);
1656 /* Add a name to the section renaming list. */
1659 add_section_rename (const char * old_name
, const char * new_name
,
1662 section_rename
* rename
;
1664 /* Check for conflicts first. */
1665 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1666 if (strcmp (rename
->old_name
, old_name
) == 0)
1668 /* Silently ignore duplicate definitions. */
1669 if (strcmp (rename
->new_name
, new_name
) == 0
1670 && rename
->flags
== flags
)
1673 fatal (_("Multiple renames of section %s"), old_name
);
1676 rename
= xmalloc (sizeof (* rename
));
1678 rename
->old_name
= old_name
;
1679 rename
->new_name
= new_name
;
1680 rename
->flags
= flags
;
1681 rename
->next
= section_rename_list
;
1683 section_rename_list
= rename
;
1686 /* Check the section rename list for a new name of the input section
1687 ISECTION. Return the new name if one is found.
1688 Also set RETURNED_FLAGS to the flags to be used for this section. */
1691 find_section_rename (bfd
* ibfd ATTRIBUTE_UNUSED
, sec_ptr isection
,
1692 flagword
* returned_flags
)
1694 const char * old_name
= bfd_section_name (ibfd
, isection
);
1695 section_rename
* rename
;
1697 /* Default to using the flags of the input section. */
1698 * returned_flags
= bfd_get_section_flags (ibfd
, isection
);
1700 for (rename
= section_rename_list
; rename
!= NULL
; rename
= rename
->next
)
1701 if (strcmp (rename
->old_name
, old_name
) == 0)
1703 if (rename
->flags
!= (flagword
) -1)
1704 * returned_flags
= rename
->flags
;
1706 return rename
->new_name
;
1712 /* Create a section in OBFD with the same
1713 name and attributes as ISECTION in IBFD. */
1716 setup_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1718 bfd
*obfd
= obfdarg
;
1719 struct section_list
*p
;
1727 char *prefix
= NULL
;
1729 if (is_strip_section (ibfd
, isection
))
1732 p
= find_section_list (bfd_section_name (ibfd
, isection
), FALSE
);
1736 /* Get the, possibly new, name of the output section. */
1737 name
= find_section_rename (ibfd
, isection
, & flags
);
1739 /* Prefix sections. */
1740 if ((prefix_alloc_sections_string
)
1741 && (bfd_get_section_flags (ibfd
, isection
) & SEC_ALLOC
))
1742 prefix
= prefix_alloc_sections_string
;
1743 else if (prefix_sections_string
)
1744 prefix
= prefix_sections_string
;
1750 n
= xmalloc (strlen (prefix
) + strlen (name
) + 1);
1756 osection
= bfd_make_section_anyway (obfd
, name
);
1758 if (osection
== NULL
)
1764 size
= bfd_section_size (ibfd
, isection
);
1766 size
= (size
+ interleave
- 1) / interleave
;
1767 if (! bfd_set_section_size (obfd
, osection
, size
))
1773 vma
= bfd_section_vma (ibfd
, isection
);
1774 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1776 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1779 vma
+= change_section_address
;
1781 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1787 lma
= isection
->lma
;
1788 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1790 if (p
->change_lma
== CHANGE_MODIFY
)
1792 else if (p
->change_lma
== CHANGE_SET
)
1798 lma
+= change_section_address
;
1800 osection
->lma
= lma
;
1802 /* FIXME: This is probably not enough. If we change the LMA we
1803 may have to recompute the header for the file as well. */
1804 if (!bfd_set_section_alignment (obfd
,
1806 bfd_section_alignment (ibfd
, isection
)))
1808 err
= _("alignment");
1812 if (p
!= NULL
&& p
->set_flags
)
1813 flags
= p
->flags
| (flags
& (SEC_HAS_CONTENTS
| SEC_RELOC
));
1814 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1820 /* Copy merge entity size. */
1821 osection
->entsize
= isection
->entsize
;
1823 /* This used to be mangle_section; we do here to avoid using
1824 bfd_get_section_by_name since some formats allow multiple
1825 sections with the same name. */
1826 isection
->output_section
= osection
;
1827 isection
->output_offset
= 0;
1829 /* Allow the BFD backend to copy any private data it understands
1830 from the input section to the output section. */
1831 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
1832 && strip_symbols
== STRIP_NONDEBUG
)
1833 /* Do not copy the private data when creating an ELF format
1834 debug info file. We do not want the program headers. */
1836 else if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1838 err
= _("private data");
1842 /* All went well. */
1846 non_fatal (_("%s: section `%s': error in %s: %s"),
1847 bfd_get_filename (ibfd
),
1848 bfd_section_name (ibfd
, isection
),
1849 err
, bfd_errmsg (bfd_get_error ()));
1853 /* Copy the data of input section ISECTION of IBFD
1854 to an output section with the same name in OBFD.
1855 If stripping then don't copy any relocation info. */
1858 copy_section (bfd
*ibfd
, sec_ptr isection
, void *obfdarg
)
1860 bfd
*obfd
= obfdarg
;
1861 struct section_list
*p
;
1869 /* If we have already failed earlier on,
1870 do not keep on generating complaints now. */
1874 if (is_strip_section (ibfd
, isection
))
1877 flags
= bfd_get_section_flags (ibfd
, isection
);
1878 if ((flags
& SEC_GROUP
) != 0)
1881 osection
= isection
->output_section
;
1882 size
= bfd_get_section_size_before_reloc (isection
);
1884 if (size
== 0 || osection
== 0)
1887 p
= find_section_list (bfd_get_section_name (ibfd
, isection
), FALSE
);
1889 /* Core files do not need to be relocated. */
1890 if (bfd_get_format (obfd
) == bfd_core
)
1894 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1898 /* Do not complain if the target does not support relocations. */
1899 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
1902 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1907 bfd_set_reloc (obfd
, osection
, NULL
, 0);
1910 relpp
= xmalloc (relsize
);
1911 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1913 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1915 if (strip_symbols
== STRIP_ALL
)
1917 /* Remove relocations which are not in
1918 keep_strip_specific_list. */
1919 arelent
**temp_relpp
;
1920 long temp_relcount
= 0;
1923 temp_relpp
= xmalloc (relsize
);
1924 for (i
= 0; i
< relcount
; i
++)
1925 if (is_specified_symbol (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1926 keep_specific_list
))
1927 temp_relpp
[temp_relcount
++] = relpp
[i
];
1928 relcount
= temp_relcount
;
1933 bfd_set_reloc (obfd
, osection
, relcount
== 0 ? NULL
: relpp
, relcount
);
1936 isection
->_cooked_size
= isection
->_raw_size
;
1937 isection
->reloc_done
= TRUE
;
1939 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
1940 && bfd_get_section_flags (obfd
, osection
) & SEC_HAS_CONTENTS
)
1942 void *memhunk
= xmalloc (size
);
1944 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, 0, size
))
1945 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1948 filter_bytes (memhunk
, &size
);
1950 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
1951 RETURN_NONFATAL (bfd_get_filename (obfd
));
1955 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1957 void *memhunk
= xmalloc (size
);
1959 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1960 flag--they can just remove the section entirely and add it
1961 back again. However, we do permit them to turn on the
1962 SEC_HAS_CONTENTS flag, and take it to mean that the section
1963 contents should be zeroed out. */
1965 memset (memhunk
, 0, size
);
1966 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, 0, size
))
1967 RETURN_NONFATAL (bfd_get_filename (obfd
));
1972 /* Get all the sections. This is used when --gap-fill or --pad-to is
1976 get_sections (bfd
*obfd ATTRIBUTE_UNUSED
, asection
*osection
, void *secppparg
)
1978 asection
***secppp
= secppparg
;
1980 **secppp
= osection
;
1984 /* Sort sections by VMA. This is called via qsort, and is used when
1985 --gap-fill or --pad-to is used. We force non loadable or empty
1986 sections to the front, where they are easier to ignore. */
1989 compare_section_lma (const void *arg1
, const void *arg2
)
1991 const asection
*const *sec1
= arg1
;
1992 const asection
*const *sec2
= arg2
;
1993 flagword flags1
, flags2
;
1995 /* Sort non loadable sections to the front. */
1996 flags1
= (*sec1
)->flags
;
1997 flags2
= (*sec2
)->flags
;
1998 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1999 || (flags1
& SEC_LOAD
) == 0)
2001 if ((flags2
& SEC_HAS_CONTENTS
) != 0
2002 && (flags2
& SEC_LOAD
) != 0)
2007 if ((flags2
& SEC_HAS_CONTENTS
) == 0
2008 || (flags2
& SEC_LOAD
) == 0)
2012 /* Sort sections by LMA. */
2013 if ((*sec1
)->lma
> (*sec2
)->lma
)
2015 else if ((*sec1
)->lma
< (*sec2
)->lma
)
2018 /* Sort sections with the same LMA by size. */
2019 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
2021 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
2027 /* Mark all the symbols which will be used in output relocations with
2028 the BSF_KEEP flag so that those symbols will not be stripped.
2030 Ignore relocations which will not appear in the output file. */
2033 mark_symbols_used_in_relocations (bfd
*ibfd
, sec_ptr isection
, void *symbolsarg
)
2035 asymbol
**symbols
= symbolsarg
;
2040 /* Ignore an input section with no corresponding output section. */
2041 if (isection
->output_section
== NULL
)
2044 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
2047 /* Do not complain if the target does not support relocations. */
2048 if (relsize
== -1 && bfd_get_error () == bfd_error_invalid_operation
)
2050 bfd_fatal (bfd_get_filename (ibfd
));
2056 relpp
= xmalloc (relsize
);
2057 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
2059 bfd_fatal (bfd_get_filename (ibfd
));
2061 /* Examine each symbol used in a relocation. If it's not one of the
2062 special bfd section symbols, then mark it with BSF_KEEP. */
2063 for (i
= 0; i
< relcount
; i
++)
2065 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
2066 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
2067 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
2068 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
2075 /* Write out debugging information. */
2078 write_debugging_info (bfd
*obfd
, void *dhandle
,
2079 long *symcountp ATTRIBUTE_UNUSED
,
2080 asymbol
***symppp ATTRIBUTE_UNUSED
)
2082 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
2083 return write_ieee_debugging_info (obfd
, dhandle
);
2085 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
2086 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
2088 bfd_byte
*syms
, *strings
;
2089 bfd_size_type symsize
, stringsize
;
2090 asection
*stabsec
, *stabstrsec
;
2092 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
2097 stabsec
= bfd_make_section (obfd
, ".stab");
2098 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
2100 || stabstrsec
== NULL
2101 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
2102 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
2103 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
2104 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
2105 || ! bfd_set_section_flags (obfd
, stabsec
,
2109 || ! bfd_set_section_flags (obfd
, stabstrsec
,
2114 non_fatal (_("%s: can't create debugging section: %s"),
2115 bfd_get_filename (obfd
),
2116 bfd_errmsg (bfd_get_error ()));
2120 /* We can get away with setting the section contents now because
2121 the next thing the caller is going to do is copy over the
2122 real sections. We may someday have to split the contents
2123 setting out of this function. */
2124 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, 0, symsize
)
2125 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
, 0,
2128 non_fatal (_("%s: can't set debugging section contents: %s"),
2129 bfd_get_filename (obfd
),
2130 bfd_errmsg (bfd_get_error ()));
2137 non_fatal (_("%s: don't know how to write debugging information for %s"),
2138 bfd_get_filename (obfd
), bfd_get_target (obfd
));
2143 strip_main (int argc
, char *argv
[])
2145 char *input_target
= NULL
;
2146 char *output_target
= NULL
;
2147 bfd_boolean show_version
= FALSE
;
2148 bfd_boolean formats_info
= FALSE
;
2151 struct section_list
*p
;
2152 char *output_file
= NULL
;
2154 while ((c
= getopt_long (argc
, argv
, "I:O:F:K:N:R:o:sSpdgxXHhVv",
2155 strip_options
, (int *) 0)) != EOF
)
2160 input_target
= optarg
;
2163 output_target
= optarg
;
2166 input_target
= output_target
= optarg
;
2169 p
= find_section_list (optarg
, TRUE
);
2171 sections_removed
= TRUE
;
2174 strip_symbols
= STRIP_ALL
;
2178 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2179 strip_symbols
= STRIP_DEBUG
;
2181 case OPTION_STRIP_UNNEEDED
:
2182 strip_symbols
= STRIP_UNNEEDED
;
2185 add_specific_symbol (optarg
, &keep_specific_list
);
2188 add_specific_symbol (optarg
, &strip_specific_list
);
2191 output_file
= optarg
;
2194 preserve_dates
= TRUE
;
2197 discard_locals
= LOCALS_ALL
;
2200 discard_locals
= LOCALS_START_L
;
2206 show_version
= TRUE
;
2208 case OPTION_FORMATS_INFO
:
2209 formats_info
= TRUE
;
2211 case OPTION_ONLY_KEEP_DEBUG
:
2212 strip_symbols
= STRIP_NONDEBUG
;
2215 /* We've been given a long option. */
2219 strip_usage (stdout
, 0);
2221 strip_usage (stderr
, 1);
2232 print_version ("strip");
2234 /* Default is to strip all symbols. */
2235 if (strip_symbols
== STRIP_UNDEF
2236 && discard_locals
== LOCALS_UNDEF
2237 && strip_specific_list
== NULL
)
2238 strip_symbols
= STRIP_ALL
;
2240 if (output_target
== NULL
)
2241 output_target
= input_target
;
2245 || (output_file
!= NULL
&& (i
+ 1) < argc
))
2246 strip_usage (stderr
, 1);
2248 for (; i
< argc
; i
++)
2250 int hold_status
= status
;
2251 struct stat statbuf
;
2254 if (get_file_size (argv
[i
]) < 1)
2258 /* No need to check the return value of stat().
2259 It has already been checked in get_file_size(). */
2260 stat (argv
[i
], &statbuf
);
2262 if (output_file
!= NULL
)
2263 tmpname
= output_file
;
2265 tmpname
= make_tempname (argv
[i
]);
2268 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
2272 set_times (tmpname
, &statbuf
);
2273 if (output_file
== NULL
)
2274 smart_rename (tmpname
, argv
[i
], preserve_dates
);
2275 status
= hold_status
;
2279 if (output_file
== NULL
)
2287 copy_main (int argc
, char *argv
[])
2289 char * binary_architecture
= NULL
;
2290 char *input_filename
= NULL
;
2291 char *output_filename
= NULL
;
2292 char *input_target
= NULL
;
2293 char *output_target
= NULL
;
2294 bfd_boolean show_version
= FALSE
;
2295 bfd_boolean change_warn
= TRUE
;
2296 bfd_boolean formats_info
= FALSE
;
2298 struct section_list
*p
;
2299 struct stat statbuf
;
2301 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
2302 copy_options
, (int *) 0)) != EOF
)
2307 copy_byte
= atoi (optarg
);
2309 fatal (_("byte number must be non-negative"));
2313 binary_architecture
= optarg
;
2317 interleave
= atoi (optarg
);
2319 fatal (_("interleave must be positive"));
2323 case 's': /* "source" - 'I' is preferred */
2324 input_target
= optarg
;
2328 case 'd': /* "destination" - 'O' is preferred */
2329 output_target
= optarg
;
2333 input_target
= output_target
= optarg
;
2337 p
= find_section_list (optarg
, TRUE
);
2339 fatal (_("%s both copied and removed"), optarg
);
2341 sections_copied
= TRUE
;
2345 p
= find_section_list (optarg
, TRUE
);
2347 fatal (_("%s both copied and removed"), optarg
);
2349 sections_removed
= TRUE
;
2353 strip_symbols
= STRIP_ALL
;
2357 strip_symbols
= STRIP_DEBUG
;
2360 case OPTION_STRIP_UNNEEDED
:
2361 strip_symbols
= STRIP_UNNEEDED
;
2364 case OPTION_ONLY_KEEP_DEBUG
:
2365 strip_symbols
= STRIP_NONDEBUG
;
2368 case OPTION_ADD_GNU_DEBUGLINK
:
2369 gnu_debuglink_filename
= optarg
;
2373 add_specific_symbol (optarg
, &keep_specific_list
);
2377 add_specific_symbol (optarg
, &strip_specific_list
);
2381 add_specific_symbol (optarg
, &localize_specific_list
);
2385 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2389 add_specific_symbol (optarg
, &weaken_specific_list
);
2393 preserve_dates
= TRUE
;
2397 discard_locals
= LOCALS_ALL
;
2401 discard_locals
= LOCALS_START_L
;
2409 show_version
= TRUE
;
2412 case OPTION_FORMATS_INFO
:
2413 formats_info
= TRUE
;
2420 case OPTION_ADD_SECTION
:
2424 struct section_add
*pa
;
2429 s
= strchr (optarg
, '=');
2432 fatal (_("bad format for %s"), "--add-section");
2434 size
= get_file_size (s
+ 1);
2438 pa
= xmalloc (sizeof (struct section_add
));
2441 name
= xmalloc (len
+ 1);
2442 strncpy (name
, optarg
, len
);
2446 pa
->filename
= s
+ 1;
2448 pa
->contents
= xmalloc (size
);
2450 f
= fopen (pa
->filename
, FOPEN_RB
);
2453 fatal (_("cannot open: %s: %s"),
2454 pa
->filename
, strerror (errno
));
2456 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2458 fatal (_("%s: fread failed"), pa
->filename
);
2462 pa
->next
= add_sections
;
2467 case OPTION_CHANGE_START
:
2468 change_start
= parse_vma (optarg
, "--change-start");
2471 case OPTION_CHANGE_SECTION_ADDRESS
:
2472 case OPTION_CHANGE_SECTION_LMA
:
2473 case OPTION_CHANGE_SECTION_VMA
:
2478 char *option
= NULL
;
2480 enum change_action what
= CHANGE_IGNORE
;
2484 case OPTION_CHANGE_SECTION_ADDRESS
:
2485 option
= "--change-section-address";
2487 case OPTION_CHANGE_SECTION_LMA
:
2488 option
= "--change-section-lma";
2490 case OPTION_CHANGE_SECTION_VMA
:
2491 option
= "--change-section-vma";
2495 s
= strchr (optarg
, '=');
2498 s
= strchr (optarg
, '+');
2501 s
= strchr (optarg
, '-');
2503 fatal (_("bad format for %s"), option
);
2508 name
= xmalloc (len
+ 1);
2509 strncpy (name
, optarg
, len
);
2512 p
= find_section_list (name
, TRUE
);
2514 val
= parse_vma (s
+ 1, option
);
2518 case '=': what
= CHANGE_SET
; break;
2519 case '-': val
= - val
; /* Drop through. */
2520 case '+': what
= CHANGE_MODIFY
; break;
2525 case OPTION_CHANGE_SECTION_ADDRESS
:
2526 p
->change_vma
= what
;
2530 case OPTION_CHANGE_SECTION_LMA
:
2531 p
->change_lma
= what
;
2535 case OPTION_CHANGE_SECTION_VMA
:
2536 p
->change_vma
= what
;
2543 case OPTION_CHANGE_ADDRESSES
:
2544 change_section_address
= parse_vma (optarg
, "--change-addresses");
2545 change_start
= change_section_address
;
2548 case OPTION_CHANGE_WARNINGS
:
2552 case OPTION_CHANGE_LEADING_CHAR
:
2553 change_leading_char
= TRUE
;
2556 case OPTION_DEBUGGING
:
2557 convert_debugging
= TRUE
;
2560 case OPTION_GAP_FILL
:
2562 bfd_vma gap_fill_vma
;
2564 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2565 gap_fill
= (bfd_byte
) gap_fill_vma
;
2566 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2570 sprintf_vma (buff
, gap_fill_vma
);
2572 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2575 gap_fill_set
= TRUE
;
2579 case OPTION_NO_CHANGE_WARNINGS
:
2580 change_warn
= FALSE
;
2584 pad_to
= parse_vma (optarg
, "--pad-to");
2588 case OPTION_REMOVE_LEADING_CHAR
:
2589 remove_leading_char
= TRUE
;
2592 case OPTION_REDEFINE_SYM
:
2594 /* Push this redefinition onto redefine_symbol_list. */
2598 const char *nextarg
;
2599 char *source
, *target
;
2601 s
= strchr (optarg
, '=');
2603 fatal (_("bad format for %s"), "--redefine-sym");
2606 source
= xmalloc (len
+ 1);
2607 strncpy (source
, optarg
, len
);
2611 len
= strlen (nextarg
);
2612 target
= xmalloc (len
+ 1);
2613 strcpy (target
, nextarg
);
2615 redefine_list_append ("--redefine-sym", source
, target
);
2622 case OPTION_REDEFINE_SYMS
:
2623 add_redefine_syms_file (optarg
);
2626 case OPTION_SET_SECTION_FLAGS
:
2632 s
= strchr (optarg
, '=');
2634 fatal (_("bad format for %s"), "--set-section-flags");
2637 name
= xmalloc (len
+ 1);
2638 strncpy (name
, optarg
, len
);
2641 p
= find_section_list (name
, TRUE
);
2643 p
->set_flags
= TRUE
;
2644 p
->flags
= parse_flags (s
+ 1);
2648 case OPTION_RENAME_SECTION
:
2651 const char *eq
, *fl
;
2656 eq
= strchr (optarg
, '=');
2658 fatal (_("bad format for %s"), "--rename-section");
2662 fatal (_("bad format for %s"), "--rename-section");
2664 old_name
= xmalloc (len
+ 1);
2665 strncpy (old_name
, optarg
, len
);
2669 fl
= strchr (eq
, ',');
2672 flags
= parse_flags (fl
+ 1);
2682 fatal (_("bad format for %s"), "--rename-section");
2684 new_name
= xmalloc (len
+ 1);
2685 strncpy (new_name
, eq
, len
);
2688 add_section_rename (old_name
, new_name
, flags
);
2692 case OPTION_SET_START
:
2693 set_start
= parse_vma (optarg
, "--set-start");
2694 set_start_set
= TRUE
;
2697 case OPTION_SREC_LEN
:
2698 Chunk
= parse_vma (optarg
, "--srec-len");
2701 case OPTION_SREC_FORCES3
:
2705 case OPTION_STRIP_SYMBOLS
:
2706 add_specific_symbols (optarg
, &strip_specific_list
);
2709 case OPTION_KEEP_SYMBOLS
:
2710 add_specific_symbols (optarg
, &keep_specific_list
);
2713 case OPTION_LOCALIZE_SYMBOLS
:
2714 add_specific_symbols (optarg
, &localize_specific_list
);
2717 case OPTION_KEEPGLOBAL_SYMBOLS
:
2718 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2721 case OPTION_WEAKEN_SYMBOLS
:
2722 add_specific_symbols (optarg
, &weaken_specific_list
);
2725 case OPTION_ALT_MACH_CODE
:
2726 use_alt_mach_code
= atoi (optarg
);
2727 if (use_alt_mach_code
<= 0)
2728 fatal (_("alternate machine code index must be positive"));
2731 case OPTION_PREFIX_SYMBOLS
:
2732 prefix_symbols_string
= optarg
;
2735 case OPTION_PREFIX_SECTIONS
:
2736 prefix_sections_string
= optarg
;
2739 case OPTION_PREFIX_ALLOC_SECTIONS
:
2740 prefix_alloc_sections_string
= optarg
;
2744 /* We've been given a long option. */
2749 copy_usage (stdout
, 0);
2752 copy_usage (stderr
, 1);
2763 print_version ("objcopy");
2765 if (copy_byte
>= interleave
)
2766 fatal (_("byte number must be less than interleave"));
2768 if (optind
== argc
|| optind
+ 2 < argc
)
2769 copy_usage (stderr
, 1);
2771 input_filename
= argv
[optind
];
2772 if (optind
+ 1 < argc
)
2773 output_filename
= argv
[optind
+ 1];
2775 /* Default is to strip no symbols. */
2776 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2777 strip_symbols
= STRIP_NONE
;
2779 if (output_target
== NULL
)
2780 output_target
= input_target
;
2782 if (binary_architecture
!= NULL
)
2784 if (input_target
&& strcmp (input_target
, "binary") == 0)
2786 const bfd_arch_info_type
* temp_arch_info
;
2788 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2790 if (temp_arch_info
!= NULL
)
2792 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2793 bfd_external_machine
= temp_arch_info
->mach
;
2796 fatal (_("architecture %s unknown"), binary_architecture
);
2800 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2801 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2806 if (stat (input_filename
, & statbuf
) < 0)
2807 fatal (_("warning: could not locate '%s'. System error message: %s"),
2808 input_filename
, strerror (errno
));
2810 /* If there is no destination file, or the source and destination files
2811 are the same, then create a temp and rename the result into the input. */
2812 if (output_filename
== NULL
|| strcmp (input_filename
, output_filename
) == 0)
2814 char *tmpname
= make_tempname (input_filename
);
2816 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2820 set_times (tmpname
, &statbuf
);
2821 smart_rename (tmpname
, input_filename
, preserve_dates
);
2828 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2830 if (status
== 0 && preserve_dates
)
2831 set_times (output_filename
, &statbuf
);
2836 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2840 if (p
->change_vma
!= CHANGE_IGNORE
)
2844 sprintf_vma (buff
, p
->vma_val
);
2846 /* xgettext:c-format */
2847 non_fatal (_("%s %s%c0x%s never used"),
2848 "--change-section-vma",
2850 p
->change_vma
== CHANGE_SET
? '=' : '+',
2854 if (p
->change_lma
!= CHANGE_IGNORE
)
2858 sprintf_vma (buff
, p
->lma_val
);
2860 /* xgettext:c-format */
2861 non_fatal (_("%s %s%c0x%s never used"),
2862 "--change-section-lma",
2864 p
->change_lma
== CHANGE_SET
? '=' : '+',
2875 main (int argc
, char *argv
[])
2877 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2878 setlocale (LC_MESSAGES
, "");
2880 #if defined (HAVE_SETLOCALE)
2881 setlocale (LC_CTYPE
, "");
2883 bindtextdomain (PACKAGE
, LOCALEDIR
);
2884 textdomain (PACKAGE
);
2886 program_name
= argv
[0];
2887 xmalloc_set_program_name (program_name
);
2889 START_PROGRESS (program_name
, 0);
2891 strip_symbols
= STRIP_UNDEF
;
2892 discard_locals
= LOCALS_UNDEF
;
2895 set_default_bfd_target ();
2899 int i
= strlen (program_name
);
2900 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2901 /* Drop the .exe suffix, if any. */
2902 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2905 program_name
[i
] = '\0';
2908 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2912 strip_main (argc
, argv
);
2914 copy_main (argc
, argv
);
2916 END_PROGRESS (program_name
);