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