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