*** empty log message ***
[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,
6e2c86ac 3 2001, 2002, 2003, 2004, 2005, 2006
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
10 the Free Software Foundation; either version 2 of the License, or
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
RH
22\f
23#include "bfd.h"
24#include "progress.h"
25#include "bucomm.h"
26#include "getopt.h"
27#include "libiberty.h"
28#include "budbg.h"
5af11cab 29#include "filenames.h"
5fe11841 30#include "fnmatch.h"
f0312d39 31#include "elf-bfd.h"
252b5132 32#include <sys/stat.h>
6e2c86ac 33#include "libbfd.h"
252b5132
RH
34
35/* A list of symbols to explicitly strip out, or to keep. A linked
36 list is good enough for a small number from the command line, but
37 this will slow things down a lot if many symbols are being
0af11b59 38 deleted. */
252b5132
RH
39
40struct symlist
41{
42 const char *name;
43 struct symlist *next;
44};
45
57938635
AM
46/* A list to support redefine_sym. */
47struct redefine_node
48{
49 char *source;
50 char *target;
51 struct redefine_node *next;
52};
53
594ef5db
NC
54typedef struct section_rename
55{
56 const char * old_name;
57 const char * new_name;
58 flagword flags;
59 struct section_rename * next;
60}
61section_rename;
62
63/* List of sections to be renamed. */
84e2f313 64static section_rename *section_rename_list;
594ef5db 65
252b5132
RH
66#define RETURN_NONFATAL(s) {bfd_nonfatal (s); status = 1; return;}
67
84e2f313
NC
68static asymbol **isympp = NULL; /* Input symbols. */
69static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
252b5132
RH
70
71/* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
72static int copy_byte = -1;
73static int interleave = 4;
74
b34976b6
AM
75static bfd_boolean verbose; /* Print file and target names. */
76static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
252b5132
RH
77static int status = 0; /* Exit status. */
78
79enum strip_action
80 {
81 STRIP_UNDEF,
84e2f313
NC
82 STRIP_NONE, /* Don't strip. */
83 STRIP_DEBUG, /* Strip all debugger symbols. */
84 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
ed1653a7 85 STRIP_NONDEBUG, /* Strip everything but debug info. */
84e2f313 86 STRIP_ALL /* Strip all symbols. */
252b5132
RH
87 };
88
0af11b59 89/* Which symbols to remove. */
252b5132
RH
90static enum strip_action strip_symbols;
91
92enum locals_action
93 {
94 LOCALS_UNDEF,
84e2f313
NC
95 LOCALS_START_L, /* Discard locals starting with L. */
96 LOCALS_ALL /* Discard all locals. */
252b5132
RH
97 };
98
99/* Which local symbols to remove. Overrides STRIP_ALL. */
100static enum locals_action discard_locals;
101
102/* What kind of change to perform. */
103enum change_action
104{
105 CHANGE_IGNORE,
106 CHANGE_MODIFY,
107 CHANGE_SET
108};
109
110/* Structure used to hold lists of sections and actions to take. */
111struct section_list
112{
b34976b6
AM
113 struct section_list * next; /* Next section to change. */
114 const char * name; /* Section name. */
115 bfd_boolean used; /* Whether this entry was used. */
116 bfd_boolean remove; /* Whether to remove this section. */
117 bfd_boolean copy; /* Whether to copy this section. */
118 enum change_action change_vma;/* Whether to change or set VMA. */
119 bfd_vma vma_val; /* Amount to change by or set to. */
120 enum change_action change_lma;/* Whether to change or set LMA. */
121 bfd_vma lma_val; /* Amount to change by or set to. */
122 bfd_boolean set_flags; /* Whether to set the section flags. */
123 flagword flags; /* What to set the section flags to. */
252b5132
RH
124};
125
126static struct section_list *change_sections;
594ef5db 127
b34976b6
AM
128/* TRUE if some sections are to be removed. */
129static bfd_boolean sections_removed;
594ef5db 130
b34976b6
AM
131/* TRUE if only some sections are to be copied. */
132static bfd_boolean sections_copied;
252b5132
RH
133
134/* Changes to the start address. */
135static bfd_vma change_start = 0;
b34976b6 136static bfd_boolean set_start_set = FALSE;
252b5132
RH
137static bfd_vma set_start;
138
139/* Changes to section addresses. */
140static bfd_vma change_section_address = 0;
141
142/* Filling gaps between sections. */
b34976b6 143static bfd_boolean gap_fill_set = FALSE;
252b5132
RH
144static bfd_byte gap_fill = 0;
145
146/* Pad to a given address. */
b34976b6 147static bfd_boolean pad_to_set = FALSE;
252b5132
RH
148static bfd_vma pad_to;
149
f9d4ad2a
NC
150/* Use alternative machine code? */
151static unsigned long use_alt_mach_code = 0;
1ae8b3d2 152
4087920c
MR
153/* Output BFD flags user wants to set or clear */
154static flagword bfd_flags_to_set;
155static flagword bfd_flags_to_clear;
156
252b5132 157/* List of sections to add. */
252b5132
RH
158struct section_add
159{
160 /* Next section to add. */
161 struct section_add *next;
162 /* Name of section to add. */
163 const char *name;
164 /* Name of file holding section contents. */
165 const char *filename;
166 /* Size of file. */
167 size_t size;
168 /* Contents of file. */
169 bfd_byte *contents;
170 /* BFD section, after it has been added. */
171 asection *section;
172};
173
594ef5db 174/* List of sections to add to the output BFD. */
252b5132
RH
175static struct section_add *add_sections;
176
2593f09a
NC
177/* If non-NULL the argument to --add-gnu-debuglink.
178 This should be the filename to store in the .gnu_debuglink section. */
179static const char * gnu_debuglink_filename = NULL;
180
252b5132 181/* Whether to convert debugging information. */
b34976b6 182static bfd_boolean convert_debugging = FALSE;
252b5132
RH
183
184/* Whether to change the leading character in symbol names. */
b34976b6 185static bfd_boolean change_leading_char = FALSE;
252b5132
RH
186
187/* Whether to remove the leading character from global symbol names. */
b34976b6 188static bfd_boolean remove_leading_char = FALSE;
252b5132 189
aaad4cf3 190/* Whether to permit wildcard in symbol comparison. */
5fe11841
NC
191static bfd_boolean wildcard = FALSE;
192
d58c2e3a
RS
193/* True if --localize-hidden is in effect. */
194static bfd_boolean localize_hidden = FALSE;
195
16b2b71c
NC
196/* List of symbols to strip, keep, localize, keep-global, weaken,
197 or redefine. */
252b5132 198static struct symlist *strip_specific_list = NULL;
bcf32829 199static struct symlist *strip_unneeded_list = NULL;
252b5132
RH
200static struct symlist *keep_specific_list = NULL;
201static struct symlist *localize_specific_list = NULL;
7b4a0685 202static struct symlist *globalize_specific_list = NULL;
16b2b71c 203static struct symlist *keepglobal_specific_list = NULL;
252b5132 204static struct symlist *weaken_specific_list = NULL;
57938635 205static struct redefine_node *redefine_sym_list = NULL;
252b5132 206
b34976b6
AM
207/* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
208static bfd_boolean weaken = FALSE;
252b5132 209
1637cd90
JB
210/* If this is TRUE, we retain BSF_FILE symbols. */
211static bfd_boolean keep_file_symbols = FALSE;
212
d7fb0dd2
NC
213/* Prefix symbols/sections. */
214static char *prefix_symbols_string = 0;
215static char *prefix_sections_string = 0;
216static char *prefix_alloc_sections_string = 0;
217
252b5132 218/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
84e2f313
NC
219enum command_line_switch
220 {
221 OPTION_ADD_SECTION=150,
222 OPTION_CHANGE_ADDRESSES,
223 OPTION_CHANGE_LEADING_CHAR,
224 OPTION_CHANGE_START,
225 OPTION_CHANGE_SECTION_ADDRESS,
226 OPTION_CHANGE_SECTION_LMA,
227 OPTION_CHANGE_SECTION_VMA,
228 OPTION_CHANGE_WARNINGS,
229 OPTION_DEBUGGING,
230 OPTION_GAP_FILL,
231 OPTION_NO_CHANGE_WARNINGS,
232 OPTION_PAD_TO,
233 OPTION_REMOVE_LEADING_CHAR,
234 OPTION_SET_SECTION_FLAGS,
235 OPTION_SET_START,
236 OPTION_STRIP_UNNEEDED,
237 OPTION_WEAKEN,
238 OPTION_REDEFINE_SYM,
239 OPTION_REDEFINE_SYMS,
240 OPTION_SREC_LEN,
241 OPTION_SREC_FORCES3,
242 OPTION_STRIP_SYMBOLS,
bcf32829
JB
243 OPTION_STRIP_UNNEEDED_SYMBOL,
244 OPTION_STRIP_UNNEEDED_SYMBOLS,
84e2f313 245 OPTION_KEEP_SYMBOLS,
d58c2e3a 246 OPTION_LOCALIZE_HIDDEN,
84e2f313 247 OPTION_LOCALIZE_SYMBOLS,
7b4a0685
NC
248 OPTION_GLOBALIZE_SYMBOL,
249 OPTION_GLOBALIZE_SYMBOLS,
84e2f313
NC
250 OPTION_KEEPGLOBAL_SYMBOLS,
251 OPTION_WEAKEN_SYMBOLS,
252 OPTION_RENAME_SECTION,
253 OPTION_ALT_MACH_CODE,
254 OPTION_PREFIX_SYMBOLS,
255 OPTION_PREFIX_SECTIONS,
256 OPTION_PREFIX_ALLOC_SECTIONS,
257 OPTION_FORMATS_INFO,
258 OPTION_ADD_GNU_DEBUGLINK,
4087920c 259 OPTION_ONLY_KEEP_DEBUG,
1637cd90 260 OPTION_KEEP_FILE_SYMBOLS,
4087920c
MR
261 OPTION_READONLY_TEXT,
262 OPTION_WRITABLE_TEXT,
263 OPTION_PURE,
264 OPTION_IMPURE
84e2f313 265 };
252b5132
RH
266
267/* Options to handle if running as "strip". */
268
269static struct option strip_options[] =
270{
271 {"discard-all", no_argument, 0, 'x'},
272 {"discard-locals", no_argument, 0, 'X'},
273 {"format", required_argument, 0, 'F'}, /* Obsolete */
274 {"help", no_argument, 0, 'h'},
7c29036b 275 {"info", no_argument, 0, OPTION_FORMATS_INFO},
252b5132
RH
276 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
277 {"input-target", required_argument, 0, 'I'},
1637cd90 278 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
252b5132 279 {"keep-symbol", required_argument, 0, 'K'},
ed1653a7 280 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
252b5132
RH
281 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
282 {"output-target", required_argument, 0, 'O'},
af3bdff7 283 {"output-file", required_argument, 0, 'o'},
252b5132
RH
284 {"preserve-dates", no_argument, 0, 'p'},
285 {"remove-section", required_argument, 0, 'R'},
286 {"strip-all", no_argument, 0, 's'},
287 {"strip-debug", no_argument, 0, 'S'},
288 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
289 {"strip-symbol", required_argument, 0, 'N'},
290 {"target", required_argument, 0, 'F'},
291 {"verbose", no_argument, 0, 'v'},
292 {"version", no_argument, 0, 'V'},
5fe11841 293 {"wildcard", no_argument, 0, 'w'},
252b5132
RH
294 {0, no_argument, 0, 0}
295};
296
297/* Options to handle if running as "objcopy". */
298
299static struct option copy_options[] =
300{
2593f09a 301 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
252b5132
RH
302 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
303 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
304 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
305 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
306 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
d7fb0dd2 307 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
43a0748c 308 {"binary-architecture", required_argument, 0, 'B'},
252b5132
RH
309 {"byte", required_argument, 0, 'b'},
310 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
311 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
312 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
313 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
314 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
315 {"change-start", required_argument, 0, OPTION_CHANGE_START},
316 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
317 {"debugging", no_argument, 0, OPTION_DEBUGGING},
318 {"discard-all", no_argument, 0, 'x'},
319 {"discard-locals", no_argument, 0, 'X'},
320 {"format", required_argument, 0, 'F'}, /* Obsolete */
321 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
7b4a0685
NC
322 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
323 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
252b5132 324 {"help", no_argument, 0, 'h'},
4087920c 325 {"impure", no_argument, 0, OPTION_IMPURE},
7c29036b 326 {"info", no_argument, 0, OPTION_FORMATS_INFO},
252b5132
RH
327 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
328 {"input-target", required_argument, 0, 'I'},
329 {"interleave", required_argument, 0, 'i'},
1637cd90 330 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
d7fb0dd2
NC
331 {"keep-global-symbol", required_argument, 0, 'G'},
332 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
252b5132 333 {"keep-symbol", required_argument, 0, 'K'},
d7fb0dd2 334 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
d58c2e3a 335 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
d7fb0dd2
NC
336 {"localize-symbol", required_argument, 0, 'L'},
337 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
252b5132
RH
338 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
339 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
ed1653a7 340 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
d7fb0dd2 341 {"only-section", required_argument, 0, 'j'},
252b5132
RH
342 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
343 {"output-target", required_argument, 0, 'O'},
344 {"pad-to", required_argument, 0, OPTION_PAD_TO},
d7fb0dd2
NC
345 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
346 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
347 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
252b5132 348 {"preserve-dates", no_argument, 0, 'p'},
4087920c
MR
349 {"pure", no_argument, 0, OPTION_PURE},
350 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
d7fb0dd2 351 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
92991082 352 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
252b5132
RH
353 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
354 {"remove-section", required_argument, 0, 'R'},
594ef5db 355 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
252b5132
RH
356 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
357 {"set-start", required_argument, 0, OPTION_SET_START},
d7fb0dd2
NC
358 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
359 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
252b5132
RH
360 {"strip-all", no_argument, 0, 'S'},
361 {"strip-debug", no_argument, 0, 'g'},
362 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
bcf32829
JB
363 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
364 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
252b5132 365 {"strip-symbol", required_argument, 0, 'N'},
d7fb0dd2 366 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
252b5132
RH
367 {"target", required_argument, 0, 'F'},
368 {"verbose", no_argument, 0, 'v'},
369 {"version", no_argument, 0, 'V'},
370 {"weaken", no_argument, 0, OPTION_WEAKEN},
371 {"weaken-symbol", required_argument, 0, 'W'},
16b2b71c 372 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
5fe11841 373 {"wildcard", no_argument, 0, 'w'},
4087920c 374 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
252b5132
RH
375 {0, no_argument, 0, 0}
376};
377
378/* IMPORTS */
379extern char *program_name;
380
381/* This flag distinguishes between strip and objcopy:
382 1 means this is 'strip'; 0 means this is 'objcopy'.
0af11b59 383 -1 means if we should use argv[0] to decide. */
252b5132
RH
384extern int is_strip;
385
420496c1
NC
386/* The maximum length of an S record. This variable is declared in srec.c
387 and can be modified by the --srec-len parameter. */
388extern unsigned int Chunk;
389
390/* Restrict the generation of Srecords to type S3 only.
391 This variable is declare in bfd/srec.c and can be toggled
392 on by the --srec-forceS3 command line switch. */
b34976b6 393extern bfd_boolean S3Forced;
252b5132 394
b749473b
NC
395/* Defined in bfd/binary.c. Used to set architecture and machine of input
396 binary files. */
397extern enum bfd_architecture bfd_external_binary_architecture;
398extern unsigned long bfd_external_machine;
43a0748c 399
d3ba0551
AM
400/* Forward declarations. */
401static void setup_section (bfd *, asection *, void *);
80fccad2 402static void setup_bfd_headers (bfd *, bfd *);
d3ba0551
AM
403static void copy_section (bfd *, asection *, void *);
404static void get_sections (bfd *, asection *, void *);
405static int compare_section_lma (const void *, const void *);
406static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
407static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
408static const char *lookup_sym_redefinition (const char *);
594ef5db 409\f
252b5132 410static void
84e2f313 411copy_usage (FILE *stream, int exit_status)
252b5132 412{
8b53311e
NC
413 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
414 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
6364e0b4 415 fprintf (stream, _(" The options are:\n"));
252b5132 416 fprintf (stream, _("\
d5bcb29d
NC
417 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
418 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
43a0748c 419 -B --binary-architecture <arch> Set arch of output file, when input is binary\n\
d5bcb29d
NC
420 -F --target <bfdname> Set both input and output format to <bfdname>\n\
421 --debugging Convert debugging information, if possible\n\
422 -p --preserve-dates Copy modified/access timestamps to the output\n\
423 -j --only-section <name> Only copy section <name> into the output\n\
2593f09a 424 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
d5bcb29d
NC
425 -R --remove-section <name> Remove section <name> from the output\n\
426 -S --strip-all Remove all symbol and relocation information\n\
2593f09a 427 -g --strip-debug Remove all debugging symbols & sections\n\
d5bcb29d
NC
428 --strip-unneeded Remove all symbols not needed by relocations\n\
429 -N --strip-symbol <name> Do not copy symbol <name>\n\
bcf32829
JB
430 --strip-unneeded-symbol <name>\n\
431 Do not copy symbol <name> unless needed by\n\
432 relocations\n\
6ea3dd37 433 --only-keep-debug Strip everything but the debug information\n\
e7f918ad 434 -K --keep-symbol <name> Do not strip symbol <name>\n\
1637cd90 435 --keep-file-symbols Do not strip file symbol(s)\n\
d58c2e3a 436 --localize-hidden Turn all ELF hidden symbols into locals\n\
d5bcb29d 437 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
7b4a0685 438 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
16b2b71c 439 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
d5bcb29d
NC
440 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
441 --weaken Force all global symbols to be marked as weak\n\
a95b5cf9 442 -w --wildcard Permit wildcard in symbol comparison\n\
d5bcb29d
NC
443 -x --discard-all Remove all non-global symbols\n\
444 -X --discard-locals Remove any compiler-generated symbols\n\
445 -i --interleave <number> Only copy one out of every <number> bytes\n\
446 -b --byte <num> Select byte <num> in every interleaved block\n\
447 --gap-fill <val> Fill gaps between sections with <val>\n\
448 --pad-to <addr> Pad the last section up to address <addr>\n\
449 --set-start <addr> Set the start address to <addr>\n\
450 {--change-start|--adjust-start} <incr>\n\
451 Add <incr> to the start address\n\
452 {--change-addresses|--adjust-vma} <incr>\n\
453 Add <incr> to LMA, VMA and start addresses\n\
454 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
455 Change LMA and VMA of section <name> by <val>\n\
456 --change-section-lma <name>{=|+|-}<val>\n\
457 Change the LMA of section <name> by <val>\n\
458 --change-section-vma <name>{=|+|-}<val>\n\
459 Change the VMA of section <name> by <val>\n\
460 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
461 Warn if a named section does not exist\n\
462 --set-section-flags <name>=<flags>\n\
463 Set section <name>'s properties to <flags>\n\
464 --add-section <name>=<file> Add section <name> found in <file> to output\n\
594ef5db 465 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
d5bcb29d
NC
466 --change-leading-char Force output format's leading character style\n\
467 --remove-leading-char Remove leading character from global symbols\n\
57938635 468 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
92991082
JT
469 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
470 listed in <file>\n\
420496c1
NC
471 --srec-len <number> Restrict the length of generated Srecords\n\
472 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
16b2b71c 473 --strip-symbols <file> -N for all symbols listed in <file>\n\
bcf32829
JB
474 --strip-unneeded-symbols <file>\n\
475 --strip-unneeded-symbol for all symbols listed\n\
476 in <file>\n\
16b2b71c
NC
477 --keep-symbols <file> -K for all symbols listed in <file>\n\
478 --localize-symbols <file> -L for all symbols listed in <file>\n\
7b4a0685 479 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
16b2b71c
NC
480 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
481 --weaken-symbols <file> -W for all symbols listed in <file>\n\
f9d4ad2a 482 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
4087920c
MR
483 --writable-text Mark the output text as writable\n\
484 --readonly-text Make the output text write protected\n\
485 --pure Mark the output file as demand paged\n\
486 --impure Mark the output file as impure\n\
d7fb0dd2
NC
487 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
488 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
489 --prefix-alloc-sections <prefix>\n\
490 Add <prefix> to start of every allocatable\n\
491 section name\n\
d5bcb29d 492 -v --verbose List all object files modified\n\
07012eee 493 @<file> Read options from <file>\n\
d5bcb29d
NC
494 -V --version Display this program's version number\n\
495 -h --help Display this output\n\
7c29036b 496 --info List object formats & architectures supported\n\
d5bcb29d 497"));
252b5132
RH
498 list_supported_targets (program_name, stream);
499 if (exit_status == 0)
8ad3436c 500 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
501 exit (exit_status);
502}
503
504static void
84e2f313 505strip_usage (FILE *stream, int exit_status)
252b5132 506{
8b53311e
NC
507 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
508 fprintf (stream, _(" Removes symbols and sections from files\n"));
6364e0b4 509 fprintf (stream, _(" The options are:\n"));
252b5132 510 fprintf (stream, _("\
8b53311e
NC
511 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
512 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
513 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
d5bcb29d 514 -p --preserve-dates Copy modified/access timestamps to the output\n\
8b53311e 515 -R --remove-section=<name> Remove section <name> from the output\n\
d5bcb29d 516 -s --strip-all Remove all symbol and relocation information\n\
2593f09a 517 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
d5bcb29d 518 --strip-unneeded Remove all symbols not needed by relocations\n\
6ea3dd37 519 --only-keep-debug Strip everything but the debug information\n\
8b53311e 520 -N --strip-symbol=<name> Do not copy symbol <name>\n\
5219e4c0 521 -K --keep-symbol=<name> Do not strip symbol <name>\n\
1637cd90 522 --keep-file-symbols Do not strip file symbol(s)\n\
a95b5cf9 523 -w --wildcard Permit wildcard in symbol comparison\n\
d5bcb29d
NC
524 -x --discard-all Remove all non-global symbols\n\
525 -X --discard-locals Remove any compiler-generated symbols\n\
526 -v --verbose List all object files modified\n\
527 -V --version Display this program's version number\n\
528 -h --help Display this output\n\
7c29036b 529 --info List object formats & architectures supported\n\
d5bcb29d
NC
530 -o <file> Place stripped output into <file>\n\
531"));
532
252b5132
RH
533 list_supported_targets (program_name, stream);
534 if (exit_status == 0)
8ad3436c 535 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
536 exit (exit_status);
537}
538
539/* Parse section flags into a flagword, with a fatal error if the
540 string can't be parsed. */
541
542static flagword
84e2f313 543parse_flags (const char *s)
252b5132
RH
544{
545 flagword ret;
546 const char *snext;
547 int len;
548
549 ret = SEC_NO_FLAGS;
550
551 do
552 {
553 snext = strchr (s, ',');
554 if (snext == NULL)
555 len = strlen (s);
556 else
557 {
558 len = snext - s;
559 ++snext;
560 }
561
562 if (0) ;
563#define PARSE_FLAG(fname,fval) \
564 else if (strncasecmp (fname, s, len) == 0) ret |= fval
565 PARSE_FLAG ("alloc", SEC_ALLOC);
566 PARSE_FLAG ("load", SEC_LOAD);
3994e2c6 567 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
252b5132 568 PARSE_FLAG ("readonly", SEC_READONLY);
3994e2c6 569 PARSE_FLAG ("debug", SEC_DEBUGGING);
252b5132
RH
570 PARSE_FLAG ("code", SEC_CODE);
571 PARSE_FLAG ("data", SEC_DATA);
572 PARSE_FLAG ("rom", SEC_ROM);
ebe372c1 573 PARSE_FLAG ("share", SEC_COFF_SHARED);
252b5132
RH
574 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
575#undef PARSE_FLAG
576 else
577 {
578 char *copy;
579
580 copy = xmalloc (len + 1);
581 strncpy (copy, s, len);
582 copy[len] = '\0';
583 non_fatal (_("unrecognized section flag `%s'"), copy);
57938635
AM
584 fatal (_("supported flags: %s"),
585 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
252b5132
RH
586 }
587
588 s = snext;
589 }
590 while (s != NULL);
591
592 return ret;
593}
594
595/* Find and optionally add an entry in the change_sections list. */
596
597static struct section_list *
84e2f313 598find_section_list (const char *name, bfd_boolean add)
252b5132 599{
84e2f313 600 struct section_list *p;
252b5132
RH
601
602 for (p = change_sections; p != NULL; p = p->next)
603 if (strcmp (p->name, name) == 0)
604 return p;
605
606 if (! add)
607 return NULL;
608
d3ba0551 609 p = xmalloc (sizeof (struct section_list));
252b5132 610 p->name = name;
b34976b6
AM
611 p->used = FALSE;
612 p->remove = FALSE;
613 p->copy = FALSE;
252b5132
RH
614 p->change_vma = CHANGE_IGNORE;
615 p->change_lma = CHANGE_IGNORE;
616 p->vma_val = 0;
617 p->lma_val = 0;
b34976b6 618 p->set_flags = FALSE;
252b5132
RH
619 p->flags = 0;
620
621 p->next = change_sections;
622 change_sections = p;
623
624 return p;
625}
626
627/* Add a symbol to strip_specific_list. */
628
57938635 629static void
84e2f313 630add_specific_symbol (const char *name, struct symlist **list)
252b5132
RH
631{
632 struct symlist *tmp_list;
633
d3ba0551 634 tmp_list = xmalloc (sizeof (struct symlist));
252b5132
RH
635 tmp_list->name = name;
636 tmp_list->next = *list;
637 *list = tmp_list;
638}
639
0af11b59 640/* Add symbols listed in `filename' to strip_specific_list. */
16b2b71c
NC
641
642#define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
643#define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
644
645static void
84e2f313 646add_specific_symbols (const char *filename, struct symlist **list)
16b2b71c 647{
f24ddbdd 648 off_t size;
16b2b71c
NC
649 FILE * f;
650 char * line;
651 char * buffer;
652 unsigned int line_count;
0af11b59 653
f24ddbdd
NC
654 size = get_file_size (filename);
655 if (size == 0)
d68c385b
NC
656 {
657 status = 1;
658 return;
659 }
16b2b71c 660
f24ddbdd 661 buffer = xmalloc (size + 2);
16b2b71c
NC
662 f = fopen (filename, FOPEN_RT);
663 if (f == NULL)
f24ddbdd 664 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
16b2b71c 665
f24ddbdd 666 if (fread (buffer, 1, size, f) == 0 || ferror (f))
16b2b71c
NC
667 fatal (_("%s: fread failed"), filename);
668
669 fclose (f);
f24ddbdd
NC
670 buffer [size] = '\n';
671 buffer [size + 1] = '\0';
16b2b71c
NC
672
673 line_count = 1;
0af11b59 674
16b2b71c
NC
675 for (line = buffer; * line != '\0'; line ++)
676 {
677 char * eol;
678 char * name;
679 char * name_end;
b34976b6 680 int finished = FALSE;
16b2b71c
NC
681
682 for (eol = line;; eol ++)
683 {
684 switch (* eol)
685 {
686 case '\n':
687 * eol = '\0';
688 /* Cope with \n\r. */
689 if (eol[1] == '\r')
690 ++ eol;
b34976b6 691 finished = TRUE;
16b2b71c 692 break;
0af11b59 693
16b2b71c
NC
694 case '\r':
695 * eol = '\0';
696 /* Cope with \r\n. */
697 if (eol[1] == '\n')
698 ++ eol;
b34976b6 699 finished = TRUE;
16b2b71c 700 break;
0af11b59 701
16b2b71c 702 case 0:
b34976b6 703 finished = TRUE;
16b2b71c 704 break;
0af11b59 705
16b2b71c
NC
706 case '#':
707 /* Line comment, Terminate the line here, in case a
708 name is present and then allow the rest of the
709 loop to find the real end of the line. */
710 * eol = '\0';
711 break;
0af11b59 712
16b2b71c
NC
713 default:
714 break;
715 }
716
717 if (finished)
718 break;
719 }
720
721 /* A name may now exist somewhere between 'line' and 'eol'.
722 Strip off leading whitespace and trailing whitespace,
723 then add it to the list. */
724 for (name = line; IS_WHITESPACE (* name); name ++)
725 ;
726 for (name_end = name;
727 (! IS_WHITESPACE (* name_end))
728 && (! IS_LINE_TERMINATOR (* name_end));
0af11b59
KH
729 name_end ++)
730 ;
16b2b71c
NC
731
732 if (! IS_LINE_TERMINATOR (* name_end))
733 {
734 char * extra;
735
736 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
737 ;
738
739 if (! IS_LINE_TERMINATOR (* extra))
d412a550
NC
740 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
741 filename, line_count);
16b2b71c 742 }
0af11b59 743
16b2b71c
NC
744 * name_end = '\0';
745
746 if (name_end > name)
747 add_specific_symbol (name, list);
748
749 /* Advance line pointer to end of line. The 'eol ++' in the for
750 loop above will then advance us to the start of the next line. */
751 line = eol;
752 line_count ++;
753 }
754}
755
252b5132
RH
756/* See whether a symbol should be stripped or kept based on
757 strip_specific_list and keep_symbols. */
758
b34976b6 759static bfd_boolean
84e2f313 760is_specified_symbol (const char *name, struct symlist *list)
252b5132
RH
761{
762 struct symlist *tmp_list;
763
5fe11841
NC
764 if (wildcard)
765 {
766 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
767 if (*(tmp_list->name) != '!')
768 {
769 if (!fnmatch (tmp_list->name, name, 0))
770 return TRUE;
771 }
772 else
773 {
774 if (fnmatch (tmp_list->name + 1, name, 0))
775 return TRUE;
776 }
777 }
778 else
779 {
780 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
781 if (strcmp (name, tmp_list->name) == 0)
782 return TRUE;
783 }
594ef5db 784
b34976b6 785 return FALSE;
252b5132
RH
786}
787
788/* See if a section is being removed. */
789
b34976b6 790static bfd_boolean
84e2f313 791is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
252b5132 792{
2593f09a
NC
793 if (sections_removed || sections_copied)
794 {
795 struct section_list *p;
796
797 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
798
799 if (sections_removed && p != NULL && p->remove)
800 return TRUE;
801 if (sections_copied && (p == NULL || ! p->copy))
802 return TRUE;
803 }
252b5132 804
2593f09a
NC
805 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
806 {
807 if (strip_symbols == STRIP_DEBUG
252b5132
RH
808 || strip_symbols == STRIP_UNNEEDED
809 || strip_symbols == STRIP_ALL
810 || discard_locals == LOCALS_ALL
2593f09a
NC
811 || convert_debugging)
812 return TRUE;
ed1653a7
NC
813
814 if (strip_symbols == STRIP_NONDEBUG)
815 return FALSE;
2593f09a 816 }
f91ea849 817
f0312d39 818 return FALSE;
252b5132
RH
819}
820
d58c2e3a
RS
821/* Return true if SYM is a hidden symbol. */
822
823static bfd_boolean
824is_hidden_symbol (asymbol *sym)
825{
826 elf_symbol_type *elf_sym;
827
828 elf_sym = elf_symbol_from (sym->the_bfd, sym);
829 if (elf_sym != NULL)
830 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
831 {
832 case STV_HIDDEN:
833 case STV_INTERNAL:
834 return TRUE;
835 }
836 return FALSE;
837}
838
252b5132
RH
839/* Choose which symbol entries to copy; put the result in OSYMS.
840 We don't copy in place, because that confuses the relocs.
841 Return the number of symbols to print. */
842
843static unsigned int
84e2f313
NC
844filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
845 asymbol **isyms, long symcount)
252b5132 846{
84e2f313 847 asymbol **from = isyms, **to = osyms;
252b5132 848 long src_count = 0, dst_count = 0;
d8121479
L
849 int relocatable = (abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
850 == HAS_RELOC;
252b5132
RH
851
852 for (; src_count < symcount; src_count++)
853 {
854 asymbol *sym = from[src_count];
855 flagword flags = sym->flags;
d7fb0dd2 856 char *name = (char *) bfd_asymbol_name (sym);
252b5132 857 int keep;
b34976b6 858 bfd_boolean undefined;
d7fb0dd2
NC
859 bfd_boolean rem_leading_char;
860 bfd_boolean add_leading_char;
861
862 undefined = bfd_is_und_section (bfd_get_section (sym));
252b5132 863
57938635
AM
864 if (redefine_sym_list)
865 {
d7fb0dd2 866 char *old_name, *new_name;
57938635 867
d7fb0dd2
NC
868 old_name = (char *) bfd_asymbol_name (sym);
869 new_name = (char *) lookup_sym_redefinition (old_name);
66491ebc
AM
870 bfd_asymbol_name (sym) = new_name;
871 name = new_name;
57938635
AM
872 }
873
d7fb0dd2
NC
874 /* Check if we will remove the current leading character. */
875 rem_leading_char =
876 (name[0] == bfd_get_symbol_leading_char (abfd))
877 && (change_leading_char
878 || (remove_leading_char
879 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
880 || undefined
881 || bfd_is_com_section (bfd_get_section (sym)))));
882
883 /* Check if we will add a new leading character. */
884 add_leading_char =
885 change_leading_char
886 && (bfd_get_symbol_leading_char (obfd) != '\0')
887 && (bfd_get_symbol_leading_char (abfd) == '\0'
888 || (name[0] == bfd_get_symbol_leading_char (abfd)));
889
890 /* Short circuit for change_leading_char if we can do it in-place. */
891 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
892 {
893 name[0] = bfd_get_symbol_leading_char (obfd);
894 bfd_asymbol_name (sym) = name;
895 rem_leading_char = FALSE;
896 add_leading_char = FALSE;
897 }
898
899 /* Remove leading char. */
900 if (rem_leading_char)
66491ebc 901 bfd_asymbol_name (sym) = ++name;
d7fb0dd2
NC
902
903 /* Add new leading char and/or prefix. */
904 if (add_leading_char || prefix_symbols_string)
905 {
906 char *n, *ptr;
907
84e2f313
NC
908 ptr = n = xmalloc (1 + strlen (prefix_symbols_string)
909 + strlen (name) + 1);
d7fb0dd2
NC
910 if (add_leading_char)
911 *ptr++ = bfd_get_symbol_leading_char (obfd);
912
913 if (prefix_symbols_string)
914 {
915 strcpy (ptr, prefix_symbols_string);
916 ptr += strlen (prefix_symbols_string);
917 }
918
919 strcpy (ptr, name);
66491ebc
AM
920 bfd_asymbol_name (sym) = n;
921 name = n;
252b5132
RH
922 }
923
252b5132
RH
924 if (strip_symbols == STRIP_ALL)
925 keep = 0;
926 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
927 || ((flags & BSF_SECTION_SYM) != 0
928 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
929 & BSF_KEEP) != 0))
930 keep = 1;
0af11b59 931 else if (relocatable /* Relocatable file. */
d8121479
L
932 && (flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
933 keep = 1;
16b2b71c
NC
934 else if (bfd_decode_symclass (sym) == 'I')
935 /* Global symbols in $idata sections need to be retained
b34976b6 936 even if relocatable is FALSE. External users of the
16b2b71c
NC
937 library containing the $idata section may reference these
938 symbols. */
af3bdff7 939 keep = 1;
252b5132
RH
940 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
941 || (flags & BSF_WEAK) != 0
24e01a36 942 || undefined
252b5132
RH
943 || bfd_is_com_section (bfd_get_section (sym)))
944 keep = strip_symbols != STRIP_UNNEEDED;
945 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
946 keep = (strip_symbols != STRIP_DEBUG
947 && strip_symbols != STRIP_UNNEEDED
948 && ! convert_debugging);
082b7297 949 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
af3bdff7
NC
950 /* COMDAT sections store special information in local
951 symbols, so we cannot risk stripping any of them. */
952 keep = 1;
252b5132
RH
953 else /* Local symbol. */
954 keep = (strip_symbols != STRIP_UNNEEDED
955 && (discard_locals != LOCALS_ALL
956 && (discard_locals != LOCALS_START_L
957 || ! bfd_is_local_label (abfd, sym))));
958
959 if (keep && is_specified_symbol (name, strip_specific_list))
960 keep = 0;
bcf32829
JB
961 if (keep
962 && !(flags & BSF_KEEP)
963 && is_specified_symbol (name, strip_unneeded_list))
964 keep = 0;
1637cd90
JB
965 if (!keep
966 && ((keep_file_symbols && (flags & BSF_FILE))
967 || is_specified_symbol (name, keep_specific_list)))
252b5132
RH
968 keep = 1;
969 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
970 keep = 0;
e0c60db2 971
7b4a0685 972 if (keep)
252b5132 973 {
7b4a0685
NC
974 if ((flags & BSF_GLOBAL) != 0
975 && (weaken || is_specified_symbol (name, weaken_specific_list)))
976 {
977 sym->flags &= ~ BSF_GLOBAL;
978 sym->flags |= BSF_WEAK;
979 }
252b5132 980
7b4a0685
NC
981 if (!undefined
982 && (flags & (BSF_GLOBAL | BSF_WEAK))
983 && (is_specified_symbol (name, localize_specific_list)
984 || (keepglobal_specific_list != NULL
d58c2e3a
RS
985 && ! is_specified_symbol (name, keepglobal_specific_list))
986 || (localize_hidden && is_hidden_symbol (sym))))
7b4a0685
NC
987 {
988 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
989 sym->flags |= BSF_LOCAL;
990 }
991
992 if (!undefined
993 && (flags & BSF_LOCAL)
994 && is_specified_symbol (name, globalize_specific_list))
995 {
996 sym->flags &= ~ BSF_LOCAL;
997 sym->flags |= BSF_GLOBAL;
998 }
999
1000 to[dst_count++] = sym;
1001 }
252b5132
RH
1002 }
1003
1004 to[dst_count] = NULL;
1005
1006 return dst_count;
1007}
1008
594ef5db
NC
1009/* Find the redefined name of symbol SOURCE. */
1010
57938635 1011static const char *
84e2f313 1012lookup_sym_redefinition (const char *source)
57938635 1013{
57938635
AM
1014 struct redefine_node *list;
1015
57938635 1016 for (list = redefine_sym_list; list != NULL; list = list->next)
594ef5db
NC
1017 if (strcmp (source, list->source) == 0)
1018 return list->target;
1019
1020 return source;
57938635
AM
1021}
1022
594ef5db 1023/* Add a node to a symbol redefine list. */
57938635
AM
1024
1025static void
84e2f313 1026redefine_list_append (const char *cause, const char *source, const char *target)
57938635
AM
1027{
1028 struct redefine_node **p;
1029 struct redefine_node *list;
1030 struct redefine_node *new_node;
1031
1032 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1033 {
1034 if (strcmp (source, list->source) == 0)
594ef5db 1035 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
92991082 1036 cause, source);
57938635
AM
1037
1038 if (strcmp (target, list->target) == 0)
594ef5db 1039 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
92991082 1040 cause, target);
57938635
AM
1041 }
1042
d3ba0551 1043 new_node = xmalloc (sizeof (struct redefine_node));
57938635
AM
1044
1045 new_node->source = strdup (source);
1046 new_node->target = strdup (target);
1047 new_node->next = NULL;
1048
1049 *p = new_node;
1050}
1051
92991082
JT
1052/* Handle the --redefine-syms option. Read lines containing "old new"
1053 from the file, and add them to the symbol redefine list. */
1054
2593f09a 1055static void
84e2f313 1056add_redefine_syms_file (const char *filename)
92991082
JT
1057{
1058 FILE *file;
1059 char *buf;
84e2f313
NC
1060 size_t bufsize;
1061 size_t len;
1062 size_t outsym_off;
92991082
JT
1063 int c, lineno;
1064
1065 file = fopen (filename, "r");
d3ba0551 1066 if (file == NULL)
92991082
JT
1067 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1068 filename, strerror (errno));
1069
1070 bufsize = 100;
d3ba0551 1071 buf = xmalloc (bufsize);
92991082
JT
1072
1073 lineno = 1;
1074 c = getc (file);
1075 len = 0;
1076 outsym_off = 0;
1077 while (c != EOF)
1078 {
1079 /* Collect the input symbol name. */
1080 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1081 {
1082 if (c == '#')
1083 goto comment;
1084 buf[len++] = c;
1085 if (len >= bufsize)
1086 {
1087 bufsize *= 2;
1088 buf = xrealloc (buf, bufsize);
1089 }
1090 c = getc (file);
1091 }
1092 buf[len++] = '\0';
1093 if (c == EOF)
1094 break;
1095
1096 /* Eat white space between the symbol names. */
1097 while (IS_WHITESPACE (c))
1098 c = getc (file);
1099 if (c == '#' || IS_LINE_TERMINATOR (c))
1100 goto comment;
1101 if (c == EOF)
1102 break;
1103
1104 /* Collect the output symbol name. */
1105 outsym_off = len;
1106 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1107 {
1108 if (c == '#')
1109 goto comment;
1110 buf[len++] = c;
1111 if (len >= bufsize)
1112 {
1113 bufsize *= 2;
1114 buf = xrealloc (buf, bufsize);
1115 }
1116 c = getc (file);
1117 }
1118 buf[len++] = '\0';
1119 if (c == EOF)
1120 break;
1121
1122 /* Eat white space at end of line. */
1123 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1124 c = getc (file);
1125 if (c == '#')
1126 goto comment;
1127 /* Handle \r\n. */
1128 if ((c == '\r' && (c = getc (file)) == '\n')
1129 || c == '\n' || c == EOF)
1130 {
1131 end_of_line:
1132 /* Append the redefinition to the list. */
1133 if (buf[0] != '\0')
1134 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1135
1136 lineno++;
1137 len = 0;
1138 outsym_off = 0;
1139 if (c == EOF)
1140 break;
1141 c = getc (file);
1142 continue;
1143 }
1144 else
d412a550 1145 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
92991082
JT
1146 comment:
1147 if (len != 0 && (outsym_off == 0 || outsym_off == len))
d412a550 1148 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
92991082
JT
1149 buf[len++] = '\0';
1150
1151 /* Eat the rest of the line and finish it. */
1152 while (c != '\n' && c != EOF)
1153 c = getc (file);
1154 goto end_of_line;
1155 }
1156
1157 if (len != 0)
d412a550 1158 fatal (_("%s:%d: premature end of file"), filename, lineno);
92991082
JT
1159
1160 free (buf);
1161}
1162
77f762d6
L
1163/* Copy unkown object file IBFD onto OBFD.
1164 Returns TRUE upon success, FALSE otherwise. */
1165
1166static bfd_boolean
1167copy_unknown_object (bfd *ibfd, bfd *obfd)
1168{
1169 char *cbuf;
1170 int tocopy;
1171 long ncopied;
1172 long size;
1173 struct stat buf;
1174
1175 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1176 {
1177 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1178 return FALSE;
1179 }
1180
1181 size = buf.st_size;
1182 if (size < 0)
1183 {
1184 non_fatal (_("stat returns negative size for `%s'"),
1185 bfd_get_archive_filename (ibfd));
1186 return FALSE;
1187 }
1188
1189 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1190 {
1191 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1192 return FALSE;
1193 }
1194
1195 if (verbose)
1196 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1197 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1198
1199 cbuf = xmalloc (BUFSIZE);
1200 ncopied = 0;
1201 while (ncopied < size)
1202 {
1203 tocopy = size - ncopied;
1204 if (tocopy > BUFSIZE)
1205 tocopy = BUFSIZE;
1206
1207 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1208 != (bfd_size_type) tocopy)
1209 {
1210 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1211 free (cbuf);
1212 return FALSE;
1213 }
1214
1215 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1216 != (bfd_size_type) tocopy)
1217 {
1218 bfd_nonfatal (bfd_get_filename (obfd));
1219 free (cbuf);
1220 return FALSE;
1221 }
1222
1223 ncopied += tocopy;
1224 }
1225
1226 chmod (bfd_get_filename (obfd), buf.st_mode);
1227 free (cbuf);
1228 return TRUE;
1229}
1230
950d48e7 1231/* Copy object file IBFD onto OBFD.
5b8c74e6 1232 Returns TRUE upon success, FALSE otherwise. */
252b5132 1233
950d48e7 1234static bfd_boolean
84e2f313 1235copy_object (bfd *ibfd, bfd *obfd)
252b5132
RH
1236{
1237 bfd_vma start;
1238 long symcount;
1239 asection **osections = NULL;
84e2f313 1240 asection *gnu_debuglink_section = NULL;
252b5132
RH
1241 bfd_size_type *gaps = NULL;
1242 bfd_size_type max_gap = 0;
1243 long symsize;
84e2f313 1244 void *dhandle;
66491ebc
AM
1245 enum bfd_architecture iarch;
1246 unsigned int imach;
252b5132 1247
23719f39
NC
1248 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1249 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1250 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
950d48e7 1251 fatal (_("Unable to change endianness of input file(s)"));
252b5132
RH
1252
1253 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
950d48e7
NC
1254 {
1255 bfd_nonfatal (bfd_get_filename (obfd));
1256 return FALSE;
1257 }
252b5132
RH
1258
1259 if (verbose)
77f762d6
L
1260 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1261 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
252b5132
RH
1262 bfd_get_filename (obfd), bfd_get_target (obfd));
1263
1264 if (set_start_set)
1265 start = set_start;
1266 else
1267 start = bfd_get_start_address (ibfd);
1268 start += change_start;
1269
0af11b59
KH
1270 /* Neither the start address nor the flags
1271 need to be set for a core file. */
4dd67f29
MS
1272 if (bfd_get_format (obfd) != bfd_core)
1273 {
4087920c
MR
1274 flagword flags;
1275
1276 flags = bfd_get_file_flags (ibfd);
1277 flags |= bfd_flags_to_set;
1278 flags &= ~bfd_flags_to_clear;
1279 flags &= bfd_applicable_file_flags (obfd);
1280
4dd67f29 1281 if (!bfd_set_start_address (obfd, start)
4087920c 1282 || !bfd_set_file_flags (obfd, flags))
950d48e7 1283 {
77f762d6 1284 bfd_nonfatal (bfd_get_archive_filename (ibfd));
950d48e7
NC
1285 return FALSE;
1286 }
4dd67f29 1287 }
252b5132 1288
594ef5db 1289 /* Copy architecture of input file to output file. */
66491ebc
AM
1290 iarch = bfd_get_arch (ibfd);
1291 imach = bfd_get_mach (ibfd);
1292 if (!bfd_set_arch_mach (obfd, iarch, imach)
212a3c4d
L
1293 && (ibfd->target_defaulted
1294 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
f57a841a
NC
1295 {
1296 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
77f762d6
L
1297 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1298 bfd_get_archive_filename (ibfd));
f57a841a 1299 else
77f762d6
L
1300 non_fatal (_("Warning: Output file cannot represent architecture `%s'"),
1301 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1302 bfd_get_mach (ibfd)));
1303 return FALSE;
f57a841a 1304 }
57938635 1305
252b5132 1306 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
950d48e7 1307 {
77f762d6 1308 bfd_nonfatal (bfd_get_archive_filename (ibfd));
950d48e7
NC
1309 return FALSE;
1310 }
252b5132
RH
1311
1312 if (isympp)
62d732f5 1313 free (isympp);
57938635 1314
252b5132 1315 if (osympp != isympp)
62d732f5
AM
1316 free (osympp);
1317
1318 isympp = NULL;
1319 osympp = NULL;
252b5132
RH
1320
1321 /* BFD mandates that all output sections be created and sizes set before
1322 any output is done. Thus, we traverse all sections multiple times. */
d3ba0551 1323 bfd_map_over_sections (ibfd, setup_section, obfd);
252b5132 1324
80fccad2
BW
1325 setup_bfd_headers (ibfd, obfd);
1326
252b5132
RH
1327 if (add_sections != NULL)
1328 {
1329 struct section_add *padd;
1330 struct section_list *pset;
1331
1332 for (padd = add_sections; padd != NULL; padd = padd->next)
1333 {
2593f09a
NC
1334 flagword flags;
1335
551b43fd
AM
1336 pset = find_section_list (padd->name, FALSE);
1337 if (pset != NULL)
1338 pset->used = TRUE;
1339
1340 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1341 if (pset != NULL && pset->set_flags)
1342 flags = pset->flags | SEC_HAS_CONTENTS;
1343
c8782eee
NC
1344 /* bfd_make_section_with_flags() does not return very helpful
1345 error codes, so check for the most likely user error first. */
1346 if (bfd_get_section_by_name (obfd, padd->name))
252b5132 1347 {
c8782eee 1348 non_fatal (_("can't add section '%s' - it already exists!"), padd->name);
950d48e7 1349 return FALSE;
252b5132 1350 }
c8782eee
NC
1351 else
1352 {
1353 padd->section = bfd_make_section_with_flags (obfd, padd->name, flags);
1354 if (padd->section == NULL)
1355 {
1356 non_fatal (_("can't create section `%s': %s"),
1357 padd->name, bfd_errmsg (bfd_get_error ()));
1358 return FALSE;
1359 }
1360 }
252b5132 1361
2593f09a 1362 if (! bfd_set_section_size (obfd, padd->section, padd->size))
950d48e7
NC
1363 {
1364 bfd_nonfatal (bfd_get_filename (obfd));
1365 return FALSE;
1366 }
252b5132 1367
2593f09a
NC
1368 if (pset != NULL)
1369 {
1370 if (pset->change_vma != CHANGE_IGNORE)
84e2f313
NC
1371 if (! bfd_set_section_vma (obfd, padd->section,
1372 pset->vma_val))
950d48e7
NC
1373 {
1374 bfd_nonfatal (bfd_get_filename (obfd));
1375 return FALSE;
1376 }
57938635 1377
2593f09a
NC
1378 if (pset->change_lma != CHANGE_IGNORE)
1379 {
1380 padd->section->lma = pset->lma_val;
950d48e7 1381
2593f09a
NC
1382 if (! bfd_set_section_alignment
1383 (obfd, padd->section,
1384 bfd_section_alignment (obfd, padd->section)))
950d48e7
NC
1385 {
1386 bfd_nonfatal (bfd_get_filename (obfd));
1387 return FALSE;
1388 }
252b5132
RH
1389 }
1390 }
1391 }
1392 }
1393
2593f09a
NC
1394 if (gnu_debuglink_filename != NULL)
1395 {
84e2f313
NC
1396 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1397 (obfd, gnu_debuglink_filename);
e7c81c25
NC
1398
1399 if (gnu_debuglink_section == NULL)
950d48e7
NC
1400 {
1401 bfd_nonfatal (gnu_debuglink_filename);
1402 return FALSE;
1403 }
6e2c86ac
NC
1404
1405 /* Special processing for PE format files. We
1406 have no way to distinguish PE from COFF here. */
1407 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1408 {
1409 bfd_vma debuglink_vma;
1410 asection * highest_section;
1411 asection * sec;
1412
1413 /* The PE spec requires that all sections be adjacent and sorted
1414 in ascending order of VMA. It also specifies that debug
1415 sections should be last. This is despite the fact that debug
1416 sections are not loaded into memory and so in theory have no
1417 use for a VMA.
1418
1419 This means that the debuglink section must be given a non-zero
1420 VMA which makes it contiguous with other debug sections. So
1421 walk the current section list, find the section with the
1422 highest VMA and start the debuglink section after that one. */
1423 for (sec = obfd->sections, highest_section = NULL;
1424 sec != NULL;
1425 sec = sec->next)
1426 if (sec->vma > 0
1427 && (highest_section == NULL
1428 || sec->vma > highest_section->vma))
1429 highest_section = sec;
1430
1431 if (highest_section)
1432 debuglink_vma = BFD_ALIGN (highest_section->vma
1433 + highest_section->size,
1434 /* FIXME: We ought to be using
1435 COFF_PAGE_SIZE here or maybe
1436 bfd_get_section_alignment() (if it
1437 was set) but since this is for PE
1438 and we know the required alignment
1439 it is easier just to hard code it. */
1440 0x1000);
1441 else
1442 /* Umm, not sure what to do in this case. */
1443 debuglink_vma = 0x1000;
1444
1445 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1446 }
950d48e7
NC
1447 }
1448
1449 if (bfd_count_sections (obfd) == 0)
1450 {
1451 non_fatal (_("there are no sections to be copied!"));
1452 return FALSE;
2593f09a
NC
1453 }
1454
252b5132
RH
1455 if (gap_fill_set || pad_to_set)
1456 {
1457 asection **set;
1458 unsigned int c, i;
1459
1460 /* We must fill in gaps between the sections and/or we must pad
1461 the last section to a specified address. We do this by
1462 grabbing a list of the sections, sorting them by VMA, and
1463 increasing the section sizes as required to fill the gaps.
1464 We write out the gap contents below. */
1465
1466 c = bfd_count_sections (obfd);
d3ba0551 1467 osections = xmalloc (c * sizeof (asection *));
252b5132 1468 set = osections;
d3ba0551 1469 bfd_map_over_sections (obfd, get_sections, &set);
252b5132
RH
1470
1471 qsort (osections, c, sizeof (asection *), compare_section_lma);
1472
d3ba0551 1473 gaps = xmalloc (c * sizeof (bfd_size_type));
252b5132
RH
1474 memset (gaps, 0, c * sizeof (bfd_size_type));
1475
1476 if (gap_fill_set)
1477 {
1478 for (i = 0; i < c - 1; i++)
1479 {
1480 flagword flags;
1481 bfd_size_type size;
1482 bfd_vma gap_start, gap_stop;
1483
1484 flags = bfd_get_section_flags (obfd, osections[i]);
1485 if ((flags & SEC_HAS_CONTENTS) == 0
1486 || (flags & SEC_LOAD) == 0)
1487 continue;
1488
1489 size = bfd_section_size (obfd, osections[i]);
1490 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1491 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1492 if (gap_start < gap_stop)
1493 {
1494 if (! bfd_set_section_size (obfd, osections[i],
1495 size + (gap_stop - gap_start)))
1496 {
1497 non_fatal (_("Can't fill gap after %s: %s"),
0af11b59
KH
1498 bfd_get_section_name (obfd, osections[i]),
1499 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1500 status = 1;
1501 break;
1502 }
1503 gaps[i] = gap_stop - gap_start;
1504 if (max_gap < gap_stop - gap_start)
1505 max_gap = gap_stop - gap_start;
1506 }
1507 }
1508 }
1509
1510 if (pad_to_set)
1511 {
1512 bfd_vma lma;
1513 bfd_size_type size;
1514
1515 lma = bfd_section_lma (obfd, osections[c - 1]);
1516 size = bfd_section_size (obfd, osections[c - 1]);
1517 if (lma + size < pad_to)
1518 {
1519 if (! bfd_set_section_size (obfd, osections[c - 1],
1520 pad_to - lma))
1521 {
1522 non_fatal (_("Can't add padding to %s: %s"),
0af11b59
KH
1523 bfd_get_section_name (obfd, osections[c - 1]),
1524 bfd_errmsg (bfd_get_error ()));
252b5132
RH
1525 status = 1;
1526 }
1527 else
1528 {
1529 gaps[c - 1] = pad_to - (lma + size);
1530 if (max_gap < pad_to - (lma + size))
1531 max_gap = pad_to - (lma + size);
1532 }
1533 }
1534 }
1535 }
1536
594ef5db
NC
1537 /* Symbol filtering must happen after the output sections
1538 have been created, but before their contents are set. */
252b5132
RH
1539 dhandle = NULL;
1540 symsize = bfd_get_symtab_upper_bound (ibfd);
1541 if (symsize < 0)
950d48e7 1542 {
77f762d6 1543 bfd_nonfatal (bfd_get_archive_filename (ibfd));
950d48e7
NC
1544 return FALSE;
1545 }
57938635 1546
d3ba0551 1547 osympp = isympp = xmalloc (symsize);
252b5132
RH
1548 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1549 if (symcount < 0)
950d48e7
NC
1550 {
1551 bfd_nonfatal (bfd_get_filename (ibfd));
1552 return FALSE;
1553 }
57938635 1554
252b5132
RH
1555 if (convert_debugging)
1556 dhandle = read_debugging_info (ibfd, isympp, symcount);
57938635
AM
1557
1558 if (strip_symbols == STRIP_DEBUG
252b5132
RH
1559 || strip_symbols == STRIP_ALL
1560 || strip_symbols == STRIP_UNNEEDED
ed1653a7 1561 || strip_symbols == STRIP_NONDEBUG
252b5132 1562 || discard_locals != LOCALS_UNDEF
d58c2e3a 1563 || localize_hidden
252b5132
RH
1564 || strip_specific_list != NULL
1565 || keep_specific_list != NULL
1566 || localize_specific_list != NULL
7b4a0685 1567 || globalize_specific_list != NULL
16b2b71c 1568 || keepglobal_specific_list != NULL
252b5132 1569 || weaken_specific_list != NULL
d7fb0dd2 1570 || prefix_symbols_string
252b5132 1571 || sections_removed
f91ea849 1572 || sections_copied
252b5132
RH
1573 || convert_debugging
1574 || change_leading_char
1575 || remove_leading_char
57938635 1576 || redefine_sym_list
252b5132
RH
1577 || weaken)
1578 {
1579 /* Mark symbols used in output relocations so that they
1580 are kept, even if they are local labels or static symbols.
57938635 1581
252b5132
RH
1582 Note we iterate over the input sections examining their
1583 relocations since the relocations for the output sections
1584 haven't been set yet. mark_symbols_used_in_relocations will
1585 ignore input sections which have no corresponding output
1586 section. */
1587 if (strip_symbols != STRIP_ALL)
1588 bfd_map_over_sections (ibfd,
1589 mark_symbols_used_in_relocations,
d3ba0551
AM
1590 isympp);
1591 osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
252b5132
RH
1592 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1593 }
1594
1595 if (convert_debugging && dhandle != NULL)
1596 {
1597 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1598 {
1599 status = 1;
950d48e7 1600 return FALSE;
252b5132
RH
1601 }
1602 }
1603
1604 bfd_set_symtab (obfd, osympp, symcount);
1605
1606 /* This has to happen after the symbol table has been set. */
d3ba0551 1607 bfd_map_over_sections (ibfd, copy_section, obfd);
252b5132
RH
1608
1609 if (add_sections != NULL)
1610 {
1611 struct section_add *padd;
1612
1613 for (padd = add_sections; padd != NULL; padd = padd->next)
1614 {
d3ba0551
AM
1615 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1616 0, padd->size))
950d48e7
NC
1617 {
1618 bfd_nonfatal (bfd_get_filename (obfd));
1619 return FALSE;
1620 }
252b5132
RH
1621 }
1622 }
1623
e7c81c25
NC
1624 if (gnu_debuglink_filename != NULL)
1625 {
1626 if (! bfd_fill_in_gnu_debuglink_section
1627 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
950d48e7
NC
1628 {
1629 bfd_nonfatal (gnu_debuglink_filename);
1630 return FALSE;
1631 }
e7c81c25
NC
1632 }
1633
252b5132
RH
1634 if (gap_fill_set || pad_to_set)
1635 {
1636 bfd_byte *buf;
1637 int c, i;
1638
1639 /* Fill in the gaps. */
252b5132
RH
1640 if (max_gap > 8192)
1641 max_gap = 8192;
d3ba0551
AM
1642 buf = xmalloc (max_gap);
1643 memset (buf, gap_fill, max_gap);
252b5132
RH
1644
1645 c = bfd_count_sections (obfd);
1646 for (i = 0; i < c; i++)
1647 {
1648 if (gaps[i] != 0)
1649 {
1650 bfd_size_type left;
1651 file_ptr off;
1652
1653 left = gaps[i];
1654 off = bfd_section_size (obfd, osections[i]) - left;
594ef5db 1655
252b5132
RH
1656 while (left > 0)
1657 {
1658 bfd_size_type now;
1659
1660 if (left > 8192)
1661 now = 8192;
1662 else
1663 now = left;
1664
1665 if (! bfd_set_section_contents (obfd, osections[i], buf,
1666 off, now))
950d48e7
NC
1667 {
1668 bfd_nonfatal (bfd_get_filename (obfd));
1669 return FALSE;
1670 }
252b5132
RH
1671
1672 left -= now;
1673 off += now;
1674 }
1675 }
1676 }
1677 }
1678
1679 /* Allow the BFD backend to copy any private data it understands
1680 from the input BFD to the output BFD. This is done last to
1681 permit the routine to look at the filtered symbol table, which is
1682 important for the ECOFF code at least. */
ed1653a7
NC
1683 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
1684 && strip_symbols == STRIP_NONDEBUG)
1685 /* Do not copy the private data when creating an ELF format
1686 debug info file. We do not want the program headers. */
1687 ;
1688 else if (! bfd_copy_private_bfd_data (ibfd, obfd))
252b5132
RH
1689 {
1690 non_fatal (_("%s: error copying private BFD data: %s"),
1691 bfd_get_filename (obfd),
1692 bfd_errmsg (bfd_get_error ()));
950d48e7 1693 return FALSE;
252b5132 1694 }
1ae8b3d2
AO
1695
1696 /* Switch to the alternate machine code. We have to do this at the
1697 very end, because we only initialize the header when we create
1698 the first section. */
f9d4ad2a
NC
1699 if (use_alt_mach_code != 0)
1700 {
1701 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
1702 {
1703 non_fatal (_("this target does not support %lu alternative machine codes"),
1704 use_alt_mach_code);
1705 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1706 {
1707 non_fatal (_("treating that number as an absolute e_machine value instead"));
1708 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
1709 }
1710 else
1711 non_fatal (_("ignoring the alternative value"));
1712 }
1713 }
950d48e7
NC
1714
1715 return TRUE;
252b5132
RH
1716}
1717
4c168fa3
AM
1718#undef MKDIR
1719#if defined (_WIN32) && !defined (__CYGWIN32__)
1720#define MKDIR(DIR, MODE) mkdir (DIR)
1721#else
1722#define MKDIR(DIR, MODE) mkdir (DIR, MODE)
1723#endif
1724
252b5132
RH
1725/* Read each archive element in turn from IBFD, copy the
1726 contents to temp file, and keep the temp file handle. */
1727
1728static void
84e2f313 1729copy_archive (bfd *ibfd, bfd *obfd, const char *output_target)
252b5132
RH
1730{
1731 struct name_list
1732 {
1733 struct name_list *next;
4c168fa3 1734 const char *name;
252b5132
RH
1735 bfd *obfd;
1736 } *list, *l;
1737 bfd **ptr = &obfd->archive_head;
1738 bfd *this_element;
1739 char *dir = make_tempname (bfd_get_filename (obfd));
1740
1741 /* Make a temp directory to hold the contents. */
4c168fa3 1742 if (MKDIR (dir, 0700) != 0)
84e2f313
NC
1743 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1744 dir, strerror (errno));
1745
252b5132
RH
1746 obfd->has_armap = ibfd->has_armap;
1747
1748 list = NULL;
1749
1750 this_element = bfd_openr_next_archived_file (ibfd, NULL);
594ef5db 1751
b667df2e
AM
1752 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1753 RETURN_NONFATAL (bfd_get_filename (obfd));
1754
d3ba0551 1755 while (!status && this_element != NULL)
252b5132 1756 {
4c168fa3
AM
1757 char *output_name;
1758 bfd *output_bfd;
252b5132 1759 bfd *last_element;
8066d1a2
AS
1760 struct stat buf;
1761 int stat_status = 0;
950d48e7 1762 bfd_boolean delete = TRUE;
8066d1a2 1763
4c168fa3
AM
1764 /* Create an output file for this member. */
1765 output_name = concat (dir, "/",
1766 bfd_get_filename (this_element), (char *) 0);
1767
1768 /* If the file already exists, make another temp dir. */
1769 if (stat (output_name, &buf) >= 0)
1770 {
1771 output_name = make_tempname (output_name);
1772 if (MKDIR (output_name, 0700) != 0)
84e2f313
NC
1773 fatal (_("cannot mkdir %s for archive copying (error: %s)"),
1774 output_name, strerror (errno));
1775
d3ba0551 1776 l = xmalloc (sizeof (struct name_list));
4c168fa3
AM
1777 l->name = output_name;
1778 l->next = list;
1779 l->obfd = NULL;
1780 list = l;
1781 output_name = concat (output_name, "/",
1782 bfd_get_filename (this_element), (char *) 0);
1783 }
1784
1785 output_bfd = bfd_openw (output_name, output_target);
8066d1a2
AS
1786 if (preserve_dates)
1787 {
1788 stat_status = bfd_stat_arch_elt (this_element, &buf);
594ef5db 1789
8066d1a2
AS
1790 if (stat_status != 0)
1791 non_fatal (_("internal stat error on %s"),
1792 bfd_get_filename (this_element));
1793 }
252b5132 1794
d3ba0551 1795 l = xmalloc (sizeof (struct name_list));
252b5132
RH
1796 l->name = output_name;
1797 l->next = list;
bee59fd2 1798 l->obfd = NULL;
252b5132
RH
1799 list = l;
1800
d3ba0551 1801 if (output_bfd == NULL)
252b5132
RH
1802 RETURN_NONFATAL (output_name);
1803
b34976b6 1804 if (bfd_check_format (this_element, bfd_object))
77f762d6
L
1805 {
1806 delete = ! copy_object (this_element, output_bfd);
252b5132 1807
77f762d6
L
1808 if (! delete
1809 || bfd_get_arch (this_element) != bfd_arch_unknown)
1810 {
1811 if (!bfd_close (output_bfd))
1812 {
1813 bfd_nonfatal (bfd_get_filename (output_bfd));
1814 /* Error in new object file. Don't change archive. */
1815 status = 1;
1816 }
1817 }
1818 else
1819 goto copy_unknown_element;
1820 }
1821 else
252b5132 1822 {
77f762d6
L
1823 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1824 bfd_get_archive_filename (this_element));
1825
1826copy_unknown_element:
1827 delete = !copy_unknown_object (this_element, output_bfd);
1828 if (!bfd_close_all_done (output_bfd))
1829 {
1830 bfd_nonfatal (bfd_get_filename (output_bfd));
1831 /* Error in new object file. Don't change archive. */
1832 status = 1;
1833 }
252b5132
RH
1834 }
1835
950d48e7
NC
1836 if (delete)
1837 {
1838 unlink (output_name);
1839 status = 1;
1840 }
1841 else
1842 {
1843 if (preserve_dates && stat_status == 0)
1844 set_times (output_name, &buf);
8066d1a2 1845
950d48e7
NC
1846 /* Open the newly output file and attach to our list. */
1847 output_bfd = bfd_openr (output_name, output_target);
252b5132 1848
950d48e7 1849 l->obfd = output_bfd;
252b5132 1850
950d48e7
NC
1851 *ptr = output_bfd;
1852 ptr = &output_bfd->next;
252b5132 1853
950d48e7 1854 last_element = this_element;
252b5132 1855
950d48e7 1856 this_element = bfd_openr_next_archived_file (ibfd, last_element);
252b5132 1857
950d48e7
NC
1858 bfd_close (last_element);
1859 }
252b5132 1860 }
d3ba0551 1861 *ptr = NULL;
252b5132
RH
1862
1863 if (!bfd_close (obfd))
1864 RETURN_NONFATAL (bfd_get_filename (obfd));
1865
1866 if (!bfd_close (ibfd))
1867 RETURN_NONFATAL (bfd_get_filename (ibfd));
1868
1869 /* Delete all the files that we opened. */
1870 for (l = list; l != NULL; l = l->next)
1871 {
4c168fa3
AM
1872 if (l->obfd == NULL)
1873 rmdir (l->name);
1874 else
1875 {
1876 bfd_close (l->obfd);
1877 unlink (l->name);
1878 }
252b5132
RH
1879 }
1880 rmdir (dir);
1881}
1882
1883/* The top-level control. */
1884
1885static void
84e2f313
NC
1886copy_file (const char *input_filename, const char *output_filename,
1887 const char *input_target, const char *output_target)
252b5132
RH
1888{
1889 bfd *ibfd;
49c12576
AM
1890 char **obj_matching;
1891 char **core_matching;
252b5132 1892
f24ddbdd
NC
1893 if (get_file_size (input_filename) < 1)
1894 {
1895 status = 1;
1896 return;
1897 }
1898
252b5132
RH
1899 /* To allow us to do "strip *" without dying on the first
1900 non-object file, failures are nonfatal. */
252b5132
RH
1901 ibfd = bfd_openr (input_filename, input_target);
1902 if (ibfd == NULL)
1903 RETURN_NONFATAL (input_filename);
1904
1905 if (bfd_check_format (ibfd, bfd_archive))
1906 {
1907 bfd *obfd;
1908
1909 /* bfd_get_target does not return the correct value until
1910 bfd_check_format succeeds. */
1911 if (output_target == NULL)
1912 output_target = bfd_get_target (ibfd);
1913
1914 obfd = bfd_openw (output_filename, output_target);
1915 if (obfd == NULL)
1916 RETURN_NONFATAL (output_filename);
1917
1918 copy_archive (ibfd, obfd, output_target);
1919 }
49c12576 1920 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
252b5132
RH
1921 {
1922 bfd *obfd;
49c12576 1923 do_copy:
950d48e7 1924
252b5132
RH
1925 /* bfd_get_target does not return the correct value until
1926 bfd_check_format succeeds. */
1927 if (output_target == NULL)
1928 output_target = bfd_get_target (ibfd);
1929
1930 obfd = bfd_openw (output_filename, output_target);
1931 if (obfd == NULL)
1932 RETURN_NONFATAL (output_filename);
1933
a580b8e0
JB
1934 if (! copy_object (ibfd, obfd))
1935 status = 1;
252b5132
RH
1936
1937 if (!bfd_close (obfd))
1938 RETURN_NONFATAL (output_filename);
1939
1940 if (!bfd_close (ibfd))
1941 RETURN_NONFATAL (input_filename);
950d48e7 1942
252b5132
RH
1943 }
1944 else
1945 {
49c12576
AM
1946 bfd_error_type obj_error = bfd_get_error ();
1947 bfd_error_type core_error;
b34976b6 1948
49c12576
AM
1949 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
1950 {
1951 /* This probably can't happen.. */
1952 if (obj_error == bfd_error_file_ambiguously_recognized)
1953 free (obj_matching);
1954 goto do_copy;
1955 }
1956
1957 core_error = bfd_get_error ();
1958 /* Report the object error in preference to the core error. */
1959 if (obj_error != core_error)
1960 bfd_set_error (obj_error);
1961
252b5132 1962 bfd_nonfatal (input_filename);
57938635 1963
49c12576
AM
1964 if (obj_error == bfd_error_file_ambiguously_recognized)
1965 {
1966 list_matching_formats (obj_matching);
1967 free (obj_matching);
1968 }
1969 if (core_error == bfd_error_file_ambiguously_recognized)
252b5132 1970 {
49c12576
AM
1971 list_matching_formats (core_matching);
1972 free (core_matching);
252b5132 1973 }
57938635 1974
252b5132
RH
1975 status = 1;
1976 }
1977}
1978
594ef5db
NC
1979/* Add a name to the section renaming list. */
1980
1981static void
84e2f313
NC
1982add_section_rename (const char * old_name, const char * new_name,
1983 flagword flags)
594ef5db
NC
1984{
1985 section_rename * rename;
1986
1987 /* Check for conflicts first. */
1988 for (rename = section_rename_list; rename != NULL; rename = rename->next)
1989 if (strcmp (rename->old_name, old_name) == 0)
1990 {
1991 /* Silently ignore duplicate definitions. */
1992 if (strcmp (rename->new_name, new_name) == 0
1993 && rename->flags == flags)
1994 return;
0af11b59 1995
594ef5db
NC
1996 fatal (_("Multiple renames of section %s"), old_name);
1997 }
1998
d3ba0551 1999 rename = xmalloc (sizeof (* rename));
594ef5db
NC
2000
2001 rename->old_name = old_name;
2002 rename->new_name = new_name;
2003 rename->flags = flags;
2004 rename->next = section_rename_list;
0af11b59 2005
594ef5db
NC
2006 section_rename_list = rename;
2007}
2008
2009/* Check the section rename list for a new name of the input section
2010 ISECTION. Return the new name if one is found.
2011 Also set RETURNED_FLAGS to the flags to be used for this section. */
2012
2013static const char *
84e2f313
NC
2014find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2015 flagword * returned_flags)
594ef5db
NC
2016{
2017 const char * old_name = bfd_section_name (ibfd, isection);
2018 section_rename * rename;
2019
2020 /* Default to using the flags of the input section. */
2021 * returned_flags = bfd_get_section_flags (ibfd, isection);
2022
2023 for (rename = section_rename_list; rename != NULL; rename = rename->next)
2024 if (strcmp (rename->old_name, old_name) == 0)
2025 {
2026 if (rename->flags != (flagword) -1)
2027 * returned_flags = rename->flags;
2028
2029 return rename->new_name;
2030 }
2031
2032 return old_name;
2033}
2034
80fccad2
BW
2035/* Once each of the sections is copied, we may still need to do some
2036 finalization work for private section headers. Do that here. */
2037
2038static void
2039setup_bfd_headers (bfd *ibfd, bfd *obfd)
2040{
2041 const char *err;
2042
2043 /* Allow the BFD backend to copy any private data it understands
2044 from the input section to the output section. */
2045 if (! bfd_copy_private_header_data (ibfd, obfd))
2046 {
2047 err = _("private header data");
2048 goto loser;
2049 }
2050
2051 /* All went well. */
2052 return;
2053
2054loser:
2055 non_fatal (_("%s: error in %s: %s"),
2056 bfd_get_filename (ibfd),
2057 err, bfd_errmsg (bfd_get_error ()));
2058 status = 1;
2059}
2060
594ef5db
NC
2061/* Create a section in OBFD with the same
2062 name and attributes as ISECTION in IBFD. */
252b5132
RH
2063
2064static void
84e2f313 2065setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
252b5132 2066{
d3ba0551 2067 bfd *obfd = obfdarg;
252b5132
RH
2068 struct section_list *p;
2069 sec_ptr osection;
2070 bfd_size_type size;
2071 bfd_vma vma;
2072 bfd_vma lma;
2073 flagword flags;
1a89cc7d 2074 const char *err;
594ef5db 2075 const char * name;
d7fb0dd2 2076 char *prefix = NULL;
0af11b59 2077
2593f09a 2078 if (is_strip_section (ibfd, isection))
252b5132
RH
2079 return;
2080
b34976b6 2081 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
252b5132 2082 if (p != NULL)
b34976b6 2083 p->used = TRUE;
252b5132 2084
594ef5db
NC
2085 /* Get the, possibly new, name of the output section. */
2086 name = find_section_rename (ibfd, isection, & flags);
0af11b59 2087
d7fb0dd2 2088 /* Prefix sections. */
84e2f313
NC
2089 if ((prefix_alloc_sections_string)
2090 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
d7fb0dd2
NC
2091 prefix = prefix_alloc_sections_string;
2092 else if (prefix_sections_string)
2093 prefix = prefix_sections_string;
2094
2095 if (prefix)
2096 {
2097 char *n;
2098
2099 n = xmalloc (strlen (prefix) + strlen (name) + 1);
2100 strcpy (n, prefix);
2101 strcat (n, name);
2102 name = n;
2103 }
66491ebc 2104
551b43fd
AM
2105 if (p != NULL && p->set_flags)
2106 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2107 else if (strip_symbols == STRIP_NONDEBUG && (flags & SEC_ALLOC) != 0)
2108 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD);
2109
2110 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
57938635 2111
252b5132
RH
2112 if (osection == NULL)
2113 {
1a89cc7d 2114 err = _("making");
252b5132
RH
2115 goto loser;
2116 }
2117
551b43fd
AM
2118 if (strip_symbols == STRIP_NONDEBUG
2119 && obfd->xvec->flavour == bfd_target_elf_flavour
2120 && (flags & SEC_ALLOC) != 0
2121 && (p == NULL || !p->set_flags))
2122 elf_section_type (osection) = SHT_NOBITS;
2123
252b5132
RH
2124 size = bfd_section_size (ibfd, isection);
2125 if (copy_byte >= 0)
2126 size = (size + interleave - 1) / interleave;
2127 if (! bfd_set_section_size (obfd, osection, size))
2128 {
1a89cc7d 2129 err = _("size");
252b5132
RH
2130 goto loser;
2131 }
57938635 2132
252b5132
RH
2133 vma = bfd_section_vma (ibfd, isection);
2134 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2135 vma += p->vma_val;
2136 else if (p != NULL && p->change_vma == CHANGE_SET)
2137 vma = p->vma_val;
2138 else
2139 vma += change_section_address;
57938635 2140
252b5132
RH
2141 if (! bfd_set_section_vma (obfd, osection, vma))
2142 {
1a89cc7d 2143 err = _("vma");
252b5132
RH
2144 goto loser;
2145 }
2146
2147 lma = isection->lma;
2148 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2149 {
2150 if (p->change_lma == CHANGE_MODIFY)
2151 lma += p->lma_val;
2152 else if (p->change_lma == CHANGE_SET)
2153 lma = p->lma_val;
2154 else
2155 abort ();
2156 }
2157 else
2158 lma += change_section_address;
57938635 2159
252b5132
RH
2160 osection->lma = lma;
2161
2162 /* FIXME: This is probably not enough. If we change the LMA we
2163 may have to recompute the header for the file as well. */
b34976b6
AM
2164 if (!bfd_set_section_alignment (obfd,
2165 osection,
2166 bfd_section_alignment (ibfd, isection)))
252b5132 2167 {
1a89cc7d 2168 err = _("alignment");
252b5132
RH
2169 goto loser;
2170 }
2171
bc408b8a
JJ
2172 /* Copy merge entity size. */
2173 osection->entsize = isection->entsize;
2174
252b5132
RH
2175 /* This used to be mangle_section; we do here to avoid using
2176 bfd_get_section_by_name since some formats allow multiple
2177 sections with the same name. */
2178 isection->output_section = osection;
2179 isection->output_offset = 0;
2180
2181 /* Allow the BFD backend to copy any private data it understands
2182 from the input section to the output section. */
ed1653a7
NC
2183 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
2184 && strip_symbols == STRIP_NONDEBUG)
2185 /* Do not copy the private data when creating an ELF format
2186 debug info file. We do not want the program headers. */
2187 ;
2188 else if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
252b5132 2189 {
1a89cc7d 2190 err = _("private data");
252b5132
RH
2191 goto loser;
2192 }
2193
594ef5db 2194 /* All went well. */
252b5132
RH
2195 return;
2196
2197loser:
2198 non_fatal (_("%s: section `%s': error in %s: %s"),
2199 bfd_get_filename (ibfd),
2200 bfd_section_name (ibfd, isection),
2201 err, bfd_errmsg (bfd_get_error ()));
2202 status = 1;
2203}
2204
2205/* Copy the data of input section ISECTION of IBFD
2206 to an output section with the same name in OBFD.
2207 If stripping then don't copy any relocation info. */
2208
2209static void
84e2f313 2210copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
252b5132 2211{
d3ba0551 2212 bfd *obfd = obfdarg;
252b5132
RH
2213 struct section_list *p;
2214 arelent **relpp;
2215 long relcount;
2216 sec_ptr osection;
2217 bfd_size_type size;
2218 long relsize;
dc156bc0 2219 flagword flags;
252b5132 2220
594ef5db
NC
2221 /* If we have already failed earlier on,
2222 do not keep on generating complaints now. */
252b5132
RH
2223 if (status != 0)
2224 return;
57938635 2225
2593f09a 2226 if (is_strip_section (ibfd, isection))
e0c60db2 2227 return;
252b5132 2228
2593f09a 2229 flags = bfd_get_section_flags (ibfd, isection);
dc156bc0
AM
2230 if ((flags & SEC_GROUP) != 0)
2231 return;
2232
252b5132 2233 osection = isection->output_section;
135dfb4a 2234 size = bfd_get_section_size (isection);
252b5132
RH
2235
2236 if (size == 0 || osection == 0)
2237 return;
2238
2593f09a
NC
2239 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2240
0af11b59 2241 /* Core files do not need to be relocated. */
4dd67f29
MS
2242 if (bfd_get_format (obfd) == bfd_core)
2243 relsize = 0;
2244 else
ed570f48
NC
2245 {
2246 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
4dd67f29 2247
ed570f48
NC
2248 if (relsize < 0)
2249 {
2250 /* Do not complain if the target does not support relocations. */
2251 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2252 relsize = 0;
2253 else
2254 RETURN_NONFATAL (bfd_get_filename (ibfd));
2255 }
2256 }
57938635 2257
252b5132 2258 if (relsize == 0)
d3ba0551 2259 bfd_set_reloc (obfd, osection, NULL, 0);
252b5132
RH
2260 else
2261 {
d3ba0551 2262 relpp = xmalloc (relsize);
252b5132
RH
2263 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2264 if (relcount < 0)
2265 RETURN_NONFATAL (bfd_get_filename (ibfd));
57938635 2266
252b5132
RH
2267 if (strip_symbols == STRIP_ALL)
2268 {
2269 /* Remove relocations which are not in
0af11b59 2270 keep_strip_specific_list. */
252b5132
RH
2271 arelent **temp_relpp;
2272 long temp_relcount = 0;
2273 long i;
57938635 2274
d3ba0551 2275 temp_relpp = xmalloc (relsize);
252b5132 2276 for (i = 0; i < relcount; i++)
d3ba0551
AM
2277 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2278 keep_specific_list))
252b5132
RH
2279 temp_relpp [temp_relcount++] = relpp [i];
2280 relcount = temp_relcount;
2281 free (relpp);
2282 relpp = temp_relpp;
2283 }
e0c60db2 2284
d3ba0551 2285 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
f0312d39
JJ
2286 if (relcount == 0)
2287 free (relpp);
252b5132 2288 }
57938635 2289
0af11b59 2290 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
4dd67f29 2291 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
252b5132 2292 {
d3ba0551 2293 void *memhunk = xmalloc (size);
252b5132 2294
d3ba0551 2295 if (!bfd_get_section_contents (ibfd, isection, memhunk, 0, size))
252b5132
RH
2296 RETURN_NONFATAL (bfd_get_filename (ibfd));
2297
57938635 2298 if (copy_byte >= 0)
5e675b72
AM
2299 {
2300 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2f01ffbf 2301 char *from = (char *) memhunk + copy_byte;
5e675b72 2302 char *to = memhunk;
2f01ffbf 2303 char *end = (char *) memhunk + size;
5e675b72
AM
2304
2305 for (; from < end; from += interleave)
2306 *to++ = *from;
2307
2308 size = (size + interleave - 1 - copy_byte) / interleave;
2309 osection->lma /= interleave;
2310 }
252b5132 2311
d3ba0551 2312 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
252b5132
RH
2313 RETURN_NONFATAL (bfd_get_filename (obfd));
2314
2315 free (memhunk);
2316 }
2317 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2318 {
d3ba0551 2319 void *memhunk = xmalloc (size);
252b5132
RH
2320
2321 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2322 flag--they can just remove the section entirely and add it
2323 back again. However, we do permit them to turn on the
2324 SEC_HAS_CONTENTS flag, and take it to mean that the section
2325 contents should be zeroed out. */
2326
2327 memset (memhunk, 0, size);
d3ba0551 2328 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
252b5132
RH
2329 RETURN_NONFATAL (bfd_get_filename (obfd));
2330 free (memhunk);
2331 }
2332}
2333
2334/* Get all the sections. This is used when --gap-fill or --pad-to is
2335 used. */
2336
2337static void
84e2f313 2338get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
252b5132 2339{
d3ba0551 2340 asection ***secppp = secppparg;
252b5132
RH
2341
2342 **secppp = osection;
2343 ++(*secppp);
2344}
2345
2346/* Sort sections by VMA. This is called via qsort, and is used when
2347 --gap-fill or --pad-to is used. We force non loadable or empty
2348 sections to the front, where they are easier to ignore. */
2349
2350static int
84e2f313 2351compare_section_lma (const void *arg1, const void *arg2)
252b5132 2352{
d3ba0551
AM
2353 const asection *const *sec1 = arg1;
2354 const asection *const *sec2 = arg2;
252b5132
RH
2355 flagword flags1, flags2;
2356
2357 /* Sort non loadable sections to the front. */
2358 flags1 = (*sec1)->flags;
2359 flags2 = (*sec2)->flags;
2360 if ((flags1 & SEC_HAS_CONTENTS) == 0
2361 || (flags1 & SEC_LOAD) == 0)
2362 {
2363 if ((flags2 & SEC_HAS_CONTENTS) != 0
2364 && (flags2 & SEC_LOAD) != 0)
2365 return -1;
2366 }
2367 else
2368 {
2369 if ((flags2 & SEC_HAS_CONTENTS) == 0
2370 || (flags2 & SEC_LOAD) == 0)
2371 return 1;
2372 }
2373
2374 /* Sort sections by LMA. */
2375 if ((*sec1)->lma > (*sec2)->lma)
2376 return 1;
2377 else if ((*sec1)->lma < (*sec2)->lma)
2378 return -1;
2379
2380 /* Sort sections with the same LMA by size. */
135dfb4a 2381 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
252b5132 2382 return 1;
135dfb4a 2383 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
252b5132
RH
2384 return -1;
2385
2386 return 0;
2387}
2388
2389/* Mark all the symbols which will be used in output relocations with
2390 the BSF_KEEP flag so that those symbols will not be stripped.
2391
2392 Ignore relocations which will not appear in the output file. */
2393
2394static void
84e2f313 2395mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
252b5132 2396{
d3ba0551 2397 asymbol **symbols = symbolsarg;
252b5132
RH
2398 long relsize;
2399 arelent **relpp;
2400 long relcount, i;
2401
2402 /* Ignore an input section with no corresponding output section. */
2403 if (isection->output_section == NULL)
2404 return;
2405
2406 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2407 if (relsize < 0)
ed570f48
NC
2408 {
2409 /* Do not complain if the target does not support relocations. */
2410 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2411 return;
2412 bfd_fatal (bfd_get_filename (ibfd));
2413 }
252b5132
RH
2414
2415 if (relsize == 0)
2416 return;
2417
d3ba0551 2418 relpp = xmalloc (relsize);
252b5132
RH
2419 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2420 if (relcount < 0)
2421 bfd_fatal (bfd_get_filename (ibfd));
2422
ec5d57d5
NC
2423 /* Examine each symbol used in a relocation. If it's not one of the
2424 special bfd section symbols, then mark it with BSF_KEEP. */
252b5132
RH
2425 for (i = 0; i < relcount; i++)
2426 {
ec5d57d5
NC
2427 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2428 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2429 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2430 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
252b5132
RH
2431 }
2432
2433 if (relpp != NULL)
2434 free (relpp);
2435}
2436
2437/* Write out debugging information. */
2438
b34976b6 2439static bfd_boolean
84e2f313
NC
2440write_debugging_info (bfd *obfd, void *dhandle,
2441 long *symcountp ATTRIBUTE_UNUSED,
2442 asymbol ***symppp ATTRIBUTE_UNUSED)
252b5132
RH
2443{
2444 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2445 return write_ieee_debugging_info (obfd, dhandle);
2446
2447 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2448 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2449 {
2450 bfd_byte *syms, *strings;
2451 bfd_size_type symsize, stringsize;
2452 asection *stabsec, *stabstrsec;
551b43fd 2453 flagword flags;
252b5132
RH
2454
2455 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2456 &symsize, &strings,
2457 &stringsize))
b34976b6 2458 return FALSE;
252b5132 2459
551b43fd
AM
2460 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2461 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2462 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
252b5132
RH
2463 if (stabsec == NULL
2464 || stabstrsec == NULL
2465 || ! bfd_set_section_size (obfd, stabsec, symsize)
2466 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2467 || ! bfd_set_section_alignment (obfd, stabsec, 2)
551b43fd 2468 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
252b5132
RH
2469 {
2470 non_fatal (_("%s: can't create debugging section: %s"),
2471 bfd_get_filename (obfd),
2472 bfd_errmsg (bfd_get_error ()));
b34976b6 2473 return FALSE;
252b5132
RH
2474 }
2475
2476 /* We can get away with setting the section contents now because
2477 the next thing the caller is going to do is copy over the
2478 real sections. We may someday have to split the contents
2479 setting out of this function. */
d3ba0551
AM
2480 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2481 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2482 stringsize))
252b5132
RH
2483 {
2484 non_fatal (_("%s: can't set debugging section contents: %s"),
2485 bfd_get_filename (obfd),
2486 bfd_errmsg (bfd_get_error ()));
b34976b6 2487 return FALSE;
252b5132
RH
2488 }
2489
b34976b6 2490 return TRUE;
252b5132
RH
2491 }
2492
2493 non_fatal (_("%s: don't know how to write debugging information for %s"),
2494 bfd_get_filename (obfd), bfd_get_target (obfd));
b34976b6 2495 return FALSE;
252b5132
RH
2496}
2497
2498static int
84e2f313 2499strip_main (int argc, char *argv[])
252b5132 2500{
7c29036b
NC
2501 char *input_target = NULL;
2502 char *output_target = NULL;
b34976b6 2503 bfd_boolean show_version = FALSE;
7c29036b
NC
2504 bfd_boolean formats_info = FALSE;
2505 int c;
2506 int i;
252b5132
RH
2507 struct section_list *p;
2508 char *output_file = NULL;
2509
5fe11841 2510 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
252b5132
RH
2511 strip_options, (int *) 0)) != EOF)
2512 {
2513 switch (c)
2514 {
2515 case 'I':
2516 input_target = optarg;
2517 break;
2518 case 'O':
2519 output_target = optarg;
2520 break;
2521 case 'F':
2522 input_target = output_target = optarg;
2523 break;
2524 case 'R':
b34976b6
AM
2525 p = find_section_list (optarg, TRUE);
2526 p->remove = TRUE;
2527 sections_removed = TRUE;
252b5132
RH
2528 break;
2529 case 's':
2530 strip_symbols = STRIP_ALL;
2531 break;
2532 case 'S':
2533 case 'g':
db4f6831 2534 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
252b5132
RH
2535 strip_symbols = STRIP_DEBUG;
2536 break;
2537 case OPTION_STRIP_UNNEEDED:
2538 strip_symbols = STRIP_UNNEEDED;
2539 break;
2540 case 'K':
2541 add_specific_symbol (optarg, &keep_specific_list);
2542 break;
2543 case 'N':
2544 add_specific_symbol (optarg, &strip_specific_list);
2545 break;
2546 case 'o':
2547 output_file = optarg;
2548 break;
2549 case 'p':
b34976b6 2550 preserve_dates = TRUE;
252b5132
RH
2551 break;
2552 case 'x':
2553 discard_locals = LOCALS_ALL;
2554 break;
2555 case 'X':
2556 discard_locals = LOCALS_START_L;
2557 break;
2558 case 'v':
b34976b6 2559 verbose = TRUE;
252b5132
RH
2560 break;
2561 case 'V':
b34976b6 2562 show_version = TRUE;
252b5132 2563 break;
7c29036b
NC
2564 case OPTION_FORMATS_INFO:
2565 formats_info = TRUE;
2566 break;
ed1653a7
NC
2567 case OPTION_ONLY_KEEP_DEBUG:
2568 strip_symbols = STRIP_NONDEBUG;
2569 break;
1637cd90
JB
2570 case OPTION_KEEP_FILE_SYMBOLS:
2571 keep_file_symbols = 1;
2572 break;
252b5132 2573 case 0:
594ef5db
NC
2574 /* We've been given a long option. */
2575 break;
5fe11841
NC
2576 case 'w':
2577 wildcard = TRUE;
2578 break;
8b53311e 2579 case 'H':
252b5132
RH
2580 case 'h':
2581 strip_usage (stdout, 0);
2582 default:
2583 strip_usage (stderr, 1);
2584 }
2585 }
2586
84e2f313
NC
2587 if (formats_info)
2588 {
2589 display_info ();
2590 return 0;
2591 }
7c29036b 2592
252b5132
RH
2593 if (show_version)
2594 print_version ("strip");
2595
2596 /* Default is to strip all symbols. */
2597 if (strip_symbols == STRIP_UNDEF
2598 && discard_locals == LOCALS_UNDEF
2599 && strip_specific_list == NULL)
2600 strip_symbols = STRIP_ALL;
2601
d3ba0551 2602 if (output_target == NULL)
252b5132
RH
2603 output_target = input_target;
2604
2605 i = optind;
2606 if (i == argc
2607 || (output_file != NULL && (i + 1) < argc))
2608 strip_usage (stderr, 1);
2609
2610 for (; i < argc; i++)
2611 {
2612 int hold_status = status;
2613 struct stat statbuf;
2614 char *tmpname;
2615
f24ddbdd 2616 if (get_file_size (argv[i]) < 1)
d68c385b
NC
2617 {
2618 status = 1;
2619 continue;
2620 }
f24ddbdd 2621
252b5132 2622 if (preserve_dates)
f24ddbdd
NC
2623 /* No need to check the return value of stat().
2624 It has already been checked in get_file_size(). */
2625 stat (argv[i], &statbuf);
252b5132
RH
2626
2627 if (output_file != NULL)
2628 tmpname = output_file;
2629 else
2630 tmpname = make_tempname (argv[i]);
252b5132 2631
d68c385b 2632 status = 0;
252b5132
RH
2633 copy_file (argv[i], tmpname, input_target, output_target);
2634 if (status == 0)
2635 {
2636 if (preserve_dates)
2637 set_times (tmpname, &statbuf);
2638 if (output_file == NULL)
2639 smart_rename (tmpname, argv[i], preserve_dates);
2640 status = hold_status;
2641 }
2642 else
bb14f524 2643 unlink_if_ordinary (tmpname);
252b5132
RH
2644 if (output_file == NULL)
2645 free (tmpname);
2646 }
2647
d68c385b 2648 return status;
252b5132
RH
2649}
2650
2651static int
84e2f313 2652copy_main (int argc, char *argv[])
252b5132 2653{
43a0748c 2654 char * binary_architecture = NULL;
7c29036b
NC
2655 char *input_filename = NULL;
2656 char *output_filename = NULL;
2657 char *input_target = NULL;
2658 char *output_target = NULL;
b34976b6
AM
2659 bfd_boolean show_version = FALSE;
2660 bfd_boolean change_warn = TRUE;
7c29036b 2661 bfd_boolean formats_info = FALSE;
252b5132
RH
2662 int c;
2663 struct section_list *p;
2664 struct stat statbuf;
2665
5fe11841 2666 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
252b5132
RH
2667 copy_options, (int *) 0)) != EOF)
2668 {
2669 switch (c)
2670 {
2671 case 'b':
2672 copy_byte = atoi (optarg);
2673 if (copy_byte < 0)
2674 fatal (_("byte number must be non-negative"));
2675 break;
57938635 2676
0af11b59
KH
2677 case 'B':
2678 binary_architecture = optarg;
2679 break;
43a0748c 2680
252b5132
RH
2681 case 'i':
2682 interleave = atoi (optarg);
2683 if (interleave < 1)
2684 fatal (_("interleave must be positive"));
2685 break;
57938635 2686
252b5132
RH
2687 case 'I':
2688 case 's': /* "source" - 'I' is preferred */
2689 input_target = optarg;
2690 break;
57938635 2691
252b5132
RH
2692 case 'O':
2693 case 'd': /* "destination" - 'O' is preferred */
2694 output_target = optarg;
2695 break;
57938635 2696
252b5132
RH
2697 case 'F':
2698 input_target = output_target = optarg;
2699 break;
57938635 2700
f91ea849 2701 case 'j':
b34976b6 2702 p = find_section_list (optarg, TRUE);
f91ea849
ILT
2703 if (p->remove)
2704 fatal (_("%s both copied and removed"), optarg);
b34976b6
AM
2705 p->copy = TRUE;
2706 sections_copied = TRUE;
f91ea849 2707 break;
57938635 2708
252b5132 2709 case 'R':
b34976b6 2710 p = find_section_list (optarg, TRUE);
f91ea849
ILT
2711 if (p->copy)
2712 fatal (_("%s both copied and removed"), optarg);
b34976b6
AM
2713 p->remove = TRUE;
2714 sections_removed = TRUE;
252b5132 2715 break;
57938635 2716
252b5132
RH
2717 case 'S':
2718 strip_symbols = STRIP_ALL;
2719 break;
57938635 2720
252b5132
RH
2721 case 'g':
2722 strip_symbols = STRIP_DEBUG;
2723 break;
57938635 2724
252b5132
RH
2725 case OPTION_STRIP_UNNEEDED:
2726 strip_symbols = STRIP_UNNEEDED;
2727 break;
57938635 2728
ed1653a7
NC
2729 case OPTION_ONLY_KEEP_DEBUG:
2730 strip_symbols = STRIP_NONDEBUG;
2731 break;
2732
1637cd90
JB
2733 case OPTION_KEEP_FILE_SYMBOLS:
2734 keep_file_symbols = 1;
2735 break;
2736
2593f09a
NC
2737 case OPTION_ADD_GNU_DEBUGLINK:
2738 gnu_debuglink_filename = optarg;
2739 break;
2740
252b5132
RH
2741 case 'K':
2742 add_specific_symbol (optarg, &keep_specific_list);
2743 break;
57938635 2744
252b5132
RH
2745 case 'N':
2746 add_specific_symbol (optarg, &strip_specific_list);
2747 break;
57938635 2748
bcf32829
JB
2749 case OPTION_STRIP_UNNEEDED_SYMBOL:
2750 add_specific_symbol (optarg, &strip_unneeded_list);
2751 break;
2752
252b5132
RH
2753 case 'L':
2754 add_specific_symbol (optarg, &localize_specific_list);
2755 break;
57938635 2756
7b4a0685
NC
2757 case OPTION_GLOBALIZE_SYMBOL:
2758 add_specific_symbol (optarg, &globalize_specific_list);
2759 break;
2760
16b2b71c
NC
2761 case 'G':
2762 add_specific_symbol (optarg, &keepglobal_specific_list);
2763 break;
2764
252b5132
RH
2765 case 'W':
2766 add_specific_symbol (optarg, &weaken_specific_list);
2767 break;
57938635 2768
252b5132 2769 case 'p':
b34976b6 2770 preserve_dates = TRUE;
252b5132 2771 break;
57938635 2772
5fe11841
NC
2773 case 'w':
2774 wildcard = TRUE;
2775 break;
2776
252b5132
RH
2777 case 'x':
2778 discard_locals = LOCALS_ALL;
2779 break;
57938635 2780
252b5132
RH
2781 case 'X':
2782 discard_locals = LOCALS_START_L;
2783 break;
57938635 2784
252b5132 2785 case 'v':
b34976b6 2786 verbose = TRUE;
252b5132 2787 break;
57938635 2788
252b5132 2789 case 'V':
b34976b6 2790 show_version = TRUE;
252b5132 2791 break;
57938635 2792
7c29036b
NC
2793 case OPTION_FORMATS_INFO:
2794 formats_info = TRUE;
2795 break;
2796
252b5132 2797 case OPTION_WEAKEN:
b34976b6 2798 weaken = TRUE;
252b5132 2799 break;
57938635 2800
252b5132
RH
2801 case OPTION_ADD_SECTION:
2802 {
2803 const char *s;
f24ddbdd 2804 off_t size;
252b5132
RH
2805 struct section_add *pa;
2806 int len;
2807 char *name;
2808 FILE *f;
2809
2810 s = strchr (optarg, '=');
57938635 2811
252b5132 2812 if (s == NULL)
57938635 2813 fatal (_("bad format for %s"), "--add-section");
252b5132 2814
f24ddbdd
NC
2815 size = get_file_size (s + 1);
2816 if (size < 1)
d68c385b
NC
2817 {
2818 status = 1;
2819 break;
2820 }
252b5132 2821
d3ba0551 2822 pa = xmalloc (sizeof (struct section_add));
252b5132
RH
2823
2824 len = s - optarg;
d3ba0551 2825 name = xmalloc (len + 1);
252b5132
RH
2826 strncpy (name, optarg, len);
2827 name[len] = '\0';
2828 pa->name = name;
2829
2830 pa->filename = s + 1;
f24ddbdd
NC
2831 pa->size = size;
2832 pa->contents = xmalloc (size);
252b5132 2833
252b5132 2834 f = fopen (pa->filename, FOPEN_RB);
57938635 2835
252b5132 2836 if (f == NULL)
84e2f313
NC
2837 fatal (_("cannot open: %s: %s"),
2838 pa->filename, strerror (errno));
57938635 2839
252b5132
RH
2840 if (fread (pa->contents, 1, pa->size, f) == 0
2841 || ferror (f))
2842 fatal (_("%s: fread failed"), pa->filename);
2843
2844 fclose (f);
2845
2846 pa->next = add_sections;
2847 add_sections = pa;
2848 }
2849 break;
57938635 2850
252b5132
RH
2851 case OPTION_CHANGE_START:
2852 change_start = parse_vma (optarg, "--change-start");
2853 break;
57938635 2854
252b5132
RH
2855 case OPTION_CHANGE_SECTION_ADDRESS:
2856 case OPTION_CHANGE_SECTION_LMA:
2857 case OPTION_CHANGE_SECTION_VMA:
2858 {
2859 const char *s;
2860 int len;
2861 char *name;
b4c96d0d 2862 char *option = NULL;
252b5132 2863 bfd_vma val;
b4c96d0d 2864 enum change_action what = CHANGE_IGNORE;
57938635 2865
252b5132
RH
2866 switch (c)
2867 {
b4c96d0d
ILT
2868 case OPTION_CHANGE_SECTION_ADDRESS:
2869 option = "--change-section-address";
2870 break;
2871 case OPTION_CHANGE_SECTION_LMA:
2872 option = "--change-section-lma";
2873 break;
2874 case OPTION_CHANGE_SECTION_VMA:
2875 option = "--change-section-vma";
2876 break;
252b5132 2877 }
57938635 2878
252b5132
RH
2879 s = strchr (optarg, '=');
2880 if (s == NULL)
2881 {
2882 s = strchr (optarg, '+');
2883 if (s == NULL)
2884 {
2885 s = strchr (optarg, '-');
2886 if (s == NULL)
2887 fatal (_("bad format for %s"), option);
2888 }
2889 }
2890
2891 len = s - optarg;
d3ba0551 2892 name = xmalloc (len + 1);
252b5132
RH
2893 strncpy (name, optarg, len);
2894 name[len] = '\0';
2895
b34976b6 2896 p = find_section_list (name, TRUE);
252b5132
RH
2897
2898 val = parse_vma (s + 1, option);
2899
2900 switch (*s)
2901 {
2902 case '=': what = CHANGE_SET; break;
2903 case '-': val = - val; /* Drop through. */
2904 case '+': what = CHANGE_MODIFY; break;
2905 }
57938635 2906
252b5132
RH
2907 switch (c)
2908 {
2909 case OPTION_CHANGE_SECTION_ADDRESS:
2910 p->change_vma = what;
2911 p->vma_val = val;
2912 /* Drop through. */
57938635 2913
252b5132
RH
2914 case OPTION_CHANGE_SECTION_LMA:
2915 p->change_lma = what;
2916 p->lma_val = val;
2917 break;
57938635 2918
252b5132
RH
2919 case OPTION_CHANGE_SECTION_VMA:
2920 p->change_vma = what;
2921 p->vma_val = val;
2922 break;
2923 }
2924 }
2925 break;
57938635 2926
252b5132
RH
2927 case OPTION_CHANGE_ADDRESSES:
2928 change_section_address = parse_vma (optarg, "--change-addresses");
2929 change_start = change_section_address;
2930 break;
57938635 2931
252b5132 2932 case OPTION_CHANGE_WARNINGS:
b34976b6 2933 change_warn = TRUE;
252b5132 2934 break;
57938635 2935
252b5132 2936 case OPTION_CHANGE_LEADING_CHAR:
b34976b6 2937 change_leading_char = TRUE;
252b5132 2938 break;
57938635 2939
252b5132 2940 case OPTION_DEBUGGING:
b34976b6 2941 convert_debugging = TRUE;
252b5132 2942 break;
57938635 2943
252b5132
RH
2944 case OPTION_GAP_FILL:
2945 {
2946 bfd_vma gap_fill_vma;
2947
2948 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2949 gap_fill = (bfd_byte) gap_fill_vma;
2950 if ((bfd_vma) gap_fill != gap_fill_vma)
2951 {
2952 char buff[20];
57938635 2953
252b5132 2954 sprintf_vma (buff, gap_fill_vma);
57938635 2955
252b5132
RH
2956 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2957 buff, gap_fill);
2958 }
b34976b6 2959 gap_fill_set = TRUE;
252b5132
RH
2960 }
2961 break;
57938635 2962
252b5132 2963 case OPTION_NO_CHANGE_WARNINGS:
b34976b6 2964 change_warn = FALSE;
252b5132 2965 break;
57938635 2966
252b5132
RH
2967 case OPTION_PAD_TO:
2968 pad_to = parse_vma (optarg, "--pad-to");
b34976b6 2969 pad_to_set = TRUE;
252b5132 2970 break;
57938635 2971
252b5132 2972 case OPTION_REMOVE_LEADING_CHAR:
b34976b6 2973 remove_leading_char = TRUE;
252b5132 2974 break;
57938635
AM
2975
2976 case OPTION_REDEFINE_SYM:
2977 {
2978 /* Push this redefinition onto redefine_symbol_list. */
2979
2980 int len;
2981 const char *s;
2982 const char *nextarg;
2983 char *source, *target;
2984
2985 s = strchr (optarg, '=');
2986 if (s == NULL)
594ef5db 2987 fatal (_("bad format for %s"), "--redefine-sym");
57938635
AM
2988
2989 len = s - optarg;
d3ba0551 2990 source = xmalloc (len + 1);
57938635
AM
2991 strncpy (source, optarg, len);
2992 source[len] = '\0';
2993
2994 nextarg = s + 1;
2995 len = strlen (nextarg);
d3ba0551 2996 target = xmalloc (len + 1);
57938635
AM
2997 strcpy (target, nextarg);
2998
92991082 2999 redefine_list_append ("--redefine-sym", source, target);
57938635
AM
3000
3001 free (source);
3002 free (target);
3003 }
3004 break;
3005
92991082
JT
3006 case OPTION_REDEFINE_SYMS:
3007 add_redefine_syms_file (optarg);
3008 break;
3009
252b5132
RH
3010 case OPTION_SET_SECTION_FLAGS:
3011 {
3012 const char *s;
3013 int len;
3014 char *name;
3015
3016 s = strchr (optarg, '=');
3017 if (s == NULL)
57938635 3018 fatal (_("bad format for %s"), "--set-section-flags");
252b5132
RH
3019
3020 len = s - optarg;
d3ba0551 3021 name = xmalloc (len + 1);
252b5132
RH
3022 strncpy (name, optarg, len);
3023 name[len] = '\0';
3024
b34976b6 3025 p = find_section_list (name, TRUE);
252b5132 3026
b34976b6 3027 p->set_flags = TRUE;
252b5132
RH
3028 p->flags = parse_flags (s + 1);
3029 }
3030 break;
57938635 3031
594ef5db
NC
3032 case OPTION_RENAME_SECTION:
3033 {
3034 flagword flags;
3bcfb3e4
AM
3035 const char *eq, *fl;
3036 char *old_name;
3037 char *new_name;
594ef5db
NC
3038 unsigned int len;
3039
3bcfb3e4
AM
3040 eq = strchr (optarg, '=');
3041 if (eq == NULL)
594ef5db
NC
3042 fatal (_("bad format for %s"), "--rename-section");
3043
3bcfb3e4 3044 len = eq - optarg;
594ef5db 3045 if (len == 0)
3bcfb3e4 3046 fatal (_("bad format for %s"), "--rename-section");
594ef5db 3047
d3ba0551 3048 old_name = xmalloc (len + 1);
594ef5db
NC
3049 strncpy (old_name, optarg, len);
3050 old_name[len] = 0;
3051
3bcfb3e4
AM
3052 eq++;
3053 fl = strchr (eq, ',');
3054 if (fl)
594ef5db 3055 {
3bcfb3e4
AM
3056 flags = parse_flags (fl + 1);
3057 len = fl - eq;
594ef5db
NC
3058 }
3059 else
3060 {
594ef5db 3061 flags = -1;
3bcfb3e4 3062 len = strlen (eq);
594ef5db
NC
3063 }
3064
3bcfb3e4
AM
3065 if (len == 0)
3066 fatal (_("bad format for %s"), "--rename-section");
3067
d3ba0551 3068 new_name = xmalloc (len + 1);
3bcfb3e4
AM
3069 strncpy (new_name, eq, len);
3070 new_name[len] = 0;
3071
594ef5db
NC
3072 add_section_rename (old_name, new_name, flags);
3073 }
3074 break;
3075
252b5132
RH
3076 case OPTION_SET_START:
3077 set_start = parse_vma (optarg, "--set-start");
b34976b6 3078 set_start_set = TRUE;
252b5132 3079 break;
57938635 3080
0af11b59
KH
3081 case OPTION_SREC_LEN:
3082 Chunk = parse_vma (optarg, "--srec-len");
3083 break;
420496c1 3084
0af11b59 3085 case OPTION_SREC_FORCES3:
b34976b6 3086 S3Forced = TRUE;
0af11b59 3087 break;
420496c1 3088
16b2b71c
NC
3089 case OPTION_STRIP_SYMBOLS:
3090 add_specific_symbols (optarg, &strip_specific_list);
3091 break;
3092
bcf32829
JB
3093 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3094 add_specific_symbols (optarg, &strip_unneeded_list);
3095 break;
3096
16b2b71c
NC
3097 case OPTION_KEEP_SYMBOLS:
3098 add_specific_symbols (optarg, &keep_specific_list);
3099 break;
3100
d58c2e3a
RS
3101 case OPTION_LOCALIZE_HIDDEN:
3102 localize_hidden = TRUE;
3103 break;
3104
16b2b71c
NC
3105 case OPTION_LOCALIZE_SYMBOLS:
3106 add_specific_symbols (optarg, &localize_specific_list);
3107 break;
3108
7b4a0685
NC
3109 case OPTION_GLOBALIZE_SYMBOLS:
3110 add_specific_symbols (optarg, &globalize_specific_list);
3111 break;
3112
16b2b71c
NC
3113 case OPTION_KEEPGLOBAL_SYMBOLS:
3114 add_specific_symbols (optarg, &keepglobal_specific_list);
3115 break;
3116
3117 case OPTION_WEAKEN_SYMBOLS:
3118 add_specific_symbols (optarg, &weaken_specific_list);
3119 break;
3120
1ae8b3d2 3121 case OPTION_ALT_MACH_CODE:
f9d4ad2a
NC
3122 use_alt_mach_code = strtoul (optarg, NULL, 0);
3123 if (use_alt_mach_code == 0)
3124 fatal (_("unable to parse alternative machine code"));
1ae8b3d2
AO
3125 break;
3126
d7fb0dd2
NC
3127 case OPTION_PREFIX_SYMBOLS:
3128 prefix_symbols_string = optarg;
3129 break;
3130
3131 case OPTION_PREFIX_SECTIONS:
3132 prefix_sections_string = optarg;
3133 break;
3134
3135 case OPTION_PREFIX_ALLOC_SECTIONS:
3136 prefix_alloc_sections_string = optarg;
3137 break;
3138
4087920c
MR
3139 case OPTION_READONLY_TEXT:
3140 bfd_flags_to_set |= WP_TEXT;
3141 bfd_flags_to_clear &= ~WP_TEXT;
3142 break;
3143
3144 case OPTION_WRITABLE_TEXT:
3145 bfd_flags_to_clear |= WP_TEXT;
3146 bfd_flags_to_set &= ~WP_TEXT;
3147 break;
3148
3149 case OPTION_PURE:
3150 bfd_flags_to_set |= D_PAGED;
3151 bfd_flags_to_clear &= ~D_PAGED;
3152 break;
3153
3154 case OPTION_IMPURE:
3155 bfd_flags_to_clear |= D_PAGED;
3156 bfd_flags_to_set &= ~D_PAGED;
3157 break;
3158
252b5132 3159 case 0:
2593f09a
NC
3160 /* We've been given a long option. */
3161 break;
57938635 3162
8b53311e 3163 case 'H':
252b5132
RH
3164 case 'h':
3165 copy_usage (stdout, 0);
57938635 3166
252b5132
RH
3167 default:
3168 copy_usage (stderr, 1);
3169 }
3170 }
3171
7c29036b
NC
3172 if (formats_info)
3173 {
3174 display_info ();
3175 return 0;
3176 }
3177
252b5132
RH
3178 if (show_version)
3179 print_version ("objcopy");
3180
3181 if (copy_byte >= interleave)
3182 fatal (_("byte number must be less than interleave"));
3183
3184 if (optind == argc || optind + 2 < argc)
3185 copy_usage (stderr, 1);
3186
3187 input_filename = argv[optind];
3188 if (optind + 1 < argc)
3189 output_filename = argv[optind + 1];
3190
3191 /* Default is to strip no symbols. */
3192 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3193 strip_symbols = STRIP_NONE;
3194
d3ba0551 3195 if (output_target == NULL)
252b5132
RH
3196 output_target = input_target;
3197
d3ba0551 3198 if (binary_architecture != NULL)
252b5132 3199 {
43a0748c 3200 if (input_target && strcmp (input_target, "binary") == 0)
0af11b59
KH
3201 {
3202 const bfd_arch_info_type * temp_arch_info;
43a0748c
NC
3203
3204 temp_arch_info = bfd_scan_arch (binary_architecture);
3205
0af11b59 3206 if (temp_arch_info != NULL)
b749473b
NC
3207 {
3208 bfd_external_binary_architecture = temp_arch_info->arch;
3209 bfd_external_machine = temp_arch_info->mach;
3210 }
0af11b59
KH
3211 else
3212 fatal (_("architecture %s unknown"), binary_architecture);
3213 }
43a0748c
NC
3214 else
3215 {
3216 non_fatal (_("Warning: input target 'binary' required for binary architecture parameter."));
3217 non_fatal (_(" Argument %s ignored"), binary_architecture);
3218 }
252b5132
RH
3219 }
3220
43a0748c
NC
3221 if (preserve_dates)
3222 if (stat (input_filename, & statbuf) < 0)
f24ddbdd
NC
3223 fatal (_("warning: could not locate '%s'. System error message: %s"),
3224 input_filename, strerror (errno));
43a0748c 3225
0fcdcb91 3226 /* If there is no destination file, or the source and destination files
d3ba0551
AM
3227 are the same, then create a temp and rename the result into the input. */
3228 if (output_filename == NULL || strcmp (input_filename, output_filename) == 0)
252b5132
RH
3229 {
3230 char *tmpname = make_tempname (input_filename);
3231
3232 copy_file (input_filename, tmpname, input_target, output_target);
3233 if (status == 0)
57938635 3234 {
252b5132
RH
3235 if (preserve_dates)
3236 set_times (tmpname, &statbuf);
3237 smart_rename (tmpname, input_filename, preserve_dates);
3238 }
3239 else
3240 unlink (tmpname);
3241 }
3242 else
3243 {
3244 copy_file (input_filename, output_filename, input_target, output_target);
594ef5db 3245
252b5132
RH
3246 if (status == 0 && preserve_dates)
3247 set_times (output_filename, &statbuf);
a580b8e0
JB
3248 else if (status != 0)
3249 unlink_if_ordinary (output_filename);
252b5132
RH
3250 }
3251
3252 if (change_warn)
3253 {
3254 for (p = change_sections; p != NULL; p = p->next)
3255 {
3256 if (! p->used)
3257 {
3258 if (p->change_vma != CHANGE_IGNORE)
3259 {
3260 char buff [20];
3261
3262 sprintf_vma (buff, p->vma_val);
57938635 3263
252b5132 3264 /* xgettext:c-format */
57938635
AM
3265 non_fatal (_("%s %s%c0x%s never used"),
3266 "--change-section-vma",
252b5132
RH
3267 p->name,
3268 p->change_vma == CHANGE_SET ? '=' : '+',
3269 buff);
3270 }
57938635 3271
252b5132
RH
3272 if (p->change_lma != CHANGE_IGNORE)
3273 {
3274 char buff [20];
3275
3276 sprintf_vma (buff, p->lma_val);
57938635 3277
252b5132 3278 /* xgettext:c-format */
57938635
AM
3279 non_fatal (_("%s %s%c0x%s never used"),
3280 "--change-section-lma",
252b5132
RH
3281 p->name,
3282 p->change_lma == CHANGE_SET ? '=' : '+',
3283 buff);
3284 }
3285 }
3286 }
3287 }
3288
3289 return 0;
3290}
3291
3292int
84e2f313 3293main (int argc, char *argv[])
252b5132
RH
3294{
3295#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
3296 setlocale (LC_MESSAGES, "");
3882b010
L
3297#endif
3298#if defined (HAVE_SETLOCALE)
3299 setlocale (LC_CTYPE, "");
252b5132
RH
3300#endif
3301 bindtextdomain (PACKAGE, LOCALEDIR);
3302 textdomain (PACKAGE);
3303
3304 program_name = argv[0];
3305 xmalloc_set_program_name (program_name);
3306
3307 START_PROGRESS (program_name, 0);
3308
869b9d07
MM
3309 expandargv (&argc, &argv);
3310
252b5132
RH
3311 strip_symbols = STRIP_UNDEF;
3312 discard_locals = LOCALS_UNDEF;
3313
3314 bfd_init ();
3315 set_default_bfd_target ();
3316
3317 if (is_strip < 0)
3318 {
3319 int i = strlen (program_name);
5af11cab
AM
3320#ifdef HAVE_DOS_BASED_FILE_SYSTEM
3321 /* Drop the .exe suffix, if any. */
3322 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
3323 {
3324 i -= 4;
3325 program_name[i] = '\0';
3326 }
3327#endif
3328 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
252b5132
RH
3329 }
3330
3331 if (is_strip)
3332 strip_main (argc, argv);
3333 else
3334 copy_main (argc, argv);
3335
3336 END_PROGRESS (program_name);
3337
3338 return status;
3339}
This page took 0.46207 seconds and 4 git commands to generate.