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