(fetch_indirect_string): Display a warning message when a corrupt
[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 object file IBFD onto OBFD.
1124 Returns TRUE upon success, FALSE otherwise. */
1125
1126 static bfd_boolean
1127 copy_object (bfd *ibfd, bfd *obfd)
1128 {
1129 bfd_vma start;
1130 long symcount;
1131 asection **osections = NULL;
1132 asection *gnu_debuglink_section = NULL;
1133 bfd_size_type *gaps = NULL;
1134 bfd_size_type max_gap = 0;
1135 long symsize;
1136 void *dhandle;
1137 enum bfd_architecture iarch;
1138 unsigned int imach;
1139
1140 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1141 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1142 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1143 fatal (_("Unable to change endianness of input file(s)"));
1144
1145 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1146 {
1147 bfd_nonfatal (bfd_get_filename (obfd));
1148 return FALSE;
1149 }
1150
1151 if (verbose)
1152 printf (_("copy from %s(%s) to %s(%s)\n"),
1153 bfd_get_filename (ibfd), bfd_get_target (ibfd),
1154 bfd_get_filename (obfd), bfd_get_target (obfd));
1155
1156 if (set_start_set)
1157 start = set_start;
1158 else
1159 start = bfd_get_start_address (ibfd);
1160 start += change_start;
1161
1162 /* Neither the start address nor the flags
1163 need to be set for a core file. */
1164 if (bfd_get_format (obfd) != bfd_core)
1165 {
1166 flagword flags;
1167
1168 flags = bfd_get_file_flags (ibfd);
1169 flags |= bfd_flags_to_set;
1170 flags &= ~bfd_flags_to_clear;
1171 flags &= bfd_applicable_file_flags (obfd);
1172
1173 if (!bfd_set_start_address (obfd, start)
1174 || !bfd_set_file_flags (obfd, flags))
1175 {
1176 bfd_nonfatal (bfd_get_filename (ibfd));
1177 return FALSE;
1178 }
1179 }
1180
1181 /* Copy architecture of input file to output file. */
1182 iarch = bfd_get_arch (ibfd);
1183 imach = bfd_get_mach (ibfd);
1184 if (!bfd_set_arch_mach (obfd, iarch, imach)
1185 && (ibfd->target_defaulted
1186 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1187 {
1188 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1189 fatal (_("Unable to recognise the format of the input file %s"),
1190 bfd_get_filename (ibfd));
1191 else
1192 {
1193 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1194 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1195 bfd_get_mach (ibfd)));
1196 return FALSE;
1197 }
1198 }
1199
1200 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1201 {
1202 bfd_nonfatal (bfd_get_filename (ibfd));
1203 return FALSE;
1204 }
1205
1206 if (isympp)
1207 free (isympp);
1208
1209 if (osympp != isympp)
1210 free (osympp);
1211
1212 /* BFD mandates that all output sections be created and sizes set before
1213 any output is done. Thus, we traverse all sections multiple times. */
1214 bfd_map_over_sections (ibfd, setup_section, obfd);
1215
1216 setup_bfd_headers (ibfd, obfd);
1217
1218 if (add_sections != NULL)
1219 {
1220 struct section_add *padd;
1221 struct section_list *pset;
1222
1223 for (padd = add_sections; padd != NULL; padd = padd->next)
1224 {
1225 flagword flags;
1226
1227 padd->section = bfd_make_section (obfd, padd->name);
1228 if (padd->section == NULL)
1229 {
1230 non_fatal (_("can't create section `%s': %s"),
1231 padd->name, bfd_errmsg (bfd_get_error ()));
1232 return FALSE;
1233 }
1234
1235 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1236 {
1237 bfd_nonfatal (bfd_get_filename (obfd));
1238 return FALSE;
1239 }
1240
1241 pset = find_section_list (padd->name, FALSE);
1242 if (pset != NULL)
1243 pset->used = TRUE;
1244
1245 if (pset != NULL && pset->set_flags)
1246 flags = pset->flags | SEC_HAS_CONTENTS;
1247 else
1248 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1249
1250 if (! bfd_set_section_flags (obfd, padd->section, flags))
1251 {
1252 bfd_nonfatal (bfd_get_filename (obfd));
1253 return FALSE;
1254 }
1255
1256 if (pset != NULL)
1257 {
1258 if (pset->change_vma != CHANGE_IGNORE)
1259 if (! bfd_set_section_vma (obfd, padd->section,
1260 pset->vma_val))
1261 {
1262 bfd_nonfatal (bfd_get_filename (obfd));
1263 return FALSE;
1264 }
1265
1266 if (pset->change_lma != CHANGE_IGNORE)
1267 {
1268 padd->section->lma = pset->lma_val;
1269
1270 if (! bfd_set_section_alignment
1271 (obfd, padd->section,
1272 bfd_section_alignment (obfd, padd->section)))
1273 {
1274 bfd_nonfatal (bfd_get_filename (obfd));
1275 return FALSE;
1276 }
1277 }
1278 }
1279 }
1280 }
1281
1282 if (gnu_debuglink_filename != NULL)
1283 {
1284 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1285 (obfd, gnu_debuglink_filename);
1286
1287 if (gnu_debuglink_section == NULL)
1288 {
1289 bfd_nonfatal (gnu_debuglink_filename);
1290 return FALSE;
1291 }
1292 }
1293
1294 if (bfd_count_sections (obfd) == 0)
1295 {
1296 non_fatal (_("there are no sections to be copied!"));
1297 return FALSE;
1298 }
1299
1300 if (gap_fill_set || pad_to_set)
1301 {
1302 asection **set;
1303 unsigned int c, i;
1304
1305 /* We must fill in gaps between the sections and/or we must pad
1306 the last section to a specified address. We do this by
1307 grabbing a list of the sections, sorting them by VMA, and
1308 increasing the section sizes as required to fill the gaps.
1309 We write out the gap contents below. */
1310
1311 c = bfd_count_sections (obfd);
1312 osections = xmalloc (c * sizeof (asection *));
1313 set = osections;
1314 bfd_map_over_sections (obfd, get_sections, &set);
1315
1316 qsort (osections, c, sizeof (asection *), compare_section_lma);
1317
1318 gaps = xmalloc (c * sizeof (bfd_size_type));
1319 memset (gaps, 0, c * sizeof (bfd_size_type));
1320
1321 if (gap_fill_set)
1322 {
1323 for (i = 0; i < c - 1; i++)
1324 {
1325 flagword flags;
1326 bfd_size_type size;
1327 bfd_vma gap_start, gap_stop;
1328
1329 flags = bfd_get_section_flags (obfd, osections[i]);
1330 if ((flags & SEC_HAS_CONTENTS) == 0
1331 || (flags & SEC_LOAD) == 0)
1332 continue;
1333
1334 size = bfd_section_size (obfd, osections[i]);
1335 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1336 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1337 if (gap_start < gap_stop)
1338 {
1339 if (! bfd_set_section_size (obfd, osections[i],
1340 size + (gap_stop - gap_start)))
1341 {
1342 non_fatal (_("Can't fill gap after %s: %s"),
1343 bfd_get_section_name (obfd, osections[i]),
1344 bfd_errmsg (bfd_get_error ()));
1345 status = 1;
1346 break;
1347 }
1348 gaps[i] = gap_stop - gap_start;
1349 if (max_gap < gap_stop - gap_start)
1350 max_gap = gap_stop - gap_start;
1351 }
1352 }
1353 }
1354
1355 if (pad_to_set)
1356 {
1357 bfd_vma lma;
1358 bfd_size_type size;
1359
1360 lma = bfd_section_lma (obfd, osections[c - 1]);
1361 size = bfd_section_size (obfd, osections[c - 1]);
1362 if (lma + size < pad_to)
1363 {
1364 if (! bfd_set_section_size (obfd, osections[c - 1],
1365 pad_to - lma))
1366 {
1367 non_fatal (_("Can't add padding to %s: %s"),
1368 bfd_get_section_name (obfd, osections[c - 1]),
1369 bfd_errmsg (bfd_get_error ()));
1370 status = 1;
1371 }
1372 else
1373 {
1374 gaps[c - 1] = pad_to - (lma + size);
1375 if (max_gap < pad_to - (lma + size))
1376 max_gap = pad_to - (lma + size);
1377 }
1378 }
1379 }
1380 }
1381
1382 /* Symbol filtering must happen after the output sections
1383 have been created, but before their contents are set. */
1384 dhandle = NULL;
1385 symsize = bfd_get_symtab_upper_bound (ibfd);
1386 if (symsize < 0)
1387 {
1388 bfd_nonfatal (bfd_get_filename (ibfd));
1389 return FALSE;
1390 }
1391
1392 osympp = isympp = xmalloc (symsize);
1393 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1394 if (symcount < 0)
1395 {
1396 bfd_nonfatal (bfd_get_filename (ibfd));
1397 return FALSE;
1398 }
1399
1400 if (convert_debugging)
1401 dhandle = read_debugging_info (ibfd, isympp, symcount);
1402
1403 if (strip_symbols == STRIP_DEBUG
1404 || strip_symbols == STRIP_ALL
1405 || strip_symbols == STRIP_UNNEEDED
1406 || strip_symbols == STRIP_NONDEBUG
1407 || discard_locals != LOCALS_UNDEF
1408 || strip_specific_list != NULL
1409 || keep_specific_list != NULL
1410 || localize_specific_list != NULL
1411 || globalize_specific_list != NULL
1412 || keepglobal_specific_list != NULL
1413 || weaken_specific_list != NULL
1414 || prefix_symbols_string
1415 || sections_removed
1416 || sections_copied
1417 || convert_debugging
1418 || change_leading_char
1419 || remove_leading_char
1420 || redefine_sym_list
1421 || weaken)
1422 {
1423 /* Mark symbols used in output relocations so that they
1424 are kept, even if they are local labels or static symbols.
1425
1426 Note we iterate over the input sections examining their
1427 relocations since the relocations for the output sections
1428 haven't been set yet. mark_symbols_used_in_relocations will
1429 ignore input sections which have no corresponding output
1430 section. */
1431 if (strip_symbols != STRIP_ALL)
1432 bfd_map_over_sections (ibfd,
1433 mark_symbols_used_in_relocations,
1434 isympp);
1435 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1436 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1437 }
1438
1439 if (convert_debugging && dhandle != NULL)
1440 {
1441 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1442 {
1443 status = 1;
1444 return FALSE;
1445 }
1446 }
1447
1448 bfd_set_symtab (obfd, osympp, symcount);
1449
1450 /* This has to happen after the symbol table has been set. */
1451 bfd_map_over_sections (ibfd, copy_section, obfd);
1452
1453 if (add_sections != NULL)
1454 {
1455 struct section_add *padd;
1456
1457 for (padd = add_sections; padd != NULL; padd = padd->next)
1458 {
1459 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1460 0, padd->size))
1461 {
1462 bfd_nonfatal (bfd_get_filename (obfd));
1463 return FALSE;
1464 }
1465 }
1466 }
1467
1468 if (gnu_debuglink_filename != NULL)
1469 {
1470 if (! bfd_fill_in_gnu_debuglink_section
1471 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1472 {
1473 bfd_nonfatal (gnu_debuglink_filename);
1474 return FALSE;
1475 }
1476 }
1477
1478 if (gap_fill_set || pad_to_set)
1479 {
1480 bfd_byte *buf;
1481 int c, i;
1482
1483 /* Fill in the gaps. */
1484 if (max_gap > 8192)
1485 max_gap = 8192;
1486 buf = xmalloc (max_gap);
1487 memset (buf, gap_fill, max_gap);
1488
1489 c = bfd_count_sections (obfd);
1490 for (i = 0; i < c; i++)
1491 {
1492 if (gaps[i] != 0)
1493 {
1494 bfd_size_type left;
1495 file_ptr off;
1496
1497 left = gaps[i];
1498 off = bfd_section_size (obfd, osections[i]) - left;
1499
1500 while (left > 0)
1501 {
1502 bfd_size_type now;
1503
1504 if (left > 8192)
1505 now = 8192;
1506 else
1507 now = left;
1508
1509 if (! bfd_set_section_contents (obfd, osections[i], buf,
1510 off, now))
1511 {
1512 bfd_nonfatal (bfd_get_filename (obfd));
1513 return FALSE;
1514 }
1515
1516 left -= now;
1517 off += now;
1518 }
1519 }
1520 }
1521 }
1522
1523 /* Allow the BFD backend to copy any private data it understands
1524 from the input BFD to the output BFD. This is done last to
1525 permit the routine to look at the filtered symbol table, which is
1526 important for the ECOFF code at least. */
1527 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1528 && strip_symbols == STRIP_NONDEBUG)
1529 /* Do not copy the private data when creating an ELF format
1530 debug info file. We do not want the program headers. */
1531 ;
1532 else if (! bfd_copy_private_bfd_data (ibfd, obfd))
1533 {
1534 non_fatal (_("%s: error copying private BFD data: %s"),
1535 bfd_get_filename (obfd),
1536 bfd_errmsg (bfd_get_error ()));
1537 return FALSE;
1538 }
1539
1540 /* Switch to the alternate machine code. We have to do this at the
1541 very end, because we only initialize the header when we create
1542 the first section. */
1543 if (use_alt_mach_code != 0
1544 && ! bfd_alt_mach_code (obfd, use_alt_mach_code))
1545 non_fatal (_("unknown alternate machine code, ignored"));
1546
1547 return TRUE;
1548 }
1549
1550 #undef MKDIR
1551 #if defined (_WIN32) && !defined (__CYGWIN32__)
1552 #define MKDIR(DIR, MODE) mkdir (DIR)
1553 #else
1554 #define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1555 #endif
1556
1557 /* Read each archive element in turn from IBFD, copy the
1558 contents to temp file, and keep the temp file handle. */
1559
1560 static void
1561 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target)
1562 {
1563 struct name_list
1564 {
1565 struct name_list *next;
1566 const char *name;
1567 bfd *obfd;
1568 } *list, *l;
1569 bfd **ptr = &obfd->archive_head;
1570 bfd *this_element;
1571 char *dir = make_tempname (bfd_get_filename (obfd));
1572
1573 /* Make a temp directory to hold the contents. */
1574 if (MKDIR (dir, 0700) != 0)
1575 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1576 dir, strerror (errno));
1577
1578 obfd->has_armap = ibfd->has_armap;
1579
1580 list = NULL;
1581
1582 this_element = bfd_openr_next_archived_file (ibfd, NULL);
1583
1584 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1585 RETURN_NONFATAL (bfd_get_filename (obfd));
1586
1587 while (!status && this_element != NULL)
1588 {
1589 char *output_name;
1590 bfd *output_bfd;
1591 bfd *last_element;
1592 struct stat buf;
1593 int stat_status = 0;
1594 bfd_boolean delete = TRUE;
1595
1596 /* Create an output file for this member. */
1597 output_name = concat (dir, "/",
1598 bfd_get_filename (this_element), (char *) 0);
1599
1600 /* If the file already exists, make another temp dir. */
1601 if (stat (output_name, &buf) >= 0)
1602 {
1603 output_name = make_tempname (output_name);
1604 if (MKDIR (output_name, 0700) != 0)
1605 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1606 output_name, strerror (errno));
1607
1608 l = xmalloc (sizeof (struct name_list));
1609 l->name = output_name;
1610 l->next = list;
1611 l->obfd = NULL;
1612 list = l;
1613 output_name = concat (output_name, "/",
1614 bfd_get_filename (this_element), (char *) 0);
1615 }
1616
1617 output_bfd = bfd_openw (output_name, output_target);
1618 if (preserve_dates)
1619 {
1620 stat_status = bfd_stat_arch_elt (this_element, &buf);
1621
1622 if (stat_status != 0)
1623 non_fatal (_("internal stat error on %s"),
1624 bfd_get_filename (this_element));
1625 }
1626
1627 l = xmalloc (sizeof (struct name_list));
1628 l->name = output_name;
1629 l->next = list;
1630 l->obfd = NULL;
1631 list = l;
1632
1633 if (output_bfd == NULL)
1634 RETURN_NONFATAL (output_name);
1635
1636 if (bfd_check_format (this_element, bfd_object))
1637 delete = ! copy_object (this_element, output_bfd);
1638
1639 if (!bfd_close (output_bfd))
1640 {
1641 bfd_nonfatal (bfd_get_filename (output_bfd));
1642 /* Error in new object file. Don't change archive. */
1643 status = 1;
1644 }
1645
1646 if (delete)
1647 {
1648 unlink (output_name);
1649 status = 1;
1650 }
1651 else
1652 {
1653 if (preserve_dates && stat_status == 0)
1654 set_times (output_name, &buf);
1655
1656 /* Open the newly output file and attach to our list. */
1657 output_bfd = bfd_openr (output_name, output_target);
1658
1659 l->obfd = output_bfd;
1660
1661 *ptr = output_bfd;
1662 ptr = &output_bfd->next;
1663
1664 last_element = this_element;
1665
1666 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1667
1668 bfd_close (last_element);
1669 }
1670 }
1671 *ptr = NULL;
1672
1673 if (!bfd_close (obfd))
1674 RETURN_NONFATAL (bfd_get_filename (obfd));
1675
1676 if (!bfd_close (ibfd))
1677 RETURN_NONFATAL (bfd_get_filename (ibfd));
1678
1679 /* Delete all the files that we opened. */
1680 for (l = list; l != NULL; l = l->next)
1681 {
1682 if (l->obfd == NULL)
1683 rmdir (l->name);
1684 else
1685 {
1686 bfd_close (l->obfd);
1687 unlink (l->name);
1688 }
1689 }
1690 rmdir (dir);
1691 }
1692
1693 /* The top-level control. */
1694
1695 static void
1696 copy_file (const char *input_filename, const char *output_filename,
1697 const char *input_target, const char *output_target)
1698 {
1699 bfd *ibfd;
1700 char **obj_matching;
1701 char **core_matching;
1702
1703 if (get_file_size (input_filename) < 1)
1704 {
1705 non_fatal (_("error: the input file '%s' is empty"), input_filename);
1706 status = 1;
1707 return;
1708 }
1709
1710 /* To allow us to do "strip *" without dying on the first
1711 non-object file, failures are nonfatal. */
1712 ibfd = bfd_openr (input_filename, input_target);
1713 if (ibfd == NULL)
1714 RETURN_NONFATAL (input_filename);
1715
1716 if (bfd_check_format (ibfd, bfd_archive))
1717 {
1718 bfd *obfd;
1719
1720 /* bfd_get_target does not return the correct value until
1721 bfd_check_format succeeds. */
1722 if (output_target == NULL)
1723 output_target = bfd_get_target (ibfd);
1724
1725 obfd = bfd_openw (output_filename, output_target);
1726 if (obfd == NULL)
1727 RETURN_NONFATAL (output_filename);
1728
1729 copy_archive (ibfd, obfd, output_target);
1730 }
1731 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
1732 {
1733 bfd *obfd;
1734 do_copy:
1735
1736 /* bfd_get_target does not return the correct value until
1737 bfd_check_format succeeds. */
1738 if (output_target == NULL)
1739 output_target = bfd_get_target (ibfd);
1740
1741 obfd = bfd_openw (output_filename, output_target);
1742 if (obfd == NULL)
1743 RETURN_NONFATAL (output_filename);
1744
1745 if (! copy_object (ibfd, obfd))
1746 status = 1;
1747
1748 if (!bfd_close (obfd))
1749 RETURN_NONFATAL (output_filename);
1750
1751 if (!bfd_close (ibfd))
1752 RETURN_NONFATAL (input_filename);
1753
1754 }
1755 else
1756 {
1757 bfd_error_type obj_error = bfd_get_error ();
1758 bfd_error_type core_error;
1759
1760 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1761 {
1762 /* This probably can't happen.. */
1763 if (obj_error == bfd_error_file_ambiguously_recognized)
1764 free (obj_matching);
1765 goto do_copy;
1766 }
1767
1768 core_error = bfd_get_error ();
1769 /* Report the object error in preference to the core error. */
1770 if (obj_error != core_error)
1771 bfd_set_error (obj_error);
1772
1773 bfd_nonfatal (input_filename);
1774
1775 if (obj_error == bfd_error_file_ambiguously_recognized)
1776 {
1777 list_matching_formats (obj_matching);
1778 free (obj_matching);
1779 }
1780 if (core_error == bfd_error_file_ambiguously_recognized)
1781 {
1782 list_matching_formats (core_matching);
1783 free (core_matching);
1784 }
1785
1786 status = 1;
1787 }
1788 }
1789
1790 /* Add a name to the section renaming list. */
1791
1792 static void
1793 add_section_rename (const char * old_name, const char * new_name,
1794 flagword flags)
1795 {
1796 section_rename * rename;
1797
1798 /* Check for conflicts first. */
1799 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1800 if (strcmp (rename->old_name, old_name) == 0)
1801 {
1802 /* Silently ignore duplicate definitions. */
1803 if (strcmp (rename->new_name, new_name) == 0
1804 && rename->flags == flags)
1805 return;
1806
1807 fatal (_("Multiple renames of section %s"), old_name);
1808 }
1809
1810 rename = xmalloc (sizeof (* rename));
1811
1812 rename->old_name = old_name;
1813 rename->new_name = new_name;
1814 rename->flags = flags;
1815 rename->next = section_rename_list;
1816
1817 section_rename_list = rename;
1818 }
1819
1820 /* Check the section rename list for a new name of the input section
1821 ISECTION. Return the new name if one is found.
1822 Also set RETURNED_FLAGS to the flags to be used for this section. */
1823
1824 static const char *
1825 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
1826 flagword * returned_flags)
1827 {
1828 const char * old_name = bfd_section_name (ibfd, isection);
1829 section_rename * rename;
1830
1831 /* Default to using the flags of the input section. */
1832 * returned_flags = bfd_get_section_flags (ibfd, isection);
1833
1834 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1835 if (strcmp (rename->old_name, old_name) == 0)
1836 {
1837 if (rename->flags != (flagword) -1)
1838 * returned_flags = rename->flags;
1839
1840 return rename->new_name;
1841 }
1842
1843 return old_name;
1844 }
1845
1846 /* Once each of the sections is copied, we may still need to do some
1847 finalization work for private section headers. Do that here. */
1848
1849 static void
1850 setup_bfd_headers (bfd *ibfd, bfd *obfd)
1851 {
1852 const char *err;
1853
1854 /* Allow the BFD backend to copy any private data it understands
1855 from the input section to the output section. */
1856 if (! bfd_copy_private_header_data (ibfd, obfd))
1857 {
1858 err = _("private header data");
1859 goto loser;
1860 }
1861
1862 /* All went well. */
1863 return;
1864
1865 loser:
1866 non_fatal (_("%s: error in %s: %s"),
1867 bfd_get_filename (ibfd),
1868 err, bfd_errmsg (bfd_get_error ()));
1869 status = 1;
1870 }
1871
1872 /* Create a section in OBFD with the same
1873 name and attributes as ISECTION in IBFD. */
1874
1875 static void
1876 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
1877 {
1878 bfd *obfd = obfdarg;
1879 struct section_list *p;
1880 sec_ptr osection;
1881 bfd_size_type size;
1882 bfd_vma vma;
1883 bfd_vma lma;
1884 flagword flags;
1885 const char *err;
1886 const char * name;
1887 char *prefix = NULL;
1888
1889 if (is_strip_section (ibfd, isection))
1890 return;
1891
1892 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
1893 if (p != NULL)
1894 p->used = TRUE;
1895
1896 /* Get the, possibly new, name of the output section. */
1897 name = find_section_rename (ibfd, isection, & flags);
1898
1899 /* Prefix sections. */
1900 if ((prefix_alloc_sections_string)
1901 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
1902 prefix = prefix_alloc_sections_string;
1903 else if (prefix_sections_string)
1904 prefix = prefix_sections_string;
1905
1906 if (prefix)
1907 {
1908 char *n;
1909
1910 n = xmalloc (strlen (prefix) + strlen (name) + 1);
1911 strcpy (n, prefix);
1912 strcat (n, name);
1913 name = n;
1914 }
1915
1916 osection = bfd_make_section_anyway (obfd, name);
1917
1918 if (osection == NULL)
1919 {
1920 err = _("making");
1921 goto loser;
1922 }
1923
1924 size = bfd_section_size (ibfd, isection);
1925 if (copy_byte >= 0)
1926 size = (size + interleave - 1) / interleave;
1927 if (! bfd_set_section_size (obfd, osection, size))
1928 {
1929 err = _("size");
1930 goto loser;
1931 }
1932
1933 vma = bfd_section_vma (ibfd, isection);
1934 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1935 vma += p->vma_val;
1936 else if (p != NULL && p->change_vma == CHANGE_SET)
1937 vma = p->vma_val;
1938 else
1939 vma += change_section_address;
1940
1941 if (! bfd_set_section_vma (obfd, osection, vma))
1942 {
1943 err = _("vma");
1944 goto loser;
1945 }
1946
1947 lma = isection->lma;
1948 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1949 {
1950 if (p->change_lma == CHANGE_MODIFY)
1951 lma += p->lma_val;
1952 else if (p->change_lma == CHANGE_SET)
1953 lma = p->lma_val;
1954 else
1955 abort ();
1956 }
1957 else
1958 lma += change_section_address;
1959
1960 osection->lma = lma;
1961
1962 /* FIXME: This is probably not enough. If we change the LMA we
1963 may have to recompute the header for the file as well. */
1964 if (!bfd_set_section_alignment (obfd,
1965 osection,
1966 bfd_section_alignment (ibfd, isection)))
1967 {
1968 err = _("alignment");
1969 goto loser;
1970 }
1971
1972 if (p != NULL && p->set_flags)
1973 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
1974 else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
1975 {
1976 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
1977 if (obfd->xvec->flavour == bfd_target_elf_flavour)
1978 elf_section_type (osection) = SHT_NOBITS;
1979 }
1980
1981 if (!bfd_set_section_flags (obfd, osection, flags))
1982 {
1983 err = _("flags");
1984 goto loser;
1985 }
1986
1987 /* Copy merge entity size. */
1988 osection->entsize = isection->entsize;
1989
1990 /* This used to be mangle_section; we do here to avoid using
1991 bfd_get_section_by_name since some formats allow multiple
1992 sections with the same name. */
1993 isection->output_section = osection;
1994 isection->output_offset = 0;
1995
1996 /* Allow the BFD backend to copy any private data it understands
1997 from the input section to the output section. */
1998 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1999 && strip_symbols == STRIP_NONDEBUG)
2000 /* Do not copy the private data when creating an ELF format
2001 debug info file. We do not want the program headers. */
2002 ;
2003 else if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2004 {
2005 err = _("private data");
2006 goto loser;
2007 }
2008
2009 /* All went well. */
2010 return;
2011
2012 loser:
2013 non_fatal (_("%s: section `%s': error in %s: %s"),
2014 bfd_get_filename (ibfd),
2015 bfd_section_name (ibfd, isection),
2016 err, bfd_errmsg (bfd_get_error ()));
2017 status = 1;
2018 }
2019
2020 /* Copy the data of input section ISECTION of IBFD
2021 to an output section with the same name in OBFD.
2022 If stripping then don't copy any relocation info. */
2023
2024 static void
2025 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2026 {
2027 bfd *obfd = obfdarg;
2028 struct section_list *p;
2029 arelent **relpp;
2030 long relcount;
2031 sec_ptr osection;
2032 bfd_size_type size;
2033 long relsize;
2034 flagword flags;
2035
2036 /* If we have already failed earlier on,
2037 do not keep on generating complaints now. */
2038 if (status != 0)
2039 return;
2040
2041 if (is_strip_section (ibfd, isection))
2042 return;
2043
2044 flags = bfd_get_section_flags (ibfd, isection);
2045 if ((flags & SEC_GROUP) != 0)
2046 return;
2047
2048 osection = isection->output_section;
2049 size = bfd_get_section_size (isection);
2050
2051 if (size == 0 || osection == 0)
2052 return;
2053
2054 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2055
2056 /* Core files do not need to be relocated. */
2057 if (bfd_get_format (obfd) == bfd_core)
2058 relsize = 0;
2059 else
2060 {
2061 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2062
2063 if (relsize < 0)
2064 {
2065 /* Do not complain if the target does not support relocations. */
2066 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2067 relsize = 0;
2068 else
2069 RETURN_NONFATAL (bfd_get_filename (ibfd));
2070 }
2071 }
2072
2073 if (relsize == 0)
2074 bfd_set_reloc (obfd, osection, NULL, 0);
2075 else
2076 {
2077 relpp = xmalloc (relsize);
2078 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2079 if (relcount < 0)
2080 RETURN_NONFATAL (bfd_get_filename (ibfd));
2081
2082 if (strip_symbols == STRIP_ALL)
2083 {
2084 /* Remove relocations which are not in
2085 keep_strip_specific_list. */
2086 arelent **temp_relpp;
2087 long temp_relcount = 0;
2088 long i;
2089
2090 temp_relpp = xmalloc (relsize);
2091 for (i = 0; i < relcount; i++)
2092 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2093 keep_specific_list))
2094 temp_relpp [temp_relcount++] = relpp [i];
2095 relcount = temp_relcount;
2096 free (relpp);
2097 relpp = temp_relpp;
2098 }
2099
2100 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2101 if (relcount == 0)
2102 free (relpp);
2103 }
2104
2105 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2106 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2107 {
2108 void *memhunk = xmalloc (size);
2109
2110 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2111 RETURN_NONFATAL (bfd_get_filename (ibfd));
2112
2113 if (copy_byte >= 0)
2114 {
2115 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2116 char *from = (char *) memhunk + copy_byte;
2117 char *to = memhunk;
2118 char *end = (char *) memhunk + size;
2119
2120 for (; from < end; from += interleave)
2121 *to++ = *from;
2122
2123 size = (size + interleave - 1 - copy_byte) / interleave;
2124 osection->lma /= interleave;
2125 }
2126
2127 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2128 RETURN_NONFATAL (bfd_get_filename (obfd));
2129
2130 free (memhunk);
2131 }
2132 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2133 {
2134 void *memhunk = xmalloc (size);
2135
2136 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2137 flag--they can just remove the section entirely and add it
2138 back again. However, we do permit them to turn on the
2139 SEC_HAS_CONTENTS flag, and take it to mean that the section
2140 contents should be zeroed out. */
2141
2142 memset (memhunk, 0, size);
2143 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2144 RETURN_NONFATAL (bfd_get_filename (obfd));
2145 free (memhunk);
2146 }
2147 }
2148
2149 /* Get all the sections. This is used when --gap-fill or --pad-to is
2150 used. */
2151
2152 static void
2153 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2154 {
2155 asection ***secppp = secppparg;
2156
2157 **secppp = osection;
2158 ++(*secppp);
2159 }
2160
2161 /* Sort sections by VMA. This is called via qsort, and is used when
2162 --gap-fill or --pad-to is used. We force non loadable or empty
2163 sections to the front, where they are easier to ignore. */
2164
2165 static int
2166 compare_section_lma (const void *arg1, const void *arg2)
2167 {
2168 const asection *const *sec1 = arg1;
2169 const asection *const *sec2 = arg2;
2170 flagword flags1, flags2;
2171
2172 /* Sort non loadable sections to the front. */
2173 flags1 = (*sec1)->flags;
2174 flags2 = (*sec2)->flags;
2175 if ((flags1 & SEC_HAS_CONTENTS) == 0
2176 || (flags1 & SEC_LOAD) == 0)
2177 {
2178 if ((flags2 & SEC_HAS_CONTENTS) != 0
2179 && (flags2 & SEC_LOAD) != 0)
2180 return -1;
2181 }
2182 else
2183 {
2184 if ((flags2 & SEC_HAS_CONTENTS) == 0
2185 || (flags2 & SEC_LOAD) == 0)
2186 return 1;
2187 }
2188
2189 /* Sort sections by LMA. */
2190 if ((*sec1)->lma > (*sec2)->lma)
2191 return 1;
2192 else if ((*sec1)->lma < (*sec2)->lma)
2193 return -1;
2194
2195 /* Sort sections with the same LMA by size. */
2196 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2197 return 1;
2198 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2199 return -1;
2200
2201 return 0;
2202 }
2203
2204 /* Mark all the symbols which will be used in output relocations with
2205 the BSF_KEEP flag so that those symbols will not be stripped.
2206
2207 Ignore relocations which will not appear in the output file. */
2208
2209 static void
2210 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2211 {
2212 asymbol **symbols = symbolsarg;
2213 long relsize;
2214 arelent **relpp;
2215 long relcount, i;
2216
2217 /* Ignore an input section with no corresponding output section. */
2218 if (isection->output_section == NULL)
2219 return;
2220
2221 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2222 if (relsize < 0)
2223 {
2224 /* Do not complain if the target does not support relocations. */
2225 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2226 return;
2227 bfd_fatal (bfd_get_filename (ibfd));
2228 }
2229
2230 if (relsize == 0)
2231 return;
2232
2233 relpp = xmalloc (relsize);
2234 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2235 if (relcount < 0)
2236 bfd_fatal (bfd_get_filename (ibfd));
2237
2238 /* Examine each symbol used in a relocation. If it's not one of the
2239 special bfd section symbols, then mark it with BSF_KEEP. */
2240 for (i = 0; i < relcount; i++)
2241 {
2242 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2243 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2244 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2245 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2246 }
2247
2248 if (relpp != NULL)
2249 free (relpp);
2250 }
2251
2252 /* Write out debugging information. */
2253
2254 static bfd_boolean
2255 write_debugging_info (bfd *obfd, void *dhandle,
2256 long *symcountp ATTRIBUTE_UNUSED,
2257 asymbol ***symppp ATTRIBUTE_UNUSED)
2258 {
2259 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2260 return write_ieee_debugging_info (obfd, dhandle);
2261
2262 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2263 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2264 {
2265 bfd_byte *syms, *strings;
2266 bfd_size_type symsize, stringsize;
2267 asection *stabsec, *stabstrsec;
2268
2269 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2270 &symsize, &strings,
2271 &stringsize))
2272 return FALSE;
2273
2274 stabsec = bfd_make_section (obfd, ".stab");
2275 stabstrsec = bfd_make_section (obfd, ".stabstr");
2276 if (stabsec == NULL
2277 || stabstrsec == NULL
2278 || ! bfd_set_section_size (obfd, stabsec, symsize)
2279 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2280 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2281 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
2282 || ! bfd_set_section_flags (obfd, stabsec,
2283 (SEC_HAS_CONTENTS
2284 | SEC_READONLY
2285 | SEC_DEBUGGING))
2286 || ! bfd_set_section_flags (obfd, stabstrsec,
2287 (SEC_HAS_CONTENTS
2288 | SEC_READONLY
2289 | SEC_DEBUGGING)))
2290 {
2291 non_fatal (_("%s: can't create debugging section: %s"),
2292 bfd_get_filename (obfd),
2293 bfd_errmsg (bfd_get_error ()));
2294 return FALSE;
2295 }
2296
2297 /* We can get away with setting the section contents now because
2298 the next thing the caller is going to do is copy over the
2299 real sections. We may someday have to split the contents
2300 setting out of this function. */
2301 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2302 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2303 stringsize))
2304 {
2305 non_fatal (_("%s: can't set debugging section contents: %s"),
2306 bfd_get_filename (obfd),
2307 bfd_errmsg (bfd_get_error ()));
2308 return FALSE;
2309 }
2310
2311 return TRUE;
2312 }
2313
2314 non_fatal (_("%s: don't know how to write debugging information for %s"),
2315 bfd_get_filename (obfd), bfd_get_target (obfd));
2316 return FALSE;
2317 }
2318
2319 static int
2320 strip_main (int argc, char *argv[])
2321 {
2322 char *input_target = NULL;
2323 char *output_target = NULL;
2324 bfd_boolean show_version = FALSE;
2325 bfd_boolean formats_info = FALSE;
2326 int c;
2327 int i;
2328 struct section_list *p;
2329 char *output_file = NULL;
2330
2331 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2332 strip_options, (int *) 0)) != EOF)
2333 {
2334 switch (c)
2335 {
2336 case 'I':
2337 input_target = optarg;
2338 break;
2339 case 'O':
2340 output_target = optarg;
2341 break;
2342 case 'F':
2343 input_target = output_target = optarg;
2344 break;
2345 case 'R':
2346 p = find_section_list (optarg, TRUE);
2347 p->remove = TRUE;
2348 sections_removed = TRUE;
2349 break;
2350 case 's':
2351 strip_symbols = STRIP_ALL;
2352 break;
2353 case 'S':
2354 case 'g':
2355 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
2356 strip_symbols = STRIP_DEBUG;
2357 break;
2358 case OPTION_STRIP_UNNEEDED:
2359 strip_symbols = STRIP_UNNEEDED;
2360 break;
2361 case 'K':
2362 add_specific_symbol (optarg, &keep_specific_list);
2363 break;
2364 case 'N':
2365 add_specific_symbol (optarg, &strip_specific_list);
2366 break;
2367 case 'o':
2368 output_file = optarg;
2369 break;
2370 case 'p':
2371 preserve_dates = TRUE;
2372 break;
2373 case 'x':
2374 discard_locals = LOCALS_ALL;
2375 break;
2376 case 'X':
2377 discard_locals = LOCALS_START_L;
2378 break;
2379 case 'v':
2380 verbose = TRUE;
2381 break;
2382 case 'V':
2383 show_version = TRUE;
2384 break;
2385 case OPTION_FORMATS_INFO:
2386 formats_info = TRUE;
2387 break;
2388 case OPTION_ONLY_KEEP_DEBUG:
2389 strip_symbols = STRIP_NONDEBUG;
2390 break;
2391 case 0:
2392 /* We've been given a long option. */
2393 break;
2394 case 'w':
2395 wildcard = TRUE;
2396 break;
2397 case 'H':
2398 case 'h':
2399 strip_usage (stdout, 0);
2400 default:
2401 strip_usage (stderr, 1);
2402 }
2403 }
2404
2405 if (formats_info)
2406 {
2407 display_info ();
2408 return 0;
2409 }
2410
2411 if (show_version)
2412 print_version ("strip");
2413
2414 /* Default is to strip all symbols. */
2415 if (strip_symbols == STRIP_UNDEF
2416 && discard_locals == LOCALS_UNDEF
2417 && strip_specific_list == NULL)
2418 strip_symbols = STRIP_ALL;
2419
2420 if (output_target == NULL)
2421 output_target = input_target;
2422
2423 i = optind;
2424 if (i == argc
2425 || (output_file != NULL && (i + 1) < argc))
2426 strip_usage (stderr, 1);
2427
2428 for (; i < argc; i++)
2429 {
2430 int hold_status = status;
2431 struct stat statbuf;
2432 char *tmpname;
2433
2434 if (get_file_size (argv[i]) < 1)
2435 continue;
2436
2437 if (preserve_dates)
2438 /* No need to check the return value of stat().
2439 It has already been checked in get_file_size(). */
2440 stat (argv[i], &statbuf);
2441
2442 if (output_file != NULL)
2443 tmpname = output_file;
2444 else
2445 tmpname = make_tempname (argv[i]);
2446 status = 0;
2447
2448 copy_file (argv[i], tmpname, input_target, output_target);
2449 if (status == 0)
2450 {
2451 if (preserve_dates)
2452 set_times (tmpname, &statbuf);
2453 if (output_file == NULL)
2454 smart_rename (tmpname, argv[i], preserve_dates);
2455 status = hold_status;
2456 }
2457 else
2458 unlink_if_ordinary (tmpname);
2459 if (output_file == NULL)
2460 free (tmpname);
2461 }
2462
2463 return 0;
2464 }
2465
2466 static int
2467 copy_main (int argc, char *argv[])
2468 {
2469 char * binary_architecture = NULL;
2470 char *input_filename = NULL;
2471 char *output_filename = NULL;
2472 char *input_target = NULL;
2473 char *output_target = NULL;
2474 bfd_boolean show_version = FALSE;
2475 bfd_boolean change_warn = TRUE;
2476 bfd_boolean formats_info = FALSE;
2477 int c;
2478 struct section_list *p;
2479 struct stat statbuf;
2480
2481 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
2482 copy_options, (int *) 0)) != EOF)
2483 {
2484 switch (c)
2485 {
2486 case 'b':
2487 copy_byte = atoi (optarg);
2488 if (copy_byte < 0)
2489 fatal (_("byte number must be non-negative"));
2490 break;
2491
2492 case 'B':
2493 binary_architecture = optarg;
2494 break;
2495
2496 case 'i':
2497 interleave = atoi (optarg);
2498 if (interleave < 1)
2499 fatal (_("interleave must be positive"));
2500 break;
2501
2502 case 'I':
2503 case 's': /* "source" - 'I' is preferred */
2504 input_target = optarg;
2505 break;
2506
2507 case 'O':
2508 case 'd': /* "destination" - 'O' is preferred */
2509 output_target = optarg;
2510 break;
2511
2512 case 'F':
2513 input_target = output_target = optarg;
2514 break;
2515
2516 case 'j':
2517 p = find_section_list (optarg, TRUE);
2518 if (p->remove)
2519 fatal (_("%s both copied and removed"), optarg);
2520 p->copy = TRUE;
2521 sections_copied = TRUE;
2522 break;
2523
2524 case 'R':
2525 p = find_section_list (optarg, TRUE);
2526 if (p->copy)
2527 fatal (_("%s both copied and removed"), optarg);
2528 p->remove = TRUE;
2529 sections_removed = TRUE;
2530 break;
2531
2532 case 'S':
2533 strip_symbols = STRIP_ALL;
2534 break;
2535
2536 case 'g':
2537 strip_symbols = STRIP_DEBUG;
2538 break;
2539
2540 case OPTION_STRIP_UNNEEDED:
2541 strip_symbols = STRIP_UNNEEDED;
2542 break;
2543
2544 case OPTION_ONLY_KEEP_DEBUG:
2545 strip_symbols = STRIP_NONDEBUG;
2546 break;
2547
2548 case OPTION_ADD_GNU_DEBUGLINK:
2549 gnu_debuglink_filename = optarg;
2550 break;
2551
2552 case 'K':
2553 add_specific_symbol (optarg, &keep_specific_list);
2554 break;
2555
2556 case 'N':
2557 add_specific_symbol (optarg, &strip_specific_list);
2558 break;
2559
2560 case OPTION_STRIP_UNNEEDED_SYMBOL:
2561 add_specific_symbol (optarg, &strip_unneeded_list);
2562 break;
2563
2564 case 'L':
2565 add_specific_symbol (optarg, &localize_specific_list);
2566 break;
2567
2568 case OPTION_GLOBALIZE_SYMBOL:
2569 add_specific_symbol (optarg, &globalize_specific_list);
2570 break;
2571
2572 case 'G':
2573 add_specific_symbol (optarg, &keepglobal_specific_list);
2574 break;
2575
2576 case 'W':
2577 add_specific_symbol (optarg, &weaken_specific_list);
2578 break;
2579
2580 case 'p':
2581 preserve_dates = TRUE;
2582 break;
2583
2584 case 'w':
2585 wildcard = TRUE;
2586 break;
2587
2588 case 'x':
2589 discard_locals = LOCALS_ALL;
2590 break;
2591
2592 case 'X':
2593 discard_locals = LOCALS_START_L;
2594 break;
2595
2596 case 'v':
2597 verbose = TRUE;
2598 break;
2599
2600 case 'V':
2601 show_version = TRUE;
2602 break;
2603
2604 case OPTION_FORMATS_INFO:
2605 formats_info = TRUE;
2606 break;
2607
2608 case OPTION_WEAKEN:
2609 weaken = TRUE;
2610 break;
2611
2612 case OPTION_ADD_SECTION:
2613 {
2614 const char *s;
2615 off_t size;
2616 struct section_add *pa;
2617 int len;
2618 char *name;
2619 FILE *f;
2620
2621 s = strchr (optarg, '=');
2622
2623 if (s == NULL)
2624 fatal (_("bad format for %s"), "--add-section");
2625
2626 size = get_file_size (s + 1);
2627 if (size < 1)
2628 break;
2629
2630 pa = xmalloc (sizeof (struct section_add));
2631
2632 len = s - optarg;
2633 name = xmalloc (len + 1);
2634 strncpy (name, optarg, len);
2635 name[len] = '\0';
2636 pa->name = name;
2637
2638 pa->filename = s + 1;
2639 pa->size = size;
2640 pa->contents = xmalloc (size);
2641
2642 f = fopen (pa->filename, FOPEN_RB);
2643
2644 if (f == NULL)
2645 fatal (_("cannot open: %s: %s"),
2646 pa->filename, strerror (errno));
2647
2648 if (fread (pa->contents, 1, pa->size, f) == 0
2649 || ferror (f))
2650 fatal (_("%s: fread failed"), pa->filename);
2651
2652 fclose (f);
2653
2654 pa->next = add_sections;
2655 add_sections = pa;
2656 }
2657 break;
2658
2659 case OPTION_CHANGE_START:
2660 change_start = parse_vma (optarg, "--change-start");
2661 break;
2662
2663 case OPTION_CHANGE_SECTION_ADDRESS:
2664 case OPTION_CHANGE_SECTION_LMA:
2665 case OPTION_CHANGE_SECTION_VMA:
2666 {
2667 const char *s;
2668 int len;
2669 char *name;
2670 char *option = NULL;
2671 bfd_vma val;
2672 enum change_action what = CHANGE_IGNORE;
2673
2674 switch (c)
2675 {
2676 case OPTION_CHANGE_SECTION_ADDRESS:
2677 option = "--change-section-address";
2678 break;
2679 case OPTION_CHANGE_SECTION_LMA:
2680 option = "--change-section-lma";
2681 break;
2682 case OPTION_CHANGE_SECTION_VMA:
2683 option = "--change-section-vma";
2684 break;
2685 }
2686
2687 s = strchr (optarg, '=');
2688 if (s == NULL)
2689 {
2690 s = strchr (optarg, '+');
2691 if (s == NULL)
2692 {
2693 s = strchr (optarg, '-');
2694 if (s == NULL)
2695 fatal (_("bad format for %s"), option);
2696 }
2697 }
2698
2699 len = s - optarg;
2700 name = xmalloc (len + 1);
2701 strncpy (name, optarg, len);
2702 name[len] = '\0';
2703
2704 p = find_section_list (name, TRUE);
2705
2706 val = parse_vma (s + 1, option);
2707
2708 switch (*s)
2709 {
2710 case '=': what = CHANGE_SET; break;
2711 case '-': val = - val; /* Drop through. */
2712 case '+': what = CHANGE_MODIFY; break;
2713 }
2714
2715 switch (c)
2716 {
2717 case OPTION_CHANGE_SECTION_ADDRESS:
2718 p->change_vma = what;
2719 p->vma_val = val;
2720 /* Drop through. */
2721
2722 case OPTION_CHANGE_SECTION_LMA:
2723 p->change_lma = what;
2724 p->lma_val = val;
2725 break;
2726
2727 case OPTION_CHANGE_SECTION_VMA:
2728 p->change_vma = what;
2729 p->vma_val = val;
2730 break;
2731 }
2732 }
2733 break;
2734
2735 case OPTION_CHANGE_ADDRESSES:
2736 change_section_address = parse_vma (optarg, "--change-addresses");
2737 change_start = change_section_address;
2738 break;
2739
2740 case OPTION_CHANGE_WARNINGS:
2741 change_warn = TRUE;
2742 break;
2743
2744 case OPTION_CHANGE_LEADING_CHAR:
2745 change_leading_char = TRUE;
2746 break;
2747
2748 case OPTION_DEBUGGING:
2749 convert_debugging = TRUE;
2750 break;
2751
2752 case OPTION_GAP_FILL:
2753 {
2754 bfd_vma gap_fill_vma;
2755
2756 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2757 gap_fill = (bfd_byte) gap_fill_vma;
2758 if ((bfd_vma) gap_fill != gap_fill_vma)
2759 {
2760 char buff[20];
2761
2762 sprintf_vma (buff, gap_fill_vma);
2763
2764 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2765 buff, gap_fill);
2766 }
2767 gap_fill_set = TRUE;
2768 }
2769 break;
2770
2771 case OPTION_NO_CHANGE_WARNINGS:
2772 change_warn = FALSE;
2773 break;
2774
2775 case OPTION_PAD_TO:
2776 pad_to = parse_vma (optarg, "--pad-to");
2777 pad_to_set = TRUE;
2778 break;
2779
2780 case OPTION_REMOVE_LEADING_CHAR:
2781 remove_leading_char = TRUE;
2782 break;
2783
2784 case OPTION_REDEFINE_SYM:
2785 {
2786 /* Push this redefinition onto redefine_symbol_list. */
2787
2788 int len;
2789 const char *s;
2790 const char *nextarg;
2791 char *source, *target;
2792
2793 s = strchr (optarg, '=');
2794 if (s == NULL)
2795 fatal (_("bad format for %s"), "--redefine-sym");
2796
2797 len = s - optarg;
2798 source = xmalloc (len + 1);
2799 strncpy (source, optarg, len);
2800 source[len] = '\0';
2801
2802 nextarg = s + 1;
2803 len = strlen (nextarg);
2804 target = xmalloc (len + 1);
2805 strcpy (target, nextarg);
2806
2807 redefine_list_append ("--redefine-sym", source, target);
2808
2809 free (source);
2810 free (target);
2811 }
2812 break;
2813
2814 case OPTION_REDEFINE_SYMS:
2815 add_redefine_syms_file (optarg);
2816 break;
2817
2818 case OPTION_SET_SECTION_FLAGS:
2819 {
2820 const char *s;
2821 int len;
2822 char *name;
2823
2824 s = strchr (optarg, '=');
2825 if (s == NULL)
2826 fatal (_("bad format for %s"), "--set-section-flags");
2827
2828 len = s - optarg;
2829 name = xmalloc (len + 1);
2830 strncpy (name, optarg, len);
2831 name[len] = '\0';
2832
2833 p = find_section_list (name, TRUE);
2834
2835 p->set_flags = TRUE;
2836 p->flags = parse_flags (s + 1);
2837 }
2838 break;
2839
2840 case OPTION_RENAME_SECTION:
2841 {
2842 flagword flags;
2843 const char *eq, *fl;
2844 char *old_name;
2845 char *new_name;
2846 unsigned int len;
2847
2848 eq = strchr (optarg, '=');
2849 if (eq == NULL)
2850 fatal (_("bad format for %s"), "--rename-section");
2851
2852 len = eq - optarg;
2853 if (len == 0)
2854 fatal (_("bad format for %s"), "--rename-section");
2855
2856 old_name = xmalloc (len + 1);
2857 strncpy (old_name, optarg, len);
2858 old_name[len] = 0;
2859
2860 eq++;
2861 fl = strchr (eq, ',');
2862 if (fl)
2863 {
2864 flags = parse_flags (fl + 1);
2865 len = fl - eq;
2866 }
2867 else
2868 {
2869 flags = -1;
2870 len = strlen (eq);
2871 }
2872
2873 if (len == 0)
2874 fatal (_("bad format for %s"), "--rename-section");
2875
2876 new_name = xmalloc (len + 1);
2877 strncpy (new_name, eq, len);
2878 new_name[len] = 0;
2879
2880 add_section_rename (old_name, new_name, flags);
2881 }
2882 break;
2883
2884 case OPTION_SET_START:
2885 set_start = parse_vma (optarg, "--set-start");
2886 set_start_set = TRUE;
2887 break;
2888
2889 case OPTION_SREC_LEN:
2890 Chunk = parse_vma (optarg, "--srec-len");
2891 break;
2892
2893 case OPTION_SREC_FORCES3:
2894 S3Forced = TRUE;
2895 break;
2896
2897 case OPTION_STRIP_SYMBOLS:
2898 add_specific_symbols (optarg, &strip_specific_list);
2899 break;
2900
2901 case OPTION_STRIP_UNNEEDED_SYMBOLS:
2902 add_specific_symbols (optarg, &strip_unneeded_list);
2903 break;
2904
2905 case OPTION_KEEP_SYMBOLS:
2906 add_specific_symbols (optarg, &keep_specific_list);
2907 break;
2908
2909 case OPTION_LOCALIZE_SYMBOLS:
2910 add_specific_symbols (optarg, &localize_specific_list);
2911 break;
2912
2913 case OPTION_GLOBALIZE_SYMBOLS:
2914 add_specific_symbols (optarg, &globalize_specific_list);
2915 break;
2916
2917 case OPTION_KEEPGLOBAL_SYMBOLS:
2918 add_specific_symbols (optarg, &keepglobal_specific_list);
2919 break;
2920
2921 case OPTION_WEAKEN_SYMBOLS:
2922 add_specific_symbols (optarg, &weaken_specific_list);
2923 break;
2924
2925 case OPTION_ALT_MACH_CODE:
2926 use_alt_mach_code = atoi (optarg);
2927 if (use_alt_mach_code <= 0)
2928 fatal (_("alternate machine code index must be positive"));
2929 break;
2930
2931 case OPTION_PREFIX_SYMBOLS:
2932 prefix_symbols_string = optarg;
2933 break;
2934
2935 case OPTION_PREFIX_SECTIONS:
2936 prefix_sections_string = optarg;
2937 break;
2938
2939 case OPTION_PREFIX_ALLOC_SECTIONS:
2940 prefix_alloc_sections_string = optarg;
2941 break;
2942
2943 case OPTION_READONLY_TEXT:
2944 bfd_flags_to_set |= WP_TEXT;
2945 bfd_flags_to_clear &= ~WP_TEXT;
2946 break;
2947
2948 case OPTION_WRITABLE_TEXT:
2949 bfd_flags_to_clear |= WP_TEXT;
2950 bfd_flags_to_set &= ~WP_TEXT;
2951 break;
2952
2953 case OPTION_PURE:
2954 bfd_flags_to_set |= D_PAGED;
2955 bfd_flags_to_clear &= ~D_PAGED;
2956 break;
2957
2958 case OPTION_IMPURE:
2959 bfd_flags_to_clear |= D_PAGED;
2960 bfd_flags_to_set &= ~D_PAGED;
2961 break;
2962
2963 case 0:
2964 /* We've been given a long option. */
2965 break;
2966
2967 case 'H':
2968 case 'h':
2969 copy_usage (stdout, 0);
2970
2971 default:
2972 copy_usage (stderr, 1);
2973 }
2974 }
2975
2976 if (formats_info)
2977 {
2978 display_info ();
2979 return 0;
2980 }
2981
2982 if (show_version)
2983 print_version ("objcopy");
2984
2985 if (copy_byte >= interleave)
2986 fatal (_("byte number must be less than interleave"));
2987
2988 if (optind == argc || optind + 2 < argc)
2989 copy_usage (stderr, 1);
2990
2991 input_filename = argv[optind];
2992 if (optind + 1 < argc)
2993 output_filename = argv[optind + 1];
2994
2995 /* Default is to strip no symbols. */
2996 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2997 strip_symbols = STRIP_NONE;
2998
2999 if (output_target == NULL)
3000 output_target = input_target;
3001
3002 if (binary_architecture != NULL)
3003 {
3004 if (input_target && strcmp (input_target, "binary") == 0)
3005 {
3006 const bfd_arch_info_type * temp_arch_info;
3007
3008 temp_arch_info = bfd_scan_arch (binary_architecture);
3009
3010 if (temp_arch_info != NULL)
3011 {
3012 bfd_external_binary_architecture = temp_arch_info->arch;
3013 bfd_external_machine = temp_arch_info->mach;
3014 }
3015 else
3016 fatal (_("architecture %s unknown"), binary_architecture);
3017 }
3018 else
3019 {
3020 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3021 non_fatal (_(" Argument %s ignored"), binary_architecture);
3022 }
3023 }
3024
3025 if (preserve_dates)
3026 if (stat (input_filename, & statbuf) < 0)
3027 fatal (_("warning: could not locate '%s'. System error message: %s"),
3028 input_filename, strerror (errno));
3029
3030 /* If there is no destination file, or the source and destination files
3031 are the same, then create a temp and rename the result into the input. */
3032 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
3033 {
3034 char *tmpname = make_tempname (input_filename);
3035
3036 copy_file (input_filename, tmpname, input_target, output_target);
3037 if (status == 0)
3038 {
3039 if (preserve_dates)
3040 set_times (tmpname, &statbuf);
3041 smart_rename (tmpname, input_filename, preserve_dates);
3042 }
3043 else
3044 unlink (tmpname);
3045 }
3046 else
3047 {
3048 copy_file (input_filename, output_filename, input_target, output_target);
3049
3050 if (status == 0 && preserve_dates)
3051 set_times (output_filename, &statbuf);
3052 else if (status != 0)
3053 unlink_if_ordinary (output_filename);
3054 }
3055
3056 if (change_warn)
3057 {
3058 for (p = change_sections; p != NULL; p = p->next)
3059 {
3060 if (! p->used)
3061 {
3062 if (p->change_vma != CHANGE_IGNORE)
3063 {
3064 char buff [20];
3065
3066 sprintf_vma (buff, p->vma_val);
3067
3068 /* xgettext:c-format */
3069 non_fatal (_("%s %s%c0x%s never used"),
3070 "--change-section-vma",
3071 p->name,
3072 p->change_vma == CHANGE_SET ? '=' : '+',
3073 buff);
3074 }
3075
3076 if (p->change_lma != CHANGE_IGNORE)
3077 {
3078 char buff [20];
3079
3080 sprintf_vma (buff, p->lma_val);
3081
3082 /* xgettext:c-format */
3083 non_fatal (_("%s %s%c0x%s never used"),
3084 "--change-section-lma",
3085 p->name,
3086 p->change_lma == CHANGE_SET ? '=' : '+',
3087 buff);
3088 }
3089 }
3090 }
3091 }
3092
3093 return 0;
3094 }
3095
3096 int
3097 main (int argc, char *argv[])
3098 {
3099 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3100 setlocale (LC_MESSAGES, "");
3101 #endif
3102 #if defined (HAVE_SETLOCALE)
3103 setlocale (LC_CTYPE, "");
3104 #endif
3105 bindtextdomain (PACKAGE, LOCALEDIR);
3106 textdomain (PACKAGE);
3107
3108 program_name = argv[0];
3109 xmalloc_set_program_name (program_name);
3110
3111 START_PROGRESS (program_name, 0);
3112
3113 strip_symbols = STRIP_UNDEF;
3114 discard_locals = LOCALS_UNDEF;
3115
3116 bfd_init ();
3117 set_default_bfd_target ();
3118
3119 if (is_strip < 0)
3120 {
3121 int i = strlen (program_name);
3122 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3123 /* Drop the .exe suffix, if any. */
3124 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3125 {
3126 i -= 4;
3127 program_name[i] = '\0';
3128 }
3129 #endif
3130 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
3131 }
3132
3133 if (is_strip)
3134 strip_main (argc, argv);
3135 else
3136 copy_main (argc, argv);
3137
3138 END_PROGRESS (program_name);
3139
3140 return status;
3141 }