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