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