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