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