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