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