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