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