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