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