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