Move display_info() function into bucomm.c
[deliverable/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,
66491ebc 3 2001, 2002, 2003
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);
66491ebc
AM
801 bfd_asymbol_name (sym) = new_name;
802 name = new_name;
57938635
AM
803 }
804
d7fb0dd2
NC
805 /* Check if we will remove the current leading character. */
806 rem_leading_char =
807 (name[0] == bfd_get_symbol_leading_char (abfd))
808 && (change_leading_char
809 || (remove_leading_char
810 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
811 || undefined
812 || bfd_is_com_section (bfd_get_section (sym)))));
813
814 /* Check if we will add a new leading character. */
815 add_leading_char =
816 change_leading_char
817 && (bfd_get_symbol_leading_char (obfd) != '\0')
818 && (bfd_get_symbol_leading_char (abfd) == '\0'
819 || (name[0] == bfd_get_symbol_leading_char (abfd)));
820
821 /* Short circuit for change_leading_char if we can do it in-place. */
822 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
823 {
824 name[0] = bfd_get_symbol_leading_char (obfd);
825 bfd_asymbol_name (sym) = name;
826 rem_leading_char = FALSE;
827 add_leading_char = FALSE;
828 }
829
830 /* Remove leading char. */
831 if (rem_leading_char)
66491ebc 832 bfd_asymbol_name (sym) = ++name;
d7fb0dd2
NC
833
834 /* Add new leading char and/or prefix. */
835 if (add_leading_char || prefix_symbols_string)
836 {
837 char *n, *ptr;
838
839 ptr = n = xmalloc (1 + strlen (prefix_symbols_string) + strlen (name) + 1);
840 if (add_leading_char)
841 *ptr++ = bfd_get_symbol_leading_char (obfd);
842
843 if (prefix_symbols_string)
844 {
845 strcpy (ptr, prefix_symbols_string);
846 ptr += strlen (prefix_symbols_string);
847 }
848
849 strcpy (ptr, name);
66491ebc
AM
850 bfd_asymbol_name (sym) = n;
851 name = n;
252b5132
RH
852 }
853
252b5132
RH
854 if (strip_symbols == STRIP_ALL)
855 keep = 0;
856 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
857 || ((flags & BSF_SECTION_SYM) != 0
858 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
859 & BSF_KEEP) != 0))
860 keep = 1;
0af11b59 861 else if (relocatable /* Relocatable file. */
d8121479
L
862 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
863 keep = 1;
16b2b71c
NC
864 else if (bfd_decode_symclass (sym) == 'I')
865 /* Global symbols in $idata sections need to be retained
b34976b6 866 even if relocatable is FALSE. External users of the
16b2b71c
NC
867 library containing the $idata section may reference these
868 symbols. */
af3bdff7 869 keep = 1;
252b5132
RH
870 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
871 || (flags & BSF_WEAK) != 0
24e01a36 872 || undefined
252b5132
RH
873 || bfd_is_com_section (bfd_get_section (sym)))
874 keep = strip_symbols != STRIP_UNNEEDED;
875 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
876 keep = (strip_symbols != STRIP_DEBUG
877 && strip_symbols != STRIP_UNNEEDED
878 && ! convert_debugging);
af3bdff7
NC
879 else if (bfd_get_section (sym)->comdat)
880 /* COMDAT sections store special information in local
881 symbols, so we cannot risk stripping any of them. */
882 keep = 1;
252b5132
RH
883 else /* Local symbol. */
884 keep = (strip_symbols != STRIP_UNNEEDED
885 && (discard_locals != LOCALS_ALL
886 && (discard_locals != LOCALS_START_L
887 || ! bfd_is_local_label (abfd, sym))));
888
889 if (keep && is_specified_symbol (name, strip_specific_list))
890 keep = 0;
891 if (!keep && is_specified_symbol (name, keep_specific_list))
892 keep = 1;
893 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
894 keep = 0;
e0c60db2 895
252b5132
RH
896 if (keep && (flags & BSF_GLOBAL) != 0
897 && (weaken || is_specified_symbol (name, weaken_specific_list)))
898 {
899 sym->flags &=~ BSF_GLOBAL;
900 sym->flags |= BSF_WEAK;
901 }
24e01a36 902 if (keep && !undefined && (flags & (BSF_GLOBAL | BSF_WEAK))
16b2b71c
NC
903 && (is_specified_symbol (name, localize_specific_list)
904 || (keepglobal_specific_list != NULL
905 && ! is_specified_symbol (name, keepglobal_specific_list))))
252b5132
RH
906 {
907 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
908 sym->flags |= BSF_LOCAL;
909 }
910
911 if (keep)
912 to[dst_count++] = sym;
913 }
914
915 to[dst_count] = NULL;
916
917 return dst_count;
918}
919
594ef5db
NC
920/* Find the redefined name of symbol SOURCE. */
921
57938635
AM
922static const char *
923lookup_sym_redefinition (source)
924 const char *source;
925{
57938635
AM
926 struct redefine_node *list;
927
57938635 928 for (list = redefine_sym_list; list != NULL; list = list->next)
594ef5db
NC
929 if (strcmp (source, list->source) == 0)
930 return list->target;
931
932 return source;
57938635
AM
933}
934
594ef5db 935/* Add a node to a symbol redefine list. */
57938635
AM
936
937static void
938redefine_list_append (source, target)
939 const char *source;
940 const char *target;
941{
942 struct redefine_node **p;
943 struct redefine_node *list;
944 struct redefine_node *new_node;
945
946 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
947 {
948 if (strcmp (source, list->source) == 0)
594ef5db
NC
949 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
950 "--redefine-sym",
951 source);
57938635
AM
952
953 if (strcmp (target, list->target) == 0)
594ef5db
NC
954 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
955 "--redefine-sym",
956 target);
57938635
AM
957 }
958
959 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
960
961 new_node->source = strdup (source);
962 new_node->target = strdup (target);
963 new_node->next = NULL;
964
965 *p = new_node;
966}
967
252b5132
RH
968/* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
969 Adjust *SIZE. */
970
971static void
972filter_bytes (memhunk, size)
973 char *memhunk;
974 bfd_size_type *size;
975{
976 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
977
978 for (; from < end; from += interleave)
979 *to++ = *from;
594ef5db 980
b4c96d0d 981 if (*size % interleave > (bfd_size_type) copy_byte)
252b5132
RH
982 *size = (*size / interleave) + 1;
983 else
984 *size /= interleave;
985}
986
987/* Copy object file IBFD onto OBFD. */
988
989static void
990copy_object (ibfd, obfd)
991 bfd *ibfd;
992 bfd *obfd;
993{
994 bfd_vma start;
995 long symcount;
996 asection **osections = NULL;
997 bfd_size_type *gaps = NULL;
998 bfd_size_type max_gap = 0;
999 long symsize;
1000 PTR dhandle;
66491ebc
AM
1001 enum bfd_architecture iarch;
1002 unsigned int imach;
252b5132 1003
23719f39
NC
1004 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1005 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1006 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1007 {
1008 fatal (_("Unable to change endianness of input file(s)"));
1009 return;
1010 }
252b5132
RH
1011
1012 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1013 RETURN_NONFATAL (bfd_get_filename (obfd));
1014
1015 if (verbose)
1016 printf (_("copy from %s(%s) to %s(%s)\n"),
1017 bfd_get_filename (ibfd), bfd_get_target (ibfd),
1018 bfd_get_filename (obfd), bfd_get_target (obfd));
1019
1020 if (set_start_set)
1021 start = set_start;
1022 else
1023 start = bfd_get_start_address (ibfd);
1024 start += change_start;
1025
0af11b59
KH
1026 /* Neither the start address nor the flags
1027 need to be set for a core file. */
4dd67f29
MS
1028 if (bfd_get_format (obfd) != bfd_core)
1029 {
1030 if (!bfd_set_start_address (obfd, start)
1031 || !bfd_set_file_flags (obfd,
1032 (bfd_get_file_flags (ibfd)
1033 & bfd_applicable_file_flags (obfd))))
1034 RETURN_NONFATAL (bfd_get_filename (ibfd));
1035 }
252b5132 1036
594ef5db 1037 /* Copy architecture of input file to output file. */
66491ebc
AM
1038 iarch = bfd_get_arch (ibfd);
1039 imach = bfd_get_mach (ibfd);
1040 if (!bfd_set_arch_mach (obfd, iarch, imach)
212a3c4d
L
1041 && (ibfd->target_defaulted
1042 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
252b5132
RH
1043 non_fatal (_("Warning: Output file cannot represent architecture %s"),
1044 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1045 bfd_get_mach (ibfd)));
57938635 1046
252b5132
RH
1047 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1048 RETURN_NONFATAL (bfd_get_filename (ibfd));
1049
1050 if (isympp)
1051 free (isympp);
57938635 1052
252b5132
RH
1053 if (osympp != isympp)
1054 free (osympp);
1055
1056 /* BFD mandates that all output sections be created and sizes set before
1057 any output is done. Thus, we traverse all sections multiple times. */
1058 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
1059
1060 if (add_sections != NULL)
1061 {
1062 struct section_add *padd;
1063 struct section_list *pset;
1064
1065 for (padd = add_sections; padd != NULL; padd = padd->next)
1066 {
1067 padd->section = bfd_make_section (obfd, padd->name);
1068 if (padd->section == NULL)
1069 {
1070 non_fatal (_("can't create section `%s': %s"),
1071 padd->name, bfd_errmsg (bfd_get_error ()));
1072 status = 1;
1073 return;
1074 }
1075 else
1076 {
1077 flagword flags;
57938635 1078
252b5132
RH
1079 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1080 RETURN_NONFATAL (bfd_get_filename (obfd));
1081
b34976b6 1082 pset = find_section_list (padd->name, FALSE);
252b5132 1083 if (pset != NULL)
b34976b6 1084 pset->used = TRUE;
252b5132
RH
1085
1086 if (pset != NULL && pset->set_flags)
1087 flags = pset->flags | SEC_HAS_CONTENTS;
1088 else
1089 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
57938635 1090
252b5132
RH
1091 if (! bfd_set_section_flags (obfd, padd->section, flags))
1092 RETURN_NONFATAL (bfd_get_filename (obfd));
1093
1094 if (pset != NULL)
1095 {
1096 if (pset->change_vma != CHANGE_IGNORE)
1097 if (! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
1098 RETURN_NONFATAL (bfd_get_filename (obfd));
57938635 1099
252b5132
RH
1100 if (pset->change_lma != CHANGE_IGNORE)
1101 {
1102 padd->section->lma = pset->lma_val;
57938635 1103
252b5132
RH
1104 if (! bfd_set_section_alignment
1105 (obfd, padd->section,
1106 bfd_section_alignment (obfd, padd->section)))
1107 RETURN_NONFATAL (bfd_get_filename (obfd));
1108 }
1109 }
1110 }
1111 }
1112 }
1113
1114 if (gap_fill_set || pad_to_set)
1115 {
1116 asection **set;
1117 unsigned int c, i;
1118
1119 /* We must fill in gaps between the sections and/or we must pad
1120 the last section to a specified address. We do this by
1121 grabbing a list of the sections, sorting them by VMA, and
1122 increasing the section sizes as required to fill the gaps.
1123 We write out the gap contents below. */
1124
1125 c = bfd_count_sections (obfd);
1126 osections = (asection **) xmalloc (c * sizeof (asection *));
1127 set = osections;
1128 bfd_map_over_sections (obfd, get_sections, (void *) &set);
1129
1130 qsort (osections, c, sizeof (asection *), compare_section_lma);
1131
1132 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
1133 memset (gaps, 0, c * sizeof (bfd_size_type));
1134
1135 if (gap_fill_set)
1136 {
1137 for (i = 0; i < c - 1; i++)
1138 {
1139 flagword flags;
1140 bfd_size_type size;
1141 bfd_vma gap_start, gap_stop;
1142
1143 flags = bfd_get_section_flags (obfd, osections[i]);
1144 if ((flags & SEC_HAS_CONTENTS) == 0
1145 || (flags & SEC_LOAD) == 0)
1146 continue;
1147
1148 size = bfd_section_size (obfd, osections[i]);
1149 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1150 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1151 if (gap_start < gap_stop)
1152 {
1153 if (! bfd_set_section_size (obfd, osections[i],
1154 size + (gap_stop - gap_start)))
1155 {
1156 non_fatal (_("Can't fill gap after %s: %s"),
0af11b59
KH
1157 bfd_get_section_name (obfd, osections[i]),
1158 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1159 status = 1;
1160 break;
1161 }
1162 gaps[i] = gap_stop - gap_start;
1163 if (max_gap < gap_stop - gap_start)
1164 max_gap = gap_stop - gap_start;
1165 }
1166 }
1167 }
1168
1169 if (pad_to_set)
1170 {
1171 bfd_vma lma;
1172 bfd_size_type size;
1173
1174 lma = bfd_section_lma (obfd, osections[c - 1]);
1175 size = bfd_section_size (obfd, osections[c - 1]);
1176 if (lma + size < pad_to)
1177 {
1178 if (! bfd_set_section_size (obfd, osections[c - 1],
1179 pad_to - lma))
1180 {
1181 non_fatal (_("Can't add padding to %s: %s"),
0af11b59
KH
1182 bfd_get_section_name (obfd, osections[c - 1]),
1183 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1184 status = 1;
1185 }
1186 else
1187 {
1188 gaps[c - 1] = pad_to - (lma + size);
1189 if (max_gap < pad_to - (lma + size))
1190 max_gap = pad_to - (lma + size);
1191 }
1192 }
1193 }
1194 }
1195
594ef5db
NC
1196 /* Symbol filtering must happen after the output sections
1197 have been created, but before their contents are set. */
252b5132
RH
1198 dhandle = NULL;
1199 symsize = bfd_get_symtab_upper_bound (ibfd);
1200 if (symsize < 0)
1201 RETURN_NONFATAL (bfd_get_filename (ibfd));
57938635 1202
252b5132
RH
1203 osympp = isympp = (asymbol **) xmalloc (symsize);
1204 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1205 if (symcount < 0)
1206 RETURN_NONFATAL (bfd_get_filename (ibfd));
57938635 1207
252b5132
RH
1208 if (convert_debugging)
1209 dhandle = read_debugging_info (ibfd, isympp, symcount);
57938635
AM
1210
1211 if (strip_symbols == STRIP_DEBUG
252b5132
RH
1212 || strip_symbols == STRIP_ALL
1213 || strip_symbols == STRIP_UNNEEDED
1214 || discard_locals != LOCALS_UNDEF
1215 || strip_specific_list != NULL
1216 || keep_specific_list != NULL
1217 || localize_specific_list != NULL
16b2b71c 1218 || keepglobal_specific_list != NULL
252b5132 1219 || weaken_specific_list != NULL
d7fb0dd2 1220 || prefix_symbols_string
252b5132 1221 || sections_removed
f91ea849 1222 || sections_copied
252b5132
RH
1223 || convert_debugging
1224 || change_leading_char
1225 || remove_leading_char
57938635 1226 || redefine_sym_list
252b5132
RH
1227 || weaken)
1228 {
1229 /* Mark symbols used in output relocations so that they
1230 are kept, even if they are local labels or static symbols.
57938635 1231
252b5132
RH
1232 Note we iterate over the input sections examining their
1233 relocations since the relocations for the output sections
1234 haven't been set yet. mark_symbols_used_in_relocations will
1235 ignore input sections which have no corresponding output
1236 section. */
1237 if (strip_symbols != STRIP_ALL)
1238 bfd_map_over_sections (ibfd,
1239 mark_symbols_used_in_relocations,
1240 (PTR)isympp);
1241 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1242 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1243 }
1244
1245 if (convert_debugging && dhandle != NULL)
1246 {
1247 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1248 {
1249 status = 1;
1250 return;
1251 }
1252 }
1253
1254 bfd_set_symtab (obfd, osympp, symcount);
1255
1256 /* This has to happen after the symbol table has been set. */
1257 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
1258
1259 if (add_sections != NULL)
1260 {
1261 struct section_add *padd;
1262
1263 for (padd = add_sections; padd != NULL; padd = padd->next)
1264 {
1265 if (! bfd_set_section_contents (obfd, padd->section,
1266 (PTR) padd->contents,
1267 (file_ptr) 0,
1268 (bfd_size_type) padd->size))
1269 RETURN_NONFATAL (bfd_get_filename (obfd));
1270 }
1271 }
1272
1273 if (gap_fill_set || pad_to_set)
1274 {
1275 bfd_byte *buf;
1276 int c, i;
1277
1278 /* Fill in the gaps. */
252b5132
RH
1279 if (max_gap > 8192)
1280 max_gap = 8192;
1281 buf = (bfd_byte *) xmalloc (max_gap);
1282 memset (buf, gap_fill, (size_t) max_gap);
1283
1284 c = bfd_count_sections (obfd);
1285 for (i = 0; i < c; i++)
1286 {
1287 if (gaps[i] != 0)
1288 {
1289 bfd_size_type left;
1290 file_ptr off;
1291
1292 left = gaps[i];
1293 off = bfd_section_size (obfd, osections[i]) - left;
594ef5db 1294
252b5132
RH
1295 while (left > 0)
1296 {
1297 bfd_size_type now;
1298
1299 if (left > 8192)
1300 now = 8192;
1301 else
1302 now = left;
1303
1304 if (! bfd_set_section_contents (obfd, osections[i], buf,
1305 off, now))
1306 RETURN_NONFATAL (bfd_get_filename (obfd));
1307
1308 left -= now;
1309 off += now;
1310 }
1311 }
1312 }
1313 }
1314
1315 /* Allow the BFD backend to copy any private data it understands
1316 from the input BFD to the output BFD. This is done last to
1317 permit the routine to look at the filtered symbol table, which is
1318 important for the ECOFF code at least. */
594ef5db 1319 if (! bfd_copy_private_bfd_data (ibfd, obfd))
252b5132
RH
1320 {
1321 non_fatal (_("%s: error copying private BFD data: %s"),
1322 bfd_get_filename (obfd),
1323 bfd_errmsg (bfd_get_error ()));
1324 status = 1;
1325 return;
1326 }
1ae8b3d2
AO
1327
1328 /* Switch to the alternate machine code. We have to do this at the
1329 very end, because we only initialize the header when we create
1330 the first section. */
1331 if (use_alt_mach_code != 0)
1332 {
1333 if (!bfd_alt_mach_code (obfd, use_alt_mach_code))
1334 non_fatal (_("unknown alternate machine code, ignored"));
1335 }
252b5132
RH
1336}
1337
4c168fa3
AM
1338#undef MKDIR
1339#if defined (_WIN32) && !defined (__CYGWIN32__)
1340#define MKDIR(DIR, MODE) mkdir (DIR)
1341#else
1342#define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1343#endif
1344
252b5132
RH
1345/* Read each archive element in turn from IBFD, copy the
1346 contents to temp file, and keep the temp file handle. */
1347
1348static void
1349copy_archive (ibfd, obfd, output_target)
1350 bfd *ibfd;
1351 bfd *obfd;
1352 const char *output_target;
1353{
1354 struct name_list
1355 {
1356 struct name_list *next;
4c168fa3 1357 const char *name;
252b5132
RH
1358 bfd *obfd;
1359 } *list, *l;
1360 bfd **ptr = &obfd->archive_head;
1361 bfd *this_element;
1362 char *dir = make_tempname (bfd_get_filename (obfd));
1363
1364 /* Make a temp directory to hold the contents. */
4c168fa3 1365 if (MKDIR (dir, 0700) != 0)
252b5132
RH
1366 {
1367 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1368 dir, strerror (errno));
1369 }
1370 obfd->has_armap = ibfd->has_armap;
1371
1372 list = NULL;
1373
1374 this_element = bfd_openr_next_archived_file (ibfd, NULL);
594ef5db 1375
b667df2e
AM
1376 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1377 RETURN_NONFATAL (bfd_get_filename (obfd));
1378
252b5132
RH
1379 while (!status && this_element != (bfd *) NULL)
1380 {
4c168fa3
AM
1381 char *output_name;
1382 bfd *output_bfd;
252b5132 1383 bfd *last_element;
8066d1a2
AS
1384 struct stat buf;
1385 int stat_status = 0;
1386
4c168fa3
AM
1387 /* Create an output file for this member. */
1388 output_name = concat (dir, "/",
1389 bfd_get_filename (this_element), (char *) 0);
1390
1391 /* If the file already exists, make another temp dir. */
1392 if (stat (output_name, &buf) >= 0)
1393 {
1394 output_name = make_tempname (output_name);
1395 if (MKDIR (output_name, 0700) != 0)
1396 {
1397 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1398 output_name, strerror (errno));
1399 }
1400 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1401 l->name = output_name;
1402 l->next = list;
1403 l->obfd = NULL;
1404 list = l;
1405 output_name = concat (output_name, "/",
1406 bfd_get_filename (this_element), (char *) 0);
1407 }
1408
1409 output_bfd = bfd_openw (output_name, output_target);
8066d1a2
AS
1410 if (preserve_dates)
1411 {
1412 stat_status = bfd_stat_arch_elt (this_element, &buf);
594ef5db 1413
8066d1a2
AS
1414 if (stat_status != 0)
1415 non_fatal (_("internal stat error on %s"),
1416 bfd_get_filename (this_element));
1417 }
252b5132
RH
1418
1419 l = (struct name_list *) xmalloc (sizeof (struct name_list));
1420 l->name = output_name;
1421 l->next = list;
1422 list = l;
1423
1424 if (output_bfd == (bfd *) NULL)
1425 RETURN_NONFATAL (output_name);
1426
b34976b6 1427 if (bfd_check_format (this_element, bfd_object))
252b5132
RH
1428 copy_object (this_element, output_bfd);
1429
1430 if (!bfd_close (output_bfd))
1431 {
1432 bfd_nonfatal (bfd_get_filename (output_bfd));
0af11b59 1433 /* Error in new object file. Don't change archive. */
252b5132
RH
1434 status = 1;
1435 }
1436
8066d1a2
AS
1437 if (preserve_dates && stat_status == 0)
1438 set_times (output_name, &buf);
1439
252b5132
RH
1440 /* Open the newly output file and attach to our list. */
1441 output_bfd = bfd_openr (output_name, output_target);
1442
1443 l->obfd = output_bfd;
1444
1445 *ptr = output_bfd;
1446 ptr = &output_bfd->next;
1447
1448 last_element = this_element;
1449
1450 this_element = bfd_openr_next_archived_file (ibfd, last_element);
1451
1452 bfd_close (last_element);
1453 }
1454 *ptr = (bfd *) NULL;
1455
1456 if (!bfd_close (obfd))
1457 RETURN_NONFATAL (bfd_get_filename (obfd));
1458
1459 if (!bfd_close (ibfd))
1460 RETURN_NONFATAL (bfd_get_filename (ibfd));
1461
1462 /* Delete all the files that we opened. */
1463 for (l = list; l != NULL; l = l->next)
1464 {
4c168fa3
AM
1465 if (l->obfd == NULL)
1466 rmdir (l->name);
1467 else
1468 {
1469 bfd_close (l->obfd);
1470 unlink (l->name);
1471 }
252b5132
RH
1472 }
1473 rmdir (dir);
1474}
1475
1476/* The top-level control. */
1477
1478static void
1479copy_file (input_filename, output_filename, input_target, output_target)
1480 const char *input_filename;
1481 const char *output_filename;
1482 const char *input_target;
1483 const char *output_target;
1484{
1485 bfd *ibfd;
49c12576
AM
1486 char **obj_matching;
1487 char **core_matching;
252b5132
RH
1488
1489 /* To allow us to do "strip *" without dying on the first
1490 non-object file, failures are nonfatal. */
252b5132
RH
1491 ibfd = bfd_openr (input_filename, input_target);
1492 if (ibfd == NULL)
1493 RETURN_NONFATAL (input_filename);
1494
1495 if (bfd_check_format (ibfd, bfd_archive))
1496 {
1497 bfd *obfd;
1498
1499 /* bfd_get_target does not return the correct value until
1500 bfd_check_format succeeds. */
1501 if (output_target == NULL)
1502 output_target = bfd_get_target (ibfd);
1503
1504 obfd = bfd_openw (output_filename, output_target);
1505 if (obfd == NULL)
1506 RETURN_NONFATAL (output_filename);
1507
1508 copy_archive (ibfd, obfd, output_target);
1509 }
49c12576 1510 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
252b5132
RH
1511 {
1512 bfd *obfd;
49c12576 1513 do_copy:
252b5132
RH
1514 /* bfd_get_target does not return the correct value until
1515 bfd_check_format succeeds. */
1516 if (output_target == NULL)
1517 output_target = bfd_get_target (ibfd);
1518
1519 obfd = bfd_openw (output_filename, output_target);
1520 if (obfd == NULL)
1521 RETURN_NONFATAL (output_filename);
1522
1523 copy_object (ibfd, obfd);
1524
1525 if (!bfd_close (obfd))
1526 RETURN_NONFATAL (output_filename);
1527
1528 if (!bfd_close (ibfd))
1529 RETURN_NONFATAL (input_filename);
1530 }
1531 else
1532 {
49c12576
AM
1533 bfd_error_type obj_error = bfd_get_error ();
1534 bfd_error_type core_error;
b34976b6 1535
49c12576
AM
1536 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1537 {
1538 /* This probably can't happen.. */
1539 if (obj_error == bfd_error_file_ambiguously_recognized)
1540 free (obj_matching);
1541 goto do_copy;
1542 }
1543
1544 core_error = bfd_get_error ();
1545 /* Report the object error in preference to the core error. */
1546 if (obj_error != core_error)
1547 bfd_set_error (obj_error);
1548
252b5132 1549 bfd_nonfatal (input_filename);
57938635 1550
49c12576
AM
1551 if (obj_error == bfd_error_file_ambiguously_recognized)
1552 {
1553 list_matching_formats (obj_matching);
1554 free (obj_matching);
1555 }
1556 if (core_error == bfd_error_file_ambiguously_recognized)
252b5132 1557 {
49c12576
AM
1558 list_matching_formats (core_matching);
1559 free (core_matching);
252b5132 1560 }
57938635 1561
252b5132
RH
1562 status = 1;
1563 }
1564}
1565
594ef5db
NC
1566/* Add a name to the section renaming list. */
1567
1568static void
1569add_section_rename (old_name, new_name, flags)
1570 const char * old_name;
1571 const char * new_name;
1572 flagword flags;
1573{
1574 section_rename * rename;
1575
1576 /* Check for conflicts first. */
1577 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1578 if (strcmp (rename->old_name, old_name) == 0)
1579 {
1580 /* Silently ignore duplicate definitions. */
1581 if (strcmp (rename->new_name, new_name) == 0
1582 && rename->flags == flags)
1583 return;
0af11b59 1584
594ef5db
NC
1585 fatal (_("Multiple renames of section %s"), old_name);
1586 }
1587
1588 rename = (section_rename *) xmalloc (sizeof (* rename));
1589
1590 rename->old_name = old_name;
1591 rename->new_name = new_name;
1592 rename->flags = flags;
1593 rename->next = section_rename_list;
0af11b59 1594
594ef5db
NC
1595 section_rename_list = rename;
1596}
1597
1598/* Check the section rename list for a new name of the input section
1599 ISECTION. Return the new name if one is found.
1600 Also set RETURNED_FLAGS to the flags to be used for this section. */
1601
1602static const char *
1603find_section_rename (ibfd, isection, returned_flags)
1604 bfd * ibfd ATTRIBUTE_UNUSED;
1605 sec_ptr isection;
1606 flagword * returned_flags;
1607{
1608 const char * old_name = bfd_section_name (ibfd, isection);
1609 section_rename * rename;
1610
1611 /* Default to using the flags of the input section. */
1612 * returned_flags = bfd_get_section_flags (ibfd, isection);
1613
1614 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1615 if (strcmp (rename->old_name, old_name) == 0)
1616 {
1617 if (rename->flags != (flagword) -1)
1618 * returned_flags = rename->flags;
1619
1620 return rename->new_name;
1621 }
1622
1623 return old_name;
1624}
1625
1626/* Create a section in OBFD with the same
1627 name and attributes as ISECTION in IBFD. */
252b5132
RH
1628
1629static void
1630setup_section (ibfd, isection, obfdarg)
1631 bfd *ibfd;
1632 sec_ptr isection;
1633 PTR obfdarg;
1634{
1635 bfd *obfd = (bfd *) obfdarg;
1636 struct section_list *p;
1637 sec_ptr osection;
1638 bfd_size_type size;
1639 bfd_vma vma;
1640 bfd_vma lma;
1641 flagword flags;
1a89cc7d 1642 const char *err;
594ef5db 1643 const char * name;
d7fb0dd2 1644 char *prefix = NULL;
0af11b59 1645
252b5132
RH
1646 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1647 && (strip_symbols == STRIP_DEBUG
1648 || strip_symbols == STRIP_UNNEEDED
1649 || strip_symbols == STRIP_ALL
1650 || discard_locals == LOCALS_ALL
1651 || convert_debugging))
1652 return;
1653
b34976b6 1654 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
252b5132 1655 if (p != NULL)
b34976b6 1656 p->used = TRUE;
252b5132 1657
f91ea849
ILT
1658 if (sections_removed && p != NULL && p->remove)
1659 return;
1660 if (sections_copied && (p == NULL || ! p->copy))
252b5132
RH
1661 return;
1662
594ef5db
NC
1663 /* Get the, possibly new, name of the output section. */
1664 name = find_section_rename (ibfd, isection, & flags);
0af11b59 1665
d7fb0dd2
NC
1666 /* Prefix sections. */
1667 if ((prefix_alloc_sections_string) && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
1668 prefix = prefix_alloc_sections_string;
1669 else if (prefix_sections_string)
1670 prefix = prefix_sections_string;
1671
1672 if (prefix)
1673 {
1674 char *n;
1675
1676 n = xmalloc (strlen (prefix) + strlen (name) + 1);
1677 strcpy (n, prefix);
1678 strcat (n, name);
1679 name = n;
1680 }
66491ebc 1681
594ef5db 1682 osection = bfd_make_section_anyway (obfd, name);
57938635 1683
252b5132
RH
1684 if (osection == NULL)
1685 {
1a89cc7d 1686 err = _("making");
252b5132
RH
1687 goto loser;
1688 }
1689
1690 size = bfd_section_size (ibfd, isection);
1691 if (copy_byte >= 0)
1692 size = (size + interleave - 1) / interleave;
1693 if (! bfd_set_section_size (obfd, osection, size))
1694 {
1a89cc7d 1695 err = _("size");
252b5132
RH
1696 goto loser;
1697 }
57938635 1698
252b5132
RH
1699 vma = bfd_section_vma (ibfd, isection);
1700 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1701 vma += p->vma_val;
1702 else if (p != NULL && p->change_vma == CHANGE_SET)
1703 vma = p->vma_val;
1704 else
1705 vma += change_section_address;
57938635 1706
252b5132
RH
1707 if (! bfd_set_section_vma (obfd, osection, vma))
1708 {
1a89cc7d 1709 err = _("vma");
252b5132
RH
1710 goto loser;
1711 }
1712
1713 lma = isection->lma;
1714 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1715 {
1716 if (p->change_lma == CHANGE_MODIFY)
1717 lma += p->lma_val;
1718 else if (p->change_lma == CHANGE_SET)
1719 lma = p->lma_val;
1720 else
1721 abort ();
1722 }
1723 else
1724 lma += change_section_address;
57938635 1725
252b5132
RH
1726 osection->lma = lma;
1727
1728 /* FIXME: This is probably not enough. If we change the LMA we
1729 may have to recompute the header for the file as well. */
b34976b6
AM
1730 if (!bfd_set_section_alignment (obfd,
1731 osection,
1732 bfd_section_alignment (ibfd, isection)))
252b5132 1733 {
1a89cc7d 1734 err = _("alignment");
252b5132
RH
1735 goto loser;
1736 }
1737
252b5132 1738 if (p != NULL && p->set_flags)
17978339 1739 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
252b5132
RH
1740 if (!bfd_set_section_flags (obfd, osection, flags))
1741 {
1a89cc7d 1742 err = _("flags");
252b5132
RH
1743 goto loser;
1744 }
1745
bc408b8a
JJ
1746 /* Copy merge entity size. */
1747 osection->entsize = isection->entsize;
1748
252b5132
RH
1749 /* This used to be mangle_section; we do here to avoid using
1750 bfd_get_section_by_name since some formats allow multiple
1751 sections with the same name. */
1752 isection->output_section = osection;
1753 isection->output_offset = 0;
1754
1755 /* Allow the BFD backend to copy any private data it understands
1756 from the input section to the output section. */
1757 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1758 {
1a89cc7d 1759 err = _("private data");
252b5132
RH
1760 goto loser;
1761 }
1762
594ef5db 1763 /* All went well. */
252b5132
RH
1764 return;
1765
1766loser:
1767 non_fatal (_("%s: section `%s': error in %s: %s"),
1768 bfd_get_filename (ibfd),
1769 bfd_section_name (ibfd, isection),
1770 err, bfd_errmsg (bfd_get_error ()));
1771 status = 1;
1772}
1773
1774/* Copy the data of input section ISECTION of IBFD
1775 to an output section with the same name in OBFD.
1776 If stripping then don't copy any relocation info. */
1777
1778static void
1779copy_section (ibfd, isection, obfdarg)
1780 bfd *ibfd;
1781 sec_ptr isection;
1782 PTR obfdarg;
1783{
1784 bfd *obfd = (bfd *) obfdarg;
1785 struct section_list *p;
1786 arelent **relpp;
1787 long relcount;
1788 sec_ptr osection;
1789 bfd_size_type size;
1790 long relsize;
dc156bc0 1791 flagword flags;
252b5132 1792
594ef5db
NC
1793 /* If we have already failed earlier on,
1794 do not keep on generating complaints now. */
252b5132
RH
1795 if (status != 0)
1796 return;
57938635 1797
dc156bc0
AM
1798 flags = bfd_get_section_flags (ibfd, isection);
1799 if ((flags & SEC_DEBUGGING) != 0
252b5132
RH
1800 && (strip_symbols == STRIP_DEBUG
1801 || strip_symbols == STRIP_UNNEEDED
1802 || strip_symbols == STRIP_ALL
1803 || discard_locals == LOCALS_ALL
1804 || convert_debugging))
e0c60db2 1805 return;
252b5132 1806
dc156bc0
AM
1807 if ((flags & SEC_GROUP) != 0)
1808 return;
1809
b34976b6 1810 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
252b5132 1811
f91ea849
ILT
1812 if (sections_removed && p != NULL && p->remove)
1813 return;
1814 if (sections_copied && (p == NULL || ! p->copy))
252b5132
RH
1815 return;
1816
1817 osection = isection->output_section;
1818 size = bfd_get_section_size_before_reloc (isection);
1819
1820 if (size == 0 || osection == 0)
1821 return;
1822
0af11b59 1823 /* Core files do not need to be relocated. */
4dd67f29
MS
1824 if (bfd_get_format (obfd) == bfd_core)
1825 relsize = 0;
1826 else
1827 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1828
252b5132
RH
1829 if (relsize < 0)
1830 RETURN_NONFATAL (bfd_get_filename (ibfd));
57938635 1831
252b5132
RH
1832 if (relsize == 0)
1833 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1834 else
1835 {
1836 relpp = (arelent **) xmalloc (relsize);
1837 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1838 if (relcount < 0)
1839 RETURN_NONFATAL (bfd_get_filename (ibfd));
57938635 1840
252b5132
RH
1841 if (strip_symbols == STRIP_ALL)
1842 {
1843 /* Remove relocations which are not in
0af11b59 1844 keep_strip_specific_list. */
252b5132
RH
1845 arelent **temp_relpp;
1846 long temp_relcount = 0;
1847 long i;
57938635 1848
252b5132
RH
1849 temp_relpp = (arelent **) xmalloc (relsize);
1850 for (i = 0; i < relcount; i++)
1851 if (is_specified_symbol
1852 (bfd_asymbol_name (*relpp [i]->sym_ptr_ptr),
1853 keep_specific_list))
1854 temp_relpp [temp_relcount++] = relpp [i];
1855 relcount = temp_relcount;
1856 free (relpp);
1857 relpp = temp_relpp;
1858 }
e0c60db2 1859
252b5132
RH
1860 bfd_set_reloc (obfd, osection,
1861 (relcount == 0 ? (arelent **) NULL : relpp), relcount);
1862 }
57938635 1863
252b5132 1864 isection->_cooked_size = isection->_raw_size;
b34976b6 1865 isection->reloc_done = TRUE;
252b5132 1866
0af11b59 1867 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
4dd67f29 1868 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
252b5132
RH
1869 {
1870 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1871
1872 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1873 size))
1874 RETURN_NONFATAL (bfd_get_filename (ibfd));
1875
57938635 1876 if (copy_byte >= 0)
252b5132
RH
1877 filter_bytes (memhunk, &size);
1878
1879 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1880 size))
1881 RETURN_NONFATAL (bfd_get_filename (obfd));
1882
1883 free (memhunk);
1884 }
1885 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1886 {
1887 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1888
1889 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1890 flag--they can just remove the section entirely and add it
1891 back again. However, we do permit them to turn on the
1892 SEC_HAS_CONTENTS flag, and take it to mean that the section
1893 contents should be zeroed out. */
1894
1895 memset (memhunk, 0, size);
1896 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1897 size))
1898 RETURN_NONFATAL (bfd_get_filename (obfd));
1899 free (memhunk);
1900 }
1901}
1902
1903/* Get all the sections. This is used when --gap-fill or --pad-to is
1904 used. */
1905
1906static void
1907get_sections (obfd, osection, secppparg)
b4c96d0d 1908 bfd *obfd ATTRIBUTE_UNUSED;
252b5132
RH
1909 asection *osection;
1910 PTR secppparg;
1911{
1912 asection ***secppp = (asection ***) secppparg;
1913
1914 **secppp = osection;
1915 ++(*secppp);
1916}
1917
1918/* Sort sections by VMA. This is called via qsort, and is used when
1919 --gap-fill or --pad-to is used. We force non loadable or empty
1920 sections to the front, where they are easier to ignore. */
1921
1922static int
1923compare_section_lma (arg1, arg2)
1924 const PTR arg1;
1925 const PTR arg2;
1926{
1927 const asection **sec1 = (const asection **) arg1;
1928 const asection **sec2 = (const asection **) arg2;
1929 flagword flags1, flags2;
1930
1931 /* Sort non loadable sections to the front. */
1932 flags1 = (*sec1)->flags;
1933 flags2 = (*sec2)->flags;
1934 if ((flags1 & SEC_HAS_CONTENTS) == 0
1935 || (flags1 & SEC_LOAD) == 0)
1936 {
1937 if ((flags2 & SEC_HAS_CONTENTS) != 0
1938 && (flags2 & SEC_LOAD) != 0)
1939 return -1;
1940 }
1941 else
1942 {
1943 if ((flags2 & SEC_HAS_CONTENTS) == 0
1944 || (flags2 & SEC_LOAD) == 0)
1945 return 1;
1946 }
1947
1948 /* Sort sections by LMA. */
1949 if ((*sec1)->lma > (*sec2)->lma)
1950 return 1;
1951 else if ((*sec1)->lma < (*sec2)->lma)
1952 return -1;
1953
1954 /* Sort sections with the same LMA by size. */
1955 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1956 return 1;
1957 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1958 return -1;
1959
1960 return 0;
1961}
1962
1963/* Mark all the symbols which will be used in output relocations with
1964 the BSF_KEEP flag so that those symbols will not be stripped.
1965
1966 Ignore relocations which will not appear in the output file. */
1967
1968static void
1969mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1970 bfd *ibfd;
1971 sec_ptr isection;
1972 PTR symbolsarg;
1973{
1974 asymbol **symbols = (asymbol **) symbolsarg;
1975 long relsize;
1976 arelent **relpp;
1977 long relcount, i;
1978
1979 /* Ignore an input section with no corresponding output section. */
1980 if (isection->output_section == NULL)
1981 return;
1982
1983 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1984 if (relsize < 0)
1985 bfd_fatal (bfd_get_filename (ibfd));
1986
1987 if (relsize == 0)
1988 return;
1989
1990 relpp = (arelent **) xmalloc (relsize);
1991 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1992 if (relcount < 0)
1993 bfd_fatal (bfd_get_filename (ibfd));
1994
ec5d57d5
NC
1995 /* Examine each symbol used in a relocation. If it's not one of the
1996 special bfd section symbols, then mark it with BSF_KEEP. */
252b5132
RH
1997 for (i = 0; i < relcount; i++)
1998 {
ec5d57d5
NC
1999 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2000 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2001 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2002 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
252b5132
RH
2003 }
2004
2005 if (relpp != NULL)
2006 free (relpp);
2007}
2008
2009/* Write out debugging information. */
2010
b34976b6 2011static bfd_boolean
252b5132
RH
2012write_debugging_info (obfd, dhandle, symcountp, symppp)
2013 bfd *obfd;
2014 PTR dhandle;
b4c96d0d
ILT
2015 long *symcountp ATTRIBUTE_UNUSED;
2016 asymbol ***symppp ATTRIBUTE_UNUSED;
252b5132
RH
2017{
2018 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2019 return write_ieee_debugging_info (obfd, dhandle);
2020
2021 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2022 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2023 {
2024 bfd_byte *syms, *strings;
2025 bfd_size_type symsize, stringsize;
2026 asection *stabsec, *stabstrsec;
2027
2028 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2029 &symsize, &strings,
2030 &stringsize))
b34976b6 2031 return FALSE;
252b5132
RH
2032
2033 stabsec = bfd_make_section (obfd, ".stab");
2034 stabstrsec = bfd_make_section (obfd, ".stabstr");
2035 if (stabsec == NULL
2036 || stabstrsec == NULL
2037 || ! bfd_set_section_size (obfd, stabsec, symsize)
2038 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2039 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2040 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
2041 || ! bfd_set_section_flags (obfd, stabsec,
2042 (SEC_HAS_CONTENTS
2043 | SEC_READONLY
2044 | SEC_DEBUGGING))
2045 || ! bfd_set_section_flags (obfd, stabstrsec,
2046 (SEC_HAS_CONTENTS
2047 | SEC_READONLY
2048 | SEC_DEBUGGING)))
2049 {
2050 non_fatal (_("%s: can't create debugging section: %s"),
2051 bfd_get_filename (obfd),
2052 bfd_errmsg (bfd_get_error ()));
b34976b6 2053 return FALSE;
252b5132
RH
2054 }
2055
2056 /* We can get away with setting the section contents now because
2057 the next thing the caller is going to do is copy over the
2058 real sections. We may someday have to split the contents
2059 setting out of this function. */
2060 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
2061 symsize)
2062 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
2063 (file_ptr) 0, stringsize))
2064 {
2065 non_fatal (_("%s: can't set debugging section contents: %s"),
2066 bfd_get_filename (obfd),
2067 bfd_errmsg (bfd_get_error ()));
b34976b6 2068 return FALSE;
252b5132
RH
2069 }
2070
b34976b6 2071 return TRUE;
252b5132
RH
2072 }
2073
2074 non_fatal (_("%s: don't know how to write debugging information for %s"),
2075 bfd_get_filename (obfd), bfd_get_target (obfd));
b34976b6 2076 return FALSE;
252b5132
RH
2077}
2078
2079static int
2080strip_main (argc, argv)
2081 int argc;
2082 char *argv[];
2083{
2084 char *input_target = NULL, *output_target = NULL;
b34976b6 2085 bfd_boolean show_version = FALSE;
252b5132
RH
2086 int c, i;
2087 struct section_list *p;
2088 char *output_file = NULL;
2089
8b53311e 2090 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVv",
252b5132
RH
2091 strip_options, (int *) 0)) != EOF)
2092 {
2093 switch (c)
2094 {
2095 case 'I':
2096 input_target = optarg;
2097 break;
2098 case 'O':
2099 output_target = optarg;
2100 break;
2101 case 'F':
2102 input_target = output_target = optarg;
2103 break;
2104 case 'R':
b34976b6
AM
2105 p = find_section_list (optarg, TRUE);
2106 p->remove = TRUE;
2107 sections_removed = TRUE;
252b5132
RH
2108 break;
2109 case 's':
2110 strip_symbols = STRIP_ALL;
2111 break;
2112 case 'S':
2113 case 'g':
db4f6831 2114 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
252b5132
RH
2115 strip_symbols = STRIP_DEBUG;
2116 break;
2117 case OPTION_STRIP_UNNEEDED:
2118 strip_symbols = STRIP_UNNEEDED;
2119 break;
2120 case 'K':
2121 add_specific_symbol (optarg, &keep_specific_list);
2122 break;
2123 case 'N':
2124 add_specific_symbol (optarg, &strip_specific_list);
2125 break;
2126 case 'o':
2127 output_file = optarg;
2128 break;
2129 case 'p':
b34976b6 2130 preserve_dates = TRUE;
252b5132
RH
2131 break;
2132 case 'x':
2133 discard_locals = LOCALS_ALL;
2134 break;
2135 case 'X':
2136 discard_locals = LOCALS_START_L;
2137 break;
2138 case 'v':
b34976b6 2139 verbose = TRUE;
252b5132
RH
2140 break;
2141 case 'V':
b34976b6 2142 show_version = TRUE;
252b5132
RH
2143 break;
2144 case 0:
594ef5db
NC
2145 /* We've been given a long option. */
2146 break;
8b53311e 2147 case 'H':
252b5132
RH
2148 case 'h':
2149 strip_usage (stdout, 0);
2150 default:
2151 strip_usage (stderr, 1);
2152 }
2153 }
2154
2155 if (show_version)
2156 print_version ("strip");
2157
2158 /* Default is to strip all symbols. */
2159 if (strip_symbols == STRIP_UNDEF
2160 && discard_locals == LOCALS_UNDEF
2161 && strip_specific_list == NULL)
2162 strip_symbols = STRIP_ALL;
2163
2164 if (output_target == (char *) NULL)
2165 output_target = input_target;
2166
2167 i = optind;
2168 if (i == argc
2169 || (output_file != NULL && (i + 1) < argc))
2170 strip_usage (stderr, 1);
2171
2172 for (; i < argc; i++)
2173 {
2174 int hold_status = status;
2175 struct stat statbuf;
2176 char *tmpname;
2177
2178 if (preserve_dates)
2179 {
2180 if (stat (argv[i], &statbuf) < 0)
2181 {
2182 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
2183 continue;
2184 }
2185 }
2186
2187 if (output_file != NULL)
2188 tmpname = output_file;
2189 else
2190 tmpname = make_tempname (argv[i]);
2191 status = 0;
2192
2193 copy_file (argv[i], tmpname, input_target, output_target);
2194 if (status == 0)
2195 {
2196 if (preserve_dates)
2197 set_times (tmpname, &statbuf);
2198 if (output_file == NULL)
2199 smart_rename (tmpname, argv[i], preserve_dates);
2200 status = hold_status;
2201 }
2202 else
2203 unlink (tmpname);
2204 if (output_file == NULL)
2205 free (tmpname);
2206 }
2207
2208 return 0;
2209}
2210
2211static int
2212copy_main (argc, argv)
2213 int argc;
2214 char *argv[];
2215{
43a0748c 2216 char * binary_architecture = NULL;
252b5132
RH
2217 char *input_filename = NULL, *output_filename = NULL;
2218 char *input_target = NULL, *output_target = NULL;
b34976b6
AM
2219 bfd_boolean show_version = FALSE;
2220 bfd_boolean change_warn = TRUE;
252b5132
RH
2221 int c;
2222 struct section_list *p;
2223 struct stat statbuf;
2224
8b53311e 2225 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:",
252b5132
RH
2226 copy_options, (int *) 0)) != EOF)
2227 {
2228 switch (c)
2229 {
2230 case 'b':
2231 copy_byte = atoi (optarg);
2232 if (copy_byte < 0)
2233 fatal (_("byte number must be non-negative"));
2234 break;
57938635 2235
0af11b59
KH
2236 case 'B':
2237 binary_architecture = optarg;
2238 break;
43a0748c 2239
252b5132
RH
2240 case 'i':
2241 interleave = atoi (optarg);
2242 if (interleave < 1)
2243 fatal (_("interleave must be positive"));
2244 break;
57938635 2245
252b5132
RH
2246 case 'I':
2247 case 's': /* "source" - 'I' is preferred */
2248 input_target = optarg;
2249 break;
57938635 2250
252b5132
RH
2251 case 'O':
2252 case 'd': /* "destination" - 'O' is preferred */
2253 output_target = optarg;
2254 break;
57938635 2255
252b5132
RH
2256 case 'F':
2257 input_target = output_target = optarg;
2258 break;
57938635 2259
f91ea849 2260 case 'j':
b34976b6 2261 p = find_section_list (optarg, TRUE);
f91ea849
ILT
2262 if (p->remove)
2263 fatal (_("%s both copied and removed"), optarg);
b34976b6
AM
2264 p->copy = TRUE;
2265 sections_copied = TRUE;
f91ea849 2266 break;
57938635 2267
252b5132 2268 case 'R':
b34976b6 2269 p = find_section_list (optarg, TRUE);
f91ea849
ILT
2270 if (p->copy)
2271 fatal (_("%s both copied and removed"), optarg);
b34976b6
AM
2272 p->remove = TRUE;
2273 sections_removed = TRUE;
252b5132 2274 break;
57938635 2275
252b5132
RH
2276 case 'S':
2277 strip_symbols = STRIP_ALL;
2278 break;
57938635 2279
252b5132
RH
2280 case 'g':
2281 strip_symbols = STRIP_DEBUG;
2282 break;
57938635 2283
252b5132
RH
2284 case OPTION_STRIP_UNNEEDED:
2285 strip_symbols = STRIP_UNNEEDED;
2286 break;
57938635 2287
252b5132
RH
2288 case 'K':
2289 add_specific_symbol (optarg, &keep_specific_list);
2290 break;
57938635 2291
252b5132
RH
2292 case 'N':
2293 add_specific_symbol (optarg, &strip_specific_list);
2294 break;
57938635 2295
252b5132
RH
2296 case 'L':
2297 add_specific_symbol (optarg, &localize_specific_list);
2298 break;
57938635 2299
16b2b71c
NC
2300 case 'G':
2301 add_specific_symbol (optarg, &keepglobal_specific_list);
2302 break;
2303
252b5132
RH
2304 case 'W':
2305 add_specific_symbol (optarg, &weaken_specific_list);
2306 break;
57938635 2307
252b5132 2308 case 'p':
b34976b6 2309 preserve_dates = TRUE;
252b5132 2310 break;
57938635 2311
252b5132
RH
2312 case 'x':
2313 discard_locals = LOCALS_ALL;
2314 break;
57938635 2315
252b5132
RH
2316 case 'X':
2317 discard_locals = LOCALS_START_L;
2318 break;
57938635 2319
252b5132 2320 case 'v':
b34976b6 2321 verbose = TRUE;
252b5132 2322 break;
57938635 2323
252b5132 2324 case 'V':
b34976b6 2325 show_version = TRUE;
252b5132 2326 break;
57938635 2327
252b5132 2328 case OPTION_WEAKEN:
b34976b6 2329 weaken = TRUE;
252b5132 2330 break;
57938635 2331
252b5132
RH
2332 case OPTION_ADD_SECTION:
2333 {
2334 const char *s;
2335 struct stat st;
2336 struct section_add *pa;
2337 int len;
2338 char *name;
2339 FILE *f;
2340
2341 s = strchr (optarg, '=');
57938635 2342
252b5132 2343 if (s == NULL)
57938635 2344 fatal (_("bad format for %s"), "--add-section");
252b5132
RH
2345
2346 if (stat (s + 1, & st) < 0)
2347 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
2348
2349 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
2350
2351 len = s - optarg;
2352 name = (char *) xmalloc (len + 1);
2353 strncpy (name, optarg, len);
2354 name[len] = '\0';
2355 pa->name = name;
2356
2357 pa->filename = s + 1;
2358
2359 pa->size = st.st_size;
2360
2361 pa->contents = (bfd_byte *) xmalloc (pa->size);
2362 f = fopen (pa->filename, FOPEN_RB);
57938635 2363
252b5132
RH
2364 if (f == NULL)
2365 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
57938635 2366
252b5132
RH
2367 if (fread (pa->contents, 1, pa->size, f) == 0
2368 || ferror (f))
2369 fatal (_("%s: fread failed"), pa->filename);
2370
2371 fclose (f);
2372
2373 pa->next = add_sections;
2374 add_sections = pa;
2375 }
2376 break;
57938635 2377
252b5132
RH
2378 case OPTION_CHANGE_START:
2379 change_start = parse_vma (optarg, "--change-start");
2380 break;
57938635 2381
252b5132
RH
2382 case OPTION_CHANGE_SECTION_ADDRESS:
2383 case OPTION_CHANGE_SECTION_LMA:
2384 case OPTION_CHANGE_SECTION_VMA:
2385 {
2386 const char *s;
2387 int len;
2388 char *name;
b4c96d0d 2389 char *option = NULL;
252b5132 2390 bfd_vma val;
b4c96d0d 2391 enum change_action what = CHANGE_IGNORE;
57938635 2392
252b5132
RH
2393 switch (c)
2394 {
b4c96d0d
ILT
2395 case OPTION_CHANGE_SECTION_ADDRESS:
2396 option = "--change-section-address";
2397 break;
2398 case OPTION_CHANGE_SECTION_LMA:
2399 option = "--change-section-lma";
2400 break;
2401 case OPTION_CHANGE_SECTION_VMA:
2402 option = "--change-section-vma";
2403 break;
252b5132 2404 }
57938635 2405
252b5132
RH
2406 s = strchr (optarg, '=');
2407 if (s == NULL)
2408 {
2409 s = strchr (optarg, '+');
2410 if (s == NULL)
2411 {
2412 s = strchr (optarg, '-');
2413 if (s == NULL)
2414 fatal (_("bad format for %s"), option);
2415 }
2416 }
2417
2418 len = s - optarg;
2419 name = (char *) xmalloc (len + 1);
2420 strncpy (name, optarg, len);
2421 name[len] = '\0';
2422
b34976b6 2423 p = find_section_list (name, TRUE);
252b5132
RH
2424
2425 val = parse_vma (s + 1, option);
2426
2427 switch (*s)
2428 {
2429 case '=': what = CHANGE_SET; break;
2430 case '-': val = - val; /* Drop through. */
2431 case '+': what = CHANGE_MODIFY; break;
2432 }
57938635 2433
252b5132
RH
2434 switch (c)
2435 {
2436 case OPTION_CHANGE_SECTION_ADDRESS:
2437 p->change_vma = what;
2438 p->vma_val = val;
2439 /* Drop through. */
57938635 2440
252b5132
RH
2441 case OPTION_CHANGE_SECTION_LMA:
2442 p->change_lma = what;
2443 p->lma_val = val;
2444 break;
57938635 2445
252b5132
RH
2446 case OPTION_CHANGE_SECTION_VMA:
2447 p->change_vma = what;
2448 p->vma_val = val;
2449 break;
2450 }
2451 }
2452 break;
57938635 2453
252b5132
RH
2454 case OPTION_CHANGE_ADDRESSES:
2455 change_section_address = parse_vma (optarg, "--change-addresses");
2456 change_start = change_section_address;
2457 break;
57938635 2458
252b5132 2459 case OPTION_CHANGE_WARNINGS:
b34976b6 2460 change_warn = TRUE;
252b5132 2461 break;
57938635 2462
252b5132 2463 case OPTION_CHANGE_LEADING_CHAR:
b34976b6 2464 change_leading_char = TRUE;
252b5132 2465 break;
57938635 2466
252b5132 2467 case OPTION_DEBUGGING:
b34976b6 2468 convert_debugging = TRUE;
252b5132 2469 break;
57938635 2470
252b5132
RH
2471 case OPTION_GAP_FILL:
2472 {
2473 bfd_vma gap_fill_vma;
2474
2475 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2476 gap_fill = (bfd_byte) gap_fill_vma;
2477 if ((bfd_vma) gap_fill != gap_fill_vma)
2478 {
2479 char buff[20];
57938635 2480
252b5132 2481 sprintf_vma (buff, gap_fill_vma);
57938635 2482
252b5132
RH
2483 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2484 buff, gap_fill);
2485 }
b34976b6 2486 gap_fill_set = TRUE;
252b5132
RH
2487 }
2488 break;
57938635 2489
252b5132 2490 case OPTION_NO_CHANGE_WARNINGS:
b34976b6 2491 change_warn = FALSE;
252b5132 2492 break;
57938635 2493
252b5132
RH
2494 case OPTION_PAD_TO:
2495 pad_to = parse_vma (optarg, "--pad-to");
b34976b6 2496 pad_to_set = TRUE;
252b5132 2497 break;
57938635 2498
252b5132 2499 case OPTION_REMOVE_LEADING_CHAR:
b34976b6 2500 remove_leading_char = TRUE;
252b5132 2501 break;
57938635
AM
2502
2503 case OPTION_REDEFINE_SYM:
2504 {
2505 /* Push this redefinition onto redefine_symbol_list. */
2506
2507 int len;
2508 const char *s;
2509 const char *nextarg;
2510 char *source, *target;
2511
2512 s = strchr (optarg, '=');
2513 if (s == NULL)
594ef5db 2514 fatal (_("bad format for %s"), "--redefine-sym");
57938635
AM
2515
2516 len = s - optarg;
2517 source = (char *) xmalloc (len + 1);
2518 strncpy (source, optarg, len);
2519 source[len] = '\0';
2520
2521 nextarg = s + 1;
2522 len = strlen (nextarg);
2523 target = (char *) xmalloc (len + 1);
2524 strcpy (target, nextarg);
2525
2526 redefine_list_append (source, target);
2527
2528 free (source);
2529 free (target);
2530 }
2531 break;
2532
252b5132
RH
2533 case OPTION_SET_SECTION_FLAGS:
2534 {
2535 const char *s;
2536 int len;
2537 char *name;
2538
2539 s = strchr (optarg, '=');
2540 if (s == NULL)
57938635 2541 fatal (_("bad format for %s"), "--set-section-flags");
252b5132
RH
2542
2543 len = s - optarg;
2544 name = (char *) xmalloc (len + 1);
2545 strncpy (name, optarg, len);
2546 name[len] = '\0';
2547
b34976b6 2548 p = find_section_list (name, TRUE);
252b5132 2549
b34976b6 2550 p->set_flags = TRUE;
252b5132
RH
2551 p->flags = parse_flags (s + 1);
2552 }
2553 break;
57938635 2554
594ef5db
NC
2555 case OPTION_RENAME_SECTION:
2556 {
2557 flagword flags;
3bcfb3e4
AM
2558 const char *eq, *fl;
2559 char *old_name;
2560 char *new_name;
594ef5db
NC
2561 unsigned int len;
2562
3bcfb3e4
AM
2563 eq = strchr (optarg, '=');
2564 if (eq == NULL)
594ef5db
NC
2565 fatal (_("bad format for %s"), "--rename-section");
2566
3bcfb3e4 2567 len = eq - optarg;
594ef5db 2568 if (len == 0)
3bcfb3e4 2569 fatal (_("bad format for %s"), "--rename-section");
594ef5db
NC
2570
2571 old_name = (char *) xmalloc (len + 1);
2572 strncpy (old_name, optarg, len);
2573 old_name[len] = 0;
2574
3bcfb3e4
AM
2575 eq++;
2576 fl = strchr (eq, ',');
2577 if (fl)
594ef5db 2578 {
3bcfb3e4
AM
2579 flags = parse_flags (fl + 1);
2580 len = fl - eq;
594ef5db
NC
2581 }
2582 else
2583 {
594ef5db 2584 flags = -1;
3bcfb3e4 2585 len = strlen (eq);
594ef5db
NC
2586 }
2587
3bcfb3e4
AM
2588 if (len == 0)
2589 fatal (_("bad format for %s"), "--rename-section");
2590
2591 new_name = (char *) xmalloc (len + 1);
2592 strncpy (new_name, eq, len);
2593 new_name[len] = 0;
2594
594ef5db
NC
2595 add_section_rename (old_name, new_name, flags);
2596 }
2597 break;
2598
252b5132
RH
2599 case OPTION_SET_START:
2600 set_start = parse_vma (optarg, "--set-start");
b34976b6 2601 set_start_set = TRUE;
252b5132 2602 break;
57938635 2603
0af11b59
KH
2604 case OPTION_SREC_LEN:
2605 Chunk = parse_vma (optarg, "--srec-len");
2606 break;
420496c1 2607
0af11b59 2608 case OPTION_SREC_FORCES3:
b34976b6 2609 S3Forced = TRUE;
0af11b59 2610 break;
420496c1 2611
16b2b71c
NC
2612 case OPTION_STRIP_SYMBOLS:
2613 add_specific_symbols (optarg, &strip_specific_list);
2614 break;
2615
2616 case OPTION_KEEP_SYMBOLS:
2617 add_specific_symbols (optarg, &keep_specific_list);
2618 break;
2619
2620 case OPTION_LOCALIZE_SYMBOLS:
2621 add_specific_symbols (optarg, &localize_specific_list);
2622 break;
2623
2624 case OPTION_KEEPGLOBAL_SYMBOLS:
2625 add_specific_symbols (optarg, &keepglobal_specific_list);
2626 break;
2627
2628 case OPTION_WEAKEN_SYMBOLS:
2629 add_specific_symbols (optarg, &weaken_specific_list);
2630 break;
2631
1ae8b3d2
AO
2632 case OPTION_ALT_MACH_CODE:
2633 use_alt_mach_code = atoi (optarg);
2634 if (use_alt_mach_code <= 0)
2635 fatal (_("alternate machine code index must be positive"));
2636 break;
2637
d7fb0dd2
NC
2638 case OPTION_PREFIX_SYMBOLS:
2639 prefix_symbols_string = optarg;
2640 break;
2641
2642 case OPTION_PREFIX_SECTIONS:
2643 prefix_sections_string = optarg;
2644 break;
2645
2646 case OPTION_PREFIX_ALLOC_SECTIONS:
2647 prefix_alloc_sections_string = optarg;
2648 break;
2649
252b5132
RH
2650 case 0:
2651 break; /* we've been given a long option */
57938635 2652
8b53311e 2653 case 'H':
252b5132
RH
2654 case 'h':
2655 copy_usage (stdout, 0);
57938635 2656
252b5132
RH
2657 default:
2658 copy_usage (stderr, 1);
2659 }
2660 }
2661
2662 if (show_version)
2663 print_version ("objcopy");
2664
2665 if (copy_byte >= interleave)
2666 fatal (_("byte number must be less than interleave"));
2667
2668 if (optind == argc || optind + 2 < argc)
2669 copy_usage (stderr, 1);
2670
2671 input_filename = argv[optind];
2672 if (optind + 1 < argc)
2673 output_filename = argv[optind + 1];
2674
2675 /* Default is to strip no symbols. */
2676 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2677 strip_symbols = STRIP_NONE;
2678
2679 if (output_target == (char *) NULL)
2680 output_target = input_target;
2681
43a0748c 2682 if (binary_architecture != (char *) NULL)
252b5132 2683 {
43a0748c 2684 if (input_target && strcmp (input_target, "binary") == 0)
0af11b59
KH
2685 {
2686 const bfd_arch_info_type * temp_arch_info;
43a0748c
NC
2687
2688 temp_arch_info = bfd_scan_arch (binary_architecture);
2689
0af11b59
KH
2690 if (temp_arch_info != NULL)
2691 bfd_external_binary_architecture = temp_arch_info->arch;
2692 else
2693 fatal (_("architecture %s unknown"), binary_architecture);
2694 }
43a0748c
NC
2695 else
2696 {
2697 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
2698 non_fatal (_(" Argument %s ignored"), binary_architecture);
2699 }
252b5132
RH
2700 }
2701
43a0748c
NC
2702 if (preserve_dates)
2703 if (stat (input_filename, & statbuf) < 0)
2704 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2705
252b5132
RH
2706 /* If there is no destination file then create a temp and rename
2707 the result into the input. */
2708
2709 if (output_filename == (char *) NULL)
2710 {
2711 char *tmpname = make_tempname (input_filename);
2712
2713 copy_file (input_filename, tmpname, input_target, output_target);
2714 if (status == 0)
57938635 2715 {
252b5132
RH
2716 if (preserve_dates)
2717 set_times (tmpname, &statbuf);
2718 smart_rename (tmpname, input_filename, preserve_dates);
2719 }
2720 else
2721 unlink (tmpname);
2722 }
2723 else
2724 {
2725 copy_file (input_filename, output_filename, input_target, output_target);
594ef5db 2726
252b5132
RH
2727 if (status == 0 && preserve_dates)
2728 set_times (output_filename, &statbuf);
2729 }
2730
2731 if (change_warn)
2732 {
2733 for (p = change_sections; p != NULL; p = p->next)
2734 {
2735 if (! p->used)
2736 {
2737 if (p->change_vma != CHANGE_IGNORE)
2738 {
2739 char buff [20];
2740
2741 sprintf_vma (buff, p->vma_val);
57938635 2742
252b5132 2743 /* xgettext:c-format */
57938635
AM
2744 non_fatal (_("%s %s%c0x%s never used"),
2745 "--change-section-vma",
252b5132
RH
2746 p->name,
2747 p->change_vma == CHANGE_SET ? '=' : '+',
2748 buff);
2749 }
57938635 2750
252b5132
RH
2751 if (p->change_lma != CHANGE_IGNORE)
2752 {
2753 char buff [20];
2754
2755 sprintf_vma (buff, p->lma_val);
57938635 2756
252b5132 2757 /* xgettext:c-format */
57938635
AM
2758 non_fatal (_("%s %s%c0x%s never used"),
2759 "--change-section-lma",
252b5132
RH
2760 p->name,
2761 p->change_lma == CHANGE_SET ? '=' : '+',
2762 buff);
2763 }
2764 }
2765 }
2766 }
2767
2768 return 0;
2769}
2770
65de42c0
TS
2771int main PARAMS ((int, char **));
2772
252b5132
RH
2773int
2774main (argc, argv)
2775 int argc;
2776 char *argv[];
2777{
2778#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2779 setlocale (LC_MESSAGES, "");
3882b010
L
2780#endif
2781#if defined (HAVE_SETLOCALE)
2782 setlocale (LC_CTYPE, "");
252b5132
RH
2783#endif
2784 bindtextdomain (PACKAGE, LOCALEDIR);
2785 textdomain (PACKAGE);
2786
2787 program_name = argv[0];
2788 xmalloc_set_program_name (program_name);
2789
2790 START_PROGRESS (program_name, 0);
2791
2792 strip_symbols = STRIP_UNDEF;
2793 discard_locals = LOCALS_UNDEF;
2794
2795 bfd_init ();
2796 set_default_bfd_target ();
2797
2798 if (is_strip < 0)
2799 {
2800 int i = strlen (program_name);
5af11cab
AM
2801#ifdef HAVE_DOS_BASED_FILE_SYSTEM
2802 /* Drop the .exe suffix, if any. */
2803 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
2804 {
2805 i -= 4;
2806 program_name[i] = '\0';
2807 }
2808#endif
2809 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
252b5132
RH
2810 }
2811
2812 if (is_strip)
2813 strip_main (argc, argv);
2814 else
2815 copy_main (argc, argv);
2816
2817 END_PROGRESS (program_name);
2818
2819 return status;
2820}
This page took 0.306311 seconds and 4 git commands to generate.