/src/binutils-gdb/bfd/format.c
Line | Count | Source |
1 | | /* Generic BFD support for file formats. |
2 | | Copyright (C) 1990-2026 Free Software Foundation, Inc. |
3 | | Written by Cygnus Support. |
4 | | |
5 | | This file is part of BFD, the Binary File Descriptor library. |
6 | | |
7 | | This program is free software; you can redistribute it and/or modify |
8 | | it under the terms of the GNU General Public License as published by |
9 | | the Free Software Foundation; either version 3 of the License, or |
10 | | (at your option) any later version. |
11 | | |
12 | | This program is distributed in the hope that it will be useful, |
13 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | GNU General Public License for more details. |
16 | | |
17 | | You should have received a copy of the GNU General Public License |
18 | | along with this program; if not, write to the Free Software |
19 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | | MA 02110-1301, USA. */ |
21 | | |
22 | | |
23 | | /* |
24 | | SECTION |
25 | | File formats |
26 | | |
27 | | A format is a BFD concept of high level file contents type. The |
28 | | formats supported by BFD are: |
29 | | |
30 | | o <<bfd_object>> |
31 | | |
32 | | The BFD may contain data, symbols, relocations and debug info. |
33 | | |
34 | | o <<bfd_archive>> |
35 | | |
36 | | The BFD contains other BFDs and an optional index. |
37 | | |
38 | | o <<bfd_core>> |
39 | | |
40 | | The BFD contains the result of an executable core dump. |
41 | | |
42 | | SUBSECTION |
43 | | File format functions |
44 | | */ |
45 | | |
46 | | #include "sysdep.h" |
47 | | #include "bfd.h" |
48 | | #include "libbfd.h" |
49 | | #include "plugin.h" |
50 | | #include "elf-bfd.h" |
51 | | |
52 | | /* IMPORT from targets.c. */ |
53 | | extern const size_t _bfd_target_vector_entries; |
54 | | |
55 | | /* |
56 | | FUNCTION |
57 | | bfd_check_format |
58 | | |
59 | | SYNOPSIS |
60 | | bool bfd_check_format (bfd *abfd, bfd_format format); |
61 | | |
62 | | DESCRIPTION |
63 | | Verify if the file attached to the BFD @var{abfd} is compatible |
64 | | with the format @var{format} (i.e., one of <<bfd_object>>, |
65 | | <<bfd_archive>> or <<bfd_core>>). |
66 | | |
67 | | If the BFD has been set to a specific target before the |
68 | | call, only the named target and format combination is |
69 | | checked. If the target has not been set, or has been set to |
70 | | <<default>>, then all the known target backends is |
71 | | interrogated to determine a match. If the default target |
72 | | matches, it is used. If not, exactly one target must recognize |
73 | | the file, or an error results. |
74 | | |
75 | | The function returns <<TRUE>> on success, otherwise <<FALSE>> |
76 | | with one of the following error codes: |
77 | | |
78 | | o <<bfd_error_invalid_operation>> - |
79 | | if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or |
80 | | <<bfd_core>>. |
81 | | |
82 | | o <<bfd_error_system_call>> - |
83 | | if an error occured during a read - even some file mismatches |
84 | | can cause bfd_error_system_calls. |
85 | | |
86 | | o <<file_not_recognised>> - |
87 | | none of the backends recognised the file format. |
88 | | |
89 | | o <<bfd_error_file_ambiguously_recognized>> - |
90 | | more than one backend recognised the file format. |
91 | | |
92 | | When calling bfd_check_format (or bfd_check_format_matches), |
93 | | any underlying file descriptor will be kept open for the |
94 | | duration of the call. This is done to avoid races when |
95 | | another thread calls bfd_cache_close_all. In this scenario, |
96 | | the thread calling bfd_check_format must call bfd_cache_close |
97 | | itself. |
98 | | */ |
99 | | |
100 | | bool |
101 | | bfd_check_format (bfd *abfd, bfd_format format) |
102 | 11.0M | { |
103 | 11.0M | return bfd_check_format_matches (abfd, format, NULL); |
104 | 11.0M | } |
105 | | |
106 | | struct bfd_preserve |
107 | | { |
108 | | void *marker; |
109 | | void *tdata; |
110 | | flagword flags; |
111 | | const struct bfd_iovec *iovec; |
112 | | void *iostream; |
113 | | const struct bfd_arch_info *arch_info; |
114 | | const struct bfd_build_id *build_id; |
115 | | bfd_cleanup cleanup; |
116 | | struct bfd_section *sections; |
117 | | struct bfd_section *section_last; |
118 | | unsigned int section_count; |
119 | | unsigned int section_id; |
120 | | unsigned int symcount; |
121 | | bool read_only; |
122 | | bfd_vma start_address; |
123 | | struct bfd_hash_table section_htab; |
124 | | }; |
125 | | |
126 | | /* When testing an object for compatibility with a particular target |
127 | | back-end, the back-end object_p function needs to set up certain |
128 | | fields in the bfd on successfully recognizing the object. This |
129 | | typically happens in a piecemeal fashion, with failures possible at |
130 | | many points. On failure, the bfd is supposed to be restored to its |
131 | | initial state, which is virtually impossible. However, restoring a |
132 | | subset of the bfd state works in practice. This function stores |
133 | | the subset. */ |
134 | | |
135 | | static bool |
136 | | bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve, |
137 | | bfd_cleanup cleanup) |
138 | 11.3M | { |
139 | 11.3M | preserve->tdata = abfd->tdata.any; |
140 | 11.3M | preserve->arch_info = abfd->arch_info; |
141 | 11.3M | preserve->flags = abfd->flags; |
142 | 11.3M | preserve->iovec = abfd->iovec; |
143 | 11.3M | preserve->iostream = abfd->iostream; |
144 | 11.3M | preserve->sections = abfd->sections; |
145 | 11.3M | preserve->section_last = abfd->section_last; |
146 | 11.3M | preserve->section_count = abfd->section_count; |
147 | 11.3M | preserve->section_id = _bfd_section_id; |
148 | 11.3M | preserve->symcount = abfd->symcount; |
149 | 11.3M | preserve->read_only = abfd->read_only; |
150 | 11.3M | preserve->start_address = abfd->start_address; |
151 | 11.3M | preserve->section_htab = abfd->section_htab; |
152 | 11.3M | preserve->marker = bfd_alloc (abfd, 1); |
153 | 11.3M | preserve->build_id = abfd->build_id; |
154 | 11.3M | preserve->cleanup = cleanup; |
155 | 11.3M | if (preserve->marker == NULL) |
156 | 0 | return false; |
157 | | |
158 | 11.3M | return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc, |
159 | 11.3M | sizeof (struct section_hash_entry)); |
160 | 11.3M | } |
161 | | |
162 | | /* A back-end object_p function may flip a bfd from file backed to |
163 | | in-memory, eg. pe_ILF_object_p. In that case to restore the |
164 | | original IO state we need to reopen the file. Conversely, if we |
165 | | are restoring a previously matched pe ILF format and have been |
166 | | checking further target matches using file IO then we need to close |
167 | | the file and detach the bfd from the cache lru list. */ |
168 | | |
169 | | static void |
170 | | io_reinit (bfd *abfd, struct bfd_preserve *preserve) |
171 | 89.8M | { |
172 | 89.8M | if (abfd->iovec != preserve->iovec) |
173 | 522 | { |
174 | | /* Handle file backed to in-memory transition. bfd_cache_close |
175 | | won't do anything unless abfd->iovec is the cache_iovec. |
176 | | Don't be tempted to call iovec->bclose here. We don't want |
177 | | to call memory_bclose, which would free the bim. The bim |
178 | | must be kept if bfd_check_format_matches is going to decide |
179 | | later that the PE format needing it is in fact the correct |
180 | | target match. */ |
181 | 522 | bfd_cache_close (abfd); |
182 | 522 | abfd->iovec = preserve->iovec; |
183 | 522 | abfd->iostream = preserve->iostream; |
184 | | |
185 | | /* Handle in-memory to file backed transition. */ |
186 | 522 | if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0 |
187 | 436 | && (abfd->flags & BFD_IN_MEMORY) != 0 |
188 | 253 | && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0 |
189 | 253 | && (preserve->flags & BFD_IN_MEMORY) == 0) |
190 | 253 | bfd_open_file (abfd); |
191 | 522 | } |
192 | 89.8M | abfd->flags = preserve->flags; |
193 | 89.8M | } |
194 | | |
195 | | /* Clear out a subset of BFD state. */ |
196 | | |
197 | | static void |
198 | | bfd_reinit (bfd *abfd, unsigned int section_id, |
199 | | struct bfd_preserve *preserve, bfd_cleanup cleanup) |
200 | 78.7M | { |
201 | 78.7M | _bfd_section_id = section_id; |
202 | 78.7M | if (cleanup) |
203 | 10.9M | cleanup (abfd); |
204 | 78.7M | abfd->tdata.any = NULL; |
205 | 78.7M | abfd->arch_info = &bfd_default_arch_struct; |
206 | 78.7M | io_reinit (abfd, preserve); |
207 | 78.7M | abfd->symcount = 0; |
208 | 78.7M | abfd->read_only = 0; |
209 | 78.7M | abfd->start_address = 0; |
210 | 78.7M | abfd->build_id = NULL; |
211 | 78.7M | bfd_section_list_clear (abfd); |
212 | 78.7M | } |
213 | | |
214 | | /* Restores bfd state saved by bfd_preserve_save. */ |
215 | | |
216 | | static bfd_cleanup |
217 | | bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) |
218 | 11.1M | { |
219 | 11.1M | bfd_hash_table_free (&abfd->section_htab); |
220 | | |
221 | 11.1M | abfd->tdata.any = preserve->tdata; |
222 | 11.1M | abfd->arch_info = preserve->arch_info; |
223 | 11.1M | io_reinit (abfd, preserve); |
224 | 11.1M | abfd->section_htab = preserve->section_htab; |
225 | 11.1M | abfd->sections = preserve->sections; |
226 | 11.1M | abfd->section_last = preserve->section_last; |
227 | 11.1M | abfd->section_count = preserve->section_count; |
228 | 11.1M | _bfd_section_id = preserve->section_id; |
229 | 11.1M | abfd->symcount = preserve->symcount; |
230 | 11.1M | abfd->read_only = preserve->read_only; |
231 | 11.1M | abfd->start_address = preserve->start_address; |
232 | 11.1M | abfd->build_id = preserve->build_id; |
233 | | |
234 | | /* bfd_release frees all memory more recently bfd_alloc'd than |
235 | | its arg, as well as its arg. */ |
236 | 11.1M | bfd_release (abfd, preserve->marker); |
237 | 11.1M | preserve->marker = NULL; |
238 | 11.1M | return preserve->cleanup; |
239 | 11.1M | } |
240 | | |
241 | | /* Called when the bfd state saved by bfd_preserve_save is no longer |
242 | | needed. */ |
243 | | |
244 | | static void |
245 | | bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve) |
246 | 204k | { |
247 | 204k | if (preserve->cleanup) |
248 | 0 | { |
249 | | /* Run the cleanup, assuming that all it will need is the |
250 | | tdata at the time the cleanup was returned. */ |
251 | 0 | void *tdata = abfd->tdata.any; |
252 | 0 | abfd->tdata.any = preserve->tdata; |
253 | 0 | preserve->cleanup (abfd); |
254 | 0 | abfd->tdata.any = tdata; |
255 | 0 | } |
256 | | /* It would be nice to be able to free more memory here, eg. old |
257 | | tdata, but that's not possible since these blocks are sitting |
258 | | inside bfd_alloc'd memory. The section hash is on a separate |
259 | | objalloc. */ |
260 | 204k | bfd_hash_table_free (&preserve->section_htab); |
261 | 204k | preserve->marker = NULL; |
262 | 204k | } |
263 | | |
264 | | static void |
265 | | print_warnmsg (struct per_xvec_message **list) |
266 | 11.0M | { |
267 | 11.2M | for (struct per_xvec_message *warn = *list; warn; warn = warn->next) |
268 | 208k | _bfd_error_handler ("%s", warn->message); |
269 | 11.0M | } |
270 | | |
271 | | static void |
272 | | clear_warnmsg (struct per_xvec_message **list) |
273 | 11.2M | { |
274 | 11.2M | struct per_xvec_message *warn = *list; |
275 | 11.6M | while (warn) |
276 | 357k | { |
277 | 357k | struct per_xvec_message *next = warn->next; |
278 | 357k | free (warn); |
279 | 357k | warn = next; |
280 | 357k | } |
281 | 11.2M | *list = NULL; |
282 | 11.2M | } |
283 | | |
284 | | /* Free all the storage in LIST. Note that the first element of LIST |
285 | | is special and is assumed to be stack-allocated. TARG is used for |
286 | | re-issuing warning messages. If TARG is PER_XVEC_NO_TARGET, then |
287 | | it acts like a sort of wildcard -- messages are reissued if all |
288 | | targets with messages have identical messages. One copy of the |
289 | | messages are then reissued. If TARG is anything else, then only |
290 | | messages associated with TARG are emitted. */ |
291 | | |
292 | | static void |
293 | | print_and_clear_messages (struct per_xvec_messages *list, |
294 | | const bfd_target *targ) |
295 | 11.2M | { |
296 | 11.2M | struct per_xvec_messages *iter; |
297 | | |
298 | 11.2M | if (targ == PER_XVEC_NO_TARGET) |
299 | 11.0M | { |
300 | 11.0M | iter = list->next; |
301 | 11.0M | while (iter != NULL) |
302 | 29.3k | { |
303 | 29.3k | struct per_xvec_message *msg1 = list->messages; |
304 | 29.3k | struct per_xvec_message *msg2 = iter->messages; |
305 | 29.3k | do |
306 | 46.0k | { |
307 | 46.0k | if (strcmp (msg1->message, msg2->message)) |
308 | 2.46k | break; |
309 | 43.5k | msg1 = msg1->next; |
310 | 43.5k | msg2 = msg2->next; |
311 | 43.5k | } while (msg1 && msg2); |
312 | 29.3k | if (msg1 || msg2) |
313 | 2.81k | break; |
314 | 26.4k | iter = iter->next; |
315 | 26.4k | } |
316 | 11.0M | if (iter == NULL) |
317 | 11.0M | targ = list->targ; |
318 | 11.0M | } |
319 | | |
320 | 11.2M | iter = list; |
321 | 22.5M | while (iter != NULL) |
322 | 11.2M | { |
323 | 11.2M | struct per_xvec_messages *next = iter->next; |
324 | | |
325 | 11.2M | if (iter->targ == targ) |
326 | 11.0M | print_warnmsg (&iter->messages); |
327 | 11.2M | clear_warnmsg (&iter->messages); |
328 | 11.2M | if (iter != list) |
329 | 71.8k | free (iter); |
330 | 11.2M | iter = next; |
331 | 11.2M | } |
332 | | |
333 | | /* Don't retain a pointer to free'd memory. */ |
334 | 11.2M | list->next = NULL; |
335 | 11.2M | } |
336 | | |
337 | | /* Discard all messages associated with TARG in LIST. Unlike |
338 | | print_and_clear_messages, PER_XVEC_NO_TARGET is not valid for TARG. */ |
339 | | |
340 | | static void |
341 | | clear_messages (struct per_xvec_messages *list, |
342 | | const bfd_target *targ) |
343 | 78.5M | { |
344 | 78.5M | struct per_xvec_messages *iter; |
345 | | |
346 | 166M | for (iter = list; iter != NULL; iter = iter->next) |
347 | 87.5M | { |
348 | 87.5M | if (iter->targ == targ) |
349 | 31 | clear_warnmsg (&iter->messages); |
350 | 87.5M | } |
351 | 78.5M | } |
352 | | |
353 | | /* This a copy of lto_section defined in GCC (lto-streamer.h). */ |
354 | | |
355 | | struct lto_section |
356 | | { |
357 | | int16_t major_version; |
358 | | int16_t minor_version; |
359 | | unsigned char slim_object; |
360 | | |
361 | | /* Flags is a private field that is not defined publicly. */ |
362 | | uint16_t flags; |
363 | | }; |
364 | | |
365 | | /* Set lto_type in ABFD. */ |
366 | | |
367 | | static void |
368 | | bfd_set_lto_type (bfd *abfd) |
369 | 204k | { |
370 | 204k | if (abfd->format == bfd_object |
371 | 144k | && abfd->lto_type == lto_non_object |
372 | 144k | && (abfd->flags |
373 | 144k | & (DYNAMIC |
374 | 144k | | (bfd_get_flavour (abfd) == bfd_target_elf_flavour |
375 | 144k | ? EXEC_P : 0))) == 0) |
376 | 137k | { |
377 | 137k | asection *sec = abfd->sections; |
378 | 137k | enum bfd_lto_object_type type = lto_non_ir_object; |
379 | 137k | if (sec == NULL) |
380 | 12.4k | { |
381 | | /* If there are no sections, check for slim LLVM IR object whose |
382 | | first 4 bytes are: 'B', 'C', 0xc0, 0xde. */ |
383 | 12.4k | bfd_byte llvm_ir_magic[4]; |
384 | 12.4k | if (bfd_seek (abfd, 0, SEEK_SET) == 0 |
385 | 12.4k | && bfd_read (llvm_ir_magic, 4, abfd) == 4 |
386 | 12.4k | && llvm_ir_magic[0] == 'B' |
387 | 10 | && llvm_ir_magic[1] == 'C' |
388 | 0 | && llvm_ir_magic[2] == 0xc0 |
389 | 0 | && llvm_ir_magic[3] == 0xde) |
390 | 0 | type = lto_slim_ir_object; |
391 | 12.4k | } |
392 | 125k | else |
393 | 125k | { |
394 | 125k | struct lto_section lsection = { 0, 0, 0, 0 }; |
395 | | /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode |
396 | | information section. */ |
397 | 2.32M | for (; sec != NULL; sec = sec->next) |
398 | 2.20M | if (strcmp (sec->name, GNU_OBJECT_ONLY_SECTION_NAME) == 0) |
399 | 27 | { |
400 | 27 | type = lto_mixed_object; |
401 | 27 | abfd->object_only_section = sec; |
402 | 27 | break; |
403 | 27 | } |
404 | 2.20M | else if (strcmp (sec->name, ".llvm.lto") == 0) |
405 | 80 | { |
406 | 80 | type = lto_fat_ir_object; |
407 | 80 | break; |
408 | 80 | } |
409 | 2.20M | else if (lsection.major_version == 0 |
410 | 2.20M | && startswith (sec->name, ".gnu.lto_.lto.") |
411 | 801 | && bfd_get_section_contents (abfd, sec, &lsection, 0, |
412 | 801 | sizeof (struct lto_section))) |
413 | 455 | { |
414 | 455 | if (lsection.slim_object) |
415 | 218 | type = lto_slim_ir_object; |
416 | 237 | else |
417 | 237 | type = lto_fat_ir_object; |
418 | 455 | } |
419 | 125k | } |
420 | | |
421 | 137k | abfd->lto_type = type; |
422 | 137k | } |
423 | 204k | } |
424 | | |
425 | | /* |
426 | | FUNCTION |
427 | | bfd_check_format_matches |
428 | | |
429 | | SYNOPSIS |
430 | | bool bfd_check_format_matches |
431 | | (bfd *abfd, bfd_format format, char ***matching); |
432 | | |
433 | | DESCRIPTION |
434 | | Like <<bfd_check_format>>, except when it returns FALSE with |
435 | | <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. |
436 | | In that case, if @var{matching} is not NULL, it will be filled |
437 | | in with a NULL-terminated list of the names of the formats |
438 | | that matched, allocated with <<malloc>>. |
439 | | Then the user may choose a format and try again. |
440 | | |
441 | | When done with the list that @var{matching} points to, the caller |
442 | | should free it. |
443 | | */ |
444 | | |
445 | | bool |
446 | | bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) |
447 | 11.2M | { |
448 | 11.2M | extern const bfd_target binary_vec; |
449 | 11.2M | const bfd_target * const *target; |
450 | 11.2M | const bfd_target **matching_vector; |
451 | 11.2M | const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ; |
452 | 11.2M | const bfd_target *fail_targ; |
453 | 11.2M | int match_count, best_count; |
454 | 11.2M | unsigned int best_match; |
455 | 11.2M | int ar_match_index; |
456 | 11.2M | unsigned int initial_section_id; |
457 | 11.2M | struct bfd_preserve preserve, preserve_match; |
458 | 11.2M | bfd_cleanup cleanup = NULL; |
459 | 11.2M | struct per_xvec_messages messages = { abfd, PER_XVEC_NO_TARGET, NULL, NULL }; |
460 | 11.2M | struct per_xvec_messages *orig_messages; |
461 | 11.2M | bool old_in_format_matches; |
462 | | |
463 | 11.2M | if (matching != NULL) |
464 | 125k | *matching = NULL; |
465 | | |
466 | 11.2M | if (!bfd_read_p (abfd) |
467 | 11.2M | || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) |
468 | 7.37k | { |
469 | 7.37k | bfd_set_error (bfd_error_invalid_operation); |
470 | 7.37k | return false; |
471 | 7.37k | } |
472 | | |
473 | 11.2M | if (abfd->format != bfd_unknown) |
474 | 94 | return abfd->format == format; |
475 | | |
476 | 11.2M | matching_vector = bfd_malloc (sizeof (*matching_vector) |
477 | 11.2M | * 2 * _bfd_target_vector_entries); |
478 | 11.2M | if (!matching_vector) |
479 | 0 | return false; |
480 | | |
481 | | /* Avoid clashes with bfd_cache_close_all running in another |
482 | | thread. */ |
483 | 11.2M | if (!bfd_cache_set_uncloseable (abfd, true, &old_in_format_matches)) |
484 | 0 | { |
485 | 0 | free (matching_vector); |
486 | 0 | return false; |
487 | 0 | } |
488 | | |
489 | | /* Locking is required here in order to manage _bfd_section_id. */ |
490 | 11.2M | if (!bfd_lock ()) |
491 | 0 | { |
492 | 0 | bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL); |
493 | 0 | free (matching_vector); |
494 | 0 | return false; |
495 | 0 | } |
496 | 11.2M | initial_section_id = _bfd_section_id; |
497 | | |
498 | | /* Presume the answer is yes. */ |
499 | 11.2M | abfd->format = format; |
500 | 11.2M | save_targ = abfd->xvec; |
501 | | |
502 | | /* Don't report errors on recursive calls checking the first element |
503 | | of an archive. */ |
504 | 11.2M | orig_messages = _bfd_set_error_handler_caching (&messages); |
505 | | |
506 | 11.2M | preserve_match.marker = NULL; |
507 | 11.2M | if (!bfd_preserve_save (abfd, &preserve, NULL)) |
508 | 0 | goto err_ret; |
509 | | |
510 | | /* First try matching the plugin target if appropriate. Next try |
511 | | the current target. The current target may have been set due to |
512 | | a user option, or due to the linker trying optimistically to load |
513 | | input files for the same target as the output. Either will |
514 | | have target_defaulted false. Failing that, bfd_find_target will |
515 | | have chosen a default target, and target_defaulted will be true. */ |
516 | 11.2M | fail_targ = NULL; |
517 | 11.2M | if (bfd_plugin_enabled () |
518 | 11.2M | && abfd->format == bfd_object |
519 | 11.0M | && abfd->target_defaulted |
520 | 148k | && !abfd->is_linker_input |
521 | 148k | && abfd->plugin_format != bfd_plugin_no) |
522 | 138k | { |
523 | 138k | if (bfd_seek (abfd, 0, SEEK_SET) != 0) |
524 | 0 | goto err_ret; |
525 | | |
526 | 138k | BFD_ASSERT (save_targ != bfd_plugin_vec ()); |
527 | 138k | abfd->xvec = bfd_plugin_vec (); |
528 | 138k | bfd_set_error (bfd_error_no_error); |
529 | 138k | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
530 | 138k | if (cleanup) |
531 | 0 | goto ok_ret; |
532 | | |
533 | 138k | bfd_reinit (abfd, initial_section_id, &preserve, cleanup); |
534 | 138k | bfd_release (abfd, preserve.marker); |
535 | 138k | preserve.marker = bfd_alloc (abfd, 1); |
536 | 138k | abfd->xvec = save_targ; |
537 | 138k | } |
538 | | |
539 | | /* bfd_plugin_no excluding the plugin target is an optimisation. |
540 | | The test can be removed if desired. */ |
541 | 11.2M | if (!(abfd->plugin_format == bfd_plugin_no |
542 | 22.4k | && bfd_plugin_target_p (save_targ))) |
543 | 11.2M | { |
544 | 11.2M | if (bfd_seek (abfd, 0, SEEK_SET) != 0) |
545 | 0 | goto err_ret; |
546 | | |
547 | 11.2M | bfd_set_error (bfd_error_no_error); |
548 | 11.2M | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
549 | 11.2M | if (cleanup) |
550 | 114k | { |
551 | 114k | if (abfd->format != bfd_archive |
552 | | /* An archive with object files matching the archive |
553 | | target is OK. Other archives should be further |
554 | | tested. */ |
555 | 49.0k | || bfd_get_error () != bfd_error_wrong_object_format) |
556 | 67.2k | goto ok_ret; |
557 | 114k | } |
558 | 11.1M | else |
559 | 11.1M | { |
560 | 11.1M | if (!abfd->target_defaulted && !abfd->is_linker_input) |
561 | 10.8M | goto err_unrecog; |
562 | 260k | fail_targ = save_targ; |
563 | 260k | } |
564 | 11.2M | } |
565 | | |
566 | | /* Check all targets in the hope that one will be recognized. */ |
567 | 307k | right_targ = NULL; |
568 | 307k | ar_right_targ = NULL; |
569 | 307k | match_targ = NULL; |
570 | 307k | best_match = -1u; |
571 | 307k | best_count = 0; |
572 | 307k | match_count = 0; |
573 | 307k | ar_match_index = _bfd_target_vector_entries; |
574 | | |
575 | 79.7M | for (target = bfd_target_vector; *target != NULL; target++) |
576 | 79.3M | { |
577 | 79.3M | void **high_water; |
578 | | |
579 | | /* The binary target matches anything, so don't return it when |
580 | | searching. Also, don't check the current target twice when |
581 | | it has failed already. |
582 | | Don't match the plugin target during linking if we have |
583 | | another alternative since we want to properly set the input |
584 | | format before allowing a plugin to claim the file. |
585 | | Also as an optimisation don't match the plugin target when |
586 | | abfd->plugin_format is set to bfd_plugin_no. (This occurs |
587 | | when LTO sections have been stripped or when we have a |
588 | | recursive call here from the plugin object_p via |
589 | | bfd_plugin_get_symbols_in_object_only.) */ |
590 | 79.3M | if (*target == &binary_vec |
591 | 79.0M | || *target == fail_targ |
592 | 78.5M | || (((abfd->is_linker_input && match_count != 0) |
593 | 78.5M | || abfd->plugin_format == bfd_plugin_no) |
594 | 5.45M | && bfd_plugin_target_p (*target))) |
595 | 846k | continue; |
596 | | |
597 | | /* If we already tried a match, the bfd is modified and may |
598 | | have sections attached, which will confuse the next |
599 | | _bfd_check_format call. */ |
600 | 78.5M | bfd_reinit (abfd, initial_section_id, &preserve, cleanup); |
601 | | /* Free bfd_alloc memory too. If we have matched and preserved |
602 | | a target then the high water mark is that much higher. */ |
603 | 78.5M | if (preserve_match.marker) |
604 | 28.9M | high_water = &preserve_match.marker; |
605 | 49.5M | else |
606 | 49.5M | high_water = &preserve.marker; |
607 | 78.5M | bfd_release (abfd, *high_water); |
608 | 78.5M | *high_water = bfd_alloc (abfd, 1); |
609 | | |
610 | | /* Change BFD's target temporarily. */ |
611 | 78.5M | abfd->xvec = *target; |
612 | | |
613 | | /* It is possible that targets appear multiple times in |
614 | | bfd_target_vector. If this is the case, then we want to avoid |
615 | | accumulating duplicate messages for a target in MESSAGES, so |
616 | | discard any previous messages associated with this target. */ |
617 | 78.5M | clear_messages (&messages, abfd->xvec); |
618 | | |
619 | 78.5M | if (bfd_seek (abfd, 0, SEEK_SET) != 0) |
620 | 0 | goto err_ret; |
621 | | |
622 | 78.5M | bfd_set_error (bfd_error_no_error); |
623 | 78.5M | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
624 | 78.5M | if (cleanup) |
625 | 11.0M | { |
626 | 11.0M | if (abfd->format != bfd_archive |
627 | 10.8M | || bfd_get_error () != bfd_error_wrong_object_format) |
628 | 210k | { |
629 | | /* If this is the default target, accept it, even if |
630 | | other targets might match. People who want those |
631 | | other targets have to set the GNUTARGET variable. */ |
632 | 210k | if (abfd->xvec == bfd_default_vector[0]) |
633 | 2 | goto ok_ret; |
634 | | |
635 | 210k | matching_vector[match_count] = abfd->xvec; |
636 | 210k | match_count++; |
637 | | |
638 | 210k | unsigned int match_priority = abfd->xvec->match_priority; |
639 | 210k | if (match_priority == 1 |
640 | 44.5k | && bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
641 | 41.2k | { |
642 | | /* If the object e_ident matches the hint elf_osabi, |
643 | | bump priority up. */ |
644 | 41.2k | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); |
645 | 41.2k | elf_backend_data *bed = get_elf_backend_data (abfd); |
646 | 41.2k | if (bed->elf_osabi != ELFOSABI_NONE |
647 | 2.00k | && i_ehdrp->e_ident[EI_OSABI] == bed->elf_osabi) |
648 | 3 | match_priority = 0; |
649 | 41.2k | } |
650 | 210k | match_priority <<= 16; |
651 | | /* A target that produces more sections is given a higher |
652 | | priority (smaller values of match_priority are higher |
653 | | priority) in order to prefer targets that decode note |
654 | | sections in core files. */ |
655 | 210k | if (abfd->section_count < 0xffff) |
656 | 210k | match_priority |= 0xffff - abfd->section_count; |
657 | 210k | if (match_priority < best_match) |
658 | 156k | { |
659 | 156k | best_match = match_priority; |
660 | 156k | best_count = 0; |
661 | 156k | } |
662 | 210k | if (match_priority <= best_match) |
663 | 198k | { |
664 | | /* This format checks out as ok! */ |
665 | 198k | right_targ = abfd->xvec; |
666 | 198k | best_count++; |
667 | 198k | } |
668 | 210k | } |
669 | 10.7M | else |
670 | 10.7M | { |
671 | | /* An archive with no armap or objects of the wrong |
672 | | type. We want this target to match if we get no |
673 | | better matches. */ |
674 | 10.7M | if (ar_right_targ != bfd_default_vector[0]) |
675 | 78.8k | ar_right_targ = *target; |
676 | 10.7M | matching_vector[ar_match_index] = *target; |
677 | 10.7M | ar_match_index++; |
678 | 10.7M | } |
679 | | |
680 | 11.0M | if (preserve_match.marker == NULL) |
681 | 151k | { |
682 | 151k | match_targ = abfd->xvec; |
683 | 151k | if (!bfd_preserve_save (abfd, &preserve_match, cleanup)) |
684 | 0 | goto err_ret; |
685 | 151k | cleanup = NULL; |
686 | 151k | } |
687 | 11.0M | } |
688 | 78.5M | } |
689 | | |
690 | 307k | if (best_count == 1) |
691 | 102k | match_count = 1; |
692 | | |
693 | 307k | if (match_count == 0) |
694 | 180k | { |
695 | | /* Try partial matches. */ |
696 | 180k | right_targ = ar_right_targ; |
697 | | |
698 | 180k | if (right_targ == bfd_default_vector[0]) |
699 | 22.5k | { |
700 | 22.5k | match_count = 1; |
701 | 22.5k | } |
702 | 158k | else |
703 | 158k | { |
704 | 158k | match_count = ar_match_index - _bfd_target_vector_entries; |
705 | | |
706 | 158k | if (match_count > 1) |
707 | 1.58k | memcpy (matching_vector, |
708 | 1.58k | matching_vector + _bfd_target_vector_entries, |
709 | 1.58k | sizeof (*matching_vector) * match_count); |
710 | 158k | } |
711 | 180k | } |
712 | | |
713 | | /* We have more than one equally good match. If any of the best |
714 | | matches is a target in config.bfd targ_defvec or targ_selvecs, |
715 | | choose it. */ |
716 | 307k | if (match_count > 1) |
717 | 25.8k | { |
718 | 25.8k | const bfd_target * const *assoc = bfd_associated_vector; |
719 | | |
720 | 258k | while ((right_targ = *assoc++) != NULL) |
721 | 236k | { |
722 | 236k | int i = match_count; |
723 | | |
724 | 1.11M | while (--i >= 0) |
725 | 880k | if (matching_vector[i] == right_targ |
726 | 10.1k | && right_targ->match_priority <= best_match >> 16) |
727 | 4.13k | break; |
728 | | |
729 | 236k | if (i >= 0) |
730 | 4.13k | { |
731 | 4.13k | match_count = 1; |
732 | 4.13k | break; |
733 | 4.13k | } |
734 | 236k | } |
735 | 25.8k | } |
736 | | |
737 | | /* We still have more than one equally good match, and at least some |
738 | | of the targets support match priority. Choose the first of the |
739 | | best matches. */ |
740 | 307k | if (match_count > 1 && best_count != match_count) |
741 | 7.57k | { |
742 | 7.57k | int i; |
743 | | |
744 | 12.6k | for (i = 0; i < match_count; i++) |
745 | 12.6k | { |
746 | 12.6k | right_targ = matching_vector[i]; |
747 | 12.6k | if (right_targ->match_priority <= best_match >> 16) |
748 | 7.57k | break; |
749 | 12.6k | } |
750 | 7.57k | match_count = 1; |
751 | 7.57k | } |
752 | | |
753 | | /* There is way too much undoing of half-known state here. We |
754 | | really shouldn't iterate on live bfd's. Note that saving the |
755 | | whole bfd and restoring it would be even worse; the first thing |
756 | | you notice is that the cached bfd file position gets out of sync. */ |
757 | 307k | if (preserve_match.marker != NULL) |
758 | 151k | cleanup = bfd_preserve_restore (abfd, &preserve_match); |
759 | | |
760 | 307k | if (match_count == 1) |
761 | 137k | { |
762 | 137k | abfd->xvec = right_targ; |
763 | | /* If we come out of the loop knowing that the last target that |
764 | | matched is the one we want, then ABFD should still be in a usable |
765 | | state (except possibly for XVEC). This is not just an |
766 | | optimisation. In the case of plugins a match against the |
767 | | plugin target can result in the bfd being changed such that |
768 | | it no longer matches the plugin target, nor will it match |
769 | | RIGHT_TARG again. */ |
770 | 137k | if (match_targ != right_targ) |
771 | 49.0k | { |
772 | 49.0k | bfd_reinit (abfd, initial_section_id, &preserve, cleanup); |
773 | 49.0k | bfd_release (abfd, preserve.marker); |
774 | 49.0k | if (bfd_seek (abfd, 0, SEEK_SET) != 0) |
775 | 0 | goto err_ret; |
776 | 49.0k | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
777 | 49.0k | BFD_ASSERT (cleanup != NULL); |
778 | 49.0k | } |
779 | | |
780 | 204k | ok_ret: |
781 | | /* If the file was opened for update, then `output_has_begun' |
782 | | some time ago when the file was created. Do not recompute |
783 | | sections sizes or alignments in _bfd_set_section_contents. |
784 | | We can not set this flag until after checking the format, |
785 | | because it will interfere with creation of BFD sections. */ |
786 | 204k | if (abfd->direction == both_direction) |
787 | 18.7k | abfd->output_has_begun = true; |
788 | | |
789 | 204k | free (matching_vector); |
790 | 204k | if (preserve_match.marker != NULL) |
791 | 0 | bfd_preserve_finish (abfd, &preserve_match); |
792 | 204k | bfd_preserve_finish (abfd, &preserve); |
793 | 204k | _bfd_restore_error_handler_caching (orig_messages); |
794 | | |
795 | 204k | print_and_clear_messages (&messages, abfd->xvec); |
796 | | |
797 | 204k | bfd_set_lto_type (abfd); |
798 | | |
799 | | /* File position has moved, BTW. */ |
800 | 204k | bool ret = bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL); |
801 | 204k | if (!bfd_unlock ()) |
802 | 0 | return false; |
803 | 204k | return ret; |
804 | 204k | } |
805 | | |
806 | 170k | if (match_count == 0) |
807 | 156k | { |
808 | 10.9M | err_unrecog: |
809 | 10.9M | bfd_set_error (bfd_error_file_not_recognized); |
810 | 10.9M | err_ret: |
811 | 10.9M | if (cleanup) |
812 | 0 | cleanup (abfd); |
813 | 10.9M | abfd->xvec = save_targ; |
814 | 10.9M | abfd->format = bfd_unknown; |
815 | 10.9M | free (matching_vector); |
816 | 10.9M | goto out; |
817 | 10.9M | } |
818 | | |
819 | | /* Restore original target type and format. */ |
820 | 14.1k | abfd->xvec = save_targ; |
821 | 14.1k | abfd->format = bfd_unknown; |
822 | 14.1k | bfd_set_error (bfd_error_file_ambiguously_recognized); |
823 | | |
824 | 14.1k | if (matching) |
825 | 3.52k | { |
826 | 3.52k | *matching = (char **) matching_vector; |
827 | 3.52k | matching_vector[match_count] = NULL; |
828 | | /* Return target names. This is a little nasty. Maybe we |
829 | | should do another bfd_malloc? */ |
830 | 13.4k | while (--match_count >= 0) |
831 | 9.96k | { |
832 | 9.96k | const char *name = matching_vector[match_count]->name; |
833 | 9.96k | *(const char **) &matching_vector[match_count] = name; |
834 | 9.96k | } |
835 | 3.52k | } |
836 | 10.6k | else |
837 | 10.6k | free (matching_vector); |
838 | 14.1k | if (cleanup) |
839 | 14.1k | cleanup (abfd); |
840 | 11.0M | out: |
841 | 11.0M | if (preserve_match.marker != NULL) |
842 | 0 | bfd_preserve_finish (abfd, &preserve_match); |
843 | 11.0M | if (preserve.marker != NULL) |
844 | 11.0M | bfd_preserve_restore (abfd, &preserve); |
845 | 11.0M | _bfd_restore_error_handler_caching (orig_messages); |
846 | 11.0M | print_and_clear_messages (&messages, PER_XVEC_NO_TARGET); |
847 | 11.0M | bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL); |
848 | 11.0M | bfd_unlock (); |
849 | 11.0M | return false; |
850 | 14.1k | } |
851 | | |
852 | | /* |
853 | | FUNCTION |
854 | | bfd_set_format |
855 | | |
856 | | SYNOPSIS |
857 | | bool bfd_set_format (bfd *abfd, bfd_format format); |
858 | | |
859 | | DESCRIPTION |
860 | | This function sets the file format of the BFD @var{abfd} to the |
861 | | format @var{format}. If the target set in the BFD does not |
862 | | support the format requested, the format is invalid, or the BFD |
863 | | is not open for writing, then an error occurs. |
864 | | */ |
865 | | |
866 | | bool |
867 | | bfd_set_format (bfd *abfd, bfd_format format) |
868 | 20.5k | { |
869 | 20.5k | if (bfd_read_p (abfd) |
870 | 20.5k | || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) |
871 | 0 | { |
872 | 0 | bfd_set_error (bfd_error_invalid_operation); |
873 | 0 | return false; |
874 | 0 | } |
875 | | |
876 | 20.5k | if (abfd->format != bfd_unknown) |
877 | 6.15k | return abfd->format == format; |
878 | | |
879 | | /* Presume the answer is yes. */ |
880 | 14.4k | abfd->format = format; |
881 | | |
882 | 14.4k | if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) |
883 | 5.85k | { |
884 | 5.85k | abfd->format = bfd_unknown; |
885 | 5.85k | return false; |
886 | 5.85k | } |
887 | | |
888 | 8.57k | return true; |
889 | 14.4k | } |
890 | | |
891 | | /* |
892 | | FUNCTION |
893 | | bfd_format_string |
894 | | |
895 | | SYNOPSIS |
896 | | const char *bfd_format_string (bfd_format format); |
897 | | |
898 | | DESCRIPTION |
899 | | Return a pointer to a const string |
900 | | <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>, |
901 | | depending upon the value of @var{format}. |
902 | | */ |
903 | | |
904 | | const char * |
905 | | bfd_format_string (bfd_format format) |
906 | 0 | { |
907 | 0 | if (((int) format < (int) bfd_unknown) |
908 | 0 | || ((int) format >= (int) bfd_type_end)) |
909 | 0 | return "invalid"; |
910 | | |
911 | 0 | switch (format) |
912 | 0 | { |
913 | 0 | case bfd_object: |
914 | 0 | return "object"; /* Linker/assembler/compiler output. */ |
915 | 0 | case bfd_archive: |
916 | 0 | return "archive"; /* Object archive file. */ |
917 | 0 | case bfd_core: |
918 | 0 | return "core"; /* Core dump. */ |
919 | 0 | default: |
920 | 0 | return "unknown"; |
921 | 0 | } |
922 | 0 | } |