Fix seg-fault in linker parsing a corrupt input file.
[deliverable/binutils-gdb.git] / binutils / objcopy.c
CommitLineData
252b5132 1/* objcopy.c -- copy object file from input to output, optionally massaging it.
6f2750fe 2 Copyright (C) 1991-2016 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GNU Binutils.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
32866df7 8 the Free Software Foundation; either version 3 of the License, or
252b5132
RH
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
b43b5d5f
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20\f
3db64b00 21#include "sysdep.h"
252b5132
RH
22#include "bfd.h"
23#include "progress.h"
252b5132
RH
24#include "getopt.h"
25#include "libiberty.h"
3db64b00 26#include "bucomm.h"
252b5132 27#include "budbg.h"
5af11cab 28#include "filenames.h"
5fe11841 29#include "fnmatch.h"
f0312d39 30#include "elf-bfd.h"
0408dee6
DK
31#include "coff/internal.h"
32#include "libcoff.h"
252b5132 33
92dd4511
L
34/* FIXME: See bfd/peXXigen.c for why we include an architecture specific
35 header in generic PE code. */
36#include "coff/i386.h"
37#include "coff/pe.h"
38
39static bfd_vma pe_file_alignment = (bfd_vma) -1;
40static bfd_vma pe_heap_commit = (bfd_vma) -1;
41static bfd_vma pe_heap_reserve = (bfd_vma) -1;
42static bfd_vma pe_image_base = (bfd_vma) -1;
43static bfd_vma pe_section_alignment = (bfd_vma) -1;
44static bfd_vma pe_stack_commit = (bfd_vma) -1;
45static bfd_vma pe_stack_reserve = (bfd_vma) -1;
46static short pe_subsystem = -1;
47static short pe_major_subsystem_version = -1;
48static short pe_minor_subsystem_version = -1;
49
047c9024 50struct is_specified_symbol_predicate_data
252b5132 51{
2b35fb28 52 const char * name;
047c9024 53 bfd_boolean found;
252b5132
RH
54};
55
57938635
AM
56/* A list to support redefine_sym. */
57struct redefine_node
58{
59 char *source;
60 char *target;
61 struct redefine_node *next;
62};
63
2b35fb28
RH
64struct addsym_node
65{
66 struct addsym_node *next;
67 char * symdef;
68 long symval;
69 flagword flags;
70 char * section;
71 char * othersym;
72};
73
594ef5db
NC
74typedef struct section_rename
75{
76 const char * old_name;
77 const char * new_name;
78 flagword flags;
79 struct section_rename * next;
80}
81section_rename;
82
83/* List of sections to be renamed. */
84e2f313 84static section_rename *section_rename_list;
594ef5db 85
84e2f313
NC
86static asymbol **isympp = NULL; /* Input symbols. */
87static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
252b5132 88
b7dd81f7 89/* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
252b5132 90static int copy_byte = -1;
b7dd81f7
NC
91static int interleave = 0; /* Initialised to 4 in copy_main(). */
92static int copy_width = 1;
252b5132 93
b34976b6
AM
94static bfd_boolean verbose; /* Print file and target names. */
95static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
955d0b3b 96static int deterministic = -1; /* Enable deterministic archives. */
252b5132
RH
97static int status = 0; /* Exit status. */
98
99enum strip_action
2b35fb28
RH
100{
101 STRIP_UNDEF,
102 STRIP_NONE, /* Don't strip. */
103 STRIP_DEBUG, /* Strip all debugger symbols. */
104 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
105 STRIP_NONDEBUG, /* Strip everything but debug info. */
106 STRIP_DWO, /* Strip all DWO info. */
107 STRIP_NONDWO, /* Strip everything but DWO info. */
108 STRIP_ALL /* Strip all symbols. */
109};
252b5132 110
0af11b59 111/* Which symbols to remove. */
4fc8b895 112static enum strip_action strip_symbols = STRIP_UNDEF;
252b5132
RH
113
114enum locals_action
2b35fb28
RH
115{
116 LOCALS_UNDEF,
117 LOCALS_START_L, /* Discard locals starting with L. */
118 LOCALS_ALL /* Discard all locals. */
119};
252b5132
RH
120
121/* Which local symbols to remove. Overrides STRIP_ALL. */
122static enum locals_action discard_locals;
123
252b5132
RH
124/* Structure used to hold lists of sections and actions to take. */
125struct section_list
126{
b34976b6 127 struct section_list * next; /* Next section to change. */
2e62b721 128 const char * pattern; /* Section name pattern. */
b34976b6 129 bfd_boolean used; /* Whether this entry was used. */
2e62b721
NC
130
131 unsigned int context; /* What to do with matching sections. */
132 /* Flag bits used in the context field.
133 COPY and REMOVE are mutually exlusive. SET and ALTER are mutually exclusive. */
134#define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
135#define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
136#define SECTION_CONTEXT_SET_VMA (1 << 2) /* Set the sections' VMA address. */
137#define SECTION_CONTEXT_ALTER_VMA (1 << 3) /* Increment or decrement the section's VMA address. */
138#define SECTION_CONTEXT_SET_LMA (1 << 4) /* Set the sections' LMA address. */
139#define SECTION_CONTEXT_ALTER_LMA (1 << 5) /* Increment or decrement the section's LMA address. */
140#define SECTION_CONTEXT_SET_FLAGS (1 << 6) /* Set the section's flags. */
d3e5f6c8 141#define SECTION_CONTEXT_REMOVE_RELOCS (1 << 7) /* Remove relocations for this section. */
2e62b721 142
b34976b6 143 bfd_vma vma_val; /* Amount to change by or set to. */
b34976b6 144 bfd_vma lma_val; /* Amount to change by or set to. */
b34976b6 145 flagword flags; /* What to set the section flags to. */
252b5132
RH
146};
147
148static struct section_list *change_sections;
594ef5db 149
b34976b6
AM
150/* TRUE if some sections are to be removed. */
151static bfd_boolean sections_removed;
594ef5db 152
b34976b6
AM
153/* TRUE if only some sections are to be copied. */
154static bfd_boolean sections_copied;
252b5132
RH
155
156/* Changes to the start address. */
157static bfd_vma change_start = 0;
b34976b6 158static bfd_boolean set_start_set = FALSE;
252b5132
RH
159static bfd_vma set_start;
160
161/* Changes to section addresses. */
162static bfd_vma change_section_address = 0;
163
164/* Filling gaps between sections. */
b34976b6 165static bfd_boolean gap_fill_set = FALSE;
252b5132
RH
166static bfd_byte gap_fill = 0;
167
168/* Pad to a given address. */
b34976b6 169static bfd_boolean pad_to_set = FALSE;
252b5132
RH
170static bfd_vma pad_to;
171
f9d4ad2a
NC
172/* Use alternative machine code? */
173static unsigned long use_alt_mach_code = 0;
1ae8b3d2 174
4087920c
MR
175/* Output BFD flags user wants to set or clear */
176static flagword bfd_flags_to_set;
177static flagword bfd_flags_to_clear;
178
252b5132 179/* List of sections to add. */
252b5132
RH
180struct section_add
181{
182 /* Next section to add. */
183 struct section_add *next;
184 /* Name of section to add. */
185 const char *name;
186 /* Name of file holding section contents. */
187 const char *filename;
188 /* Size of file. */
189 size_t size;
190 /* Contents of file. */
191 bfd_byte *contents;
192 /* BFD section, after it has been added. */
193 asection *section;
194};
195
594ef5db 196/* List of sections to add to the output BFD. */
252b5132
RH
197static struct section_add *add_sections;
198
acf1419f
AB
199/* List of sections to update in the output BFD. */
200static struct section_add *update_sections;
201
bbad633b
NC
202/* List of sections to dump from the output BFD. */
203static struct section_add *dump_sections;
204
2593f09a
NC
205/* If non-NULL the argument to --add-gnu-debuglink.
206 This should be the filename to store in the .gnu_debuglink section. */
207static const char * gnu_debuglink_filename = NULL;
208
252b5132 209/* Whether to convert debugging information. */
b34976b6 210static bfd_boolean convert_debugging = FALSE;
252b5132 211
4a114e3e
L
212/* Whether to compress/decompress DWARF debug sections. */
213static enum
214{
cd6faa73
L
215 nothing = 0,
216 compress = 1 << 0,
217 compress_zlib = compress | 1 << 1,
218 compress_gnu_zlib = compress | 1 << 2,
219 compress_gabi_zlib = compress | 1 << 3,
220 decompress = 1 << 4
4a114e3e
L
221} do_debug_sections = nothing;
222
b8871f35 223/* Whether to generate ELF common symbols with the STT_COMMON type. */
eecc1a7f 224static enum bfd_link_elf_stt_common do_elf_stt_common = unchanged;
b8871f35 225
252b5132 226/* Whether to change the leading character in symbol names. */
b34976b6 227static bfd_boolean change_leading_char = FALSE;
252b5132
RH
228
229/* Whether to remove the leading character from global symbol names. */
b34976b6 230static bfd_boolean remove_leading_char = FALSE;
252b5132 231
aaad4cf3 232/* Whether to permit wildcard in symbol comparison. */
5fe11841
NC
233static bfd_boolean wildcard = FALSE;
234
d58c2e3a
RS
235/* True if --localize-hidden is in effect. */
236static bfd_boolean localize_hidden = FALSE;
237
16b2b71c
NC
238/* List of symbols to strip, keep, localize, keep-global, weaken,
239 or redefine. */
047c9024
NC
240static htab_t strip_specific_htab = NULL;
241static htab_t strip_unneeded_htab = NULL;
242static htab_t keep_specific_htab = NULL;
243static htab_t localize_specific_htab = NULL;
244static htab_t globalize_specific_htab = NULL;
245static htab_t keepglobal_specific_htab = NULL;
246static htab_t weaken_specific_htab = NULL;
57938635 247static struct redefine_node *redefine_sym_list = NULL;
2b35fb28
RH
248static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
249static int add_symbols = 0;
252b5132 250
b34976b6
AM
251/* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
252static bfd_boolean weaken = FALSE;
252b5132 253
1637cd90
JB
254/* If this is TRUE, we retain BSF_FILE symbols. */
255static bfd_boolean keep_file_symbols = FALSE;
256
d7fb0dd2
NC
257/* Prefix symbols/sections. */
258static char *prefix_symbols_string = 0;
259static char *prefix_sections_string = 0;
260static char *prefix_alloc_sections_string = 0;
261
d3e52d40
RS
262/* True if --extract-symbol was passed on the command line. */
263static bfd_boolean extract_symbol = FALSE;
264
9e48b4c6
NC
265/* If `reverse_bytes' is nonzero, then reverse the order of every chunk
266 of <reverse_bytes> bytes within each output section. */
267static int reverse_bytes = 0;
268
0408dee6
DK
269/* For Coff objects, we may want to allow or disallow long section names,
270 or preserve them where found in the inputs. Debug info relies on them. */
271enum long_section_name_handling
2b35fb28
RH
272{
273 DISABLE,
274 ENABLE,
275 KEEP
276};
0408dee6
DK
277
278/* The default long section handling mode is to preserve them.
279 This is also the only behaviour for 'strip'. */
280static enum long_section_name_handling long_section_names = KEEP;
9e48b4c6 281
252b5132 282/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
84e2f313 283enum command_line_switch
2b35fb28
RH
284{
285 OPTION_ADD_SECTION=150,
286 OPTION_ADD_GNU_DEBUGLINK,
287 OPTION_ADD_SYMBOL,
288 OPTION_ALT_MACH_CODE,
289 OPTION_CHANGE_ADDRESSES,
290 OPTION_CHANGE_LEADING_CHAR,
291 OPTION_CHANGE_SECTION_ADDRESS,
292 OPTION_CHANGE_SECTION_LMA,
293 OPTION_CHANGE_SECTION_VMA,
294 OPTION_CHANGE_START,
295 OPTION_CHANGE_WARNINGS,
296 OPTION_COMPRESS_DEBUG_SECTIONS,
297 OPTION_DEBUGGING,
298 OPTION_DECOMPRESS_DEBUG_SECTIONS,
299 OPTION_DUMP_SECTION,
b8871f35 300 OPTION_ELF_STT_COMMON,
2b35fb28
RH
301 OPTION_EXTRACT_DWO,
302 OPTION_EXTRACT_SYMBOL,
303 OPTION_FILE_ALIGNMENT,
304 OPTION_FORMATS_INFO,
305 OPTION_GAP_FILL,
306 OPTION_GLOBALIZE_SYMBOL,
307 OPTION_GLOBALIZE_SYMBOLS,
308 OPTION_HEAP,
309 OPTION_IMAGE_BASE,
310 OPTION_IMPURE,
311 OPTION_INTERLEAVE_WIDTH,
312 OPTION_KEEPGLOBAL_SYMBOLS,
313 OPTION_KEEP_FILE_SYMBOLS,
314 OPTION_KEEP_SYMBOLS,
315 OPTION_LOCALIZE_HIDDEN,
316 OPTION_LOCALIZE_SYMBOLS,
317 OPTION_LONG_SECTION_NAMES,
318 OPTION_NO_CHANGE_WARNINGS,
319 OPTION_ONLY_KEEP_DEBUG,
320 OPTION_PAD_TO,
321 OPTION_PREFIX_ALLOC_SECTIONS,
322 OPTION_PREFIX_SECTIONS,
323 OPTION_PREFIX_SYMBOLS,
324 OPTION_PURE,
325 OPTION_READONLY_TEXT,
326 OPTION_REDEFINE_SYM,
327 OPTION_REDEFINE_SYMS,
328 OPTION_REMOVE_LEADING_CHAR,
d3e5f6c8 329 OPTION_REMOVE_RELOCS,
2b35fb28
RH
330 OPTION_RENAME_SECTION,
331 OPTION_REVERSE_BYTES,
332 OPTION_SECTION_ALIGNMENT,
333 OPTION_SET_SECTION_FLAGS,
334 OPTION_SET_START,
335 OPTION_SREC_FORCES3,
336 OPTION_SREC_LEN,
337 OPTION_STACK,
338 OPTION_STRIP_DWO,
339 OPTION_STRIP_SYMBOLS,
340 OPTION_STRIP_UNNEEDED,
341 OPTION_STRIP_UNNEEDED_SYMBOL,
342 OPTION_STRIP_UNNEEDED_SYMBOLS,
343 OPTION_SUBSYSTEM,
344 OPTION_UPDATE_SECTION,
345 OPTION_WEAKEN,
346 OPTION_WEAKEN_SYMBOLS,
347 OPTION_WRITABLE_TEXT
348};
252b5132
RH
349
350/* Options to handle if running as "strip". */
351
352static struct option strip_options[] =
353{
955d0b3b 354 {"disable-deterministic-archives", no_argument, 0, 'U'},
252b5132
RH
355 {"discard-all", no_argument, 0, 'x'},
356 {"discard-locals", no_argument, 0, 'X'},
2e30cb57 357 {"enable-deterministic-archives", no_argument, 0, 'D'},
252b5132
RH
358 {"format", required_argument, 0, 'F'}, /* Obsolete */
359 {"help", no_argument, 0, 'h'},
7c29036b 360 {"info", no_argument, 0, OPTION_FORMATS_INFO},
252b5132
RH
361 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
362 {"input-target", required_argument, 0, 'I'},
1637cd90 363 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
252b5132 364 {"keep-symbol", required_argument, 0, 'K'},
ed1653a7 365 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
2b35fb28 366 {"output-file", required_argument, 0, 'o'},
252b5132
RH
367 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
368 {"output-target", required_argument, 0, 'O'},
369 {"preserve-dates", no_argument, 0, 'p'},
370 {"remove-section", required_argument, 0, 'R'},
d3e5f6c8 371 {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
252b5132
RH
372 {"strip-all", no_argument, 0, 's'},
373 {"strip-debug", no_argument, 0, 'S'},
96109726 374 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
252b5132 375 {"strip-symbol", required_argument, 0, 'N'},
2b35fb28 376 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
252b5132
RH
377 {"target", required_argument, 0, 'F'},
378 {"verbose", no_argument, 0, 'v'},
379 {"version", no_argument, 0, 'V'},
5fe11841 380 {"wildcard", no_argument, 0, 'w'},
252b5132
RH
381 {0, no_argument, 0, 0}
382};
383
384/* Options to handle if running as "objcopy". */
385
386static struct option copy_options[] =
387{
2593f09a 388 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
252b5132 389 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
2b35fb28
RH
390 {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
391 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
252b5132
RH
392 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
393 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
252b5132 394 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
d7fb0dd2 395 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
43a0748c 396 {"binary-architecture", required_argument, 0, 'B'},
252b5132
RH
397 {"byte", required_argument, 0, 'b'},
398 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
399 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
400 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
401 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
402 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
403 {"change-start", required_argument, 0, OPTION_CHANGE_START},
404 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
151411f8 405 {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
252b5132 406 {"debugging", no_argument, 0, OPTION_DEBUGGING},
4a114e3e 407 {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
955d0b3b 408 {"disable-deterministic-archives", no_argument, 0, 'U'},
252b5132
RH
409 {"discard-all", no_argument, 0, 'x'},
410 {"discard-locals", no_argument, 0, 'X'},
bbad633b 411 {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
b8871f35 412 {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
2e30cb57 413 {"enable-deterministic-archives", no_argument, 0, 'D'},
96109726 414 {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
d3e52d40 415 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
2b35fb28 416 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
252b5132
RH
417 {"format", required_argument, 0, 'F'}, /* Obsolete */
418 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
7b4a0685
NC
419 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
420 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
2b35fb28 421 {"heap", required_argument, 0, OPTION_HEAP},
252b5132 422 {"help", no_argument, 0, 'h'},
2b35fb28 423 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
4087920c 424 {"impure", no_argument, 0, OPTION_IMPURE},
7c29036b 425 {"info", no_argument, 0, OPTION_FORMATS_INFO},
252b5132
RH
426 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
427 {"input-target", required_argument, 0, 'I'},
b7dd81f7
NC
428 {"interleave", optional_argument, 0, 'i'},
429 {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
1637cd90 430 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
d7fb0dd2
NC
431 {"keep-global-symbol", required_argument, 0, 'G'},
432 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
252b5132 433 {"keep-symbol", required_argument, 0, 'K'},
d7fb0dd2 434 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
d58c2e3a 435 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
d7fb0dd2
NC
436 {"localize-symbol", required_argument, 0, 'L'},
437 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
0408dee6 438 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
252b5132
RH
439 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
440 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
ed1653a7 441 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
d7fb0dd2 442 {"only-section", required_argument, 0, 'j'},
252b5132
RH
443 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
444 {"output-target", required_argument, 0, 'O'},
445 {"pad-to", required_argument, 0, OPTION_PAD_TO},
d7fb0dd2 446 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
2b35fb28
RH
447 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
448 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
252b5132 449 {"preserve-dates", no_argument, 0, 'p'},
4087920c
MR
450 {"pure", no_argument, 0, OPTION_PURE},
451 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
d7fb0dd2 452 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
92991082 453 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
252b5132
RH
454 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
455 {"remove-section", required_argument, 0, 'R'},
d3e5f6c8 456 {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
594ef5db 457 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
9e48b4c6 458 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
2b35fb28 459 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
252b5132
RH
460 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
461 {"set-start", required_argument, 0, OPTION_SET_START},
d7fb0dd2 462 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
2b35fb28
RH
463 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
464 {"stack", required_argument, 0, OPTION_STACK},
252b5132
RH
465 {"strip-all", no_argument, 0, 'S'},
466 {"strip-debug", no_argument, 0, 'g'},
96109726 467 {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
2b35fb28
RH
468 {"strip-symbol", required_argument, 0, 'N'},
469 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
252b5132 470 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
bcf32829
JB
471 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
472 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
2b35fb28 473 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
252b5132 474 {"target", required_argument, 0, 'F'},
2b35fb28 475 {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
252b5132
RH
476 {"verbose", no_argument, 0, 'v'},
477 {"version", no_argument, 0, 'V'},
478 {"weaken", no_argument, 0, OPTION_WEAKEN},
479 {"weaken-symbol", required_argument, 0, 'W'},
16b2b71c 480 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
5fe11841 481 {"wildcard", no_argument, 0, 'w'},
4087920c 482 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
252b5132
RH
483 {0, no_argument, 0, 0}
484};
485
486/* IMPORTS */
487extern char *program_name;
488
489/* This flag distinguishes between strip and objcopy:
490 1 means this is 'strip'; 0 means this is 'objcopy'.
0af11b59 491 -1 means if we should use argv[0] to decide. */
252b5132
RH
492extern int is_strip;
493
420496c1
NC
494/* The maximum length of an S record. This variable is declared in srec.c
495 and can be modified by the --srec-len parameter. */
496extern unsigned int Chunk;
497
498/* Restrict the generation of Srecords to type S3 only.
499 This variable is declare in bfd/srec.c and can be toggled
500 on by the --srec-forceS3 command line switch. */
b34976b6 501extern bfd_boolean S3Forced;
252b5132 502
d3ba0551
AM
503/* Forward declarations. */
504static void setup_section (bfd *, asection *, void *);
80fccad2 505static void setup_bfd_headers (bfd *, bfd *);
c3989150 506static void copy_relocations_in_section (bfd *, asection *, void *);
d3ba0551
AM
507static void copy_section (bfd *, asection *, void *);
508static void get_sections (bfd *, asection *, void *);
509static int compare_section_lma (const void *, const void *);
510static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
511static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
512static const char *lookup_sym_redefinition (const char *);
9cc0123f 513static const char *find_section_rename (const char *, flagword *);
594ef5db 514\f
1e0f0b4d 515ATTRIBUTE_NORETURN static void
84e2f313 516copy_usage (FILE *stream, int exit_status)
252b5132 517{
8b53311e
NC
518 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
519 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
6364e0b4 520 fprintf (stream, _(" The options are:\n"));
252b5132 521 fprintf (stream, _("\
d5bcb29d
NC
522 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
523 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
8b31b6c4 524 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
d5bcb29d
NC
525 -F --target <bfdname> Set both input and output format to <bfdname>\n\
526 --debugging Convert debugging information, if possible\n\
955d0b3b
RM
527 -p --preserve-dates Copy modified/access timestamps to the output\n"));
528 if (DEFAULT_AR_DETERMINISTIC)
529 fprintf (stream, _("\
530 -D --enable-deterministic-archives\n\
531 Produce deterministic output when stripping archives (default)\n\
532 -U --disable-deterministic-archives\n\
533 Disable -D behavior\n"));
534 else
535 fprintf (stream, _("\
2e30cb57
CC
536 -D --enable-deterministic-archives\n\
537 Produce deterministic output when stripping archives\n\
955d0b3b
RM
538 -U --disable-deterministic-archives\n\
539 Disable -D behavior (default)\n"));
540 fprintf (stream, _("\
d5bcb29d 541 -j --only-section <name> Only copy section <name> into the output\n\
2593f09a 542 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
d5bcb29d 543 -R --remove-section <name> Remove section <name> from the output\n\
d3e5f6c8 544 --remove-relocations <name> Remove relocations from section <name>\n\
d5bcb29d 545 -S --strip-all Remove all symbol and relocation information\n\
2593f09a 546 -g --strip-debug Remove all debugging symbols & sections\n\
96109726 547 --strip-dwo Remove all DWO sections\n\
d5bcb29d
NC
548 --strip-unneeded Remove all symbols not needed by relocations\n\
549 -N --strip-symbol <name> Do not copy symbol <name>\n\
bcf32829
JB
550 --strip-unneeded-symbol <name>\n\
551 Do not copy symbol <name> unless needed by\n\
552 relocations\n\
6ea3dd37 553 --only-keep-debug Strip everything but the debug information\n\
96109726 554 --extract-dwo Copy only DWO sections\n\
d3e52d40 555 --extract-symbol Remove section contents but keep symbols\n\
e7f918ad 556 -K --keep-symbol <name> Do not strip symbol <name>\n\
1637cd90 557 --keep-file-symbols Do not strip file symbol(s)\n\
d58c2e3a 558 --localize-hidden Turn all ELF hidden symbols into locals\n\
d5bcb29d 559 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
7b4a0685 560 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
16b2b71c 561 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
d5bcb29d
NC
562 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
563 --weaken Force all global symbols to be marked as weak\n\
a95b5cf9 564 -w --wildcard Permit wildcard in symbol comparison\n\
d5bcb29d
NC
565 -x --discard-all Remove all non-global symbols\n\
566 -X --discard-locals Remove any compiler-generated symbols\n\
bfcf0ccd 567 -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
b7dd81f7 568 --interleave-width <number> Set N for --interleave\n\
d5bcb29d
NC
569 -b --byte <num> Select byte <num> in every interleaved block\n\
570 --gap-fill <val> Fill gaps between sections with <val>\n\
571 --pad-to <addr> Pad the last section up to address <addr>\n\
572 --set-start <addr> Set the start address to <addr>\n\
573 {--change-start|--adjust-start} <incr>\n\
574 Add <incr> to the start address\n\
575 {--change-addresses|--adjust-vma} <incr>\n\
576 Add <incr> to LMA, VMA and start addresses\n\
577 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
578 Change LMA and VMA of section <name> by <val>\n\
579 --change-section-lma <name>{=|+|-}<val>\n\
580 Change the LMA of section <name> by <val>\n\
581 --change-section-vma <name>{=|+|-}<val>\n\
582 Change the VMA of section <name> by <val>\n\
583 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
584 Warn if a named section does not exist\n\
585 --set-section-flags <name>=<flags>\n\
586 Set section <name>'s properties to <flags>\n\
587 --add-section <name>=<file> Add section <name> found in <file> to output\n\
acf1419f
AB
588 --update-section <name>=<file>\n\
589 Update contents of section <name> with\n\
590 contents found in <file>\n\
bbad633b 591 --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
594ef5db 592 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
0408dee6
DK
593 --long-section-names {enable|disable|keep}\n\
594 Handle long section names in Coff objects.\n\
d5bcb29d
NC
595 --change-leading-char Force output format's leading character style\n\
596 --remove-leading-char Remove leading character from global symbols\n\
9e48b4c6 597 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
57938635 598 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
92991082
JT
599 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
600 listed in <file>\n\
420496c1
NC
601 --srec-len <number> Restrict the length of generated Srecords\n\
602 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
16b2b71c 603 --strip-symbols <file> -N for all symbols listed in <file>\n\
bcf32829
JB
604 --strip-unneeded-symbols <file>\n\
605 --strip-unneeded-symbol for all symbols listed\n\
606 in <file>\n\
16b2b71c
NC
607 --keep-symbols <file> -K for all symbols listed in <file>\n\
608 --localize-symbols <file> -L for all symbols listed in <file>\n\
7b4a0685 609 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
16b2b71c
NC
610 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
611 --weaken-symbols <file> -W for all symbols listed in <file>\n\
2b35fb28 612 --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
f9d4ad2a 613 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
4087920c
MR
614 --writable-text Mark the output text as writable\n\
615 --readonly-text Make the output text write protected\n\
616 --pure Mark the output file as demand paged\n\
617 --impure Mark the output file as impure\n\
d7fb0dd2
NC
618 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
619 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
620 --prefix-alloc-sections <prefix>\n\
621 Add <prefix> to start of every allocatable\n\
622 section name\n\
92dd4511
L
623 --file-alignment <num> Set PE file alignment to <num>\n\
624 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
625 <commit>\n\
626 --image-base <address> Set PE image base to <address>\n\
627 --section-alignment <num> Set PE section alignment to <num>\n\
628 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
629 <commit>\n\
630 --subsystem <name>[:<version>]\n\
447049af 631 Set PE subsystem to <name> [& <version>]\n\
151411f8
L
632 --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
633 Compress DWARF debug sections using zlib\n\
4a114e3e 634 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
b8871f35
L
635 --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
636 type\n\
d5bcb29d 637 -v --verbose List all object files modified\n\
07012eee 638 @<file> Read options from <file>\n\
d5bcb29d
NC
639 -V --version Display this program's version number\n\
640 -h --help Display this output\n\
7c29036b 641 --info List object formats & architectures supported\n\
d5bcb29d 642"));
252b5132 643 list_supported_targets (program_name, stream);
92f01d61 644 if (REPORT_BUGS_TO[0] && exit_status == 0)
8ad3436c 645 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
646 exit (exit_status);
647}
648
1e0f0b4d 649ATTRIBUTE_NORETURN static void
84e2f313 650strip_usage (FILE *stream, int exit_status)
252b5132 651{
8b53311e
NC
652 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
653 fprintf (stream, _(" Removes symbols and sections from files\n"));
6364e0b4 654 fprintf (stream, _(" The options are:\n"));
252b5132 655 fprintf (stream, _("\
8b53311e
NC
656 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
657 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
658 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
d5bcb29d 659 -p --preserve-dates Copy modified/access timestamps to the output\n\
955d0b3b
RM
660"));
661 if (DEFAULT_AR_DETERMINISTIC)
662 fprintf (stream, _("\
663 -D --enable-deterministic-archives\n\
664 Produce deterministic output when stripping archives (default)\n\
665 -U --disable-deterministic-archives\n\
666 Disable -D behavior\n"));
667 else
668 fprintf (stream, _("\
2e30cb57
CC
669 -D --enable-deterministic-archives\n\
670 Produce deterministic output when stripping archives\n\
955d0b3b
RM
671 -U --disable-deterministic-archives\n\
672 Disable -D behavior (default)\n"));
673 fprintf (stream, _("\
805b1c8b 674 -R --remove-section=<name> Also remove section <name> from the output\n\
d3e5f6c8 675 --remove-relocations <name> Remove relocations from section <name>\n\
d5bcb29d 676 -s --strip-all Remove all symbol and relocation information\n\
2593f09a 677 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
96109726 678 --strip-dwo Remove all DWO sections\n\
d5bcb29d 679 --strip-unneeded Remove all symbols not needed by relocations\n\
6ea3dd37 680 --only-keep-debug Strip everything but the debug information\n\
8b53311e 681 -N --strip-symbol=<name> Do not copy symbol <name>\n\
5219e4c0 682 -K --keep-symbol=<name> Do not strip symbol <name>\n\
1637cd90 683 --keep-file-symbols Do not strip file symbol(s)\n\
a95b5cf9 684 -w --wildcard Permit wildcard in symbol comparison\n\
d5bcb29d
NC
685 -x --discard-all Remove all non-global symbols\n\
686 -X --discard-locals Remove any compiler-generated symbols\n\
687 -v --verbose List all object files modified\n\
688 -V --version Display this program's version number\n\
689 -h --help Display this output\n\
7c29036b 690 --info List object formats & architectures supported\n\
d5bcb29d
NC
691 -o <file> Place stripped output into <file>\n\
692"));
693
252b5132 694 list_supported_targets (program_name, stream);
92f01d61 695 if (REPORT_BUGS_TO[0] && exit_status == 0)
8ad3436c 696 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
697 exit (exit_status);
698}
699
700/* Parse section flags into a flagword, with a fatal error if the
701 string can't be parsed. */
702
703static flagword
84e2f313 704parse_flags (const char *s)
252b5132
RH
705{
706 flagword ret;
707 const char *snext;
708 int len;
709
710 ret = SEC_NO_FLAGS;
711
712 do
713 {
714 snext = strchr (s, ',');
715 if (snext == NULL)
716 len = strlen (s);
717 else
718 {
719 len = snext - s;
720 ++snext;
721 }
722
723 if (0) ;
f7433f01
AM
724#define PARSE_FLAG(fname,fval) \
725 else if (strncasecmp (fname, s, len) == 0) ret |= fval
252b5132
RH
726 PARSE_FLAG ("alloc", SEC_ALLOC);
727 PARSE_FLAG ("load", SEC_LOAD);
3994e2c6 728 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
252b5132 729 PARSE_FLAG ("readonly", SEC_READONLY);
3994e2c6 730 PARSE_FLAG ("debug", SEC_DEBUGGING);
252b5132
RH
731 PARSE_FLAG ("code", SEC_CODE);
732 PARSE_FLAG ("data", SEC_DATA);
733 PARSE_FLAG ("rom", SEC_ROM);
ebe372c1 734 PARSE_FLAG ("share", SEC_COFF_SHARED);
252b5132 735 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
5dddde8e
AM
736 PARSE_FLAG ("merge", SEC_MERGE);
737 PARSE_FLAG ("strings", SEC_STRINGS);
252b5132
RH
738#undef PARSE_FLAG
739 else
740 {
741 char *copy;
742
3f5e193b 743 copy = (char *) xmalloc (len + 1);
252b5132
RH
744 strncpy (copy, s, len);
745 copy[len] = '\0';
746 non_fatal (_("unrecognized section flag `%s'"), copy);
57938635 747 fatal (_("supported flags: %s"),
5dddde8e 748 "alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings");
252b5132
RH
749 }
750
751 s = snext;
752 }
753 while (s != NULL);
754
755 return ret;
756}
757
2b35fb28
RH
758/* Parse symbol flags into a flagword, with a fatal error if the
759 string can't be parsed. */
760
761static flagword
762parse_symflags (const char *s, char **other)
763{
764 flagword ret;
765 const char *snext;
f7433f01 766 size_t len;
2b35fb28
RH
767
768 ret = BSF_NO_FLAGS;
769
770 do
771 {
772 snext = strchr (s, ',');
773 if (snext == NULL)
f7433f01 774 len = strlen (s);
2b35fb28
RH
775 else
776 {
777 len = snext - s;
778 ++snext;
779 }
780
f7433f01
AM
781#define PARSE_FLAG(fname, fval) \
782 else if (len == sizeof fname - 1 \
783 && strncasecmp (fname, s, len) == 0) \
2b35fb28
RH
784 ret |= fval
785
f7433f01
AM
786#define PARSE_OTHER(fname, fval) \
787 else if (len >= sizeof fname \
788 && strncasecmp (fname, s, sizeof fname - 1) == 0) \
a4f8732b 789 fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
f7433f01 790
2b35fb28
RH
791 if (0) ;
792 PARSE_FLAG ("local", BSF_LOCAL);
793 PARSE_FLAG ("global", BSF_GLOBAL);
794 PARSE_FLAG ("export", BSF_EXPORT);
795 PARSE_FLAG ("debug", BSF_DEBUGGING);
796 PARSE_FLAG ("function", BSF_FUNCTION);
797 PARSE_FLAG ("weak", BSF_WEAK);
798 PARSE_FLAG ("section", BSF_SECTION_SYM);
799 PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
800 PARSE_FLAG ("warning", BSF_WARNING);
801 PARSE_FLAG ("indirect", BSF_INDIRECT);
802 PARSE_FLAG ("file", BSF_FILE);
803 PARSE_FLAG ("object", BSF_OBJECT);
804 PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
805 PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
806 PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
807 PARSE_OTHER ("before=", *other);
808
809#undef PARSE_FLAG
810#undef PARSE_OTHER
811 else
812 {
813 char *copy;
814
815 copy = (char *) xmalloc (len + 1);
816 strncpy (copy, s, len);
817 copy[len] = '\0';
818 non_fatal (_("unrecognized symbol flag `%s'"), copy);
819 fatal (_("supported flags: %s"),
f7433f01
AM
820 "local, global, export, debug, function, weak, section, "
821 "constructor, warning, indirect, file, object, synthetic, "
822 "indirect-function, unique-object, before=<othersym>");
2b35fb28
RH
823 }
824
825 s = snext;
826 }
827 while (s != NULL);
828
829 return ret;
830}
831
2e62b721
NC
832/* Find and optionally add an entry in the change_sections list.
833
834 We need to be careful in how we match section names because of the support
835 for wildcard characters. For example suppose that the user has invoked
836 objcopy like this:
3aade688 837
2e62b721
NC
838 --set-section-flags .debug_*=debug
839 --set-section-flags .debug_str=readonly,debug
840 --change-section-address .debug_*ranges=0x1000
841
842 With the idea that all debug sections will receive the DEBUG flag, the
843 .debug_str section will also receive the READONLY flag and the
844 .debug_ranges and .debug_aranges sections will have their address set to
845 0x1000. (This may not make much sense, but it is just an example).
846
847 When adding the section name patterns to the section list we need to make
848 sure that previous entries do not match with the new entry, unless the
849 match is exact. (In which case we assume that the user is overriding
850 the previous entry with the new context).
851
852 When matching real section names to the section list we make use of the
853 wildcard characters, but we must do so in context. Eg if we are setting
854 section addresses then we match for .debug_ranges but not for .debug_info.
855
856 Finally, if ADD is false and we do find a match, we mark the section list
857 entry as used. */
252b5132
RH
858
859static struct section_list *
2e62b721 860find_section_list (const char *name, bfd_boolean add, unsigned int context)
252b5132 861{
e511c9b1 862 struct section_list *p, *match = NULL;
252b5132 863
2e62b721 864 /* assert ((context & ((1 << 7) - 1)) != 0); */
3aade688 865
252b5132 866 for (p = change_sections; p != NULL; p = p->next)
2e62b721
NC
867 {
868 if (add)
869 {
870 if (strcmp (p->pattern, name) == 0)
871 {
872 /* Check for context conflicts. */
873 if (((p->context & SECTION_CONTEXT_REMOVE)
874 && (context & SECTION_CONTEXT_COPY))
875 || ((context & SECTION_CONTEXT_REMOVE)
876 && (p->context & SECTION_CONTEXT_COPY)))
877 fatal (_("error: %s both copied and removed"), name);
878
879 if (((p->context & SECTION_CONTEXT_SET_VMA)
880 && (context & SECTION_CONTEXT_ALTER_VMA))
881 || ((context & SECTION_CONTEXT_SET_VMA)
882 && (context & SECTION_CONTEXT_ALTER_VMA)))
883 fatal (_("error: %s both sets and alters VMA"), name);
884
885 if (((p->context & SECTION_CONTEXT_SET_LMA)
886 && (context & SECTION_CONTEXT_ALTER_LMA))
887 || ((context & SECTION_CONTEXT_SET_LMA)
888 && (context & SECTION_CONTEXT_ALTER_LMA)))
889 fatal (_("error: %s both sets and alters LMA"), name);
890
891 /* Extend the context. */
892 p->context |= context;
893 return p;
894 }
895 }
896 /* If we are not adding a new name/pattern then
897 only check for a match if the context applies. */
e511c9b1
AB
898 else if (p->context & context)
899 {
900 /* We could check for the presence of wildchar characters
901 first and choose between calling strcmp and fnmatch,
902 but is that really worth it ? */
903 if (p->pattern [0] == '!')
904 {
905 if (fnmatch (p->pattern + 1, name, 0) == 0)
906 {
907 p->used = TRUE;
908 return NULL;
909 }
910 }
911 else
912 {
913 if (fnmatch (p->pattern, name, 0) == 0)
914 {
915 if (match == NULL)
916 match = p;
917 }
918 }
919 }
2e62b721 920 }
252b5132
RH
921
922 if (! add)
e511c9b1
AB
923 {
924 if (match != NULL)
925 match->used = TRUE;
926 return match;
927 }
252b5132 928
3f5e193b 929 p = (struct section_list *) xmalloc (sizeof (struct section_list));
2e62b721 930 p->pattern = name;
b34976b6 931 p->used = FALSE;
2e62b721 932 p->context = context;
252b5132
RH
933 p->vma_val = 0;
934 p->lma_val = 0;
252b5132 935 p->flags = 0;
252b5132
RH
936 p->next = change_sections;
937 change_sections = p;
938
939 return p;
940}
941
047c9024
NC
942/* There is htab_hash_string but no htab_eq_string. Makes sense. */
943
944static int
945eq_string (const void *s1, const void *s2)
946{
3f5e193b 947 return strcmp ((const char *) s1, (const char *) s2) == 0;
047c9024
NC
948}
949
950static htab_t
951create_symbol_htab (void)
952{
953 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
954}
252b5132 955
57938635 956static void
047c9024 957create_symbol_htabs (void)
252b5132 958{
047c9024
NC
959 strip_specific_htab = create_symbol_htab ();
960 strip_unneeded_htab = create_symbol_htab ();
961 keep_specific_htab = create_symbol_htab ();
962 localize_specific_htab = create_symbol_htab ();
963 globalize_specific_htab = create_symbol_htab ();
964 keepglobal_specific_htab = create_symbol_htab ();
965 weaken_specific_htab = create_symbol_htab ();
966}
967
968/* Add a symbol to strip_specific_list. */
252b5132 969
047c9024
NC
970static void
971add_specific_symbol (const char *name, htab_t htab)
972{
973 *htab_find_slot (htab, name, INSERT) = (char *) name;
252b5132
RH
974}
975
0af11b59 976/* Add symbols listed in `filename' to strip_specific_list. */
16b2b71c
NC
977
978#define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
979#define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
980
981static void
047c9024 982add_specific_symbols (const char *filename, htab_t htab)
16b2b71c 983{
f24ddbdd 984 off_t size;
16b2b71c
NC
985 FILE * f;
986 char * line;
987 char * buffer;
988 unsigned int line_count;
0af11b59 989
f24ddbdd
NC
990 size = get_file_size (filename);
991 if (size == 0)
d68c385b
NC
992 {
993 status = 1;
994 return;
995 }
16b2b71c 996
3f5e193b 997 buffer = (char *) xmalloc (size + 2);
16b2b71c
NC
998 f = fopen (filename, FOPEN_RT);
999 if (f == NULL)
f24ddbdd 1000 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
16b2b71c 1001
f24ddbdd 1002 if (fread (buffer, 1, size, f) == 0 || ferror (f))
16b2b71c
NC
1003 fatal (_("%s: fread failed"), filename);
1004
1005 fclose (f);
f24ddbdd
NC
1006 buffer [size] = '\n';
1007 buffer [size + 1] = '\0';
16b2b71c
NC
1008
1009 line_count = 1;
0af11b59 1010
16b2b71c
NC
1011 for (line = buffer; * line != '\0'; line ++)
1012 {
1013 char * eol;
1014 char * name;
1015 char * name_end;
b34976b6 1016 int finished = FALSE;
16b2b71c
NC
1017
1018 for (eol = line;; eol ++)
1019 {
1020 switch (* eol)
1021 {
1022 case '\n':
1023 * eol = '\0';
1024 /* Cope with \n\r. */
1025 if (eol[1] == '\r')
1026 ++ eol;
b34976b6 1027 finished = TRUE;
16b2b71c 1028 break;
0af11b59 1029
16b2b71c
NC
1030 case '\r':
1031 * eol = '\0';
1032 /* Cope with \r\n. */
1033 if (eol[1] == '\n')
1034 ++ eol;
b34976b6 1035 finished = TRUE;
16b2b71c 1036 break;
0af11b59 1037
16b2b71c 1038 case 0:
b34976b6 1039 finished = TRUE;
16b2b71c 1040 break;
0af11b59 1041
16b2b71c
NC
1042 case '#':
1043 /* Line comment, Terminate the line here, in case a
1044 name is present and then allow the rest of the
1045 loop to find the real end of the line. */
1046 * eol = '\0';
1047 break;
0af11b59 1048
16b2b71c
NC
1049 default:
1050 break;
1051 }
1052
1053 if (finished)
1054 break;
1055 }
1056
1057 /* A name may now exist somewhere between 'line' and 'eol'.
1058 Strip off leading whitespace and trailing whitespace,
1059 then add it to the list. */
1060 for (name = line; IS_WHITESPACE (* name); name ++)
1061 ;
1062 for (name_end = name;
1063 (! IS_WHITESPACE (* name_end))
1064 && (! IS_LINE_TERMINATOR (* name_end));
0af11b59
KH
1065 name_end ++)
1066 ;
16b2b71c
NC
1067
1068 if (! IS_LINE_TERMINATOR (* name_end))
1069 {
1070 char * extra;
1071
1072 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
1073 ;
1074
1075 if (! IS_LINE_TERMINATOR (* extra))
d412a550
NC
1076 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
1077 filename, line_count);
16b2b71c 1078 }
0af11b59 1079
16b2b71c
NC
1080 * name_end = '\0';
1081
1082 if (name_end > name)
047c9024 1083 add_specific_symbol (name, htab);
16b2b71c
NC
1084
1085 /* Advance line pointer to end of line. The 'eol ++' in the for
1086 loop above will then advance us to the start of the next line. */
1087 line = eol;
1088 line_count ++;
1089 }
1090}
1091
047c9024
NC
1092/* See whether a symbol should be stripped or kept
1093 based on strip_specific_list and keep_symbols. */
252b5132 1094
047c9024
NC
1095static int
1096is_specified_symbol_predicate (void **slot, void *data)
252b5132 1097{
3f5e193b
NC
1098 struct is_specified_symbol_predicate_data *d =
1099 (struct is_specified_symbol_predicate_data *) data;
1100 const char *slot_name = (char *) *slot;
252b5132 1101
047c9024 1102 if (*slot_name != '!')
5fe11841 1103 {
047c9024
NC
1104 if (! fnmatch (slot_name, d->name, 0))
1105 {
1106 d->found = TRUE;
0b45135e
AB
1107 /* Continue traversal, there might be a non-match rule. */
1108 return 1;
047c9024 1109 }
5fe11841
NC
1110 }
1111 else
1112 {
0b45135e 1113 if (! fnmatch (slot_name + 1, d->name, 0))
047c9024 1114 {
0b45135e 1115 d->found = FALSE;
047c9024
NC
1116 /* Stop traversal. */
1117 return 0;
1118 }
5fe11841 1119 }
594ef5db 1120
047c9024
NC
1121 /* Continue traversal. */
1122 return 1;
1123}
1124
1125static bfd_boolean
1126is_specified_symbol (const char *name, htab_t htab)
1127{
1128 if (wildcard)
1129 {
1130 struct is_specified_symbol_predicate_data data;
1131
1132 data.name = name;
1133 data.found = FALSE;
1134
1135 htab_traverse (htab, is_specified_symbol_predicate, &data);
1136
1137 return data.found;
1138 }
1139
1140 return htab_find (htab, name) != NULL;
252b5132
RH
1141}
1142
30288845
AM
1143/* Return a pointer to the symbol used as a signature for GROUP. */
1144
1145static asymbol *
1146group_signature (asection *group)
1147{
1148 bfd *abfd = group->owner;
1149 Elf_Internal_Shdr *ghdr;
1150
bcc3a8bc
NC
1151 /* PR 20089: An earlier error may have prevented us from loading the symbol table. */
1152 if (isympp == NULL)
1153 return NULL;
1154
30288845
AM
1155 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1156 return NULL;
1157
1158 ghdr = &elf_section_data (group)->this_hdr;
1159 if (ghdr->sh_link < elf_numsections (abfd))
1160 {
1161 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1162 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
1163
1164 if (symhdr->sh_type == SHT_SYMTAB
c09ec62d
NC
1165 && ghdr->sh_info > 0
1166 && ghdr->sh_info < (symhdr->sh_size / bed->s->sizeof_sym))
748fc5e9 1167 return isympp[ghdr->sh_info - 1];
30288845
AM
1168 }
1169 return NULL;
1170}
1171
96109726
CC
1172/* Return TRUE if the section is a DWO section. */
1173
1174static bfd_boolean
1175is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1176{
1177 const char *name = bfd_get_section_name (abfd, sec);
1178 int len = strlen (name);
1179
1180 return strncmp (name + len - 4, ".dwo", 4) == 0;
1181}
1182
acf1419f
AB
1183/* Return TRUE if section SEC is in the update list. */
1184
1185static bfd_boolean
1186is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1187{
1188 if (update_sections != NULL)
1189 {
1190 struct section_add *pupdate;
1191
1192 for (pupdate = update_sections;
f7433f01
AM
1193 pupdate != NULL;
1194 pupdate = pupdate->next)
acf1419f 1195 {
f7433f01
AM
1196 if (strcmp (sec->name, pupdate->name) == 0)
1197 return TRUE;
1198 }
acf1419f
AB
1199 }
1200
1201 return FALSE;
1202}
1203
4c8e8a7e 1204/* See if a non-group section is being removed. */
252b5132 1205
b34976b6 1206static bfd_boolean
4c8e8a7e 1207is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
252b5132 1208{
2593f09a
NC
1209 if (sections_removed || sections_copied)
1210 {
1211 struct section_list *p;
2e62b721 1212 struct section_list *q;
2593f09a 1213
2e62b721
NC
1214 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1215 SECTION_CONTEXT_REMOVE);
1216 q = find_section_list (bfd_get_section_name (abfd, sec), FALSE,
1217 SECTION_CONTEXT_COPY);
2593f09a 1218
2e62b721
NC
1219 if (p && q)
1220 fatal (_("error: section %s matches both remove and copy options"),
1221 bfd_get_section_name (abfd, sec));
acf1419f 1222 if (p && is_update_section (abfd, sec))
f7433f01
AM
1223 fatal (_("error: section %s matches both update and remove options"),
1224 bfd_get_section_name (abfd, sec));
2e62b721
NC
1225
1226 if (p != NULL)
2593f09a 1227 return TRUE;
2e62b721 1228 if (sections_copied && q == NULL)
2593f09a
NC
1229 return TRUE;
1230 }
252b5132 1231
2593f09a
NC
1232 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
1233 {
1234 if (strip_symbols == STRIP_DEBUG
252b5132
RH
1235 || strip_symbols == STRIP_UNNEEDED
1236 || strip_symbols == STRIP_ALL
1237 || discard_locals == LOCALS_ALL
2593f09a 1238 || convert_debugging)
4fc8b895
KT
1239 {
1240 /* By default we don't want to strip .reloc section.
1241 This section has for pe-coff special meaning. See
1242 pe-dll.c file in ld, and peXXigen.c in bfd for details. */
1243 if (strcmp (bfd_get_section_name (abfd, sec), ".reloc") != 0)
1244 return TRUE;
1245 }
ed1653a7 1246
96109726
CC
1247 if (strip_symbols == STRIP_DWO)
1248 return is_dwo_section (abfd, sec);
1249
ed1653a7
NC
1250 if (strip_symbols == STRIP_NONDEBUG)
1251 return FALSE;
2593f09a 1252 }
f91ea849 1253
96109726
CC
1254 if (strip_symbols == STRIP_NONDWO)
1255 return !is_dwo_section (abfd, sec);
1256
4c8e8a7e
L
1257 return FALSE;
1258}
1259
1260/* See if a section is being removed. */
1261
1262static bfd_boolean
1263is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1264{
1265 if (is_strip_section_1 (abfd, sec))
1266 return TRUE;
1267
30288845
AM
1268 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
1269 {
1270 asymbol *gsym;
1271 const char *gname;
4c8e8a7e 1272 asection *elt, *first;
30288845 1273
30288845
AM
1274 /* PR binutils/3181
1275 If we are going to strip the group signature symbol, then
1276 strip the group section too. */
1277 gsym = group_signature (sec);
1278 if (gsym != NULL)
1279 gname = gsym->name;
1280 else
1281 gname = sec->name;
1282 if ((strip_symbols == STRIP_ALL
047c9024
NC
1283 && !is_specified_symbol (gname, keep_specific_htab))
1284 || is_specified_symbol (gname, strip_specific_htab))
30288845 1285 return TRUE;
4c8e8a7e
L
1286
1287 /* Remove the group section if all members are removed. */
1288 first = elt = elf_next_in_group (sec);
1289 while (elt != NULL)
1290 {
1291 if (!is_strip_section_1 (abfd, elt))
1292 return FALSE;
1293 elt = elf_next_in_group (elt);
1294 if (elt == first)
1295 break;
1296 }
1297
1298 return TRUE;
30288845 1299 }
91bb255c 1300
f0312d39 1301 return FALSE;
252b5132
RH
1302}
1303
6e6e7cfc
JT
1304static bfd_boolean
1305is_nondebug_keep_contents_section (bfd *ibfd, asection *isection)
1306{
1307 /* Always keep ELF note sections. */
1308 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
1309 return (elf_section_type (isection) == SHT_NOTE);
1310
74fffc39 1311 /* Always keep the .buildid section for PE/COFF.
6e6e7cfc
JT
1312
1313 Strictly, this should be written "always keep the section storing the debug
1314 directory", but that may be the .text section for objects produced by some
1315 tools, which it is not sensible to keep. */
1316 if (ibfd->xvec->flavour == bfd_target_coff_flavour)
74fffc39 1317 return (strcmp (bfd_get_section_name (ibfd, isection), ".buildid") == 0);
6e6e7cfc
JT
1318
1319 return FALSE;
1320}
1321
d58c2e3a
RS
1322/* Return true if SYM is a hidden symbol. */
1323
1324static bfd_boolean
1325is_hidden_symbol (asymbol *sym)
1326{
1327 elf_symbol_type *elf_sym;
1328
1329 elf_sym = elf_symbol_from (sym->the_bfd, sym);
1330 if (elf_sym != NULL)
1331 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
1332 {
1333 case STV_HIDDEN:
1334 case STV_INTERNAL:
1335 return TRUE;
1336 }
1337 return FALSE;
1338}
1339
2b35fb28
RH
1340static bfd_boolean
1341need_sym_before (struct addsym_node **node, const char *sym)
1342{
1343 int count;
1344 struct addsym_node *ptr = add_sym_list;
1345
1346 /* 'othersym' symbols are at the front of the list. */
1347 for (count = 0; count < add_symbols; count++)
1348 {
1349 if (!ptr->othersym)
1350 break;
1351 else if (strcmp (ptr->othersym, sym) == 0)
1352 {
1353 free (ptr->othersym);
1354 ptr->othersym = ""; /* Empty name is hopefully never a valid symbol name. */
1355 *node = ptr;
1356 return TRUE;
1357 }
1358 ptr = ptr->next;
1359 }
1360 return FALSE;
1361}
1362
1363static asymbol *
1364create_new_symbol (struct addsym_node *ptr, bfd *obfd)
1365{
f7433f01 1366 asymbol *sym = bfd_make_empty_symbol (obfd);
2b35fb28 1367
f7433f01 1368 bfd_asymbol_name (sym) = ptr->symdef;
2b35fb28
RH
1369 sym->value = ptr->symval;
1370 sym->flags = ptr->flags;
1371 if (ptr->section)
1372 {
1373 asection *sec = bfd_get_section_by_name (obfd, ptr->section);
1374 if (!sec)
1375 fatal (_("Section %s not found"), ptr->section);
1376 sym->section = sec;
1377 }
f7433f01
AM
1378 else
1379 sym->section = bfd_abs_section_ptr;
2b35fb28
RH
1380 return sym;
1381}
1382
252b5132
RH
1383/* Choose which symbol entries to copy; put the result in OSYMS.
1384 We don't copy in place, because that confuses the relocs.
1385 Return the number of symbols to print. */
1386
1387static unsigned int
84e2f313
NC
1388filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
1389 asymbol **isyms, long symcount)
252b5132 1390{
84e2f313 1391 asymbol **from = isyms, **to = osyms;
252b5132 1392 long src_count = 0, dst_count = 0;
e205a099 1393 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
252b5132
RH
1394
1395 for (; src_count < symcount; src_count++)
1396 {
1397 asymbol *sym = from[src_count];
1398 flagword flags = sym->flags;
d7fb0dd2 1399 char *name = (char *) bfd_asymbol_name (sym);
312aaa3c
NC
1400 bfd_boolean keep;
1401 bfd_boolean used_in_reloc = FALSE;
b34976b6 1402 bfd_boolean undefined;
d7fb0dd2
NC
1403 bfd_boolean rem_leading_char;
1404 bfd_boolean add_leading_char;
1405
1406 undefined = bfd_is_und_section (bfd_get_section (sym));
252b5132 1407
2b35fb28
RH
1408 if (add_sym_list)
1409 {
1410 struct addsym_node *ptr;
1411
1412 if (need_sym_before (&ptr, name))
1413 to[dst_count++] = create_new_symbol (ptr, obfd);
1414 }
1415
9cc0123f 1416 if (redefine_sym_list || section_rename_list)
57938635 1417 {
9cc0123f 1418 char *new_name;
57938635 1419
9cc0123f
AM
1420 new_name = (char *) lookup_sym_redefinition (name);
1421 if (new_name == name
1422 && (flags & BSF_SECTION_SYM) != 0)
1423 new_name = (char *) find_section_rename (name, NULL);
66491ebc
AM
1424 bfd_asymbol_name (sym) = new_name;
1425 name = new_name;
57938635
AM
1426 }
1427
d7fb0dd2
NC
1428 /* Check if we will remove the current leading character. */
1429 rem_leading_char =
1430 (name[0] == bfd_get_symbol_leading_char (abfd))
1431 && (change_leading_char
1432 || (remove_leading_char
1433 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1434 || undefined
1435 || bfd_is_com_section (bfd_get_section (sym)))));
1436
1437 /* Check if we will add a new leading character. */
1438 add_leading_char =
1439 change_leading_char
1440 && (bfd_get_symbol_leading_char (obfd) != '\0')
1441 && (bfd_get_symbol_leading_char (abfd) == '\0'
1442 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1443
1444 /* Short circuit for change_leading_char if we can do it in-place. */
1445 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
f7433f01 1446 {
d7fb0dd2
NC
1447 name[0] = bfd_get_symbol_leading_char (obfd);
1448 bfd_asymbol_name (sym) = name;
1449 rem_leading_char = FALSE;
1450 add_leading_char = FALSE;
f7433f01 1451 }
d7fb0dd2
NC
1452
1453 /* Remove leading char. */
1454 if (rem_leading_char)
66491ebc 1455 bfd_asymbol_name (sym) = ++name;
d7fb0dd2
NC
1456
1457 /* Add new leading char and/or prefix. */
1458 if (add_leading_char || prefix_symbols_string)
f7433f01
AM
1459 {
1460 char *n, *ptr;
d7fb0dd2 1461
f7433f01
AM
1462 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1463 + strlen (name) + 1);
1464 if (add_leading_char)
d7fb0dd2
NC
1465 *ptr++ = bfd_get_symbol_leading_char (obfd);
1466
f7433f01
AM
1467 if (prefix_symbols_string)
1468 {
1469 strcpy (ptr, prefix_symbols_string);
1470 ptr += strlen (prefix_symbols_string);
1471 }
d7fb0dd2 1472
f7433f01
AM
1473 strcpy (ptr, name);
1474 bfd_asymbol_name (sym) = n;
1475 name = n;
252b5132
RH
1476 }
1477
252b5132 1478 if (strip_symbols == STRIP_ALL)
312aaa3c 1479 keep = FALSE;
252b5132
RH
1480 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1481 || ((flags & BSF_SECTION_SYM) != 0
1482 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1483 & BSF_KEEP) != 0))
312aaa3c
NC
1484 {
1485 keep = TRUE;
1486 used_in_reloc = TRUE;
1487 }
0af11b59 1488 else if (relocatable /* Relocatable file. */
0691f7af
L
1489 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1490 || bfd_is_com_section (bfd_get_section (sym))))
312aaa3c 1491 keep = TRUE;
16b2b71c
NC
1492 else if (bfd_decode_symclass (sym) == 'I')
1493 /* Global symbols in $idata sections need to be retained
b34976b6 1494 even if relocatable is FALSE. External users of the
16b2b71c
NC
1495 library containing the $idata section may reference these
1496 symbols. */
312aaa3c 1497 keep = TRUE;
252b5132
RH
1498 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1499 || (flags & BSF_WEAK) != 0
24e01a36 1500 || undefined
252b5132
RH
1501 || bfd_is_com_section (bfd_get_section (sym)))
1502 keep = strip_symbols != STRIP_UNNEEDED;
1503 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1504 keep = (strip_symbols != STRIP_DEBUG
1505 && strip_symbols != STRIP_UNNEEDED
1506 && ! convert_debugging);
082b7297 1507 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
af3bdff7
NC
1508 /* COMDAT sections store special information in local
1509 symbols, so we cannot risk stripping any of them. */
312aaa3c 1510 keep = TRUE;
252b5132
RH
1511 else /* Local symbol. */
1512 keep = (strip_symbols != STRIP_UNNEEDED
1513 && (discard_locals != LOCALS_ALL
1514 && (discard_locals != LOCALS_START_L
1515 || ! bfd_is_local_label (abfd, sym))));
1516
047c9024 1517 if (keep && is_specified_symbol (name, strip_specific_htab))
312aaa3c
NC
1518 {
1519 /* There are multiple ways to set 'keep' above, but if it
1520 was the relocatable symbol case, then that's an error. */
1521 if (used_in_reloc)
1522 {
1523 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1524 status = 1;
1525 }
1526 else
1527 keep = FALSE;
1528 }
1529
bcf32829
JB
1530 if (keep
1531 && !(flags & BSF_KEEP)
047c9024 1532 && is_specified_symbol (name, strip_unneeded_htab))
312aaa3c
NC
1533 keep = FALSE;
1534
1637cd90
JB
1535 if (!keep
1536 && ((keep_file_symbols && (flags & BSF_FILE))
047c9024 1537 || is_specified_symbol (name, keep_specific_htab)))
312aaa3c
NC
1538 keep = TRUE;
1539
252b5132 1540 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
312aaa3c 1541 keep = FALSE;
e0c60db2 1542
7b4a0685 1543 if (keep)
252b5132 1544 {
7b4a0685 1545 if ((flags & BSF_GLOBAL) != 0
047c9024 1546 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
7b4a0685
NC
1547 {
1548 sym->flags &= ~ BSF_GLOBAL;
1549 sym->flags |= BSF_WEAK;
1550 }
252b5132 1551
7b4a0685
NC
1552 if (!undefined
1553 && (flags & (BSF_GLOBAL | BSF_WEAK))
047c9024
NC
1554 && (is_specified_symbol (name, localize_specific_htab)
1555 || (htab_elements (keepglobal_specific_htab) != 0
1556 && ! is_specified_symbol (name, keepglobal_specific_htab))
d58c2e3a 1557 || (localize_hidden && is_hidden_symbol (sym))))
7b4a0685
NC
1558 {
1559 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1560 sym->flags |= BSF_LOCAL;
1561 }
1562
1563 if (!undefined
c1c0eb9e 1564 && (flags & BSF_LOCAL)
047c9024 1565 && is_specified_symbol (name, globalize_specific_htab))
7b4a0685
NC
1566 {
1567 sym->flags &= ~ BSF_LOCAL;
1568 sym->flags |= BSF_GLOBAL;
1569 }
1570
1571 to[dst_count++] = sym;
1572 }
252b5132 1573 }
2b35fb28
RH
1574 if (add_sym_list)
1575 {
1576 struct addsym_node *ptr = add_sym_list;
1577
1578 for (src_count = 0; src_count < add_symbols; src_count++)
1579 {
1580 if (ptr->othersym)
1581 {
1582 if (strcmp (ptr->othersym, ""))
1583 fatal (_("'before=%s' not found"), ptr->othersym);
1584 }
1585 else
1586 to[dst_count++] = create_new_symbol (ptr, obfd);
1587
1588 ptr = ptr->next;
1589 }
1590 }
252b5132
RH
1591
1592 to[dst_count] = NULL;
1593
1594 return dst_count;
1595}
1596
594ef5db
NC
1597/* Find the redefined name of symbol SOURCE. */
1598
57938635 1599static const char *
84e2f313 1600lookup_sym_redefinition (const char *source)
57938635 1601{
57938635
AM
1602 struct redefine_node *list;
1603
57938635 1604 for (list = redefine_sym_list; list != NULL; list = list->next)
594ef5db
NC
1605 if (strcmp (source, list->source) == 0)
1606 return list->target;
1607
1608 return source;
57938635
AM
1609}
1610
594ef5db 1611/* Add a node to a symbol redefine list. */
57938635
AM
1612
1613static void
84e2f313 1614redefine_list_append (const char *cause, const char *source, const char *target)
57938635
AM
1615{
1616 struct redefine_node **p;
1617 struct redefine_node *list;
1618 struct redefine_node *new_node;
1619
1620 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1621 {
1622 if (strcmp (source, list->source) == 0)
594ef5db 1623 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
92991082 1624 cause, source);
57938635
AM
1625
1626 if (strcmp (target, list->target) == 0)
594ef5db 1627 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
92991082 1628 cause, target);
57938635
AM
1629 }
1630
3f5e193b 1631 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
57938635
AM
1632
1633 new_node->source = strdup (source);
1634 new_node->target = strdup (target);
1635 new_node->next = NULL;
1636
1637 *p = new_node;
1638}
1639
92991082
JT
1640/* Handle the --redefine-syms option. Read lines containing "old new"
1641 from the file, and add them to the symbol redefine list. */
1642
2593f09a 1643static void
84e2f313 1644add_redefine_syms_file (const char *filename)
92991082
JT
1645{
1646 FILE *file;
1647 char *buf;
84e2f313
NC
1648 size_t bufsize;
1649 size_t len;
1650 size_t outsym_off;
92991082
JT
1651 int c, lineno;
1652
1653 file = fopen (filename, "r");
d3ba0551 1654 if (file == NULL)
92991082
JT
1655 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1656 filename, strerror (errno));
1657
1658 bufsize = 100;
a6da20b5 1659 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
92991082
JT
1660
1661 lineno = 1;
1662 c = getc (file);
1663 len = 0;
1664 outsym_off = 0;
1665 while (c != EOF)
1666 {
1667 /* Collect the input symbol name. */
1668 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1669 {
1670 if (c == '#')
1671 goto comment;
1672 buf[len++] = c;
1673 if (len >= bufsize)
1674 {
1675 bufsize *= 2;
a6da20b5 1676 buf = (char *) xrealloc (buf, bufsize + 1);
92991082
JT
1677 }
1678 c = getc (file);
1679 }
1680 buf[len++] = '\0';
1681 if (c == EOF)
1682 break;
1683
1684 /* Eat white space between the symbol names. */
1685 while (IS_WHITESPACE (c))
1686 c = getc (file);
1687 if (c == '#' || IS_LINE_TERMINATOR (c))
1688 goto comment;
1689 if (c == EOF)
1690 break;
1691
1692 /* Collect the output symbol name. */
1693 outsym_off = len;
1694 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1695 {
1696 if (c == '#')
1697 goto comment;
1698 buf[len++] = c;
1699 if (len >= bufsize)
1700 {
1701 bufsize *= 2;
a6da20b5 1702 buf = (char *) xrealloc (buf, bufsize + 1);
92991082
JT
1703 }
1704 c = getc (file);
1705 }
1706 buf[len++] = '\0';
1707 if (c == EOF)
1708 break;
1709
1710 /* Eat white space at end of line. */
1711 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1712 c = getc (file);
1713 if (c == '#')
1714 goto comment;
1715 /* Handle \r\n. */
1716 if ((c == '\r' && (c = getc (file)) == '\n')
1717 || c == '\n' || c == EOF)
1718 {
f7433f01 1719 end_of_line:
92991082
JT
1720 /* Append the redefinition to the list. */
1721 if (buf[0] != '\0')
1722 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1723
c1c0eb9e 1724 lineno++;
92991082
JT
1725 len = 0;
1726 outsym_off = 0;
1727 if (c == EOF)
1728 break;
1729 c = getc (file);
1730 continue;
1731 }
1732 else
d412a550 1733 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
f7433f01 1734 comment:
92991082 1735 if (len != 0 && (outsym_off == 0 || outsym_off == len))
d412a550 1736 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
92991082
JT
1737 buf[len++] = '\0';
1738
1739 /* Eat the rest of the line and finish it. */
1740 while (c != '\n' && c != EOF)
1741 c = getc (file);
1742 goto end_of_line;
1743 }
1744
1745 if (len != 0)
d412a550 1746 fatal (_("%s:%d: premature end of file"), filename, lineno);
92991082
JT
1747
1748 free (buf);
1749}
1750
222c2bf0 1751/* Copy unknown object file IBFD onto OBFD.
77f762d6
L
1752 Returns TRUE upon success, FALSE otherwise. */
1753
1754static bfd_boolean
1755copy_unknown_object (bfd *ibfd, bfd *obfd)
1756{
1757 char *cbuf;
1758 int tocopy;
1759 long ncopied;
1760 long size;
1761 struct stat buf;
1762
1763 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1764 {
8d8e0703 1765 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
77f762d6
L
1766 return FALSE;
1767 }
1768
1769 size = buf.st_size;
1770 if (size < 0)
1771 {
1772 non_fatal (_("stat returns negative size for `%s'"),
1773 bfd_get_archive_filename (ibfd));
1774 return FALSE;
1775 }
1776
1777 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1778 {
1779 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1780 return FALSE;
1781 }
1782
1783 if (verbose)
1784 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1785 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1786
3f5e193b 1787 cbuf = (char *) xmalloc (BUFSIZE);
77f762d6
L
1788 ncopied = 0;
1789 while (ncopied < size)
1790 {
1791 tocopy = size - ncopied;
1792 if (tocopy > BUFSIZE)
1793 tocopy = BUFSIZE;
1794
1795 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1796 != (bfd_size_type) tocopy)
1797 {
8d8e0703 1798 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
77f762d6
L
1799 free (cbuf);
1800 return FALSE;
1801 }
1802
1803 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1804 != (bfd_size_type) tocopy)
1805 {
2db6cde7 1806 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
77f762d6
L
1807 free (cbuf);
1808 return FALSE;
1809 }
1810
1811 ncopied += tocopy;
1812 }
1813
1e99536a
L
1814 /* We should at least to be able to read it back when copying an
1815 unknown object in an archive. */
1816 chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
77f762d6
L
1817 free (cbuf);
1818 return TRUE;
1819}
1820
950d48e7 1821/* Copy object file IBFD onto OBFD.
5b8c74e6 1822 Returns TRUE upon success, FALSE otherwise. */
252b5132 1823
950d48e7 1824static bfd_boolean
8b31b6c4 1825copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
252b5132
RH
1826{
1827 bfd_vma start;
1828 long symcount;
1829 asection **osections = NULL;
84e2f313 1830 asection *gnu_debuglink_section = NULL;
252b5132
RH
1831 bfd_size_type *gaps = NULL;
1832 bfd_size_type max_gap = 0;
1833 long symsize;
84e2f313 1834 void *dhandle;
66491ebc
AM
1835 enum bfd_architecture iarch;
1836 unsigned int imach;
c68c1637 1837 unsigned int c, i;
252b5132 1838
23719f39
NC
1839 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1840 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1841 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
cfad8730
NC
1842 {
1843 /* PR 17636: Call non-fatal so that we return to our parent who
1844 may need to tidy temporary files. */
1845 non_fatal (_("Unable to change endianness of input file(s)"));
1846 return FALSE;
1847 }
252b5132
RH
1848
1849 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
950d48e7 1850 {
2db6cde7 1851 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
950d48e7
NC
1852 return FALSE;
1853 }
252b5132 1854
5063a421
AM
1855 if (ibfd->sections == NULL)
1856 {
1857 non_fatal (_("error: the input file '%s' has no sections"),
1858 bfd_get_archive_filename (ibfd));
1859 return FALSE;
1860 }
1861
b8871f35 1862 if (ibfd->xvec->flavour != bfd_target_elf_flavour)
cd6faa73 1863 {
b8871f35
L
1864 if ((do_debug_sections & compress) != 0
1865 && do_debug_sections != compress)
1866 {
1867 non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
1868 bfd_get_archive_filename (ibfd));
1869 return FALSE;
1870 }
1871
1872 if (do_elf_stt_common)
1873 {
1874 non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
1875 bfd_get_archive_filename (ibfd));
1876 return FALSE;
1877 }
cd6faa73
L
1878 }
1879
252b5132 1880 if (verbose)
77f762d6
L
1881 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1882 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
252b5132
RH
1883 bfd_get_filename (obfd), bfd_get_target (obfd));
1884
d3e52d40
RS
1885 if (extract_symbol)
1886 start = 0;
252b5132 1887 else
d3e52d40
RS
1888 {
1889 if (set_start_set)
1890 start = set_start;
1891 else
1892 start = bfd_get_start_address (ibfd);
1893 start += change_start;
1894 }
252b5132 1895
0af11b59
KH
1896 /* Neither the start address nor the flags
1897 need to be set for a core file. */
4dd67f29
MS
1898 if (bfd_get_format (obfd) != bfd_core)
1899 {
4087920c
MR
1900 flagword flags;
1901
1902 flags = bfd_get_file_flags (ibfd);
1903 flags |= bfd_flags_to_set;
1904 flags &= ~bfd_flags_to_clear;
1905 flags &= bfd_applicable_file_flags (obfd);
1906
3516e984
L
1907 if (strip_symbols == STRIP_ALL)
1908 flags &= ~HAS_RELOC;
1909
4dd67f29 1910 if (!bfd_set_start_address (obfd, start)
4087920c 1911 || !bfd_set_file_flags (obfd, flags))
950d48e7 1912 {
8d8e0703 1913 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
950d48e7
NC
1914 return FALSE;
1915 }
4dd67f29 1916 }
252b5132 1917
594ef5db 1918 /* Copy architecture of input file to output file. */
66491ebc
AM
1919 iarch = bfd_get_arch (ibfd);
1920 imach = bfd_get_mach (ibfd);
8b31b6c4
NC
1921 if (input_arch)
1922 {
1923 if (bfd_get_arch_info (ibfd) == NULL
1924 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
1925 {
1926 iarch = input_arch->arch;
1927 imach = input_arch->mach;
1928 }
1929 else
1930 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1931 bfd_get_archive_filename (ibfd));
1932 }
66491ebc 1933 if (!bfd_set_arch_mach (obfd, iarch, imach)
212a3c4d
L
1934 && (ibfd->target_defaulted
1935 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
f57a841a
NC
1936 {
1937 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
77f762d6
L
1938 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1939 bfd_get_archive_filename (ibfd));
f57a841a 1940 else
c1e2cb9d 1941 non_fatal (_("Output file cannot represent architecture `%s'"),
77f762d6
L
1942 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1943 bfd_get_mach (ibfd)));
1944 return FALSE;
f57a841a 1945 }
57938635 1946
252b5132 1947 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
950d48e7 1948 {
8d8e0703 1949 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
950d48e7
NC
1950 return FALSE;
1951 }
252b5132 1952
92dd4511
L
1953 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1954 && bfd_pei_p (obfd))
1955 {
1956 /* Set up PE parameters. */
1957 pe_data_type *pe = pe_data (obfd);
1958
325c681d
L
1959 /* Copy PE parameters before changing them. */
1960 if (ibfd->xvec->flavour == bfd_target_coff_flavour
1961 && bfd_pei_p (ibfd))
1962 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1963
92dd4511
L
1964 if (pe_file_alignment != (bfd_vma) -1)
1965 pe->pe_opthdr.FileAlignment = pe_file_alignment;
1966 else
1967 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
1968
1969 if (pe_heap_commit != (bfd_vma) -1)
1970 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
1971
1972 if (pe_heap_reserve != (bfd_vma) -1)
1973 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
1974
1975 if (pe_image_base != (bfd_vma) -1)
1976 pe->pe_opthdr.ImageBase = pe_image_base;
1977
1978 if (pe_section_alignment != (bfd_vma) -1)
1979 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
1980 else
1981 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
1982
1983 if (pe_stack_commit != (bfd_vma) -1)
1984 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
1985
1986 if (pe_stack_reserve != (bfd_vma) -1)
1987 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
1988
1989 if (pe_subsystem != -1)
1990 pe->pe_opthdr.Subsystem = pe_subsystem;
1991
1992 if (pe_major_subsystem_version != -1)
1993 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
1994
1995 if (pe_minor_subsystem_version != -1)
1996 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
1997
1998 if (pe_file_alignment > pe_section_alignment)
1999 {
2000 char file_alignment[20], section_alignment[20];
2001
2002 sprintf_vma (file_alignment, pe_file_alignment);
2003 sprintf_vma (section_alignment, pe_section_alignment);
2004 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
2005
2006 file_alignment, section_alignment);
2007 }
2008 }
2009
252b5132 2010 if (isympp)
62d732f5 2011 free (isympp);
57938635 2012
252b5132 2013 if (osympp != isympp)
62d732f5
AM
2014 free (osympp);
2015
2016 isympp = NULL;
2017 osympp = NULL;
252b5132 2018
c39ada54
AM
2019 symsize = bfd_get_symtab_upper_bound (ibfd);
2020 if (symsize < 0)
2021 {
8d8e0703 2022 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
c39ada54
AM
2023 return FALSE;
2024 }
2025
3f5e193b 2026 osympp = isympp = (asymbol **) xmalloc (symsize);
c39ada54
AM
2027 symcount = bfd_canonicalize_symtab (ibfd, isympp);
2028 if (symcount < 0)
2029 {
2db6cde7 2030 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
c39ada54
AM
2031 return FALSE;
2032 }
063bb025
NC
2033 /* PR 17512: file: d6323821
2034 If the symbol table could not be loaded do not pretend that we have
2035 any symbols. This trips us up later on when we load the relocs. */
2036 if (symcount == 0)
2037 {
2038 free (isympp);
2039 osympp = isympp = NULL;
2040 }
c39ada54 2041
252b5132
RH
2042 /* BFD mandates that all output sections be created and sizes set before
2043 any output is done. Thus, we traverse all sections multiple times. */
d3ba0551 2044 bfd_map_over_sections (ibfd, setup_section, obfd);
252b5132 2045
237dcb53
AM
2046 if (!extract_symbol)
2047 setup_bfd_headers (ibfd, obfd);
80fccad2 2048
252b5132
RH
2049 if (add_sections != NULL)
2050 {
2051 struct section_add *padd;
2052 struct section_list *pset;
2053
2054 for (padd = add_sections; padd != NULL; padd = padd->next)
2055 {
2593f09a
NC
2056 flagword flags;
2057
2e62b721
NC
2058 pset = find_section_list (padd->name, FALSE,
2059 SECTION_CONTEXT_SET_FLAGS);
551b43fd 2060 if (pset != NULL)
551b43fd 2061 flags = pset->flags | SEC_HAS_CONTENTS;
2e62b721
NC
2062 else
2063 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
551b43fd 2064
c8782eee
NC
2065 /* bfd_make_section_with_flags() does not return very helpful
2066 error codes, so check for the most likely user error first. */
2067 if (bfd_get_section_by_name (obfd, padd->name))
252b5132 2068 {
2db6cde7 2069 bfd_nonfatal_message (NULL, obfd, NULL,
f7433f01 2070 _("can't add section '%s'"), padd->name);
950d48e7 2071 return FALSE;
252b5132 2072 }
c8782eee
NC
2073 else
2074 {
0930eddd 2075 /* We use LINKER_CREATED here so that the backend hooks
f7433f01
AM
2076 will create any special section type information,
2077 instead of presuming we know what we're doing merely
2078 because we set the flags. */
0930eddd
NS
2079 padd->section = bfd_make_section_with_flags
2080 (obfd, padd->name, flags | SEC_LINKER_CREATED);
c8782eee
NC
2081 if (padd->section == NULL)
2082 {
2db6cde7
NS
2083 bfd_nonfatal_message (NULL, obfd, NULL,
2084 _("can't create section `%s'"),
2085 padd->name);
c8782eee
NC
2086 return FALSE;
2087 }
2088 }
252b5132 2089
2593f09a 2090 if (! bfd_set_section_size (obfd, padd->section, padd->size))
950d48e7 2091 {
2db6cde7 2092 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
950d48e7
NC
2093 return FALSE;
2094 }
252b5132 2095
2e62b721
NC
2096 pset = find_section_list (padd->name, FALSE,
2097 SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
2098 if (pset != NULL
2099 && ! bfd_set_section_vma (obfd, padd->section, pset->vma_val))
2100 {
2101 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2102 return FALSE;
2103 }
2104
2105 pset = find_section_list (padd->name, FALSE,
2106 SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
2593f09a
NC
2107 if (pset != NULL)
2108 {
2e62b721 2109 padd->section->lma = pset->lma_val;
57938635 2110
2e62b721
NC
2111 if (! bfd_set_section_alignment
2112 (obfd, padd->section,
2113 bfd_section_alignment (obfd, padd->section)))
2593f09a 2114 {
2e62b721
NC
2115 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
2116 return FALSE;
252b5132
RH
2117 }
2118 }
2119 }
2120 }
2121
acf1419f
AB
2122 if (update_sections != NULL)
2123 {
2124 struct section_add *pupdate;
2125
2126 for (pupdate = update_sections;
2127 pupdate != NULL;
2128 pupdate = pupdate->next)
2129 {
2130 asection *osec;
2131
2132 pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name);
2133 if (pupdate->section == NULL)
cfad8730
NC
2134 {
2135 non_fatal (_("error: %s not found, can't be updated"), pupdate->name);
2136 return FALSE;
2137 }
acf1419f
AB
2138
2139 osec = pupdate->section->output_section;
2140 if (! bfd_set_section_size (obfd, osec, pupdate->size))
2141 {
2142 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2143 return FALSE;
2144 }
2145 }
2146 }
2147
bbad633b
NC
2148 if (dump_sections != NULL)
2149 {
2150 struct section_add * pdump;
2151
2152 for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
2153 {
2154 asection * sec;
2155
2156 sec = bfd_get_section_by_name (ibfd, pdump->name);
2157 if (sec == NULL)
2158 {
2159 bfd_nonfatal_message (NULL, ibfd, NULL,
2160 _("can't dump section '%s' - it does not exist"),
2161 pdump->name);
2162 continue;
2163 }
2164
2165 if ((bfd_get_section_flags (ibfd, sec) & SEC_HAS_CONTENTS) == 0)
2166 {
2167 bfd_nonfatal_message (NULL, ibfd, sec,
2168 _("can't dump section - it has no contents"));
2169 continue;
2170 }
3aade688 2171
bbad633b
NC
2172 bfd_size_type size = bfd_get_section_size (sec);
2173 if (size == 0)
2174 {
2175 bfd_nonfatal_message (NULL, ibfd, sec,
2176 _("can't dump section - it is empty"));
2177 continue;
2178 }
2179
2180 FILE * f;
2181 f = fopen (pdump->filename, FOPEN_WB);
2182 if (f == NULL)
2183 {
2184 bfd_nonfatal_message (pdump->filename, NULL, NULL,
2185 _("could not open section dump file"));
2186 continue;
2187 }
2188
2189 bfd_byte * contents = xmalloc (size);
2190 if (bfd_get_section_contents (ibfd, sec, contents, 0, size))
182a105a
AG
2191 {
2192 if (fwrite (contents, 1, size, f) != size)
cfad8730
NC
2193 {
2194 non_fatal (_("error writing section contents to %s (error: %s)"),
2195 pdump->filename,
2196 strerror (errno));
2197 return FALSE;
2198 }
182a105a 2199 }
bbad633b
NC
2200 else
2201 bfd_nonfatal_message (NULL, ibfd, sec,
2202 _("could not retrieve section contents"));
2203
2204 fclose (f);
2205 free (contents);
2206 }
2207 }
3aade688 2208
2593f09a
NC
2209 if (gnu_debuglink_filename != NULL)
2210 {
d99b05a3
NC
2211 /* PR 15125: Give a helpful warning message if
2212 the debuglink section already exists, and
2213 allow the rest of the copy to complete. */
2214 if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
950d48e7 2215 {
d99b05a3
NC
2216 non_fatal (_("%s: debuglink section already exists"),
2217 bfd_get_filename (obfd));
2218 gnu_debuglink_filename = NULL;
950d48e7 2219 }
d99b05a3 2220 else
6e2c86ac 2221 {
d99b05a3
NC
2222 gnu_debuglink_section = bfd_create_gnu_debuglink_section
2223 (obfd, gnu_debuglink_filename);
2224
2225 if (gnu_debuglink_section == NULL)
2226 {
2227 bfd_nonfatal_message (NULL, obfd, NULL,
2228 _("cannot create debug link section `%s'"),
2229 gnu_debuglink_filename);
2230 return FALSE;
2231 }
6e2c86ac 2232
d99b05a3
NC
2233 /* Special processing for PE format files. We
2234 have no way to distinguish PE from COFF here. */
2235 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
2236 {
2237 bfd_vma debuglink_vma;
2238 asection * highest_section;
2239 asection * sec;
2240
2241 /* The PE spec requires that all sections be adjacent and sorted
2242 in ascending order of VMA. It also specifies that debug
2243 sections should be last. This is despite the fact that debug
2244 sections are not loaded into memory and so in theory have no
2245 use for a VMA.
2246
2247 This means that the debuglink section must be given a non-zero
2248 VMA which makes it contiguous with other debug sections. So
2249 walk the current section list, find the section with the
2250 highest VMA and start the debuglink section after that one. */
2251 for (sec = obfd->sections, highest_section = NULL;
2252 sec != NULL;
2253 sec = sec->next)
2254 if (sec->vma > 0
2255 && (highest_section == NULL
2256 || sec->vma > highest_section->vma))
2257 highest_section = sec;
2258
2259 if (highest_section)
2260 debuglink_vma = BFD_ALIGN (highest_section->vma
2261 + highest_section->size,
2262 /* FIXME: We ought to be using
2263 COFF_PAGE_SIZE here or maybe
2264 bfd_get_section_alignment() (if it
2265 was set) but since this is for PE
2266 and we know the required alignment
2267 it is easier just to hard code it. */
2268 0x1000);
2269 else
2270 /* Umm, not sure what to do in this case. */
2271 debuglink_vma = 0x1000;
2272
2273 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
2274 }
6e2c86ac 2275 }
950d48e7
NC
2276 }
2277
c68c1637
L
2278 c = bfd_count_sections (obfd);
2279 if (c != 0
1aa9ef63 2280 && (gap_fill_set || pad_to_set))
252b5132
RH
2281 {
2282 asection **set;
252b5132
RH
2283
2284 /* We must fill in gaps between the sections and/or we must pad
2285 the last section to a specified address. We do this by
2286 grabbing a list of the sections, sorting them by VMA, and
2287 increasing the section sizes as required to fill the gaps.
2288 We write out the gap contents below. */
2289
3f5e193b 2290 osections = (asection **) xmalloc (c * sizeof (asection *));
252b5132 2291 set = osections;
d3ba0551 2292 bfd_map_over_sections (obfd, get_sections, &set);
252b5132
RH
2293
2294 qsort (osections, c, sizeof (asection *), compare_section_lma);
2295
3f5e193b 2296 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
252b5132
RH
2297 memset (gaps, 0, c * sizeof (bfd_size_type));
2298
2299 if (gap_fill_set)
2300 {
2301 for (i = 0; i < c - 1; i++)
2302 {
2303 flagword flags;
2304 bfd_size_type size;
2305 bfd_vma gap_start, gap_stop;
2306
2307 flags = bfd_get_section_flags (obfd, osections[i]);
2308 if ((flags & SEC_HAS_CONTENTS) == 0
2309 || (flags & SEC_LOAD) == 0)
2310 continue;
2311
2312 size = bfd_section_size (obfd, osections[i]);
2313 gap_start = bfd_section_lma (obfd, osections[i]) + size;
2314 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
2315 if (gap_start < gap_stop)
2316 {
2317 if (! bfd_set_section_size (obfd, osections[i],
2318 size + (gap_stop - gap_start)))
2319 {
2db6cde7
NS
2320 bfd_nonfatal_message (NULL, obfd, osections[i],
2321 _("Can't fill gap after section"));
252b5132
RH
2322 status = 1;
2323 break;
2324 }
2325 gaps[i] = gap_stop - gap_start;
2326 if (max_gap < gap_stop - gap_start)
2327 max_gap = gap_stop - gap_start;
2328 }
2329 }
2330 }
2331
2332 if (pad_to_set)
2333 {
2334 bfd_vma lma;
2335 bfd_size_type size;
2336
2337 lma = bfd_section_lma (obfd, osections[c - 1]);
2338 size = bfd_section_size (obfd, osections[c - 1]);
2339 if (lma + size < pad_to)
2340 {
2341 if (! bfd_set_section_size (obfd, osections[c - 1],
2342 pad_to - lma))
2343 {
2db6cde7
NS
2344 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
2345 _("can't add padding"));
252b5132
RH
2346 status = 1;
2347 }
2348 else
2349 {
2350 gaps[c - 1] = pad_to - (lma + size);
2351 if (max_gap < pad_to - (lma + size))
2352 max_gap = pad_to - (lma + size);
2353 }
2354 }
2355 }
2356 }
2357
594ef5db
NC
2358 /* Symbol filtering must happen after the output sections
2359 have been created, but before their contents are set. */
252b5132 2360 dhandle = NULL;
252b5132 2361 if (convert_debugging)
b922d590 2362 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
57938635
AM
2363
2364 if (strip_symbols == STRIP_DEBUG
252b5132
RH
2365 || strip_symbols == STRIP_ALL
2366 || strip_symbols == STRIP_UNNEEDED
ed1653a7 2367 || strip_symbols == STRIP_NONDEBUG
96109726
CC
2368 || strip_symbols == STRIP_DWO
2369 || strip_symbols == STRIP_NONDWO
252b5132 2370 || discard_locals != LOCALS_UNDEF
d58c2e3a 2371 || localize_hidden
047c9024
NC
2372 || htab_elements (strip_specific_htab) != 0
2373 || htab_elements (keep_specific_htab) != 0
2374 || htab_elements (localize_specific_htab) != 0
2375 || htab_elements (globalize_specific_htab) != 0
2376 || htab_elements (keepglobal_specific_htab) != 0
2377 || htab_elements (weaken_specific_htab) != 0
d7fb0dd2 2378 || prefix_symbols_string
252b5132 2379 || sections_removed
f91ea849 2380 || sections_copied
252b5132
RH
2381 || convert_debugging
2382 || change_leading_char
2383 || remove_leading_char
57938635 2384 || redefine_sym_list
9cc0123f 2385 || section_rename_list
2b35fb28
RH
2386 || weaken
2387 || add_symbols)
252b5132
RH
2388 {
2389 /* Mark symbols used in output relocations so that they
2390 are kept, even if they are local labels or static symbols.
57938635 2391
252b5132
RH
2392 Note we iterate over the input sections examining their
2393 relocations since the relocations for the output sections
2394 haven't been set yet. mark_symbols_used_in_relocations will
2395 ignore input sections which have no corresponding output
2396 section. */
2397 if (strip_symbols != STRIP_ALL)
2398 bfd_map_over_sections (ibfd,
2399 mark_symbols_used_in_relocations,
d3ba0551 2400 isympp);
2b35fb28 2401 osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *));
252b5132
RH
2402 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
2403 }
2404
2405 if (convert_debugging && dhandle != NULL)
2406 {
2407 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
2408 {
2409 status = 1;
950d48e7 2410 return FALSE;
252b5132
RH
2411 }
2412 }
2413
2414 bfd_set_symtab (obfd, osympp, symcount);
2415
c3989150
L
2416 /* This has to happen before section positions are set. */
2417 bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
2418
252b5132 2419 /* This has to happen after the symbol table has been set. */
d3ba0551 2420 bfd_map_over_sections (ibfd, copy_section, obfd);
252b5132
RH
2421
2422 if (add_sections != NULL)
2423 {
2424 struct section_add *padd;
2425
2426 for (padd = add_sections; padd != NULL; padd = padd->next)
2427 {
d3ba0551
AM
2428 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
2429 0, padd->size))
950d48e7 2430 {
2db6cde7 2431 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
950d48e7
NC
2432 return FALSE;
2433 }
252b5132
RH
2434 }
2435 }
2436
acf1419f
AB
2437 if (update_sections != NULL)
2438 {
2439 struct section_add *pupdate;
2440
2441 for (pupdate = update_sections;
f7433f01
AM
2442 pupdate != NULL;
2443 pupdate = pupdate->next)
acf1419f
AB
2444 {
2445 asection *osec;
2446
2447 osec = pupdate->section->output_section;
2448 if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
f7433f01 2449 0, pupdate->size))
acf1419f
AB
2450 {
2451 bfd_nonfatal_message (NULL, obfd, osec, NULL);
2452 return FALSE;
2453 }
2454 }
2455 }
2456
e7c81c25
NC
2457 if (gnu_debuglink_filename != NULL)
2458 {
2459 if (! bfd_fill_in_gnu_debuglink_section
2460 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
950d48e7 2461 {
2db6cde7
NS
2462 bfd_nonfatal_message (NULL, obfd, NULL,
2463 _("cannot fill debug link section `%s'"),
2464 gnu_debuglink_filename);
950d48e7
NC
2465 return FALSE;
2466 }
e7c81c25
NC
2467 }
2468
252b5132
RH
2469 if (gap_fill_set || pad_to_set)
2470 {
2471 bfd_byte *buf;
252b5132
RH
2472
2473 /* Fill in the gaps. */
252b5132
RH
2474 if (max_gap > 8192)
2475 max_gap = 8192;
3f5e193b 2476 buf = (bfd_byte *) xmalloc (max_gap);
d3ba0551 2477 memset (buf, gap_fill, max_gap);
252b5132
RH
2478
2479 c = bfd_count_sections (obfd);
2480 for (i = 0; i < c; i++)
2481 {
2482 if (gaps[i] != 0)
2483 {
2484 bfd_size_type left;
2485 file_ptr off;
2486
2487 left = gaps[i];
2488 off = bfd_section_size (obfd, osections[i]) - left;
594ef5db 2489
252b5132
RH
2490 while (left > 0)
2491 {
2492 bfd_size_type now;
2493
2494 if (left > 8192)
2495 now = 8192;
2496 else
2497 now = left;
2498
2499 if (! bfd_set_section_contents (obfd, osections[i], buf,
2500 off, now))
950d48e7 2501 {
2db6cde7 2502 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
950d48e7
NC
2503 return FALSE;
2504 }
252b5132
RH
2505
2506 left -= now;
2507 off += now;
2508 }
2509 }
2510 }
2511 }
2512
2513 /* Allow the BFD backend to copy any private data it understands
2514 from the input BFD to the output BFD. This is done last to
2515 permit the routine to look at the filtered symbol table, which is
2516 important for the ECOFF code at least. */
42bb2e33 2517 if (! bfd_copy_private_bfd_data (ibfd, obfd))
252b5132 2518 {
2db6cde7
NS
2519 bfd_nonfatal_message (NULL, obfd, NULL,
2520 _("error copying private BFD data"));
950d48e7 2521 return FALSE;
252b5132 2522 }
1ae8b3d2
AO
2523
2524 /* Switch to the alternate machine code. We have to do this at the
2525 very end, because we only initialize the header when we create
2526 the first section. */
f9d4ad2a
NC
2527 if (use_alt_mach_code != 0)
2528 {
2529 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
2530 {
2531 non_fatal (_("this target does not support %lu alternative machine codes"),
2532 use_alt_mach_code);
2533 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2534 {
2535 non_fatal (_("treating that number as an absolute e_machine value instead"));
2536 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
2537 }
2538 else
2539 non_fatal (_("ignoring the alternative value"));
2540 }
2541 }
950d48e7
NC
2542
2543 return TRUE;
252b5132
RH
2544}
2545
2546/* Read each archive element in turn from IBFD, copy the
ee873e00
NC
2547 contents to temp file, and keep the temp file handle.
2548 If 'force_output_target' is TRUE then make sure that
2549 all elements in the new archive are of the type
2550 'output_target'. */
252b5132
RH
2551
2552static void
ee873e00 2553copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
8b31b6c4
NC
2554 bfd_boolean force_output_target,
2555 const bfd_arch_info_type *input_arch)
252b5132
RH
2556{
2557 struct name_list
2558 {
2559 struct name_list *next;
4c168fa3 2560 const char *name;
252b5132
RH
2561 bfd *obfd;
2562 } *list, *l;
2563 bfd **ptr = &obfd->archive_head;
2564 bfd *this_element;
8d8e0703
AM
2565 char *dir;
2566 const char *filename;
252b5132
RH
2567
2568 /* Make a temp directory to hold the contents. */
f9c026a8 2569 dir = make_tempdir (bfd_get_filename (obfd));
f9c026a8 2570 if (dir == NULL)
f7433f01 2571 fatal (_("cannot create tempdir for archive copying (error: %s)"),
f9c026a8 2572 strerror (errno));
84e2f313 2573
2e30cb57
CC
2574 if (strip_symbols == STRIP_ALL)
2575 obfd->has_armap = FALSE;
2576 else
2577 obfd->has_armap = ibfd->has_armap;
a8da6403 2578 obfd->is_thin_archive = ibfd->is_thin_archive;
252b5132 2579
2e30cb57
CC
2580 if (deterministic)
2581 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2582
252b5132
RH
2583 list = NULL;
2584
2585 this_element = bfd_openr_next_archived_file (ibfd, NULL);
594ef5db 2586
b667df2e 2587 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
8d8e0703
AM
2588 {
2589 status = 1;
2590 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
cfad8730 2591 goto cleanup_and_exit;
8d8e0703 2592 }
b667df2e 2593
d3ba0551 2594 while (!status && this_element != NULL)
252b5132 2595 {
4c168fa3
AM
2596 char *output_name;
2597 bfd *output_bfd;
252b5132 2598 bfd *last_element;
8066d1a2
AS
2599 struct stat buf;
2600 int stat_status = 0;
3f5e193b 2601 bfd_boolean del = TRUE;
19094d10 2602 bfd_boolean ok_object;
8066d1a2 2603
dd9b91de
NC
2604 /* PR binutils/17533: Do not allow directory traversal
2605 outside of the current directory tree by archive members. */
2606 if (! is_valid_archive_path (bfd_get_filename (this_element)))
5e186ece
NC
2607 {
2608 non_fatal (_("illegal pathname found in archive member: %s"),
2609 bfd_get_filename (this_element));
2610 status = 1;
2611 goto cleanup_and_exit;
2612 }
dd9b91de 2613
4c168fa3
AM
2614 /* Create an output file for this member. */
2615 output_name = concat (dir, "/",
2616 bfd_get_filename (this_element), (char *) 0);
2617
2618 /* If the file already exists, make another temp dir. */
2619 if (stat (output_name, &buf) >= 0)
2620 {
f9c026a8
NC
2621 output_name = make_tempdir (output_name);
2622 if (output_name == NULL)
5e186ece
NC
2623 {
2624 non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
2625 strerror (errno));
2626 status = 1;
2627 goto cleanup_and_exit;
2628 }
84e2f313 2629
3f5e193b 2630 l = (struct name_list *) xmalloc (sizeof (struct name_list));
4c168fa3
AM
2631 l->name = output_name;
2632 l->next = list;
2633 l->obfd = NULL;
2634 list = l;
2635 output_name = concat (output_name, "/",
2636 bfd_get_filename (this_element), (char *) 0);
2637 }
2638
8066d1a2
AS
2639 if (preserve_dates)
2640 {
2641 stat_status = bfd_stat_arch_elt (this_element, &buf);
594ef5db 2642
8066d1a2
AS
2643 if (stat_status != 0)
2644 non_fatal (_("internal stat error on %s"),
2645 bfd_get_filename (this_element));
2646 }
252b5132 2647
3f5e193b 2648 l = (struct name_list *) xmalloc (sizeof (struct name_list));
252b5132
RH
2649 l->name = output_name;
2650 l->next = list;
bee59fd2 2651 l->obfd = NULL;
252b5132
RH
2652 list = l;
2653
19094d10
AM
2654 ok_object = bfd_check_format (this_element, bfd_object);
2655 if (!ok_object)
2656 bfd_nonfatal_message (NULL, this_element, NULL,
2657 _("Unable to recognise the format of file"));
2658
2659 /* PR binutils/3110: Cope with archives
2660 containing multiple target types. */
2661 if (force_output_target || !ok_object)
2662 output_bfd = bfd_openw (output_name, output_target);
2663 else
2664 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
2665
2666 if (output_bfd == NULL)
77f762d6 2667 {
19094d10
AM
2668 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2669 status = 1;
5e186ece 2670 goto cleanup_and_exit;
19094d10
AM
2671 }
2672
2673 if (ok_object)
2674 {
2675 del = !copy_object (this_element, output_bfd, input_arch);
ee873e00 2676
19094d10
AM
2677 if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
2678 /* Try again as an unknown object file. */
2679 ok_object = FALSE;
2680 else if (!bfd_close (output_bfd))
2db6cde7
NS
2681 {
2682 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
19094d10 2683 /* Error in new object file. Don't change archive. */
2db6cde7 2684 status = 1;
77f762d6 2685 }
77f762d6 2686 }
77f762d6 2687
19094d10
AM
2688 if (!ok_object)
2689 {
3f5e193b 2690 del = !copy_unknown_object (this_element, output_bfd);
77f762d6
L
2691 if (!bfd_close_all_done (output_bfd))
2692 {
8d8e0703 2693 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
77f762d6
L
2694 /* Error in new object file. Don't change archive. */
2695 status = 1;
2696 }
252b5132
RH
2697 }
2698
3f5e193b 2699 if (del)
950d48e7
NC
2700 {
2701 unlink (output_name);
2702 status = 1;
2703 }
2704 else
2705 {
2706 if (preserve_dates && stat_status == 0)
2707 set_times (output_name, &buf);
8066d1a2 2708
950d48e7
NC
2709 /* Open the newly output file and attach to our list. */
2710 output_bfd = bfd_openr (output_name, output_target);
252b5132 2711
950d48e7 2712 l->obfd = output_bfd;
252b5132 2713
950d48e7 2714 *ptr = output_bfd;
cc481421 2715 ptr = &output_bfd->archive_next;
252b5132 2716
950d48e7 2717 last_element = this_element;
252b5132 2718
950d48e7 2719 this_element = bfd_openr_next_archived_file (ibfd, last_element);
252b5132 2720
950d48e7
NC
2721 bfd_close (last_element);
2722 }
252b5132 2723 }
d3ba0551 2724 *ptr = NULL;
252b5132 2725
8d8e0703 2726 filename = bfd_get_filename (obfd);
252b5132 2727 if (!bfd_close (obfd))
8d8e0703
AM
2728 {
2729 status = 1;
2730 bfd_nonfatal_message (filename, NULL, NULL, NULL);
8d8e0703 2731 }
252b5132 2732
8d8e0703 2733 filename = bfd_get_filename (ibfd);
252b5132 2734 if (!bfd_close (ibfd))
8d8e0703
AM
2735 {
2736 status = 1;
2737 bfd_nonfatal_message (filename, NULL, NULL, NULL);
8d8e0703 2738 }
252b5132 2739
5e186ece 2740 cleanup_and_exit:
252b5132
RH
2741 /* Delete all the files that we opened. */
2742 for (l = list; l != NULL; l = l->next)
2743 {
4c168fa3
AM
2744 if (l->obfd == NULL)
2745 rmdir (l->name);
2746 else
2747 {
2748 bfd_close (l->obfd);
2749 unlink (l->name);
2750 }
252b5132 2751 }
cfad8730 2752
252b5132
RH
2753 rmdir (dir);
2754}
2755
0408dee6
DK
2756static void
2757set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
2758{
2759 /* This is only relevant to Coff targets. */
2760 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
2761 {
78e82dc3
AM
2762 if (style == KEEP
2763 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
0408dee6
DK
2764 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
2765 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
2766 }
2767}
2768
252b5132
RH
2769/* The top-level control. */
2770
2771static void
84e2f313 2772copy_file (const char *input_filename, const char *output_filename,
8b31b6c4
NC
2773 const char *input_target, const char *output_target,
2774 const bfd_arch_info_type *input_arch)
252b5132
RH
2775{
2776 bfd *ibfd;
49c12576
AM
2777 char **obj_matching;
2778 char **core_matching;
52a476ee 2779 off_t size = get_file_size (input_filename);
252b5132 2780
52a476ee 2781 if (size < 1)
f24ddbdd 2782 {
52a476ee
L
2783 if (size == 0)
2784 non_fatal (_("error: the input file '%s' is empty"),
2785 input_filename);
f24ddbdd
NC
2786 status = 1;
2787 return;
2788 }
2789
252b5132
RH
2790 /* To allow us to do "strip *" without dying on the first
2791 non-object file, failures are nonfatal. */
252b5132
RH
2792 ibfd = bfd_openr (input_filename, input_target);
2793 if (ibfd == NULL)
2db6cde7
NS
2794 {
2795 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2796 status = 1;
2797 return;
2798 }
252b5132 2799
4a114e3e
L
2800 switch (do_debug_sections)
2801 {
2802 case compress:
151411f8
L
2803 case compress_zlib:
2804 case compress_gnu_zlib:
2805 case compress_gabi_zlib:
4a114e3e 2806 ibfd->flags |= BFD_COMPRESS;
cd6faa73
L
2807 /* Don't check if input is ELF here since this information is
2808 only available after bfd_check_format_matches is called. */
19a7fe52 2809 if (do_debug_sections != compress_gnu_zlib)
cd6faa73 2810 ibfd->flags |= BFD_COMPRESS_GABI;
4a114e3e
L
2811 break;
2812 case decompress:
2813 ibfd->flags |= BFD_DECOMPRESS;
2814 break;
2815 default:
2816 break;
2817 }
2818
b8871f35
L
2819 switch (do_elf_stt_common)
2820 {
2821 case elf_stt_common:
2822 ibfd->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
2823 break;
2824 break;
2825 case no_elf_stt_common:
2826 ibfd->flags |= BFD_CONVERT_ELF_COMMON;
2827 break;
2828 default:
2829 break;
2830 }
2831
252b5132
RH
2832 if (bfd_check_format (ibfd, bfd_archive))
2833 {
ee873e00 2834 bfd_boolean force_output_target;
252b5132
RH
2835 bfd *obfd;
2836
2837 /* bfd_get_target does not return the correct value until
f7433f01 2838 bfd_check_format succeeds. */
252b5132 2839 if (output_target == NULL)
ee873e00
NC
2840 {
2841 output_target = bfd_get_target (ibfd);
2842 force_output_target = FALSE;
2843 }
2844 else
2845 force_output_target = TRUE;
252b5132
RH
2846
2847 obfd = bfd_openw (output_filename, output_target);
2848 if (obfd == NULL)
2db6cde7
NS
2849 {
2850 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2851 status = 1;
2852 return;
2853 }
0408dee6
DK
2854 /* This is a no-op on non-Coff targets. */
2855 set_long_section_mode (obfd, ibfd, long_section_names);
252b5132 2856
8b31b6c4 2857 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
252b5132 2858 }
49c12576 2859 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
252b5132
RH
2860 {
2861 bfd *obfd;
49c12576 2862 do_copy:
950d48e7 2863
252b5132 2864 /* bfd_get_target does not return the correct value until
f7433f01 2865 bfd_check_format succeeds. */
252b5132
RH
2866 if (output_target == NULL)
2867 output_target = bfd_get_target (ibfd);
2868
2869 obfd = bfd_openw (output_filename, output_target);
2870 if (obfd == NULL)
2db6cde7
NS
2871 {
2872 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2873 status = 1;
2874 return;
2875 }
0408dee6
DK
2876 /* This is a no-op on non-Coff targets. */
2877 set_long_section_mode (obfd, ibfd, long_section_names);
252b5132 2878
8b31b6c4 2879 if (! copy_object (ibfd, obfd, input_arch))
a580b8e0 2880 status = 1;
252b5132 2881
063bb025
NC
2882 /* PR 17512: file: 0f15796a.
2883 If the file could not be copied it may not be in a writeable
2884 state. So use bfd_close_all_done to avoid the possibility of
2885 writing uninitialised data into the file. */
2886 if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd)))
8d8e0703
AM
2887 {
2888 status = 1;
2889 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2890 return;
2891 }
252b5132
RH
2892
2893 if (!bfd_close (ibfd))
8d8e0703
AM
2894 {
2895 status = 1;
2896 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2897 return;
2898 }
252b5132
RH
2899 }
2900 else
2901 {
49c12576
AM
2902 bfd_error_type obj_error = bfd_get_error ();
2903 bfd_error_type core_error;
b34976b6 2904
49c12576
AM
2905 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2906 {
2907 /* This probably can't happen.. */
2908 if (obj_error == bfd_error_file_ambiguously_recognized)
2909 free (obj_matching);
2910 goto do_copy;
2911 }
2912
2913 core_error = bfd_get_error ();
2914 /* Report the object error in preference to the core error. */
2915 if (obj_error != core_error)
2916 bfd_set_error (obj_error);
2917
2db6cde7 2918 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
57938635 2919
49c12576
AM
2920 if (obj_error == bfd_error_file_ambiguously_recognized)
2921 {
2922 list_matching_formats (obj_matching);
2923 free (obj_matching);
2924 }
2925 if (core_error == bfd_error_file_ambiguously_recognized)
252b5132 2926 {
49c12576
AM
2927 list_matching_formats (core_matching);
2928 free (core_matching);
252b5132 2929 }
57938635 2930
252b5132
RH
2931 status = 1;
2932 }
2933}
2934
594ef5db
NC
2935/* Add a name to the section renaming list. */
2936
2937static void
84e2f313
NC
2938add_section_rename (const char * old_name, const char * new_name,
2939 flagword flags)
594ef5db 2940{
91d6fa6a 2941 section_rename * srename;
594ef5db
NC
2942
2943 /* Check for conflicts first. */
91d6fa6a
NC
2944 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2945 if (strcmp (srename->old_name, old_name) == 0)
594ef5db
NC
2946 {
2947 /* Silently ignore duplicate definitions. */
91d6fa6a
NC
2948 if (strcmp (srename->new_name, new_name) == 0
2949 && srename->flags == flags)
594ef5db 2950 return;
0af11b59 2951
594ef5db
NC
2952 fatal (_("Multiple renames of section %s"), old_name);
2953 }
2954
91d6fa6a 2955 srename = (section_rename *) xmalloc (sizeof (* srename));
594ef5db 2956
91d6fa6a
NC
2957 srename->old_name = old_name;
2958 srename->new_name = new_name;
2959 srename->flags = flags;
2960 srename->next = section_rename_list;
0af11b59 2961
91d6fa6a 2962 section_rename_list = srename;
594ef5db
NC
2963}
2964
2965/* Check the section rename list for a new name of the input section
9cc0123f
AM
2966 called OLD_NAME. Returns the new name if one is found and sets
2967 RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
594ef5db
NC
2968
2969static const char *
9cc0123f 2970find_section_rename (const char *old_name, flagword *returned_flags)
594ef5db 2971{
9cc0123f 2972 const section_rename *srename;
594ef5db 2973
91d6fa6a
NC
2974 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2975 if (strcmp (srename->old_name, old_name) == 0)
594ef5db 2976 {
9cc0123f
AM
2977 if (returned_flags != NULL && srename->flags != (flagword) -1)
2978 *returned_flags = srename->flags;
594ef5db 2979
91d6fa6a 2980 return srename->new_name;
594ef5db
NC
2981 }
2982
2983 return old_name;
2984}
2985
80fccad2
BW
2986/* Once each of the sections is copied, we may still need to do some
2987 finalization work for private section headers. Do that here. */
2988
2989static void
2990setup_bfd_headers (bfd *ibfd, bfd *obfd)
2991{
80fccad2
BW
2992 /* Allow the BFD backend to copy any private data it understands
2993 from the input section to the output section. */
2994 if (! bfd_copy_private_header_data (ibfd, obfd))
2995 {
2db6cde7
NS
2996 status = 1;
2997 bfd_nonfatal_message (NULL, ibfd, NULL,
8d8e0703 2998 _("error in private header data"));
2db6cde7 2999 return;
80fccad2
BW
3000 }
3001
3002 /* All went well. */
3003 return;
80fccad2
BW
3004}
3005
594ef5db
NC
3006/* Create a section in OBFD with the same
3007 name and attributes as ISECTION in IBFD. */
252b5132
RH
3008
3009static void
84e2f313 3010setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
252b5132 3011{
3f5e193b 3012 bfd *obfd = (bfd *) obfdarg;
252b5132
RH
3013 struct section_list *p;
3014 sec_ptr osection;
3015 bfd_size_type size;
3016 bfd_vma vma;
3017 bfd_vma lma;
3018 flagword flags;
1a89cc7d 3019 const char *err;
594ef5db 3020 const char * name;
d7fb0dd2 3021 char *prefix = NULL;
66125551 3022 bfd_boolean make_nobits;
0af11b59 3023
2593f09a 3024 if (is_strip_section (ibfd, isection))
252b5132
RH
3025 return;
3026
594ef5db 3027 /* Get the, possibly new, name of the output section. */
9cc0123f
AM
3028 name = bfd_section_name (ibfd, isection);
3029 flags = bfd_get_section_flags (ibfd, isection);
3030 name = find_section_rename (name, &flags);
0af11b59 3031
d7fb0dd2 3032 /* Prefix sections. */
84e2f313
NC
3033 if ((prefix_alloc_sections_string)
3034 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
d7fb0dd2
NC
3035 prefix = prefix_alloc_sections_string;
3036 else if (prefix_sections_string)
3037 prefix = prefix_sections_string;
3038
3039 if (prefix)
3040 {
3041 char *n;
3042
3f5e193b 3043 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
d7fb0dd2
NC
3044 strcpy (n, prefix);
3045 strcat (n, name);
3046 name = n;
3047 }
66491ebc 3048
66125551 3049 make_nobits = FALSE;
2e62b721
NC
3050
3051 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3052 SECTION_CONTEXT_SET_FLAGS);
3053 if (p != NULL)
551b43fd 3054 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
42bb2e33 3055 else if (strip_symbols == STRIP_NONDEBUG
6c67a030 3056 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
f7433f01 3057 && !is_nondebug_keep_contents_section (ibfd, isection))
66125551 3058 {
6c67a030 3059 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
66125551
AM
3060 if (obfd->xvec->flavour == bfd_target_elf_flavour)
3061 {
3062 make_nobits = TRUE;
3063
3064 /* Twiddle the input section flags so that it seems to
3065 elf.c:copy_private_bfd_data that section flags have not
3066 changed between input and output sections. This hack
3067 prevents wholesale rewriting of the program headers. */
6c67a030 3068 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
66125551
AM
3069 }
3070 }
551b43fd
AM
3071
3072 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
57938635 3073
252b5132
RH
3074 if (osection == NULL)
3075 {
2db6cde7 3076 err = _("failed to create output section");
252b5132
RH
3077 goto loser;
3078 }
3079
66125551 3080 if (make_nobits)
551b43fd
AM
3081 elf_section_type (osection) = SHT_NOBITS;
3082
252b5132 3083 size = bfd_section_size (ibfd, isection);
88988473 3084 size = bfd_convert_section_size (ibfd, isection, obfd, size);
252b5132 3085 if (copy_byte >= 0)
b7dd81f7 3086 size = (size + interleave - 1) / interleave * copy_width;
d3e52d40
RS
3087 else if (extract_symbol)
3088 size = 0;
252b5132
RH
3089 if (! bfd_set_section_size (obfd, osection, size))
3090 {
2db6cde7 3091 err = _("failed to set size");
252b5132
RH
3092 goto loser;
3093 }
57938635 3094
252b5132 3095 vma = bfd_section_vma (ibfd, isection);
2e62b721
NC
3096 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3097 SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
3098 if (p != NULL)
3099 {
3100 if (p->context & SECTION_CONTEXT_SET_VMA)
3101 vma = p->vma_val;
3102 else
3103 vma += p->vma_val;
3104 }
252b5132
RH
3105 else
3106 vma += change_section_address;
57938635 3107
237dcb53 3108 if (! bfd_set_section_vma (obfd, osection, vma))
252b5132 3109 {
2db6cde7 3110 err = _("failed to set vma");
252b5132
RH
3111 goto loser;
3112 }
3113
3114 lma = isection->lma;
2e62b721
NC
3115 p = find_section_list (bfd_section_name (ibfd, isection), FALSE,
3116 SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
3117 if (p != NULL)
252b5132 3118 {
2e62b721 3119 if (p->context & SECTION_CONTEXT_ALTER_LMA)
252b5132 3120 lma += p->lma_val;
252b5132 3121 else
2e62b721 3122 lma = p->lma_val;
252b5132
RH
3123 }
3124 else
3125 lma += change_section_address;
57938635 3126
237dcb53 3127 osection->lma = lma;
252b5132
RH
3128
3129 /* FIXME: This is probably not enough. If we change the LMA we
3130 may have to recompute the header for the file as well. */
b34976b6
AM
3131 if (!bfd_set_section_alignment (obfd,
3132 osection,
3133 bfd_section_alignment (ibfd, isection)))
252b5132 3134 {
2db6cde7 3135 err = _("failed to set alignment");
252b5132
RH
3136 goto loser;
3137 }
3138
bc408b8a
JJ
3139 /* Copy merge entity size. */
3140 osection->entsize = isection->entsize;
3141
f6fe1ccd
L
3142 /* Copy compress status. */
3143 osection->compress_status = isection->compress_status;
3144
252b5132
RH
3145 /* This used to be mangle_section; we do here to avoid using
3146 bfd_get_section_by_name since some formats allow multiple
3147 sections with the same name. */
3148 isection->output_section = osection;
237dcb53 3149 isection->output_offset = 0;
d3e52d40 3150
119f4245
AM
3151 if ((isection->flags & SEC_GROUP) != 0)
3152 {
3153 asymbol *gsym = group_signature (isection);
3154
3155 if (gsym != NULL)
3156 {
3157 gsym->flags |= BSF_KEEP;
3158 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
3159 elf_group_id (isection) = gsym;
3160 }
3161 }
3162
252b5132
RH
3163 /* Allow the BFD backend to copy any private data it understands
3164 from the input section to the output section. */
42bb2e33 3165 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
252b5132 3166 {
2db6cde7 3167 err = _("failed to copy private data");
252b5132
RH
3168 goto loser;
3169 }
3170
594ef5db 3171 /* All went well. */
252b5132
RH
3172 return;
3173
f7433f01 3174 loser:
252b5132 3175 status = 1;
2db6cde7 3176 bfd_nonfatal_message (NULL, obfd, osection, err);
252b5132
RH
3177}
3178
c3989150 3179/* Return TRUE if input section ISECTION should be skipped. */
252b5132 3180
c3989150
L
3181static bfd_boolean
3182skip_section (bfd *ibfd, sec_ptr isection)
252b5132 3183{
252b5132
RH
3184 sec_ptr osection;
3185 bfd_size_type size;
dc156bc0 3186 flagword flags;
252b5132 3187
594ef5db
NC
3188 /* If we have already failed earlier on,
3189 do not keep on generating complaints now. */
252b5132 3190 if (status != 0)
c3989150
L
3191 return TRUE;
3192
3193 if (extract_symbol)
3194 return TRUE;
57938635 3195
2593f09a 3196 if (is_strip_section (ibfd, isection))
c3989150 3197 return TRUE;
252b5132 3198
acf1419f
AB
3199 if (is_update_section (ibfd, isection))
3200 return TRUE;
3201
2593f09a 3202 flags = bfd_get_section_flags (ibfd, isection);
dc156bc0 3203 if ((flags & SEC_GROUP) != 0)
c3989150 3204 return TRUE;
dc156bc0 3205
252b5132 3206 osection = isection->output_section;
135dfb4a 3207 size = bfd_get_section_size (isection);
252b5132
RH
3208
3209 if (size == 0 || osection == 0)
c3989150 3210 return TRUE;
252b5132 3211
c3989150
L
3212 return FALSE;
3213}
3214
d3e5f6c8
AB
3215/* Add section SECTION_PATTERN to the list of sections that will have their
3216 relocations removed. */
3217
3218static void
3219handle_remove_relocations_option (const char *section_pattern)
3220{
3221 find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE_RELOCS);
3222}
3223
3224/* Return TRUE if ISECTION from IBFD should have its relocations removed,
3225 otherwise return FALSE. If the user has requested that relocations be
3226 removed from a section that does not have relocations then this
3227 function will still return TRUE. */
3228
3229static bfd_boolean
3230discard_relocations (bfd *ibfd ATTRIBUTE_UNUSED, asection *isection)
3231{
3232 return (find_section_list (bfd_section_name (ibfd, isection), FALSE,
3233 SECTION_CONTEXT_REMOVE_RELOCS) != NULL);
3234}
3235
3236/* Wrapper for dealing with --remove-section (-R) command line arguments.
3237 A special case is detected here, if the user asks to remove a relocation
3238 section (one starting with ".rela." or ".rel.") then this removal must
3239 be done using a different technique. */
3240
3241static void
3242handle_remove_section_option (const char *section_pattern)
3243{
3244 if (strncmp (section_pattern, ".rela.", 6) == 0)
3245 handle_remove_relocations_option (section_pattern + 5);
3246 else if (strncmp (section_pattern, ".rel.", 5) == 0)
3247 handle_remove_relocations_option (section_pattern + 4);
3248 else
3249 {
3250 find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE);
3251 sections_removed = TRUE;
3252 }
3253}
3254
c3989150
L
3255/* Copy relocations in input section ISECTION of IBFD to an output
3256 section with the same name in OBFDARG. If stripping then don't
3257 copy any relocation info. */
3258
3259static void
3260copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3261{
3262 bfd *obfd = (bfd *) obfdarg;
3263 long relsize;
3264 arelent **relpp;
3265 long relcount;
3266 sec_ptr osection;
3267
3268 if (skip_section (ibfd, isection))
237dcb53
AM
3269 return;
3270
c3989150 3271 osection = isection->output_section;
2593f09a 3272
96109726 3273 /* Core files and DWO files do not need to be relocated. */
d3e5f6c8
AB
3274 if (bfd_get_format (obfd) == bfd_core
3275 || strip_symbols == STRIP_NONDWO
3276 || discard_relocations (ibfd, isection))
4dd67f29
MS
3277 relsize = 0;
3278 else
ed570f48
NC
3279 {
3280 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
4dd67f29 3281
ed570f48
NC
3282 if (relsize < 0)
3283 {
3284 /* Do not complain if the target does not support relocations. */
3285 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3286 relsize = 0;
3287 else
2db6cde7
NS
3288 {
3289 status = 1;
3290 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
3291 return;
3292 }
ed570f48
NC
3293 }
3294 }
57938635 3295
252b5132 3296 if (relsize == 0)
96109726
CC
3297 {
3298 bfd_set_reloc (obfd, osection, NULL, 0);
3299 osection->flags &= ~SEC_RELOC;
3300 }
252b5132
RH
3301 else
3302 {
3f5e193b 3303 relpp = (arelent **) xmalloc (relsize);
252b5132
RH
3304 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
3305 if (relcount < 0)
2db6cde7
NS
3306 {
3307 status = 1;
3308 bfd_nonfatal_message (NULL, ibfd, isection,
3309 _("relocation count is negative"));
3310 return;
3311 }
57938635 3312
252b5132
RH
3313 if (strip_symbols == STRIP_ALL)
3314 {
3315 /* Remove relocations which are not in
0af11b59 3316 keep_strip_specific_list. */
252b5132
RH
3317 arelent **temp_relpp;
3318 long temp_relcount = 0;
3319 long i;
57938635 3320
3f5e193b 3321 temp_relpp = (arelent **) xmalloc (relsize);
252b5132 3322 for (i = 0; i < relcount; i++)
86eafac0
NC
3323 {
3324 /* PR 17512: file: 9e907e0c. */
f507bebf
NC
3325 if (relpp[i]->sym_ptr_ptr
3326 /* PR 20096 */
3327 && * relpp[i]->sym_ptr_ptr)
86eafac0
NC
3328 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
3329 keep_specific_htab))
3330 temp_relpp [temp_relcount++] = relpp [i];
3331 }
252b5132
RH
3332 relcount = temp_relcount;
3333 free (relpp);
3334 relpp = temp_relpp;
3335 }
e0c60db2 3336
d3ba0551 3337 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
f0312d39 3338 if (relcount == 0)
c3989150
L
3339 {
3340 osection->flags &= ~SEC_RELOC;
3341 free (relpp);
3342 }
252b5132 3343 }
c3989150
L
3344}
3345
3346/* Copy the data of input section ISECTION of IBFD
3347 to an output section with the same name in OBFD. */
3348
3349static void
3350copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
3351{
3352 bfd *obfd = (bfd *) obfdarg;
3353 struct section_list *p;
3354 sec_ptr osection;
3355 bfd_size_type size;
3356
3357 if (skip_section (ibfd, isection))
3358 return;
3359
3360 osection = isection->output_section;
88988473 3361 /* The output SHF_COMPRESSED section size is different from input if
cbd44e24
L
3362 ELF classes of input and output aren't the same. We can't use
3363 the output section size since --interleave will shrink the output
3364 section. Size will be updated if the section is converted. */
3365 size = bfd_get_section_size (isection);
c3989150 3366
0af11b59 3367 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
4dd67f29 3368 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
252b5132 3369 {
4a114e3e 3370 bfd_byte *memhunk = NULL;
252b5132 3371
88988473
L
3372 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)
3373 || !bfd_convert_section_contents (ibfd, isection, obfd,
cbd44e24 3374 &memhunk, &size))
2db6cde7
NS
3375 {
3376 status = 1;
3377 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
848ac659 3378 free (memhunk);
2db6cde7
NS
3379 return;
3380 }
252b5132 3381
9e48b4c6
NC
3382 if (reverse_bytes)
3383 {
3384 /* We don't handle leftover bytes (too many possible behaviors,
3385 and we don't know what the user wants). The section length
3386 must be a multiple of the number of bytes to swap. */
3387 if ((size % reverse_bytes) == 0)
3388 {
3389 unsigned long i, j;
3390 bfd_byte b;
3391
3392 for (i = 0; i < size; i += reverse_bytes)
3393 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
3394 {
3395 bfd_byte *m = (bfd_byte *) memhunk;
3396
3397 b = m[i + j];
3398 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
3399 m[(i + reverse_bytes) - (j + 1)] = b;
3400 }
3401 }
3402 else
3403 /* User must pad the section up in order to do this. */
3404 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
3405 bfd_section_name (ibfd, isection), reverse_bytes);
3406 }
3407
57938635 3408 if (copy_byte >= 0)
5e675b72
AM
3409 {
3410 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2f01ffbf 3411 char *from = (char *) memhunk + copy_byte;
3f5e193b 3412 char *to = (char *) memhunk;
2f01ffbf 3413 char *end = (char *) memhunk + size;
b7dd81f7 3414 int i;
5e675b72
AM
3415
3416 for (; from < end; from += interleave)
b7dd81f7 3417 for (i = 0; i < copy_width; i++)
ee7da987
L
3418 {
3419 if (&from[i] >= end)
3420 break;
3421 *to++ = from[i];
3422 }
5e675b72 3423
b7dd81f7 3424 size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
5e675b72
AM
3425 osection->lma /= interleave;
3426 }
252b5132 3427
d3ba0551 3428 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2db6cde7
NS
3429 {
3430 status = 1;
3431 bfd_nonfatal_message (NULL, obfd, osection, NULL);
848ac659 3432 free (memhunk);
2db6cde7
NS
3433 return;
3434 }
252b5132
RH
3435 free (memhunk);
3436 }
2e62b721
NC
3437 else if ((p = find_section_list (bfd_get_section_name (ibfd, isection),
3438 FALSE, SECTION_CONTEXT_SET_FLAGS)) != NULL
3439 && (p->flags & SEC_HAS_CONTENTS) != 0)
252b5132 3440 {
d3ba0551 3441 void *memhunk = xmalloc (size);
252b5132
RH
3442
3443 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
3444 flag--they can just remove the section entirely and add it
3445 back again. However, we do permit them to turn on the
3446 SEC_HAS_CONTENTS flag, and take it to mean that the section
3447 contents should be zeroed out. */
3448
3449 memset (memhunk, 0, size);
d3ba0551 3450 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2db6cde7
NS
3451 {
3452 status = 1;
3453 bfd_nonfatal_message (NULL, obfd, osection, NULL);
848ac659 3454 free (memhunk);
2db6cde7
NS
3455 return;
3456 }
252b5132
RH
3457 free (memhunk);
3458 }
3459}
3460
3461/* Get all the sections. This is used when --gap-fill or --pad-to is
3462 used. */
3463
3464static void
84e2f313 3465get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
252b5132 3466{
3f5e193b 3467 asection ***secppp = (asection ***) secppparg;
252b5132
RH
3468
3469 **secppp = osection;
3470 ++(*secppp);
3471}
3472
3473/* Sort sections by VMA. This is called via qsort, and is used when
3474 --gap-fill or --pad-to is used. We force non loadable or empty
3475 sections to the front, where they are easier to ignore. */
3476
3477static int
84e2f313 3478compare_section_lma (const void *arg1, const void *arg2)
252b5132 3479{
3f5e193b
NC
3480 const asection *const *sec1 = (const asection * const *) arg1;
3481 const asection *const *sec2 = (const asection * const *) arg2;
252b5132
RH
3482 flagword flags1, flags2;
3483
3484 /* Sort non loadable sections to the front. */
3485 flags1 = (*sec1)->flags;
3486 flags2 = (*sec2)->flags;
3487 if ((flags1 & SEC_HAS_CONTENTS) == 0
3488 || (flags1 & SEC_LOAD) == 0)
3489 {
3490 if ((flags2 & SEC_HAS_CONTENTS) != 0
3491 && (flags2 & SEC_LOAD) != 0)
3492 return -1;
3493 }
3494 else
3495 {
3496 if ((flags2 & SEC_HAS_CONTENTS) == 0
3497 || (flags2 & SEC_LOAD) == 0)
3498 return 1;
3499 }
3500
3501 /* Sort sections by LMA. */
3502 if ((*sec1)->lma > (*sec2)->lma)
3503 return 1;
3504 else if ((*sec1)->lma < (*sec2)->lma)
3505 return -1;
3506
3507 /* Sort sections with the same LMA by size. */
135dfb4a 3508 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
252b5132 3509 return 1;
135dfb4a 3510 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
252b5132
RH
3511 return -1;
3512
3513 return 0;
3514}
3515
3516/* Mark all the symbols which will be used in output relocations with
3517 the BSF_KEEP flag so that those symbols will not be stripped.
3518
3519 Ignore relocations which will not appear in the output file. */
3520
3521static void
84e2f313 3522mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
252b5132 3523{
3f5e193b 3524 asymbol **symbols = (asymbol **) symbolsarg;
252b5132
RH
3525 long relsize;
3526 arelent **relpp;
3527 long relcount, i;
3528
3529 /* Ignore an input section with no corresponding output section. */
3530 if (isection->output_section == NULL)
3531 return;
3532
3533 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
3534 if (relsize < 0)
ed570f48
NC
3535 {
3536 /* Do not complain if the target does not support relocations. */
3537 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
3538 return;
3539 bfd_fatal (bfd_get_filename (ibfd));
3540 }
252b5132
RH
3541
3542 if (relsize == 0)
3543 return;
3544
3f5e193b 3545 relpp = (arelent **) xmalloc (relsize);
252b5132
RH
3546 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
3547 if (relcount < 0)
3548 bfd_fatal (bfd_get_filename (ibfd));
3549
ec5d57d5
NC
3550 /* Examine each symbol used in a relocation. If it's not one of the
3551 special bfd section symbols, then mark it with BSF_KEEP. */
252b5132
RH
3552 for (i = 0; i < relcount; i++)
3553 {
ec5d57d5
NC
3554 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
3555 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
3556 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
3557 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
252b5132
RH
3558 }
3559
3560 if (relpp != NULL)
3561 free (relpp);
3562}
3563
3564/* Write out debugging information. */
3565
b34976b6 3566static bfd_boolean
84e2f313
NC
3567write_debugging_info (bfd *obfd, void *dhandle,
3568 long *symcountp ATTRIBUTE_UNUSED,
3569 asymbol ***symppp ATTRIBUTE_UNUSED)
252b5132
RH
3570{
3571 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
3572 return write_ieee_debugging_info (obfd, dhandle);
3573
3574 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
3575 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
3576 {
3577 bfd_byte *syms, *strings;
3578 bfd_size_type symsize, stringsize;
3579 asection *stabsec, *stabstrsec;
551b43fd 3580 flagword flags;
252b5132
RH
3581
3582 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
3583 &symsize, &strings,
3584 &stringsize))
b34976b6 3585 return FALSE;
252b5132 3586
551b43fd
AM
3587 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
3588 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
3589 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
252b5132
RH
3590 if (stabsec == NULL
3591 || stabstrsec == NULL
3592 || ! bfd_set_section_size (obfd, stabsec, symsize)
3593 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
3594 || ! bfd_set_section_alignment (obfd, stabsec, 2)
551b43fd 3595 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
252b5132 3596 {
2db6cde7
NS
3597 bfd_nonfatal_message (NULL, obfd, NULL,
3598 _("can't create debugging section"));
b34976b6 3599 return FALSE;
252b5132
RH
3600 }
3601
3602 /* We can get away with setting the section contents now because
f7433f01
AM
3603 the next thing the caller is going to do is copy over the
3604 real sections. We may someday have to split the contents
3605 setting out of this function. */
d3ba0551
AM
3606 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
3607 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
3608 stringsize))
252b5132 3609 {
2db6cde7
NS
3610 bfd_nonfatal_message (NULL, obfd, NULL,
3611 _("can't set debugging section contents"));
b34976b6 3612 return FALSE;
252b5132
RH
3613 }
3614
b34976b6 3615 return TRUE;
252b5132
RH
3616 }
3617
2db6cde7
NS
3618 bfd_nonfatal_message (NULL, obfd, NULL,
3619 _("don't know how to write debugging information for %s"),
f7433f01 3620 bfd_get_target (obfd));
b34976b6 3621 return FALSE;
252b5132
RH
3622}
3623
955d0b3b
RM
3624/* If neither -D nor -U was specified explicitly,
3625 then use the configured default. */
3626static void
3627default_deterministic (void)
3628{
3629 if (deterministic < 0)
3630 deterministic = DEFAULT_AR_DETERMINISTIC;
3631}
3632
252b5132 3633static int
84e2f313 3634strip_main (int argc, char *argv[])
252b5132 3635{
7c29036b
NC
3636 char *input_target = NULL;
3637 char *output_target = NULL;
b34976b6 3638 bfd_boolean show_version = FALSE;
7c29036b
NC
3639 bfd_boolean formats_info = FALSE;
3640 int c;
3641 int i;
252b5132
RH
3642 char *output_file = NULL;
3643
7a093a78 3644 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvwDU",
252b5132
RH
3645 strip_options, (int *) 0)) != EOF)
3646 {
3647 switch (c)
3648 {
3649 case 'I':
3650 input_target = optarg;
3651 break;
3652 case 'O':
3653 output_target = optarg;
3654 break;
3655 case 'F':
3656 input_target = output_target = optarg;
3657 break;
3658 case 'R':
d3e5f6c8
AB
3659 handle_remove_section_option (optarg);
3660 break;
3661 case OPTION_REMOVE_RELOCS:
3662 handle_remove_relocations_option (optarg);
252b5132
RH
3663 break;
3664 case 's':
3665 strip_symbols = STRIP_ALL;
3666 break;
3667 case 'S':
3668 case 'g':
db4f6831 3669 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
252b5132
RH
3670 strip_symbols = STRIP_DEBUG;
3671 break;
96109726
CC
3672 case OPTION_STRIP_DWO:
3673 strip_symbols = STRIP_DWO;
3674 break;
252b5132
RH
3675 case OPTION_STRIP_UNNEEDED:
3676 strip_symbols = STRIP_UNNEEDED;
3677 break;
3678 case 'K':
047c9024 3679 add_specific_symbol (optarg, keep_specific_htab);
252b5132
RH
3680 break;
3681 case 'N':
047c9024 3682 add_specific_symbol (optarg, strip_specific_htab);
252b5132
RH
3683 break;
3684 case 'o':
3685 output_file = optarg;
3686 break;
3687 case 'p':
b34976b6 3688 preserve_dates = TRUE;
252b5132 3689 break;
2e30cb57
CC
3690 case 'D':
3691 deterministic = TRUE;
3692 break;
955d0b3b
RM
3693 case 'U':
3694 deterministic = FALSE;
3695 break;
252b5132
RH
3696 case 'x':
3697 discard_locals = LOCALS_ALL;
3698 break;
3699 case 'X':
3700 discard_locals = LOCALS_START_L;
3701 break;
3702 case 'v':
b34976b6 3703 verbose = TRUE;
252b5132
RH
3704 break;
3705 case 'V':
b34976b6 3706 show_version = TRUE;
252b5132 3707 break;
7c29036b
NC
3708 case OPTION_FORMATS_INFO:
3709 formats_info = TRUE;
3710 break;
ed1653a7
NC
3711 case OPTION_ONLY_KEEP_DEBUG:
3712 strip_symbols = STRIP_NONDEBUG;
3713 break;
1637cd90
JB
3714 case OPTION_KEEP_FILE_SYMBOLS:
3715 keep_file_symbols = 1;
3716 break;
252b5132 3717 case 0:
594ef5db
NC
3718 /* We've been given a long option. */
3719 break;
5fe11841
NC
3720 case 'w':
3721 wildcard = TRUE;
3722 break;
8b53311e 3723 case 'H':
252b5132
RH
3724 case 'h':
3725 strip_usage (stdout, 0);
3726 default:
3727 strip_usage (stderr, 1);
3728 }
3729 }
3730
84e2f313
NC
3731 if (formats_info)
3732 {
3733 display_info ();
3734 return 0;
3735 }
c1c0eb9e 3736
252b5132
RH
3737 if (show_version)
3738 print_version ("strip");
3739
955d0b3b
RM
3740 default_deterministic ();
3741
252b5132
RH
3742 /* Default is to strip all symbols. */
3743 if (strip_symbols == STRIP_UNDEF
3744 && discard_locals == LOCALS_UNDEF
047c9024 3745 && htab_elements (strip_specific_htab) == 0)
252b5132
RH
3746 strip_symbols = STRIP_ALL;
3747
d3ba0551 3748 if (output_target == NULL)
252b5132
RH
3749 output_target = input_target;
3750
3751 i = optind;
3752 if (i == argc
3753 || (output_file != NULL && (i + 1) < argc))
3754 strip_usage (stderr, 1);
3755
3756 for (; i < argc; i++)
3757 {
3758 int hold_status = status;
3759 struct stat statbuf;
3760 char *tmpname;
3761
f24ddbdd 3762 if (get_file_size (argv[i]) < 1)
d68c385b
NC
3763 {
3764 status = 1;
3765 continue;
3766 }
f24ddbdd 3767
252b5132 3768 if (preserve_dates)
f24ddbdd
NC
3769 /* No need to check the return value of stat().
3770 It has already been checked in get_file_size(). */
3771 stat (argv[i], &statbuf);
252b5132 3772
8b6efd89
KT
3773 if (output_file == NULL
3774 || filename_cmp (argv[i], output_file) == 0)
252b5132 3775 tmpname = make_tempname (argv[i]);
12f498a7
NS
3776 else
3777 tmpname = output_file;
252b5132 3778
f9c026a8
NC
3779 if (tmpname == NULL)
3780 {
2db6cde7
NS
3781 bfd_nonfatal_message (argv[i], NULL, NULL,
3782 _("could not create temporary file to hold stripped copy"));
f9c026a8
NC
3783 status = 1;
3784 continue;
3785 }
3786
d68c385b 3787 status = 0;
8b31b6c4 3788 copy_file (argv[i], tmpname, input_target, output_target, NULL);
252b5132
RH
3789 if (status == 0)
3790 {
3791 if (preserve_dates)
3792 set_times (tmpname, &statbuf);
12f498a7 3793 if (output_file != tmpname)
92fac5ec
L
3794 status = (smart_rename (tmpname,
3795 output_file ? output_file : argv[i],
3796 preserve_dates) != 0);
3797 if (status == 0)
3798 status = hold_status;
252b5132
RH
3799 }
3800 else
bb14f524 3801 unlink_if_ordinary (tmpname);
12f498a7 3802 if (output_file != tmpname)
252b5132
RH
3803 free (tmpname);
3804 }
3805
d68c385b 3806 return status;
252b5132
RH
3807}
3808
92dd4511
L
3809/* Set up PE subsystem. */
3810
3811static void
3812set_pe_subsystem (const char *s)
3813{
3814 const char *version, *subsystem;
3815 size_t i;
3816 static const struct
3817 {
3818 const char *name;
3819 const char set_def;
3820 const short value;
3821 }
3822 v[] =
3823 {
955d0b3b 3824 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
92dd4511
L
3825 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
3826 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
3827 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
3828 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
3829 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
3830 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
3831 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
d9118602 3832 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
92dd4511
L
3833 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
3834 };
3835 short value;
3836 char *copy;
3837 int set_def = -1;
3838
3839 /* Check for the presence of a version number. */
3840 version = strchr (s, ':');
3841 if (version == NULL)
3842 subsystem = s;
3843 else
3844 {
3845 int len = version - s;
3846 copy = xstrdup (s);
3847 subsystem = copy;
3848 copy[len] = '\0';
3849 version = copy + 1 + len;
3850 pe_major_subsystem_version = strtoul (version, &copy, 0);
3851 if (*copy == '.')
3852 pe_minor_subsystem_version = strtoul (copy + 1, &copy, 0);
3853 if (*copy != '\0')
3854 non_fatal (_("%s: bad version in PE subsystem"), s);
3855 }
3856
3857 /* Check for numeric subsystem. */
3858 value = (short) strtol (subsystem, &copy, 0);
3859 if (*copy == '\0')
3860 {
3861 for (i = 0; i < ARRAY_SIZE (v); i++)
3862 if (v[i].value == value)
3863 {
3864 pe_subsystem = value;
3865 set_def = v[i].set_def;
3866 break;
3867 }
3868 }
3869 else
3870 {
3871 /* Search for subsystem by name. */
3872 for (i = 0; i < ARRAY_SIZE (v); i++)
3873 if (strcmp (subsystem, v[i].name) == 0)
3874 {
3875 pe_subsystem = v[i].value;
3876 set_def = v[i].set_def;
3877 break;
3878 }
3879 }
3880
3881 switch (set_def)
3882 {
3883 case -1:
3884 fatal (_("unknown PE subsystem: %s"), s);
3885 break;
3886 case 0:
3887 break;
3888 default:
3889 if (pe_file_alignment == (bfd_vma) -1)
3890 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3891 if (pe_section_alignment == (bfd_vma) -1)
3892 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3893 break;
3894 }
0cbf3531
MS
3895 if (s != subsystem)
3896 free ((char *) subsystem);
92dd4511
L
3897}
3898
3899/* Convert EFI target to PEI target. */
3900
3901static void
3902convert_efi_target (char *efi)
3903{
3904 efi[0] = 'p';
3905 efi[1] = 'e';
3906 efi[2] = 'i';
3907
3908 if (strcmp (efi + 4, "ia32") == 0)
3909 {
3910 /* Change ia32 to i386. */
3911 efi[5]= '3';
3912 efi[6]= '8';
3913 efi[7]= '6';
3914 }
3915 else if (strcmp (efi + 4, "x86_64") == 0)
3916 {
3917 /* Change x86_64 to x86-64. */
3918 efi[7] = '-';
3919 }
3920}
3921
7173b38a 3922/* Allocate and return a pointer to a struct section_add, initializing the
06b73f41 3923 structure using ARG, a string in the format "sectionname=filename".
7173b38a
AB
3924 The returned structure will have its next pointer set to NEXT. The
3925 OPTION field is the name of the command line option currently being
3926 parsed, and is only used if an error needs to be reported. */
3927
3928static struct section_add *
06b73f41 3929init_section_add (const char *arg,
f7433f01
AM
3930 struct section_add *next,
3931 const char *option)
7173b38a
AB
3932{
3933 struct section_add *pa;
3934 const char *s;
3935
06b73f41 3936 s = strchr (arg, '=');
7173b38a
AB
3937 if (s == NULL)
3938 fatal (_("bad format for %s"), option);
3939
3940 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
06b73f41 3941 pa->name = xstrndup (arg, s - arg);
7173b38a
AB
3942 pa->filename = s + 1;
3943 pa->next = next;
3944 pa->contents = NULL;
3945 pa->size = 0;
3946
3947 return pa;
3948}
3949
3950/* Load the file specified in PA, allocating memory to hold the file
3951 contents, and store a pointer to the allocated memory in the contents
3952 field of PA. The size field of PA is also updated. All errors call
3953 FATAL. */
3954
3955static void
3956section_add_load_file (struct section_add *pa)
3957{
3958 size_t off, alloc;
3959 FILE *f;
3960
3961 /* We don't use get_file_size so that we can do
3962 --add-section .note.GNU_stack=/dev/null
3963 get_file_size doesn't work on /dev/null. */
3964
3965 f = fopen (pa->filename, FOPEN_RB);
3966 if (f == NULL)
3967 fatal (_("cannot open: %s: %s"),
f7433f01 3968 pa->filename, strerror (errno));
7173b38a
AB
3969
3970 off = 0;
3971 alloc = 4096;
3972 pa->contents = (bfd_byte *) xmalloc (alloc);
3973 while (!feof (f))
3974 {
3975 off_t got;
3976
3977 if (off == alloc)
f7433f01
AM
3978 {
3979 alloc <<= 1;
3980 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
3981 }
7173b38a
AB
3982
3983 got = fread (pa->contents + off, 1, alloc - off, f);
3984 if (ferror (f))
f7433f01 3985 fatal (_("%s: fread failed"), pa->filename);
7173b38a
AB
3986
3987 off += got;
3988 }
3989
3990 pa->size = off;
3991
3992 fclose (f);
3993}
3994
252b5132 3995static int
84e2f313 3996copy_main (int argc, char *argv[])
252b5132 3997{
7c29036b
NC
3998 char *input_filename = NULL;
3999 char *output_filename = NULL;
c1c0eb9e 4000 char *tmpname;
7c29036b
NC
4001 char *input_target = NULL;
4002 char *output_target = NULL;
b34976b6
AM
4003 bfd_boolean show_version = FALSE;
4004 bfd_boolean change_warn = TRUE;
7c29036b 4005 bfd_boolean formats_info = FALSE;
252b5132 4006 int c;
252b5132 4007 struct stat statbuf;
8b31b6c4 4008 const bfd_arch_info_type *input_arch = NULL;
252b5132 4009
7a093a78 4010 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
252b5132
RH
4011 copy_options, (int *) 0)) != EOF)
4012 {
4013 switch (c)
4014 {
4015 case 'b':
4016 copy_byte = atoi (optarg);
4017 if (copy_byte < 0)
4018 fatal (_("byte number must be non-negative"));
4019 break;
57938635 4020
0af11b59 4021 case 'B':
8b31b6c4
NC
4022 input_arch = bfd_scan_arch (optarg);
4023 if (input_arch == NULL)
4024 fatal (_("architecture %s unknown"), optarg);
0af11b59 4025 break;
43a0748c 4026
252b5132 4027 case 'i':
b7dd81f7
NC
4028 if (optarg)
4029 {
4030 interleave = atoi (optarg);
4031 if (interleave < 1)
4032 fatal (_("interleave must be positive"));
4033 }
4034 else
4035 interleave = 4;
4036 break;
4037
4038 case OPTION_INTERLEAVE_WIDTH:
4039 copy_width = atoi (optarg);
4040 if (copy_width < 1)
4041 fatal(_("interleave width must be positive"));
252b5132 4042 break;
57938635 4043
252b5132
RH
4044 case 'I':
4045 case 's': /* "source" - 'I' is preferred */
4046 input_target = optarg;
4047 break;
57938635 4048
252b5132
RH
4049 case 'O':
4050 case 'd': /* "destination" - 'O' is preferred */
4051 output_target = optarg;
4052 break;
57938635 4053
252b5132
RH
4054 case 'F':
4055 input_target = output_target = optarg;
4056 break;
57938635 4057
f91ea849 4058 case 'j':
2e62b721 4059 find_section_list (optarg, TRUE, SECTION_CONTEXT_COPY);
b34976b6 4060 sections_copied = TRUE;
f91ea849 4061 break;
57938635 4062
252b5132 4063 case 'R':
d3e5f6c8
AB
4064 handle_remove_section_option (optarg);
4065 break;
4066
4067 case OPTION_REMOVE_RELOCS:
4068 handle_remove_relocations_option (optarg);
252b5132 4069 break;
57938635 4070
252b5132
RH
4071 case 'S':
4072 strip_symbols = STRIP_ALL;
4073 break;
57938635 4074
252b5132
RH
4075 case 'g':
4076 strip_symbols = STRIP_DEBUG;
4077 break;
57938635 4078
96109726
CC
4079 case OPTION_STRIP_DWO:
4080 strip_symbols = STRIP_DWO;
4081 break;
4082
252b5132
RH
4083 case OPTION_STRIP_UNNEEDED:
4084 strip_symbols = STRIP_UNNEEDED;
4085 break;
57938635 4086
ed1653a7
NC
4087 case OPTION_ONLY_KEEP_DEBUG:
4088 strip_symbols = STRIP_NONDEBUG;
4089 break;
4090
1637cd90
JB
4091 case OPTION_KEEP_FILE_SYMBOLS:
4092 keep_file_symbols = 1;
4093 break;
4094
2593f09a 4095 case OPTION_ADD_GNU_DEBUGLINK:
9b8bf321 4096 long_section_names = ENABLE ;
2593f09a
NC
4097 gnu_debuglink_filename = optarg;
4098 break;
4099
252b5132 4100 case 'K':
047c9024 4101 add_specific_symbol (optarg, keep_specific_htab);
252b5132 4102 break;
57938635 4103
252b5132 4104 case 'N':
047c9024 4105 add_specific_symbol (optarg, strip_specific_htab);
252b5132 4106 break;
57938635 4107
bcf32829 4108 case OPTION_STRIP_UNNEEDED_SYMBOL:
047c9024 4109 add_specific_symbol (optarg, strip_unneeded_htab);
bcf32829
JB
4110 break;
4111
252b5132 4112 case 'L':
047c9024 4113 add_specific_symbol (optarg, localize_specific_htab);
252b5132 4114 break;
57938635 4115
7b4a0685 4116 case OPTION_GLOBALIZE_SYMBOL:
047c9024 4117 add_specific_symbol (optarg, globalize_specific_htab);
7b4a0685
NC
4118 break;
4119
16b2b71c 4120 case 'G':
047c9024 4121 add_specific_symbol (optarg, keepglobal_specific_htab);
16b2b71c
NC
4122 break;
4123
252b5132 4124 case 'W':
047c9024 4125 add_specific_symbol (optarg, weaken_specific_htab);
252b5132 4126 break;
57938635 4127
252b5132 4128 case 'p':
b34976b6 4129 preserve_dates = TRUE;
252b5132 4130 break;
57938635 4131
2e30cb57
CC
4132 case 'D':
4133 deterministic = TRUE;
4134 break;
4135
955d0b3b
RM
4136 case 'U':
4137 deterministic = FALSE;
4138 break;
4139
5fe11841
NC
4140 case 'w':
4141 wildcard = TRUE;
4142 break;
4143
252b5132
RH
4144 case 'x':
4145 discard_locals = LOCALS_ALL;
4146 break;
57938635 4147
252b5132
RH
4148 case 'X':
4149 discard_locals = LOCALS_START_L;
4150 break;
57938635 4151
252b5132 4152 case 'v':
b34976b6 4153 verbose = TRUE;
252b5132 4154 break;
57938635 4155
252b5132 4156 case 'V':
b34976b6 4157 show_version = TRUE;
252b5132 4158 break;
57938635 4159
7c29036b
NC
4160 case OPTION_FORMATS_INFO:
4161 formats_info = TRUE;
4162 break;
4163
252b5132 4164 case OPTION_WEAKEN:
b34976b6 4165 weaken = TRUE;
252b5132 4166 break;
57938635 4167
252b5132 4168 case OPTION_ADD_SECTION:
f7433f01
AM
4169 add_sections = init_section_add (optarg, add_sections,
4170 "--add-section");
4171 section_add_load_file (add_sections);
252b5132 4172 break;
57938635 4173
acf1419f
AB
4174 case OPTION_UPDATE_SECTION:
4175 update_sections = init_section_add (optarg, update_sections,
f7433f01 4176 "--update-section");
acf1419f
AB
4177 section_add_load_file (update_sections);
4178 break;
4179
bbad633b 4180 case OPTION_DUMP_SECTION:
f7433f01
AM
4181 dump_sections = init_section_add (optarg, dump_sections,
4182 "--dump-section");
bbad633b 4183 break;
3aade688 4184
2b35fb28
RH
4185 case OPTION_ADD_SYMBOL:
4186 {
4187 char *s, *t;
4188 struct addsym_node *newsym = xmalloc (sizeof *newsym);
4189
4190 newsym->next = NULL;
4191 s = strchr (optarg, '=');
4192 if (s == NULL)
4193 fatal (_("bad format for %s"), "--add-symbol");
4194 t = strchr (s + 1, ':');
4195
a4f8732b 4196 newsym->symdef = xstrndup (optarg, s - optarg);
2b35fb28
RH
4197 if (t)
4198 {
a4f8732b 4199 newsym->section = xstrndup (s + 1, t - (s + 1));
2b35fb28
RH
4200 newsym->symval = strtol (t + 1, NULL, 0);
4201 }
4202 else
4203 {
4204 newsym->section = NULL;
4205 newsym->symval = strtol (s + 1, NULL, 0);
4206 t = s;
4207 }
4208
4209 t = strchr (t + 1, ',');
f7433f01 4210 newsym->othersym = NULL;
2b35fb28
RH
4211 if (t)
4212 newsym->flags = parse_symflags (t+1, &newsym->othersym);
4213 else
4214 newsym->flags = BSF_GLOBAL;
4215
4216 /* Keep 'othersym' symbols at the front of the list. */
4217 if (newsym->othersym)
4218 {
4219 newsym->next = add_sym_list;
4220 if (!add_sym_list)
4221 add_sym_tail = &newsym->next;
4222 add_sym_list = newsym;
4223 }
4224 else
4225 {
4226 *add_sym_tail = newsym;
4227 add_sym_tail = &newsym->next;
4228 }
4229 add_symbols++;
4230 }
4231 break;
4232
252b5132
RH
4233 case OPTION_CHANGE_START:
4234 change_start = parse_vma (optarg, "--change-start");
4235 break;
57938635 4236
252b5132
RH
4237 case OPTION_CHANGE_SECTION_ADDRESS:
4238 case OPTION_CHANGE_SECTION_LMA:
4239 case OPTION_CHANGE_SECTION_VMA:
4240 {
2e62b721 4241 struct section_list * p;
76d8cf45 4242 unsigned int context = 0;
252b5132
RH
4243 const char *s;
4244 int len;
4245 char *name;
b4c96d0d 4246 char *option = NULL;
252b5132 4247 bfd_vma val;
57938635 4248
252b5132
RH
4249 switch (c)
4250 {
b4c96d0d
ILT
4251 case OPTION_CHANGE_SECTION_ADDRESS:
4252 option = "--change-section-address";
2e62b721 4253 context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
b4c96d0d
ILT
4254 break;
4255 case OPTION_CHANGE_SECTION_LMA:
4256 option = "--change-section-lma";
2e62b721 4257 context = SECTION_CONTEXT_ALTER_LMA;
b4c96d0d
ILT
4258 break;
4259 case OPTION_CHANGE_SECTION_VMA:
4260 option = "--change-section-vma";
2e62b721 4261 context = SECTION_CONTEXT_ALTER_VMA;
b4c96d0d 4262 break;
252b5132 4263 }
57938635 4264
252b5132
RH
4265 s = strchr (optarg, '=');
4266 if (s == NULL)
4267 {
4268 s = strchr (optarg, '+');
4269 if (s == NULL)
4270 {
4271 s = strchr (optarg, '-');
4272 if (s == NULL)
4273 fatal (_("bad format for %s"), option);
4274 }
4275 }
2e62b721
NC
4276 else
4277 {
4278 /* Correct the context. */
4279 switch (c)
4280 {
4281 case OPTION_CHANGE_SECTION_ADDRESS:
4282 context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
4283 break;
4284 case OPTION_CHANGE_SECTION_LMA:
4285 context = SECTION_CONTEXT_SET_LMA;
4286 break;
4287 case OPTION_CHANGE_SECTION_VMA:
4288 context = SECTION_CONTEXT_SET_VMA;
4289 break;
4290 }
4291 }
252b5132
RH
4292
4293 len = s - optarg;
3f5e193b 4294 name = (char *) xmalloc (len + 1);
252b5132
RH
4295 strncpy (name, optarg, len);
4296 name[len] = '\0';
4297
2e62b721 4298 p = find_section_list (name, TRUE, context);
252b5132
RH
4299
4300 val = parse_vma (s + 1, option);
2e62b721
NC
4301 if (*s == '-')
4302 val = - val;
57938635 4303
252b5132
RH
4304 switch (c)
4305 {
4306 case OPTION_CHANGE_SECTION_ADDRESS:
2e62b721 4307 p->vma_val = val;
1a0670f3 4308 /* Fall through. */
57938635 4309
252b5132 4310 case OPTION_CHANGE_SECTION_LMA:
2e62b721 4311 p->lma_val = val;
252b5132 4312 break;
57938635 4313
252b5132 4314 case OPTION_CHANGE_SECTION_VMA:
2e62b721 4315 p->vma_val = val;
252b5132
RH
4316 break;
4317 }
4318 }
4319 break;
57938635 4320
252b5132
RH
4321 case OPTION_CHANGE_ADDRESSES:
4322 change_section_address = parse_vma (optarg, "--change-addresses");
4323 change_start = change_section_address;
4324 break;
57938635 4325
252b5132 4326 case OPTION_CHANGE_WARNINGS:
b34976b6 4327 change_warn = TRUE;
252b5132 4328 break;
57938635 4329
252b5132 4330 case OPTION_CHANGE_LEADING_CHAR:
b34976b6 4331 change_leading_char = TRUE;
252b5132 4332 break;
57938635 4333
4a114e3e 4334 case OPTION_COMPRESS_DEBUG_SECTIONS:
151411f8
L
4335 if (optarg)
4336 {
4337 if (strcasecmp (optarg, "none") == 0)
4338 do_debug_sections = decompress;
4339 else if (strcasecmp (optarg, "zlib") == 0)
4340 do_debug_sections = compress_zlib;
4341 else if (strcasecmp (optarg, "zlib-gnu") == 0)
4342 do_debug_sections = compress_gnu_zlib;
4343 else if (strcasecmp (optarg, "zlib-gabi") == 0)
4344 do_debug_sections = compress_gabi_zlib;
4345 else
4346 fatal (_("unrecognized --compress-debug-sections type `%s'"),
4347 optarg);
4348 }
4349 else
4350 do_debug_sections = compress;
4a114e3e
L
4351 break;
4352
252b5132 4353 case OPTION_DEBUGGING:
b34976b6 4354 convert_debugging = TRUE;
252b5132 4355 break;
57938635 4356
4a114e3e
L
4357 case OPTION_DECOMPRESS_DEBUG_SECTIONS:
4358 do_debug_sections = decompress;
4359 break;
4360
b8871f35
L
4361 case OPTION_ELF_STT_COMMON:
4362 if (strcasecmp (optarg, "yes") == 0)
4363 do_elf_stt_common = elf_stt_common;
4364 else if (strcasecmp (optarg, "no") == 0)
4365 do_elf_stt_common = no_elf_stt_common;
4366 else
4367 fatal (_("unrecognized --elf-stt-common= option `%s'"),
4368 optarg);
4369 break;
4370
252b5132
RH
4371 case OPTION_GAP_FILL:
4372 {
4373 bfd_vma gap_fill_vma;
4374
4375 gap_fill_vma = parse_vma (optarg, "--gap-fill");
4376 gap_fill = (bfd_byte) gap_fill_vma;
4377 if ((bfd_vma) gap_fill != gap_fill_vma)
4378 {
4379 char buff[20];
57938635 4380
252b5132 4381 sprintf_vma (buff, gap_fill_vma);
57938635 4382
252b5132
RH
4383 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
4384 buff, gap_fill);
4385 }
b34976b6 4386 gap_fill_set = TRUE;
252b5132
RH
4387 }
4388 break;
57938635 4389
252b5132 4390 case OPTION_NO_CHANGE_WARNINGS:
b34976b6 4391 change_warn = FALSE;
252b5132 4392 break;
57938635 4393
252b5132
RH
4394 case OPTION_PAD_TO:
4395 pad_to = parse_vma (optarg, "--pad-to");
b34976b6 4396 pad_to_set = TRUE;
252b5132 4397 break;
57938635 4398
252b5132 4399 case OPTION_REMOVE_LEADING_CHAR:
b34976b6 4400 remove_leading_char = TRUE;
252b5132 4401 break;
57938635
AM
4402
4403 case OPTION_REDEFINE_SYM:
4404 {
4405 /* Push this redefinition onto redefine_symbol_list. */
4406
4407 int len;
4408 const char *s;
4409 const char *nextarg;
4410 char *source, *target;
4411
4412 s = strchr (optarg, '=');
4413 if (s == NULL)
594ef5db 4414 fatal (_("bad format for %s"), "--redefine-sym");
57938635
AM
4415
4416 len = s - optarg;
3f5e193b 4417 source = (char *) xmalloc (len + 1);
57938635
AM
4418 strncpy (source, optarg, len);
4419 source[len] = '\0';
4420
4421 nextarg = s + 1;
4422 len = strlen (nextarg);
3f5e193b 4423 target = (char *) xmalloc (len + 1);
57938635
AM
4424 strcpy (target, nextarg);
4425
92991082 4426 redefine_list_append ("--redefine-sym", source, target);
57938635
AM
4427
4428 free (source);
4429 free (target);
4430 }
4431 break;
4432
92991082
JT
4433 case OPTION_REDEFINE_SYMS:
4434 add_redefine_syms_file (optarg);
4435 break;
4436
252b5132
RH
4437 case OPTION_SET_SECTION_FLAGS:
4438 {
2e62b721 4439 struct section_list *p;
252b5132
RH
4440 const char *s;
4441 int len;
4442 char *name;
4443
4444 s = strchr (optarg, '=');
4445 if (s == NULL)
57938635 4446 fatal (_("bad format for %s"), "--set-section-flags");
252b5132
RH
4447
4448 len = s - optarg;
3f5e193b 4449 name = (char *) xmalloc (len + 1);
252b5132
RH
4450 strncpy (name, optarg, len);
4451 name[len] = '\0';
4452
2e62b721 4453 p = find_section_list (name, TRUE, SECTION_CONTEXT_SET_FLAGS);
252b5132 4454
252b5132
RH
4455 p->flags = parse_flags (s + 1);
4456 }
4457 break;
57938635 4458
594ef5db
NC
4459 case OPTION_RENAME_SECTION:
4460 {
4461 flagword flags;
3bcfb3e4
AM
4462 const char *eq, *fl;
4463 char *old_name;
4464 char *new_name;
594ef5db
NC
4465 unsigned int len;
4466
3bcfb3e4
AM
4467 eq = strchr (optarg, '=');
4468 if (eq == NULL)
594ef5db
NC
4469 fatal (_("bad format for %s"), "--rename-section");
4470
3bcfb3e4 4471 len = eq - optarg;
594ef5db 4472 if (len == 0)
3bcfb3e4 4473 fatal (_("bad format for %s"), "--rename-section");
594ef5db 4474
3f5e193b 4475 old_name = (char *) xmalloc (len + 1);
594ef5db
NC
4476 strncpy (old_name, optarg, len);
4477 old_name[len] = 0;
4478
3bcfb3e4
AM
4479 eq++;
4480 fl = strchr (eq, ',');
4481 if (fl)
594ef5db 4482 {
3bcfb3e4
AM
4483 flags = parse_flags (fl + 1);
4484 len = fl - eq;
594ef5db
NC
4485 }
4486 else
4487 {
594ef5db 4488 flags = -1;
3bcfb3e4 4489 len = strlen (eq);
594ef5db
NC
4490 }
4491
3bcfb3e4
AM
4492 if (len == 0)
4493 fatal (_("bad format for %s"), "--rename-section");
4494
3f5e193b 4495 new_name = (char *) xmalloc (len + 1);
3bcfb3e4
AM
4496 strncpy (new_name, eq, len);
4497 new_name[len] = 0;
4498
594ef5db
NC
4499 add_section_rename (old_name, new_name, flags);
4500 }
4501 break;
4502
252b5132
RH
4503 case OPTION_SET_START:
4504 set_start = parse_vma (optarg, "--set-start");
b34976b6 4505 set_start_set = TRUE;
252b5132 4506 break;
57938635 4507
0af11b59
KH
4508 case OPTION_SREC_LEN:
4509 Chunk = parse_vma (optarg, "--srec-len");
4510 break;
420496c1 4511
0af11b59 4512 case OPTION_SREC_FORCES3:
b34976b6 4513 S3Forced = TRUE;
0af11b59 4514 break;
420496c1 4515
16b2b71c 4516 case OPTION_STRIP_SYMBOLS:
047c9024 4517 add_specific_symbols (optarg, strip_specific_htab);
16b2b71c
NC
4518 break;
4519
bcf32829 4520 case OPTION_STRIP_UNNEEDED_SYMBOLS:
047c9024 4521 add_specific_symbols (optarg, strip_unneeded_htab);
bcf32829
JB
4522 break;
4523
16b2b71c 4524 case OPTION_KEEP_SYMBOLS:
047c9024 4525 add_specific_symbols (optarg, keep_specific_htab);
16b2b71c
NC
4526 break;
4527
d58c2e3a
RS
4528 case OPTION_LOCALIZE_HIDDEN:
4529 localize_hidden = TRUE;
4530 break;
4531
16b2b71c 4532 case OPTION_LOCALIZE_SYMBOLS:
047c9024 4533 add_specific_symbols (optarg, localize_specific_htab);
16b2b71c
NC
4534 break;
4535
0408dee6
DK
4536 case OPTION_LONG_SECTION_NAMES:
4537 if (!strcmp ("enable", optarg))
4538 long_section_names = ENABLE;
4539 else if (!strcmp ("disable", optarg))
4540 long_section_names = DISABLE;
4541 else if (!strcmp ("keep", optarg))
4542 long_section_names = KEEP;
4543 else
4544 fatal (_("unknown long section names option '%s'"), optarg);
4545 break;
4546
7b4a0685 4547 case OPTION_GLOBALIZE_SYMBOLS:
047c9024 4548 add_specific_symbols (optarg, globalize_specific_htab);
7b4a0685
NC
4549 break;
4550
16b2b71c 4551 case OPTION_KEEPGLOBAL_SYMBOLS:
047c9024 4552 add_specific_symbols (optarg, keepglobal_specific_htab);
16b2b71c
NC
4553 break;
4554
4555 case OPTION_WEAKEN_SYMBOLS:
047c9024 4556 add_specific_symbols (optarg, weaken_specific_htab);
16b2b71c
NC
4557 break;
4558
1ae8b3d2 4559 case OPTION_ALT_MACH_CODE:
f9d4ad2a
NC
4560 use_alt_mach_code = strtoul (optarg, NULL, 0);
4561 if (use_alt_mach_code == 0)
4562 fatal (_("unable to parse alternative machine code"));
1ae8b3d2
AO
4563 break;
4564
d7fb0dd2
NC
4565 case OPTION_PREFIX_SYMBOLS:
4566 prefix_symbols_string = optarg;
4567 break;
4568
4569 case OPTION_PREFIX_SECTIONS:
4570 prefix_sections_string = optarg;
4571 break;
4572
4573 case OPTION_PREFIX_ALLOC_SECTIONS:
4574 prefix_alloc_sections_string = optarg;
4575 break;
4576
4087920c
MR
4577 case OPTION_READONLY_TEXT:
4578 bfd_flags_to_set |= WP_TEXT;
4579 bfd_flags_to_clear &= ~WP_TEXT;
4580 break;
4581
4582 case OPTION_WRITABLE_TEXT:
4583 bfd_flags_to_clear |= WP_TEXT;
4584 bfd_flags_to_set &= ~WP_TEXT;
4585 break;
4586
4587 case OPTION_PURE:
4588 bfd_flags_to_set |= D_PAGED;
4589 bfd_flags_to_clear &= ~D_PAGED;
4590 break;
4591
4592 case OPTION_IMPURE:
4593 bfd_flags_to_clear |= D_PAGED;
4594 bfd_flags_to_set &= ~D_PAGED;
4595 break;
4596
96109726
CC
4597 case OPTION_EXTRACT_DWO:
4598 strip_symbols = STRIP_NONDWO;
4599 break;
4600
d3e52d40
RS
4601 case OPTION_EXTRACT_SYMBOL:
4602 extract_symbol = TRUE;
4603 break;
4604
9e48b4c6 4605 case OPTION_REVERSE_BYTES:
f7433f01
AM
4606 {
4607 int prev = reverse_bytes;
9e48b4c6 4608
f7433f01
AM
4609 reverse_bytes = atoi (optarg);
4610 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
4611 fatal (_("number of bytes to reverse must be positive and even"));
9e48b4c6 4612
f7433f01
AM
4613 if (prev && prev != reverse_bytes)
4614 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
4615 prev);
4616 break;
4617 }
9e48b4c6 4618
92dd4511
L
4619 case OPTION_FILE_ALIGNMENT:
4620 pe_file_alignment = parse_vma (optarg, "--file-alignment");
4621 break;
955d0b3b 4622
92dd4511 4623 case OPTION_HEAP:
f7433f01
AM
4624 {
4625 char *end;
4626 pe_heap_reserve = strtoul (optarg, &end, 0);
4627 if (end == optarg
4628 || (*end != '.' && *end != '\0'))
4629 non_fatal (_("%s: invalid reserve value for --heap"),
4630 optarg);
4631 else if (*end != '\0')
4632 {
4633 pe_heap_commit = strtoul (end + 1, &end, 0);
4634 if (*end != '\0')
4635 non_fatal (_("%s: invalid commit value for --heap"),
4636 optarg);
4637 }
4638 }
92dd4511 4639 break;
955d0b3b 4640
92dd4511
L
4641 case OPTION_IMAGE_BASE:
4642 pe_image_base = parse_vma (optarg, "--image-base");
4643 break;
955d0b3b 4644
92dd4511
L
4645 case OPTION_SECTION_ALIGNMENT:
4646 pe_section_alignment = parse_vma (optarg,
4647 "--section-alignment");
4648 break;
955d0b3b 4649
92dd4511
L
4650 case OPTION_SUBSYSTEM:
4651 set_pe_subsystem (optarg);
4652 break;
955d0b3b 4653
92dd4511 4654 case OPTION_STACK:
f7433f01
AM
4655 {
4656 char *end;
4657 pe_stack_reserve = strtoul (optarg, &end, 0);
4658 if (end == optarg
4659 || (*end != '.' && *end != '\0'))
4660 non_fatal (_("%s: invalid reserve value for --stack"),
4661 optarg);
4662 else if (*end != '\0')
4663 {
4664 pe_stack_commit = strtoul (end + 1, &end, 0);
4665 if (*end != '\0')
4666 non_fatal (_("%s: invalid commit value for --stack"),
4667 optarg);
4668 }
4669 }
92dd4511 4670 break;
955d0b3b 4671
252b5132 4672 case 0:
2593f09a
NC
4673 /* We've been given a long option. */
4674 break;
57938635 4675
8b53311e 4676 case 'H':
252b5132
RH
4677 case 'h':
4678 copy_usage (stdout, 0);
57938635 4679
252b5132
RH
4680 default:
4681 copy_usage (stderr, 1);
4682 }
4683 }
4684
7c29036b
NC
4685 if (formats_info)
4686 {
4687 display_info ();
4688 return 0;
4689 }
c1c0eb9e 4690
252b5132
RH
4691 if (show_version)
4692 print_version ("objcopy");
4693
b7dd81f7
NC
4694 if (interleave && copy_byte == -1)
4695 fatal (_("interleave start byte must be set with --byte"));
4696
252b5132
RH
4697 if (copy_byte >= interleave)
4698 fatal (_("byte number must be less than interleave"));
4699
b7dd81f7
NC
4700 if (copy_width > interleave - copy_byte)
4701 fatal (_("interleave width must be less than or equal to interleave - byte`"));
4702
252b5132
RH
4703 if (optind == argc || optind + 2 < argc)
4704 copy_usage (stderr, 1);
4705
4706 input_filename = argv[optind];
4707 if (optind + 1 < argc)
4708 output_filename = argv[optind + 1];
4709
955d0b3b
RM
4710 default_deterministic ();
4711
252b5132
RH
4712 /* Default is to strip no symbols. */
4713 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
4714 strip_symbols = STRIP_NONE;
4715
d3ba0551 4716 if (output_target == NULL)
252b5132
RH
4717 output_target = input_target;
4718
92dd4511
L
4719 /* Convert input EFI target to PEI target. */
4720 if (input_target != NULL
4721 && strncmp (input_target, "efi-", 4) == 0)
4722 {
4723 char *efi;
4724
4725 efi = xstrdup (output_target + 4);
4726 if (strncmp (efi, "bsdrv-", 6) == 0
4727 || strncmp (efi, "rtdrv-", 6) == 0)
4728 efi += 2;
4729 else if (strncmp (efi, "app-", 4) != 0)
4730 fatal (_("unknown input EFI target: %s"), input_target);
4731
4732 input_target = efi;
4733 convert_efi_target (efi);
4734 }
4735
4736 /* Convert output EFI target to PEI target. */
4737 if (output_target != NULL
4738 && strncmp (output_target, "efi-", 4) == 0)
4739 {
4740 char *efi;
4741
4742 efi = xstrdup (output_target + 4);
4743 if (strncmp (efi, "app-", 4) == 0)
4744 {
4745 if (pe_subsystem == -1)
4746 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
4747 }
4748 else if (strncmp (efi, "bsdrv-", 6) == 0)
4749 {
4750 if (pe_subsystem == -1)
4751 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
4752 efi += 2;
4753 }
4754 else if (strncmp (efi, "rtdrv-", 6) == 0)
4755 {
4756 if (pe_subsystem == -1)
4757 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
4758 efi += 2;
4759 }
4760 else
4761 fatal (_("unknown output EFI target: %s"), output_target);
4762
4763 if (pe_file_alignment == (bfd_vma) -1)
4764 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
4765 if (pe_section_alignment == (bfd_vma) -1)
4766 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
4767
4768 output_target = efi;
4769 convert_efi_target (efi);
4770 }
4771
43a0748c
NC
4772 if (preserve_dates)
4773 if (stat (input_filename, & statbuf) < 0)
f24ddbdd
NC
4774 fatal (_("warning: could not locate '%s'. System error message: %s"),
4775 input_filename, strerror (errno));
43a0748c 4776
0fcdcb91 4777 /* If there is no destination file, or the source and destination files
d3ba0551 4778 are the same, then create a temp and rename the result into the input. */
8b6efd89
KT
4779 if (output_filename == NULL
4780 || filename_cmp (input_filename, output_filename) == 0)
12f498a7 4781 tmpname = make_tempname (input_filename);
252b5132 4782 else
12f498a7 4783 tmpname = output_filename;
c1c0eb9e 4784
12f498a7
NS
4785 if (tmpname == NULL)
4786 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
4787 input_filename, strerror (errno));
594ef5db 4788
8b31b6c4 4789 copy_file (input_filename, tmpname, input_target, output_target, input_arch);
12f498a7
NS
4790 if (status == 0)
4791 {
4792 if (preserve_dates)
4793 set_times (tmpname, &statbuf);
4794 if (tmpname != output_filename)
92fac5ec
L
4795 status = (smart_rename (tmpname, input_filename,
4796 preserve_dates) != 0);
252b5132 4797 }
12f498a7
NS
4798 else
4799 unlink_if_ordinary (tmpname);
252b5132
RH
4800
4801 if (change_warn)
4802 {
2e62b721
NC
4803 struct section_list *p;
4804
252b5132
RH
4805 for (p = change_sections; p != NULL; p = p->next)
4806 {
4807 if (! p->used)
4808 {
2e62b721 4809 if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
252b5132
RH
4810 {
4811 char buff [20];
4812
4813 sprintf_vma (buff, p->vma_val);
57938635 4814
252b5132 4815 /* xgettext:c-format */
57938635
AM
4816 non_fatal (_("%s %s%c0x%s never used"),
4817 "--change-section-vma",
2e62b721
NC
4818 p->pattern,
4819 p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
252b5132
RH
4820 buff);
4821 }
57938635 4822
2e62b721 4823 if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
252b5132
RH
4824 {
4825 char buff [20];
4826
4827 sprintf_vma (buff, p->lma_val);
57938635 4828
252b5132 4829 /* xgettext:c-format */
57938635
AM
4830 non_fatal (_("%s %s%c0x%s never used"),
4831 "--change-section-lma",
2e62b721
NC
4832 p->pattern,
4833 p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
252b5132
RH
4834 buff);
4835 }
4836 }
4837 }
4838 }
4839
4840 return 0;
4841}
4842
4843int
84e2f313 4844main (int argc, char *argv[])
252b5132
RH
4845{
4846#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4847 setlocale (LC_MESSAGES, "");
3882b010
L
4848#endif
4849#if defined (HAVE_SETLOCALE)
4850 setlocale (LC_CTYPE, "");
252b5132
RH
4851#endif
4852 bindtextdomain (PACKAGE, LOCALEDIR);
4853 textdomain (PACKAGE);
4854
4855 program_name = argv[0];
4856 xmalloc_set_program_name (program_name);
4857
4858 START_PROGRESS (program_name, 0);
4859
869b9d07
MM
4860 expandargv (&argc, &argv);
4861
252b5132
RH
4862 strip_symbols = STRIP_UNDEF;
4863 discard_locals = LOCALS_UNDEF;
4864
4865 bfd_init ();
4866 set_default_bfd_target ();
4867
4868 if (is_strip < 0)
4869 {
4870 int i = strlen (program_name);
5af11cab
AM
4871#ifdef HAVE_DOS_BASED_FILE_SYSTEM
4872 /* Drop the .exe suffix, if any. */
4873 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
4874 {
4875 i -= 4;
4876 program_name[i] = '\0';
4877 }
4878#endif
4879 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
252b5132
RH
4880 }
4881
047c9024
NC
4882 create_symbol_htabs ();
4883
86eafac0
NC
4884 if (argv != NULL)
4885 bfd_set_error_program_name (argv[0]);
4886
252b5132
RH
4887 if (is_strip)
4888 strip_main (argc, argv);
4889 else
4890 copy_main (argc, argv);
4891
4892 END_PROGRESS (program_name);
4893
4894 return status;
4895}
This page took 1.182621 seconds and 4 git commands to generate.