]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/objcopy.c
* doc/binutils.texi: Add -D/--enable-deterministic-archives option
[thirdparty/binutils-gdb.git] / binutils / objcopy.c
1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22 \f
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "progress.h"
26 #include "getopt.h"
27 #include "libiberty.h"
28 #include "bucomm.h"
29 #include "budbg.h"
30 #include "filenames.h"
31 #include "fnmatch.h"
32 #include "elf-bfd.h"
33 #include "libbfd.h"
34 #include "coff/internal.h"
35 #include "libcoff.h"
36
37 /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
38 header in generic PE code. */
39 #include "coff/i386.h"
40 #include "coff/pe.h"
41
42 static bfd_vma pe_file_alignment = (bfd_vma) -1;
43 static bfd_vma pe_heap_commit = (bfd_vma) -1;
44 static bfd_vma pe_heap_reserve = (bfd_vma) -1;
45 static bfd_vma pe_image_base = (bfd_vma) -1;
46 static bfd_vma pe_section_alignment = (bfd_vma) -1;
47 static bfd_vma pe_stack_commit = (bfd_vma) -1;
48 static bfd_vma pe_stack_reserve = (bfd_vma) -1;
49 static short pe_subsystem = -1;
50 static short pe_major_subsystem_version = -1;
51 static short pe_minor_subsystem_version = -1;
52
53 struct is_specified_symbol_predicate_data
54 {
55 const char *name;
56 bfd_boolean found;
57 };
58
59 /* A list to support redefine_sym. */
60 struct redefine_node
61 {
62 char *source;
63 char *target;
64 struct redefine_node *next;
65 };
66
67 typedef struct section_rename
68 {
69 const char * old_name;
70 const char * new_name;
71 flagword flags;
72 struct section_rename * next;
73 }
74 section_rename;
75
76 /* List of sections to be renamed. */
77 static section_rename *section_rename_list;
78
79 static asymbol **isympp = NULL; /* Input symbols. */
80 static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
81
82 /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
83 static int copy_byte = -1;
84 static int interleave = 0; /* Initialised to 4 in copy_main(). */
85 static int copy_width = 1;
86
87 static bfd_boolean verbose; /* Print file and target names. */
88 static bfd_boolean preserve_dates; /* Preserve input file timestamp. */
89 static bfd_boolean deterministic; /* Enable deterministic archives. */
90 static int status = 0; /* Exit status. */
91
92 enum strip_action
93 {
94 STRIP_UNDEF,
95 STRIP_NONE, /* Don't strip. */
96 STRIP_DEBUG, /* Strip all debugger symbols. */
97 STRIP_UNNEEDED, /* Strip unnecessary symbols. */
98 STRIP_NONDEBUG, /* Strip everything but debug info. */
99 STRIP_ALL /* Strip all symbols. */
100 };
101
102 /* Which symbols to remove. */
103 static enum strip_action strip_symbols;
104
105 enum locals_action
106 {
107 LOCALS_UNDEF,
108 LOCALS_START_L, /* Discard locals starting with L. */
109 LOCALS_ALL /* Discard all locals. */
110 };
111
112 /* Which local symbols to remove. Overrides STRIP_ALL. */
113 static enum locals_action discard_locals;
114
115 /* What kind of change to perform. */
116 enum change_action
117 {
118 CHANGE_IGNORE,
119 CHANGE_MODIFY,
120 CHANGE_SET
121 };
122
123 /* Structure used to hold lists of sections and actions to take. */
124 struct section_list
125 {
126 struct section_list * next; /* Next section to change. */
127 const char * name; /* Section name. */
128 bfd_boolean used; /* Whether this entry was used. */
129 bfd_boolean remove; /* Whether to remove this section. */
130 bfd_boolean copy; /* Whether to copy this section. */
131 enum change_action change_vma;/* Whether to change or set VMA. */
132 bfd_vma vma_val; /* Amount to change by or set to. */
133 enum change_action change_lma;/* Whether to change or set LMA. */
134 bfd_vma lma_val; /* Amount to change by or set to. */
135 bfd_boolean set_flags; /* Whether to set the section flags. */
136 flagword flags; /* What to set the section flags to. */
137 };
138
139 static struct section_list *change_sections;
140
141 /* TRUE if some sections are to be removed. */
142 static bfd_boolean sections_removed;
143
144 /* TRUE if only some sections are to be copied. */
145 static bfd_boolean sections_copied;
146
147 /* Changes to the start address. */
148 static bfd_vma change_start = 0;
149 static bfd_boolean set_start_set = FALSE;
150 static bfd_vma set_start;
151
152 /* Changes to section addresses. */
153 static bfd_vma change_section_address = 0;
154
155 /* Filling gaps between sections. */
156 static bfd_boolean gap_fill_set = FALSE;
157 static bfd_byte gap_fill = 0;
158
159 /* Pad to a given address. */
160 static bfd_boolean pad_to_set = FALSE;
161 static bfd_vma pad_to;
162
163 /* Use alternative machine code? */
164 static unsigned long use_alt_mach_code = 0;
165
166 /* Output BFD flags user wants to set or clear */
167 static flagword bfd_flags_to_set;
168 static flagword bfd_flags_to_clear;
169
170 /* List of sections to add. */
171 struct section_add
172 {
173 /* Next section to add. */
174 struct section_add *next;
175 /* Name of section to add. */
176 const char *name;
177 /* Name of file holding section contents. */
178 const char *filename;
179 /* Size of file. */
180 size_t size;
181 /* Contents of file. */
182 bfd_byte *contents;
183 /* BFD section, after it has been added. */
184 asection *section;
185 };
186
187 /* List of sections to add to the output BFD. */
188 static struct section_add *add_sections;
189
190 /* If non-NULL the argument to --add-gnu-debuglink.
191 This should be the filename to store in the .gnu_debuglink section. */
192 static const char * gnu_debuglink_filename = NULL;
193
194 /* Whether to convert debugging information. */
195 static bfd_boolean convert_debugging = FALSE;
196
197 /* Whether to compress/decompress DWARF debug sections. */
198 static enum
199 {
200 nothing,
201 compress,
202 decompress
203 } do_debug_sections = nothing;
204
205 /* Whether to change the leading character in symbol names. */
206 static bfd_boolean change_leading_char = FALSE;
207
208 /* Whether to remove the leading character from global symbol names. */
209 static bfd_boolean remove_leading_char = FALSE;
210
211 /* Whether to permit wildcard in symbol comparison. */
212 static bfd_boolean wildcard = FALSE;
213
214 /* True if --localize-hidden is in effect. */
215 static bfd_boolean localize_hidden = FALSE;
216
217 /* List of symbols to strip, keep, localize, keep-global, weaken,
218 or redefine. */
219 static htab_t strip_specific_htab = NULL;
220 static htab_t strip_unneeded_htab = NULL;
221 static htab_t keep_specific_htab = NULL;
222 static htab_t localize_specific_htab = NULL;
223 static htab_t globalize_specific_htab = NULL;
224 static htab_t keepglobal_specific_htab = NULL;
225 static htab_t weaken_specific_htab = NULL;
226 static struct redefine_node *redefine_sym_list = NULL;
227
228 /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
229 static bfd_boolean weaken = FALSE;
230
231 /* If this is TRUE, we retain BSF_FILE symbols. */
232 static bfd_boolean keep_file_symbols = FALSE;
233
234 /* Prefix symbols/sections. */
235 static char *prefix_symbols_string = 0;
236 static char *prefix_sections_string = 0;
237 static char *prefix_alloc_sections_string = 0;
238
239 /* True if --extract-symbol was passed on the command line. */
240 static bfd_boolean extract_symbol = FALSE;
241
242 /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
243 of <reverse_bytes> bytes within each output section. */
244 static int reverse_bytes = 0;
245
246 /* For Coff objects, we may want to allow or disallow long section names,
247 or preserve them where found in the inputs. Debug info relies on them. */
248 enum long_section_name_handling
249 {
250 DISABLE,
251 ENABLE,
252 KEEP
253 };
254
255 /* The default long section handling mode is to preserve them.
256 This is also the only behaviour for 'strip'. */
257 static enum long_section_name_handling long_section_names = KEEP;
258
259 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
260 enum command_line_switch
261 {
262 OPTION_ADD_SECTION=150,
263 OPTION_CHANGE_ADDRESSES,
264 OPTION_CHANGE_LEADING_CHAR,
265 OPTION_CHANGE_START,
266 OPTION_CHANGE_SECTION_ADDRESS,
267 OPTION_CHANGE_SECTION_LMA,
268 OPTION_CHANGE_SECTION_VMA,
269 OPTION_CHANGE_WARNINGS,
270 OPTION_COMPRESS_DEBUG_SECTIONS,
271 OPTION_DEBUGGING,
272 OPTION_DECOMPRESS_DEBUG_SECTIONS,
273 OPTION_GAP_FILL,
274 OPTION_NO_CHANGE_WARNINGS,
275 OPTION_PAD_TO,
276 OPTION_REMOVE_LEADING_CHAR,
277 OPTION_SET_SECTION_FLAGS,
278 OPTION_SET_START,
279 OPTION_STRIP_UNNEEDED,
280 OPTION_WEAKEN,
281 OPTION_REDEFINE_SYM,
282 OPTION_REDEFINE_SYMS,
283 OPTION_SREC_LEN,
284 OPTION_SREC_FORCES3,
285 OPTION_STRIP_SYMBOLS,
286 OPTION_STRIP_UNNEEDED_SYMBOL,
287 OPTION_STRIP_UNNEEDED_SYMBOLS,
288 OPTION_KEEP_SYMBOLS,
289 OPTION_LOCALIZE_HIDDEN,
290 OPTION_LOCALIZE_SYMBOLS,
291 OPTION_LONG_SECTION_NAMES,
292 OPTION_GLOBALIZE_SYMBOL,
293 OPTION_GLOBALIZE_SYMBOLS,
294 OPTION_KEEPGLOBAL_SYMBOLS,
295 OPTION_WEAKEN_SYMBOLS,
296 OPTION_RENAME_SECTION,
297 OPTION_ALT_MACH_CODE,
298 OPTION_PREFIX_SYMBOLS,
299 OPTION_PREFIX_SECTIONS,
300 OPTION_PREFIX_ALLOC_SECTIONS,
301 OPTION_FORMATS_INFO,
302 OPTION_ADD_GNU_DEBUGLINK,
303 OPTION_ONLY_KEEP_DEBUG,
304 OPTION_KEEP_FILE_SYMBOLS,
305 OPTION_READONLY_TEXT,
306 OPTION_WRITABLE_TEXT,
307 OPTION_PURE,
308 OPTION_IMPURE,
309 OPTION_EXTRACT_SYMBOL,
310 OPTION_REVERSE_BYTES,
311 OPTION_FILE_ALIGNMENT,
312 OPTION_HEAP,
313 OPTION_IMAGE_BASE,
314 OPTION_SECTION_ALIGNMENT,
315 OPTION_STACK,
316 OPTION_INTERLEAVE_WIDTH,
317 OPTION_SUBSYSTEM
318 };
319
320 /* Options to handle if running as "strip". */
321
322 static struct option strip_options[] =
323 {
324 {"discard-all", no_argument, 0, 'x'},
325 {"discard-locals", no_argument, 0, 'X'},
326 {"enable-deterministic-archives", no_argument, 0, 'D'},
327 {"format", required_argument, 0, 'F'}, /* Obsolete */
328 {"help", no_argument, 0, 'h'},
329 {"info", no_argument, 0, OPTION_FORMATS_INFO},
330 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
331 {"input-target", required_argument, 0, 'I'},
332 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
333 {"keep-symbol", required_argument, 0, 'K'},
334 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
335 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
336 {"output-target", required_argument, 0, 'O'},
337 {"output-file", required_argument, 0, 'o'},
338 {"preserve-dates", no_argument, 0, 'p'},
339 {"remove-section", required_argument, 0, 'R'},
340 {"strip-all", no_argument, 0, 's'},
341 {"strip-debug", no_argument, 0, 'S'},
342 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
343 {"strip-symbol", required_argument, 0, 'N'},
344 {"target", required_argument, 0, 'F'},
345 {"verbose", no_argument, 0, 'v'},
346 {"version", no_argument, 0, 'V'},
347 {"wildcard", no_argument, 0, 'w'},
348 {0, no_argument, 0, 0}
349 };
350
351 /* Options to handle if running as "objcopy". */
352
353 static struct option copy_options[] =
354 {
355 {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
356 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
357 {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
358 {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
359 {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
360 {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
361 {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
362 {"binary-architecture", required_argument, 0, 'B'},
363 {"byte", required_argument, 0, 'b'},
364 {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
365 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
366 {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
367 {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
368 {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
369 {"change-start", required_argument, 0, OPTION_CHANGE_START},
370 {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
371 {"compress-debug-sections", no_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
372 {"debugging", no_argument, 0, OPTION_DEBUGGING},
373 {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
374 {"discard-all", no_argument, 0, 'x'},
375 {"discard-locals", no_argument, 0, 'X'},
376 {"enable-deterministic-archives", no_argument, 0, 'D'},
377 {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
378 {"format", required_argument, 0, 'F'}, /* Obsolete */
379 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
380 {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
381 {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
382 {"help", no_argument, 0, 'h'},
383 {"impure", no_argument, 0, OPTION_IMPURE},
384 {"info", no_argument, 0, OPTION_FORMATS_INFO},
385 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
386 {"input-target", required_argument, 0, 'I'},
387 {"interleave", optional_argument, 0, 'i'},
388 {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
389 {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
390 {"keep-global-symbol", required_argument, 0, 'G'},
391 {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
392 {"keep-symbol", required_argument, 0, 'K'},
393 {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
394 {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
395 {"localize-symbol", required_argument, 0, 'L'},
396 {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
397 {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
398 {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
399 {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
400 {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
401 {"only-section", required_argument, 0, 'j'},
402 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
403 {"output-target", required_argument, 0, 'O'},
404 {"pad-to", required_argument, 0, OPTION_PAD_TO},
405 {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
406 {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
407 {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
408 {"preserve-dates", no_argument, 0, 'p'},
409 {"pure", no_argument, 0, OPTION_PURE},
410 {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
411 {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
412 {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
413 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
414 {"remove-section", required_argument, 0, 'R'},
415 {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
416 {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
417 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
418 {"set-start", required_argument, 0, OPTION_SET_START},
419 {"srec-len", required_argument, 0, OPTION_SREC_LEN},
420 {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
421 {"strip-all", no_argument, 0, 'S'},
422 {"strip-debug", no_argument, 0, 'g'},
423 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
424 {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
425 {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
426 {"strip-symbol", required_argument, 0, 'N'},
427 {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
428 {"target", required_argument, 0, 'F'},
429 {"verbose", no_argument, 0, 'v'},
430 {"version", no_argument, 0, 'V'},
431 {"weaken", no_argument, 0, OPTION_WEAKEN},
432 {"weaken-symbol", required_argument, 0, 'W'},
433 {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
434 {"wildcard", no_argument, 0, 'w'},
435 {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
436 {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
437 {"heap", required_argument, 0, OPTION_HEAP},
438 {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
439 {"section-alignment", required_argument, 0, OPTION_SECTION_ALIGNMENT},
440 {"stack", required_argument, 0, OPTION_STACK},
441 {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
442 {0, no_argument, 0, 0}
443 };
444
445 /* IMPORTS */
446 extern char *program_name;
447
448 /* This flag distinguishes between strip and objcopy:
449 1 means this is 'strip'; 0 means this is 'objcopy'.
450 -1 means if we should use argv[0] to decide. */
451 extern int is_strip;
452
453 /* The maximum length of an S record. This variable is declared in srec.c
454 and can be modified by the --srec-len parameter. */
455 extern unsigned int Chunk;
456
457 /* Restrict the generation of Srecords to type S3 only.
458 This variable is declare in bfd/srec.c and can be toggled
459 on by the --srec-forceS3 command line switch. */
460 extern bfd_boolean S3Forced;
461
462 /* Forward declarations. */
463 static void setup_section (bfd *, asection *, void *);
464 static void setup_bfd_headers (bfd *, bfd *);
465 static void copy_relocations_in_section (bfd *, asection *, void *);
466 static void copy_section (bfd *, asection *, void *);
467 static void get_sections (bfd *, asection *, void *);
468 static int compare_section_lma (const void *, const void *);
469 static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
470 static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
471 static const char *lookup_sym_redefinition (const char *);
472 \f
473 static void
474 copy_usage (FILE *stream, int exit_status)
475 {
476 fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
477 fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
478 fprintf (stream, _(" The options are:\n"));
479 fprintf (stream, _("\
480 -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
481 -O --output-target <bfdname> Create an output file in format <bfdname>\n\
482 -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
483 -F --target <bfdname> Set both input and output format to <bfdname>\n\
484 --debugging Convert debugging information, if possible\n\
485 -p --preserve-dates Copy modified/access timestamps to the output\n\
486 -D --enable-deterministic-archives\n\
487 Produce deterministic output when stripping archives\n\
488 -j --only-section <name> Only copy section <name> into the output\n\
489 --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
490 -R --remove-section <name> Remove section <name> from the output\n\
491 -S --strip-all Remove all symbol and relocation information\n\
492 -g --strip-debug Remove all debugging symbols & sections\n\
493 --strip-unneeded Remove all symbols not needed by relocations\n\
494 -N --strip-symbol <name> Do not copy symbol <name>\n\
495 --strip-unneeded-symbol <name>\n\
496 Do not copy symbol <name> unless needed by\n\
497 relocations\n\
498 --only-keep-debug Strip everything but the debug information\n\
499 --extract-symbol Remove section contents but keep symbols\n\
500 -K --keep-symbol <name> Do not strip symbol <name>\n\
501 --keep-file-symbols Do not strip file symbol(s)\n\
502 --localize-hidden Turn all ELF hidden symbols into locals\n\
503 -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
504 --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
505 -G --keep-global-symbol <name> Localize all symbols except <name>\n\
506 -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
507 --weaken Force all global symbols to be marked as weak\n\
508 -w --wildcard Permit wildcard in symbol comparison\n\
509 -x --discard-all Remove all non-global symbols\n\
510 -X --discard-locals Remove any compiler-generated symbols\n\
511 -i --interleave [<number>] Only copy N out of every <number> bytes\n\
512 --interleave-width <number> Set N for --interleave\n\
513 -b --byte <num> Select byte <num> in every interleaved block\n\
514 --gap-fill <val> Fill gaps between sections with <val>\n\
515 --pad-to <addr> Pad the last section up to address <addr>\n\
516 --set-start <addr> Set the start address to <addr>\n\
517 {--change-start|--adjust-start} <incr>\n\
518 Add <incr> to the start address\n\
519 {--change-addresses|--adjust-vma} <incr>\n\
520 Add <incr> to LMA, VMA and start addresses\n\
521 {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
522 Change LMA and VMA of section <name> by <val>\n\
523 --change-section-lma <name>{=|+|-}<val>\n\
524 Change the LMA of section <name> by <val>\n\
525 --change-section-vma <name>{=|+|-}<val>\n\
526 Change the VMA of section <name> by <val>\n\
527 {--[no-]change-warnings|--[no-]adjust-warnings}\n\
528 Warn if a named section does not exist\n\
529 --set-section-flags <name>=<flags>\n\
530 Set section <name>'s properties to <flags>\n\
531 --add-section <name>=<file> Add section <name> found in <file> to output\n\
532 --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
533 --long-section-names {enable|disable|keep}\n\
534 Handle long section names in Coff objects.\n\
535 --change-leading-char Force output format's leading character style\n\
536 --remove-leading-char Remove leading character from global symbols\n\
537 --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
538 --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
539 --redefine-syms <file> --redefine-sym for all symbol pairs \n\
540 listed in <file>\n\
541 --srec-len <number> Restrict the length of generated Srecords\n\
542 --srec-forceS3 Restrict the type of generated Srecords to S3\n\
543 --strip-symbols <file> -N for all symbols listed in <file>\n\
544 --strip-unneeded-symbols <file>\n\
545 --strip-unneeded-symbol for all symbols listed\n\
546 in <file>\n\
547 --keep-symbols <file> -K for all symbols listed in <file>\n\
548 --localize-symbols <file> -L for all symbols listed in <file>\n\
549 --globalize-symbols <file> --globalize-symbol for all in <file>\n\
550 --keep-global-symbols <file> -G for all symbols listed in <file>\n\
551 --weaken-symbols <file> -W for all symbols listed in <file>\n\
552 --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
553 --writable-text Mark the output text as writable\n\
554 --readonly-text Make the output text write protected\n\
555 --pure Mark the output file as demand paged\n\
556 --impure Mark the output file as impure\n\
557 --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
558 --prefix-sections <prefix> Add <prefix> to start of every section name\n\
559 --prefix-alloc-sections <prefix>\n\
560 Add <prefix> to start of every allocatable\n\
561 section name\n\
562 --file-alignment <num> Set PE file alignment to <num>\n\
563 --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
564 <commit>\n\
565 --image-base <address> Set PE image base to <address>\n\
566 --section-alignment <num> Set PE section alignment to <num>\n\
567 --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
568 <commit>\n\
569 --subsystem <name>[:<version>]\n\
570 Set PE subsystem to <name> [& <version>]\n\
571 --compress-debug-sections Compress DWARF debug sections using zlib\n\
572 --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
573 -v --verbose List all object files modified\n\
574 @<file> Read options from <file>\n\
575 -V --version Display this program's version number\n\
576 -h --help Display this output\n\
577 --info List object formats & architectures supported\n\
578 "));
579 list_supported_targets (program_name, stream);
580 if (REPORT_BUGS_TO[0] && exit_status == 0)
581 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
582 exit (exit_status);
583 }
584
585 static void
586 strip_usage (FILE *stream, int exit_status)
587 {
588 fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
589 fprintf (stream, _(" Removes symbols and sections from files\n"));
590 fprintf (stream, _(" The options are:\n"));
591 fprintf (stream, _("\
592 -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
593 -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
594 -F --target=<bfdname> Set both input and output format to <bfdname>\n\
595 -p --preserve-dates Copy modified/access timestamps to the output\n\
596 -D --enable-deterministic-archives\n\
597 Produce deterministic output when stripping archives\n\
598 -R --remove-section=<name> Remove section <name> from the output\n\
599 -s --strip-all Remove all symbol and relocation information\n\
600 -g -S -d --strip-debug Remove all debugging symbols & sections\n\
601 --strip-unneeded Remove all symbols not needed by relocations\n\
602 --only-keep-debug Strip everything but the debug information\n\
603 -N --strip-symbol=<name> Do not copy symbol <name>\n\
604 -K --keep-symbol=<name> Do not strip symbol <name>\n\
605 --keep-file-symbols Do not strip file symbol(s)\n\
606 -w --wildcard Permit wildcard in symbol comparison\n\
607 -x --discard-all Remove all non-global symbols\n\
608 -X --discard-locals Remove any compiler-generated symbols\n\
609 -v --verbose List all object files modified\n\
610 -V --version Display this program's version number\n\
611 -h --help Display this output\n\
612 --info List object formats & architectures supported\n\
613 -o <file> Place stripped output into <file>\n\
614 "));
615
616 list_supported_targets (program_name, stream);
617 if (REPORT_BUGS_TO[0] && exit_status == 0)
618 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
619 exit (exit_status);
620 }
621
622 /* Parse section flags into a flagword, with a fatal error if the
623 string can't be parsed. */
624
625 static flagword
626 parse_flags (const char *s)
627 {
628 flagword ret;
629 const char *snext;
630 int len;
631
632 ret = SEC_NO_FLAGS;
633
634 do
635 {
636 snext = strchr (s, ',');
637 if (snext == NULL)
638 len = strlen (s);
639 else
640 {
641 len = snext - s;
642 ++snext;
643 }
644
645 if (0) ;
646 #define PARSE_FLAG(fname,fval) \
647 else if (strncasecmp (fname, s, len) == 0) ret |= fval
648 PARSE_FLAG ("alloc", SEC_ALLOC);
649 PARSE_FLAG ("load", SEC_LOAD);
650 PARSE_FLAG ("noload", SEC_NEVER_LOAD);
651 PARSE_FLAG ("readonly", SEC_READONLY);
652 PARSE_FLAG ("debug", SEC_DEBUGGING);
653 PARSE_FLAG ("code", SEC_CODE);
654 PARSE_FLAG ("data", SEC_DATA);
655 PARSE_FLAG ("rom", SEC_ROM);
656 PARSE_FLAG ("share", SEC_COFF_SHARED);
657 PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
658 #undef PARSE_FLAG
659 else
660 {
661 char *copy;
662
663 copy = (char *) xmalloc (len + 1);
664 strncpy (copy, s, len);
665 copy[len] = '\0';
666 non_fatal (_("unrecognized section flag `%s'"), copy);
667 fatal (_("supported flags: %s"),
668 "alloc, load, noload, readonly, debug, code, data, rom, share, contents");
669 }
670
671 s = snext;
672 }
673 while (s != NULL);
674
675 return ret;
676 }
677
678 /* Find and optionally add an entry in the change_sections list. */
679
680 static struct section_list *
681 find_section_list (const char *name, bfd_boolean add)
682 {
683 struct section_list *p;
684
685 for (p = change_sections; p != NULL; p = p->next)
686 if (strcmp (p->name, name) == 0)
687 return p;
688
689 if (! add)
690 return NULL;
691
692 p = (struct section_list *) xmalloc (sizeof (struct section_list));
693 p->name = name;
694 p->used = FALSE;
695 p->remove = FALSE;
696 p->copy = FALSE;
697 p->change_vma = CHANGE_IGNORE;
698 p->change_lma = CHANGE_IGNORE;
699 p->vma_val = 0;
700 p->lma_val = 0;
701 p->set_flags = FALSE;
702 p->flags = 0;
703
704 p->next = change_sections;
705 change_sections = p;
706
707 return p;
708 }
709
710 /* There is htab_hash_string but no htab_eq_string. Makes sense. */
711
712 static int
713 eq_string (const void *s1, const void *s2)
714 {
715 return strcmp ((const char *) s1, (const char *) s2) == 0;
716 }
717
718 static htab_t
719 create_symbol_htab (void)
720 {
721 return htab_create_alloc (16, htab_hash_string, eq_string, NULL, xcalloc, free);
722 }
723
724 static void
725 create_symbol_htabs (void)
726 {
727 strip_specific_htab = create_symbol_htab ();
728 strip_unneeded_htab = create_symbol_htab ();
729 keep_specific_htab = create_symbol_htab ();
730 localize_specific_htab = create_symbol_htab ();
731 globalize_specific_htab = create_symbol_htab ();
732 keepglobal_specific_htab = create_symbol_htab ();
733 weaken_specific_htab = create_symbol_htab ();
734 }
735
736 /* Add a symbol to strip_specific_list. */
737
738 static void
739 add_specific_symbol (const char *name, htab_t htab)
740 {
741 *htab_find_slot (htab, name, INSERT) = (char *) name;
742 }
743
744 /* Add symbols listed in `filename' to strip_specific_list. */
745
746 #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
747 #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
748
749 static void
750 add_specific_symbols (const char *filename, htab_t htab)
751 {
752 off_t size;
753 FILE * f;
754 char * line;
755 char * buffer;
756 unsigned int line_count;
757
758 size = get_file_size (filename);
759 if (size == 0)
760 {
761 status = 1;
762 return;
763 }
764
765 buffer = (char *) xmalloc (size + 2);
766 f = fopen (filename, FOPEN_RT);
767 if (f == NULL)
768 fatal (_("cannot open '%s': %s"), filename, strerror (errno));
769
770 if (fread (buffer, 1, size, f) == 0 || ferror (f))
771 fatal (_("%s: fread failed"), filename);
772
773 fclose (f);
774 buffer [size] = '\n';
775 buffer [size + 1] = '\0';
776
777 line_count = 1;
778
779 for (line = buffer; * line != '\0'; line ++)
780 {
781 char * eol;
782 char * name;
783 char * name_end;
784 int finished = FALSE;
785
786 for (eol = line;; eol ++)
787 {
788 switch (* eol)
789 {
790 case '\n':
791 * eol = '\0';
792 /* Cope with \n\r. */
793 if (eol[1] == '\r')
794 ++ eol;
795 finished = TRUE;
796 break;
797
798 case '\r':
799 * eol = '\0';
800 /* Cope with \r\n. */
801 if (eol[1] == '\n')
802 ++ eol;
803 finished = TRUE;
804 break;
805
806 case 0:
807 finished = TRUE;
808 break;
809
810 case '#':
811 /* Line comment, Terminate the line here, in case a
812 name is present and then allow the rest of the
813 loop to find the real end of the line. */
814 * eol = '\0';
815 break;
816
817 default:
818 break;
819 }
820
821 if (finished)
822 break;
823 }
824
825 /* A name may now exist somewhere between 'line' and 'eol'.
826 Strip off leading whitespace and trailing whitespace,
827 then add it to the list. */
828 for (name = line; IS_WHITESPACE (* name); name ++)
829 ;
830 for (name_end = name;
831 (! IS_WHITESPACE (* name_end))
832 && (! IS_LINE_TERMINATOR (* name_end));
833 name_end ++)
834 ;
835
836 if (! IS_LINE_TERMINATOR (* name_end))
837 {
838 char * extra;
839
840 for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
841 ;
842
843 if (! IS_LINE_TERMINATOR (* extra))
844 non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
845 filename, line_count);
846 }
847
848 * name_end = '\0';
849
850 if (name_end > name)
851 add_specific_symbol (name, htab);
852
853 /* Advance line pointer to end of line. The 'eol ++' in the for
854 loop above will then advance us to the start of the next line. */
855 line = eol;
856 line_count ++;
857 }
858 }
859
860 /* See whether a symbol should be stripped or kept
861 based on strip_specific_list and keep_symbols. */
862
863 static int
864 is_specified_symbol_predicate (void **slot, void *data)
865 {
866 struct is_specified_symbol_predicate_data *d =
867 (struct is_specified_symbol_predicate_data *) data;
868 const char *slot_name = (char *) *slot;
869
870 if (*slot_name != '!')
871 {
872 if (! fnmatch (slot_name, d->name, 0))
873 {
874 d->found = TRUE;
875 /* Stop traversal. */
876 return 0;
877 }
878 }
879 else
880 {
881 if (fnmatch (slot_name + 1, d->name, 0))
882 {
883 d->found = TRUE;
884 /* Stop traversal. */
885 return 0;
886 }
887 }
888
889 /* Continue traversal. */
890 return 1;
891 }
892
893 static bfd_boolean
894 is_specified_symbol (const char *name, htab_t htab)
895 {
896 if (wildcard)
897 {
898 struct is_specified_symbol_predicate_data data;
899
900 data.name = name;
901 data.found = FALSE;
902
903 htab_traverse (htab, is_specified_symbol_predicate, &data);
904
905 return data.found;
906 }
907
908 return htab_find (htab, name) != NULL;
909 }
910
911 /* Return a pointer to the symbol used as a signature for GROUP. */
912
913 static asymbol *
914 group_signature (asection *group)
915 {
916 bfd *abfd = group->owner;
917 Elf_Internal_Shdr *ghdr;
918
919 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
920 return NULL;
921
922 ghdr = &elf_section_data (group)->this_hdr;
923 if (ghdr->sh_link < elf_numsections (abfd))
924 {
925 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
926 Elf_Internal_Shdr *symhdr = elf_elfsections (abfd) [ghdr->sh_link];
927
928 if (symhdr->sh_type == SHT_SYMTAB
929 && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
930 return isympp[ghdr->sh_info - 1];
931 }
932 return NULL;
933 }
934
935 /* See if a non-group section is being removed. */
936
937 static bfd_boolean
938 is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
939 {
940 if (sections_removed || sections_copied)
941 {
942 struct section_list *p;
943
944 p = find_section_list (bfd_get_section_name (abfd, sec), FALSE);
945
946 if (sections_removed && p != NULL && p->remove)
947 return TRUE;
948 if (sections_copied && (p == NULL || ! p->copy))
949 return TRUE;
950 }
951
952 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0)
953 {
954 if (strip_symbols == STRIP_DEBUG
955 || strip_symbols == STRIP_UNNEEDED
956 || strip_symbols == STRIP_ALL
957 || discard_locals == LOCALS_ALL
958 || convert_debugging)
959 return TRUE;
960
961 if (strip_symbols == STRIP_NONDEBUG)
962 return FALSE;
963 }
964
965 return FALSE;
966 }
967
968 /* See if a section is being removed. */
969
970 static bfd_boolean
971 is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
972 {
973 if (is_strip_section_1 (abfd, sec))
974 return TRUE;
975
976 if ((bfd_get_section_flags (abfd, sec) & SEC_GROUP) != 0)
977 {
978 asymbol *gsym;
979 const char *gname;
980 asection *elt, *first;
981
982 /* PR binutils/3181
983 If we are going to strip the group signature symbol, then
984 strip the group section too. */
985 gsym = group_signature (sec);
986 if (gsym != NULL)
987 gname = gsym->name;
988 else
989 gname = sec->name;
990 if ((strip_symbols == STRIP_ALL
991 && !is_specified_symbol (gname, keep_specific_htab))
992 || is_specified_symbol (gname, strip_specific_htab))
993 return TRUE;
994
995 /* Remove the group section if all members are removed. */
996 first = elt = elf_next_in_group (sec);
997 while (elt != NULL)
998 {
999 if (!is_strip_section_1 (abfd, elt))
1000 return FALSE;
1001 elt = elf_next_in_group (elt);
1002 if (elt == first)
1003 break;
1004 }
1005
1006 return TRUE;
1007 }
1008
1009 return FALSE;
1010 }
1011
1012 /* Return true if SYM is a hidden symbol. */
1013
1014 static bfd_boolean
1015 is_hidden_symbol (asymbol *sym)
1016 {
1017 elf_symbol_type *elf_sym;
1018
1019 elf_sym = elf_symbol_from (sym->the_bfd, sym);
1020 if (elf_sym != NULL)
1021 switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
1022 {
1023 case STV_HIDDEN:
1024 case STV_INTERNAL:
1025 return TRUE;
1026 }
1027 return FALSE;
1028 }
1029
1030 /* Choose which symbol entries to copy; put the result in OSYMS.
1031 We don't copy in place, because that confuses the relocs.
1032 Return the number of symbols to print. */
1033
1034 static unsigned int
1035 filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
1036 asymbol **isyms, long symcount)
1037 {
1038 asymbol **from = isyms, **to = osyms;
1039 long src_count = 0, dst_count = 0;
1040 int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
1041
1042 for (; src_count < symcount; src_count++)
1043 {
1044 asymbol *sym = from[src_count];
1045 flagword flags = sym->flags;
1046 char *name = (char *) bfd_asymbol_name (sym);
1047 bfd_boolean keep;
1048 bfd_boolean used_in_reloc = FALSE;
1049 bfd_boolean undefined;
1050 bfd_boolean rem_leading_char;
1051 bfd_boolean add_leading_char;
1052
1053 undefined = bfd_is_und_section (bfd_get_section (sym));
1054
1055 if (redefine_sym_list)
1056 {
1057 char *old_name, *new_name;
1058
1059 old_name = (char *) bfd_asymbol_name (sym);
1060 new_name = (char *) lookup_sym_redefinition (old_name);
1061 bfd_asymbol_name (sym) = new_name;
1062 name = new_name;
1063 }
1064
1065 /* Check if we will remove the current leading character. */
1066 rem_leading_char =
1067 (name[0] == bfd_get_symbol_leading_char (abfd))
1068 && (change_leading_char
1069 || (remove_leading_char
1070 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1071 || undefined
1072 || bfd_is_com_section (bfd_get_section (sym)))));
1073
1074 /* Check if we will add a new leading character. */
1075 add_leading_char =
1076 change_leading_char
1077 && (bfd_get_symbol_leading_char (obfd) != '\0')
1078 && (bfd_get_symbol_leading_char (abfd) == '\0'
1079 || (name[0] == bfd_get_symbol_leading_char (abfd)));
1080
1081 /* Short circuit for change_leading_char if we can do it in-place. */
1082 if (rem_leading_char && add_leading_char && !prefix_symbols_string)
1083 {
1084 name[0] = bfd_get_symbol_leading_char (obfd);
1085 bfd_asymbol_name (sym) = name;
1086 rem_leading_char = FALSE;
1087 add_leading_char = FALSE;
1088 }
1089
1090 /* Remove leading char. */
1091 if (rem_leading_char)
1092 bfd_asymbol_name (sym) = ++name;
1093
1094 /* Add new leading char and/or prefix. */
1095 if (add_leading_char || prefix_symbols_string)
1096 {
1097 char *n, *ptr;
1098
1099 ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
1100 + strlen (name) + 1);
1101 if (add_leading_char)
1102 *ptr++ = bfd_get_symbol_leading_char (obfd);
1103
1104 if (prefix_symbols_string)
1105 {
1106 strcpy (ptr, prefix_symbols_string);
1107 ptr += strlen (prefix_symbols_string);
1108 }
1109
1110 strcpy (ptr, name);
1111 bfd_asymbol_name (sym) = n;
1112 name = n;
1113 }
1114
1115 if (strip_symbols == STRIP_ALL)
1116 keep = FALSE;
1117 else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
1118 || ((flags & BSF_SECTION_SYM) != 0
1119 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags
1120 & BSF_KEEP) != 0))
1121 {
1122 keep = TRUE;
1123 used_in_reloc = TRUE;
1124 }
1125 else if (relocatable /* Relocatable file. */
1126 && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1127 || bfd_is_com_section (bfd_get_section (sym))))
1128 keep = TRUE;
1129 else if (bfd_decode_symclass (sym) == 'I')
1130 /* Global symbols in $idata sections need to be retained
1131 even if relocatable is FALSE. External users of the
1132 library containing the $idata section may reference these
1133 symbols. */
1134 keep = TRUE;
1135 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
1136 || (flags & BSF_WEAK) != 0
1137 || undefined
1138 || bfd_is_com_section (bfd_get_section (sym)))
1139 keep = strip_symbols != STRIP_UNNEEDED;
1140 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
1141 keep = (strip_symbols != STRIP_DEBUG
1142 && strip_symbols != STRIP_UNNEEDED
1143 && ! convert_debugging);
1144 else if (bfd_coff_get_comdat_section (abfd, bfd_get_section (sym)))
1145 /* COMDAT sections store special information in local
1146 symbols, so we cannot risk stripping any of them. */
1147 keep = TRUE;
1148 else /* Local symbol. */
1149 keep = (strip_symbols != STRIP_UNNEEDED
1150 && (discard_locals != LOCALS_ALL
1151 && (discard_locals != LOCALS_START_L
1152 || ! bfd_is_local_label (abfd, sym))));
1153
1154 if (keep && is_specified_symbol (name, strip_specific_htab))
1155 {
1156 /* There are multiple ways to set 'keep' above, but if it
1157 was the relocatable symbol case, then that's an error. */
1158 if (used_in_reloc)
1159 {
1160 non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
1161 status = 1;
1162 }
1163 else
1164 keep = FALSE;
1165 }
1166
1167 if (keep
1168 && !(flags & BSF_KEEP)
1169 && is_specified_symbol (name, strip_unneeded_htab))
1170 keep = FALSE;
1171
1172 if (!keep
1173 && ((keep_file_symbols && (flags & BSF_FILE))
1174 || is_specified_symbol (name, keep_specific_htab)))
1175 keep = TRUE;
1176
1177 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
1178 keep = FALSE;
1179
1180 if (keep)
1181 {
1182 if ((flags & BSF_GLOBAL) != 0
1183 && (weaken || is_specified_symbol (name, weaken_specific_htab)))
1184 {
1185 sym->flags &= ~ BSF_GLOBAL;
1186 sym->flags |= BSF_WEAK;
1187 }
1188
1189 if (!undefined
1190 && (flags & (BSF_GLOBAL | BSF_WEAK))
1191 && (is_specified_symbol (name, localize_specific_htab)
1192 || (htab_elements (keepglobal_specific_htab) != 0
1193 && ! is_specified_symbol (name, keepglobal_specific_htab))
1194 || (localize_hidden && is_hidden_symbol (sym))))
1195 {
1196 sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
1197 sym->flags |= BSF_LOCAL;
1198 }
1199
1200 if (!undefined
1201 && (flags & BSF_LOCAL)
1202 && is_specified_symbol (name, globalize_specific_htab))
1203 {
1204 sym->flags &= ~ BSF_LOCAL;
1205 sym->flags |= BSF_GLOBAL;
1206 }
1207
1208 to[dst_count++] = sym;
1209 }
1210 }
1211
1212 to[dst_count] = NULL;
1213
1214 return dst_count;
1215 }
1216
1217 /* Find the redefined name of symbol SOURCE. */
1218
1219 static const char *
1220 lookup_sym_redefinition (const char *source)
1221 {
1222 struct redefine_node *list;
1223
1224 for (list = redefine_sym_list; list != NULL; list = list->next)
1225 if (strcmp (source, list->source) == 0)
1226 return list->target;
1227
1228 return source;
1229 }
1230
1231 /* Add a node to a symbol redefine list. */
1232
1233 static void
1234 redefine_list_append (const char *cause, const char *source, const char *target)
1235 {
1236 struct redefine_node **p;
1237 struct redefine_node *list;
1238 struct redefine_node *new_node;
1239
1240 for (p = &redefine_sym_list; (list = *p) != NULL; p = &list->next)
1241 {
1242 if (strcmp (source, list->source) == 0)
1243 fatal (_("%s: Multiple redefinition of symbol \"%s\""),
1244 cause, source);
1245
1246 if (strcmp (target, list->target) == 0)
1247 fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
1248 cause, target);
1249 }
1250
1251 new_node = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
1252
1253 new_node->source = strdup (source);
1254 new_node->target = strdup (target);
1255 new_node->next = NULL;
1256
1257 *p = new_node;
1258 }
1259
1260 /* Handle the --redefine-syms option. Read lines containing "old new"
1261 from the file, and add them to the symbol redefine list. */
1262
1263 static void
1264 add_redefine_syms_file (const char *filename)
1265 {
1266 FILE *file;
1267 char *buf;
1268 size_t bufsize;
1269 size_t len;
1270 size_t outsym_off;
1271 int c, lineno;
1272
1273 file = fopen (filename, "r");
1274 if (file == NULL)
1275 fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
1276 filename, strerror (errno));
1277
1278 bufsize = 100;
1279 buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
1280
1281 lineno = 1;
1282 c = getc (file);
1283 len = 0;
1284 outsym_off = 0;
1285 while (c != EOF)
1286 {
1287 /* Collect the input symbol name. */
1288 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1289 {
1290 if (c == '#')
1291 goto comment;
1292 buf[len++] = c;
1293 if (len >= bufsize)
1294 {
1295 bufsize *= 2;
1296 buf = (char *) xrealloc (buf, bufsize + 1);
1297 }
1298 c = getc (file);
1299 }
1300 buf[len++] = '\0';
1301 if (c == EOF)
1302 break;
1303
1304 /* Eat white space between the symbol names. */
1305 while (IS_WHITESPACE (c))
1306 c = getc (file);
1307 if (c == '#' || IS_LINE_TERMINATOR (c))
1308 goto comment;
1309 if (c == EOF)
1310 break;
1311
1312 /* Collect the output symbol name. */
1313 outsym_off = len;
1314 while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
1315 {
1316 if (c == '#')
1317 goto comment;
1318 buf[len++] = c;
1319 if (len >= bufsize)
1320 {
1321 bufsize *= 2;
1322 buf = (char *) xrealloc (buf, bufsize + 1);
1323 }
1324 c = getc (file);
1325 }
1326 buf[len++] = '\0';
1327 if (c == EOF)
1328 break;
1329
1330 /* Eat white space at end of line. */
1331 while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
1332 c = getc (file);
1333 if (c == '#')
1334 goto comment;
1335 /* Handle \r\n. */
1336 if ((c == '\r' && (c = getc (file)) == '\n')
1337 || c == '\n' || c == EOF)
1338 {
1339 end_of_line:
1340 /* Append the redefinition to the list. */
1341 if (buf[0] != '\0')
1342 redefine_list_append (filename, &buf[0], &buf[outsym_off]);
1343
1344 lineno++;
1345 len = 0;
1346 outsym_off = 0;
1347 if (c == EOF)
1348 break;
1349 c = getc (file);
1350 continue;
1351 }
1352 else
1353 fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
1354 comment:
1355 if (len != 0 && (outsym_off == 0 || outsym_off == len))
1356 fatal (_("%s:%d: missing new symbol name"), filename, lineno);
1357 buf[len++] = '\0';
1358
1359 /* Eat the rest of the line and finish it. */
1360 while (c != '\n' && c != EOF)
1361 c = getc (file);
1362 goto end_of_line;
1363 }
1364
1365 if (len != 0)
1366 fatal (_("%s:%d: premature end of file"), filename, lineno);
1367
1368 free (buf);
1369 }
1370
1371 /* Copy unkown object file IBFD onto OBFD.
1372 Returns TRUE upon success, FALSE otherwise. */
1373
1374 static bfd_boolean
1375 copy_unknown_object (bfd *ibfd, bfd *obfd)
1376 {
1377 char *cbuf;
1378 int tocopy;
1379 long ncopied;
1380 long size;
1381 struct stat buf;
1382
1383 if (bfd_stat_arch_elt (ibfd, &buf) != 0)
1384 {
1385 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1386 return FALSE;
1387 }
1388
1389 size = buf.st_size;
1390 if (size < 0)
1391 {
1392 non_fatal (_("stat returns negative size for `%s'"),
1393 bfd_get_archive_filename (ibfd));
1394 return FALSE;
1395 }
1396
1397 if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
1398 {
1399 bfd_nonfatal (bfd_get_archive_filename (ibfd));
1400 return FALSE;
1401 }
1402
1403 if (verbose)
1404 printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
1405 bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
1406
1407 cbuf = (char *) xmalloc (BUFSIZE);
1408 ncopied = 0;
1409 while (ncopied < size)
1410 {
1411 tocopy = size - ncopied;
1412 if (tocopy > BUFSIZE)
1413 tocopy = BUFSIZE;
1414
1415 if (bfd_bread (cbuf, (bfd_size_type) tocopy, ibfd)
1416 != (bfd_size_type) tocopy)
1417 {
1418 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1419 free (cbuf);
1420 return FALSE;
1421 }
1422
1423 if (bfd_bwrite (cbuf, (bfd_size_type) tocopy, obfd)
1424 != (bfd_size_type) tocopy)
1425 {
1426 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1427 free (cbuf);
1428 return FALSE;
1429 }
1430
1431 ncopied += tocopy;
1432 }
1433
1434 /* We should at least to be able to read it back when copying an
1435 unknown object in an archive. */
1436 chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
1437 free (cbuf);
1438 return TRUE;
1439 }
1440
1441 /* Copy object file IBFD onto OBFD.
1442 Returns TRUE upon success, FALSE otherwise. */
1443
1444 static bfd_boolean
1445 copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
1446 {
1447 bfd_vma start;
1448 long symcount;
1449 asection **osections = NULL;
1450 asection *gnu_debuglink_section = NULL;
1451 bfd_size_type *gaps = NULL;
1452 bfd_size_type max_gap = 0;
1453 long symsize;
1454 void *dhandle;
1455 enum bfd_architecture iarch;
1456 unsigned int imach;
1457
1458 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1459 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1460 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1461 fatal (_("Unable to change endianness of input file(s)"));
1462
1463 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1464 {
1465 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
1466 return FALSE;
1467 }
1468
1469 if (verbose)
1470 printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
1471 bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
1472 bfd_get_filename (obfd), bfd_get_target (obfd));
1473
1474 if (extract_symbol)
1475 start = 0;
1476 else
1477 {
1478 if (set_start_set)
1479 start = set_start;
1480 else
1481 start = bfd_get_start_address (ibfd);
1482 start += change_start;
1483 }
1484
1485 /* Neither the start address nor the flags
1486 need to be set for a core file. */
1487 if (bfd_get_format (obfd) != bfd_core)
1488 {
1489 flagword flags;
1490
1491 flags = bfd_get_file_flags (ibfd);
1492 flags |= bfd_flags_to_set;
1493 flags &= ~bfd_flags_to_clear;
1494 flags &= bfd_applicable_file_flags (obfd);
1495
1496 if (strip_symbols == STRIP_ALL)
1497 flags &= ~HAS_RELOC;
1498
1499 if (!bfd_set_start_address (obfd, start)
1500 || !bfd_set_file_flags (obfd, flags))
1501 {
1502 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1503 return FALSE;
1504 }
1505 }
1506
1507 /* Copy architecture of input file to output file. */
1508 iarch = bfd_get_arch (ibfd);
1509 imach = bfd_get_mach (ibfd);
1510 if (input_arch)
1511 {
1512 if (bfd_get_arch_info (ibfd) == NULL
1513 || bfd_get_arch_info (ibfd)->arch == bfd_arch_unknown)
1514 {
1515 iarch = input_arch->arch;
1516 imach = input_arch->mach;
1517 }
1518 else
1519 non_fatal (_("Input file `%s' ignores binary architecture parameter."),
1520 bfd_get_archive_filename (ibfd));
1521 }
1522 if (!bfd_set_arch_mach (obfd, iarch, imach)
1523 && (ibfd->target_defaulted
1524 || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
1525 {
1526 if (bfd_get_arch (ibfd) == bfd_arch_unknown)
1527 non_fatal (_("Unable to recognise the format of the input file `%s'"),
1528 bfd_get_archive_filename (ibfd));
1529 else
1530 non_fatal (_("Output file cannot represent architecture `%s'"),
1531 bfd_printable_arch_mach (bfd_get_arch (ibfd),
1532 bfd_get_mach (ibfd)));
1533 return FALSE;
1534 }
1535
1536 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
1537 {
1538 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1539 return FALSE;
1540 }
1541
1542 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1543 && bfd_pei_p (obfd))
1544 {
1545 /* Set up PE parameters. */
1546 pe_data_type *pe = pe_data (obfd);
1547
1548 /* Copy PE parameters before changing them. */
1549 if (ibfd->xvec->flavour == bfd_target_coff_flavour
1550 && bfd_pei_p (ibfd))
1551 pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1552
1553 if (pe_file_alignment != (bfd_vma) -1)
1554 pe->pe_opthdr.FileAlignment = pe_file_alignment;
1555 else
1556 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
1557
1558 if (pe_heap_commit != (bfd_vma) -1)
1559 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
1560
1561 if (pe_heap_reserve != (bfd_vma) -1)
1562 pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
1563
1564 if (pe_image_base != (bfd_vma) -1)
1565 pe->pe_opthdr.ImageBase = pe_image_base;
1566
1567 if (pe_section_alignment != (bfd_vma) -1)
1568 pe->pe_opthdr.SectionAlignment = pe_section_alignment;
1569 else
1570 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
1571
1572 if (pe_stack_commit != (bfd_vma) -1)
1573 pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
1574
1575 if (pe_stack_reserve != (bfd_vma) -1)
1576 pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
1577
1578 if (pe_subsystem != -1)
1579 pe->pe_opthdr.Subsystem = pe_subsystem;
1580
1581 if (pe_major_subsystem_version != -1)
1582 pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
1583
1584 if (pe_minor_subsystem_version != -1)
1585 pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
1586
1587 if (pe_file_alignment > pe_section_alignment)
1588 {
1589 char file_alignment[20], section_alignment[20];
1590
1591 sprintf_vma (file_alignment, pe_file_alignment);
1592 sprintf_vma (section_alignment, pe_section_alignment);
1593 non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
1594
1595 file_alignment, section_alignment);
1596 }
1597 }
1598
1599 if (isympp)
1600 free (isympp);
1601
1602 if (osympp != isympp)
1603 free (osympp);
1604
1605 isympp = NULL;
1606 osympp = NULL;
1607
1608 symsize = bfd_get_symtab_upper_bound (ibfd);
1609 if (symsize < 0)
1610 {
1611 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1612 return FALSE;
1613 }
1614
1615 osympp = isympp = (asymbol **) xmalloc (symsize);
1616 symcount = bfd_canonicalize_symtab (ibfd, isympp);
1617 if (symcount < 0)
1618 {
1619 bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
1620 return FALSE;
1621 }
1622
1623 /* BFD mandates that all output sections be created and sizes set before
1624 any output is done. Thus, we traverse all sections multiple times. */
1625 bfd_map_over_sections (ibfd, setup_section, obfd);
1626
1627 if (!extract_symbol)
1628 setup_bfd_headers (ibfd, obfd);
1629
1630 if (add_sections != NULL)
1631 {
1632 struct section_add *padd;
1633 struct section_list *pset;
1634
1635 for (padd = add_sections; padd != NULL; padd = padd->next)
1636 {
1637 flagword flags;
1638
1639 pset = find_section_list (padd->name, FALSE);
1640 if (pset != NULL)
1641 pset->used = TRUE;
1642
1643 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
1644 if (pset != NULL && pset->set_flags)
1645 flags = pset->flags | SEC_HAS_CONTENTS;
1646
1647 /* bfd_make_section_with_flags() does not return very helpful
1648 error codes, so check for the most likely user error first. */
1649 if (bfd_get_section_by_name (obfd, padd->name))
1650 {
1651 bfd_nonfatal_message (NULL, obfd, NULL,
1652 _("can't add section '%s'"), padd->name);
1653 return FALSE;
1654 }
1655 else
1656 {
1657 /* We use LINKER_CREATED here so that the backend hooks
1658 will create any special section type information,
1659 instead of presuming we know what we're doing merely
1660 because we set the flags. */
1661 padd->section = bfd_make_section_with_flags
1662 (obfd, padd->name, flags | SEC_LINKER_CREATED);
1663 if (padd->section == NULL)
1664 {
1665 bfd_nonfatal_message (NULL, obfd, NULL,
1666 _("can't create section `%s'"),
1667 padd->name);
1668 return FALSE;
1669 }
1670 }
1671
1672 if (! bfd_set_section_size (obfd, padd->section, padd->size))
1673 {
1674 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1675 return FALSE;
1676 }
1677
1678 if (pset != NULL)
1679 {
1680 if (pset->change_vma != CHANGE_IGNORE)
1681 if (! bfd_set_section_vma (obfd, padd->section,
1682 pset->vma_val))
1683 {
1684 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1685 return FALSE;
1686 }
1687
1688 if (pset->change_lma != CHANGE_IGNORE)
1689 {
1690 padd->section->lma = pset->lma_val;
1691
1692 if (! bfd_set_section_alignment
1693 (obfd, padd->section,
1694 bfd_section_alignment (obfd, padd->section)))
1695 {
1696 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1697 return FALSE;
1698 }
1699 }
1700 }
1701 }
1702 }
1703
1704 if (gnu_debuglink_filename != NULL)
1705 {
1706 gnu_debuglink_section = bfd_create_gnu_debuglink_section
1707 (obfd, gnu_debuglink_filename);
1708
1709 if (gnu_debuglink_section == NULL)
1710 {
1711 bfd_nonfatal_message (NULL, obfd, NULL,
1712 _("cannot create debug link section `%s'"),
1713 gnu_debuglink_filename);
1714 return FALSE;
1715 }
1716
1717 /* Special processing for PE format files. We
1718 have no way to distinguish PE from COFF here. */
1719 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
1720 {
1721 bfd_vma debuglink_vma;
1722 asection * highest_section;
1723 asection * sec;
1724
1725 /* The PE spec requires that all sections be adjacent and sorted
1726 in ascending order of VMA. It also specifies that debug
1727 sections should be last. This is despite the fact that debug
1728 sections are not loaded into memory and so in theory have no
1729 use for a VMA.
1730
1731 This means that the debuglink section must be given a non-zero
1732 VMA which makes it contiguous with other debug sections. So
1733 walk the current section list, find the section with the
1734 highest VMA and start the debuglink section after that one. */
1735 for (sec = obfd->sections, highest_section = NULL;
1736 sec != NULL;
1737 sec = sec->next)
1738 if (sec->vma > 0
1739 && (highest_section == NULL
1740 || sec->vma > highest_section->vma))
1741 highest_section = sec;
1742
1743 if (highest_section)
1744 debuglink_vma = BFD_ALIGN (highest_section->vma
1745 + highest_section->size,
1746 /* FIXME: We ought to be using
1747 COFF_PAGE_SIZE here or maybe
1748 bfd_get_section_alignment() (if it
1749 was set) but since this is for PE
1750 and we know the required alignment
1751 it is easier just to hard code it. */
1752 0x1000);
1753 else
1754 /* Umm, not sure what to do in this case. */
1755 debuglink_vma = 0x1000;
1756
1757 bfd_set_section_vma (obfd, gnu_debuglink_section, debuglink_vma);
1758 }
1759 }
1760
1761 if (bfd_count_sections (obfd) != 0
1762 && (gap_fill_set || pad_to_set))
1763 {
1764 asection **set;
1765 unsigned int c, i;
1766
1767 /* We must fill in gaps between the sections and/or we must pad
1768 the last section to a specified address. We do this by
1769 grabbing a list of the sections, sorting them by VMA, and
1770 increasing the section sizes as required to fill the gaps.
1771 We write out the gap contents below. */
1772
1773 c = bfd_count_sections (obfd);
1774 osections = (asection **) xmalloc (c * sizeof (asection *));
1775 set = osections;
1776 bfd_map_over_sections (obfd, get_sections, &set);
1777
1778 qsort (osections, c, sizeof (asection *), compare_section_lma);
1779
1780 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
1781 memset (gaps, 0, c * sizeof (bfd_size_type));
1782
1783 if (gap_fill_set)
1784 {
1785 for (i = 0; i < c - 1; i++)
1786 {
1787 flagword flags;
1788 bfd_size_type size;
1789 bfd_vma gap_start, gap_stop;
1790
1791 flags = bfd_get_section_flags (obfd, osections[i]);
1792 if ((flags & SEC_HAS_CONTENTS) == 0
1793 || (flags & SEC_LOAD) == 0)
1794 continue;
1795
1796 size = bfd_section_size (obfd, osections[i]);
1797 gap_start = bfd_section_lma (obfd, osections[i]) + size;
1798 gap_stop = bfd_section_lma (obfd, osections[i + 1]);
1799 if (gap_start < gap_stop)
1800 {
1801 if (! bfd_set_section_size (obfd, osections[i],
1802 size + (gap_stop - gap_start)))
1803 {
1804 bfd_nonfatal_message (NULL, obfd, osections[i],
1805 _("Can't fill gap after section"));
1806 status = 1;
1807 break;
1808 }
1809 gaps[i] = gap_stop - gap_start;
1810 if (max_gap < gap_stop - gap_start)
1811 max_gap = gap_stop - gap_start;
1812 }
1813 }
1814 }
1815
1816 if (pad_to_set)
1817 {
1818 bfd_vma lma;
1819 bfd_size_type size;
1820
1821 lma = bfd_section_lma (obfd, osections[c - 1]);
1822 size = bfd_section_size (obfd, osections[c - 1]);
1823 if (lma + size < pad_to)
1824 {
1825 if (! bfd_set_section_size (obfd, osections[c - 1],
1826 pad_to - lma))
1827 {
1828 bfd_nonfatal_message (NULL, obfd, osections[c - 1],
1829 _("can't add padding"));
1830 status = 1;
1831 }
1832 else
1833 {
1834 gaps[c - 1] = pad_to - (lma + size);
1835 if (max_gap < pad_to - (lma + size))
1836 max_gap = pad_to - (lma + size);
1837 }
1838 }
1839 }
1840 }
1841
1842 /* Symbol filtering must happen after the output sections
1843 have been created, but before their contents are set. */
1844 dhandle = NULL;
1845 if (convert_debugging)
1846 dhandle = read_debugging_info (ibfd, isympp, symcount, FALSE);
1847
1848 if (strip_symbols == STRIP_DEBUG
1849 || strip_symbols == STRIP_ALL
1850 || strip_symbols == STRIP_UNNEEDED
1851 || strip_symbols == STRIP_NONDEBUG
1852 || discard_locals != LOCALS_UNDEF
1853 || localize_hidden
1854 || htab_elements (strip_specific_htab) != 0
1855 || htab_elements (keep_specific_htab) != 0
1856 || htab_elements (localize_specific_htab) != 0
1857 || htab_elements (globalize_specific_htab) != 0
1858 || htab_elements (keepglobal_specific_htab) != 0
1859 || htab_elements (weaken_specific_htab) != 0
1860 || prefix_symbols_string
1861 || sections_removed
1862 || sections_copied
1863 || convert_debugging
1864 || change_leading_char
1865 || remove_leading_char
1866 || redefine_sym_list
1867 || weaken)
1868 {
1869 /* Mark symbols used in output relocations so that they
1870 are kept, even if they are local labels or static symbols.
1871
1872 Note we iterate over the input sections examining their
1873 relocations since the relocations for the output sections
1874 haven't been set yet. mark_symbols_used_in_relocations will
1875 ignore input sections which have no corresponding output
1876 section. */
1877 if (strip_symbols != STRIP_ALL)
1878 bfd_map_over_sections (ibfd,
1879 mark_symbols_used_in_relocations,
1880 isympp);
1881 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
1882 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1883 }
1884
1885 if (convert_debugging && dhandle != NULL)
1886 {
1887 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
1888 {
1889 status = 1;
1890 return FALSE;
1891 }
1892 }
1893
1894 bfd_set_symtab (obfd, osympp, symcount);
1895
1896 /* This has to happen before section positions are set. */
1897 bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
1898
1899 /* This has to happen after the symbol table has been set. */
1900 bfd_map_over_sections (ibfd, copy_section, obfd);
1901
1902 if (add_sections != NULL)
1903 {
1904 struct section_add *padd;
1905
1906 for (padd = add_sections; padd != NULL; padd = padd->next)
1907 {
1908 if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
1909 0, padd->size))
1910 {
1911 bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
1912 return FALSE;
1913 }
1914 }
1915 }
1916
1917 if (gnu_debuglink_filename != NULL)
1918 {
1919 if (! bfd_fill_in_gnu_debuglink_section
1920 (obfd, gnu_debuglink_section, gnu_debuglink_filename))
1921 {
1922 bfd_nonfatal_message (NULL, obfd, NULL,
1923 _("cannot fill debug link section `%s'"),
1924 gnu_debuglink_filename);
1925 return FALSE;
1926 }
1927 }
1928
1929 if (gap_fill_set || pad_to_set)
1930 {
1931 bfd_byte *buf;
1932 int c, i;
1933
1934 /* Fill in the gaps. */
1935 if (max_gap > 8192)
1936 max_gap = 8192;
1937 buf = (bfd_byte *) xmalloc (max_gap);
1938 memset (buf, gap_fill, max_gap);
1939
1940 c = bfd_count_sections (obfd);
1941 for (i = 0; i < c; i++)
1942 {
1943 if (gaps[i] != 0)
1944 {
1945 bfd_size_type left;
1946 file_ptr off;
1947
1948 left = gaps[i];
1949 off = bfd_section_size (obfd, osections[i]) - left;
1950
1951 while (left > 0)
1952 {
1953 bfd_size_type now;
1954
1955 if (left > 8192)
1956 now = 8192;
1957 else
1958 now = left;
1959
1960 if (! bfd_set_section_contents (obfd, osections[i], buf,
1961 off, now))
1962 {
1963 bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
1964 return FALSE;
1965 }
1966
1967 left -= now;
1968 off += now;
1969 }
1970 }
1971 }
1972 }
1973
1974 /* Do not copy backend data if --extract-symbol is passed; anything
1975 that needs to look at the section contents will fail. */
1976 if (extract_symbol)
1977 return TRUE;
1978
1979 /* Allow the BFD backend to copy any private data it understands
1980 from the input BFD to the output BFD. This is done last to
1981 permit the routine to look at the filtered symbol table, which is
1982 important for the ECOFF code at least. */
1983 if (! bfd_copy_private_bfd_data (ibfd, obfd))
1984 {
1985 bfd_nonfatal_message (NULL, obfd, NULL,
1986 _("error copying private BFD data"));
1987 return FALSE;
1988 }
1989
1990 /* Switch to the alternate machine code. We have to do this at the
1991 very end, because we only initialize the header when we create
1992 the first section. */
1993 if (use_alt_mach_code != 0)
1994 {
1995 if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
1996 {
1997 non_fatal (_("this target does not support %lu alternative machine codes"),
1998 use_alt_mach_code);
1999 if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2000 {
2001 non_fatal (_("treating that number as an absolute e_machine value instead"));
2002 elf_elfheader (obfd)->e_machine = use_alt_mach_code;
2003 }
2004 else
2005 non_fatal (_("ignoring the alternative value"));
2006 }
2007 }
2008
2009 return TRUE;
2010 }
2011
2012 /* Read each archive element in turn from IBFD, copy the
2013 contents to temp file, and keep the temp file handle.
2014 If 'force_output_target' is TRUE then make sure that
2015 all elements in the new archive are of the type
2016 'output_target'. */
2017
2018 static void
2019 copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
2020 bfd_boolean force_output_target,
2021 const bfd_arch_info_type *input_arch)
2022 {
2023 struct name_list
2024 {
2025 struct name_list *next;
2026 const char *name;
2027 bfd *obfd;
2028 } *list, *l;
2029 bfd **ptr = &obfd->archive_head;
2030 bfd *this_element;
2031 char *dir;
2032 const char *filename;
2033
2034 /* Make a temp directory to hold the contents. */
2035 dir = make_tempdir (bfd_get_filename (obfd));
2036 if (dir == NULL)
2037 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2038 strerror (errno));
2039
2040 if (strip_symbols == STRIP_ALL)
2041 obfd->has_armap = FALSE;
2042 else
2043 obfd->has_armap = ibfd->has_armap;
2044 obfd->is_thin_archive = ibfd->is_thin_archive;
2045
2046 if (deterministic)
2047 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
2048
2049 list = NULL;
2050
2051 this_element = bfd_openr_next_archived_file (ibfd, NULL);
2052
2053 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
2054 {
2055 status = 1;
2056 bfd_nonfatal_message (NULL, obfd, NULL, NULL);
2057 return;
2058 }
2059
2060 while (!status && this_element != NULL)
2061 {
2062 char *output_name;
2063 bfd *output_bfd;
2064 bfd *last_element;
2065 struct stat buf;
2066 int stat_status = 0;
2067 bfd_boolean del = TRUE;
2068 bfd_boolean ok_object;
2069
2070 /* Create an output file for this member. */
2071 output_name = concat (dir, "/",
2072 bfd_get_filename (this_element), (char *) 0);
2073
2074 /* If the file already exists, make another temp dir. */
2075 if (stat (output_name, &buf) >= 0)
2076 {
2077 output_name = make_tempdir (output_name);
2078 if (output_name == NULL)
2079 fatal (_("cannot create tempdir for archive copying (error: %s)"),
2080 strerror (errno));
2081
2082 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2083 l->name = output_name;
2084 l->next = list;
2085 l->obfd = NULL;
2086 list = l;
2087 output_name = concat (output_name, "/",
2088 bfd_get_filename (this_element), (char *) 0);
2089 }
2090
2091 if (preserve_dates)
2092 {
2093 stat_status = bfd_stat_arch_elt (this_element, &buf);
2094
2095 if (stat_status != 0)
2096 non_fatal (_("internal stat error on %s"),
2097 bfd_get_filename (this_element));
2098 }
2099
2100 l = (struct name_list *) xmalloc (sizeof (struct name_list));
2101 l->name = output_name;
2102 l->next = list;
2103 l->obfd = NULL;
2104 list = l;
2105
2106 ok_object = bfd_check_format (this_element, bfd_object);
2107 if (!ok_object)
2108 bfd_nonfatal_message (NULL, this_element, NULL,
2109 _("Unable to recognise the format of file"));
2110
2111 /* PR binutils/3110: Cope with archives
2112 containing multiple target types. */
2113 if (force_output_target || !ok_object)
2114 output_bfd = bfd_openw (output_name, output_target);
2115 else
2116 output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
2117
2118 if (output_bfd == NULL)
2119 {
2120 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2121 status = 1;
2122 return;
2123 }
2124
2125 if (ok_object)
2126 {
2127 del = !copy_object (this_element, output_bfd, input_arch);
2128
2129 if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
2130 /* Try again as an unknown object file. */
2131 ok_object = FALSE;
2132 else if (!bfd_close (output_bfd))
2133 {
2134 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2135 /* Error in new object file. Don't change archive. */
2136 status = 1;
2137 }
2138 }
2139
2140 if (!ok_object)
2141 {
2142 del = !copy_unknown_object (this_element, output_bfd);
2143 if (!bfd_close_all_done (output_bfd))
2144 {
2145 bfd_nonfatal_message (output_name, NULL, NULL, NULL);
2146 /* Error in new object file. Don't change archive. */
2147 status = 1;
2148 }
2149 }
2150
2151 if (del)
2152 {
2153 unlink (output_name);
2154 status = 1;
2155 }
2156 else
2157 {
2158 if (preserve_dates && stat_status == 0)
2159 set_times (output_name, &buf);
2160
2161 /* Open the newly output file and attach to our list. */
2162 output_bfd = bfd_openr (output_name, output_target);
2163
2164 l->obfd = output_bfd;
2165
2166 *ptr = output_bfd;
2167 ptr = &output_bfd->archive_next;
2168
2169 last_element = this_element;
2170
2171 this_element = bfd_openr_next_archived_file (ibfd, last_element);
2172
2173 bfd_close (last_element);
2174 }
2175 }
2176 *ptr = NULL;
2177
2178 filename = bfd_get_filename (obfd);
2179 if (!bfd_close (obfd))
2180 {
2181 status = 1;
2182 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2183 return;
2184 }
2185
2186 filename = bfd_get_filename (ibfd);
2187 if (!bfd_close (ibfd))
2188 {
2189 status = 1;
2190 bfd_nonfatal_message (filename, NULL, NULL, NULL);
2191 return;
2192 }
2193
2194 /* Delete all the files that we opened. */
2195 for (l = list; l != NULL; l = l->next)
2196 {
2197 if (l->obfd == NULL)
2198 rmdir (l->name);
2199 else
2200 {
2201 bfd_close (l->obfd);
2202 unlink (l->name);
2203 }
2204 }
2205 rmdir (dir);
2206 }
2207
2208 static void
2209 set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
2210 {
2211 /* This is only relevant to Coff targets. */
2212 if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
2213 {
2214 if (style == KEEP
2215 && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
2216 style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
2217 bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
2218 }
2219 }
2220
2221 /* The top-level control. */
2222
2223 static void
2224 copy_file (const char *input_filename, const char *output_filename,
2225 const char *input_target, const char *output_target,
2226 const bfd_arch_info_type *input_arch)
2227 {
2228 bfd *ibfd;
2229 char **obj_matching;
2230 char **core_matching;
2231 off_t size = get_file_size (input_filename);
2232
2233 if (size < 1)
2234 {
2235 if (size == 0)
2236 non_fatal (_("error: the input file '%s' is empty"),
2237 input_filename);
2238 status = 1;
2239 return;
2240 }
2241
2242 /* To allow us to do "strip *" without dying on the first
2243 non-object file, failures are nonfatal. */
2244 ibfd = bfd_openr (input_filename, input_target);
2245 if (ibfd == NULL)
2246 {
2247 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2248 status = 1;
2249 return;
2250 }
2251
2252 switch (do_debug_sections)
2253 {
2254 case compress:
2255 ibfd->flags |= BFD_COMPRESS;
2256 break;
2257 case decompress:
2258 ibfd->flags |= BFD_DECOMPRESS;
2259 break;
2260 default:
2261 break;
2262 }
2263
2264 if (bfd_check_format (ibfd, bfd_archive))
2265 {
2266 bfd_boolean force_output_target;
2267 bfd *obfd;
2268
2269 /* bfd_get_target does not return the correct value until
2270 bfd_check_format succeeds. */
2271 if (output_target == NULL)
2272 {
2273 output_target = bfd_get_target (ibfd);
2274 force_output_target = FALSE;
2275 }
2276 else
2277 force_output_target = TRUE;
2278
2279 obfd = bfd_openw (output_filename, output_target);
2280 if (obfd == NULL)
2281 {
2282 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2283 status = 1;
2284 return;
2285 }
2286 /* This is a no-op on non-Coff targets. */
2287 set_long_section_mode (obfd, ibfd, long_section_names);
2288
2289 copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
2290 }
2291 else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
2292 {
2293 bfd *obfd;
2294 do_copy:
2295
2296 /* bfd_get_target does not return the correct value until
2297 bfd_check_format succeeds. */
2298 if (output_target == NULL)
2299 output_target = bfd_get_target (ibfd);
2300
2301 obfd = bfd_openw (output_filename, output_target);
2302 if (obfd == NULL)
2303 {
2304 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2305 status = 1;
2306 return;
2307 }
2308 /* This is a no-op on non-Coff targets. */
2309 set_long_section_mode (obfd, ibfd, long_section_names);
2310
2311 if (! copy_object (ibfd, obfd, input_arch))
2312 status = 1;
2313
2314 if (!bfd_close (obfd))
2315 {
2316 status = 1;
2317 bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
2318 return;
2319 }
2320
2321 if (!bfd_close (ibfd))
2322 {
2323 status = 1;
2324 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2325 return;
2326 }
2327 }
2328 else
2329 {
2330 bfd_error_type obj_error = bfd_get_error ();
2331 bfd_error_type core_error;
2332
2333 if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
2334 {
2335 /* This probably can't happen.. */
2336 if (obj_error == bfd_error_file_ambiguously_recognized)
2337 free (obj_matching);
2338 goto do_copy;
2339 }
2340
2341 core_error = bfd_get_error ();
2342 /* Report the object error in preference to the core error. */
2343 if (obj_error != core_error)
2344 bfd_set_error (obj_error);
2345
2346 bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
2347
2348 if (obj_error == bfd_error_file_ambiguously_recognized)
2349 {
2350 list_matching_formats (obj_matching);
2351 free (obj_matching);
2352 }
2353 if (core_error == bfd_error_file_ambiguously_recognized)
2354 {
2355 list_matching_formats (core_matching);
2356 free (core_matching);
2357 }
2358
2359 status = 1;
2360 }
2361 }
2362
2363 /* Add a name to the section renaming list. */
2364
2365 static void
2366 add_section_rename (const char * old_name, const char * new_name,
2367 flagword flags)
2368 {
2369 section_rename * srename;
2370
2371 /* Check for conflicts first. */
2372 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2373 if (strcmp (srename->old_name, old_name) == 0)
2374 {
2375 /* Silently ignore duplicate definitions. */
2376 if (strcmp (srename->new_name, new_name) == 0
2377 && srename->flags == flags)
2378 return;
2379
2380 fatal (_("Multiple renames of section %s"), old_name);
2381 }
2382
2383 srename = (section_rename *) xmalloc (sizeof (* srename));
2384
2385 srename->old_name = old_name;
2386 srename->new_name = new_name;
2387 srename->flags = flags;
2388 srename->next = section_rename_list;
2389
2390 section_rename_list = srename;
2391 }
2392
2393 /* Check the section rename list for a new name of the input section
2394 ISECTION. Return the new name if one is found.
2395 Also set RETURNED_FLAGS to the flags to be used for this section. */
2396
2397 static const char *
2398 find_section_rename (bfd * ibfd ATTRIBUTE_UNUSED, sec_ptr isection,
2399 flagword * returned_flags)
2400 {
2401 const char * old_name = bfd_section_name (ibfd, isection);
2402 section_rename * srename;
2403
2404 /* Default to using the flags of the input section. */
2405 * returned_flags = bfd_get_section_flags (ibfd, isection);
2406
2407 for (srename = section_rename_list; srename != NULL; srename = srename->next)
2408 if (strcmp (srename->old_name, old_name) == 0)
2409 {
2410 if (srename->flags != (flagword) -1)
2411 * returned_flags = srename->flags;
2412
2413 return srename->new_name;
2414 }
2415
2416 return old_name;
2417 }
2418
2419 /* Once each of the sections is copied, we may still need to do some
2420 finalization work for private section headers. Do that here. */
2421
2422 static void
2423 setup_bfd_headers (bfd *ibfd, bfd *obfd)
2424 {
2425 /* Allow the BFD backend to copy any private data it understands
2426 from the input section to the output section. */
2427 if (! bfd_copy_private_header_data (ibfd, obfd))
2428 {
2429 status = 1;
2430 bfd_nonfatal_message (NULL, ibfd, NULL,
2431 _("error in private header data"));
2432 return;
2433 }
2434
2435 /* All went well. */
2436 return;
2437 }
2438
2439 /* Create a section in OBFD with the same
2440 name and attributes as ISECTION in IBFD. */
2441
2442 static void
2443 setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2444 {
2445 bfd *obfd = (bfd *) obfdarg;
2446 struct section_list *p;
2447 sec_ptr osection;
2448 bfd_size_type size;
2449 bfd_vma vma;
2450 bfd_vma lma;
2451 flagword flags;
2452 const char *err;
2453 const char * name;
2454 char *prefix = NULL;
2455 bfd_boolean make_nobits;
2456
2457 if (is_strip_section (ibfd, isection))
2458 return;
2459
2460 p = find_section_list (bfd_section_name (ibfd, isection), FALSE);
2461 if (p != NULL)
2462 p->used = TRUE;
2463
2464 /* Get the, possibly new, name of the output section. */
2465 name = find_section_rename (ibfd, isection, & flags);
2466
2467 /* Prefix sections. */
2468 if ((prefix_alloc_sections_string)
2469 && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC))
2470 prefix = prefix_alloc_sections_string;
2471 else if (prefix_sections_string)
2472 prefix = prefix_sections_string;
2473
2474 if (prefix)
2475 {
2476 char *n;
2477
2478 n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
2479 strcpy (n, prefix);
2480 strcat (n, name);
2481 name = n;
2482 }
2483
2484 make_nobits = FALSE;
2485 if (p != NULL && p->set_flags)
2486 flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
2487 else if (strip_symbols == STRIP_NONDEBUG
2488 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
2489 && !(ibfd->xvec->flavour == bfd_target_elf_flavour
2490 && elf_section_type (isection) == SHT_NOTE))
2491 {
2492 flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
2493 if (obfd->xvec->flavour == bfd_target_elf_flavour)
2494 {
2495 make_nobits = TRUE;
2496
2497 /* Twiddle the input section flags so that it seems to
2498 elf.c:copy_private_bfd_data that section flags have not
2499 changed between input and output sections. This hack
2500 prevents wholesale rewriting of the program headers. */
2501 isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
2502 }
2503 }
2504
2505 osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
2506
2507 if (osection == NULL)
2508 {
2509 err = _("failed to create output section");
2510 goto loser;
2511 }
2512
2513 if (make_nobits)
2514 elf_section_type (osection) = SHT_NOBITS;
2515
2516 size = bfd_section_size (ibfd, isection);
2517 if (copy_byte >= 0)
2518 size = (size + interleave - 1) / interleave * copy_width;
2519 else if (extract_symbol)
2520 size = 0;
2521 if (! bfd_set_section_size (obfd, osection, size))
2522 {
2523 err = _("failed to set size");
2524 goto loser;
2525 }
2526
2527 vma = bfd_section_vma (ibfd, isection);
2528 if (p != NULL && p->change_vma == CHANGE_MODIFY)
2529 vma += p->vma_val;
2530 else if (p != NULL && p->change_vma == CHANGE_SET)
2531 vma = p->vma_val;
2532 else
2533 vma += change_section_address;
2534
2535 if (! bfd_set_section_vma (obfd, osection, vma))
2536 {
2537 err = _("failed to set vma");
2538 goto loser;
2539 }
2540
2541 lma = isection->lma;
2542 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
2543 {
2544 if (p->change_lma == CHANGE_MODIFY)
2545 lma += p->lma_val;
2546 else if (p->change_lma == CHANGE_SET)
2547 lma = p->lma_val;
2548 else
2549 abort ();
2550 }
2551 else
2552 lma += change_section_address;
2553
2554 osection->lma = lma;
2555
2556 /* FIXME: This is probably not enough. If we change the LMA we
2557 may have to recompute the header for the file as well. */
2558 if (!bfd_set_section_alignment (obfd,
2559 osection,
2560 bfd_section_alignment (ibfd, isection)))
2561 {
2562 err = _("failed to set alignment");
2563 goto loser;
2564 }
2565
2566 /* Copy merge entity size. */
2567 osection->entsize = isection->entsize;
2568
2569 /* This used to be mangle_section; we do here to avoid using
2570 bfd_get_section_by_name since some formats allow multiple
2571 sections with the same name. */
2572 isection->output_section = osection;
2573 isection->output_offset = 0;
2574
2575 /* Do not copy backend data if --extract-symbol is passed; anything
2576 that needs to look at the section contents will fail. */
2577 if (extract_symbol)
2578 return;
2579
2580 if ((isection->flags & SEC_GROUP) != 0)
2581 {
2582 asymbol *gsym = group_signature (isection);
2583
2584 if (gsym != NULL)
2585 {
2586 gsym->flags |= BSF_KEEP;
2587 if (ibfd->xvec->flavour == bfd_target_elf_flavour)
2588 elf_group_id (isection) = gsym;
2589 }
2590 }
2591
2592 /* Allow the BFD backend to copy any private data it understands
2593 from the input section to the output section. */
2594 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
2595 {
2596 err = _("failed to copy private data");
2597 goto loser;
2598 }
2599
2600 /* All went well. */
2601 return;
2602
2603 loser:
2604 status = 1;
2605 bfd_nonfatal_message (NULL, obfd, osection, err);
2606 }
2607
2608 /* Return TRUE if input section ISECTION should be skipped. */
2609
2610 static bfd_boolean
2611 skip_section (bfd *ibfd, sec_ptr isection)
2612 {
2613 sec_ptr osection;
2614 bfd_size_type size;
2615 flagword flags;
2616
2617 /* If we have already failed earlier on,
2618 do not keep on generating complaints now. */
2619 if (status != 0)
2620 return TRUE;
2621
2622 if (extract_symbol)
2623 return TRUE;
2624
2625 if (is_strip_section (ibfd, isection))
2626 return TRUE;
2627
2628 flags = bfd_get_section_flags (ibfd, isection);
2629 if ((flags & SEC_GROUP) != 0)
2630 return TRUE;
2631
2632 osection = isection->output_section;
2633 size = bfd_get_section_size (isection);
2634
2635 if (size == 0 || osection == 0)
2636 return TRUE;
2637
2638 return FALSE;
2639 }
2640
2641 /* Copy relocations in input section ISECTION of IBFD to an output
2642 section with the same name in OBFDARG. If stripping then don't
2643 copy any relocation info. */
2644
2645 static void
2646 copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2647 {
2648 bfd *obfd = (bfd *) obfdarg;
2649 long relsize;
2650 arelent **relpp;
2651 long relcount;
2652 sec_ptr osection;
2653
2654 if (skip_section (ibfd, isection))
2655 return;
2656
2657 osection = isection->output_section;
2658
2659 /* Core files do not need to be relocated. */
2660 if (bfd_get_format (obfd) == bfd_core)
2661 relsize = 0;
2662 else
2663 {
2664 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2665
2666 if (relsize < 0)
2667 {
2668 /* Do not complain if the target does not support relocations. */
2669 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2670 relsize = 0;
2671 else
2672 {
2673 status = 1;
2674 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2675 return;
2676 }
2677 }
2678 }
2679
2680 if (relsize == 0)
2681 bfd_set_reloc (obfd, osection, NULL, 0);
2682 else
2683 {
2684 relpp = (arelent **) xmalloc (relsize);
2685 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
2686 if (relcount < 0)
2687 {
2688 status = 1;
2689 bfd_nonfatal_message (NULL, ibfd, isection,
2690 _("relocation count is negative"));
2691 return;
2692 }
2693
2694 if (strip_symbols == STRIP_ALL)
2695 {
2696 /* Remove relocations which are not in
2697 keep_strip_specific_list. */
2698 arelent **temp_relpp;
2699 long temp_relcount = 0;
2700 long i;
2701
2702 temp_relpp = (arelent **) xmalloc (relsize);
2703 for (i = 0; i < relcount; i++)
2704 if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
2705 keep_specific_htab))
2706 temp_relpp [temp_relcount++] = relpp [i];
2707 relcount = temp_relcount;
2708 free (relpp);
2709 relpp = temp_relpp;
2710 }
2711
2712 bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
2713 if (relcount == 0)
2714 {
2715 osection->flags &= ~SEC_RELOC;
2716 free (relpp);
2717 }
2718 }
2719 }
2720
2721 /* Copy the data of input section ISECTION of IBFD
2722 to an output section with the same name in OBFD. */
2723
2724 static void
2725 copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
2726 {
2727 bfd *obfd = (bfd *) obfdarg;
2728 struct section_list *p;
2729 sec_ptr osection;
2730 bfd_size_type size;
2731
2732 if (skip_section (ibfd, isection))
2733 return;
2734
2735 osection = isection->output_section;
2736 size = bfd_get_section_size (isection);
2737
2738 p = find_section_list (bfd_get_section_name (ibfd, isection), FALSE);
2739
2740 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS
2741 && bfd_get_section_flags (obfd, osection) & SEC_HAS_CONTENTS)
2742 {
2743 bfd_byte *memhunk = NULL;
2744
2745 if (!bfd_get_full_section_contents (ibfd, isection, &memhunk))
2746 {
2747 status = 1;
2748 bfd_nonfatal_message (NULL, ibfd, isection, NULL);
2749 return;
2750 }
2751
2752 if (reverse_bytes)
2753 {
2754 /* We don't handle leftover bytes (too many possible behaviors,
2755 and we don't know what the user wants). The section length
2756 must be a multiple of the number of bytes to swap. */
2757 if ((size % reverse_bytes) == 0)
2758 {
2759 unsigned long i, j;
2760 bfd_byte b;
2761
2762 for (i = 0; i < size; i += reverse_bytes)
2763 for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
2764 {
2765 bfd_byte *m = (bfd_byte *) memhunk;
2766
2767 b = m[i + j];
2768 m[i + j] = m[(i + reverse_bytes) - (j + 1)];
2769 m[(i + reverse_bytes) - (j + 1)] = b;
2770 }
2771 }
2772 else
2773 /* User must pad the section up in order to do this. */
2774 fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
2775 bfd_section_name (ibfd, isection), reverse_bytes);
2776 }
2777
2778 if (copy_byte >= 0)
2779 {
2780 /* Keep only every `copy_byte'th byte in MEMHUNK. */
2781 char *from = (char *) memhunk + copy_byte;
2782 char *to = (char *) memhunk;
2783 char *end = (char *) memhunk + size;
2784 int i;
2785
2786 for (; from < end; from += interleave)
2787 for (i = 0; i < copy_width; i++)
2788 *to++ = from[i];
2789
2790 size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
2791 osection->lma /= interleave;
2792 }
2793
2794 if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2795 {
2796 status = 1;
2797 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2798 return;
2799 }
2800 free (memhunk);
2801 }
2802 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
2803 {
2804 void *memhunk = xmalloc (size);
2805
2806 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
2807 flag--they can just remove the section entirely and add it
2808 back again. However, we do permit them to turn on the
2809 SEC_HAS_CONTENTS flag, and take it to mean that the section
2810 contents should be zeroed out. */
2811
2812 memset (memhunk, 0, size);
2813 if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
2814 {
2815 status = 1;
2816 bfd_nonfatal_message (NULL, obfd, osection, NULL);
2817 return;
2818 }
2819 free (memhunk);
2820 }
2821 }
2822
2823 /* Get all the sections. This is used when --gap-fill or --pad-to is
2824 used. */
2825
2826 static void
2827 get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
2828 {
2829 asection ***secppp = (asection ***) secppparg;
2830
2831 **secppp = osection;
2832 ++(*secppp);
2833 }
2834
2835 /* Sort sections by VMA. This is called via qsort, and is used when
2836 --gap-fill or --pad-to is used. We force non loadable or empty
2837 sections to the front, where they are easier to ignore. */
2838
2839 static int
2840 compare_section_lma (const void *arg1, const void *arg2)
2841 {
2842 const asection *const *sec1 = (const asection * const *) arg1;
2843 const asection *const *sec2 = (const asection * const *) arg2;
2844 flagword flags1, flags2;
2845
2846 /* Sort non loadable sections to the front. */
2847 flags1 = (*sec1)->flags;
2848 flags2 = (*sec2)->flags;
2849 if ((flags1 & SEC_HAS_CONTENTS) == 0
2850 || (flags1 & SEC_LOAD) == 0)
2851 {
2852 if ((flags2 & SEC_HAS_CONTENTS) != 0
2853 && (flags2 & SEC_LOAD) != 0)
2854 return -1;
2855 }
2856 else
2857 {
2858 if ((flags2 & SEC_HAS_CONTENTS) == 0
2859 || (flags2 & SEC_LOAD) == 0)
2860 return 1;
2861 }
2862
2863 /* Sort sections by LMA. */
2864 if ((*sec1)->lma > (*sec2)->lma)
2865 return 1;
2866 else if ((*sec1)->lma < (*sec2)->lma)
2867 return -1;
2868
2869 /* Sort sections with the same LMA by size. */
2870 if (bfd_get_section_size (*sec1) > bfd_get_section_size (*sec2))
2871 return 1;
2872 else if (bfd_get_section_size (*sec1) < bfd_get_section_size (*sec2))
2873 return -1;
2874
2875 return 0;
2876 }
2877
2878 /* Mark all the symbols which will be used in output relocations with
2879 the BSF_KEEP flag so that those symbols will not be stripped.
2880
2881 Ignore relocations which will not appear in the output file. */
2882
2883 static void
2884 mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
2885 {
2886 asymbol **symbols = (asymbol **) symbolsarg;
2887 long relsize;
2888 arelent **relpp;
2889 long relcount, i;
2890
2891 /* Ignore an input section with no corresponding output section. */
2892 if (isection->output_section == NULL)
2893 return;
2894
2895 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
2896 if (relsize < 0)
2897 {
2898 /* Do not complain if the target does not support relocations. */
2899 if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
2900 return;
2901 bfd_fatal (bfd_get_filename (ibfd));
2902 }
2903
2904 if (relsize == 0)
2905 return;
2906
2907 relpp = (arelent **) xmalloc (relsize);
2908 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
2909 if (relcount < 0)
2910 bfd_fatal (bfd_get_filename (ibfd));
2911
2912 /* Examine each symbol used in a relocation. If it's not one of the
2913 special bfd section symbols, then mark it with BSF_KEEP. */
2914 for (i = 0; i < relcount; i++)
2915 {
2916 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
2917 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
2918 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
2919 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
2920 }
2921
2922 if (relpp != NULL)
2923 free (relpp);
2924 }
2925
2926 /* Write out debugging information. */
2927
2928 static bfd_boolean
2929 write_debugging_info (bfd *obfd, void *dhandle,
2930 long *symcountp ATTRIBUTE_UNUSED,
2931 asymbol ***symppp ATTRIBUTE_UNUSED)
2932 {
2933 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
2934 return write_ieee_debugging_info (obfd, dhandle);
2935
2936 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
2937 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
2938 {
2939 bfd_byte *syms, *strings;
2940 bfd_size_type symsize, stringsize;
2941 asection *stabsec, *stabstrsec;
2942 flagword flags;
2943
2944 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
2945 &symsize, &strings,
2946 &stringsize))
2947 return FALSE;
2948
2949 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
2950 stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
2951 stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
2952 if (stabsec == NULL
2953 || stabstrsec == NULL
2954 || ! bfd_set_section_size (obfd, stabsec, symsize)
2955 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
2956 || ! bfd_set_section_alignment (obfd, stabsec, 2)
2957 || ! bfd_set_section_alignment (obfd, stabstrsec, 0))
2958 {
2959 bfd_nonfatal_message (NULL, obfd, NULL,
2960 _("can't create debugging section"));
2961 return FALSE;
2962 }
2963
2964 /* We can get away with setting the section contents now because
2965 the next thing the caller is going to do is copy over the
2966 real sections. We may someday have to split the contents
2967 setting out of this function. */
2968 if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
2969 || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
2970 stringsize))
2971 {
2972 bfd_nonfatal_message (NULL, obfd, NULL,
2973 _("can't set debugging section contents"));
2974 return FALSE;
2975 }
2976
2977 return TRUE;
2978 }
2979
2980 bfd_nonfatal_message (NULL, obfd, NULL,
2981 _("don't know how to write debugging information for %s"),
2982 bfd_get_target (obfd));
2983 return FALSE;
2984 }
2985
2986 static int
2987 strip_main (int argc, char *argv[])
2988 {
2989 char *input_target = NULL;
2990 char *output_target = NULL;
2991 bfd_boolean show_version = FALSE;
2992 bfd_boolean formats_info = FALSE;
2993 int c;
2994 int i;
2995 struct section_list *p;
2996 char *output_file = NULL;
2997
2998 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpdgxXHhVvw",
2999 strip_options, (int *) 0)) != EOF)
3000 {
3001 switch (c)
3002 {
3003 case 'I':
3004 input_target = optarg;
3005 break;
3006 case 'O':
3007 output_target = optarg;
3008 break;
3009 case 'F':
3010 input_target = output_target = optarg;
3011 break;
3012 case 'R':
3013 p = find_section_list (optarg, TRUE);
3014 p->remove = TRUE;
3015 sections_removed = TRUE;
3016 break;
3017 case 's':
3018 strip_symbols = STRIP_ALL;
3019 break;
3020 case 'S':
3021 case 'g':
3022 case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
3023 strip_symbols = STRIP_DEBUG;
3024 break;
3025 case OPTION_STRIP_UNNEEDED:
3026 strip_symbols = STRIP_UNNEEDED;
3027 break;
3028 case 'K':
3029 add_specific_symbol (optarg, keep_specific_htab);
3030 break;
3031 case 'N':
3032 add_specific_symbol (optarg, strip_specific_htab);
3033 break;
3034 case 'o':
3035 output_file = optarg;
3036 break;
3037 case 'p':
3038 preserve_dates = TRUE;
3039 break;
3040 case 'D':
3041 deterministic = TRUE;
3042 break;
3043 case 'x':
3044 discard_locals = LOCALS_ALL;
3045 break;
3046 case 'X':
3047 discard_locals = LOCALS_START_L;
3048 break;
3049 case 'v':
3050 verbose = TRUE;
3051 break;
3052 case 'V':
3053 show_version = TRUE;
3054 break;
3055 case OPTION_FORMATS_INFO:
3056 formats_info = TRUE;
3057 break;
3058 case OPTION_ONLY_KEEP_DEBUG:
3059 strip_symbols = STRIP_NONDEBUG;
3060 break;
3061 case OPTION_KEEP_FILE_SYMBOLS:
3062 keep_file_symbols = 1;
3063 break;
3064 case 0:
3065 /* We've been given a long option. */
3066 break;
3067 case 'w':
3068 wildcard = TRUE;
3069 break;
3070 case 'H':
3071 case 'h':
3072 strip_usage (stdout, 0);
3073 default:
3074 strip_usage (stderr, 1);
3075 }
3076 }
3077
3078 if (formats_info)
3079 {
3080 display_info ();
3081 return 0;
3082 }
3083
3084 if (show_version)
3085 print_version ("strip");
3086
3087 /* Default is to strip all symbols. */
3088 if (strip_symbols == STRIP_UNDEF
3089 && discard_locals == LOCALS_UNDEF
3090 && htab_elements (strip_specific_htab) == 0)
3091 strip_symbols = STRIP_ALL;
3092
3093 if (output_target == NULL)
3094 output_target = input_target;
3095
3096 i = optind;
3097 if (i == argc
3098 || (output_file != NULL && (i + 1) < argc))
3099 strip_usage (stderr, 1);
3100
3101 for (; i < argc; i++)
3102 {
3103 int hold_status = status;
3104 struct stat statbuf;
3105 char *tmpname;
3106
3107 if (get_file_size (argv[i]) < 1)
3108 {
3109 status = 1;
3110 continue;
3111 }
3112
3113 if (preserve_dates)
3114 /* No need to check the return value of stat().
3115 It has already been checked in get_file_size(). */
3116 stat (argv[i], &statbuf);
3117
3118 if (output_file == NULL
3119 || filename_cmp (argv[i], output_file) == 0)
3120 tmpname = make_tempname (argv[i]);
3121 else
3122 tmpname = output_file;
3123
3124 if (tmpname == NULL)
3125 {
3126 bfd_nonfatal_message (argv[i], NULL, NULL,
3127 _("could not create temporary file to hold stripped copy"));
3128 status = 1;
3129 continue;
3130 }
3131
3132 status = 0;
3133 copy_file (argv[i], tmpname, input_target, output_target, NULL);
3134 if (status == 0)
3135 {
3136 if (preserve_dates)
3137 set_times (tmpname, &statbuf);
3138 if (output_file != tmpname)
3139 status = (smart_rename (tmpname,
3140 output_file ? output_file : argv[i],
3141 preserve_dates) != 0);
3142 if (status == 0)
3143 status = hold_status;
3144 }
3145 else
3146 unlink_if_ordinary (tmpname);
3147 if (output_file != tmpname)
3148 free (tmpname);
3149 }
3150
3151 return status;
3152 }
3153
3154 /* Set up PE subsystem. */
3155
3156 static void
3157 set_pe_subsystem (const char *s)
3158 {
3159 const char *version, *subsystem;
3160 size_t i;
3161 static const struct
3162 {
3163 const char *name;
3164 const char set_def;
3165 const short value;
3166 }
3167 v[] =
3168 {
3169 { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
3170 { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
3171 { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
3172 { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
3173 { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
3174 { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
3175 { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
3176 { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
3177 { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
3178 { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
3179 };
3180 short value;
3181 char *copy;
3182 int set_def = -1;
3183
3184 /* Check for the presence of a version number. */
3185 version = strchr (s, ':');
3186 if (version == NULL)
3187 subsystem = s;
3188 else
3189 {
3190 int len = version - s;
3191 copy = xstrdup (s);
3192 subsystem = copy;
3193 copy[len] = '\0';
3194 version = copy + 1 + len;
3195 pe_major_subsystem_version = strtoul (version, &copy, 0);
3196 if (*copy == '.')
3197 pe_minor_subsystem_version = strtoul (copy + 1, &copy, 0);
3198 if (*copy != '\0')
3199 non_fatal (_("%s: bad version in PE subsystem"), s);
3200 }
3201
3202 /* Check for numeric subsystem. */
3203 value = (short) strtol (subsystem, &copy, 0);
3204 if (*copy == '\0')
3205 {
3206 for (i = 0; i < ARRAY_SIZE (v); i++)
3207 if (v[i].value == value)
3208 {
3209 pe_subsystem = value;
3210 set_def = v[i].set_def;
3211 break;
3212 }
3213 }
3214 else
3215 {
3216 /* Search for subsystem by name. */
3217 for (i = 0; i < ARRAY_SIZE (v); i++)
3218 if (strcmp (subsystem, v[i].name) == 0)
3219 {
3220 pe_subsystem = v[i].value;
3221 set_def = v[i].set_def;
3222 break;
3223 }
3224 }
3225
3226 switch (set_def)
3227 {
3228 case -1:
3229 fatal (_("unknown PE subsystem: %s"), s);
3230 break;
3231 case 0:
3232 break;
3233 default:
3234 if (pe_file_alignment == (bfd_vma) -1)
3235 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3236 if (pe_section_alignment == (bfd_vma) -1)
3237 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3238 break;
3239 }
3240 if (s != subsystem)
3241 free ((char *) subsystem);
3242 }
3243
3244 /* Convert EFI target to PEI target. */
3245
3246 static void
3247 convert_efi_target (char *efi)
3248 {
3249 efi[0] = 'p';
3250 efi[1] = 'e';
3251 efi[2] = 'i';
3252
3253 if (strcmp (efi + 4, "ia32") == 0)
3254 {
3255 /* Change ia32 to i386. */
3256 efi[5]= '3';
3257 efi[6]= '8';
3258 efi[7]= '6';
3259 }
3260 else if (strcmp (efi + 4, "x86_64") == 0)
3261 {
3262 /* Change x86_64 to x86-64. */
3263 efi[7] = '-';
3264 }
3265 }
3266
3267 static int
3268 copy_main (int argc, char *argv[])
3269 {
3270 char *input_filename = NULL;
3271 char *output_filename = NULL;
3272 char *tmpname;
3273 char *input_target = NULL;
3274 char *output_target = NULL;
3275 bfd_boolean show_version = FALSE;
3276 bfd_boolean change_warn = TRUE;
3277 bfd_boolean formats_info = FALSE;
3278 int c;
3279 struct section_list *p;
3280 struct stat statbuf;
3281 const bfd_arch_info_type *input_arch = NULL;
3282
3283 while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:N:s:O:d:F:L:G:R:SpgxXHhVvW:w",
3284 copy_options, (int *) 0)) != EOF)
3285 {
3286 switch (c)
3287 {
3288 case 'b':
3289 copy_byte = atoi (optarg);
3290 if (copy_byte < 0)
3291 fatal (_("byte number must be non-negative"));
3292 break;
3293
3294 case 'B':
3295 input_arch = bfd_scan_arch (optarg);
3296 if (input_arch == NULL)
3297 fatal (_("architecture %s unknown"), optarg);
3298 break;
3299
3300 case 'i':
3301 if (optarg)
3302 {
3303 interleave = atoi (optarg);
3304 if (interleave < 1)
3305 fatal (_("interleave must be positive"));
3306 }
3307 else
3308 interleave = 4;
3309 break;
3310
3311 case OPTION_INTERLEAVE_WIDTH:
3312 copy_width = atoi (optarg);
3313 if (copy_width < 1)
3314 fatal(_("interleave width must be positive"));
3315 break;
3316
3317 case 'I':
3318 case 's': /* "source" - 'I' is preferred */
3319 input_target = optarg;
3320 break;
3321
3322 case 'O':
3323 case 'd': /* "destination" - 'O' is preferred */
3324 output_target = optarg;
3325 break;
3326
3327 case 'F':
3328 input_target = output_target = optarg;
3329 break;
3330
3331 case 'j':
3332 p = find_section_list (optarg, TRUE);
3333 if (p->remove)
3334 fatal (_("%s both copied and removed"), optarg);
3335 p->copy = TRUE;
3336 sections_copied = TRUE;
3337 break;
3338
3339 case 'R':
3340 p = find_section_list (optarg, TRUE);
3341 if (p->copy)
3342 fatal (_("%s both copied and removed"), optarg);
3343 p->remove = TRUE;
3344 sections_removed = TRUE;
3345 break;
3346
3347 case 'S':
3348 strip_symbols = STRIP_ALL;
3349 break;
3350
3351 case 'g':
3352 strip_symbols = STRIP_DEBUG;
3353 break;
3354
3355 case OPTION_STRIP_UNNEEDED:
3356 strip_symbols = STRIP_UNNEEDED;
3357 break;
3358
3359 case OPTION_ONLY_KEEP_DEBUG:
3360 strip_symbols = STRIP_NONDEBUG;
3361 break;
3362
3363 case OPTION_KEEP_FILE_SYMBOLS:
3364 keep_file_symbols = 1;
3365 break;
3366
3367 case OPTION_ADD_GNU_DEBUGLINK:
3368 gnu_debuglink_filename = optarg;
3369 break;
3370
3371 case 'K':
3372 add_specific_symbol (optarg, keep_specific_htab);
3373 break;
3374
3375 case 'N':
3376 add_specific_symbol (optarg, strip_specific_htab);
3377 break;
3378
3379 case OPTION_STRIP_UNNEEDED_SYMBOL:
3380 add_specific_symbol (optarg, strip_unneeded_htab);
3381 break;
3382
3383 case 'L':
3384 add_specific_symbol (optarg, localize_specific_htab);
3385 break;
3386
3387 case OPTION_GLOBALIZE_SYMBOL:
3388 add_specific_symbol (optarg, globalize_specific_htab);
3389 break;
3390
3391 case 'G':
3392 add_specific_symbol (optarg, keepglobal_specific_htab);
3393 break;
3394
3395 case 'W':
3396 add_specific_symbol (optarg, weaken_specific_htab);
3397 break;
3398
3399 case 'p':
3400 preserve_dates = TRUE;
3401 break;
3402
3403 case 'D':
3404 deterministic = TRUE;
3405 break;
3406
3407 case 'w':
3408 wildcard = TRUE;
3409 break;
3410
3411 case 'x':
3412 discard_locals = LOCALS_ALL;
3413 break;
3414
3415 case 'X':
3416 discard_locals = LOCALS_START_L;
3417 break;
3418
3419 case 'v':
3420 verbose = TRUE;
3421 break;
3422
3423 case 'V':
3424 show_version = TRUE;
3425 break;
3426
3427 case OPTION_FORMATS_INFO:
3428 formats_info = TRUE;
3429 break;
3430
3431 case OPTION_WEAKEN:
3432 weaken = TRUE;
3433 break;
3434
3435 case OPTION_ADD_SECTION:
3436 {
3437 const char *s;
3438 size_t off, alloc;
3439 struct section_add *pa;
3440 FILE *f;
3441
3442 s = strchr (optarg, '=');
3443
3444 if (s == NULL)
3445 fatal (_("bad format for %s"), "--add-section");
3446
3447 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
3448 pa->name = xstrndup (optarg, s - optarg);
3449 pa->filename = s + 1;
3450
3451 /* We don't use get_file_size so that we can do
3452 --add-section .note.GNU_stack=/dev/null
3453 get_file_size doesn't work on /dev/null. */
3454
3455 f = fopen (pa->filename, FOPEN_RB);
3456 if (f == NULL)
3457 fatal (_("cannot open: %s: %s"),
3458 pa->filename, strerror (errno));
3459
3460 off = 0;
3461 alloc = 4096;
3462 pa->contents = (bfd_byte *) xmalloc (alloc);
3463 while (!feof (f))
3464 {
3465 off_t got;
3466
3467 if (off == alloc)
3468 {
3469 alloc <<= 1;
3470 pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
3471 }
3472
3473 got = fread (pa->contents + off, 1, alloc - off, f);
3474 if (ferror (f))
3475 fatal (_("%s: fread failed"), pa->filename);
3476
3477 off += got;
3478 }
3479
3480 pa->size = off;
3481
3482 fclose (f);
3483
3484 pa->next = add_sections;
3485 add_sections = pa;
3486 }
3487 break;
3488
3489 case OPTION_CHANGE_START:
3490 change_start = parse_vma (optarg, "--change-start");
3491 break;
3492
3493 case OPTION_CHANGE_SECTION_ADDRESS:
3494 case OPTION_CHANGE_SECTION_LMA:
3495 case OPTION_CHANGE_SECTION_VMA:
3496 {
3497 const char *s;
3498 int len;
3499 char *name;
3500 char *option = NULL;
3501 bfd_vma val;
3502 enum change_action what = CHANGE_IGNORE;
3503
3504 switch (c)
3505 {
3506 case OPTION_CHANGE_SECTION_ADDRESS:
3507 option = "--change-section-address";
3508 break;
3509 case OPTION_CHANGE_SECTION_LMA:
3510 option = "--change-section-lma";
3511 break;
3512 case OPTION_CHANGE_SECTION_VMA:
3513 option = "--change-section-vma";
3514 break;
3515 }
3516
3517 s = strchr (optarg, '=');
3518 if (s == NULL)
3519 {
3520 s = strchr (optarg, '+');
3521 if (s == NULL)
3522 {
3523 s = strchr (optarg, '-');
3524 if (s == NULL)
3525 fatal (_("bad format for %s"), option);
3526 }
3527 }
3528
3529 len = s - optarg;
3530 name = (char *) xmalloc (len + 1);
3531 strncpy (name, optarg, len);
3532 name[len] = '\0';
3533
3534 p = find_section_list (name, TRUE);
3535
3536 val = parse_vma (s + 1, option);
3537
3538 switch (*s)
3539 {
3540 case '=': what = CHANGE_SET; break;
3541 case '-': val = - val; /* Drop through. */
3542 case '+': what = CHANGE_MODIFY; break;
3543 }
3544
3545 switch (c)
3546 {
3547 case OPTION_CHANGE_SECTION_ADDRESS:
3548 p->change_vma = what;
3549 p->vma_val = val;
3550 /* Drop through. */
3551
3552 case OPTION_CHANGE_SECTION_LMA:
3553 p->change_lma = what;
3554 p->lma_val = val;
3555 break;
3556
3557 case OPTION_CHANGE_SECTION_VMA:
3558 p->change_vma = what;
3559 p->vma_val = val;
3560 break;
3561 }
3562 }
3563 break;
3564
3565 case OPTION_CHANGE_ADDRESSES:
3566 change_section_address = parse_vma (optarg, "--change-addresses");
3567 change_start = change_section_address;
3568 break;
3569
3570 case OPTION_CHANGE_WARNINGS:
3571 change_warn = TRUE;
3572 break;
3573
3574 case OPTION_CHANGE_LEADING_CHAR:
3575 change_leading_char = TRUE;
3576 break;
3577
3578 case OPTION_COMPRESS_DEBUG_SECTIONS:
3579 do_debug_sections = compress;
3580 break;
3581
3582 case OPTION_DEBUGGING:
3583 convert_debugging = TRUE;
3584 break;
3585
3586 case OPTION_DECOMPRESS_DEBUG_SECTIONS:
3587 do_debug_sections = decompress;
3588 break;
3589
3590 case OPTION_GAP_FILL:
3591 {
3592 bfd_vma gap_fill_vma;
3593
3594 gap_fill_vma = parse_vma (optarg, "--gap-fill");
3595 gap_fill = (bfd_byte) gap_fill_vma;
3596 if ((bfd_vma) gap_fill != gap_fill_vma)
3597 {
3598 char buff[20];
3599
3600 sprintf_vma (buff, gap_fill_vma);
3601
3602 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
3603 buff, gap_fill);
3604 }
3605 gap_fill_set = TRUE;
3606 }
3607 break;
3608
3609 case OPTION_NO_CHANGE_WARNINGS:
3610 change_warn = FALSE;
3611 break;
3612
3613 case OPTION_PAD_TO:
3614 pad_to = parse_vma (optarg, "--pad-to");
3615 pad_to_set = TRUE;
3616 break;
3617
3618 case OPTION_REMOVE_LEADING_CHAR:
3619 remove_leading_char = TRUE;
3620 break;
3621
3622 case OPTION_REDEFINE_SYM:
3623 {
3624 /* Push this redefinition onto redefine_symbol_list. */
3625
3626 int len;
3627 const char *s;
3628 const char *nextarg;
3629 char *source, *target;
3630
3631 s = strchr (optarg, '=');
3632 if (s == NULL)
3633 fatal (_("bad format for %s"), "--redefine-sym");
3634
3635 len = s - optarg;
3636 source = (char *) xmalloc (len + 1);
3637 strncpy (source, optarg, len);
3638 source[len] = '\0';
3639
3640 nextarg = s + 1;
3641 len = strlen (nextarg);
3642 target = (char *) xmalloc (len + 1);
3643 strcpy (target, nextarg);
3644
3645 redefine_list_append ("--redefine-sym", source, target);
3646
3647 free (source);
3648 free (target);
3649 }
3650 break;
3651
3652 case OPTION_REDEFINE_SYMS:
3653 add_redefine_syms_file (optarg);
3654 break;
3655
3656 case OPTION_SET_SECTION_FLAGS:
3657 {
3658 const char *s;
3659 int len;
3660 char *name;
3661
3662 s = strchr (optarg, '=');
3663 if (s == NULL)
3664 fatal (_("bad format for %s"), "--set-section-flags");
3665
3666 len = s - optarg;
3667 name = (char *) xmalloc (len + 1);
3668 strncpy (name, optarg, len);
3669 name[len] = '\0';
3670
3671 p = find_section_list (name, TRUE);
3672
3673 p->set_flags = TRUE;
3674 p->flags = parse_flags (s + 1);
3675 }
3676 break;
3677
3678 case OPTION_RENAME_SECTION:
3679 {
3680 flagword flags;
3681 const char *eq, *fl;
3682 char *old_name;
3683 char *new_name;
3684 unsigned int len;
3685
3686 eq = strchr (optarg, '=');
3687 if (eq == NULL)
3688 fatal (_("bad format for %s"), "--rename-section");
3689
3690 len = eq - optarg;
3691 if (len == 0)
3692 fatal (_("bad format for %s"), "--rename-section");
3693
3694 old_name = (char *) xmalloc (len + 1);
3695 strncpy (old_name, optarg, len);
3696 old_name[len] = 0;
3697
3698 eq++;
3699 fl = strchr (eq, ',');
3700 if (fl)
3701 {
3702 flags = parse_flags (fl + 1);
3703 len = fl - eq;
3704 }
3705 else
3706 {
3707 flags = -1;
3708 len = strlen (eq);
3709 }
3710
3711 if (len == 0)
3712 fatal (_("bad format for %s"), "--rename-section");
3713
3714 new_name = (char *) xmalloc (len + 1);
3715 strncpy (new_name, eq, len);
3716 new_name[len] = 0;
3717
3718 add_section_rename (old_name, new_name, flags);
3719 }
3720 break;
3721
3722 case OPTION_SET_START:
3723 set_start = parse_vma (optarg, "--set-start");
3724 set_start_set = TRUE;
3725 break;
3726
3727 case OPTION_SREC_LEN:
3728 Chunk = parse_vma (optarg, "--srec-len");
3729 break;
3730
3731 case OPTION_SREC_FORCES3:
3732 S3Forced = TRUE;
3733 break;
3734
3735 case OPTION_STRIP_SYMBOLS:
3736 add_specific_symbols (optarg, strip_specific_htab);
3737 break;
3738
3739 case OPTION_STRIP_UNNEEDED_SYMBOLS:
3740 add_specific_symbols (optarg, strip_unneeded_htab);
3741 break;
3742
3743 case OPTION_KEEP_SYMBOLS:
3744 add_specific_symbols (optarg, keep_specific_htab);
3745 break;
3746
3747 case OPTION_LOCALIZE_HIDDEN:
3748 localize_hidden = TRUE;
3749 break;
3750
3751 case OPTION_LOCALIZE_SYMBOLS:
3752 add_specific_symbols (optarg, localize_specific_htab);
3753 break;
3754
3755 case OPTION_LONG_SECTION_NAMES:
3756 if (!strcmp ("enable", optarg))
3757 long_section_names = ENABLE;
3758 else if (!strcmp ("disable", optarg))
3759 long_section_names = DISABLE;
3760 else if (!strcmp ("keep", optarg))
3761 long_section_names = KEEP;
3762 else
3763 fatal (_("unknown long section names option '%s'"), optarg);
3764 break;
3765
3766 case OPTION_GLOBALIZE_SYMBOLS:
3767 add_specific_symbols (optarg, globalize_specific_htab);
3768 break;
3769
3770 case OPTION_KEEPGLOBAL_SYMBOLS:
3771 add_specific_symbols (optarg, keepglobal_specific_htab);
3772 break;
3773
3774 case OPTION_WEAKEN_SYMBOLS:
3775 add_specific_symbols (optarg, weaken_specific_htab);
3776 break;
3777
3778 case OPTION_ALT_MACH_CODE:
3779 use_alt_mach_code = strtoul (optarg, NULL, 0);
3780 if (use_alt_mach_code == 0)
3781 fatal (_("unable to parse alternative machine code"));
3782 break;
3783
3784 case OPTION_PREFIX_SYMBOLS:
3785 prefix_symbols_string = optarg;
3786 break;
3787
3788 case OPTION_PREFIX_SECTIONS:
3789 prefix_sections_string = optarg;
3790 break;
3791
3792 case OPTION_PREFIX_ALLOC_SECTIONS:
3793 prefix_alloc_sections_string = optarg;
3794 break;
3795
3796 case OPTION_READONLY_TEXT:
3797 bfd_flags_to_set |= WP_TEXT;
3798 bfd_flags_to_clear &= ~WP_TEXT;
3799 break;
3800
3801 case OPTION_WRITABLE_TEXT:
3802 bfd_flags_to_clear |= WP_TEXT;
3803 bfd_flags_to_set &= ~WP_TEXT;
3804 break;
3805
3806 case OPTION_PURE:
3807 bfd_flags_to_set |= D_PAGED;
3808 bfd_flags_to_clear &= ~D_PAGED;
3809 break;
3810
3811 case OPTION_IMPURE:
3812 bfd_flags_to_clear |= D_PAGED;
3813 bfd_flags_to_set &= ~D_PAGED;
3814 break;
3815
3816 case OPTION_EXTRACT_SYMBOL:
3817 extract_symbol = TRUE;
3818 break;
3819
3820 case OPTION_REVERSE_BYTES:
3821 {
3822 int prev = reverse_bytes;
3823
3824 reverse_bytes = atoi (optarg);
3825 if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
3826 fatal (_("number of bytes to reverse must be positive and even"));
3827
3828 if (prev && prev != reverse_bytes)
3829 non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
3830 prev);
3831 break;
3832 }
3833
3834 case OPTION_FILE_ALIGNMENT:
3835 pe_file_alignment = parse_vma (optarg, "--file-alignment");
3836 break;
3837
3838 case OPTION_HEAP:
3839 {
3840 char *end;
3841 pe_heap_reserve = strtoul (optarg, &end, 0);
3842 if (end == optarg
3843 || (*end != '.' && *end != '\0'))
3844 non_fatal (_("%s: invalid reserve value for --heap"),
3845 optarg);
3846 else if (*end != '\0')
3847 {
3848 pe_heap_commit = strtoul (end + 1, &end, 0);
3849 if (*end != '\0')
3850 non_fatal (_("%s: invalid commit value for --heap"),
3851 optarg);
3852 }
3853 }
3854 break;
3855
3856 case OPTION_IMAGE_BASE:
3857 pe_image_base = parse_vma (optarg, "--image-base");
3858 break;
3859
3860 case OPTION_SECTION_ALIGNMENT:
3861 pe_section_alignment = parse_vma (optarg,
3862 "--section-alignment");
3863 break;
3864
3865 case OPTION_SUBSYSTEM:
3866 set_pe_subsystem (optarg);
3867 break;
3868
3869 case OPTION_STACK:
3870 {
3871 char *end;
3872 pe_stack_reserve = strtoul (optarg, &end, 0);
3873 if (end == optarg
3874 || (*end != '.' && *end != '\0'))
3875 non_fatal (_("%s: invalid reserve value for --stack"),
3876 optarg);
3877 else if (*end != '\0')
3878 {
3879 pe_stack_commit = strtoul (end + 1, &end, 0);
3880 if (*end != '\0')
3881 non_fatal (_("%s: invalid commit value for --stack"),
3882 optarg);
3883 }
3884 }
3885 break;
3886
3887 case 0:
3888 /* We've been given a long option. */
3889 break;
3890
3891 case 'H':
3892 case 'h':
3893 copy_usage (stdout, 0);
3894
3895 default:
3896 copy_usage (stderr, 1);
3897 }
3898 }
3899
3900 if (formats_info)
3901 {
3902 display_info ();
3903 return 0;
3904 }
3905
3906 if (show_version)
3907 print_version ("objcopy");
3908
3909 if (interleave && copy_byte == -1)
3910 fatal (_("interleave start byte must be set with --byte"));
3911
3912 if (copy_byte >= interleave)
3913 fatal (_("byte number must be less than interleave"));
3914
3915 if (copy_width > interleave - copy_byte)
3916 fatal (_("interleave width must be less than or equal to interleave - byte`"));
3917
3918 if (optind == argc || optind + 2 < argc)
3919 copy_usage (stderr, 1);
3920
3921 input_filename = argv[optind];
3922 if (optind + 1 < argc)
3923 output_filename = argv[optind + 1];
3924
3925 /* Default is to strip no symbols. */
3926 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
3927 strip_symbols = STRIP_NONE;
3928
3929 if (output_target == NULL)
3930 output_target = input_target;
3931
3932 /* Convert input EFI target to PEI target. */
3933 if (input_target != NULL
3934 && strncmp (input_target, "efi-", 4) == 0)
3935 {
3936 char *efi;
3937
3938 efi = xstrdup (output_target + 4);
3939 if (strncmp (efi, "bsdrv-", 6) == 0
3940 || strncmp (efi, "rtdrv-", 6) == 0)
3941 efi += 2;
3942 else if (strncmp (efi, "app-", 4) != 0)
3943 fatal (_("unknown input EFI target: %s"), input_target);
3944
3945 input_target = efi;
3946 convert_efi_target (efi);
3947 }
3948
3949 /* Convert output EFI target to PEI target. */
3950 if (output_target != NULL
3951 && strncmp (output_target, "efi-", 4) == 0)
3952 {
3953 char *efi;
3954
3955 efi = xstrdup (output_target + 4);
3956 if (strncmp (efi, "app-", 4) == 0)
3957 {
3958 if (pe_subsystem == -1)
3959 pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
3960 }
3961 else if (strncmp (efi, "bsdrv-", 6) == 0)
3962 {
3963 if (pe_subsystem == -1)
3964 pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
3965 efi += 2;
3966 }
3967 else if (strncmp (efi, "rtdrv-", 6) == 0)
3968 {
3969 if (pe_subsystem == -1)
3970 pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
3971 efi += 2;
3972 }
3973 else
3974 fatal (_("unknown output EFI target: %s"), output_target);
3975
3976 if (pe_file_alignment == (bfd_vma) -1)
3977 pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
3978 if (pe_section_alignment == (bfd_vma) -1)
3979 pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
3980
3981 output_target = efi;
3982 convert_efi_target (efi);
3983 }
3984
3985 if (preserve_dates)
3986 if (stat (input_filename, & statbuf) < 0)
3987 fatal (_("warning: could not locate '%s'. System error message: %s"),
3988 input_filename, strerror (errno));
3989
3990 /* If there is no destination file, or the source and destination files
3991 are the same, then create a temp and rename the result into the input. */
3992 if (output_filename == NULL
3993 || filename_cmp (input_filename, output_filename) == 0)
3994 tmpname = make_tempname (input_filename);
3995 else
3996 tmpname = output_filename;
3997
3998 if (tmpname == NULL)
3999 fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
4000 input_filename, strerror (errno));
4001
4002 copy_file (input_filename, tmpname, input_target, output_target, input_arch);
4003 if (status == 0)
4004 {
4005 if (preserve_dates)
4006 set_times (tmpname, &statbuf);
4007 if (tmpname != output_filename)
4008 status = (smart_rename (tmpname, input_filename,
4009 preserve_dates) != 0);
4010 }
4011 else
4012 unlink_if_ordinary (tmpname);
4013
4014 if (change_warn)
4015 {
4016 for (p = change_sections; p != NULL; p = p->next)
4017 {
4018 if (! p->used)
4019 {
4020 if (p->change_vma != CHANGE_IGNORE)
4021 {
4022 char buff [20];
4023
4024 sprintf_vma (buff, p->vma_val);
4025
4026 /* xgettext:c-format */
4027 non_fatal (_("%s %s%c0x%s never used"),
4028 "--change-section-vma",
4029 p->name,
4030 p->change_vma == CHANGE_SET ? '=' : '+',
4031 buff);
4032 }
4033
4034 if (p->change_lma != CHANGE_IGNORE)
4035 {
4036 char buff [20];
4037
4038 sprintf_vma (buff, p->lma_val);
4039
4040 /* xgettext:c-format */
4041 non_fatal (_("%s %s%c0x%s never used"),
4042 "--change-section-lma",
4043 p->name,
4044 p->change_lma == CHANGE_SET ? '=' : '+',
4045 buff);
4046 }
4047 }
4048 }
4049 }
4050
4051 return 0;
4052 }
4053
4054 int
4055 main (int argc, char *argv[])
4056 {
4057 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
4058 setlocale (LC_MESSAGES, "");
4059 #endif
4060 #if defined (HAVE_SETLOCALE)
4061 setlocale (LC_CTYPE, "");
4062 #endif
4063 bindtextdomain (PACKAGE, LOCALEDIR);
4064 textdomain (PACKAGE);
4065
4066 program_name = argv[0];
4067 xmalloc_set_program_name (program_name);
4068
4069 START_PROGRESS (program_name, 0);
4070
4071 expandargv (&argc, &argv);
4072
4073 strip_symbols = STRIP_UNDEF;
4074 discard_locals = LOCALS_UNDEF;
4075
4076 bfd_init ();
4077 set_default_bfd_target ();
4078
4079 if (is_strip < 0)
4080 {
4081 int i = strlen (program_name);
4082 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4083 /* Drop the .exe suffix, if any. */
4084 if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
4085 {
4086 i -= 4;
4087 program_name[i] = '\0';
4088 }
4089 #endif
4090 is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
4091 }
4092
4093 create_symbol_htabs ();
4094
4095 if (is_strip)
4096 strip_main (argc, argv);
4097 else
4098 copy_main (argc, argv);
4099
4100 END_PROGRESS (program_name);
4101
4102 return status;
4103 }