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