Add support for thin archives.
[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,
a8da6403 3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 1832 obfd->has_armap = ibfd->has_armap;
a8da6403 1833 obfd->is_thin_archive = ibfd->is_thin_archive;
252b5132
RH
1834
1835 list = NULL;
1836
1837 this_element = bfd_openr_next_archived_file (ibfd, NULL);
594ef5db 1838
b667df2e 1839 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2db6cde7 1840 RETURN_NONFATAL (obfd);
b667df2e 1841
d3ba0551 1842 while (!status && this_element != NULL)
252b5132 1843 {
4c168fa3
AM
1844 char *output_name;
1845 bfd *output_bfd;
252b5132 1846 bfd *last_element;
8066d1a2
AS
1847 struct stat buf;
1848 int stat_status = 0;
950d48e7 1849 bfd_boolean delete = TRUE;
8066d1a2 1850
4c168fa3
AM
1851 /* Create an output file for this member. */
1852 output_name = concat (dir, "/",
1853 bfd_get_filename (this_element), (char *) 0);
1854
1855 /* If the file already exists, make another temp dir. */
1856 if (stat (output_name, &buf) >= 0)
1857 {
f9c026a8
NC
1858 output_name = make_tempdir (output_name);
1859 if (output_name == NULL)
485be063
AM
1860 fatal (_("cannot create tempdir for archive copying (error: %s)"),
1861 strerror (errno));
84e2f313 1862
d3ba0551 1863 l = xmalloc (sizeof (struct name_list));
4c168fa3
AM
1864 l->name = output_name;
1865 l->next = list;
1866 l->obfd = NULL;
1867 list = l;
1868 output_name = concat (output_name, "/",
1869 bfd_get_filename (this_element), (char *) 0);
1870 }
1871
8066d1a2
AS
1872 if (preserve_dates)
1873 {
1874 stat_status = bfd_stat_arch_elt (this_element, &buf);
594ef5db 1875
8066d1a2
AS
1876 if (stat_status != 0)
1877 non_fatal (_("internal stat error on %s"),
1878 bfd_get_filename (this_element));
1879 }
252b5132 1880
d3ba0551 1881 l = xmalloc (sizeof (struct name_list));
252b5132
RH
1882 l->name = output_name;
1883 l->next = list;
bee59fd2 1884 l->obfd = NULL;
252b5132
RH
1885 list = l;
1886
b34976b6 1887 if (bfd_check_format (this_element, bfd_object))
77f762d6 1888 {
ee873e00
NC
1889 /* PR binutils/3110: Cope with archives
1890 containing multiple target types. */
1891 if (force_output_target)
1892 output_bfd = bfd_openw (output_name, output_target);
1893 else
1894 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
1895
1896 if (output_bfd == NULL)
2db6cde7
NS
1897 {
1898 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
1899 status = 1;
1900 return;
1901 }
ee873e00 1902
77f762d6 1903 delete = ! copy_object (this_element, output_bfd);
252b5132 1904
77f762d6
L
1905 if (! delete
1906 || bfd_get_arch (this_element) != bfd_arch_unknown)
1907 {
1908 if (!bfd_close (output_bfd))
1909 {
2db6cde7 1910 bfd_nonfatal_message (NULL, output_bfd, NULL, NULL);
77f762d6
L
1911 /* Error in new object file. Don't change archive. */
1912 status = 1;
1913 }
1914 }
1915 else
1916 goto copy_unknown_element;
1917 }
1918 else
252b5132 1919 {
2db6cde7
NS
1920 bfd_nonfatal_message (bfd_get_archive_filename (this_element),
1921 NULL, NULL,
1922 _("Unable to recognise the format of file"));
77f762d6 1923
ee873e00 1924 output_bfd = bfd_openw (output_name, output_target);
77f762d6
L
1925copy_unknown_element:
1926 delete = !copy_unknown_object (this_element, output_bfd);
1927 if (!bfd_close_all_done (output_bfd))
1928 {
2db6cde7 1929 bfd_nonfatal_message (NULL, output_bfd, NULL, NULL);
77f762d6
L
1930 /* Error in new object file. Don't change archive. */
1931 status = 1;
1932 }
252b5132
RH
1933 }
1934
950d48e7
NC
1935 if (delete)
1936 {
1937 unlink (output_name);
1938 status = 1;
1939 }
1940 else
1941 {
1942 if (preserve_dates && stat_status == 0)
1943 set_times (output_name, &buf);
8066d1a2 1944
950d48e7
NC
1945 /* Open the newly output file and attach to our list. */
1946 output_bfd = bfd_openr (output_name, output_target);
252b5132 1947
950d48e7 1948 l->obfd = output_bfd;
252b5132 1949
950d48e7 1950 *ptr = output_bfd;
cc481421 1951 ptr = &output_bfd->archive_next;
252b5132 1952
950d48e7 1953 last_element = this_element;
252b5132 1954
950d48e7 1955 this_element = bfd_openr_next_archived_file (ibfd, last_element);
252b5132 1956
950d48e7
NC
1957 bfd_close (last_element);
1958 }
252b5132 1959 }
d3ba0551 1960 *ptr = NULL;
252b5132
RH
1961
1962 if (!bfd_close (obfd))
2db6cde7 1963 RETURN_NONFATAL (obfd);
252b5132
RH
1964
1965 if (!bfd_close (ibfd))
2db6cde7 1966 RETURN_NONFATAL (obfd);
252b5132
RH
1967
1968 /* Delete all the files that we opened. */
1969 for (l = list; l != NULL; l = l->next)
1970 {
4c168fa3
AM
1971 if (l->obfd == NULL)
1972 rmdir (l->name);
1973 else
1974 {
1975 bfd_close (l->obfd);
1976 unlink (l->name);
1977 }
252b5132
RH
1978 }
1979 rmdir (dir);
1980}
1981
1982/* The top-level control. */
1983
1984static void
84e2f313
NC
1985copy_file (const char *input_filename, const char *output_filename,
1986 const char *input_target, const char *output_target)
252b5132
RH
1987{
1988 bfd *ibfd;
49c12576
AM
1989 char **obj_matching;
1990 char **core_matching;
252b5132 1991
f24ddbdd
NC
1992 if (get_file_size (input_filename) < 1)
1993 {
1994 status = 1;
1995 return;
1996 }
1997
252b5132
RH
1998 /* To allow us to do "strip *" without dying on the first
1999 non-object file, failures are nonfatal. */
252b5132
RH
2000 ibfd = bfd_openr (input_filename, input_target);
2001 if (ibfd == NULL)
2db6cde7
NS
2002 {
2003 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2004 status = 1;
2005 return;
2006 }
252b5132
RH
2007
2008 if (bfd_check_format (ibfd, bfd_archive))
2009 {
ee873e00 2010 bfd_boolean force_output_target;
252b5132
RH
2011 bfd *obfd;
2012
2013 /* bfd_get_target does not return the correct value until
2014 bfd_check_format succeeds. */
2015 if (output_target == NULL)
ee873e00
NC
2016 {
2017 output_target = bfd_get_target (ibfd);
2018 force_output_target = FALSE;
2019 }
2020 else
2021 force_output_target = TRUE;
252b5132
RH
2022
2023 obfd = bfd_openw (output_filename, output_target);
2024 if (obfd == NULL)
2db6cde7
NS
2025 {
2026 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2027 status = 1;
2028 return;
2029 }
252b5132 2030
ee873e00 2031 copy_archive (ibfd, obfd, output_target, force_output_target);
252b5132 2032 }
49c12576 2033 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
252b5132
RH
2034 {
2035 bfd *obfd;
49c12576 2036 do_copy:
950d48e7 2037
252b5132
RH
2038 /* bfd_get_target does not return the correct value until
2039 bfd_check_format succeeds. */
2040 if (output_target == NULL)
2041 output_target = bfd_get_target (ibfd);
2042
2043 obfd = bfd_openw (output_filename, output_target);
2044 if (obfd == NULL)
2db6cde7
NS
2045 {
2046 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2047 status = 1;
2048 return;
2049 }
252b5132 2050
a580b8e0
JB
2051 if (! copy_object (ibfd, obfd))
2052 status = 1;
252b5132
RH
2053
2054 if (!bfd_close (obfd))
2db6cde7 2055 RETURN_NONFATAL (obfd);
252b5132
RH
2056
2057 if (!bfd_close (ibfd))
2db6cde7 2058 RETURN_NONFATAL (ibfd);
252b5132
RH
2059 }
2060 else
2061 {
49c12576
AM
2062 bfd_error_type obj_error = bfd_get_error ();
2063 bfd_error_type core_error;
b34976b6 2064
49c12576
AM
2065 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2066 {
2067 /* This probably can't happen.. */
2068 if (obj_error == bfd_error_file_ambiguously_recognized)
2069 free (obj_matching);
2070 goto do_copy;
2071 }
2072
2073 core_error = bfd_get_error ();
2074 /* Report the object error in preference to the core error. */
2075 if (obj_error != core_error)
2076 bfd_set_error (obj_error);
2077
2db6cde7 2078 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
57938635 2079
49c12576
AM
2080 if (obj_error == bfd_error_file_ambiguously_recognized)
2081 {
2082 list_matching_formats (obj_matching);
2083 free (obj_matching);
2084 }
2085 if (core_error == bfd_error_file_ambiguously_recognized)
252b5132 2086 {
49c12576
AM
2087 list_matching_formats (core_matching);
2088 free (core_matching);
252b5132 2089 }
57938635 2090
252b5132
RH
2091 status = 1;
2092 }
2093}
2094
594ef5db
NC
2095/* Add a name to the section renaming list. */
2096
2097static void
84e2f313
NC
2098add_section_rename (const char * old_name, const char * new_name,
2099 flagword flags)
594ef5db
NC
2100{
2101 section_rename * rename;
2102
2103 /* Check for conflicts first. */
2104 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2105 if (strcmp (rename->old_name, old_name) == 0)
2106 {
2107 /* Silently ignore duplicate definitions. */
2108 if (strcmp (rename->new_name, new_name) == 0
2109 && rename->flags == flags)
2110 return;
0af11b59 2111
594ef5db
NC
2112 fatal (_("Multiple renames of section %s"), old_name);
2113 }
2114
d3ba0551 2115 rename = xmalloc (sizeof (* rename));
594ef5db
NC
2116
2117 rename->old_name = old_name;
2118 rename->new_name = new_name;
2119 rename->flags = flags;
2120 rename->next = section_rename_list;
0af11b59 2121
594ef5db
NC
2122 section_rename_list = rename;
2123}
2124
2125/* Check the section rename list for a new name of the input section
2126 ISECTION. Return the new name if one is found.
2127 Also set RETURNED_FLAGS to the flags to be used for this section. */
2128
2129static const char *
84e2f313
NC
2130find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2131 flagword * returned_flags)
594ef5db
NC
2132{
2133 const char * old_name = bfd_section_name (ibfd, isection);
2134 section_rename * rename;
2135
2136 /* Default to using the flags of the input section. */
2137 * returned_flags = bfd_get_section_flags (ibfd, isection);
2138
2139 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2140 if (strcmp (rename->old_name, old_name) == 0)
2141 {
2142 if (rename->flags != (flagword) -1)
2143 * returned_flags = rename->flags;
2144
2145 return rename->new_name;
2146 }
2147
2148 return old_name;
2149}
2150
80fccad2
BW
2151/* Once each of the sections is copied, we may still need to do some
2152 finalization work for private section headers. Do that here. */
2153
2154static void
2155setup_bfd_headers (bfd *ibfd, bfd *obfd)
2156{
80fccad2
BW
2157 /* Allow the BFD backend to copy any private data it understands
2158 from the input section to the output section. */
2159 if (! bfd_copy_private_header_data (ibfd, obfd))
2160 {
2db6cde7
NS
2161 status = 1;
2162 bfd_nonfatal_message (NULL, ibfd, NULL,
2163 _("error in private h eader data"));
2164 return;
80fccad2
BW
2165 }
2166
2167 /* All went well. */
2168 return;
80fccad2
BW
2169}
2170
594ef5db
NC
2171/* Create a section in OBFD with the same
2172 name and attributes as ISECTION in IBFD. */
252b5132
RH
2173
2174static void
84e2f313 2175setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
252b5132 2176{
d3ba0551 2177 bfd *obfd = obfdarg;
252b5132
RH
2178 struct section_list *p;
2179 sec_ptr osection;
2180 bfd_size_type size;
2181 bfd_vma vma;
2182 bfd_vma lma;
2183 flagword flags;
1a89cc7d 2184 const char *err;
594ef5db 2185 const char * name;
d7fb0dd2 2186 char *prefix = NULL;
66125551 2187 bfd_boolean make_nobits;
0af11b59 2188
2593f09a 2189 if (is_strip_section (ibfd, isection))
252b5132
RH
2190 return;
2191
b34976b6 2192 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
252b5132 2193 if (p != NULL)
b34976b6 2194 p->used = TRUE;
252b5132 2195
594ef5db
NC
2196 /* Get the, possibly new, name of the output section. */
2197 name = find_section_rename (ibfd, isection, & flags);
0af11b59 2198
d7fb0dd2 2199 /* Prefix sections. */
84e2f313
NC
2200 if ((prefix_alloc_sections_string)
2201 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
d7fb0dd2
NC
2202 prefix = prefix_alloc_sections_string;
2203 else if (prefix_sections_string)
2204 prefix = prefix_sections_string;
2205
2206 if (prefix)
2207 {
2208 char *n;
2209
2210 n = xmalloc (strlen (prefix) + strlen (name) + 1);
2211 strcpy (n, prefix);
2212 strcat (n, name);
2213 name = n;
2214 }
66491ebc 2215
66125551 2216 make_nobits = FALSE;
551b43fd
AM
2217 if (p != NULL && p->set_flags)
2218 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
42bb2e33 2219 else if (strip_symbols == STRIP_NONDEBUG
66125551
AM
2220 && (flags & SEC_ALLOC) != 0
2221 && (ibfd->xvec->flavour != bfd_target_elf_flavour
2222 || elf_section_type (isection) != SHT_NOTE))
2223 {
2224 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2225 if (obfd->xvec->flavour == bfd_target_elf_flavour)
2226 {
2227 make_nobits = TRUE;
2228
2229 /* Twiddle the input section flags so that it seems to
2230 elf.c:copy_private_bfd_data that section flags have not
2231 changed between input and output sections. This hack
2232 prevents wholesale rewriting of the program headers. */
2233 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2234 }
2235 }
551b43fd
AM
2236
2237 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
57938635 2238
252b5132
RH
2239 if (osection == NULL)
2240 {
2db6cde7 2241 err = _("failed to create output section");
252b5132
RH
2242 goto loser;
2243 }
2244
66125551 2245 if (make_nobits)
551b43fd
AM
2246 elf_section_type (osection) = SHT_NOBITS;
2247
252b5132
RH
2248 size = bfd_section_size (ibfd, isection);
2249 if (copy_byte >= 0)
2250 size = (size + interleave - 1) / interleave;
d3e52d40
RS
2251 else if (extract_symbol)
2252 size = 0;
252b5132
RH
2253 if (! bfd_set_section_size (obfd, osection, size))
2254 {
2db6cde7 2255 err = _("failed to set size");
252b5132
RH
2256 goto loser;
2257 }
57938635 2258
252b5132
RH
2259 vma = bfd_section_vma (ibfd, isection);
2260 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2261 vma += p->vma_val;
2262 else if (p != NULL && p->change_vma == CHANGE_SET)
2263 vma = p->vma_val;
2264 else
2265 vma += change_section_address;
57938635 2266
d3e52d40 2267 if (! bfd_set_section_vma (obfd, osection, extract_symbol ? 0 : vma))
252b5132 2268 {
2db6cde7 2269 err = _("failed to set vma");
252b5132
RH
2270 goto loser;
2271 }
2272
2273 lma = isection->lma;
2274 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2275 {
2276 if (p->change_lma == CHANGE_MODIFY)
2277 lma += p->lma_val;
2278 else if (p->change_lma == CHANGE_SET)
2279 lma = p->lma_val;
2280 else
2281 abort ();
2282 }
2283 else
2284 lma += change_section_address;
57938635 2285
d3e52d40 2286 osection->lma = extract_symbol ? 0 : lma;
252b5132
RH
2287
2288 /* FIXME: This is probably not enough. If we change the LMA we
2289 may have to recompute the header for the file as well. */
b34976b6
AM
2290 if (!bfd_set_section_alignment (obfd,
2291 osection,
2292 bfd_section_alignment (ibfd, isection)))
252b5132 2293 {
2db6cde7 2294 err = _("failed to set alignment");
252b5132
RH
2295 goto loser;
2296 }
2297
bc408b8a
JJ
2298 /* Copy merge entity size. */
2299 osection->entsize = isection->entsize;
2300
252b5132
RH
2301 /* This used to be mangle_section; we do here to avoid using
2302 bfd_get_section_by_name since some formats allow multiple
2303 sections with the same name. */
2304 isection->output_section = osection;
d3e52d40
RS
2305 isection->output_offset = extract_symbol ? vma : 0;
2306
2307 /* Do not copy backend data if --extract-symbol is passed; anything
2308 that needs to look at the section contents will fail. */
2309 if (extract_symbol)
2310 return;
252b5132
RH
2311
2312 /* Allow the BFD backend to copy any private data it understands
2313 from the input section to the output section. */
42bb2e33 2314 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
252b5132 2315 {
2db6cde7 2316 err = _("failed to copy private data");
252b5132
RH
2317 goto loser;
2318 }
30288845 2319 else if ((isection->flags & SEC_GROUP) != 0)
c39ada54 2320 {
30288845 2321 asymbol *gsym = group_signature (isection);
c39ada54 2322
30288845
AM
2323 if (gsym != NULL)
2324 gsym->flags |= BSF_KEEP;
c39ada54 2325 }
252b5132 2326
594ef5db 2327 /* All went well. */
252b5132
RH
2328 return;
2329
2330loser:
252b5132 2331 status = 1;
2db6cde7 2332 bfd_nonfatal_message (NULL, obfd, osection, err);
252b5132
RH
2333}
2334
2335/* Copy the data of input section ISECTION of IBFD
2336 to an output section with the same name in OBFD.
2337 If stripping then don't copy any relocation info. */
2338
2339static void
84e2f313 2340copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
252b5132 2341{
d3ba0551 2342 bfd *obfd = obfdarg;
252b5132
RH
2343 struct section_list *p;
2344 arelent **relpp;
2345 long relcount;
2346 sec_ptr osection;
2347 bfd_size_type size;
2348 long relsize;
dc156bc0 2349 flagword flags;
252b5132 2350
594ef5db
NC
2351 /* If we have already failed earlier on,
2352 do not keep on generating complaints now. */
252b5132
RH
2353 if (status != 0)
2354 return;
57938635 2355
2593f09a 2356 if (is_strip_section (ibfd, isection))
e0c60db2 2357 return;
252b5132 2358
2593f09a 2359 flags = bfd_get_section_flags (ibfd, isection);
dc156bc0
AM
2360 if ((flags & SEC_GROUP) != 0)
2361 return;
2362
252b5132 2363 osection = isection->output_section;
135dfb4a 2364 size = bfd_get_section_size (isection);
252b5132
RH
2365
2366 if (size == 0 || osection == 0)
2367 return;
2368
2593f09a
NC
2369 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2370
0af11b59 2371 /* Core files do not need to be relocated. */
4dd67f29
MS
2372 if (bfd_get_format (obfd) == bfd_core)
2373 relsize = 0;
2374 else
ed570f48
NC
2375 {
2376 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
4dd67f29 2377
ed570f48
NC
2378 if (relsize < 0)
2379 {
2380 /* Do not complain if the target does not support relocations. */
2381 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2382 relsize = 0;
2383 else
2db6cde7
NS
2384 {
2385 status = 1;
2386 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2387 return;
2388 }
ed570f48
NC
2389 }
2390 }
57938635 2391
252b5132 2392 if (relsize == 0)
d3ba0551 2393 bfd_set_reloc (obfd, osection, NULL, 0);
252b5132
RH
2394 else
2395 {
d3ba0551 2396 relpp = xmalloc (relsize);
252b5132
RH
2397 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2398 if (relcount < 0)
2db6cde7
NS
2399 {
2400 status = 1;
2401 bfd_nonfatal_message (NULL, ibfd, isection,
2402 _("relocation count is negative"));
2403 return;
2404 }
57938635 2405
252b5132
RH
2406 if (strip_symbols == STRIP_ALL)
2407 {
2408 /* Remove relocations which are not in
0af11b59 2409 keep_strip_specific_list. */
252b5132
RH
2410 arelent **temp_relpp;
2411 long temp_relcount = 0;
2412 long i;
57938635 2413
d3ba0551 2414 temp_relpp = xmalloc (relsize);
252b5132 2415 for (i = 0; i < relcount; i++)
d3ba0551
AM
2416 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2417 keep_specific_list))
252b5132
RH
2418 temp_relpp [temp_relcount++] = relpp [i];
2419 relcount = temp_relcount;
2420 free (relpp);
2421 relpp = temp_relpp;
2422 }
e0c60db2 2423
d3ba0551 2424 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
f0312d39
JJ
2425 if (relcount == 0)
2426 free (relpp);
252b5132 2427 }
57938635 2428
d3e52d40
RS
2429 if (extract_symbol)
2430 return;
2431
0af11b59 2432 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
4dd67f29 2433 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
252b5132 2434 {
d3ba0551 2435 void *memhunk = xmalloc (size);
252b5132 2436
d3ba0551 2437 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
2db6cde7
NS
2438 {
2439 status = 1;
2440 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2441 return;
2442 }
252b5132 2443
9e48b4c6
NC
2444 if (reverse_bytes)
2445 {
2446 /* We don't handle leftover bytes (too many possible behaviors,
2447 and we don't know what the user wants). The section length
2448 must be a multiple of the number of bytes to swap. */
2449 if ((size % reverse_bytes) == 0)
2450 {
2451 unsigned long i, j;
2452 bfd_byte b;
2453
2454 for (i = 0; i < size; i += reverse_bytes)
2455 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
2456 {
2457 bfd_byte *m = (bfd_byte *) memhunk;
2458
2459 b = m[i + j];
2460 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
2461 m[(i + reverse_bytes) - (j + 1)] = b;
2462 }
2463 }
2464 else
2465 /* User must pad the section up in order to do this. */
2466 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2467 bfd_section_name (ibfd, isection), reverse_bytes);
2468 }
2469
57938635 2470 if (copy_byte >= 0)
5e675b72
AM
2471 {
2472 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2f01ffbf 2473 char *from = (char *) memhunk + copy_byte;
5e675b72 2474 char *to = memhunk;
2f01ffbf 2475 char *end = (char *) memhunk + size;
5e675b72
AM
2476
2477 for (; from < end; from += interleave)
2478 *to++ = *from;
2479
2480 size = (size + interleave - 1 - copy_byte) / interleave;
2481 osection->lma /= interleave;
2482 }
252b5132 2483
d3ba0551 2484 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2db6cde7
NS
2485 {
2486 status = 1;
2487 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2488 return;
2489 }
252b5132
RH
2490 free (memhunk);
2491 }
2492 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2493 {
d3ba0551 2494 void *memhunk = xmalloc (size);
252b5132
RH
2495
2496 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2497 flag--they can just remove the section entirely and add it
2498 back again. However, we do permit them to turn on the
2499 SEC_HAS_CONTENTS flag, and take it to mean that the section
2500 contents should be zeroed out. */
2501
2502 memset (memhunk, 0, size);
d3ba0551 2503 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2db6cde7
NS
2504 {
2505 status = 1;
2506 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2507 return;
2508 }
252b5132
RH
2509 free (memhunk);
2510 }
2511}
2512
2513/* Get all the sections. This is used when --gap-fill or --pad-to is
2514 used. */
2515
2516static void
84e2f313 2517get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
252b5132 2518{
d3ba0551 2519 asection ***secppp = secppparg;
252b5132
RH
2520
2521 **secppp = osection;
2522 ++(*secppp);
2523}
2524
2525/* Sort sections by VMA. This is called via qsort, and is used when
2526 --gap-fill or --pad-to is used. We force non loadable or empty
2527 sections to the front, where they are easier to ignore. */
2528
2529static int
84e2f313 2530compare_section_lma (const void *arg1, const void *arg2)
252b5132 2531{
d3ba0551
AM
2532 const asection *const *sec1 = arg1;
2533 const asection *const *sec2 = arg2;
252b5132
RH
2534 flagword flags1, flags2;
2535
2536 /* Sort non loadable sections to the front. */
2537 flags1 = (*sec1)->flags;
2538 flags2 = (*sec2)->flags;
2539 if ((flags1 & SEC_HAS_CONTENTS) == 0
2540 || (flags1 & SEC_LOAD) == 0)
2541 {
2542 if ((flags2 & SEC_HAS_CONTENTS) != 0
2543 && (flags2 & SEC_LOAD) != 0)
2544 return -1;
2545 }
2546 else
2547 {
2548 if ((flags2 & SEC_HAS_CONTENTS) == 0
2549 || (flags2 & SEC_LOAD) == 0)
2550 return 1;
2551 }
2552
2553 /* Sort sections by LMA. */
2554 if ((*sec1)->lma > (*sec2)->lma)
2555 return 1;
2556 else if ((*sec1)->lma < (*sec2)->lma)
2557 return -1;
2558
2559 /* Sort sections with the same LMA by size. */
135dfb4a 2560 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
252b5132 2561 return 1;
135dfb4a 2562 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
252b5132
RH
2563 return -1;
2564
2565 return 0;
2566}
2567
2568/* Mark all the symbols which will be used in output relocations with
2569 the BSF_KEEP flag so that those symbols will not be stripped.
2570
2571 Ignore relocations which will not appear in the output file. */
2572
2573static void
84e2f313 2574mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
252b5132 2575{
d3ba0551 2576 asymbol **symbols = symbolsarg;
252b5132
RH
2577 long relsize;
2578 arelent **relpp;
2579 long relcount, i;
2580
2581 /* Ignore an input section with no corresponding output section. */
2582 if (isection->output_section == NULL)
2583 return;
2584
2585 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2586 if (relsize < 0)
ed570f48
NC
2587 {
2588 /* Do not complain if the target does not support relocations. */
2589 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2590 return;
2591 bfd_fatal (bfd_get_filename (ibfd));
2592 }
252b5132
RH
2593
2594 if (relsize == 0)
2595 return;
2596
d3ba0551 2597 relpp = xmalloc (relsize);
252b5132
RH
2598 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2599 if (relcount < 0)
2600 bfd_fatal (bfd_get_filename (ibfd));
2601
ec5d57d5
NC
2602 /* Examine each symbol used in a relocation. If it's not one of the
2603 special bfd section symbols, then mark it with BSF_KEEP. */
252b5132
RH
2604 for (i = 0; i < relcount; i++)
2605 {
ec5d57d5
NC
2606 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2607 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2608 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2609 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
252b5132
RH
2610 }
2611
2612 if (relpp != NULL)
2613 free (relpp);
2614}
2615
2616/* Write out debugging information. */
2617
b34976b6 2618static bfd_boolean
84e2f313
NC
2619write_debugging_info (bfd *obfd, void *dhandle,
2620 long *symcountp ATTRIBUTE_UNUSED,
2621 asymbol ***symppp ATTRIBUTE_UNUSED)
252b5132
RH
2622{
2623 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2624 return write_ieee_debugging_info (obfd, dhandle);
2625
2626 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2627 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2628 {
2629 bfd_byte *syms, *strings;
2630 bfd_size_type symsize, stringsize;
2631 asection *stabsec, *stabstrsec;
551b43fd 2632 flagword flags;
252b5132
RH
2633
2634 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2635 &symsize, &strings,
2636 &stringsize))
b34976b6 2637 return FALSE;
252b5132 2638
551b43fd
AM
2639 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2640 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2641 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
252b5132
RH
2642 if (stabsec == NULL
2643 || stabstrsec == NULL
2644 || ! bfd_set_section_size (obfd, stabsec, symsize)
2645 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2646 || ! bfd_set_section_alignment (obfd, stabsec, 2)
551b43fd 2647 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
252b5132 2648 {
2db6cde7
NS
2649 bfd_nonfatal_message (NULL, obfd, NULL,
2650 _("can't create debugging section"));
b34976b6 2651 return FALSE;
252b5132
RH
2652 }
2653
2654 /* We can get away with setting the section contents now because
2655 the next thing the caller is going to do is copy over the
2656 real sections. We may someday have to split the contents
2657 setting out of this function. */
d3ba0551
AM
2658 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2659 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2660 stringsize))
252b5132 2661 {
2db6cde7
NS
2662 bfd_nonfatal_message (NULL, obfd, NULL,
2663 _("can't set debugging section contents"));
b34976b6 2664 return FALSE;
252b5132
RH
2665 }
2666
b34976b6 2667 return TRUE;
252b5132
RH
2668 }
2669
2db6cde7
NS
2670 bfd_nonfatal_message (NULL, obfd, NULL,
2671 _("don't know how to write debugging information for %s"),
2672 bfd_get_target (obfd));
b34976b6 2673 return FALSE;
252b5132
RH
2674}
2675
2676static int
84e2f313 2677strip_main (int argc, char *argv[])
252b5132 2678{
7c29036b
NC
2679 char *input_target = NULL;
2680 char *output_target = NULL;
b34976b6 2681 bfd_boolean show_version = FALSE;
7c29036b
NC
2682 bfd_boolean formats_info = FALSE;
2683 int c;
2684 int i;
252b5132
RH
2685 struct section_list *p;
2686 char *output_file = NULL;
2687
5fe11841 2688 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
252b5132
RH
2689 strip_options, (int *) 0)) != EOF)
2690 {
2691 switch (c)
2692 {
2693 case 'I':
2694 input_target = optarg;
2695 break;
2696 case 'O':
2697 output_target = optarg;
2698 break;
2699 case 'F':
2700 input_target = output_target = optarg;
2701 break;
2702 case 'R':
b34976b6
AM
2703 p = find_section_list (optarg, TRUE);
2704 p->remove = TRUE;
2705 sections_removed = TRUE;
252b5132
RH
2706 break;
2707 case 's':
2708 strip_symbols = STRIP_ALL;
2709 break;
2710 case 'S':
2711 case 'g':
db4f6831 2712 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
252b5132
RH
2713 strip_symbols = STRIP_DEBUG;
2714 break;
2715 case OPTION_STRIP_UNNEEDED:
2716 strip_symbols = STRIP_UNNEEDED;
2717 break;
2718 case 'K':
2719 add_specific_symbol (optarg, &keep_specific_list);
2720 break;
2721 case 'N':
2722 add_specific_symbol (optarg, &strip_specific_list);
2723 break;
2724 case 'o':
2725 output_file = optarg;
2726 break;
2727 case 'p':
b34976b6 2728 preserve_dates = TRUE;
252b5132
RH
2729 break;
2730 case 'x':
2731 discard_locals = LOCALS_ALL;
2732 break;
2733 case 'X':
2734 discard_locals = LOCALS_START_L;
2735 break;
2736 case 'v':
b34976b6 2737 verbose = TRUE;
252b5132
RH
2738 break;
2739 case 'V':
b34976b6 2740 show_version = TRUE;
252b5132 2741 break;
7c29036b
NC
2742 case OPTION_FORMATS_INFO:
2743 formats_info = TRUE;
2744 break;
ed1653a7
NC
2745 case OPTION_ONLY_KEEP_DEBUG:
2746 strip_symbols = STRIP_NONDEBUG;
2747 break;
1637cd90
JB
2748 case OPTION_KEEP_FILE_SYMBOLS:
2749 keep_file_symbols = 1;
2750 break;
252b5132 2751 case 0:
594ef5db
NC
2752 /* We've been given a long option. */
2753 break;
5fe11841
NC
2754 case 'w':
2755 wildcard = TRUE;
2756 break;
8b53311e 2757 case 'H':
252b5132
RH
2758 case 'h':
2759 strip_usage (stdout, 0);
2760 default:
2761 strip_usage (stderr, 1);
2762 }
2763 }
2764
84e2f313
NC
2765 if (formats_info)
2766 {
2767 display_info ();
2768 return 0;
2769 }
c1c0eb9e 2770
252b5132
RH
2771 if (show_version)
2772 print_version ("strip");
2773
2774 /* Default is to strip all symbols. */
2775 if (strip_symbols == STRIP_UNDEF
2776 && discard_locals == LOCALS_UNDEF
2777 && strip_specific_list == NULL)
2778 strip_symbols = STRIP_ALL;
2779
d3ba0551 2780 if (output_target == NULL)
252b5132
RH
2781 output_target = input_target;
2782
2783 i = optind;
2784 if (i == argc
2785 || (output_file != NULL && (i + 1) < argc))
2786 strip_usage (stderr, 1);
2787
2788 for (; i < argc; i++)
2789 {
2790 int hold_status = status;
2791 struct stat statbuf;
2792 char *tmpname;
2793
f24ddbdd 2794 if (get_file_size (argv[i]) < 1)
d68c385b
NC
2795 {
2796 status = 1;
2797 continue;
2798 }
f24ddbdd 2799
252b5132 2800 if (preserve_dates)
f24ddbdd
NC
2801 /* No need to check the return value of stat().
2802 It has already been checked in get_file_size(). */
2803 stat (argv[i], &statbuf);
252b5132 2804
12f498a7 2805 if (output_file == NULL || strcmp (argv[i], output_file) == 0)
252b5132 2806 tmpname = make_tempname (argv[i]);
12f498a7
NS
2807 else
2808 tmpname = output_file;
252b5132 2809
f9c026a8
NC
2810 if (tmpname == NULL)
2811 {
2db6cde7
NS
2812 bfd_nonfatal_message (argv[i], NULL, NULL,
2813 _("could not create temporary file to hold stripped copy"));
f9c026a8
NC
2814 status = 1;
2815 continue;
2816 }
2817
d68c385b 2818 status = 0;
252b5132
RH
2819 copy_file (argv[i], tmpname, input_target, output_target);
2820 if (status == 0)
2821 {
2822 if (preserve_dates)
2823 set_times (tmpname, &statbuf);
12f498a7
NS
2824 if (output_file != tmpname)
2825 smart_rename (tmpname, output_file ? output_file : argv[i],
2826 preserve_dates);
252b5132
RH
2827 status = hold_status;
2828 }
2829 else
bb14f524 2830 unlink_if_ordinary (tmpname);
12f498a7 2831 if (output_file != tmpname)
252b5132
RH
2832 free (tmpname);
2833 }
2834
d68c385b 2835 return status;
252b5132
RH
2836}
2837
2838static int
84e2f313 2839copy_main (int argc, char *argv[])
252b5132 2840{
43a0748c 2841 char * binary_architecture = NULL;
7c29036b
NC
2842 char *input_filename = NULL;
2843 char *output_filename = NULL;
c1c0eb9e 2844 char *tmpname;
7c29036b
NC
2845 char *input_target = NULL;
2846 char *output_target = NULL;
b34976b6
AM
2847 bfd_boolean show_version = FALSE;
2848 bfd_boolean change_warn = TRUE;
7c29036b 2849 bfd_boolean formats_info = FALSE;
252b5132
RH
2850 int c;
2851 struct section_list *p;
2852 struct stat statbuf;
2853
5fe11841 2854 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
252b5132
RH
2855 copy_options, (int *) 0)) != EOF)
2856 {
2857 switch (c)
2858 {
2859 case 'b':
2860 copy_byte = atoi (optarg);
2861 if (copy_byte < 0)
2862 fatal (_("byte number must be non-negative"));
2863 break;
57938635 2864
0af11b59
KH
2865 case 'B':
2866 binary_architecture = optarg;
2867 break;
43a0748c 2868
252b5132
RH
2869 case 'i':
2870 interleave = atoi (optarg);
2871 if (interleave < 1)
2872 fatal (_("interleave must be positive"));
2873 break;
57938635 2874
252b5132
RH
2875 case 'I':
2876 case 's': /* "source" - 'I' is preferred */
2877 input_target = optarg;
2878 break;
57938635 2879
252b5132
RH
2880 case 'O':
2881 case 'd': /* "destination" - 'O' is preferred */
2882 output_target = optarg;
2883 break;
57938635 2884
252b5132
RH
2885 case 'F':
2886 input_target = output_target = optarg;
2887 break;
57938635 2888
f91ea849 2889 case 'j':
b34976b6 2890 p = find_section_list (optarg, TRUE);
f91ea849
ILT
2891 if (p->remove)
2892 fatal (_("%s both copied and removed"), optarg);
b34976b6
AM
2893 p->copy = TRUE;
2894 sections_copied = TRUE;
f91ea849 2895 break;
57938635 2896
252b5132 2897 case 'R':
b34976b6 2898 p = find_section_list (optarg, TRUE);
f91ea849
ILT
2899 if (p->copy)
2900 fatal (_("%s both copied and removed"), optarg);
b34976b6
AM
2901 p->remove = TRUE;
2902 sections_removed = TRUE;
252b5132 2903 break;
57938635 2904
252b5132
RH
2905 case 'S':
2906 strip_symbols = STRIP_ALL;
2907 break;
57938635 2908
252b5132
RH
2909 case 'g':
2910 strip_symbols = STRIP_DEBUG;
2911 break;
57938635 2912
252b5132
RH
2913 case OPTION_STRIP_UNNEEDED:
2914 strip_symbols = STRIP_UNNEEDED;
2915 break;
57938635 2916
ed1653a7
NC
2917 case OPTION_ONLY_KEEP_DEBUG:
2918 strip_symbols = STRIP_NONDEBUG;
2919 break;
2920
1637cd90
JB
2921 case OPTION_KEEP_FILE_SYMBOLS:
2922 keep_file_symbols = 1;
2923 break;
2924
2593f09a
NC
2925 case OPTION_ADD_GNU_DEBUGLINK:
2926 gnu_debuglink_filename = optarg;
2927 break;
2928
252b5132
RH
2929 case 'K':
2930 add_specific_symbol (optarg, &keep_specific_list);
2931 break;
57938635 2932
252b5132
RH
2933 case 'N':
2934 add_specific_symbol (optarg, &strip_specific_list);
2935 break;
57938635 2936
bcf32829
JB
2937 case OPTION_STRIP_UNNEEDED_SYMBOL:
2938 add_specific_symbol (optarg, &strip_unneeded_list);
2939 break;
2940
252b5132
RH
2941 case 'L':
2942 add_specific_symbol (optarg, &localize_specific_list);
2943 break;
57938635 2944
7b4a0685
NC
2945 case OPTION_GLOBALIZE_SYMBOL:
2946 add_specific_symbol (optarg, &globalize_specific_list);
2947 break;
2948
16b2b71c
NC
2949 case 'G':
2950 add_specific_symbol (optarg, &keepglobal_specific_list);
2951 break;
2952
252b5132
RH
2953 case 'W':
2954 add_specific_symbol (optarg, &weaken_specific_list);
2955 break;
57938635 2956
252b5132 2957 case 'p':
b34976b6 2958 preserve_dates = TRUE;
252b5132 2959 break;
57938635 2960
5fe11841
NC
2961 case 'w':
2962 wildcard = TRUE;
2963 break;
2964
252b5132
RH
2965 case 'x':
2966 discard_locals = LOCALS_ALL;
2967 break;
57938635 2968
252b5132
RH
2969 case 'X':
2970 discard_locals = LOCALS_START_L;
2971 break;
57938635 2972
252b5132 2973 case 'v':
b34976b6 2974 verbose = TRUE;
252b5132 2975 break;
57938635 2976
252b5132 2977 case 'V':
b34976b6 2978 show_version = TRUE;
252b5132 2979 break;
57938635 2980
7c29036b
NC
2981 case OPTION_FORMATS_INFO:
2982 formats_info = TRUE;
2983 break;
2984
252b5132 2985 case OPTION_WEAKEN:
b34976b6 2986 weaken = TRUE;
252b5132 2987 break;
57938635 2988
252b5132
RH
2989 case OPTION_ADD_SECTION:
2990 {
2991 const char *s;
f24ddbdd 2992 off_t size;
252b5132
RH
2993 struct section_add *pa;
2994 int len;
2995 char *name;
2996 FILE *f;
2997
2998 s = strchr (optarg, '=');
57938635 2999
252b5132 3000 if (s == NULL)
57938635 3001 fatal (_("bad format for %s"), "--add-section");
252b5132 3002
f24ddbdd
NC
3003 size = get_file_size (s + 1);
3004 if (size < 1)
d68c385b
NC
3005 {
3006 status = 1;
3007 break;
3008 }
252b5132 3009
d3ba0551 3010 pa = xmalloc (sizeof (struct section_add));
252b5132
RH
3011
3012 len = s - optarg;
d3ba0551 3013 name = xmalloc (len + 1);
252b5132
RH
3014 strncpy (name, optarg, len);
3015 name[len] = '\0';
3016 pa->name = name;
3017
3018 pa->filename = s + 1;
f24ddbdd
NC
3019 pa->size = size;
3020 pa->contents = xmalloc (size);
252b5132 3021
252b5132 3022 f = fopen (pa->filename, FOPEN_RB);
57938635 3023
252b5132 3024 if (f == NULL)
84e2f313
NC
3025 fatal (_("cannot open: %s: %s"),
3026 pa->filename, strerror (errno));
57938635 3027
252b5132
RH
3028 if (fread (pa->contents, 1, pa->size, f) == 0
3029 || ferror (f))
3030 fatal (_("%s: fread failed"), pa->filename);
3031
3032 fclose (f);
3033
3034 pa->next = add_sections;
3035 add_sections = pa;
3036 }
3037 break;
57938635 3038
252b5132
RH
3039 case OPTION_CHANGE_START:
3040 change_start = parse_vma (optarg, "--change-start");
3041 break;
57938635 3042
252b5132
RH
3043 case OPTION_CHANGE_SECTION_ADDRESS:
3044 case OPTION_CHANGE_SECTION_LMA:
3045 case OPTION_CHANGE_SECTION_VMA:
3046 {
3047 const char *s;
3048 int len;
3049 char *name;
b4c96d0d 3050 char *option = NULL;
252b5132 3051 bfd_vma val;
b4c96d0d 3052 enum change_action what = CHANGE_IGNORE;
57938635 3053
252b5132
RH
3054 switch (c)
3055 {
b4c96d0d
ILT
3056 case OPTION_CHANGE_SECTION_ADDRESS:
3057 option = "--change-section-address";
3058 break;
3059 case OPTION_CHANGE_SECTION_LMA:
3060 option = "--change-section-lma";
3061 break;
3062 case OPTION_CHANGE_SECTION_VMA:
3063 option = "--change-section-vma";
3064 break;
252b5132 3065 }
57938635 3066
252b5132
RH
3067 s = strchr (optarg, '=');
3068 if (s == NULL)
3069 {
3070 s = strchr (optarg, '+');
3071 if (s == NULL)
3072 {
3073 s = strchr (optarg, '-');
3074 if (s == NULL)
3075 fatal (_("bad format for %s"), option);
3076 }
3077 }
3078
3079 len = s - optarg;
d3ba0551 3080 name = xmalloc (len + 1);
252b5132
RH
3081 strncpy (name, optarg, len);
3082 name[len] = '\0';
3083
b34976b6 3084 p = find_section_list (name, TRUE);
252b5132
RH
3085
3086 val = parse_vma (s + 1, option);
3087
3088 switch (*s)
3089 {
3090 case '=': what = CHANGE_SET; break;
3091 case '-': val = - val; /* Drop through. */
3092 case '+': what = CHANGE_MODIFY; break;
3093 }
57938635 3094
252b5132
RH
3095 switch (c)
3096 {
3097 case OPTION_CHANGE_SECTION_ADDRESS:
3098 p->change_vma = what;
3099 p->vma_val = val;
3100 /* Drop through. */
57938635 3101
252b5132
RH
3102 case OPTION_CHANGE_SECTION_LMA:
3103 p->change_lma = what;
3104 p->lma_val = val;
3105 break;
57938635 3106
252b5132
RH
3107 case OPTION_CHANGE_SECTION_VMA:
3108 p->change_vma = what;
3109 p->vma_val = val;
3110 break;
3111 }
3112 }
3113 break;
57938635 3114
252b5132
RH
3115 case OPTION_CHANGE_ADDRESSES:
3116 change_section_address = parse_vma (optarg, "--change-addresses");
3117 change_start = change_section_address;
3118 break;
57938635 3119
252b5132 3120 case OPTION_CHANGE_WARNINGS:
b34976b6 3121 change_warn = TRUE;
252b5132 3122 break;
57938635 3123
252b5132 3124 case OPTION_CHANGE_LEADING_CHAR:
b34976b6 3125 change_leading_char = TRUE;
252b5132 3126 break;
57938635 3127
252b5132 3128 case OPTION_DEBUGGING:
b34976b6 3129 convert_debugging = TRUE;
252b5132 3130 break;
57938635 3131
252b5132
RH
3132 case OPTION_GAP_FILL:
3133 {
3134 bfd_vma gap_fill_vma;
3135
3136 gap_fill_vma = parse_vma (optarg, "--gap-fill");
3137 gap_fill = (bfd_byte) gap_fill_vma;
3138 if ((bfd_vma) gap_fill != gap_fill_vma)
3139 {
3140 char buff[20];
57938635 3141
252b5132 3142 sprintf_vma (buff, gap_fill_vma);
57938635 3143
252b5132
RH
3144 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3145 buff, gap_fill);
3146 }
b34976b6 3147 gap_fill_set = TRUE;
252b5132
RH
3148 }
3149 break;
57938635 3150
252b5132 3151 case OPTION_NO_CHANGE_WARNINGS:
b34976b6 3152 change_warn = FALSE;
252b5132 3153 break;
57938635 3154
252b5132
RH
3155 case OPTION_PAD_TO:
3156 pad_to = parse_vma (optarg, "--pad-to");
b34976b6 3157 pad_to_set = TRUE;
252b5132 3158 break;
57938635 3159
252b5132 3160 case OPTION_REMOVE_LEADING_CHAR:
b34976b6 3161 remove_leading_char = TRUE;
252b5132 3162 break;
57938635
AM
3163
3164 case OPTION_REDEFINE_SYM:
3165 {
3166 /* Push this redefinition onto redefine_symbol_list. */
3167
3168 int len;
3169 const char *s;
3170 const char *nextarg;
3171 char *source, *target;
3172
3173 s = strchr (optarg, '=');
3174 if (s == NULL)
594ef5db 3175 fatal (_("bad format for %s"), "--redefine-sym");
57938635
AM
3176
3177 len = s - optarg;
d3ba0551 3178 source = xmalloc (len + 1);
57938635
AM
3179 strncpy (source, optarg, len);
3180 source[len] = '\0';
3181
3182 nextarg = s + 1;
3183 len = strlen (nextarg);
d3ba0551 3184 target = xmalloc (len + 1);
57938635
AM
3185 strcpy (target, nextarg);
3186
92991082 3187 redefine_list_append ("--redefine-sym", source, target);
57938635
AM
3188
3189 free (source);
3190 free (target);
3191 }
3192 break;
3193
92991082
JT
3194 case OPTION_REDEFINE_SYMS:
3195 add_redefine_syms_file (optarg);
3196 break;
3197
252b5132
RH
3198 case OPTION_SET_SECTION_FLAGS:
3199 {
3200 const char *s;
3201 int len;
3202 char *name;
3203
3204 s = strchr (optarg, '=');
3205 if (s == NULL)
57938635 3206 fatal (_("bad format for %s"), "--set-section-flags");
252b5132
RH
3207
3208 len = s - optarg;
d3ba0551 3209 name = xmalloc (len + 1);
252b5132
RH
3210 strncpy (name, optarg, len);
3211 name[len] = '\0';
3212
b34976b6 3213 p = find_section_list (name, TRUE);
252b5132 3214
b34976b6 3215 p->set_flags = TRUE;
252b5132
RH
3216 p->flags = parse_flags (s + 1);
3217 }
3218 break;
57938635 3219
594ef5db
NC
3220 case OPTION_RENAME_SECTION:
3221 {
3222 flagword flags;
3bcfb3e4
AM
3223 const char *eq, *fl;
3224 char *old_name;
3225 char *new_name;
594ef5db
NC
3226 unsigned int len;
3227
3bcfb3e4
AM
3228 eq = strchr (optarg, '=');
3229 if (eq == NULL)
594ef5db
NC
3230 fatal (_("bad format for %s"), "--rename-section");
3231
3bcfb3e4 3232 len = eq - optarg;
594ef5db 3233 if (len == 0)
3bcfb3e4 3234 fatal (_("bad format for %s"), "--rename-section");
594ef5db 3235
d3ba0551 3236 old_name = xmalloc (len + 1);
594ef5db
NC
3237 strncpy (old_name, optarg, len);
3238 old_name[len] = 0;
3239
3bcfb3e4
AM
3240 eq++;
3241 fl = strchr (eq, ',');
3242 if (fl)
594ef5db 3243 {
3bcfb3e4
AM
3244 flags = parse_flags (fl + 1);
3245 len = fl - eq;
594ef5db
NC
3246 }
3247 else
3248 {
594ef5db 3249 flags = -1;
3bcfb3e4 3250 len = strlen (eq);
594ef5db
NC
3251 }
3252
3bcfb3e4
AM
3253 if (len == 0)
3254 fatal (_("bad format for %s"), "--rename-section");
3255
d3ba0551 3256 new_name = xmalloc (len + 1);
3bcfb3e4
AM
3257 strncpy (new_name, eq, len);
3258 new_name[len] = 0;
3259
594ef5db
NC
3260 add_section_rename (old_name, new_name, flags);
3261 }
3262 break;
3263
252b5132
RH
3264 case OPTION_SET_START:
3265 set_start = parse_vma (optarg, "--set-start");
b34976b6 3266 set_start_set = TRUE;
252b5132 3267 break;
57938635 3268
0af11b59
KH
3269 case OPTION_SREC_LEN:
3270 Chunk = parse_vma (optarg, "--srec-len");
3271 break;
420496c1 3272
0af11b59 3273 case OPTION_SREC_FORCES3:
b34976b6 3274 S3Forced = TRUE;
0af11b59 3275 break;
420496c1 3276
16b2b71c
NC
3277 case OPTION_STRIP_SYMBOLS:
3278 add_specific_symbols (optarg, &strip_specific_list);
3279 break;
3280
bcf32829
JB
3281 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3282 add_specific_symbols (optarg, &strip_unneeded_list);
3283 break;
3284
16b2b71c
NC
3285 case OPTION_KEEP_SYMBOLS:
3286 add_specific_symbols (optarg, &keep_specific_list);
3287 break;
3288
d58c2e3a
RS
3289 case OPTION_LOCALIZE_HIDDEN:
3290 localize_hidden = TRUE;
3291 break;
3292
16b2b71c
NC
3293 case OPTION_LOCALIZE_SYMBOLS:
3294 add_specific_symbols (optarg, &localize_specific_list);
3295 break;
3296
7b4a0685
NC
3297 case OPTION_GLOBALIZE_SYMBOLS:
3298 add_specific_symbols (optarg, &globalize_specific_list);
3299 break;
3300
16b2b71c
NC
3301 case OPTION_KEEPGLOBAL_SYMBOLS:
3302 add_specific_symbols (optarg, &keepglobal_specific_list);
3303 break;
3304
3305 case OPTION_WEAKEN_SYMBOLS:
3306 add_specific_symbols (optarg, &weaken_specific_list);
3307 break;
3308
1ae8b3d2 3309 case OPTION_ALT_MACH_CODE:
f9d4ad2a
NC
3310 use_alt_mach_code = strtoul (optarg, NULL, 0);
3311 if (use_alt_mach_code == 0)
3312 fatal (_("unable to parse alternative machine code"));
1ae8b3d2
AO
3313 break;
3314
d7fb0dd2
NC
3315 case OPTION_PREFIX_SYMBOLS:
3316 prefix_symbols_string = optarg;
3317 break;
3318
3319 case OPTION_PREFIX_SECTIONS:
3320 prefix_sections_string = optarg;
3321 break;
3322
3323 case OPTION_PREFIX_ALLOC_SECTIONS:
3324 prefix_alloc_sections_string = optarg;
3325 break;
3326
4087920c
MR
3327 case OPTION_READONLY_TEXT:
3328 bfd_flags_to_set |= WP_TEXT;
3329 bfd_flags_to_clear &= ~WP_TEXT;
3330 break;
3331
3332 case OPTION_WRITABLE_TEXT:
3333 bfd_flags_to_clear |= WP_TEXT;
3334 bfd_flags_to_set &= ~WP_TEXT;
3335 break;
3336
3337 case OPTION_PURE:
3338 bfd_flags_to_set |= D_PAGED;
3339 bfd_flags_to_clear &= ~D_PAGED;
3340 break;
3341
3342 case OPTION_IMPURE:
3343 bfd_flags_to_clear |= D_PAGED;
3344 bfd_flags_to_set &= ~D_PAGED;
3345 break;
3346
d3e52d40
RS
3347 case OPTION_EXTRACT_SYMBOL:
3348 extract_symbol = TRUE;
3349 break;
3350
9e48b4c6
NC
3351 case OPTION_REVERSE_BYTES:
3352 {
3353 int prev = reverse_bytes;
3354
3355 reverse_bytes = atoi (optarg);
3356 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
3357 fatal (_("number of bytes to reverse must be positive and even"));
3358
3359 if (prev && prev != reverse_bytes)
3360 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3361 prev);
3362 break;
3363 }
3364
252b5132 3365 case 0:
2593f09a
NC
3366 /* We've been given a long option. */
3367 break;
57938635 3368
8b53311e 3369 case 'H':
252b5132
RH
3370 case 'h':
3371 copy_usage (stdout, 0);
57938635 3372
252b5132
RH
3373 default:
3374 copy_usage (stderr, 1);
3375 }
3376 }
3377
7c29036b
NC
3378 if (formats_info)
3379 {
3380 display_info ();
3381 return 0;
3382 }
c1c0eb9e 3383
252b5132
RH
3384 if (show_version)
3385 print_version ("objcopy");
3386
3387 if (copy_byte >= interleave)
3388 fatal (_("byte number must be less than interleave"));
3389
3390 if (optind == argc || optind + 2 < argc)
3391 copy_usage (stderr, 1);
3392
3393 input_filename = argv[optind];
3394 if (optind + 1 < argc)
3395 output_filename = argv[optind + 1];
3396
3397 /* Default is to strip no symbols. */
3398 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3399 strip_symbols = STRIP_NONE;
3400
d3ba0551 3401 if (output_target == NULL)
252b5132
RH
3402 output_target = input_target;
3403
d3ba0551 3404 if (binary_architecture != NULL)
252b5132 3405 {
43a0748c 3406 if (input_target && strcmp (input_target, "binary") == 0)
0af11b59
KH
3407 {
3408 const bfd_arch_info_type * temp_arch_info;
43a0748c
NC
3409
3410 temp_arch_info = bfd_scan_arch (binary_architecture);
3411
0af11b59 3412 if (temp_arch_info != NULL)
b749473b
NC
3413 {
3414 bfd_external_binary_architecture = temp_arch_info->arch;
3415 bfd_external_machine = temp_arch_info->mach;
3416 }
0af11b59
KH
3417 else
3418 fatal (_("architecture %s unknown"), binary_architecture);
3419 }
43a0748c
NC
3420 else
3421 {
3422 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3423 non_fatal (_(" Argument %s ignored"), binary_architecture);
3424 }
252b5132
RH
3425 }
3426
43a0748c
NC
3427 if (preserve_dates)
3428 if (stat (input_filename, & statbuf) < 0)
f24ddbdd
NC
3429 fatal (_("warning: could not locate '%s'. System error message: %s"),
3430 input_filename, strerror (errno));
43a0748c 3431
0fcdcb91 3432 /* If there is no destination file, or the source and destination files
d3ba0551
AM
3433 are the same, then create a temp and rename the result into the input. */
3434 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
12f498a7 3435 tmpname = make_tempname (input_filename);
252b5132 3436 else
12f498a7 3437 tmpname = output_filename;
c1c0eb9e 3438
12f498a7
NS
3439 if (tmpname == NULL)
3440 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
3441 input_filename, strerror (errno));
594ef5db 3442
12f498a7
NS
3443 copy_file (input_filename, tmpname, input_target, output_target);
3444 if (status == 0)
3445 {
3446 if (preserve_dates)
3447 set_times (tmpname, &statbuf);
3448 if (tmpname != output_filename)
3449 smart_rename (tmpname, input_filename, preserve_dates);
252b5132 3450 }
12f498a7
NS
3451 else
3452 unlink_if_ordinary (tmpname);
252b5132
RH
3453
3454 if (change_warn)
3455 {
3456 for (p = change_sections; p != NULL; p = p->next)
3457 {
3458 if (! p->used)
3459 {
3460 if (p->change_vma != CHANGE_IGNORE)
3461 {
3462 char buff [20];
3463
3464 sprintf_vma (buff, p->vma_val);
57938635 3465
252b5132 3466 /* xgettext:c-format */
57938635
AM
3467 non_fatal (_("%s %s%c0x%s never used"),
3468 "--change-section-vma",
252b5132
RH
3469 p->name,
3470 p->change_vma == CHANGE_SET ? '=' : '+',
3471 buff);
3472 }
57938635 3473
252b5132
RH
3474 if (p->change_lma != CHANGE_IGNORE)
3475 {
3476 char buff [20];
3477
3478 sprintf_vma (buff, p->lma_val);
57938635 3479
252b5132 3480 /* xgettext:c-format */
57938635
AM
3481 non_fatal (_("%s %s%c0x%s never used"),
3482 "--change-section-lma",
252b5132
RH
3483 p->name,
3484 p->change_lma == CHANGE_SET ? '=' : '+',
3485 buff);
3486 }
3487 }
3488 }
3489 }
3490
3491 return 0;
3492}
3493
3494int
84e2f313 3495main (int argc, char *argv[])
252b5132
RH
3496{
3497#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3498 setlocale (LC_MESSAGES, "");
3882b010
L
3499#endif
3500#if defined (HAVE_SETLOCALE)
3501 setlocale (LC_CTYPE, "");
252b5132
RH
3502#endif
3503 bindtextdomain (PACKAGE, LOCALEDIR);
3504 textdomain (PACKAGE);
3505
3506 program_name = argv[0];
3507 xmalloc_set_program_name (program_name);
3508
3509 START_PROGRESS (program_name, 0);
3510
869b9d07
MM
3511 expandargv (&argc, &argv);
3512
252b5132
RH
3513 strip_symbols = STRIP_UNDEF;
3514 discard_locals = LOCALS_UNDEF;
3515
3516 bfd_init ();
3517 set_default_bfd_target ();
3518
3519 if (is_strip < 0)
3520 {
3521 int i = strlen (program_name);
5af11cab
AM
3522#ifdef HAVE_DOS_BASED_FILE_SYSTEM
3523 /* Drop the .exe suffix, if any. */
3524 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3525 {
3526 i -= 4;
3527 program_name[i] = '\0';
3528 }
3529#endif
3530 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
252b5132
RH
3531 }
3532
3533 if (is_strip)
3534 strip_main (argc, argv);
3535 else
3536 copy_main (argc, argv);
3537
3538 END_PROGRESS (program_name);
3539
3540 return status;
3541}
This page took 0.57779 seconds and 4 git commands to generate.