/src/binutils-gdb/bfd/format.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Generic BFD support for file formats. |
2 | | Copyright (C) 1990-2023 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 | | |
50 | | /* IMPORT from targets.c. */ |
51 | | extern const size_t _bfd_target_vector_entries; |
52 | | |
53 | | /* |
54 | | FUNCTION |
55 | | bfd_check_format |
56 | | |
57 | | SYNOPSIS |
58 | | bool bfd_check_format (bfd *abfd, bfd_format format); |
59 | | |
60 | | DESCRIPTION |
61 | | Verify if the file attached to the BFD @var{abfd} is compatible |
62 | | with the format @var{format} (i.e., one of <<bfd_object>>, |
63 | | <<bfd_archive>> or <<bfd_core>>). |
64 | | |
65 | | If the BFD has been set to a specific target before the |
66 | | call, only the named target and format combination is |
67 | | checked. If the target has not been set, or has been set to |
68 | | <<default>>, then all the known target backends is |
69 | | interrogated to determine a match. If the default target |
70 | | matches, it is used. If not, exactly one target must recognize |
71 | | the file, or an error results. |
72 | | |
73 | | The function returns <<TRUE>> on success, otherwise <<FALSE>> |
74 | | with one of the following error codes: |
75 | | |
76 | | o <<bfd_error_invalid_operation>> - |
77 | | if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or |
78 | | <<bfd_core>>. |
79 | | |
80 | | o <<bfd_error_system_call>> - |
81 | | if an error occured during a read - even some file mismatches |
82 | | can cause bfd_error_system_calls. |
83 | | |
84 | | o <<file_not_recognised>> - |
85 | | none of the backends recognised the file format. |
86 | | |
87 | | o <<bfd_error_file_ambiguously_recognized>> - |
88 | | more than one backend recognised the file format. |
89 | | */ |
90 | | |
91 | | bool |
92 | | bfd_check_format (bfd *abfd, bfd_format format) |
93 | 1.71M | { |
94 | 1.71M | return bfd_check_format_matches (abfd, format, NULL); |
95 | 1.71M | } |
96 | | |
97 | | struct bfd_preserve |
98 | | { |
99 | | void *marker; |
100 | | void *tdata; |
101 | | flagword flags; |
102 | | const struct bfd_iovec *iovec; |
103 | | void *iostream; |
104 | | const struct bfd_arch_info *arch_info; |
105 | | const struct bfd_build_id *build_id; |
106 | | bfd_cleanup cleanup; |
107 | | struct bfd_section *sections; |
108 | | struct bfd_section *section_last; |
109 | | unsigned int section_count; |
110 | | unsigned int section_id; |
111 | | unsigned int symcount; |
112 | | bool read_only; |
113 | | bfd_vma start_address; |
114 | | struct bfd_hash_table section_htab; |
115 | | }; |
116 | | |
117 | | /* When testing an object for compatibility with a particular target |
118 | | back-end, the back-end object_p function needs to set up certain |
119 | | fields in the bfd on successfully recognizing the object. This |
120 | | typically happens in a piecemeal fashion, with failures possible at |
121 | | many points. On failure, the bfd is supposed to be restored to its |
122 | | initial state, which is virtually impossible. However, restoring a |
123 | | subset of the bfd state works in practice. This function stores |
124 | | the subset. */ |
125 | | |
126 | | static bool |
127 | | bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve, |
128 | | bfd_cleanup cleanup) |
129 | 3.17M | { |
130 | 3.17M | preserve->tdata = abfd->tdata.any; |
131 | 3.17M | preserve->arch_info = abfd->arch_info; |
132 | 3.17M | preserve->flags = abfd->flags; |
133 | 3.17M | preserve->iovec = abfd->iovec; |
134 | 3.17M | preserve->iostream = abfd->iostream; |
135 | 3.17M | preserve->sections = abfd->sections; |
136 | 3.17M | preserve->section_last = abfd->section_last; |
137 | 3.17M | preserve->section_count = abfd->section_count; |
138 | 3.17M | preserve->section_id = _bfd_section_id; |
139 | 3.17M | preserve->symcount = abfd->symcount; |
140 | 3.17M | preserve->read_only = abfd->read_only; |
141 | 3.17M | preserve->start_address = abfd->start_address; |
142 | 3.17M | preserve->section_htab = abfd->section_htab; |
143 | 3.17M | preserve->marker = bfd_alloc (abfd, 1); |
144 | 3.17M | preserve->build_id = abfd->build_id; |
145 | 3.17M | preserve->cleanup = cleanup; |
146 | 3.17M | if (preserve->marker == NULL) |
147 | 0 | return false; |
148 | | |
149 | 3.17M | return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc, |
150 | 3.17M | sizeof (struct section_hash_entry)); |
151 | 3.17M | } |
152 | | |
153 | | /* A back-end object_p function may flip a bfd from file backed to |
154 | | in-memory, eg. pe_ILF_object_p. In that case to restore the |
155 | | original IO state we need to reopen the file. Conversely, if we |
156 | | are restoring a previously matched pe ILF format and have been |
157 | | checking further target matches using file IO then we need to close |
158 | | the file and detach the bfd from the cache lru list. */ |
159 | | |
160 | | static void |
161 | | io_reinit (bfd *abfd, struct bfd_preserve *preserve) |
162 | 463M | { |
163 | 463M | if (abfd->iovec != preserve->iovec) |
164 | 31.3k | { |
165 | | /* Handle file backed to in-memory transition. bfd_cache_close |
166 | | won't do anything unless abfd->iovec is the cache_iovec. */ |
167 | 31.3k | bfd_cache_close (abfd); |
168 | 31.3k | abfd->iovec = preserve->iovec; |
169 | 31.3k | abfd->iostream = preserve->iostream; |
170 | | /* Handle in-memory to file backed transition. */ |
171 | 31.3k | if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0 |
172 | 31.3k | && (abfd->flags & BFD_IN_MEMORY) != 0 |
173 | 31.3k | && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0 |
174 | 31.3k | && (preserve->flags & BFD_IN_MEMORY) == 0) |
175 | 469 | bfd_open_file (abfd); |
176 | 31.3k | } |
177 | 463M | abfd->flags = preserve->flags; |
178 | 463M | } |
179 | | |
180 | | /* Clear out a subset of BFD state. */ |
181 | | |
182 | | static void |
183 | | bfd_reinit (bfd *abfd, unsigned int section_id, |
184 | | struct bfd_preserve *preserve, bfd_cleanup cleanup) |
185 | 461M | { |
186 | 461M | _bfd_section_id = section_id; |
187 | 461M | if (cleanup) |
188 | 3.63M | cleanup (abfd); |
189 | 461M | abfd->tdata.any = NULL; |
190 | 461M | abfd->arch_info = &bfd_default_arch_struct; |
191 | 461M | io_reinit (abfd, preserve); |
192 | 461M | abfd->symcount = 0; |
193 | 461M | abfd->read_only = 0; |
194 | 461M | abfd->start_address = 0; |
195 | 461M | abfd->build_id = NULL; |
196 | 461M | bfd_section_list_clear (abfd); |
197 | 461M | } |
198 | | |
199 | | /* Restores bfd state saved by bfd_preserve_save. */ |
200 | | |
201 | | static bfd_cleanup |
202 | | bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) |
203 | 1.86M | { |
204 | 1.86M | bfd_hash_table_free (&abfd->section_htab); |
205 | | |
206 | 1.86M | abfd->tdata.any = preserve->tdata; |
207 | 1.86M | abfd->arch_info = preserve->arch_info; |
208 | 1.86M | io_reinit (abfd, preserve); |
209 | 1.86M | abfd->section_htab = preserve->section_htab; |
210 | 1.86M | abfd->sections = preserve->sections; |
211 | 1.86M | abfd->section_last = preserve->section_last; |
212 | 1.86M | abfd->section_count = preserve->section_count; |
213 | 1.86M | _bfd_section_id = preserve->section_id; |
214 | 1.86M | abfd->symcount = preserve->symcount; |
215 | 1.86M | abfd->read_only = preserve->read_only; |
216 | 1.86M | abfd->start_address = preserve->start_address; |
217 | 1.86M | abfd->build_id = preserve->build_id; |
218 | | |
219 | | /* bfd_release frees all memory more recently bfd_alloc'd than |
220 | | its arg, as well as its arg. */ |
221 | 1.86M | bfd_release (abfd, preserve->marker); |
222 | 1.86M | preserve->marker = NULL; |
223 | 1.86M | return preserve->cleanup; |
224 | 1.86M | } |
225 | | |
226 | | /* Called when the bfd state saved by bfd_preserve_save is no longer |
227 | | needed. */ |
228 | | |
229 | | static void |
230 | | bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve) |
231 | 1.30M | { |
232 | 1.30M | if (preserve->cleanup) |
233 | 0 | { |
234 | | /* Run the cleanup, assuming that all it will need is the |
235 | | tdata at the time the cleanup was returned. */ |
236 | 0 | void *tdata = abfd->tdata.any; |
237 | 0 | abfd->tdata.any = preserve->tdata; |
238 | 0 | preserve->cleanup (abfd); |
239 | 0 | abfd->tdata.any = tdata; |
240 | 0 | } |
241 | | /* It would be nice to be able to free more memory here, eg. old |
242 | | tdata, but that's not possible since these blocks are sitting |
243 | | inside bfd_alloc'd memory. The section hash is on a separate |
244 | | objalloc. */ |
245 | 1.30M | bfd_hash_table_free (&preserve->section_htab); |
246 | 1.30M | preserve->marker = NULL; |
247 | 1.30M | } |
248 | | |
249 | | static void |
250 | | print_warnmsg (struct per_xvec_message **list) |
251 | 42.5k | { |
252 | 42.5k | fflush (stdout); |
253 | 42.5k | fprintf (stderr, "%s: ", _bfd_get_error_program_name ()); |
254 | | |
255 | 163k | for (struct per_xvec_message *warn = *list; warn; warn = warn->next) |
256 | 121k | { |
257 | 121k | fputs (warn->message, stderr); |
258 | 121k | fputc ('\n', stderr); |
259 | 121k | } |
260 | 42.5k | fflush (stderr); |
261 | 42.5k | } |
262 | | |
263 | | static void |
264 | | clear_warnmsg (struct per_xvec_message **list) |
265 | 474M | { |
266 | 474M | struct per_xvec_message *warn = *list; |
267 | 474M | while (warn) |
268 | 237k | { |
269 | 237k | struct per_xvec_message *next = warn->next; |
270 | 237k | free (warn); |
271 | 237k | warn = next; |
272 | 237k | } |
273 | 474M | *list = NULL; |
274 | 474M | } |
275 | | |
276 | | static void |
277 | | null_error_handler (const char *fmt ATTRIBUTE_UNUSED, |
278 | | va_list ap ATTRIBUTE_UNUSED) |
279 | 27.0M | { |
280 | 27.0M | } |
281 | | |
282 | | /* |
283 | | FUNCTION |
284 | | bfd_check_format_matches |
285 | | |
286 | | SYNOPSIS |
287 | | bool bfd_check_format_matches |
288 | | (bfd *abfd, bfd_format format, char ***matching); |
289 | | |
290 | | DESCRIPTION |
291 | | Like <<bfd_check_format>>, except when it returns FALSE with |
292 | | <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that |
293 | | case, if @var{matching} is not NULL, it will be filled in with |
294 | | a NULL-terminated list of the names of the formats that matched, |
295 | | allocated with <<malloc>>. |
296 | | Then the user may choose a format and try again. |
297 | | |
298 | | When done with the list that @var{matching} points to, the caller |
299 | | should free it. |
300 | | */ |
301 | | |
302 | | bool |
303 | | bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) |
304 | 1.80M | { |
305 | 1.80M | extern const bfd_target binary_vec; |
306 | 1.80M | #if BFD_SUPPORTS_PLUGINS |
307 | 1.80M | extern const bfd_target plugin_vec; |
308 | 1.80M | #endif |
309 | 1.80M | const bfd_target * const *target; |
310 | 1.80M | const bfd_target **matching_vector = NULL; |
311 | 1.80M | const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ; |
312 | 1.80M | int match_count, best_count, best_match; |
313 | 1.80M | int ar_match_index; |
314 | 1.80M | unsigned int initial_section_id = _bfd_section_id; |
315 | 1.80M | struct bfd_preserve preserve, preserve_match; |
316 | 1.80M | bfd_cleanup cleanup = NULL; |
317 | 1.80M | bfd_error_handler_type orig_error_handler; |
318 | 1.80M | static int in_check_format; |
319 | | |
320 | 1.80M | if (matching != NULL) |
321 | 90.5k | *matching = NULL; |
322 | | |
323 | 1.80M | if (!bfd_read_p (abfd) |
324 | 1.80M | || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) |
325 | 672 | { |
326 | 672 | bfd_set_error (bfd_error_invalid_operation); |
327 | 672 | return false; |
328 | 672 | } |
329 | | |
330 | 1.80M | if (abfd->format != bfd_unknown) |
331 | 15 | return abfd->format == format; |
332 | | |
333 | 1.80M | if (matching != NULL || *bfd_associated_vector != NULL) |
334 | 1.80M | { |
335 | 1.80M | size_t amt; |
336 | | |
337 | 1.80M | amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries; |
338 | 1.80M | matching_vector = (const bfd_target **) bfd_malloc (amt); |
339 | 1.80M | if (!matching_vector) |
340 | 0 | return false; |
341 | 1.80M | } |
342 | | |
343 | | /* Presume the answer is yes. */ |
344 | 1.80M | abfd->format = format; |
345 | 1.80M | save_targ = abfd->xvec; |
346 | | |
347 | | /* Don't report errors on recursive calls checking the first element |
348 | | of an archive. */ |
349 | 1.80M | if (in_check_format) |
350 | 1.55M | orig_error_handler = bfd_set_error_handler (null_error_handler); |
351 | 253k | else |
352 | 253k | orig_error_handler = _bfd_set_error_handler_caching (abfd); |
353 | 1.80M | ++in_check_format; |
354 | | |
355 | 1.80M | preserve_match.marker = NULL; |
356 | 1.80M | if (!bfd_preserve_save (abfd, &preserve, NULL)) |
357 | 0 | goto err_ret; |
358 | | |
359 | | /* If the target type was explicitly specified, just check that target. */ |
360 | 1.80M | if (!abfd->target_defaulted) |
361 | 1.57M | { |
362 | 1.57M | if (bfd_seek (abfd, 0, SEEK_SET) != 0) /* rewind! */ |
363 | 0 | goto err_ret; |
364 | | |
365 | 1.57M | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
366 | | |
367 | 1.57M | if (cleanup) |
368 | 7.39k | goto ok_ret; |
369 | | |
370 | | /* For a long time the code has dropped through to check all |
371 | | targets if the specified target was wrong. I don't know why, |
372 | | and I'm reluctant to change it. However, in the case of an |
373 | | archive, it can cause problems. If the specified target does |
374 | | not permit archives (e.g., the binary target), then we should |
375 | | not allow some other target to recognize it as an archive, but |
376 | | should instead allow the specified target to recognize it as an |
377 | | object. When I first made this change, it broke the PE target, |
378 | | because the specified pei-i386 target did not recognize the |
379 | | actual pe-i386 archive. Since there may be other problems of |
380 | | this sort, I changed this test to check only for the binary |
381 | | target. */ |
382 | 1.57M | if (format == bfd_archive && save_targ == &binary_vec) |
383 | 0 | goto err_unrecog; |
384 | 1.57M | } |
385 | | |
386 | | /* Since the target type was defaulted, check them all in the hope |
387 | | that one will be uniquely recognized. */ |
388 | 1.79M | right_targ = NULL; |
389 | 1.79M | ar_right_targ = NULL; |
390 | 1.79M | match_targ = NULL; |
391 | 1.79M | best_match = 256; |
392 | 1.79M | best_count = 0; |
393 | 1.79M | match_count = 0; |
394 | 1.79M | ar_match_index = _bfd_target_vector_entries; |
395 | | |
396 | 467M | for (target = bfd_target_vector; *target != NULL; target++) |
397 | 466M | { |
398 | 466M | void **high_water; |
399 | | |
400 | | /* The binary target matches anything, so don't return it when |
401 | | searching. Don't match the plugin target if we have another |
402 | | alternative since we want to properly set the input format |
403 | | before allowing a plugin to claim the file. Also, don't |
404 | | check the default target twice. */ |
405 | 466M | if (*target == &binary_vec |
406 | 466M | #if BFD_SUPPORTS_PLUGINS |
407 | 466M | || (match_count != 0 && *target == &plugin_vec) |
408 | 466M | #endif |
409 | 466M | || (!abfd->target_defaulted && *target == save_targ)) |
410 | 4.71M | continue; |
411 | | |
412 | | /* If we already tried a match, the bfd is modified and may |
413 | | have sections attached, which will confuse the next |
414 | | _bfd_check_format call. */ |
415 | 461M | bfd_reinit (abfd, initial_section_id, &preserve, cleanup); |
416 | | /* Free bfd_alloc memory too. If we have matched and preserved |
417 | | a target then the high water mark is that much higher. */ |
418 | 461M | if (preserve_match.marker) |
419 | 196M | high_water = &preserve_match.marker; |
420 | 264M | else |
421 | 264M | high_water = &preserve.marker; |
422 | 461M | bfd_release (abfd, *high_water); |
423 | 461M | *high_water = bfd_alloc (abfd, 1); |
424 | | |
425 | | /* Change BFD's target temporarily. */ |
426 | 461M | abfd->xvec = *target; |
427 | | |
428 | 461M | if (bfd_seek (abfd, 0, SEEK_SET) != 0) |
429 | 0 | goto err_ret; |
430 | | |
431 | 461M | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
432 | 461M | if (cleanup) |
433 | 4.80M | { |
434 | 4.80M | int match_priority = abfd->xvec->match_priority; |
435 | 4.80M | #if BFD_SUPPORTS_PLUGINS |
436 | | /* If this object can be handled by a plugin, give that the |
437 | | lowest priority; objects both handled by a plugin and |
438 | | with an underlying object format will be claimed |
439 | | separately by the plugin. */ |
440 | 4.80M | if (*target == &plugin_vec) |
441 | 7.64k | match_priority = (*target)->match_priority; |
442 | 4.80M | #endif |
443 | | |
444 | 4.80M | if (abfd->format != bfd_archive |
445 | 4.80M | || (bfd_has_map (abfd) |
446 | 2.99M | && bfd_get_error () != bfd_error_wrong_object_format)) |
447 | 2.16M | { |
448 | | /* If this is the default target, accept it, even if |
449 | | other targets might match. People who want those |
450 | | other targets have to set the GNUTARGET variable. */ |
451 | 2.16M | if (abfd->xvec == bfd_default_vector[0]) |
452 | 11.5k | goto ok_ret; |
453 | | |
454 | 2.15M | if (matching_vector) |
455 | 2.15M | matching_vector[match_count] = abfd->xvec; |
456 | 2.15M | match_count++; |
457 | | |
458 | 2.15M | if (match_priority < best_match) |
459 | 1.53M | { |
460 | 1.53M | best_match = match_priority; |
461 | 1.53M | best_count = 0; |
462 | 1.53M | } |
463 | 2.15M | if (match_priority <= best_match) |
464 | 1.87M | { |
465 | | /* This format checks out as ok! */ |
466 | 1.87M | right_targ = abfd->xvec; |
467 | 1.87M | best_count++; |
468 | 1.87M | } |
469 | 2.15M | } |
470 | 2.63M | else |
471 | 2.63M | { |
472 | | /* An archive with no armap or objects of the wrong |
473 | | type. We want this target to match if we get no |
474 | | better matches. */ |
475 | 2.63M | if (ar_right_targ != bfd_default_vector[0]) |
476 | 184k | ar_right_targ = *target; |
477 | 2.63M | if (matching_vector) |
478 | 2.63M | matching_vector[ar_match_index] = *target; |
479 | 2.63M | ar_match_index++; |
480 | 2.63M | } |
481 | | |
482 | 4.79M | if (preserve_match.marker == NULL) |
483 | 1.36M | { |
484 | 1.36M | match_targ = abfd->xvec; |
485 | 1.36M | if (!bfd_preserve_save (abfd, &preserve_match, cleanup)) |
486 | 0 | goto err_ret; |
487 | 1.36M | cleanup = NULL; |
488 | 1.36M | } |
489 | 4.79M | } |
490 | 461M | } |
491 | | |
492 | 1.78M | if (best_count == 1) |
493 | 1.18M | match_count = 1; |
494 | | |
495 | 1.78M | if (match_count == 0) |
496 | 434k | { |
497 | | /* Try partial matches. */ |
498 | 434k | right_targ = ar_right_targ; |
499 | | |
500 | 434k | if (right_targ == bfd_default_vector[0]) |
501 | 7.64k | { |
502 | 7.64k | match_count = 1; |
503 | 7.64k | } |
504 | 426k | else |
505 | 426k | { |
506 | 426k | match_count = ar_match_index - _bfd_target_vector_entries; |
507 | | |
508 | 426k | if (matching_vector && match_count > 1) |
509 | 4.80k | memcpy (matching_vector, |
510 | 4.80k | matching_vector + _bfd_target_vector_entries, |
511 | 4.80k | sizeof (*matching_vector) * match_count); |
512 | 426k | } |
513 | 434k | } |
514 | | |
515 | | /* We have more than one equally good match. If any of the best |
516 | | matches is a target in config.bfd targ_defvec or targ_selvecs, |
517 | | choose it. */ |
518 | 1.78M | if (match_count > 1) |
519 | 176k | { |
520 | 176k | const bfd_target * const *assoc = bfd_associated_vector; |
521 | | |
522 | 1.81M | while ((right_targ = *assoc++) != NULL) |
523 | 1.68M | { |
524 | 1.68M | int i = match_count; |
525 | | |
526 | 10.8M | while (--i >= 0) |
527 | 9.25M | if (matching_vector[i] == right_targ |
528 | 9.25M | && right_targ->match_priority <= best_match) |
529 | 51.1k | break; |
530 | | |
531 | 1.68M | if (i >= 0) |
532 | 51.1k | { |
533 | 51.1k | match_count = 1; |
534 | 51.1k | break; |
535 | 51.1k | } |
536 | 1.68M | } |
537 | 176k | } |
538 | | |
539 | | /* We still have more than one equally good match, and at least some |
540 | | of the targets support match priority. Choose the first of the |
541 | | best matches. */ |
542 | 1.78M | if (matching_vector && match_count > 1 && best_count != match_count) |
543 | 41.7k | { |
544 | 41.7k | int i; |
545 | | |
546 | 78.8k | for (i = 0; i < match_count; i++) |
547 | 78.8k | { |
548 | 78.8k | right_targ = matching_vector[i]; |
549 | 78.8k | if (right_targ->match_priority <= best_match) |
550 | 41.7k | break; |
551 | 78.8k | } |
552 | 41.7k | match_count = 1; |
553 | 41.7k | } |
554 | | |
555 | | /* There is way too much undoing of half-known state here. We |
556 | | really shouldn't iterate on live bfd's. Note that saving the |
557 | | whole bfd and restoring it would be even worse; the first thing |
558 | | you notice is that the cached bfd file position gets out of sync. */ |
559 | 1.78M | if (preserve_match.marker != NULL) |
560 | 1.36M | cleanup = bfd_preserve_restore (abfd, &preserve_match); |
561 | | |
562 | 1.78M | if (match_count == 1) |
563 | 1.28M | { |
564 | 1.28M | abfd->xvec = right_targ; |
565 | | /* If we come out of the loop knowing that the last target that |
566 | | matched is the one we want, then ABFD should still be in a usable |
567 | | state (except possibly for XVEC). This is not just an |
568 | | optimisation. In the case of plugins a match against the |
569 | | plugin target can result in the bfd being changed such that |
570 | | it no longer matches the plugin target, nor will it match |
571 | | RIGHT_TARG again. */ |
572 | 1.28M | if (match_targ != right_targ) |
573 | 211k | { |
574 | 211k | bfd_reinit (abfd, initial_section_id, &preserve, cleanup); |
575 | 211k | bfd_release (abfd, preserve.marker); |
576 | 211k | if (bfd_seek (abfd, 0, SEEK_SET) != 0) |
577 | 0 | goto err_ret; |
578 | 211k | cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd)); |
579 | 211k | BFD_ASSERT (cleanup != NULL); |
580 | 211k | } |
581 | | |
582 | 1.30M | ok_ret: |
583 | | /* If the file was opened for update, then `output_has_begun' |
584 | | some time ago when the file was created. Do not recompute |
585 | | sections sizes or alignments in _bfd_set_section_contents. |
586 | | We can not set this flag until after checking the format, |
587 | | because it will interfere with creation of BFD sections. */ |
588 | 1.30M | if (abfd->direction == both_direction) |
589 | 12.7k | abfd->output_has_begun = true; |
590 | | |
591 | 1.30M | free (matching_vector); |
592 | 1.30M | if (preserve_match.marker != NULL) |
593 | 0 | bfd_preserve_finish (abfd, &preserve_match); |
594 | 1.30M | bfd_preserve_finish (abfd, &preserve); |
595 | 1.30M | bfd_set_error_handler (orig_error_handler); |
596 | | |
597 | 1.30M | struct per_xvec_message **list = _bfd_per_xvec_warn (abfd->xvec, 0); |
598 | 1.30M | if (*list) |
599 | 25.5k | print_warnmsg (list); |
600 | 1.30M | list = _bfd_per_xvec_warn (NULL, 0); |
601 | 343M | for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++) |
602 | 342M | clear_warnmsg (list++); |
603 | 1.30M | --in_check_format; |
604 | | |
605 | | /* File position has moved, BTW. */ |
606 | 1.30M | return true; |
607 | 1.28M | } |
608 | | |
609 | 504k | if (match_count == 0) |
610 | 421k | { |
611 | 421k | err_unrecog: |
612 | 421k | bfd_set_error (bfd_error_file_not_recognized); |
613 | 421k | err_ret: |
614 | 421k | if (cleanup) |
615 | 0 | cleanup (abfd); |
616 | 421k | abfd->xvec = save_targ; |
617 | 421k | abfd->format = bfd_unknown; |
618 | 421k | free (matching_vector); |
619 | 421k | goto out; |
620 | 421k | } |
621 | | |
622 | | /* Restore original target type and format. */ |
623 | 83.4k | abfd->xvec = save_targ; |
624 | 83.4k | abfd->format = bfd_unknown; |
625 | 83.4k | bfd_set_error (bfd_error_file_ambiguously_recognized); |
626 | | |
627 | 83.4k | if (matching) |
628 | 3.18k | { |
629 | 3.18k | *matching = (char **) matching_vector; |
630 | 3.18k | matching_vector[match_count] = NULL; |
631 | | /* Return target names. This is a little nasty. Maybe we |
632 | | should do another bfd_malloc? */ |
633 | 12.8k | while (--match_count >= 0) |
634 | 9.62k | { |
635 | 9.62k | const char *name = matching_vector[match_count]->name; |
636 | 9.62k | *(const char **) &matching_vector[match_count] = name; |
637 | 9.62k | } |
638 | 3.18k | } |
639 | 80.2k | else |
640 | 80.2k | free (matching_vector); |
641 | 83.4k | if (cleanup) |
642 | 83.4k | cleanup (abfd); |
643 | 504k | out: |
644 | 504k | if (preserve_match.marker != NULL) |
645 | 0 | bfd_preserve_finish (abfd, &preserve_match); |
646 | 504k | bfd_preserve_restore (abfd, &preserve); |
647 | 504k | bfd_set_error_handler (orig_error_handler); |
648 | 504k | struct per_xvec_message **list = _bfd_per_xvec_warn (NULL, 0); |
649 | 504k | struct per_xvec_message **one = NULL; |
650 | 131M | for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++) |
651 | 131M | { |
652 | 131M | if (list[i]) |
653 | 32.8k | { |
654 | 32.8k | if (!one) |
655 | 24.8k | one = list + i; |
656 | 7.93k | else |
657 | 7.93k | { |
658 | 7.93k | one = NULL; |
659 | 7.93k | break; |
660 | 7.93k | } |
661 | 32.8k | } |
662 | 131M | } |
663 | 504k | if (one) |
664 | 16.9k | print_warnmsg (one); |
665 | 133M | for (size_t i = 0; i < _bfd_target_vector_entries + 1; i++) |
666 | 132M | clear_warnmsg (list++); |
667 | 504k | --in_check_format; |
668 | 504k | return false; |
669 | 83.4k | } |
670 | | |
671 | | /* |
672 | | FUNCTION |
673 | | bfd_set_format |
674 | | |
675 | | SYNOPSIS |
676 | | bool bfd_set_format (bfd *abfd, bfd_format format); |
677 | | |
678 | | DESCRIPTION |
679 | | This function sets the file format of the BFD @var{abfd} to the |
680 | | format @var{format}. If the target set in the BFD does not |
681 | | support the format requested, the format is invalid, or the BFD |
682 | | is not open for writing, then an error occurs. |
683 | | */ |
684 | | |
685 | | bool |
686 | | bfd_set_format (bfd *abfd, bfd_format format) |
687 | 4.69k | { |
688 | 4.69k | if (bfd_read_p (abfd) |
689 | 4.69k | || (unsigned int) abfd->format >= (unsigned int) bfd_type_end) |
690 | 0 | { |
691 | 0 | bfd_set_error (bfd_error_invalid_operation); |
692 | 0 | return false; |
693 | 0 | } |
694 | | |
695 | 4.69k | if (abfd->format != bfd_unknown) |
696 | 1.44k | return abfd->format == format; |
697 | | |
698 | | /* Presume the answer is yes. */ |
699 | 3.24k | abfd->format = format; |
700 | | |
701 | 3.24k | if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) |
702 | 554 | { |
703 | 554 | abfd->format = bfd_unknown; |
704 | 554 | return false; |
705 | 554 | } |
706 | | |
707 | 2.69k | return true; |
708 | 3.24k | } |
709 | | |
710 | | /* |
711 | | FUNCTION |
712 | | bfd_format_string |
713 | | |
714 | | SYNOPSIS |
715 | | const char *bfd_format_string (bfd_format format); |
716 | | |
717 | | DESCRIPTION |
718 | | Return a pointer to a const string |
719 | | <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>, |
720 | | depending upon the value of @var{format}. |
721 | | */ |
722 | | |
723 | | const char * |
724 | | bfd_format_string (bfd_format format) |
725 | 0 | { |
726 | 0 | if (((int) format < (int) bfd_unknown) |
727 | 0 | || ((int) format >= (int) bfd_type_end)) |
728 | 0 | return "invalid"; |
729 | | |
730 | 0 | switch (format) |
731 | 0 | { |
732 | 0 | case bfd_object: |
733 | 0 | return "object"; /* Linker/assembler/compiler output. */ |
734 | 0 | case bfd_archive: |
735 | 0 | return "archive"; /* Object archive file. */ |
736 | 0 | case bfd_core: |
737 | 0 | return "core"; /* Core dump. */ |
738 | 0 | default: |
739 | 0 | return "unknown"; |
740 | 0 | } |
741 | 0 | } |