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