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