* MAINTAINERS: Very belatedly remove myself from from the list of people caught
[deliverable/binutils-gdb.git] / binutils / objcopy.c
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,
3 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5
6 This file is part of GNU Binutils.
7
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.
12
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.
17
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., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22 \f
23 #include "bfd.h"
24 #include "progress.h"
25 #include "bucomm.h"
26 #include "getopt.h"
27 #include "libiberty.h"
28 #include "budbg.h"
29 #include "filenames.h"
30 #include "fnmatch.h"
31 #include "elf-bfd.h"
32 #include <sys/stat.h>
33
34 /* A list of symbols to explicitly strip out, or to keep. A linked
35 list is good enough for a small number from the command line, but
36 this will slow things down a lot if many symbols are being
37 deleted. */
38
39 struct symlist
40 {
41 const char *name;
42 struct symlist *next;
43 };
44
45 /* A list to support redefine_sym. */
46 struct redefine_node
47 {
48 char *source;
49 char *target;
50 struct redefine_node *next;
51 };
52
53 typedef struct section_rename
54 {
55 const char * old_name;
56 const char * new_name;
57 flagword flags;
58 struct section_rename * next;
59 }
60 section_rename;
61
62 /* List of sections to be renamed. */
63 static section_rename *section_rename_list;
64
65 #define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
66
67 static asymbol **isympp = NULL; /* Input symbols. */
68 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
69
70 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
71 static int copy_byte = -1;
72 static int interleave = 4;
73
74 static bfd_boolean verbose; /* Print file and target names. */
75 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
76 static int status = 0; /* Exit status. */
77
78 enum strip_action
79 {
80 STRIP_UNDEF,
81 STRIP_NONE, /* Don't strip. */
82 STRIP_DEBUG, /* Strip all debugger symbols. */
83 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
84 STRIP_NONDEBUG, /* Strip everything but debug info. */
85 STRIP_ALL /* Strip all symbols. */
86 };
87
88 /* Which symbols to remove. */
89 static enum strip_action strip_symbols;
90
91 enum locals_action
92 {
93 LOCALS_UNDEF,
94 LOCALS_START_L, /* Discard locals starting with L. */
95 LOCALS_ALL /* Discard all locals. */
96 };
97
98 /* Which local symbols to remove. Overrides STRIP_ALL. */
99 static enum locals_action discard_locals;
100
101 /* What kind of change to perform. */
102 enum change_action
103 {
104 CHANGE_IGNORE,
105 CHANGE_MODIFY,
106 CHANGE_SET
107 };
108
109 /* Structure used to hold lists of sections and actions to take. */
110 struct section_list
111 {
112 struct section_list * next; /* Next section to change. */
113 const char * name; /* Section name. */
114 bfd_boolean used; /* Whether this entry was used. */
115 bfd_boolean remove; /* Whether to remove this section. */
116 bfd_boolean copy; /* Whether to copy this section. */
117 enum change_action change_vma;/* Whether to change or set VMA. */
118 bfd_vma vma_val; /* Amount to change by or set to. */
119 enum change_action change_lma;/* Whether to change or set LMA. */
120 bfd_vma lma_val; /* Amount to change by or set to. */
121 bfd_boolean set_flags; /* Whether to set the section flags. */
122 flagword flags; /* What to set the section flags to. */
123 };
124
125 static struct section_list *change_sections;
126
127 /* TRUE if some sections are to be removed. */
128 static bfd_boolean sections_removed;
129
130 /* TRUE if only some sections are to be copied. */
131 static bfd_boolean sections_copied;
132
133 /* Changes to the start address. */
134 static bfd_vma change_start = 0;
135 static bfd_boolean set_start_set = FALSE;
136 static bfd_vma set_start;
137
138 /* Changes to section addresses. */
139 static bfd_vma change_section_address = 0;
140
141 /* Filling gaps between sections. */
142 static bfd_boolean gap_fill_set = FALSE;
143 static bfd_byte gap_fill = 0;
144
145 /* Pad to a given address. */
146 static bfd_boolean pad_to_set = FALSE;
147 static bfd_vma pad_to;
148
149 /* Use alternate machine code? */
150 static int use_alt_mach_code = 0;
151
152 /* Output BFD flags user wants to set or clear */
153 static flagword bfd_flags_to_set;
154 static flagword bfd_flags_to_clear;
155
156 /* List of sections to add. */
157 struct section_add
158 {
159 /* Next section to add. */
160 struct section_add *next;
161 /* Name of section to add. */
162 const char *name;
163 /* Name of file holding section contents. */
164 const char *filename;
165 /* Size of file. */
166 size_t size;
167 /* Contents of file. */
168 bfd_byte *contents;
169 /* BFD section, after it has been added. */
170 asection *section;
171 };
172
173 /* List of sections to add to the output BFD. */
174 static struct section_add *add_sections;
175
176 /* If non-NULL the argument to --add-gnu-debuglink.
177 This should be the filename to store in the .gnu_debuglink section. */
178 static const char * gnu_debuglink_filename = NULL;
179
180 /* Whether to convert debugging information. */
181 static bfd_boolean convert_debugging = FALSE;
182
183 /* Whether to change the leading character in symbol names. */
184 static bfd_boolean change_leading_char = FALSE;
185
186 /* Whether to remove the leading character from global symbol names. */
187 static bfd_boolean remove_leading_char = FALSE;
188
189 /* Whether to permit wildcard in symbol comparison. */
190 static bfd_boolean wildcard = FALSE;
191
192 /* List of symbols to strip, keep, localize, keep-global, weaken,
193 or redefine. */
194 static struct symlist *strip_specific_list = NULL;
195 static struct symlist *strip_unneeded_list = NULL;
196 static struct symlist *keep_specific_list = NULL;
197 static struct symlist *localize_specific_list = NULL;
198 static struct symlist *globalize_specific_list = NULL;
199 static struct symlist *keepglobal_specific_list = NULL;
200 static struct symlist *weaken_specific_list = NULL;
201 static struct redefine_node *redefine_sym_list = NULL;
202
203 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
204 static bfd_boolean weaken = FALSE;
205
206 /* If this is TRUE, we retain BSF_FILE symbols. */
207 static bfd_boolean keep_file_symbols = FALSE;
208
209 /* Prefix symbols/sections. */
210 static char *prefix_symbols_string = 0;
211 static char *prefix_sections_string = 0;
212 static char *prefix_alloc_sections_string = 0;
213
214 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
215 enum command_line_switch
216 {
217 OPTION_ADD_SECTION=150,
218 OPTION_CHANGE_ADDRESSES,
219 OPTION_CHANGE_LEADING_CHAR,
220 OPTION_CHANGE_START,
221 OPTION_CHANGE_SECTION_ADDRESS,
222 OPTION_CHANGE_SECTION_LMA,
223 OPTION_CHANGE_SECTION_VMA,
224 OPTION_CHANGE_WARNINGS,
225 OPTION_DEBUGGING,
226 OPTION_GAP_FILL,
227 OPTION_NO_CHANGE_WARNINGS,
228 OPTION_PAD_TO,
229 OPTION_REMOVE_LEADING_CHAR,
230 OPTION_SET_SECTION_FLAGS,
231 OPTION_SET_START,
232 OPTION_STRIP_UNNEEDED,
233 OPTION_WEAKEN,
234 OPTION_REDEFINE_SYM,
235 OPTION_REDEFINE_SYMS,
236 OPTION_SREC_LEN,
237 OPTION_SREC_FORCES3,
238 OPTION_STRIP_SYMBOLS,
239 OPTION_STRIP_UNNEEDED_SYMBOL,
240 OPTION_STRIP_UNNEEDED_SYMBOLS,
241 OPTION_KEEP_SYMBOLS,
242 OPTION_LOCALIZE_SYMBOLS,
243 OPTION_GLOBALIZE_SYMBOL,
244 OPTION_GLOBALIZE_SYMBOLS,
245 OPTION_KEEPGLOBAL_SYMBOLS,
246 OPTION_WEAKEN_SYMBOLS,
247 OPTION_RENAME_SECTION,
248 OPTION_ALT_MACH_CODE,
249 OPTION_PREFIX_SYMBOLS,
250 OPTION_PREFIX_SECTIONS,
251 OPTION_PREFIX_ALLOC_SECTIONS,
252 OPTION_FORMATS_INFO,
253 OPTION_ADD_GNU_DEBUGLINK,
254 OPTION_ONLY_KEEP_DEBUG,
255 OPTION_KEEP_FILE_SYMBOLS,
256 OPTION_READONLY_TEXT,
257 OPTION_WRITABLE_TEXT,
258 OPTION_PURE,
259 OPTION_IMPURE
260 };
261
262 /* Options to handle if running as "strip". */
263
264 static struct option strip_options[] =
265 {
266 {"discard-all", no_argument, 0, 'x'},
267 {"discard-locals", no_argument, 0, 'X'},
268 {"format", required_argument, 0, 'F'}, /* Obsolete */
269 {"help", no_argument, 0, 'h'},
270 {"info", no_argument, 0, OPTION_FORMATS_INFO},
271 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
272 {"input-target", required_argument, 0, 'I'},
273 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
274 {"keep-symbol", required_argument, 0, 'K'},
275 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
276 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
277 {"output-target", required_argument, 0, 'O'},
278 {"output-file", required_argument, 0, 'o'},
279 {"preserve-dates", no_argument, 0, 'p'},
280 {"remove-section", required_argument, 0, 'R'},
281 {"strip-all", no_argument, 0, 's'},
282 {"strip-debug", no_argument, 0, 'S'},
283 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
284 {"strip-symbol", required_argument, 0, 'N'},
285 {"target", required_argument, 0, 'F'},
286 {"verbose", no_argument, 0, 'v'},
287 {"version", no_argument, 0, 'V'},
288 {"wildcard", no_argument, 0, 'w'},
289 {0, no_argument, 0, 0}
290 };
291
292 /* Options to handle if running as "objcopy". */
293
294 static struct option copy_options[] =
295 {
296 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
297 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
298 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
299 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
300 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
301 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
302 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
303 {"binary-architecture", required_argument, 0, 'B'},
304 {"byte", required_argument, 0, 'b'},
305 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
306 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
307 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
308 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
309 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
310 {"change-start", required_argument, 0, OPTION_CHANGE_START},
311 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
312 {"debugging", no_argument, 0, OPTION_DEBUGGING},
313 {"discard-all", no_argument, 0, 'x'},
314 {"discard-locals", no_argument, 0, 'X'},
315 {"format", required_argument, 0, 'F'}, /* Obsolete */
316 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
317 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
318 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
319 {"help", no_argument, 0, 'h'},
320 {"impure", no_argument, 0, OPTION_IMPURE},
321 {"info", no_argument, 0, OPTION_FORMATS_INFO},
322 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
323 {"input-target", required_argument, 0, 'I'},
324 {"interleave", required_argument, 0, 'i'},
325 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
326 {"keep-global-symbol", required_argument, 0, 'G'},
327 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
328 {"keep-symbol", required_argument, 0, 'K'},
329 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
330 {"localize-symbol", required_argument, 0, 'L'},
331 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
332 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
333 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
334 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
335 {"only-section", required_argument, 0, 'j'},
336 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
337 {"output-target", required_argument, 0, 'O'},
338 {"pad-to", required_argument, 0, OPTION_PAD_TO},
339 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
340 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
341 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
342 {"preserve-dates", no_argument, 0, 'p'},
343 {"pure", no_argument, 0, OPTION_PURE},
344 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
345 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
346 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
347 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
348 {"remove-section", required_argument, 0, 'R'},
349 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
350 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
351 {"set-start", required_argument, 0, OPTION_SET_START},
352 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
353 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
354 {"strip-all", no_argument, 0, 'S'},
355 {"strip-debug", no_argument, 0, 'g'},
356 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
357 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
358 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
359 {"strip-symbol", required_argument, 0, 'N'},
360 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
361 {"target", required_argument, 0, 'F'},
362 {"verbose", no_argument, 0, 'v'},
363 {"version", no_argument, 0, 'V'},
364 {"weaken", no_argument, 0, OPTION_WEAKEN},
365 {"weaken-symbol", required_argument, 0, 'W'},
366 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
367 {"wildcard", no_argument, 0, 'w'},
368 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
369 {0, no_argument, 0, 0}
370 };
371
372 /* IMPORTS */
373 extern char *program_name;
374
375 /* This flag distinguishes between strip and objcopy:
376 1 means this is 'strip'; 0 means this is 'objcopy'.
377 -1 means if we should use argv[0] to decide. */
378 extern int is_strip;
379
380 /* The maximum length of an S record. This variable is declared in srec.c
381 and can be modified by the --srec-len parameter. */
382 extern unsigned int Chunk;
383
384 /* Restrict the generation of Srecords to type S3 only.
385 This variable is declare in bfd/srec.c and can be toggled
386 on by the --srec-forceS3 command line switch. */
387 extern bfd_boolean S3Forced;
388
389 /* Defined in bfd/binary.c. Used to set architecture and machine of input
390 binary files. */
391 extern enum bfd_architecture bfd_external_binary_architecture;
392 extern unsigned long bfd_external_machine;
393
394 /* Forward declarations. */
395 static void setup_section (bfd *, asection *, void *);
396 static void setup_bfd_headers (bfd *, bfd *);
397 static void copy_section (bfd *, asection *, void *);
398 static void get_sections (bfd *, asection *, void *);
399 static int compare_section_lma (const void *, const void *);
400 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
401 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
402 static const char *lookup_sym_redefinition (const char *);
403 \f
404 static void
405 copy_usage (FILE *stream, int exit_status)
406 {
407 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
408 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
409 fprintf (stream, _(" The options 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 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
414 -F --target <bfdname> Set both input and output format to <bfdname>\n\
415 --debugging Convert debugging information, if possible\n\
416 -p --preserve-dates Copy modified/access timestamps to the output\n\
417 -j --only-section <name> Only copy section <name> into the output\n\
418 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
419 -R --remove-section <name> Remove section <name> from the output\n\
420 -S --strip-all Remove all symbol and relocation information\n\
421 -g --strip-debug Remove all debugging symbols & sections\n\
422 --strip-unneeded Remove all symbols not needed by relocations\n\
423 -N --strip-symbol <name> Do not copy symbol <name>\n\
424 --strip-unneeded-symbol <name>\n\
425 Do not copy symbol <name> unless needed by\n\
426 relocations\n\
427 --only-keep-debug Strip everything but the debug information\n\
428 -K --keep-symbol <name> Do not strip symbol <name>\n\
429 --keep-file-symbols Do not strip file symbol(s)\n\
430 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
431 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
432 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
433 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
434 --weaken Force all global symbols to be marked as weak\n\
435 -w --wildcard Permit wildcard in symbol comparison\n\
436 -x --discard-all Remove all non-global symbols\n\
437 -X --discard-locals Remove any compiler-generated symbols\n\
438 -i --interleave <number> Only copy one out of every <number> bytes\n\
439 -b --byte <num> Select byte <num> in every interleaved block\n\
440 --gap-fill <val> Fill gaps between sections with <val>\n\
441 --pad-to <addr> Pad the last section up to address <addr>\n\
442 --set-start <addr> Set the start address to <addr>\n\
443 {--change-start|--adjust-start} <incr>\n\
444 Add <incr> to the start address\n\
445 {--change-addresses|--adjust-vma} <incr>\n\
446 Add <incr> to LMA, VMA and start addresses\n\
447 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
448 Change LMA and VMA of section <name> by <val>\n\
449 --change-section-lma <name>{=|+|-}<val>\n\
450 Change the LMA of section <name> by <val>\n\
451 --change-section-vma <name>{=|+|-}<val>\n\
452 Change the VMA of section <name> by <val>\n\
453 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
454 Warn if a named section does not exist\n\
455 --set-section-flags <name>=<flags>\n\
456 Set section <name>'s properties to <flags>\n\
457 --add-section <name>=<file> Add section <name> found in <file> to output\n\
458 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
459 --change-leading-char Force output format's leading character style\n\
460 --remove-leading-char Remove leading character from global symbols\n\
461 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
462 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
463 listed in <file>\n\
464 --srec-len <number> Restrict the length of generated Srecords\n\
465 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
466 --strip-symbols <file> -N for all symbols listed in <file>\n\
467 --strip-unneeded-symbols <file>\n\
468 --strip-unneeded-symbol for all symbols listed\n\
469 in <file>\n\
470 --keep-symbols <file> -K for all symbols listed in <file>\n\
471 --localize-symbols <file> -L for all symbols listed in <file>\n\
472 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
473 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
474 --weaken-symbols <file> -W for all symbols listed in <file>\n\
475 --alt-machine-code <index> Use alternate machine code for output\n\
476 --writable-text Mark the output text as writable\n\
477 --readonly-text Make the output text write protected\n\
478 --pure Mark the output file as demand paged\n\
479 --impure Mark the output file as impure\n\
480 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
481 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
482 --prefix-alloc-sections <prefix>\n\
483 Add <prefix> to start of every allocatable\n\
484 section name\n\
485 -v --verbose List all object files modified\n\
486 @<file> Read options from <file>\n\
487 -V --version Display this program's version number\n\
488 -h --help Display this output\n\
489 --info List object formats & architectures supported\n\
490 "));
491 list_supported_targets (program_name, stream);
492 if (exit_status == 0)
493 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
494 exit (exit_status);
495 }
496
497 static void
498 strip_usage (FILE *stream, int exit_status)
499 {
500 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
501 fprintf (stream, _(" Removes symbols and sections from files\n"));
502 fprintf (stream, _(" The options are:\n"));
503 fprintf (stream, _("\
504 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
505 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
506 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
507 -p --preserve-dates Copy modified/access timestamps to the output\n\
508 -R --remove-section=<name> Remove section <name> from the output\n\
509 -s --strip-all Remove all symbol and relocation information\n\
510 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
511 --strip-unneeded Remove all symbols not needed by relocations\n\
512 --only-keep-debug Strip everything but the debug information\n\
513 -N --strip-symbol=<name> Do not copy symbol <name>\n\
514 -K --keep-symbol=<name> Do not strip symbol <name>\n\
515 --keep-file-symbols Do not strip file symbol(s)\n\
516 -w --wildcard Permit wildcard in symbol comparison\n\
517 -x --discard-all Remove all non-global symbols\n\
518 -X --discard-locals Remove any compiler-generated symbols\n\
519 -v --verbose List all object files modified\n\
520 -V --version Display this program's version number\n\
521 -h --help Display this output\n\
522 --info List object formats & architectures supported\n\
523 -o <file> Place stripped output into <file>\n\
524 "));
525
526 list_supported_targets (program_name, stream);
527 if (exit_status == 0)
528 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
529 exit (exit_status);
530 }
531
532 /* Parse section flags into a flagword, with a fatal error if the
533 string can't be parsed. */
534
535 static flagword
536 parse_flags (const char *s)
537 {
538 flagword ret;
539 const char *snext;
540 int len;
541
542 ret = SEC_NO_FLAGS;
543
544 do
545 {
546 snext = strchr (s, ',');
547 if (snext == NULL)
548 len = strlen (s);
549 else
550 {
551 len = snext - s;
552 ++snext;
553 }
554
555 if (0) ;
556 #define PARSE_FLAG(fname,fval) \
557 else if (strncasecmp (fname, s, len) == 0) ret |= fval
558 PARSE_FLAG ("alloc", SEC_ALLOC);
559 PARSE_FLAG ("load", SEC_LOAD);
560 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
561 PARSE_FLAG ("readonly", SEC_READONLY);
562 PARSE_FLAG ("debug", SEC_DEBUGGING);
563 PARSE_FLAG ("code", SEC_CODE);
564 PARSE_FLAG ("data", SEC_DATA);
565 PARSE_FLAG ("rom", SEC_ROM);
566 PARSE_FLAG ("share", SEC_COFF_SHARED);
567 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
568 #undef PARSE_FLAG
569 else
570 {
571 char *copy;
572
573 copy = xmalloc (len + 1);
574 strncpy (copy, s, len);
575 copy[len] = '\0';
576 non_fatal (_("unrecognized section flag `%s'"), copy);
577 fatal (_("supported flags: %s"),
578 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
579 }
580
581 s = snext;
582 }
583 while (s != NULL);
584
585 return ret;
586 }
587
588 /* Find and optionally add an entry in the change_sections list. */
589
590 static struct section_list *
591 find_section_list (const char *name, bfd_boolean add)
592 {
593 struct section_list *p;
594
595 for (p = change_sections; p != NULL; p = p->next)
596 if (strcmp (p->name, name) == 0)
597 return p;
598
599 if (! add)
600 return NULL;
601
602 p = xmalloc (sizeof (struct section_list));
603 p->name = name;
604 p->used = FALSE;
605 p->remove = FALSE;
606 p->copy = FALSE;
607 p->change_vma = CHANGE_IGNORE;
608 p->change_lma = CHANGE_IGNORE;
609 p->vma_val = 0;
610 p->lma_val = 0;
611 p->set_flags = FALSE;
612 p->flags = 0;
613
614 p->next = change_sections;
615 change_sections = p;
616
617 return p;
618 }
619
620 /* Add a symbol to strip_specific_list. */
621
622 static void
623 add_specific_symbol (const char *name, struct symlist **list)
624 {
625 struct symlist *tmp_list;
626
627 tmp_list = xmalloc (sizeof (struct symlist));
628 tmp_list->name = name;
629 tmp_list->next = *list;
630 *list = tmp_list;
631 }
632
633 /* Add symbols listed in `filename' to strip_specific_list. */
634
635 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
636 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
637
638 static void
639 add_specific_symbols (const char *filename, struct symlist **list)
640 {
641 off_t size;
642 FILE * f;
643 char * line;
644 char * buffer;
645 unsigned int line_count;
646
647 size = get_file_size (filename);
648 if (size == 0)
649 return;
650
651 buffer = xmalloc (size + 2);
652 f = fopen (filename, FOPEN_RT);
653 if (f == NULL)
654 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
655
656 if (fread (buffer, 1, size, f) == 0 || ferror (f))
657 fatal (_("%s: fread failed"), filename);
658
659 fclose (f);
660 buffer [size] = '\n';
661 buffer [size + 1] = '\0';
662
663 line_count = 1;
664
665 for (line = buffer; * line != '\0'; line ++)
666 {
667 char * eol;
668 char * name;
669 char * name_end;
670 int finished = FALSE;
671
672 for (eol = line;; eol ++)
673 {
674 switch (* eol)
675 {
676 case '\n':
677 * eol = '\0';
678 /* Cope with \n\r. */
679 if (eol[1] == '\r')
680 ++ eol;
681 finished = TRUE;
682 break;
683
684 case '\r':
685 * eol = '\0';
686 /* Cope with \r\n. */
687 if (eol[1] == '\n')
688 ++ eol;
689 finished = TRUE;
690 break;
691
692 case 0:
693 finished = TRUE;
694 break;
695
696 case '#':
697 /* Line comment, Terminate the line here, in case a
698 name is present and then allow the rest of the
699 loop to find the real end of the line. */
700 * eol = '\0';
701 break;
702
703 default:
704 break;
705 }
706
707 if (finished)
708 break;
709 }
710
711 /* A name may now exist somewhere between 'line' and 'eol'.
712 Strip off leading whitespace and trailing whitespace,
713 then add it to the list. */
714 for (name = line; IS_WHITESPACE (* name); name ++)
715 ;
716 for (name_end = name;
717 (! IS_WHITESPACE (* name_end))
718 && (! IS_LINE_TERMINATOR (* name_end));
719 name_end ++)
720 ;
721
722 if (! IS_LINE_TERMINATOR (* name_end))
723 {
724 char * extra;
725
726 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
727 ;
728
729 if (! IS_LINE_TERMINATOR (* extra))
730 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
731 filename, line_count);
732 }
733
734 * name_end = '\0';
735
736 if (name_end > name)
737 add_specific_symbol (name, list);
738
739 /* Advance line pointer to end of line. The 'eol ++' in the for
740 loop above will then advance us to the start of the next line. */
741 line = eol;
742 line_count ++;
743 }
744 }
745
746 /* See whether a symbol should be stripped or kept based on
747 strip_specific_list and keep_symbols. */
748
749 static bfd_boolean
750 is_specified_symbol (const char *name, struct symlist *list)
751 {
752 struct symlist *tmp_list;
753
754 if (wildcard)
755 {
756 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
757 if (*(tmp_list->name) != '!')
758 {
759 if (!fnmatch (tmp_list->name, name, 0))
760 return TRUE;
761 }
762 else
763 {
764 if (fnmatch (tmp_list->name + 1, name, 0))
765 return TRUE;
766 }
767 }
768 else
769 {
770 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
771 if (strcmp (name, tmp_list->name) == 0)
772 return TRUE;
773 }
774
775 return FALSE;
776 }
777
778 /* See if a section is being removed. */
779
780 static bfd_boolean
781 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
782 {
783 if (sections_removed || sections_copied)
784 {
785 struct section_list *p;
786
787 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
788
789 if (sections_removed && p != NULL && p->remove)
790 return TRUE;
791 if (sections_copied && (p == NULL || ! p->copy))
792 return TRUE;
793 }
794
795 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
796 {
797 if (strip_symbols == STRIP_DEBUG
798 || strip_symbols == STRIP_UNNEEDED
799 || strip_symbols == STRIP_ALL
800 || discard_locals == LOCALS_ALL
801 || convert_debugging)
802 return TRUE;
803
804 if (strip_symbols == STRIP_NONDEBUG)
805 return FALSE;
806 }
807
808 return FALSE;
809 }
810
811 /* Choose which symbol entries to copy; put the result in OSYMS.
812 We don't copy in place, because that confuses the relocs.
813 Return the number of symbols to print. */
814
815 static unsigned int
816 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
817 asymbol **isyms, long symcount)
818 {
819 asymbol **from = isyms, **to = osyms;
820 long src_count = 0, dst_count = 0;
821 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
822 == HAS_RELOC;
823
824 for (; src_count < symcount; src_count++)
825 {
826 asymbol *sym = from[src_count];
827 flagword flags = sym->flags;
828 char *name = (char *) bfd_asymbol_name (sym);
829 int keep;
830 bfd_boolean undefined;
831 bfd_boolean rem_leading_char;
832 bfd_boolean add_leading_char;
833
834 undefined = bfd_is_und_section (bfd_get_section (sym));
835
836 if (redefine_sym_list)
837 {
838 char *old_name, *new_name;
839
840 old_name = (char *) bfd_asymbol_name (sym);
841 new_name = (char *) lookup_sym_redefinition (old_name);
842 bfd_asymbol_name (sym) = new_name;
843 name = new_name;
844 }
845
846 /* Check if we will remove the current leading character. */
847 rem_leading_char =
848 (name[0] == bfd_get_symbol_leading_char (abfd))
849 && (change_leading_char
850 || (remove_leading_char
851 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
852 || undefined
853 || bfd_is_com_section (bfd_get_section (sym)))));
854
855 /* Check if we will add a new leading character. */
856 add_leading_char =
857 change_leading_char
858 && (bfd_get_symbol_leading_char (obfd) != '\0')
859 && (bfd_get_symbol_leading_char (abfd) == '\0'
860 || (name[0] == bfd_get_symbol_leading_char (abfd)));
861
862 /* Short circuit for change_leading_char if we can do it in-place. */
863 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
864 {
865 name[0] = bfd_get_symbol_leading_char (obfd);
866 bfd_asymbol_name (sym) = name;
867 rem_leading_char = FALSE;
868 add_leading_char = FALSE;
869 }
870
871 /* Remove leading char. */
872 if (rem_leading_char)
873 bfd_asymbol_name (sym) = ++name;
874
875 /* Add new leading char and/or prefix. */
876 if (add_leading_char || prefix_symbols_string)
877 {
878 char *n, *ptr;
879
880 ptr = n = xmalloc (1 + strlen (prefix_symbols_string)
881 + strlen (name) + 1);
882 if (add_leading_char)
883 *ptr++ = bfd_get_symbol_leading_char (obfd);
884
885 if (prefix_symbols_string)
886 {
887 strcpy (ptr, prefix_symbols_string);
888 ptr += strlen (prefix_symbols_string);
889 }
890
891 strcpy (ptr, name);
892 bfd_asymbol_name (sym) = n;
893 name = n;
894 }
895
896 if (strip_symbols == STRIP_ALL)
897 keep = 0;
898 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
899 || ((flags & BSF_SECTION_SYM) != 0
900 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
901 & BSF_KEEP) != 0))
902 keep = 1;
903 else if (relocatable /* Relocatable file. */
904 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
905 keep = 1;
906 else if (bfd_decode_symclass (sym) == 'I')
907 /* Global symbols in $idata sections need to be retained
908 even if relocatable is FALSE. External users of the
909 library containing the $idata section may reference these
910 symbols. */
911 keep = 1;
912 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
913 || (flags & BSF_WEAK) != 0
914 || undefined
915 || bfd_is_com_section (bfd_get_section (sym)))
916 keep = strip_symbols != STRIP_UNNEEDED;
917 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
918 keep = (strip_symbols != STRIP_DEBUG
919 && strip_symbols != STRIP_UNNEEDED
920 && ! convert_debugging);
921 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
922 /* COMDAT sections store special information in local
923 symbols, so we cannot risk stripping any of them. */
924 keep = 1;
925 else /* Local symbol. */
926 keep = (strip_symbols != STRIP_UNNEEDED
927 && (discard_locals != LOCALS_ALL
928 && (discard_locals != LOCALS_START_L
929 || ! bfd_is_local_label (abfd, sym))));
930
931 if (keep && is_specified_symbol (name, strip_specific_list))
932 keep = 0;
933 if (keep
934 && !(flags & BSF_KEEP)
935 && is_specified_symbol (name, strip_unneeded_list))
936 keep = 0;
937 if (!keep
938 && ((keep_file_symbols && (flags & BSF_FILE))
939 || is_specified_symbol (name, keep_specific_list)))
940 keep = 1;
941 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
942 keep = 0;
943
944 if (keep)
945 {
946 if ((flags & BSF_GLOBAL) != 0
947 && (weaken || is_specified_symbol (name, weaken_specific_list)))
948 {
949 sym->flags &= ~ BSF_GLOBAL;
950 sym->flags |= BSF_WEAK;
951 }
952
953 if (!undefined
954 && (flags & (BSF_GLOBAL | BSF_WEAK))
955 && (is_specified_symbol (name, localize_specific_list)
956 || (keepglobal_specific_list != NULL
957 && ! is_specified_symbol (name, keepglobal_specific_list))))
958 {
959 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
960 sym->flags |= BSF_LOCAL;
961 }
962
963 if (!undefined
964 && (flags & BSF_LOCAL)
965 && is_specified_symbol (name, globalize_specific_list))
966 {
967 sym->flags &= ~ BSF_LOCAL;
968 sym->flags |= BSF_GLOBAL;
969 }
970
971 to[dst_count++] = sym;
972 }
973 }
974
975 to[dst_count] = NULL;
976
977 return dst_count;
978 }
979
980 /* Find the redefined name of symbol SOURCE. */
981
982 static const char *
983 lookup_sym_redefinition (const char *source)
984 {
985 struct redefine_node *list;
986
987 for (list = redefine_sym_list; list != NULL; list = list->next)
988 if (strcmp (source, list->source) == 0)
989 return list->target;
990
991 return source;
992 }
993
994 /* Add a node to a symbol redefine list. */
995
996 static void
997 redefine_list_append (const char *cause, const char *source, const char *target)
998 {
999 struct redefine_node **p;
1000 struct redefine_node *list;
1001 struct redefine_node *new_node;
1002
1003 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1004 {
1005 if (strcmp (source, list->source) == 0)
1006 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1007 cause, source);
1008
1009 if (strcmp (target, list->target) == 0)
1010 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1011 cause, target);
1012 }
1013
1014 new_node = xmalloc (sizeof (struct redefine_node));
1015
1016 new_node->source = strdup (source);
1017 new_node->target = strdup (target);
1018 new_node->next = NULL;
1019
1020 *p = new_node;
1021 }
1022
1023 /* Handle the --redefine-syms option. Read lines containing "old new"
1024 from the file, and add them to the symbol redefine list. */
1025
1026 static void
1027 add_redefine_syms_file (const char *filename)
1028 {
1029 FILE *file;
1030 char *buf;
1031 size_t bufsize;
1032 size_t len;
1033 size_t outsym_off;
1034 int c, lineno;
1035
1036 file = fopen (filename, "r");
1037 if (file == NULL)
1038 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1039 filename, strerror (errno));
1040
1041 bufsize = 100;
1042 buf = xmalloc (bufsize);
1043
1044 lineno = 1;
1045 c = getc (file);
1046 len = 0;
1047 outsym_off = 0;
1048 while (c != EOF)
1049 {
1050 /* Collect the input symbol name. */
1051 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1052 {
1053 if (c == '#')
1054 goto comment;
1055 buf[len++] = c;
1056 if (len >= bufsize)
1057 {
1058 bufsize *= 2;
1059 buf = xrealloc (buf, bufsize);
1060 }
1061 c = getc (file);
1062 }
1063 buf[len++] = '\0';
1064 if (c == EOF)
1065 break;
1066
1067 /* Eat white space between the symbol names. */
1068 while (IS_WHITESPACE (c))
1069 c = getc (file);
1070 if (c == '#' || IS_LINE_TERMINATOR (c))
1071 goto comment;
1072 if (c == EOF)
1073 break;
1074
1075 /* Collect the output symbol name. */
1076 outsym_off = len;
1077 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1078 {
1079 if (c == '#')
1080 goto comment;
1081 buf[len++] = c;
1082 if (len >= bufsize)
1083 {
1084 bufsize *= 2;
1085 buf = xrealloc (buf, bufsize);
1086 }
1087 c = getc (file);
1088 }
1089 buf[len++] = '\0';
1090 if (c == EOF)
1091 break;
1092
1093 /* Eat white space at end of line. */
1094 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1095 c = getc (file);
1096 if (c == '#')
1097 goto comment;
1098 /* Handle \r\n. */
1099 if ((c == '\r' && (c = getc (file)) == '\n')
1100 || c == '\n' || c == EOF)
1101 {
1102 end_of_line:
1103 /* Append the redefinition to the list. */
1104 if (buf[0] != '\0')
1105 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1106
1107 lineno++;
1108 len = 0;
1109 outsym_off = 0;
1110 if (c == EOF)
1111 break;
1112 c = getc (file);
1113 continue;
1114 }
1115 else
1116 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1117 comment:
1118 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1119 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1120 buf[len++] = '\0';
1121
1122 /* Eat the rest of the line and finish it. */
1123 while (c != '\n' && c != EOF)
1124 c = getc (file);
1125 goto end_of_line;
1126 }
1127
1128 if (len != 0)
1129 fatal (_("%s:%d: premature end of file"), filename, lineno);
1130
1131 free (buf);
1132 }
1133
1134 /* Copy unkown object file IBFD onto OBFD.
1135 Returns TRUE upon success, FALSE otherwise. */
1136
1137 static bfd_boolean
1138 copy_unknown_object (bfd *ibfd, bfd *obfd)
1139 {
1140 char *cbuf;
1141 int tocopy;
1142 long ncopied;
1143 long size;
1144 struct stat buf;
1145
1146 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1147 {
1148 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1149 return FALSE;
1150 }
1151
1152 size = buf.st_size;
1153 if (size < 0)
1154 {
1155 non_fatal (_("stat returns negative size for `%s'"),
1156 bfd_get_archive_filename (ibfd));
1157 return FALSE;
1158 }
1159
1160 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1161 {
1162 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1163 return FALSE;
1164 }
1165
1166 if (verbose)
1167 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1168 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1169
1170 cbuf = xmalloc (BUFSIZE);
1171 ncopied = 0;
1172 while (ncopied < size)
1173 {
1174 tocopy = size - ncopied;
1175 if (tocopy > BUFSIZE)
1176 tocopy = BUFSIZE;
1177
1178 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1179 != (bfd_size_type) tocopy)
1180 {
1181 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1182 free (cbuf);
1183 return FALSE;
1184 }
1185
1186 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1187 != (bfd_size_type) tocopy)
1188 {
1189 bfd_nonfatal (bfd_get_filename (obfd));
1190 free (cbuf);
1191 return FALSE;
1192 }
1193
1194 ncopied += tocopy;
1195 }
1196
1197 chmod (bfd_get_filename (obfd), buf.st_mode);
1198 free (cbuf);
1199 return TRUE;
1200 }
1201
1202 /* Copy object file IBFD onto OBFD.
1203 Returns TRUE upon success, FALSE otherwise. */
1204
1205 static bfd_boolean
1206 copy_object (bfd *ibfd, bfd *obfd)
1207 {
1208 bfd_vma start;
1209 long symcount;
1210 asection **osections = NULL;
1211 asection *gnu_debuglink_section = NULL;
1212 bfd_size_type *gaps = NULL;
1213 bfd_size_type max_gap = 0;
1214 long symsize;
1215 void *dhandle;
1216 enum bfd_architecture iarch;
1217 unsigned int imach;
1218
1219 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1220 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1221 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1222 fatal (_("Unable to change endianness of input file(s)"));
1223
1224 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1225 {
1226 bfd_nonfatal (bfd_get_filename (obfd));
1227 return FALSE;
1228 }
1229
1230 if (verbose)
1231 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1232 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1233 bfd_get_filename (obfd), bfd_get_target (obfd));
1234
1235 if (set_start_set)
1236 start = set_start;
1237 else
1238 start = bfd_get_start_address (ibfd);
1239 start += change_start;
1240
1241 /* Neither the start address nor the flags
1242 need to be set for a core file. */
1243 if (bfd_get_format (obfd) != bfd_core)
1244 {
1245 flagword flags;
1246
1247 flags = bfd_get_file_flags (ibfd);
1248 flags |= bfd_flags_to_set;
1249 flags &= ~bfd_flags_to_clear;
1250 flags &= bfd_applicable_file_flags (obfd);
1251
1252 if (!bfd_set_start_address (obfd, start)
1253 || !bfd_set_file_flags (obfd, flags))
1254 {
1255 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1256 return FALSE;
1257 }
1258 }
1259
1260 /* Copy architecture of input file to output file. */
1261 iarch = bfd_get_arch (ibfd);
1262 imach = bfd_get_mach (ibfd);
1263 if (!bfd_set_arch_mach (obfd, iarch, imach)
1264 && (ibfd->target_defaulted
1265 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1266 {
1267 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1268 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1269 bfd_get_archive_filename (ibfd));
1270 else
1271 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1272 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1273 bfd_get_mach (ibfd)));
1274 return FALSE;
1275 }
1276
1277 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1278 {
1279 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1280 return FALSE;
1281 }
1282
1283 if (isympp)
1284 free (isympp);
1285
1286 if (osympp != isympp)
1287 free (osympp);
1288
1289 isympp = NULL;
1290 osympp = NULL;
1291
1292 /* BFD mandates that all output sections be created and sizes set before
1293 any output is done. Thus, we traverse all sections multiple times. */
1294 bfd_map_over_sections (ibfd, setup_section, obfd);
1295
1296 setup_bfd_headers (ibfd, obfd);
1297
1298 if (add_sections != NULL)
1299 {
1300 struct section_add *padd;
1301 struct section_list *pset;
1302
1303 for (padd = add_sections; padd != NULL; padd = padd->next)
1304 {
1305 flagword flags;
1306
1307 pset = find_section_list (padd->name, FALSE);
1308 if (pset != NULL)
1309 pset->used = TRUE;
1310
1311 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1312 if (pset != NULL && pset->set_flags)
1313 flags = pset->flags | SEC_HAS_CONTENTS;
1314
1315 padd->section = bfd_make_section_with_flags (obfd, padd->name, flags);
1316 if (padd->section == NULL)
1317 {
1318 non_fatal (_("can't create section `%s': %s"),
1319 padd->name, bfd_errmsg (bfd_get_error ()));
1320 return FALSE;
1321 }
1322
1323 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1324 {
1325 bfd_nonfatal (bfd_get_filename (obfd));
1326 return FALSE;
1327 }
1328
1329 if (pset != NULL)
1330 {
1331 if (pset->change_vma != CHANGE_IGNORE)
1332 if (! bfd_set_section_vma (obfd, padd->section,
1333 pset->vma_val))
1334 {
1335 bfd_nonfatal (bfd_get_filename (obfd));
1336 return FALSE;
1337 }
1338
1339 if (pset->change_lma != CHANGE_IGNORE)
1340 {
1341 padd->section->lma = pset->lma_val;
1342
1343 if (! bfd_set_section_alignment
1344 (obfd, padd->section,
1345 bfd_section_alignment (obfd, padd->section)))
1346 {
1347 bfd_nonfatal (bfd_get_filename (obfd));
1348 return FALSE;
1349 }
1350 }
1351 }
1352 }
1353 }
1354
1355 if (gnu_debuglink_filename != NULL)
1356 {
1357 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1358 (obfd, gnu_debuglink_filename);
1359
1360 if (gnu_debuglink_section == NULL)
1361 {
1362 bfd_nonfatal (gnu_debuglink_filename);
1363 return FALSE;
1364 }
1365 }
1366
1367 if (bfd_count_sections (obfd) == 0)
1368 {
1369 non_fatal (_("there are no sections to be copied!"));
1370 return FALSE;
1371 }
1372
1373 if (gap_fill_set || pad_to_set)
1374 {
1375 asection **set;
1376 unsigned int c, i;
1377
1378 /* We must fill in gaps between the sections and/or we must pad
1379 the last section to a specified address. We do this by
1380 grabbing a list of the sections, sorting them by VMA, and
1381 increasing the section sizes as required to fill the gaps.
1382 We write out the gap contents below. */
1383
1384 c = bfd_count_sections (obfd);
1385 osections = xmalloc (c * sizeof (asection *));
1386 set = osections;
1387 bfd_map_over_sections (obfd, get_sections, &set);
1388
1389 qsort (osections, c, sizeof (asection *), compare_section_lma);
1390
1391 gaps = xmalloc (c * sizeof (bfd_size_type));
1392 memset (gaps, 0, c * sizeof (bfd_size_type));
1393
1394 if (gap_fill_set)
1395 {
1396 for (i = 0; i < c - 1; i++)
1397 {
1398 flagword flags;
1399 bfd_size_type size;
1400 bfd_vma gap_start, gap_stop;
1401
1402 flags = bfd_get_section_flags (obfd, osections[i]);
1403 if ((flags & SEC_HAS_CONTENTS) == 0
1404 || (flags & SEC_LOAD) == 0)
1405 continue;
1406
1407 size = bfd_section_size (obfd, osections[i]);
1408 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1409 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1410 if (gap_start < gap_stop)
1411 {
1412 if (! bfd_set_section_size (obfd, osections[i],
1413 size + (gap_stop - gap_start)))
1414 {
1415 non_fatal (_("Can't fill gap after %s: %s"),
1416 bfd_get_section_name (obfd, osections[i]),
1417 bfd_errmsg (bfd_get_error ()));
1418 status = 1;
1419 break;
1420 }
1421 gaps[i] = gap_stop - gap_start;
1422 if (max_gap < gap_stop - gap_start)
1423 max_gap = gap_stop - gap_start;
1424 }
1425 }
1426 }
1427
1428 if (pad_to_set)
1429 {
1430 bfd_vma lma;
1431 bfd_size_type size;
1432
1433 lma = bfd_section_lma (obfd, osections[c - 1]);
1434 size = bfd_section_size (obfd, osections[c - 1]);
1435 if (lma + size < pad_to)
1436 {
1437 if (! bfd_set_section_size (obfd, osections[c - 1],
1438 pad_to - lma))
1439 {
1440 non_fatal (_("Can't add padding to %s: %s"),
1441 bfd_get_section_name (obfd, osections[c - 1]),
1442 bfd_errmsg (bfd_get_error ()));
1443 status = 1;
1444 }
1445 else
1446 {
1447 gaps[c - 1] = pad_to - (lma + size);
1448 if (max_gap < pad_to - (lma + size))
1449 max_gap = pad_to - (lma + size);
1450 }
1451 }
1452 }
1453 }
1454
1455 /* Symbol filtering must happen after the output sections
1456 have been created, but before their contents are set. */
1457 dhandle = NULL;
1458 symsize = bfd_get_symtab_upper_bound (ibfd);
1459 if (symsize < 0)
1460 {
1461 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1462 return FALSE;
1463 }
1464
1465 osympp = isympp = xmalloc (symsize);
1466 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1467 if (symcount < 0)
1468 {
1469 bfd_nonfatal (bfd_get_filename (ibfd));
1470 return FALSE;
1471 }
1472
1473 if (convert_debugging)
1474 dhandle = read_debugging_info (ibfd, isympp, symcount);
1475
1476 if (strip_symbols == STRIP_DEBUG
1477 || strip_symbols == STRIP_ALL
1478 || strip_symbols == STRIP_UNNEEDED
1479 || strip_symbols == STRIP_NONDEBUG
1480 || discard_locals != LOCALS_UNDEF
1481 || strip_specific_list != NULL
1482 || keep_specific_list != NULL
1483 || localize_specific_list != NULL
1484 || globalize_specific_list != NULL
1485 || keepglobal_specific_list != NULL
1486 || weaken_specific_list != NULL
1487 || prefix_symbols_string
1488 || sections_removed
1489 || sections_copied
1490 || convert_debugging
1491 || change_leading_char
1492 || remove_leading_char
1493 || redefine_sym_list
1494 || weaken)
1495 {
1496 /* Mark symbols used in output relocations so that they
1497 are kept, even if they are local labels or static symbols.
1498
1499 Note we iterate over the input sections examining their
1500 relocations since the relocations for the output sections
1501 haven't been set yet. mark_symbols_used_in_relocations will
1502 ignore input sections which have no corresponding output
1503 section. */
1504 if (strip_symbols != STRIP_ALL)
1505 bfd_map_over_sections (ibfd,
1506 mark_symbols_used_in_relocations,
1507 isympp);
1508 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1509 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1510 }
1511
1512 if (convert_debugging && dhandle != NULL)
1513 {
1514 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1515 {
1516 status = 1;
1517 return FALSE;
1518 }
1519 }
1520
1521 bfd_set_symtab (obfd, osympp, symcount);
1522
1523 /* This has to happen after the symbol table has been set. */
1524 bfd_map_over_sections (ibfd, copy_section, obfd);
1525
1526 if (add_sections != NULL)
1527 {
1528 struct section_add *padd;
1529
1530 for (padd = add_sections; padd != NULL; padd = padd->next)
1531 {
1532 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1533 0, padd->size))
1534 {
1535 bfd_nonfatal (bfd_get_filename (obfd));
1536 return FALSE;
1537 }
1538 }
1539 }
1540
1541 if (gnu_debuglink_filename != NULL)
1542 {
1543 if (! bfd_fill_in_gnu_debuglink_section
1544 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1545 {
1546 bfd_nonfatal (gnu_debuglink_filename);
1547 return FALSE;
1548 }
1549 }
1550
1551 if (gap_fill_set || pad_to_set)
1552 {
1553 bfd_byte *buf;
1554 int c, i;
1555
1556 /* Fill in the gaps. */
1557 if (max_gap > 8192)
1558 max_gap = 8192;
1559 buf = xmalloc (max_gap);
1560 memset (buf, gap_fill, max_gap);
1561
1562 c = bfd_count_sections (obfd);
1563 for (i = 0; i < c; i++)
1564 {
1565 if (gaps[i] != 0)
1566 {
1567 bfd_size_type left;
1568 file_ptr off;
1569
1570 left = gaps[i];
1571 off = bfd_section_size (obfd, osections[i]) - left;
1572
1573 while (left > 0)
1574 {
1575 bfd_size_type now;
1576
1577 if (left > 8192)
1578 now = 8192;
1579 else
1580 now = left;
1581
1582 if (! bfd_set_section_contents (obfd, osections[i], buf,
1583 off, now))
1584 {
1585 bfd_nonfatal (bfd_get_filename (obfd));
1586 return FALSE;
1587 }
1588
1589 left -= now;
1590 off += now;
1591 }
1592 }
1593 }
1594 }
1595
1596 /* Allow the BFD backend to copy any private data it understands
1597 from the input BFD to the output BFD. This is done last to
1598 permit the routine to look at the filtered symbol table, which is
1599 important for the ECOFF code at least. */
1600 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1601 && strip_symbols == STRIP_NONDEBUG)
1602 /* Do not copy the private data when creating an ELF format
1603 debug info file. We do not want the program headers. */
1604 ;
1605 else if (! bfd_copy_private_bfd_data (ibfd, obfd))
1606 {
1607 non_fatal (_("%s: error copying private BFD data: %s"),
1608 bfd_get_filename (obfd),
1609 bfd_errmsg (bfd_get_error ()));
1610 return FALSE;
1611 }
1612
1613 /* Switch to the alternate machine code. We have to do this at the
1614 very end, because we only initialize the header when we create
1615 the first section. */
1616 if (use_alt_mach_code != 0
1617 && ! bfd_alt_mach_code (obfd, use_alt_mach_code))
1618 non_fatal (_("unknown alternate machine code, ignored"));
1619
1620 return TRUE;
1621 }
1622
1623 #undef MKDIR
1624 #if defined (_WIN32) && !defined (__CYGWIN32__)
1625 #define MKDIR(DIR, MODE) mkdir (DIR)
1626 #else
1627 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1628 #endif
1629
1630 /* Read each archive element in turn from IBFD, copy the
1631 contents to temp file, and keep the temp file handle. */
1632
1633 static void
1634 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target)
1635 {
1636 struct name_list
1637 {
1638 struct name_list *next;
1639 const char *name;
1640 bfd *obfd;
1641 } *list, *l;
1642 bfd **ptr = &obfd->archive_head;
1643 bfd *this_element;
1644 char *dir = make_tempname (bfd_get_filename (obfd));
1645
1646 /* Make a temp directory to hold the contents. */
1647 if (MKDIR (dir, 0700) != 0)
1648 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1649 dir, strerror (errno));
1650
1651 obfd->has_armap = ibfd->has_armap;
1652
1653 list = NULL;
1654
1655 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1656
1657 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1658 RETURN_NONFATAL (bfd_get_filename (obfd));
1659
1660 while (!status && this_element != NULL)
1661 {
1662 char *output_name;
1663 bfd *output_bfd;
1664 bfd *last_element;
1665 struct stat buf;
1666 int stat_status = 0;
1667 bfd_boolean delete = TRUE;
1668
1669 /* Create an output file for this member. */
1670 output_name = concat (dir, "/",
1671 bfd_get_filename (this_element), (char *) 0);
1672
1673 /* If the file already exists, make another temp dir. */
1674 if (stat (output_name, &buf) >= 0)
1675 {
1676 output_name = make_tempname (output_name);
1677 if (MKDIR (output_name, 0700) != 0)
1678 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1679 output_name, strerror (errno));
1680
1681 l = xmalloc (sizeof (struct name_list));
1682 l->name = output_name;
1683 l->next = list;
1684 l->obfd = NULL;
1685 list = l;
1686 output_name = concat (output_name, "/",
1687 bfd_get_filename (this_element), (char *) 0);
1688 }
1689
1690 output_bfd = bfd_openw (output_name, output_target);
1691 if (preserve_dates)
1692 {
1693 stat_status = bfd_stat_arch_elt (this_element, &buf);
1694
1695 if (stat_status != 0)
1696 non_fatal (_("internal stat error on %s"),
1697 bfd_get_filename (this_element));
1698 }
1699
1700 l = xmalloc (sizeof (struct name_list));
1701 l->name = output_name;
1702 l->next = list;
1703 l->obfd = NULL;
1704 list = l;
1705
1706 if (output_bfd == NULL)
1707 RETURN_NONFATAL (output_name);
1708
1709 if (bfd_check_format (this_element, bfd_object))
1710 {
1711 delete = ! copy_object (this_element, output_bfd);
1712
1713 if (! delete
1714 || bfd_get_arch (this_element) != bfd_arch_unknown)
1715 {
1716 if (!bfd_close (output_bfd))
1717 {
1718 bfd_nonfatal (bfd_get_filename (output_bfd));
1719 /* Error in new object file. Don't change archive. */
1720 status = 1;
1721 }
1722 }
1723 else
1724 goto copy_unknown_element;
1725 }
1726 else
1727 {
1728 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1729 bfd_get_archive_filename (this_element));
1730
1731 copy_unknown_element:
1732 delete = !copy_unknown_object (this_element, output_bfd);
1733 if (!bfd_close_all_done (output_bfd))
1734 {
1735 bfd_nonfatal (bfd_get_filename (output_bfd));
1736 /* Error in new object file. Don't change archive. */
1737 status = 1;
1738 }
1739 }
1740
1741 if (delete)
1742 {
1743 unlink (output_name);
1744 status = 1;
1745 }
1746 else
1747 {
1748 if (preserve_dates && stat_status == 0)
1749 set_times (output_name, &buf);
1750
1751 /* Open the newly output file and attach to our list. */
1752 output_bfd = bfd_openr (output_name, output_target);
1753
1754 l->obfd = output_bfd;
1755
1756 *ptr = output_bfd;
1757 ptr = &output_bfd->next;
1758
1759 last_element = this_element;
1760
1761 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1762
1763 bfd_close (last_element);
1764 }
1765 }
1766 *ptr = NULL;
1767
1768 if (!bfd_close (obfd))
1769 RETURN_NONFATAL (bfd_get_filename (obfd));
1770
1771 if (!bfd_close (ibfd))
1772 RETURN_NONFATAL (bfd_get_filename (ibfd));
1773
1774 /* Delete all the files that we opened. */
1775 for (l = list; l != NULL; l = l->next)
1776 {
1777 if (l->obfd == NULL)
1778 rmdir (l->name);
1779 else
1780 {
1781 bfd_close (l->obfd);
1782 unlink (l->name);
1783 }
1784 }
1785 rmdir (dir);
1786 }
1787
1788 /* The top-level control. */
1789
1790 static void
1791 copy_file (const char *input_filename, const char *output_filename,
1792 const char *input_target, const char *output_target)
1793 {
1794 bfd *ibfd;
1795 char **obj_matching;
1796 char **core_matching;
1797
1798 if (get_file_size (input_filename) < 1)
1799 {
1800 non_fatal (_("error: the input file '%s' is empty"), input_filename);
1801 status = 1;
1802 return;
1803 }
1804
1805 /* To allow us to do "strip *" without dying on the first
1806 non-object file, failures are nonfatal. */
1807 ibfd = bfd_openr (input_filename, input_target);
1808 if (ibfd == NULL)
1809 RETURN_NONFATAL (input_filename);
1810
1811 if (bfd_check_format (ibfd, bfd_archive))
1812 {
1813 bfd *obfd;
1814
1815 /* bfd_get_target does not return the correct value until
1816 bfd_check_format succeeds. */
1817 if (output_target == NULL)
1818 output_target = bfd_get_target (ibfd);
1819
1820 obfd = bfd_openw (output_filename, output_target);
1821 if (obfd == NULL)
1822 RETURN_NONFATAL (output_filename);
1823
1824 copy_archive (ibfd, obfd, output_target);
1825 }
1826 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
1827 {
1828 bfd *obfd;
1829 do_copy:
1830
1831 /* bfd_get_target does not return the correct value until
1832 bfd_check_format succeeds. */
1833 if (output_target == NULL)
1834 output_target = bfd_get_target (ibfd);
1835
1836 obfd = bfd_openw (output_filename, output_target);
1837 if (obfd == NULL)
1838 RETURN_NONFATAL (output_filename);
1839
1840 if (! copy_object (ibfd, obfd))
1841 status = 1;
1842
1843 if (!bfd_close (obfd))
1844 RETURN_NONFATAL (output_filename);
1845
1846 if (!bfd_close (ibfd))
1847 RETURN_NONFATAL (input_filename);
1848
1849 }
1850 else
1851 {
1852 bfd_error_type obj_error = bfd_get_error ();
1853 bfd_error_type core_error;
1854
1855 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1856 {
1857 /* This probably can't happen.. */
1858 if (obj_error == bfd_error_file_ambiguously_recognized)
1859 free (obj_matching);
1860 goto do_copy;
1861 }
1862
1863 core_error = bfd_get_error ();
1864 /* Report the object error in preference to the core error. */
1865 if (obj_error != core_error)
1866 bfd_set_error (obj_error);
1867
1868 bfd_nonfatal (input_filename);
1869
1870 if (obj_error == bfd_error_file_ambiguously_recognized)
1871 {
1872 list_matching_formats (obj_matching);
1873 free (obj_matching);
1874 }
1875 if (core_error == bfd_error_file_ambiguously_recognized)
1876 {
1877 list_matching_formats (core_matching);
1878 free (core_matching);
1879 }
1880
1881 status = 1;
1882 }
1883 }
1884
1885 /* Add a name to the section renaming list. */
1886
1887 static void
1888 add_section_rename (const char * old_name, const char * new_name,
1889 flagword flags)
1890 {
1891 section_rename * rename;
1892
1893 /* Check for conflicts first. */
1894 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1895 if (strcmp (rename->old_name, old_name) == 0)
1896 {
1897 /* Silently ignore duplicate definitions. */
1898 if (strcmp (rename->new_name, new_name) == 0
1899 && rename->flags == flags)
1900 return;
1901
1902 fatal (_("Multiple renames of section %s"), old_name);
1903 }
1904
1905 rename = xmalloc (sizeof (* rename));
1906
1907 rename->old_name = old_name;
1908 rename->new_name = new_name;
1909 rename->flags = flags;
1910 rename->next = section_rename_list;
1911
1912 section_rename_list = rename;
1913 }
1914
1915 /* Check the section rename list for a new name of the input section
1916 ISECTION. Return the new name if one is found.
1917 Also set RETURNED_FLAGS to the flags to be used for this section. */
1918
1919 static const char *
1920 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
1921 flagword * returned_flags)
1922 {
1923 const char * old_name = bfd_section_name (ibfd, isection);
1924 section_rename * rename;
1925
1926 /* Default to using the flags of the input section. */
1927 * returned_flags = bfd_get_section_flags (ibfd, isection);
1928
1929 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1930 if (strcmp (rename->old_name, old_name) == 0)
1931 {
1932 if (rename->flags != (flagword) -1)
1933 * returned_flags = rename->flags;
1934
1935 return rename->new_name;
1936 }
1937
1938 return old_name;
1939 }
1940
1941 /* Once each of the sections is copied, we may still need to do some
1942 finalization work for private section headers. Do that here. */
1943
1944 static void
1945 setup_bfd_headers (bfd *ibfd, bfd *obfd)
1946 {
1947 const char *err;
1948
1949 /* Allow the BFD backend to copy any private data it understands
1950 from the input section to the output section. */
1951 if (! bfd_copy_private_header_data (ibfd, obfd))
1952 {
1953 err = _("private header data");
1954 goto loser;
1955 }
1956
1957 /* All went well. */
1958 return;
1959
1960 loser:
1961 non_fatal (_("%s: error in %s: %s"),
1962 bfd_get_filename (ibfd),
1963 err, bfd_errmsg (bfd_get_error ()));
1964 status = 1;
1965 }
1966
1967 /* Create a section in OBFD with the same
1968 name and attributes as ISECTION in IBFD. */
1969
1970 static void
1971 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
1972 {
1973 bfd *obfd = obfdarg;
1974 struct section_list *p;
1975 sec_ptr osection;
1976 bfd_size_type size;
1977 bfd_vma vma;
1978 bfd_vma lma;
1979 flagword flags;
1980 const char *err;
1981 const char * name;
1982 char *prefix = NULL;
1983
1984 if (is_strip_section (ibfd, isection))
1985 return;
1986
1987 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
1988 if (p != NULL)
1989 p->used = TRUE;
1990
1991 /* Get the, possibly new, name of the output section. */
1992 name = find_section_rename (ibfd, isection, & flags);
1993
1994 /* Prefix sections. */
1995 if ((prefix_alloc_sections_string)
1996 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
1997 prefix = prefix_alloc_sections_string;
1998 else if (prefix_sections_string)
1999 prefix = prefix_sections_string;
2000
2001 if (prefix)
2002 {
2003 char *n;
2004
2005 n = xmalloc (strlen (prefix) + strlen (name) + 1);
2006 strcpy (n, prefix);
2007 strcat (n, name);
2008 name = n;
2009 }
2010
2011 if (p != NULL && p->set_flags)
2012 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2013 else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
2014 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2015
2016 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2017
2018 if (osection == NULL)
2019 {
2020 err = _("making");
2021 goto loser;
2022 }
2023
2024 if (strip_symbols == STRIP_NONDEBUG
2025 && obfd->xvec->flavour == bfd_target_elf_flavour
2026 && (flags & SEC_ALLOC) != 0
2027 && (p == NULL || !p->set_flags))
2028 elf_section_type (osection) = SHT_NOBITS;
2029
2030 size = bfd_section_size (ibfd, isection);
2031 if (copy_byte >= 0)
2032 size = (size + interleave - 1) / interleave;
2033 if (! bfd_set_section_size (obfd, osection, size))
2034 {
2035 err = _("size");
2036 goto loser;
2037 }
2038
2039 vma = bfd_section_vma (ibfd, isection);
2040 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2041 vma += p->vma_val;
2042 else if (p != NULL && p->change_vma == CHANGE_SET)
2043 vma = p->vma_val;
2044 else
2045 vma += change_section_address;
2046
2047 if (! bfd_set_section_vma (obfd, osection, vma))
2048 {
2049 err = _("vma");
2050 goto loser;
2051 }
2052
2053 lma = isection->lma;
2054 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2055 {
2056 if (p->change_lma == CHANGE_MODIFY)
2057 lma += p->lma_val;
2058 else if (p->change_lma == CHANGE_SET)
2059 lma = p->lma_val;
2060 else
2061 abort ();
2062 }
2063 else
2064 lma += change_section_address;
2065
2066 osection->lma = lma;
2067
2068 /* FIXME: This is probably not enough. If we change the LMA we
2069 may have to recompute the header for the file as well. */
2070 if (!bfd_set_section_alignment (obfd,
2071 osection,
2072 bfd_section_alignment (ibfd, isection)))
2073 {
2074 err = _("alignment");
2075 goto loser;
2076 }
2077
2078 /* Copy merge entity size. */
2079 osection->entsize = isection->entsize;
2080
2081 /* This used to be mangle_section; we do here to avoid using
2082 bfd_get_section_by_name since some formats allow multiple
2083 sections with the same name. */
2084 isection->output_section = osection;
2085 isection->output_offset = 0;
2086
2087 /* Allow the BFD backend to copy any private data it understands
2088 from the input section to the output section. */
2089 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
2090 && strip_symbols == STRIP_NONDEBUG)
2091 /* Do not copy the private data when creating an ELF format
2092 debug info file. We do not want the program headers. */
2093 ;
2094 else if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2095 {
2096 err = _("private data");
2097 goto loser;
2098 }
2099
2100 /* All went well. */
2101 return;
2102
2103 loser:
2104 non_fatal (_("%s: section `%s': error in %s: %s"),
2105 bfd_get_filename (ibfd),
2106 bfd_section_name (ibfd, isection),
2107 err, bfd_errmsg (bfd_get_error ()));
2108 status = 1;
2109 }
2110
2111 /* Copy the data of input section ISECTION of IBFD
2112 to an output section with the same name in OBFD.
2113 If stripping then don't copy any relocation info. */
2114
2115 static void
2116 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2117 {
2118 bfd *obfd = obfdarg;
2119 struct section_list *p;
2120 arelent **relpp;
2121 long relcount;
2122 sec_ptr osection;
2123 bfd_size_type size;
2124 long relsize;
2125 flagword flags;
2126
2127 /* If we have already failed earlier on,
2128 do not keep on generating complaints now. */
2129 if (status != 0)
2130 return;
2131
2132 if (is_strip_section (ibfd, isection))
2133 return;
2134
2135 flags = bfd_get_section_flags (ibfd, isection);
2136 if ((flags & SEC_GROUP) != 0)
2137 return;
2138
2139 osection = isection->output_section;
2140 size = bfd_get_section_size (isection);
2141
2142 if (size == 0 || osection == 0)
2143 return;
2144
2145 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2146
2147 /* Core files do not need to be relocated. */
2148 if (bfd_get_format (obfd) == bfd_core)
2149 relsize = 0;
2150 else
2151 {
2152 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2153
2154 if (relsize < 0)
2155 {
2156 /* Do not complain if the target does not support relocations. */
2157 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2158 relsize = 0;
2159 else
2160 RETURN_NONFATAL (bfd_get_filename (ibfd));
2161 }
2162 }
2163
2164 if (relsize == 0)
2165 bfd_set_reloc (obfd, osection, NULL, 0);
2166 else
2167 {
2168 relpp = xmalloc (relsize);
2169 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2170 if (relcount < 0)
2171 RETURN_NONFATAL (bfd_get_filename (ibfd));
2172
2173 if (strip_symbols == STRIP_ALL)
2174 {
2175 /* Remove relocations which are not in
2176 keep_strip_specific_list. */
2177 arelent **temp_relpp;
2178 long temp_relcount = 0;
2179 long i;
2180
2181 temp_relpp = xmalloc (relsize);
2182 for (i = 0; i < relcount; i++)
2183 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2184 keep_specific_list))
2185 temp_relpp [temp_relcount++] = relpp [i];
2186 relcount = temp_relcount;
2187 free (relpp);
2188 relpp = temp_relpp;
2189 }
2190
2191 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2192 if (relcount == 0)
2193 free (relpp);
2194 }
2195
2196 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2197 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2198 {
2199 void *memhunk = xmalloc (size);
2200
2201 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2202 RETURN_NONFATAL (bfd_get_filename (ibfd));
2203
2204 if (copy_byte >= 0)
2205 {
2206 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2207 char *from = (char *) memhunk + copy_byte;
2208 char *to = memhunk;
2209 char *end = (char *) memhunk + size;
2210
2211 for (; from < end; from += interleave)
2212 *to++ = *from;
2213
2214 size = (size + interleave - 1 - copy_byte) / interleave;
2215 osection->lma /= interleave;
2216 }
2217
2218 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2219 RETURN_NONFATAL (bfd_get_filename (obfd));
2220
2221 free (memhunk);
2222 }
2223 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2224 {
2225 void *memhunk = xmalloc (size);
2226
2227 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2228 flag--they can just remove the section entirely and add it
2229 back again. However, we do permit them to turn on the
2230 SEC_HAS_CONTENTS flag, and take it to mean that the section
2231 contents should be zeroed out. */
2232
2233 memset (memhunk, 0, size);
2234 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2235 RETURN_NONFATAL (bfd_get_filename (obfd));
2236 free (memhunk);
2237 }
2238 }
2239
2240 /* Get all the sections. This is used when --gap-fill or --pad-to is
2241 used. */
2242
2243 static void
2244 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2245 {
2246 asection ***secppp = secppparg;
2247
2248 **secppp = osection;
2249 ++(*secppp);
2250 }
2251
2252 /* Sort sections by VMA. This is called via qsort, and is used when
2253 --gap-fill or --pad-to is used. We force non loadable or empty
2254 sections to the front, where they are easier to ignore. */
2255
2256 static int
2257 compare_section_lma (const void *arg1, const void *arg2)
2258 {
2259 const asection *const *sec1 = arg1;
2260 const asection *const *sec2 = arg2;
2261 flagword flags1, flags2;
2262
2263 /* Sort non loadable sections to the front. */
2264 flags1 = (*sec1)->flags;
2265 flags2 = (*sec2)->flags;
2266 if ((flags1 & SEC_HAS_CONTENTS) == 0
2267 || (flags1 & SEC_LOAD) == 0)
2268 {
2269 if ((flags2 & SEC_HAS_CONTENTS) != 0
2270 && (flags2 & SEC_LOAD) != 0)
2271 return -1;
2272 }
2273 else
2274 {
2275 if ((flags2 & SEC_HAS_CONTENTS) == 0
2276 || (flags2 & SEC_LOAD) == 0)
2277 return 1;
2278 }
2279
2280 /* Sort sections by LMA. */
2281 if ((*sec1)->lma > (*sec2)->lma)
2282 return 1;
2283 else if ((*sec1)->lma < (*sec2)->lma)
2284 return -1;
2285
2286 /* Sort sections with the same LMA by size. */
2287 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2288 return 1;
2289 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2290 return -1;
2291
2292 return 0;
2293 }
2294
2295 /* Mark all the symbols which will be used in output relocations with
2296 the BSF_KEEP flag so that those symbols will not be stripped.
2297
2298 Ignore relocations which will not appear in the output file. */
2299
2300 static void
2301 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2302 {
2303 asymbol **symbols = symbolsarg;
2304 long relsize;
2305 arelent **relpp;
2306 long relcount, i;
2307
2308 /* Ignore an input section with no corresponding output section. */
2309 if (isection->output_section == NULL)
2310 return;
2311
2312 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2313 if (relsize < 0)
2314 {
2315 /* Do not complain if the target does not support relocations. */
2316 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2317 return;
2318 bfd_fatal (bfd_get_filename (ibfd));
2319 }
2320
2321 if (relsize == 0)
2322 return;
2323
2324 relpp = xmalloc (relsize);
2325 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2326 if (relcount < 0)
2327 bfd_fatal (bfd_get_filename (ibfd));
2328
2329 /* Examine each symbol used in a relocation. If it's not one of the
2330 special bfd section symbols, then mark it with BSF_KEEP. */
2331 for (i = 0; i < relcount; i++)
2332 {
2333 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2334 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2335 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2336 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2337 }
2338
2339 if (relpp != NULL)
2340 free (relpp);
2341 }
2342
2343 /* Write out debugging information. */
2344
2345 static bfd_boolean
2346 write_debugging_info (bfd *obfd, void *dhandle,
2347 long *symcountp ATTRIBUTE_UNUSED,
2348 asymbol ***symppp ATTRIBUTE_UNUSED)
2349 {
2350 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2351 return write_ieee_debugging_info (obfd, dhandle);
2352
2353 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2354 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2355 {
2356 bfd_byte *syms, *strings;
2357 bfd_size_type symsize, stringsize;
2358 asection *stabsec, *stabstrsec;
2359 flagword flags;
2360
2361 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2362 &symsize, &strings,
2363 &stringsize))
2364 return FALSE;
2365
2366 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2367 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2368 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2369 if (stabsec == NULL
2370 || stabstrsec == NULL
2371 || ! bfd_set_section_size (obfd, stabsec, symsize)
2372 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2373 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2374 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2375 {
2376 non_fatal (_("%s: can't create debugging section: %s"),
2377 bfd_get_filename (obfd),
2378 bfd_errmsg (bfd_get_error ()));
2379 return FALSE;
2380 }
2381
2382 /* We can get away with setting the section contents now because
2383 the next thing the caller is going to do is copy over the
2384 real sections. We may someday have to split the contents
2385 setting out of this function. */
2386 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2387 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2388 stringsize))
2389 {
2390 non_fatal (_("%s: can't set debugging section contents: %s"),
2391 bfd_get_filename (obfd),
2392 bfd_errmsg (bfd_get_error ()));
2393 return FALSE;
2394 }
2395
2396 return TRUE;
2397 }
2398
2399 non_fatal (_("%s: don't know how to write debugging information for %s"),
2400 bfd_get_filename (obfd), bfd_get_target (obfd));
2401 return FALSE;
2402 }
2403
2404 static int
2405 strip_main (int argc, char *argv[])
2406 {
2407 char *input_target = NULL;
2408 char *output_target = NULL;
2409 bfd_boolean show_version = FALSE;
2410 bfd_boolean formats_info = FALSE;
2411 int c;
2412 int i;
2413 struct section_list *p;
2414 char *output_file = NULL;
2415
2416 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2417 strip_options, (int *) 0)) != EOF)
2418 {
2419 switch (c)
2420 {
2421 case 'I':
2422 input_target = optarg;
2423 break;
2424 case 'O':
2425 output_target = optarg;
2426 break;
2427 case 'F':
2428 input_target = output_target = optarg;
2429 break;
2430 case 'R':
2431 p = find_section_list (optarg, TRUE);
2432 p->remove = TRUE;
2433 sections_removed = TRUE;
2434 break;
2435 case 's':
2436 strip_symbols = STRIP_ALL;
2437 break;
2438 case 'S':
2439 case 'g':
2440 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2441 strip_symbols = STRIP_DEBUG;
2442 break;
2443 case OPTION_STRIP_UNNEEDED:
2444 strip_symbols = STRIP_UNNEEDED;
2445 break;
2446 case 'K':
2447 add_specific_symbol (optarg, &keep_specific_list);
2448 break;
2449 case 'N':
2450 add_specific_symbol (optarg, &strip_specific_list);
2451 break;
2452 case 'o':
2453 output_file = optarg;
2454 break;
2455 case 'p':
2456 preserve_dates = TRUE;
2457 break;
2458 case 'x':
2459 discard_locals = LOCALS_ALL;
2460 break;
2461 case 'X':
2462 discard_locals = LOCALS_START_L;
2463 break;
2464 case 'v':
2465 verbose = TRUE;
2466 break;
2467 case 'V':
2468 show_version = TRUE;
2469 break;
2470 case OPTION_FORMATS_INFO:
2471 formats_info = TRUE;
2472 break;
2473 case OPTION_ONLY_KEEP_DEBUG:
2474 strip_symbols = STRIP_NONDEBUG;
2475 break;
2476 case OPTION_KEEP_FILE_SYMBOLS:
2477 keep_file_symbols = 1;
2478 break;
2479 case 0:
2480 /* We've been given a long option. */
2481 break;
2482 case 'w':
2483 wildcard = TRUE;
2484 break;
2485 case 'H':
2486 case 'h':
2487 strip_usage (stdout, 0);
2488 default:
2489 strip_usage (stderr, 1);
2490 }
2491 }
2492
2493 if (formats_info)
2494 {
2495 display_info ();
2496 return 0;
2497 }
2498
2499 if (show_version)
2500 print_version ("strip");
2501
2502 /* Default is to strip all symbols. */
2503 if (strip_symbols == STRIP_UNDEF
2504 && discard_locals == LOCALS_UNDEF
2505 && strip_specific_list == NULL)
2506 strip_symbols = STRIP_ALL;
2507
2508 if (output_target == NULL)
2509 output_target = input_target;
2510
2511 i = optind;
2512 if (i == argc
2513 || (output_file != NULL && (i + 1) < argc))
2514 strip_usage (stderr, 1);
2515
2516 for (; i < argc; i++)
2517 {
2518 int hold_status = status;
2519 struct stat statbuf;
2520 char *tmpname;
2521
2522 if (get_file_size (argv[i]) < 1)
2523 continue;
2524
2525 if (preserve_dates)
2526 /* No need to check the return value of stat().
2527 It has already been checked in get_file_size(). */
2528 stat (argv[i], &statbuf);
2529
2530 if (output_file != NULL)
2531 tmpname = output_file;
2532 else
2533 tmpname = make_tempname (argv[i]);
2534 status = 0;
2535
2536 copy_file (argv[i], tmpname, input_target, output_target);
2537 if (status == 0)
2538 {
2539 if (preserve_dates)
2540 set_times (tmpname, &statbuf);
2541 if (output_file == NULL)
2542 smart_rename (tmpname, argv[i], preserve_dates);
2543 status = hold_status;
2544 }
2545 else
2546 unlink_if_ordinary (tmpname);
2547 if (output_file == NULL)
2548 free (tmpname);
2549 }
2550
2551 return 0;
2552 }
2553
2554 static int
2555 copy_main (int argc, char *argv[])
2556 {
2557 char * binary_architecture = NULL;
2558 char *input_filename = NULL;
2559 char *output_filename = NULL;
2560 char *input_target = NULL;
2561 char *output_target = NULL;
2562 bfd_boolean show_version = FALSE;
2563 bfd_boolean change_warn = TRUE;
2564 bfd_boolean formats_info = FALSE;
2565 int c;
2566 struct section_list *p;
2567 struct stat statbuf;
2568
2569 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2570 copy_options, (int *) 0)) != EOF)
2571 {
2572 switch (c)
2573 {
2574 case 'b':
2575 copy_byte = atoi (optarg);
2576 if (copy_byte < 0)
2577 fatal (_("byte number must be non-negative"));
2578 break;
2579
2580 case 'B':
2581 binary_architecture = optarg;
2582 break;
2583
2584 case 'i':
2585 interleave = atoi (optarg);
2586 if (interleave < 1)
2587 fatal (_("interleave must be positive"));
2588 break;
2589
2590 case 'I':
2591 case 's': /* "source" - 'I' is preferred */
2592 input_target = optarg;
2593 break;
2594
2595 case 'O':
2596 case 'd': /* "destination" - 'O' is preferred */
2597 output_target = optarg;
2598 break;
2599
2600 case 'F':
2601 input_target = output_target = optarg;
2602 break;
2603
2604 case 'j':
2605 p = find_section_list (optarg, TRUE);
2606 if (p->remove)
2607 fatal (_("%s both copied and removed"), optarg);
2608 p->copy = TRUE;
2609 sections_copied = TRUE;
2610 break;
2611
2612 case 'R':
2613 p = find_section_list (optarg, TRUE);
2614 if (p->copy)
2615 fatal (_("%s both copied and removed"), optarg);
2616 p->remove = TRUE;
2617 sections_removed = TRUE;
2618 break;
2619
2620 case 'S':
2621 strip_symbols = STRIP_ALL;
2622 break;
2623
2624 case 'g':
2625 strip_symbols = STRIP_DEBUG;
2626 break;
2627
2628 case OPTION_STRIP_UNNEEDED:
2629 strip_symbols = STRIP_UNNEEDED;
2630 break;
2631
2632 case OPTION_ONLY_KEEP_DEBUG:
2633 strip_symbols = STRIP_NONDEBUG;
2634 break;
2635
2636 case OPTION_KEEP_FILE_SYMBOLS:
2637 keep_file_symbols = 1;
2638 break;
2639
2640 case OPTION_ADD_GNU_DEBUGLINK:
2641 gnu_debuglink_filename = optarg;
2642 break;
2643
2644 case 'K':
2645 add_specific_symbol (optarg, &keep_specific_list);
2646 break;
2647
2648 case 'N':
2649 add_specific_symbol (optarg, &strip_specific_list);
2650 break;
2651
2652 case OPTION_STRIP_UNNEEDED_SYMBOL:
2653 add_specific_symbol (optarg, &strip_unneeded_list);
2654 break;
2655
2656 case 'L':
2657 add_specific_symbol (optarg, &localize_specific_list);
2658 break;
2659
2660 case OPTION_GLOBALIZE_SYMBOL:
2661 add_specific_symbol (optarg, &globalize_specific_list);
2662 break;
2663
2664 case 'G':
2665 add_specific_symbol (optarg, &keepglobal_specific_list);
2666 break;
2667
2668 case 'W':
2669 add_specific_symbol (optarg, &weaken_specific_list);
2670 break;
2671
2672 case 'p':
2673 preserve_dates = TRUE;
2674 break;
2675
2676 case 'w':
2677 wildcard = TRUE;
2678 break;
2679
2680 case 'x':
2681 discard_locals = LOCALS_ALL;
2682 break;
2683
2684 case 'X':
2685 discard_locals = LOCALS_START_L;
2686 break;
2687
2688 case 'v':
2689 verbose = TRUE;
2690 break;
2691
2692 case 'V':
2693 show_version = TRUE;
2694 break;
2695
2696 case OPTION_FORMATS_INFO:
2697 formats_info = TRUE;
2698 break;
2699
2700 case OPTION_WEAKEN:
2701 weaken = TRUE;
2702 break;
2703
2704 case OPTION_ADD_SECTION:
2705 {
2706 const char *s;
2707 off_t size;
2708 struct section_add *pa;
2709 int len;
2710 char *name;
2711 FILE *f;
2712
2713 s = strchr (optarg, '=');
2714
2715 if (s == NULL)
2716 fatal (_("bad format for %s"), "--add-section");
2717
2718 size = get_file_size (s + 1);
2719 if (size < 1)
2720 break;
2721
2722 pa = xmalloc (sizeof (struct section_add));
2723
2724 len = s - optarg;
2725 name = xmalloc (len + 1);
2726 strncpy (name, optarg, len);
2727 name[len] = '\0';
2728 pa->name = name;
2729
2730 pa->filename = s + 1;
2731 pa->size = size;
2732 pa->contents = xmalloc (size);
2733
2734 f = fopen (pa->filename, FOPEN_RB);
2735
2736 if (f == NULL)
2737 fatal (_("cannot open: %s: %s"),
2738 pa->filename, strerror (errno));
2739
2740 if (fread (pa->contents, 1, pa->size, f) == 0
2741 || ferror (f))
2742 fatal (_("%s: fread failed"), pa->filename);
2743
2744 fclose (f);
2745
2746 pa->next = add_sections;
2747 add_sections = pa;
2748 }
2749 break;
2750
2751 case OPTION_CHANGE_START:
2752 change_start = parse_vma (optarg, "--change-start");
2753 break;
2754
2755 case OPTION_CHANGE_SECTION_ADDRESS:
2756 case OPTION_CHANGE_SECTION_LMA:
2757 case OPTION_CHANGE_SECTION_VMA:
2758 {
2759 const char *s;
2760 int len;
2761 char *name;
2762 char *option = NULL;
2763 bfd_vma val;
2764 enum change_action what = CHANGE_IGNORE;
2765
2766 switch (c)
2767 {
2768 case OPTION_CHANGE_SECTION_ADDRESS:
2769 option = "--change-section-address";
2770 break;
2771 case OPTION_CHANGE_SECTION_LMA:
2772 option = "--change-section-lma";
2773 break;
2774 case OPTION_CHANGE_SECTION_VMA:
2775 option = "--change-section-vma";
2776 break;
2777 }
2778
2779 s = strchr (optarg, '=');
2780 if (s == NULL)
2781 {
2782 s = strchr (optarg, '+');
2783 if (s == NULL)
2784 {
2785 s = strchr (optarg, '-');
2786 if (s == NULL)
2787 fatal (_("bad format for %s"), option);
2788 }
2789 }
2790
2791 len = s - optarg;
2792 name = xmalloc (len + 1);
2793 strncpy (name, optarg, len);
2794 name[len] = '\0';
2795
2796 p = find_section_list (name, TRUE);
2797
2798 val = parse_vma (s + 1, option);
2799
2800 switch (*s)
2801 {
2802 case '=': what = CHANGE_SET; break;
2803 case '-': val = - val; /* Drop through. */
2804 case '+': what = CHANGE_MODIFY; break;
2805 }
2806
2807 switch (c)
2808 {
2809 case OPTION_CHANGE_SECTION_ADDRESS:
2810 p->change_vma = what;
2811 p->vma_val = val;
2812 /* Drop through. */
2813
2814 case OPTION_CHANGE_SECTION_LMA:
2815 p->change_lma = what;
2816 p->lma_val = val;
2817 break;
2818
2819 case OPTION_CHANGE_SECTION_VMA:
2820 p->change_vma = what;
2821 p->vma_val = val;
2822 break;
2823 }
2824 }
2825 break;
2826
2827 case OPTION_CHANGE_ADDRESSES:
2828 change_section_address = parse_vma (optarg, "--change-addresses");
2829 change_start = change_section_address;
2830 break;
2831
2832 case OPTION_CHANGE_WARNINGS:
2833 change_warn = TRUE;
2834 break;
2835
2836 case OPTION_CHANGE_LEADING_CHAR:
2837 change_leading_char = TRUE;
2838 break;
2839
2840 case OPTION_DEBUGGING:
2841 convert_debugging = TRUE;
2842 break;
2843
2844 case OPTION_GAP_FILL:
2845 {
2846 bfd_vma gap_fill_vma;
2847
2848 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2849 gap_fill = (bfd_byte) gap_fill_vma;
2850 if ((bfd_vma) gap_fill != gap_fill_vma)
2851 {
2852 char buff[20];
2853
2854 sprintf_vma (buff, gap_fill_vma);
2855
2856 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2857 buff, gap_fill);
2858 }
2859 gap_fill_set = TRUE;
2860 }
2861 break;
2862
2863 case OPTION_NO_CHANGE_WARNINGS:
2864 change_warn = FALSE;
2865 break;
2866
2867 case OPTION_PAD_TO:
2868 pad_to = parse_vma (optarg, "--pad-to");
2869 pad_to_set = TRUE;
2870 break;
2871
2872 case OPTION_REMOVE_LEADING_CHAR:
2873 remove_leading_char = TRUE;
2874 break;
2875
2876 case OPTION_REDEFINE_SYM:
2877 {
2878 /* Push this redefinition onto redefine_symbol_list. */
2879
2880 int len;
2881 const char *s;
2882 const char *nextarg;
2883 char *source, *target;
2884
2885 s = strchr (optarg, '=');
2886 if (s == NULL)
2887 fatal (_("bad format for %s"), "--redefine-sym");
2888
2889 len = s - optarg;
2890 source = xmalloc (len + 1);
2891 strncpy (source, optarg, len);
2892 source[len] = '\0';
2893
2894 nextarg = s + 1;
2895 len = strlen (nextarg);
2896 target = xmalloc (len + 1);
2897 strcpy (target, nextarg);
2898
2899 redefine_list_append ("--redefine-sym", source, target);
2900
2901 free (source);
2902 free (target);
2903 }
2904 break;
2905
2906 case OPTION_REDEFINE_SYMS:
2907 add_redefine_syms_file (optarg);
2908 break;
2909
2910 case OPTION_SET_SECTION_FLAGS:
2911 {
2912 const char *s;
2913 int len;
2914 char *name;
2915
2916 s = strchr (optarg, '=');
2917 if (s == NULL)
2918 fatal (_("bad format for %s"), "--set-section-flags");
2919
2920 len = s - optarg;
2921 name = xmalloc (len + 1);
2922 strncpy (name, optarg, len);
2923 name[len] = '\0';
2924
2925 p = find_section_list (name, TRUE);
2926
2927 p->set_flags = TRUE;
2928 p->flags = parse_flags (s + 1);
2929 }
2930 break;
2931
2932 case OPTION_RENAME_SECTION:
2933 {
2934 flagword flags;
2935 const char *eq, *fl;
2936 char *old_name;
2937 char *new_name;
2938 unsigned int len;
2939
2940 eq = strchr (optarg, '=');
2941 if (eq == NULL)
2942 fatal (_("bad format for %s"), "--rename-section");
2943
2944 len = eq - optarg;
2945 if (len == 0)
2946 fatal (_("bad format for %s"), "--rename-section");
2947
2948 old_name = xmalloc (len + 1);
2949 strncpy (old_name, optarg, len);
2950 old_name[len] = 0;
2951
2952 eq++;
2953 fl = strchr (eq, ',');
2954 if (fl)
2955 {
2956 flags = parse_flags (fl + 1);
2957 len = fl - eq;
2958 }
2959 else
2960 {
2961 flags = -1;
2962 len = strlen (eq);
2963 }
2964
2965 if (len == 0)
2966 fatal (_("bad format for %s"), "--rename-section");
2967
2968 new_name = xmalloc (len + 1);
2969 strncpy (new_name, eq, len);
2970 new_name[len] = 0;
2971
2972 add_section_rename (old_name, new_name, flags);
2973 }
2974 break;
2975
2976 case OPTION_SET_START:
2977 set_start = parse_vma (optarg, "--set-start");
2978 set_start_set = TRUE;
2979 break;
2980
2981 case OPTION_SREC_LEN:
2982 Chunk = parse_vma (optarg, "--srec-len");
2983 break;
2984
2985 case OPTION_SREC_FORCES3:
2986 S3Forced = TRUE;
2987 break;
2988
2989 case OPTION_STRIP_SYMBOLS:
2990 add_specific_symbols (optarg, &strip_specific_list);
2991 break;
2992
2993 case OPTION_STRIP_UNNEEDED_SYMBOLS:
2994 add_specific_symbols (optarg, &strip_unneeded_list);
2995 break;
2996
2997 case OPTION_KEEP_SYMBOLS:
2998 add_specific_symbols (optarg, &keep_specific_list);
2999 break;
3000
3001 case OPTION_LOCALIZE_SYMBOLS:
3002 add_specific_symbols (optarg, &localize_specific_list);
3003 break;
3004
3005 case OPTION_GLOBALIZE_SYMBOLS:
3006 add_specific_symbols (optarg, &globalize_specific_list);
3007 break;
3008
3009 case OPTION_KEEPGLOBAL_SYMBOLS:
3010 add_specific_symbols (optarg, &keepglobal_specific_list);
3011 break;
3012
3013 case OPTION_WEAKEN_SYMBOLS:
3014 add_specific_symbols (optarg, &weaken_specific_list);
3015 break;
3016
3017 case OPTION_ALT_MACH_CODE:
3018 use_alt_mach_code = atoi (optarg);
3019 if (use_alt_mach_code <= 0)
3020 fatal (_("alternate machine code index must be positive"));
3021 break;
3022
3023 case OPTION_PREFIX_SYMBOLS:
3024 prefix_symbols_string = optarg;
3025 break;
3026
3027 case OPTION_PREFIX_SECTIONS:
3028 prefix_sections_string = optarg;
3029 break;
3030
3031 case OPTION_PREFIX_ALLOC_SECTIONS:
3032 prefix_alloc_sections_string = optarg;
3033 break;
3034
3035 case OPTION_READONLY_TEXT:
3036 bfd_flags_to_set |= WP_TEXT;
3037 bfd_flags_to_clear &= ~WP_TEXT;
3038 break;
3039
3040 case OPTION_WRITABLE_TEXT:
3041 bfd_flags_to_clear |= WP_TEXT;
3042 bfd_flags_to_set &= ~WP_TEXT;
3043 break;
3044
3045 case OPTION_PURE:
3046 bfd_flags_to_set |= D_PAGED;
3047 bfd_flags_to_clear &= ~D_PAGED;
3048 break;
3049
3050 case OPTION_IMPURE:
3051 bfd_flags_to_clear |= D_PAGED;
3052 bfd_flags_to_set &= ~D_PAGED;
3053 break;
3054
3055 case 0:
3056 /* We've been given a long option. */
3057 break;
3058
3059 case 'H':
3060 case 'h':
3061 copy_usage (stdout, 0);
3062
3063 default:
3064 copy_usage (stderr, 1);
3065 }
3066 }
3067
3068 if (formats_info)
3069 {
3070 display_info ();
3071 return 0;
3072 }
3073
3074 if (show_version)
3075 print_version ("objcopy");
3076
3077 if (copy_byte >= interleave)
3078 fatal (_("byte number must be less than interleave"));
3079
3080 if (optind == argc || optind + 2 < argc)
3081 copy_usage (stderr, 1);
3082
3083 input_filename = argv[optind];
3084 if (optind + 1 < argc)
3085 output_filename = argv[optind + 1];
3086
3087 /* Default is to strip no symbols. */
3088 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3089 strip_symbols = STRIP_NONE;
3090
3091 if (output_target == NULL)
3092 output_target = input_target;
3093
3094 if (binary_architecture != NULL)
3095 {
3096 if (input_target && strcmp (input_target, "binary") == 0)
3097 {
3098 const bfd_arch_info_type * temp_arch_info;
3099
3100 temp_arch_info = bfd_scan_arch (binary_architecture);
3101
3102 if (temp_arch_info != NULL)
3103 {
3104 bfd_external_binary_architecture = temp_arch_info->arch;
3105 bfd_external_machine = temp_arch_info->mach;
3106 }
3107 else
3108 fatal (_("architecture %s unknown"), binary_architecture);
3109 }
3110 else
3111 {
3112 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3113 non_fatal (_(" Argument %s ignored"), binary_architecture);
3114 }
3115 }
3116
3117 if (preserve_dates)
3118 if (stat (input_filename, & statbuf) < 0)
3119 fatal (_("warning: could not locate '%s'. System error message: %s"),
3120 input_filename, strerror (errno));
3121
3122 /* If there is no destination file, or the source and destination files
3123 are the same, then create a temp and rename the result into the input. */
3124 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3125 {
3126 char *tmpname = make_tempname (input_filename);
3127
3128 copy_file (input_filename, tmpname, input_target, output_target);
3129 if (status == 0)
3130 {
3131 if (preserve_dates)
3132 set_times (tmpname, &statbuf);
3133 smart_rename (tmpname, input_filename, preserve_dates);
3134 }
3135 else
3136 unlink (tmpname);
3137 }
3138 else
3139 {
3140 copy_file (input_filename, output_filename, input_target, output_target);
3141
3142 if (status == 0 && preserve_dates)
3143 set_times (output_filename, &statbuf);
3144 else if (status != 0)
3145 unlink_if_ordinary (output_filename);
3146 }
3147
3148 if (change_warn)
3149 {
3150 for (p = change_sections; p != NULL; p = p->next)
3151 {
3152 if (! p->used)
3153 {
3154 if (p->change_vma != CHANGE_IGNORE)
3155 {
3156 char buff [20];
3157
3158 sprintf_vma (buff, p->vma_val);
3159
3160 /* xgettext:c-format */
3161 non_fatal (_("%s %s%c0x%s never used"),
3162 "--change-section-vma",
3163 p->name,
3164 p->change_vma == CHANGE_SET ? '=' : '+',
3165 buff);
3166 }
3167
3168 if (p->change_lma != CHANGE_IGNORE)
3169 {
3170 char buff [20];
3171
3172 sprintf_vma (buff, p->lma_val);
3173
3174 /* xgettext:c-format */
3175 non_fatal (_("%s %s%c0x%s never used"),
3176 "--change-section-lma",
3177 p->name,
3178 p->change_lma == CHANGE_SET ? '=' : '+',
3179 buff);
3180 }
3181 }
3182 }
3183 }
3184
3185 return 0;
3186 }
3187
3188 int
3189 main (int argc, char *argv[])
3190 {
3191 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3192 setlocale (LC_MESSAGES, "");
3193 #endif
3194 #if defined (HAVE_SETLOCALE)
3195 setlocale (LC_CTYPE, "");
3196 #endif
3197 bindtextdomain (PACKAGE, LOCALEDIR);
3198 textdomain (PACKAGE);
3199
3200 program_name = argv[0];
3201 xmalloc_set_program_name (program_name);
3202
3203 START_PROGRESS (program_name, 0);
3204
3205 expandargv (&argc, &argv);
3206
3207 strip_symbols = STRIP_UNDEF;
3208 discard_locals = LOCALS_UNDEF;
3209
3210 bfd_init ();
3211 set_default_bfd_target ();
3212
3213 if (is_strip < 0)
3214 {
3215 int i = strlen (program_name);
3216 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3217 /* Drop the .exe suffix, if any. */
3218 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3219 {
3220 i -= 4;
3221 program_name[i] = '\0';
3222 }
3223 #endif
3224 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3225 }
3226
3227 if (is_strip)
3228 strip_main (argc, argv);
3229 else
3230 copy_main (argc, argv);
3231
3232 END_PROGRESS (program_name);
3233
3234 return status;
3235 }
This page took 0.096981 seconds and 4 git commands to generate.