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