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