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