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 static void copy_usage
PARAMS ((FILE *, int));
52 static void strip_usage
PARAMS ((FILE *, int));
53 static flagword parse_flags
PARAMS ((const char *));
54 static struct section_list
*find_section_list
PARAMS ((const char *, boolean
));
55 static void setup_section
PARAMS ((bfd
*, asection
*, PTR
));
56 static void copy_section
PARAMS ((bfd
*, asection
*, PTR
));
57 static void get_sections
PARAMS ((bfd
*, asection
*, PTR
));
58 static int compare_section_lma
PARAMS ((const PTR
, const PTR
));
59 static void add_specific_symbol
PARAMS ((const char *, struct symlist
**));
60 static void add_specific_symbols
PARAMS ((const char *, struct symlist
**));
61 static boolean is_specified_symbol
PARAMS ((const char *, struct symlist
*));
62 static boolean is_strip_section
PARAMS ((bfd
*, asection
*));
63 static unsigned int filter_symbols
64 PARAMS ((bfd
*, bfd
*, asymbol
**, asymbol
**, long));
65 static void mark_symbols_used_in_relocations
PARAMS ((bfd
*, asection
*, PTR
));
66 static void filter_bytes
PARAMS ((char *, bfd_size_type
*));
67 static boolean write_debugging_info
PARAMS ((bfd
*, PTR
, long *, asymbol
***));
68 static void copy_object
PARAMS ((bfd
*, bfd
*));
69 static void copy_archive
PARAMS ((bfd
*, bfd
*, const char *));
71 PARAMS ((const char *, const char *, const char *, const char *));
72 static int strip_main
PARAMS ((int, char **));
73 static int copy_main
PARAMS ((int, char **));
74 static const char *lookup_sym_redefinition
PARAMS((const char *));
75 static void redefine_list_append
PARAMS ((const char *, const char *));
77 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
79 static asymbol
**isympp
= NULL
; /* Input symbols */
80 static asymbol
**osympp
= NULL
; /* Output symbols that survive stripping */
82 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
83 static int copy_byte
= -1;
84 static int interleave
= 4;
86 static boolean verbose
; /* Print file and target names. */
87 static boolean preserve_dates
; /* Preserve input file timestamp. */
88 static int status
= 0; /* Exit status. */
93 STRIP_NONE
, /* don't strip */
94 STRIP_DEBUG
, /* strip all debugger symbols */
95 STRIP_UNNEEDED
, /* strip unnecessary symbols */
96 STRIP_ALL
/* strip all symbols */
99 /* Which symbols to remove. */
100 static enum strip_action strip_symbols
;
105 LOCALS_START_L
, /* discard locals starting with L */
106 LOCALS_ALL
/* discard all locals */
109 /* Which local symbols to remove. Overrides STRIP_ALL. */
110 static enum locals_action discard_locals
;
112 /* What kind of change to perform. */
120 /* Structure used to hold lists of sections and actions to take. */
123 struct section_list
* next
; /* Next section to change. */
124 const char * name
; /* Section name. */
125 boolean used
; /* Whether this entry was used. */
126 boolean remove
; /* Whether to remove this section. */
127 boolean copy
; /* Whether to copy this section. */
128 enum change_action change_vma
;/* Whether to change or set VMA. */
129 bfd_vma vma_val
; /* Amount to change by or set to. */
130 enum change_action change_lma
;/* Whether to change or set LMA. */
131 bfd_vma lma_val
; /* Amount to change by or set to. */
132 boolean set_flags
; /* Whether to set the section flags. */
133 flagword flags
; /* What to set the section flags to. */
136 static struct section_list
*change_sections
;
137 static boolean sections_removed
;
138 static boolean sections_copied
;
140 /* Changes to the start address. */
141 static bfd_vma change_start
= 0;
142 static boolean set_start_set
= false;
143 static bfd_vma set_start
;
145 /* Changes to section addresses. */
146 static bfd_vma change_section_address
= 0;
148 /* Filling gaps between sections. */
149 static boolean gap_fill_set
= false;
150 static bfd_byte gap_fill
= 0;
152 /* Pad to a given address. */
153 static boolean pad_to_set
= false;
154 static bfd_vma pad_to
;
156 /* List of sections to add. */
160 /* Next section to add. */
161 struct section_add
*next
;
162 /* Name of section to add. */
164 /* Name of file holding section contents. */
165 const char *filename
;
168 /* Contents of file. */
170 /* BFD section, after it has been added. */
174 static struct section_add
*add_sections
;
176 /* Whether to convert debugging information. */
178 static boolean convert_debugging
= false;
180 /* Whether to change the leading character in symbol names. */
182 static boolean change_leading_char
= false;
184 /* Whether to remove the leading character from global symbol names. */
186 static boolean remove_leading_char
= false;
188 /* List of symbols to strip, keep, localize, keep-global, weaken,
191 static struct symlist
*strip_specific_list
= NULL
;
192 static struct symlist
*keep_specific_list
= NULL
;
193 static struct symlist
*localize_specific_list
= NULL
;
194 static struct symlist
*keepglobal_specific_list
= NULL
;
195 static struct symlist
*weaken_specific_list
= NULL
;
196 static struct redefine_node
*redefine_sym_list
= NULL
;
198 /* If this is true, we weaken global symbols (set BSF_WEAK). */
200 static boolean weaken
= false;
202 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
204 #define OPTION_ADD_SECTION 150
205 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1)
206 #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1)
207 #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1)
208 #define OPTION_CHANGE_SECTION_ADDRESS (OPTION_CHANGE_START + 1)
209 #define OPTION_CHANGE_SECTION_LMA (OPTION_CHANGE_SECTION_ADDRESS + 1)
210 #define OPTION_CHANGE_SECTION_VMA (OPTION_CHANGE_SECTION_LMA + 1)
211 #define OPTION_CHANGE_WARNINGS (OPTION_CHANGE_SECTION_VMA + 1)
212 #define OPTION_DEBUGGING (OPTION_CHANGE_WARNINGS + 1)
213 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
214 #define OPTION_NO_CHANGE_WARNINGS (OPTION_GAP_FILL + 1)
215 #define OPTION_PAD_TO (OPTION_NO_CHANGE_WARNINGS + 1)
216 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
217 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
218 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
219 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
220 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
221 #define OPTION_REDEFINE_SYM (OPTION_WEAKEN + 1)
222 #define OPTION_SREC_LEN (OPTION_REDEFINE_SYM + 1)
223 #define OPTION_SREC_FORCES3 (OPTION_SREC_LEN + 1)
224 #define OPTION_STRIP_SYMBOLS (OPTION_SREC_FORCES3 + 1)
225 #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1)
226 #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1)
227 #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1)
228 #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1)
230 /* Options to handle if running as "strip". */
232 static struct option strip_options
[] =
234 {"discard-all", no_argument
, 0, 'x'},
235 {"discard-locals", no_argument
, 0, 'X'},
236 {"format", required_argument
, 0, 'F'}, /* Obsolete */
237 {"help", no_argument
, 0, 'h'},
238 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
239 {"input-target", required_argument
, 0, 'I'},
240 {"keep-symbol", required_argument
, 0, 'K'},
241 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
242 {"output-target", required_argument
, 0, 'O'},
243 {"preserve-dates", no_argument
, 0, 'p'},
244 {"remove-section", required_argument
, 0, 'R'},
245 {"strip-all", no_argument
, 0, 's'},
246 {"strip-debug", no_argument
, 0, 'S'},
247 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
248 {"strip-symbol", required_argument
, 0, 'N'},
249 {"target", required_argument
, 0, 'F'},
250 {"verbose", no_argument
, 0, 'v'},
251 {"version", no_argument
, 0, 'V'},
252 {0, no_argument
, 0, 0}
255 /* Options to handle if running as "objcopy". */
257 static struct option copy_options
[] =
259 {"add-section", required_argument
, 0, OPTION_ADD_SECTION
},
260 {"adjust-start", required_argument
, 0, OPTION_CHANGE_START
},
261 {"adjust-vma", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
262 {"adjust-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
263 {"adjust-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
264 {"binary-architecture", required_argument
, 0, 'B'},
265 {"byte", required_argument
, 0, 'b'},
266 {"change-addresses", required_argument
, 0, OPTION_CHANGE_ADDRESSES
},
267 {"change-leading-char", no_argument
, 0, OPTION_CHANGE_LEADING_CHAR
},
268 {"change-section-address", required_argument
, 0, OPTION_CHANGE_SECTION_ADDRESS
},
269 {"change-section-lma", required_argument
, 0, OPTION_CHANGE_SECTION_LMA
},
270 {"change-section-vma", required_argument
, 0, OPTION_CHANGE_SECTION_VMA
},
271 {"change-start", required_argument
, 0, OPTION_CHANGE_START
},
272 {"change-warnings", no_argument
, 0, OPTION_CHANGE_WARNINGS
},
273 {"debugging", no_argument
, 0, OPTION_DEBUGGING
},
274 {"discard-all", no_argument
, 0, 'x'},
275 {"discard-locals", no_argument
, 0, 'X'},
276 {"only-section", required_argument
, 0, 'j'},
277 {"format", required_argument
, 0, 'F'}, /* Obsolete */
278 {"gap-fill", required_argument
, 0, OPTION_GAP_FILL
},
279 {"help", no_argument
, 0, 'h'},
280 {"input-format", required_argument
, 0, 'I'}, /* Obsolete */
281 {"input-target", required_argument
, 0, 'I'},
282 {"interleave", required_argument
, 0, 'i'},
283 {"keep-symbol", required_argument
, 0, 'K'},
284 {"no-adjust-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
285 {"no-change-warnings", no_argument
, 0, OPTION_NO_CHANGE_WARNINGS
},
286 {"output-format", required_argument
, 0, 'O'}, /* Obsolete */
287 {"output-target", required_argument
, 0, 'O'},
288 {"pad-to", required_argument
, 0, OPTION_PAD_TO
},
289 {"preserve-dates", no_argument
, 0, 'p'},
290 {"localize-symbol", required_argument
, 0, 'L'},
291 {"keep-global-symbol", required_argument
, 0, 'G'},
292 {"remove-leading-char", no_argument
, 0, OPTION_REMOVE_LEADING_CHAR
},
293 {"remove-section", required_argument
, 0, 'R'},
294 {"set-section-flags", required_argument
, 0, OPTION_SET_SECTION_FLAGS
},
295 {"set-start", required_argument
, 0, OPTION_SET_START
},
296 {"strip-all", no_argument
, 0, 'S'},
297 {"strip-debug", no_argument
, 0, 'g'},
298 {"strip-unneeded", no_argument
, 0, OPTION_STRIP_UNNEEDED
},
299 {"strip-symbol", required_argument
, 0, 'N'},
300 {"target", required_argument
, 0, 'F'},
301 {"verbose", no_argument
, 0, 'v'},
302 {"version", no_argument
, 0, 'V'},
303 {"weaken", no_argument
, 0, OPTION_WEAKEN
},
304 {"weaken-symbol", required_argument
, 0, 'W'},
305 {"redefine-sym", required_argument
, 0, OPTION_REDEFINE_SYM
},
306 {"srec-len", required_argument
, 0, OPTION_SREC_LEN
},
307 {"srec-forceS3", no_argument
, 0, OPTION_SREC_FORCES3
},
308 {"keep-symbols", required_argument
, 0, OPTION_KEEP_SYMBOLS
},
309 {"strip-symbols", required_argument
, 0, OPTION_STRIP_SYMBOLS
},
310 {"keep-global-symbols", required_argument
, 0, OPTION_KEEPGLOBAL_SYMBOLS
},
311 {"localize-symbols", required_argument
, 0, OPTION_LOCALIZE_SYMBOLS
},
312 {"weaken-symbols", required_argument
, 0, OPTION_WEAKEN_SYMBOLS
},
313 {0, no_argument
, 0, 0}
317 extern char *program_name
;
319 /* This flag distinguishes between strip and objcopy:
320 1 means this is 'strip'; 0 means this is 'objcopy'.
321 -1 means if we should use argv[0] to decide. */
324 /* The maximum length of an S record. This variable is declared in srec.c
325 and can be modified by the --srec-len parameter. */
326 extern unsigned int Chunk
;
328 /* Restrict the generation of Srecords to type S3 only.
329 This variable is declare in bfd/srec.c and can be toggled
330 on by the --srec-forceS3 command line switch. */
331 extern boolean S3Forced
;
333 /* Defined in bfd/binary.c. Used to set architecture of input binary files. */
334 extern enum bfd_architecture bfd_external_binary_architecture
;
337 copy_usage (stream
, exit_status
)
341 fprintf (stream
, _("Usage: %s <switches> in-file [out-file]\n"), program_name
);
342 fprintf (stream
, _(" The switches are:\n"));
343 fprintf (stream
, _("\
344 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
345 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
346 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
347 -F --target <bfdname> Set both input and output format to <bfdname>\n\
348 --debugging Convert debugging information, if possible\n\
349 -p --preserve-dates Copy modified/access timestamps to the output\n\
350 -j --only-section <name> Only copy section <name> into the output\n\
351 -R --remove-section <name> Remove section <name> from the output\n\
352 -S --strip-all Remove all symbol and relocation information\n\
353 -g --strip-debug Remove all debugging symbols\n\
354 --strip-unneeded Remove all symbols not needed by relocations\n\
355 -N --strip-symbol <name> Do not copy symbol <name>\n\
356 -K --keep-symbol <name> Only copy symbol <name>\n\
357 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
358 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
359 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
360 --weaken Force all global symbols to be marked as weak\n\
361 -x --discard-all Remove all non-global symbols\n\
362 -X --discard-locals Remove any compiler-generated symbols\n\
363 -i --interleave <number> Only copy one out of every <number> bytes\n\
364 -b --byte <num> Select byte <num> in every interleaved block\n\
365 --gap-fill <val> Fill gaps between sections with <val>\n\
366 --pad-to <addr> Pad the last section up to address <addr>\n\
367 --set-start <addr> Set the start address to <addr>\n\
368 {--change-start|--adjust-start} <incr>\n\
369 Add <incr> to the start address\n\
370 {--change-addresses|--adjust-vma} <incr>\n\
371 Add <incr> to LMA, VMA and start addresses\n\
372 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
373 Change LMA and VMA of section <name> by <val>\n\
374 --change-section-lma <name>{=|+|-}<val>\n\
375 Change the LMA of section <name> by <val>\n\
376 --change-section-vma <name>{=|+|-}<val>\n\
377 Change the VMA of section <name> by <val>\n\
378 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
379 Warn if a named section does not exist\n\
380 --set-section-flags <name>=<flags>\n\
381 Set section <name>'s properties to <flags>\n\
382 --add-section <name>=<file> Add section <name> found in <file> to output\n\
383 --change-leading-char Force output format's leading character style\n\
384 --remove-leading-char Remove leading character from global symbols\n\
385 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
386 --srec-len <number> Restrict the length of generated Srecords\n\
387 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
388 --strip-symbols <file> -N for all symbols listed in <file>\n\
389 --keep-symbols <file> -K for all symbols listed in <file>\n\
390 --localize-symbols <file> -L for all symbols listed in <file>\n\
391 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
392 --weaken-symbols <file> -W for all symbols listed in <file>\n\
393 -v --verbose List all object files modified\n\
394 -V --version Display this program's version number\n\
395 -h --help Display this output\n\
397 list_supported_targets (program_name
, stream
);
398 if (exit_status
== 0)
399 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
404 strip_usage (stream
, exit_status
)
408 fprintf (stream
, _("Usage: %s <switches> in-file(s)\n"), program_name
);
409 fprintf (stream
, _(" The switches are:\n"));
410 fprintf (stream
, _("\
411 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
412 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
413 -F --target <bfdname> Set both input and output format to <bfdname>\n\
414 -p --preserve-dates Copy modified/access timestamps to the output\n\
415 -R --remove-section <name> Remove section <name> from the output\n\
416 -s --strip-all Remove all symbol and relocation information\n\
417 -g -S --strip-debug Remove all debugging symbols\n\
418 --strip-unneeded Remove all symbols not needed by relocations\n\
419 -N --strip-symbol <name> Do not copy symbol <name>\n\
420 -K --keep-symbol <name> Only copy symbol <name>\n\
421 -x --discard-all Remove all non-global symbols\n\
422 -X --discard-locals Remove any compiler-generated symbols\n\
423 -v --verbose List all object files modified\n\
424 -V --version Display this program's version number\n\
425 -h --help Display this output\n\
426 -o <file> Place stripped output into <file>\n\
429 list_supported_targets (program_name
, stream
);
430 if (exit_status
== 0)
431 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
435 /* Parse section flags into a flagword, with a fatal error if the
436 string can't be parsed. */
450 snext
= strchr (s
, ',');
460 #define PARSE_FLAG(fname,fval) \
461 else if (strncasecmp (fname, s, len) == 0) ret |= fval
462 PARSE_FLAG ("alloc", SEC_ALLOC
);
463 PARSE_FLAG ("load", SEC_LOAD
);
464 PARSE_FLAG ("noload", SEC_NEVER_LOAD
);
465 PARSE_FLAG ("readonly", SEC_READONLY
);
466 PARSE_FLAG ("debug", SEC_DEBUGGING
);
467 PARSE_FLAG ("code", SEC_CODE
);
468 PARSE_FLAG ("data", SEC_DATA
);
469 PARSE_FLAG ("rom", SEC_ROM
);
470 PARSE_FLAG ("share", SEC_SHARED
);
471 PARSE_FLAG ("contents", SEC_HAS_CONTENTS
);
477 copy
= xmalloc (len
+ 1);
478 strncpy (copy
, s
, len
);
480 non_fatal (_("unrecognized section flag `%s'"), copy
);
481 fatal (_("supported flags: %s"),
482 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
492 /* Find and optionally add an entry in the change_sections list. */
494 static struct section_list
*
495 find_section_list (name
, add
)
499 register struct section_list
*p
;
501 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
502 if (strcmp (p
->name
, name
) == 0)
508 p
= (struct section_list
*) xmalloc (sizeof (struct section_list
));
513 p
->change_vma
= CHANGE_IGNORE
;
514 p
->change_lma
= CHANGE_IGNORE
;
517 p
->set_flags
= false;
520 p
->next
= change_sections
;
526 /* Add a symbol to strip_specific_list. */
529 add_specific_symbol (name
, list
)
531 struct symlist
**list
;
533 struct symlist
*tmp_list
;
535 tmp_list
= (struct symlist
*) xmalloc (sizeof (struct symlist
));
536 tmp_list
->name
= name
;
537 tmp_list
->next
= *list
;
541 /* Add symbols listed in `filename' to strip_specific_list. */
543 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
544 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
547 add_specific_symbols (filename
, list
)
548 const char *filename
;
549 struct symlist
**list
;
555 unsigned int line_count
;
557 if (stat (filename
, & st
) < 0)
558 fatal (_("cannot stat: %s: %s"), filename
, strerror (errno
));
562 buffer
= (char *) xmalloc (st
.st_size
+ 2);
563 f
= fopen (filename
, FOPEN_RT
);
565 fatal (_("cannot open: %s: %s"), filename
, strerror (errno
));
567 if (fread (buffer
, 1, st
.st_size
, f
) == 0 || ferror (f
))
568 fatal (_("%s: fread failed"), filename
);
571 buffer
[st
.st_size
] = '\n';
572 buffer
[st
.st_size
+ 1] = '\0';
576 for (line
= buffer
; * line
!= '\0'; line
++)
581 int finished
= false;
583 for (eol
= line
;; eol
++)
589 /* Cope with \n\r. */
597 /* Cope with \r\n. */
608 /* Line comment, Terminate the line here, in case a
609 name is present and then allow the rest of the
610 loop to find the real end of the line. */
622 /* A name may now exist somewhere between 'line' and 'eol'.
623 Strip off leading whitespace and trailing whitespace,
624 then add it to the list. */
625 for (name
= line
; IS_WHITESPACE (* name
); name
++)
627 for (name_end
= name
;
628 (! IS_WHITESPACE (* name_end
))
629 && (! IS_LINE_TERMINATOR (* name_end
));
633 if (! IS_LINE_TERMINATOR (* name_end
))
637 for (extra
= name_end
+ 1; IS_WHITESPACE (* extra
); extra
++)
640 if (! IS_LINE_TERMINATOR (* extra
))
641 non_fatal (_("Ignoring rubbish found on line %d of %s"),
642 line_count
, filename
);
648 add_specific_symbol (name
, list
);
650 /* Advance line pointer to end of line. The 'eol ++' in the for
651 loop above will then advance us to the start of the next line. */
657 /* See whether a symbol should be stripped or kept based on
658 strip_specific_list and keep_symbols. */
661 is_specified_symbol (name
, list
)
663 struct symlist
*list
;
665 struct symlist
*tmp_list
;
667 for (tmp_list
= list
; tmp_list
; tmp_list
= tmp_list
->next
)
669 if (strcmp (name
, tmp_list
->name
) == 0)
675 /* See if a section is being removed. */
678 is_strip_section (abfd
, sec
)
679 bfd
*abfd ATTRIBUTE_UNUSED
;
682 struct section_list
*p
;
684 if ((bfd_get_section_flags (abfd
, sec
) & SEC_DEBUGGING
) != 0
685 && (strip_symbols
== STRIP_DEBUG
686 || strip_symbols
== STRIP_UNNEEDED
687 || strip_symbols
== STRIP_ALL
688 || discard_locals
== LOCALS_ALL
689 || convert_debugging
))
692 if (! sections_removed
&& ! sections_copied
)
695 p
= find_section_list (bfd_get_section_name (abfd
, sec
), false);
696 if (sections_removed
&& p
!= NULL
&& p
->remove
)
698 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
703 /* Choose which symbol entries to copy; put the result in OSYMS.
704 We don't copy in place, because that confuses the relocs.
705 Return the number of symbols to print. */
708 filter_symbols (abfd
, obfd
, osyms
, isyms
, symcount
)
711 asymbol
**osyms
, **isyms
;
714 register asymbol
**from
= isyms
, **to
= osyms
;
715 long src_count
= 0, dst_count
= 0;
716 int relocatable
= (abfd
->flags
& (HAS_RELOC
| EXEC_P
| DYNAMIC
))
719 for (; src_count
< symcount
; src_count
++)
721 asymbol
*sym
= from
[src_count
];
722 flagword flags
= sym
->flags
;
723 const char *name
= bfd_asymbol_name (sym
);
726 if (redefine_sym_list
)
728 const char *old_name
, *new_name
;
730 old_name
= bfd_asymbol_name (sym
);
731 new_name
= lookup_sym_redefinition (old_name
);
732 name
= bfd_asymbol_name (sym
) = new_name
;
735 if (change_leading_char
736 && (bfd_get_symbol_leading_char (abfd
)
737 != bfd_get_symbol_leading_char (obfd
))
738 && (bfd_get_symbol_leading_char (abfd
) == '\0'
739 || (name
[0] == bfd_get_symbol_leading_char (abfd
))))
741 if (bfd_get_symbol_leading_char (obfd
) == '\0')
742 name
= bfd_asymbol_name (sym
) = name
+ 1;
747 n
= xmalloc (strlen (name
) + 2);
748 n
[0] = bfd_get_symbol_leading_char (obfd
);
749 if (bfd_get_symbol_leading_char (abfd
) == '\0')
750 strcpy (n
+ 1, name
);
752 strcpy (n
+ 1, name
+ 1);
753 name
= bfd_asymbol_name (sym
) = n
;
757 if (remove_leading_char
758 && ((flags
& BSF_GLOBAL
) != 0
759 || (flags
& BSF_WEAK
) != 0
760 || bfd_is_und_section (bfd_get_section (sym
))
761 || bfd_is_com_section (bfd_get_section (sym
)))
762 && name
[0] == bfd_get_symbol_leading_char (abfd
))
763 name
= bfd_asymbol_name (sym
) = name
+ 1;
765 if (strip_symbols
== STRIP_ALL
)
767 else if ((flags
& BSF_KEEP
) != 0 /* Used in relocation. */
768 || ((flags
& BSF_SECTION_SYM
) != 0
769 && ((*bfd_get_section (sym
)->symbol_ptr_ptr
)->flags
772 else if (relocatable
/* Relocatable file. */
773 && (flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0)
775 else if (bfd_decode_symclass (sym
) == 'I')
776 /* Global symbols in $idata sections need to be retained
777 even if relocatable is false. External users of the
778 library containing the $idata section may reference these
781 else if ((flags
& BSF_GLOBAL
) != 0 /* Global symbol. */
782 || (flags
& BSF_WEAK
) != 0
783 || bfd_is_und_section (bfd_get_section (sym
))
784 || bfd_is_com_section (bfd_get_section (sym
)))
785 keep
= strip_symbols
!= STRIP_UNNEEDED
;
786 else if ((flags
& BSF_DEBUGGING
) != 0) /* Debugging symbol. */
787 keep
= (strip_symbols
!= STRIP_DEBUG
788 && strip_symbols
!= STRIP_UNNEEDED
789 && ! convert_debugging
);
790 else /* Local symbol. */
791 keep
= (strip_symbols
!= STRIP_UNNEEDED
792 && (discard_locals
!= LOCALS_ALL
793 && (discard_locals
!= LOCALS_START_L
794 || ! bfd_is_local_label (abfd
, sym
))));
796 if (keep
&& is_specified_symbol (name
, strip_specific_list
))
798 if (!keep
&& is_specified_symbol (name
, keep_specific_list
))
800 if (keep
&& is_strip_section (abfd
, bfd_get_section (sym
)))
803 if (keep
&& (flags
& BSF_GLOBAL
) != 0
804 && (weaken
|| is_specified_symbol (name
, weaken_specific_list
)))
806 sym
->flags
&=~ BSF_GLOBAL
;
807 sym
->flags
|= BSF_WEAK
;
809 if (keep
&& (flags
& (BSF_GLOBAL
| BSF_WEAK
))
810 && (is_specified_symbol (name
, localize_specific_list
)
811 || (keepglobal_specific_list
!= NULL
812 && ! is_specified_symbol (name
, keepglobal_specific_list
))))
814 sym
->flags
&= ~(BSF_GLOBAL
| BSF_WEAK
);
815 sym
->flags
|= BSF_LOCAL
;
819 to
[dst_count
++] = sym
;
822 to
[dst_count
] = NULL
;
828 lookup_sym_redefinition (source
)
832 struct redefine_node
*list
;
836 for (list
= redefine_sym_list
; list
!= NULL
; list
= list
->next
)
838 if (strcmp (source
, list
->source
) == 0)
840 result
= list
->target
;
847 /* Add a node to a symbol redefine list */
850 redefine_list_append (source
, target
)
854 struct redefine_node
**p
;
855 struct redefine_node
*list
;
856 struct redefine_node
*new_node
;
858 for (p
= &redefine_sym_list
; (list
= *p
) != NULL
; p
= &list
->next
)
860 if (strcmp (source
, list
->source
) == 0)
862 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
867 if (strcmp (target
, list
->target
) == 0)
869 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
875 new_node
= (struct redefine_node
*) xmalloc (sizeof (struct redefine_node
));
877 new_node
->source
= strdup (source
);
878 new_node
->target
= strdup (target
);
879 new_node
->next
= NULL
;
885 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
889 filter_bytes (memhunk
, size
)
893 char *from
= memhunk
+ copy_byte
, *to
= memhunk
, *end
= memhunk
+ *size
;
895 for (; from
< end
; from
+= interleave
)
897 if (*size
% interleave
> (bfd_size_type
) copy_byte
)
898 *size
= (*size
/ interleave
) + 1;
903 /* Copy object file IBFD onto OBFD. */
906 copy_object (ibfd
, obfd
)
912 asection
**osections
= NULL
;
913 bfd_size_type
*gaps
= NULL
;
914 bfd_size_type max_gap
= 0;
918 if (ibfd
->xvec
->byteorder
!= obfd
->xvec
->byteorder
919 && ibfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
920 && obfd
->xvec
->byteorder
!= BFD_ENDIAN_UNKNOWN
)
922 fatal (_("Unable to change endianness of input file(s)"));
926 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
927 RETURN_NONFATAL (bfd_get_filename (obfd
));
930 printf (_("copy from %s(%s) to %s(%s)\n"),
931 bfd_get_filename (ibfd
), bfd_get_target (ibfd
),
932 bfd_get_filename (obfd
), bfd_get_target (obfd
));
937 start
= bfd_get_start_address (ibfd
);
938 start
+= change_start
;
940 if (!bfd_set_start_address (obfd
, start
)
941 || !bfd_set_file_flags (obfd
,
942 (bfd_get_file_flags (ibfd
)
943 & bfd_applicable_file_flags (obfd
))))
944 RETURN_NONFATAL (bfd_get_filename (ibfd
));
946 /* Copy architecture of input file to output file */
947 if (!bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
948 bfd_get_mach (ibfd
)))
949 non_fatal (_("Warning: Output file cannot represent architecture %s"),
950 bfd_printable_arch_mach (bfd_get_arch (ibfd
),
951 bfd_get_mach (ibfd
)));
953 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
954 RETURN_NONFATAL (bfd_get_filename (ibfd
));
959 if (osympp
!= isympp
)
962 /* BFD mandates that all output sections be created and sizes set before
963 any output is done. Thus, we traverse all sections multiple times. */
964 bfd_map_over_sections (ibfd
, setup_section
, (void *) obfd
);
966 if (add_sections
!= NULL
)
968 struct section_add
*padd
;
969 struct section_list
*pset
;
971 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
973 padd
->section
= bfd_make_section (obfd
, padd
->name
);
974 if (padd
->section
== NULL
)
976 non_fatal (_("can't create section `%s': %s"),
977 padd
->name
, bfd_errmsg (bfd_get_error ()));
985 if (! bfd_set_section_size (obfd
, padd
->section
, padd
->size
))
986 RETURN_NONFATAL (bfd_get_filename (obfd
));
988 pset
= find_section_list (padd
->name
, false);
992 if (pset
!= NULL
&& pset
->set_flags
)
993 flags
= pset
->flags
| SEC_HAS_CONTENTS
;
995 flags
= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DATA
;
997 if (! bfd_set_section_flags (obfd
, padd
->section
, flags
))
998 RETURN_NONFATAL (bfd_get_filename (obfd
));
1002 if (pset
->change_vma
!= CHANGE_IGNORE
)
1003 if (! bfd_set_section_vma (obfd
, padd
->section
, pset
->vma_val
))
1004 RETURN_NONFATAL (bfd_get_filename (obfd
));
1006 if (pset
->change_lma
!= CHANGE_IGNORE
)
1008 padd
->section
->lma
= pset
->lma_val
;
1010 if (! bfd_set_section_alignment
1011 (obfd
, padd
->section
,
1012 bfd_section_alignment (obfd
, padd
->section
)))
1013 RETURN_NONFATAL (bfd_get_filename (obfd
));
1020 if (gap_fill_set
|| pad_to_set
)
1025 /* We must fill in gaps between the sections and/or we must pad
1026 the last section to a specified address. We do this by
1027 grabbing a list of the sections, sorting them by VMA, and
1028 increasing the section sizes as required to fill the gaps.
1029 We write out the gap contents below. */
1031 c
= bfd_count_sections (obfd
);
1032 osections
= (asection
**) xmalloc (c
* sizeof (asection
*));
1034 bfd_map_over_sections (obfd
, get_sections
, (void *) &set
);
1036 qsort (osections
, c
, sizeof (asection
*), compare_section_lma
);
1038 gaps
= (bfd_size_type
*) xmalloc (c
* sizeof (bfd_size_type
));
1039 memset (gaps
, 0, c
* sizeof (bfd_size_type
));
1043 for (i
= 0; i
< c
- 1; i
++)
1047 bfd_vma gap_start
, gap_stop
;
1049 flags
= bfd_get_section_flags (obfd
, osections
[i
]);
1050 if ((flags
& SEC_HAS_CONTENTS
) == 0
1051 || (flags
& SEC_LOAD
) == 0)
1054 size
= bfd_section_size (obfd
, osections
[i
]);
1055 gap_start
= bfd_section_lma (obfd
, osections
[i
]) + size
;
1056 gap_stop
= bfd_section_lma (obfd
, osections
[i
+ 1]);
1057 if (gap_start
< gap_stop
)
1059 if (! bfd_set_section_size (obfd
, osections
[i
],
1060 size
+ (gap_stop
- gap_start
)))
1062 non_fatal (_("Can't fill gap after %s: %s"),
1063 bfd_get_section_name (obfd
, osections
[i
]),
1064 bfd_errmsg (bfd_get_error ()));
1068 gaps
[i
] = gap_stop
- gap_start
;
1069 if (max_gap
< gap_stop
- gap_start
)
1070 max_gap
= gap_stop
- gap_start
;
1080 lma
= bfd_section_lma (obfd
, osections
[c
- 1]);
1081 size
= bfd_section_size (obfd
, osections
[c
- 1]);
1082 if (lma
+ size
< pad_to
)
1084 if (! bfd_set_section_size (obfd
, osections
[c
- 1],
1087 non_fatal (_("Can't add padding to %s: %s"),
1088 bfd_get_section_name (obfd
, osections
[c
- 1]),
1089 bfd_errmsg (bfd_get_error ()));
1094 gaps
[c
- 1] = pad_to
- (lma
+ size
);
1095 if (max_gap
< pad_to
- (lma
+ size
))
1096 max_gap
= pad_to
- (lma
+ size
);
1102 /* Symbol filtering must happen after the output sections have
1103 been created, but before their contents are set. */
1105 symsize
= bfd_get_symtab_upper_bound (ibfd
);
1107 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1109 osympp
= isympp
= (asymbol
**) xmalloc (symsize
);
1110 symcount
= bfd_canonicalize_symtab (ibfd
, isympp
);
1112 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1114 if (convert_debugging
)
1115 dhandle
= read_debugging_info (ibfd
, isympp
, symcount
);
1117 if (strip_symbols
== STRIP_DEBUG
1118 || strip_symbols
== STRIP_ALL
1119 || strip_symbols
== STRIP_UNNEEDED
1120 || discard_locals
!= LOCALS_UNDEF
1121 || strip_specific_list
!= NULL
1122 || keep_specific_list
!= NULL
1123 || localize_specific_list
!= NULL
1124 || keepglobal_specific_list
!= NULL
1125 || weaken_specific_list
!= NULL
1128 || convert_debugging
1129 || change_leading_char
1130 || remove_leading_char
1131 || redefine_sym_list
1134 /* Mark symbols used in output relocations so that they
1135 are kept, even if they are local labels or static symbols.
1137 Note we iterate over the input sections examining their
1138 relocations since the relocations for the output sections
1139 haven't been set yet. mark_symbols_used_in_relocations will
1140 ignore input sections which have no corresponding output
1142 if (strip_symbols
!= STRIP_ALL
)
1143 bfd_map_over_sections (ibfd
,
1144 mark_symbols_used_in_relocations
,
1146 osympp
= (asymbol
**) xmalloc ((symcount
+ 1) * sizeof (asymbol
*));
1147 symcount
= filter_symbols (ibfd
, obfd
, osympp
, isympp
, symcount
);
1150 if (convert_debugging
&& dhandle
!= NULL
)
1152 if (! write_debugging_info (obfd
, dhandle
, &symcount
, &osympp
))
1159 bfd_set_symtab (obfd
, osympp
, symcount
);
1161 /* This has to happen after the symbol table has been set. */
1162 bfd_map_over_sections (ibfd
, copy_section
, (void *) obfd
);
1164 if (add_sections
!= NULL
)
1166 struct section_add
*padd
;
1168 for (padd
= add_sections
; padd
!= NULL
; padd
= padd
->next
)
1170 if (! bfd_set_section_contents (obfd
, padd
->section
,
1171 (PTR
) padd
->contents
,
1173 (bfd_size_type
) padd
->size
))
1174 RETURN_NONFATAL (bfd_get_filename (obfd
));
1178 if (gap_fill_set
|| pad_to_set
)
1183 /* Fill in the gaps. */
1187 buf
= (bfd_byte
*) xmalloc (max_gap
);
1188 memset (buf
, gap_fill
, (size_t) max_gap
);
1190 c
= bfd_count_sections (obfd
);
1191 for (i
= 0; i
< c
; i
++)
1199 off
= bfd_section_size (obfd
, osections
[i
]) - left
;
1209 if (! bfd_set_section_contents (obfd
, osections
[i
], buf
,
1211 RETURN_NONFATAL (bfd_get_filename (obfd
));
1220 /* Allow the BFD backend to copy any private data it understands
1221 from the input BFD to the output BFD. This is done last to
1222 permit the routine to look at the filtered symbol table, which is
1223 important for the ECOFF code at least. */
1224 if (!bfd_copy_private_bfd_data (ibfd
, obfd
))
1226 non_fatal (_("%s: error copying private BFD data: %s"),
1227 bfd_get_filename (obfd
),
1228 bfd_errmsg (bfd_get_error ()));
1234 /* Read each archive element in turn from IBFD, copy the
1235 contents to temp file, and keep the temp file handle. */
1238 copy_archive (ibfd
, obfd
, output_target
)
1241 const char *output_target
;
1245 struct name_list
*next
;
1249 bfd
**ptr
= &obfd
->archive_head
;
1251 char *dir
= make_tempname (bfd_get_filename (obfd
));
1253 /* Make a temp directory to hold the contents. */
1254 #if defined (_WIN32) && !defined (__CYGWIN32__)
1255 if (mkdir (dir
) != 0)
1257 if (mkdir (dir
, 0700) != 0)
1260 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1261 dir
, strerror (errno
));
1263 obfd
->has_armap
= ibfd
->has_armap
;
1267 this_element
= bfd_openr_next_archived_file (ibfd
, NULL
);
1268 while (!status
&& this_element
!= (bfd
*) NULL
)
1270 /* Create an output file for this member. */
1271 char *output_name
= concat (dir
, "/", bfd_get_filename (this_element
),
1273 bfd
*output_bfd
= bfd_openw (output_name
, output_target
);
1276 int stat_status
= 0;
1280 stat_status
= bfd_stat_arch_elt (this_element
, &buf
);
1281 if (stat_status
!= 0)
1282 non_fatal (_("internal stat error on %s"),
1283 bfd_get_filename (this_element
));
1286 l
= (struct name_list
*) xmalloc (sizeof (struct name_list
));
1287 l
->name
= output_name
;
1291 if (output_bfd
== (bfd
*) NULL
)
1292 RETURN_NONFATAL (output_name
);
1294 if (!bfd_set_format (obfd
, bfd_get_format (ibfd
)))
1295 RETURN_NONFATAL (bfd_get_filename (obfd
));
1297 if (bfd_check_format (this_element
, bfd_object
) == true)
1298 copy_object (this_element
, output_bfd
);
1300 if (!bfd_close (output_bfd
))
1302 bfd_nonfatal (bfd_get_filename (output_bfd
));
1303 /* Error in new object file. Don't change archive. */
1307 if (preserve_dates
&& stat_status
== 0)
1308 set_times (output_name
, &buf
);
1310 /* Open the newly output file and attach to our list. */
1311 output_bfd
= bfd_openr (output_name
, output_target
);
1313 l
->obfd
= output_bfd
;
1316 ptr
= &output_bfd
->next
;
1318 last_element
= this_element
;
1320 this_element
= bfd_openr_next_archived_file (ibfd
, last_element
);
1322 bfd_close (last_element
);
1324 *ptr
= (bfd
*) NULL
;
1326 if (!bfd_close (obfd
))
1327 RETURN_NONFATAL (bfd_get_filename (obfd
));
1329 if (!bfd_close (ibfd
))
1330 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1332 /* Delete all the files that we opened. */
1333 for (l
= list
; l
!= NULL
; l
= l
->next
)
1335 bfd_close (l
->obfd
);
1341 /* The top-level control. */
1344 copy_file (input_filename
, output_filename
, input_target
, output_target
)
1345 const char *input_filename
;
1346 const char *output_filename
;
1347 const char *input_target
;
1348 const char *output_target
;
1353 /* To allow us to do "strip *" without dying on the first
1354 non-object file, failures are nonfatal. */
1356 ibfd
= bfd_openr (input_filename
, input_target
);
1358 RETURN_NONFATAL (input_filename
);
1360 if (bfd_check_format (ibfd
, bfd_archive
))
1364 /* bfd_get_target does not return the correct value until
1365 bfd_check_format succeeds. */
1366 if (output_target
== NULL
)
1367 output_target
= bfd_get_target (ibfd
);
1369 obfd
= bfd_openw (output_filename
, output_target
);
1371 RETURN_NONFATAL (output_filename
);
1373 copy_archive (ibfd
, obfd
, output_target
);
1375 else if (bfd_check_format_matches (ibfd
, bfd_object
, &matching
))
1379 /* bfd_get_target does not return the correct value until
1380 bfd_check_format succeeds. */
1381 if (output_target
== NULL
)
1382 output_target
= bfd_get_target (ibfd
);
1384 obfd
= bfd_openw (output_filename
, output_target
);
1386 RETURN_NONFATAL (output_filename
);
1388 copy_object (ibfd
, obfd
);
1390 if (!bfd_close (obfd
))
1391 RETURN_NONFATAL (output_filename
);
1393 if (!bfd_close (ibfd
))
1394 RETURN_NONFATAL (input_filename
);
1398 bfd_nonfatal (input_filename
);
1400 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1402 list_matching_formats (matching
);
1410 /* Create a section in OBFD with the same name and attributes
1411 as ISECTION in IBFD. */
1414 setup_section (ibfd
, isection
, obfdarg
)
1419 bfd
*obfd
= (bfd
*) obfdarg
;
1420 struct section_list
*p
;
1428 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1429 && (strip_symbols
== STRIP_DEBUG
1430 || strip_symbols
== STRIP_UNNEEDED
1431 || strip_symbols
== STRIP_ALL
1432 || discard_locals
== LOCALS_ALL
1433 || convert_debugging
))
1436 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1440 if (sections_removed
&& p
!= NULL
&& p
->remove
)
1442 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
1445 osection
= bfd_make_section_anyway (obfd
, bfd_section_name (ibfd
, isection
));
1447 if (osection
== NULL
)
1453 size
= bfd_section_size (ibfd
, isection
);
1455 size
= (size
+ interleave
- 1) / interleave
;
1456 if (! bfd_set_section_size (obfd
, osection
, size
))
1462 vma
= bfd_section_vma (ibfd
, isection
);
1463 if (p
!= NULL
&& p
->change_vma
== CHANGE_MODIFY
)
1465 else if (p
!= NULL
&& p
->change_vma
== CHANGE_SET
)
1468 vma
+= change_section_address
;
1470 if (! bfd_set_section_vma (obfd
, osection
, vma
))
1476 lma
= isection
->lma
;
1477 if ((p
!= NULL
) && p
->change_lma
!= CHANGE_IGNORE
)
1479 if (p
->change_lma
== CHANGE_MODIFY
)
1481 else if (p
->change_lma
== CHANGE_SET
)
1487 lma
+= change_section_address
;
1489 osection
->lma
= lma
;
1491 /* FIXME: This is probably not enough. If we change the LMA we
1492 may have to recompute the header for the file as well. */
1493 if (bfd_set_section_alignment (obfd
,
1495 bfd_section_alignment (ibfd
, isection
))
1498 err
= _("alignment");
1502 flags
= bfd_get_section_flags (ibfd
, isection
);
1503 if (p
!= NULL
&& p
->set_flags
)
1504 flags
= p
->flags
| (flags
& SEC_HAS_CONTENTS
);
1505 if (!bfd_set_section_flags (obfd
, osection
, flags
))
1511 /* This used to be mangle_section; we do here to avoid using
1512 bfd_get_section_by_name since some formats allow multiple
1513 sections with the same name. */
1514 isection
->output_section
= osection
;
1515 isection
->output_offset
= 0;
1517 /* Allow the BFD backend to copy any private data it understands
1518 from the input section to the output section. */
1519 if (!bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
))
1521 err
= _("private data");
1529 non_fatal (_("%s: section `%s': error in %s: %s"),
1530 bfd_get_filename (ibfd
),
1531 bfd_section_name (ibfd
, isection
),
1532 err
, bfd_errmsg (bfd_get_error ()));
1536 /* Copy the data of input section ISECTION of IBFD
1537 to an output section with the same name in OBFD.
1538 If stripping then don't copy any relocation info. */
1541 copy_section (ibfd
, isection
, obfdarg
)
1546 bfd
*obfd
= (bfd
*) obfdarg
;
1547 struct section_list
*p
;
1554 /* If we have already failed earlier on, do not keep on generating
1559 if ((bfd_get_section_flags (ibfd
, isection
) & SEC_DEBUGGING
) != 0
1560 && (strip_symbols
== STRIP_DEBUG
1561 || strip_symbols
== STRIP_UNNEEDED
1562 || strip_symbols
== STRIP_ALL
1563 || discard_locals
== LOCALS_ALL
1564 || convert_debugging
))
1569 p
= find_section_list (bfd_section_name (ibfd
, isection
), false);
1571 if (sections_removed
&& p
!= NULL
&& p
->remove
)
1573 if (sections_copied
&& (p
== NULL
|| ! p
->copy
))
1576 osection
= isection
->output_section
;
1577 size
= bfd_get_section_size_before_reloc (isection
);
1579 if (size
== 0 || osection
== 0)
1583 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1585 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1588 bfd_set_reloc (obfd
, osection
, (arelent
**) NULL
, 0);
1591 relpp
= (arelent
**) xmalloc (relsize
);
1592 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, isympp
);
1594 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1596 if (strip_symbols
== STRIP_ALL
)
1598 /* Remove relocations which are not in
1599 keep_strip_specific_list. */
1600 arelent
**temp_relpp
;
1601 long temp_relcount
= 0;
1604 temp_relpp
= (arelent
**) xmalloc (relsize
);
1605 for (i
= 0; i
< relcount
; i
++)
1606 if (is_specified_symbol
1607 (bfd_asymbol_name (*relpp
[i
]->sym_ptr_ptr
),
1608 keep_specific_list
))
1609 temp_relpp
[temp_relcount
++] = relpp
[i
];
1610 relcount
= temp_relcount
;
1614 bfd_set_reloc (obfd
, osection
,
1615 (relcount
== 0 ? (arelent
**) NULL
: relpp
), relcount
);
1618 isection
->_cooked_size
= isection
->_raw_size
;
1619 isection
->reloc_done
= true;
1621 if (bfd_get_section_flags (ibfd
, isection
) & SEC_HAS_CONTENTS
)
1623 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1625 if (!bfd_get_section_contents (ibfd
, isection
, memhunk
, (file_ptr
) 0,
1627 RETURN_NONFATAL (bfd_get_filename (ibfd
));
1630 filter_bytes (memhunk
, &size
);
1632 if (!bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1634 RETURN_NONFATAL (bfd_get_filename (obfd
));
1638 else if (p
!= NULL
&& p
->set_flags
&& (p
->flags
& SEC_HAS_CONTENTS
) != 0)
1640 PTR memhunk
= (PTR
) xmalloc ((unsigned) size
);
1642 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1643 flag--they can just remove the section entirely and add it
1644 back again. However, we do permit them to turn on the
1645 SEC_HAS_CONTENTS flag, and take it to mean that the section
1646 contents should be zeroed out. */
1648 memset (memhunk
, 0, size
);
1649 if (! bfd_set_section_contents (obfd
, osection
, memhunk
, (file_ptr
) 0,
1651 RETURN_NONFATAL (bfd_get_filename (obfd
));
1656 /* Get all the sections. This is used when --gap-fill or --pad-to is
1660 get_sections (obfd
, osection
, secppparg
)
1661 bfd
*obfd ATTRIBUTE_UNUSED
;
1665 asection
***secppp
= (asection
***) secppparg
;
1667 **secppp
= osection
;
1671 /* Sort sections by VMA. This is called via qsort, and is used when
1672 --gap-fill or --pad-to is used. We force non loadable or empty
1673 sections to the front, where they are easier to ignore. */
1676 compare_section_lma (arg1
, arg2
)
1680 const asection
**sec1
= (const asection
**) arg1
;
1681 const asection
**sec2
= (const asection
**) arg2
;
1682 flagword flags1
, flags2
;
1684 /* Sort non loadable sections to the front. */
1685 flags1
= (*sec1
)->flags
;
1686 flags2
= (*sec2
)->flags
;
1687 if ((flags1
& SEC_HAS_CONTENTS
) == 0
1688 || (flags1
& SEC_LOAD
) == 0)
1690 if ((flags2
& SEC_HAS_CONTENTS
) != 0
1691 && (flags2
& SEC_LOAD
) != 0)
1696 if ((flags2
& SEC_HAS_CONTENTS
) == 0
1697 || (flags2
& SEC_LOAD
) == 0)
1701 /* Sort sections by LMA. */
1702 if ((*sec1
)->lma
> (*sec2
)->lma
)
1704 else if ((*sec1
)->lma
< (*sec2
)->lma
)
1707 /* Sort sections with the same LMA by size. */
1708 if ((*sec1
)->_raw_size
> (*sec2
)->_raw_size
)
1710 else if ((*sec1
)->_raw_size
< (*sec2
)->_raw_size
)
1716 /* Mark all the symbols which will be used in output relocations with
1717 the BSF_KEEP flag so that those symbols will not be stripped.
1719 Ignore relocations which will not appear in the output file. */
1722 mark_symbols_used_in_relocations (ibfd
, isection
, symbolsarg
)
1727 asymbol
**symbols
= (asymbol
**) symbolsarg
;
1732 /* Ignore an input section with no corresponding output section. */
1733 if (isection
->output_section
== NULL
)
1736 relsize
= bfd_get_reloc_upper_bound (ibfd
, isection
);
1738 bfd_fatal (bfd_get_filename (ibfd
));
1743 relpp
= (arelent
**) xmalloc (relsize
);
1744 relcount
= bfd_canonicalize_reloc (ibfd
, isection
, relpp
, symbols
);
1746 bfd_fatal (bfd_get_filename (ibfd
));
1748 /* Examine each symbol used in a relocation. If it's not one of the
1749 special bfd section symbols, then mark it with BSF_KEEP. */
1750 for (i
= 0; i
< relcount
; i
++)
1752 if (*relpp
[i
]->sym_ptr_ptr
!= bfd_com_section_ptr
->symbol
1753 && *relpp
[i
]->sym_ptr_ptr
!= bfd_abs_section_ptr
->symbol
1754 && *relpp
[i
]->sym_ptr_ptr
!= bfd_und_section_ptr
->symbol
)
1755 (*relpp
[i
]->sym_ptr_ptr
)->flags
|= BSF_KEEP
;
1762 /* Write out debugging information. */
1765 write_debugging_info (obfd
, dhandle
, symcountp
, symppp
)
1768 long *symcountp ATTRIBUTE_UNUSED
;
1769 asymbol
***symppp ATTRIBUTE_UNUSED
;
1771 if (bfd_get_flavour (obfd
) == bfd_target_ieee_flavour
)
1772 return write_ieee_debugging_info (obfd
, dhandle
);
1774 if (bfd_get_flavour (obfd
) == bfd_target_coff_flavour
1775 || bfd_get_flavour (obfd
) == bfd_target_elf_flavour
)
1777 bfd_byte
*syms
, *strings
;
1778 bfd_size_type symsize
, stringsize
;
1779 asection
*stabsec
, *stabstrsec
;
1781 if (! write_stabs_in_sections_debugging_info (obfd
, dhandle
, &syms
,
1786 stabsec
= bfd_make_section (obfd
, ".stab");
1787 stabstrsec
= bfd_make_section (obfd
, ".stabstr");
1789 || stabstrsec
== NULL
1790 || ! bfd_set_section_size (obfd
, stabsec
, symsize
)
1791 || ! bfd_set_section_size (obfd
, stabstrsec
, stringsize
)
1792 || ! bfd_set_section_alignment (obfd
, stabsec
, 2)
1793 || ! bfd_set_section_alignment (obfd
, stabstrsec
, 0)
1794 || ! bfd_set_section_flags (obfd
, stabsec
,
1798 || ! bfd_set_section_flags (obfd
, stabstrsec
,
1803 non_fatal (_("%s: can't create debugging section: %s"),
1804 bfd_get_filename (obfd
),
1805 bfd_errmsg (bfd_get_error ()));
1809 /* We can get away with setting the section contents now because
1810 the next thing the caller is going to do is copy over the
1811 real sections. We may someday have to split the contents
1812 setting out of this function. */
1813 if (! bfd_set_section_contents (obfd
, stabsec
, syms
, (file_ptr
) 0,
1815 || ! bfd_set_section_contents (obfd
, stabstrsec
, strings
,
1816 (file_ptr
) 0, stringsize
))
1818 non_fatal (_("%s: can't set debugging section contents: %s"),
1819 bfd_get_filename (obfd
),
1820 bfd_errmsg (bfd_get_error ()));
1827 non_fatal (_("%s: don't know how to write debugging information for %s"),
1828 bfd_get_filename (obfd
), bfd_get_target (obfd
));
1833 strip_main (argc
, argv
)
1837 char *input_target
= NULL
, *output_target
= NULL
;
1838 boolean show_version
= false;
1840 struct section_list
*p
;
1841 char *output_file
= NULL
;
1843 while ((c
= getopt_long (argc
, argv
, "b:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXVvW:",
1844 strip_options
, (int *) 0)) != EOF
)
1849 input_target
= optarg
;
1852 output_target
= optarg
;
1855 input_target
= output_target
= optarg
;
1858 p
= find_section_list (optarg
, true);
1860 sections_removed
= true;
1863 strip_symbols
= STRIP_ALL
;
1867 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
1868 strip_symbols
= STRIP_DEBUG
;
1870 case OPTION_STRIP_UNNEEDED
:
1871 strip_symbols
= STRIP_UNNEEDED
;
1874 add_specific_symbol (optarg
, &keep_specific_list
);
1877 add_specific_symbol (optarg
, &strip_specific_list
);
1880 output_file
= optarg
;
1883 preserve_dates
= true;
1886 discard_locals
= LOCALS_ALL
;
1889 discard_locals
= LOCALS_START_L
;
1895 show_version
= true;
1898 break; /* we've been given a long option */
1900 strip_usage (stdout
, 0);
1902 strip_usage (stderr
, 1);
1907 print_version ("strip");
1909 /* Default is to strip all symbols. */
1910 if (strip_symbols
== STRIP_UNDEF
1911 && discard_locals
== LOCALS_UNDEF
1912 && strip_specific_list
== NULL
)
1913 strip_symbols
= STRIP_ALL
;
1915 if (output_target
== (char *) NULL
)
1916 output_target
= input_target
;
1920 || (output_file
!= NULL
&& (i
+ 1) < argc
))
1921 strip_usage (stderr
, 1);
1923 for (; i
< argc
; i
++)
1925 int hold_status
= status
;
1926 struct stat statbuf
;
1931 if (stat (argv
[i
], &statbuf
) < 0)
1933 non_fatal (_("%s: cannot stat: %s"), argv
[i
], strerror (errno
));
1938 if (output_file
!= NULL
)
1939 tmpname
= output_file
;
1941 tmpname
= make_tempname (argv
[i
]);
1944 copy_file (argv
[i
], tmpname
, input_target
, output_target
);
1948 set_times (tmpname
, &statbuf
);
1949 if (output_file
== NULL
)
1950 smart_rename (tmpname
, argv
[i
], preserve_dates
);
1951 status
= hold_status
;
1955 if (output_file
== NULL
)
1963 copy_main (argc
, argv
)
1967 char * binary_architecture
= NULL
;
1968 char *input_filename
= NULL
, *output_filename
= NULL
;
1969 char *input_target
= NULL
, *output_target
= NULL
;
1970 boolean show_version
= false;
1971 boolean change_warn
= true;
1973 struct section_list
*p
;
1974 struct stat statbuf
;
1976 while ((c
= getopt_long (argc
, argv
, "b:B:i:I:j:K:N:s:O:d:F:L:R:SpgxXVvW:",
1977 copy_options
, (int *) 0)) != EOF
)
1982 copy_byte
= atoi (optarg
);
1984 fatal (_("byte number must be non-negative"));
1988 binary_architecture
= optarg
;
1992 interleave
= atoi (optarg
);
1994 fatal (_("interleave must be positive"));
1998 case 's': /* "source" - 'I' is preferred */
1999 input_target
= optarg
;
2003 case 'd': /* "destination" - 'O' is preferred */
2004 output_target
= optarg
;
2008 input_target
= output_target
= optarg
;
2012 p
= find_section_list (optarg
, true);
2014 fatal (_("%s both copied and removed"), optarg
);
2016 sections_copied
= true;
2020 p
= find_section_list (optarg
, true);
2022 fatal (_("%s both copied and removed"), optarg
);
2024 sections_removed
= true;
2028 strip_symbols
= STRIP_ALL
;
2032 strip_symbols
= STRIP_DEBUG
;
2035 case OPTION_STRIP_UNNEEDED
:
2036 strip_symbols
= STRIP_UNNEEDED
;
2040 add_specific_symbol (optarg
, &keep_specific_list
);
2044 add_specific_symbol (optarg
, &strip_specific_list
);
2048 add_specific_symbol (optarg
, &localize_specific_list
);
2052 add_specific_symbol (optarg
, &keepglobal_specific_list
);
2056 add_specific_symbol (optarg
, &weaken_specific_list
);
2060 preserve_dates
= true;
2064 discard_locals
= LOCALS_ALL
;
2068 discard_locals
= LOCALS_START_L
;
2076 show_version
= true;
2083 case OPTION_ADD_SECTION
:
2087 struct section_add
*pa
;
2092 s
= strchr (optarg
, '=');
2095 fatal (_("bad format for %s"), "--add-section");
2097 if (stat (s
+ 1, & st
) < 0)
2098 fatal (_("cannot stat: %s: %s"), s
+ 1, strerror (errno
));
2100 pa
= (struct section_add
*) xmalloc (sizeof (struct section_add
));
2103 name
= (char *) xmalloc (len
+ 1);
2104 strncpy (name
, optarg
, len
);
2108 pa
->filename
= s
+ 1;
2110 pa
->size
= st
.st_size
;
2112 pa
->contents
= (bfd_byte
*) xmalloc (pa
->size
);
2113 f
= fopen (pa
->filename
, FOPEN_RB
);
2116 fatal (_("cannot open: %s: %s"), pa
->filename
, strerror (errno
));
2118 if (fread (pa
->contents
, 1, pa
->size
, f
) == 0
2120 fatal (_("%s: fread failed"), pa
->filename
);
2124 pa
->next
= add_sections
;
2129 case OPTION_CHANGE_START
:
2130 change_start
= parse_vma (optarg
, "--change-start");
2133 case OPTION_CHANGE_SECTION_ADDRESS
:
2134 case OPTION_CHANGE_SECTION_LMA
:
2135 case OPTION_CHANGE_SECTION_VMA
:
2140 char *option
= NULL
;
2142 enum change_action what
= CHANGE_IGNORE
;
2146 case OPTION_CHANGE_SECTION_ADDRESS
:
2147 option
= "--change-section-address";
2149 case OPTION_CHANGE_SECTION_LMA
:
2150 option
= "--change-section-lma";
2152 case OPTION_CHANGE_SECTION_VMA
:
2153 option
= "--change-section-vma";
2157 s
= strchr (optarg
, '=');
2160 s
= strchr (optarg
, '+');
2163 s
= strchr (optarg
, '-');
2165 fatal (_("bad format for %s"), option
);
2170 name
= (char *) xmalloc (len
+ 1);
2171 strncpy (name
, optarg
, len
);
2174 p
= find_section_list (name
, true);
2176 val
= parse_vma (s
+ 1, option
);
2180 case '=': what
= CHANGE_SET
; break;
2181 case '-': val
= - val
; /* Drop through. */
2182 case '+': what
= CHANGE_MODIFY
; break;
2187 case OPTION_CHANGE_SECTION_ADDRESS
:
2188 p
->change_vma
= what
;
2192 case OPTION_CHANGE_SECTION_LMA
:
2193 p
->change_lma
= what
;
2197 case OPTION_CHANGE_SECTION_VMA
:
2198 p
->change_vma
= what
;
2205 case OPTION_CHANGE_ADDRESSES
:
2206 change_section_address
= parse_vma (optarg
, "--change-addresses");
2207 change_start
= change_section_address
;
2210 case OPTION_CHANGE_WARNINGS
:
2214 case OPTION_CHANGE_LEADING_CHAR
:
2215 change_leading_char
= true;
2218 case OPTION_DEBUGGING
:
2219 convert_debugging
= true;
2222 case OPTION_GAP_FILL
:
2224 bfd_vma gap_fill_vma
;
2226 gap_fill_vma
= parse_vma (optarg
, "--gap-fill");
2227 gap_fill
= (bfd_byte
) gap_fill_vma
;
2228 if ((bfd_vma
) gap_fill
!= gap_fill_vma
)
2232 sprintf_vma (buff
, gap_fill_vma
);
2234 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2237 gap_fill_set
= true;
2241 case OPTION_NO_CHANGE_WARNINGS
:
2242 change_warn
= false;
2246 pad_to
= parse_vma (optarg
, "--pad-to");
2250 case OPTION_REMOVE_LEADING_CHAR
:
2251 remove_leading_char
= true;
2254 case OPTION_REDEFINE_SYM
:
2256 /* Push this redefinition onto redefine_symbol_list. */
2260 const char *nextarg
;
2261 char *source
, *target
;
2263 s
= strchr (optarg
, '=');
2266 fatal (_("bad format for %s"), "--redefine-sym");
2270 source
= (char *) xmalloc (len
+ 1);
2271 strncpy (source
, optarg
, len
);
2275 len
= strlen (nextarg
);
2276 target
= (char *) xmalloc (len
+ 1);
2277 strcpy (target
, nextarg
);
2279 redefine_list_append (source
, target
);
2286 case OPTION_SET_SECTION_FLAGS
:
2292 s
= strchr (optarg
, '=');
2294 fatal (_("bad format for %s"), "--set-section-flags");
2297 name
= (char *) xmalloc (len
+ 1);
2298 strncpy (name
, optarg
, len
);
2301 p
= find_section_list (name
, true);
2303 p
->set_flags
= true;
2304 p
->flags
= parse_flags (s
+ 1);
2308 case OPTION_SET_START
:
2309 set_start
= parse_vma (optarg
, "--set-start");
2310 set_start_set
= true;
2313 case OPTION_SREC_LEN
:
2314 Chunk
= parse_vma (optarg
, "--srec-len");
2317 case OPTION_SREC_FORCES3
:
2321 case OPTION_STRIP_SYMBOLS
:
2322 add_specific_symbols (optarg
, &strip_specific_list
);
2325 case OPTION_KEEP_SYMBOLS
:
2326 add_specific_symbols (optarg
, &keep_specific_list
);
2329 case OPTION_LOCALIZE_SYMBOLS
:
2330 add_specific_symbols (optarg
, &localize_specific_list
);
2333 case OPTION_KEEPGLOBAL_SYMBOLS
:
2334 add_specific_symbols (optarg
, &keepglobal_specific_list
);
2337 case OPTION_WEAKEN_SYMBOLS
:
2338 add_specific_symbols (optarg
, &weaken_specific_list
);
2342 break; /* we've been given a long option */
2345 copy_usage (stdout
, 0);
2348 copy_usage (stderr
, 1);
2353 print_version ("objcopy");
2355 if (copy_byte
>= interleave
)
2356 fatal (_("byte number must be less than interleave"));
2358 if (optind
== argc
|| optind
+ 2 < argc
)
2359 copy_usage (stderr
, 1);
2361 input_filename
= argv
[optind
];
2362 if (optind
+ 1 < argc
)
2363 output_filename
= argv
[optind
+ 1];
2365 /* Default is to strip no symbols. */
2366 if (strip_symbols
== STRIP_UNDEF
&& discard_locals
== LOCALS_UNDEF
)
2367 strip_symbols
= STRIP_NONE
;
2369 if (output_target
== (char *) NULL
)
2370 output_target
= input_target
;
2372 if (binary_architecture
!= (char *) NULL
)
2374 if (input_target
&& strcmp (input_target
, "binary") == 0)
2376 const bfd_arch_info_type
* temp_arch_info
;
2378 temp_arch_info
= bfd_scan_arch (binary_architecture
);
2380 if (temp_arch_info
!= NULL
)
2381 bfd_external_binary_architecture
= temp_arch_info
->arch
;
2383 fatal (_("architecture %s unknown"), binary_architecture
);
2387 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2388 non_fatal (_(" Argument %s ignored"), binary_architecture
);
2393 if (stat (input_filename
, & statbuf
) < 0)
2394 fatal (_("Cannot stat: %s: %s"), input_filename
, strerror (errno
));
2396 /* If there is no destination file then create a temp and rename
2397 the result into the input. */
2399 if (output_filename
== (char *) NULL
)
2401 char *tmpname
= make_tempname (input_filename
);
2403 copy_file (input_filename
, tmpname
, input_target
, output_target
);
2407 set_times (tmpname
, &statbuf
);
2408 smart_rename (tmpname
, input_filename
, preserve_dates
);
2415 copy_file (input_filename
, output_filename
, input_target
, output_target
);
2416 if (status
== 0 && preserve_dates
)
2417 set_times (output_filename
, &statbuf
);
2422 for (p
= change_sections
; p
!= NULL
; p
= p
->next
)
2426 if (p
->change_vma
!= CHANGE_IGNORE
)
2430 sprintf_vma (buff
, p
->vma_val
);
2432 /* xgettext:c-format */
2433 non_fatal (_("%s %s%c0x%s never used"),
2434 "--change-section-vma",
2436 p
->change_vma
== CHANGE_SET
? '=' : '+',
2440 if (p
->change_lma
!= CHANGE_IGNORE
)
2444 sprintf_vma (buff
, p
->lma_val
);
2446 /* xgettext:c-format */
2447 non_fatal (_("%s %s%c0x%s never used"),
2448 "--change-section-lma",
2450 p
->change_lma
== CHANGE_SET
? '=' : '+',
2465 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2466 setlocale (LC_MESSAGES
, "");
2468 bindtextdomain (PACKAGE
, LOCALEDIR
);
2469 textdomain (PACKAGE
);
2471 program_name
= argv
[0];
2472 xmalloc_set_program_name (program_name
);
2474 START_PROGRESS (program_name
, 0);
2476 strip_symbols
= STRIP_UNDEF
;
2477 discard_locals
= LOCALS_UNDEF
;
2480 set_default_bfd_target ();
2484 int i
= strlen (program_name
);
2485 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2486 /* Drop the .exe suffix, if any. */
2487 if (i
> 4 && FILENAME_CMP (program_name
+ i
- 4, ".exe") == 0)
2490 program_name
[i
] = '\0';
2493 is_strip
= (i
>= 5 && FILENAME_CMP (program_name
+ i
- 5, "strip") == 0);
2497 strip_main (argc
, argv
);
2499 copy_main (argc
, argv
);
2501 END_PROGRESS (program_name
);