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