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