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