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