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