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