]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/format.c
[gdb/symtab] Work around PR gas/29517, dwarf2 case
[thirdparty/binutils-gdb.git] / bfd / format.c
CommitLineData
252b5132 1/* Generic BFD support for file formats.
fd67aa11 2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
252b5132
RH
3 Written by Cygnus Support.
4
ed781d5d 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
ed781d5d
NC
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
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
ed781d5d 10 (at your option) any later version.
252b5132 11
ed781d5d
NC
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.
252b5132 16
ed781d5d
NC
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
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132
RH
22
23/*
24SECTION
25 File formats
26
27 A format is a BFD concept of high level file contents type. The
3fde5a36 28 formats supported by BFD are:
252b5132
RH
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
1b74d094
BW
42SUBSECTION
43 File format functions
252b5132
RH
44*/
45
252b5132 46#include "sysdep.h"
3db64b00 47#include "bfd.h"
252b5132
RH
48#include "libbfd.h"
49
50/* IMPORT from targets.c. */
51extern const size_t _bfd_target_vector_entries;
52
53/*
54FUNCTION
55 bfd_check_format
56
57SYNOPSIS
0a1b45a2 58 bool bfd_check_format (bfd *abfd, bfd_format format);
252b5132
RH
59
60DESCRIPTION
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
b34976b6 73 The function returns <<TRUE>> on success, otherwise <<FALSE>>
3fde5a36 74 with one of the following error codes:
252b5132
RH
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.
20bf7711
TT
89
90 When calling bfd_check_format (or bfd_check_format_matches),
91 any underlying file descriptor will be kept open for the
92 duration of the call. This is done to avoid races when
93 another thread calls bfd_cache_close_all. In this scenario,
94 the thread calling bfd_check_format must call bfd_cache_close
95 itself.
252b5132
RH
96*/
97
0a1b45a2 98bool
c58b9523 99bfd_check_format (bfd *abfd, bfd_format format)
252b5132
RH
100{
101 return bfd_check_format_matches (abfd, format, NULL);
102}
103
c9ba0c87
AM
104struct bfd_preserve
105{
106 void *marker;
107 void *tdata;
108 flagword flags;
f656f9c7
AM
109 const struct bfd_iovec *iovec;
110 void *iostream;
c9ba0c87 111 const struct bfd_arch_info *arch_info;
24a6c5ae
AM
112 const struct bfd_build_id *build_id;
113 bfd_cleanup cleanup;
c9ba0c87
AM
114 struct bfd_section *sections;
115 struct bfd_section *section_last;
116 unsigned int section_count;
7cf7fcc8 117 unsigned int section_id;
24a6c5ae
AM
118 unsigned int symcount;
119 bool read_only;
120 bfd_vma start_address;
c9ba0c87
AM
121 struct bfd_hash_table section_htab;
122};
123
124/* When testing an object for compatibility with a particular target
125 back-end, the back-end object_p function needs to set up certain
126 fields in the bfd on successfully recognizing the object. This
127 typically happens in a piecemeal fashion, with failures possible at
128 many points. On failure, the bfd is supposed to be restored to its
129 initial state, which is virtually impossible. However, restoring a
130 subset of the bfd state works in practice. This function stores
131 the subset. */
132
0a1b45a2 133static bool
f5714099
AM
134bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
135 bfd_cleanup cleanup)
c9ba0c87
AM
136{
137 preserve->tdata = abfd->tdata.any;
138 preserve->arch_info = abfd->arch_info;
139 preserve->flags = abfd->flags;
f656f9c7
AM
140 preserve->iovec = abfd->iovec;
141 preserve->iostream = abfd->iostream;
c9ba0c87
AM
142 preserve->sections = abfd->sections;
143 preserve->section_last = abfd->section_last;
144 preserve->section_count = abfd->section_count;
7cf7fcc8 145 preserve->section_id = _bfd_section_id;
24a6c5ae
AM
146 preserve->symcount = abfd->symcount;
147 preserve->read_only = abfd->read_only;
148 preserve->start_address = abfd->start_address;
c9ba0c87
AM
149 preserve->section_htab = abfd->section_htab;
150 preserve->marker = bfd_alloc (abfd, 1);
5d9bbb73 151 preserve->build_id = abfd->build_id;
f5714099 152 preserve->cleanup = cleanup;
c9ba0c87 153 if (preserve->marker == NULL)
0a1b45a2 154 return false;
c9ba0c87
AM
155
156 return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
157 sizeof (struct section_hash_entry));
158}
159
5d1fefd3
AM
160/* A back-end object_p function may flip a bfd from file backed to
161 in-memory, eg. pe_ILF_object_p. In that case to restore the
162 original IO state we need to reopen the file. Conversely, if we
163 are restoring a previously matched pe ILF format and have been
164 checking further target matches using file IO then we need to close
165 the file and detach the bfd from the cache lru list. */
166
167static void
168io_reinit (bfd *abfd, struct bfd_preserve *preserve)
169{
170 if (abfd->iovec != preserve->iovec)
171 {
172 /* Handle file backed to in-memory transition. bfd_cache_close
cf86e13d
AM
173 won't do anything unless abfd->iovec is the cache_iovec.
174 Don't be tempted to call iovec->bclose here. We don't want
175 to call memory_bclose, which would free the bim. The bim
176 must be kept if bfd_check_format_matches is going to decide
177 later that the PE format needing it is in fact the correct
178 target match. */
5d1fefd3
AM
179 bfd_cache_close (abfd);
180 abfd->iovec = preserve->iovec;
cf86e13d 181 abfd->iostream = preserve->iostream;
4ace84a1 182
5d1fefd3
AM
183 /* Handle in-memory to file backed transition. */
184 if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0
185 && (abfd->flags & BFD_IN_MEMORY) != 0
186 && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0
187 && (preserve->flags & BFD_IN_MEMORY) == 0)
188 bfd_open_file (abfd);
189 }
190 abfd->flags = preserve->flags;
191}
192
c9ba0c87
AM
193/* Clear out a subset of BFD state. */
194
195static void
f656f9c7
AM
196bfd_reinit (bfd *abfd, unsigned int section_id,
197 struct bfd_preserve *preserve, bfd_cleanup cleanup)
c9ba0c87 198{
cb001c0d
AM
199 _bfd_section_id = section_id;
200 if (cleanup)
201 cleanup (abfd);
c9ba0c87
AM
202 abfd->tdata.any = NULL;
203 abfd->arch_info = &bfd_default_arch_struct;
5d1fefd3 204 io_reinit (abfd, preserve);
24a6c5ae
AM
205 abfd->symcount = 0;
206 abfd->read_only = 0;
207 abfd->start_address = 0;
6d661cdc 208 abfd->build_id = NULL;
c9ba0c87
AM
209 bfd_section_list_clear (abfd);
210}
211
212/* Restores bfd state saved by bfd_preserve_save. */
213
f5714099 214static bfd_cleanup
c9ba0c87
AM
215bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
216{
217 bfd_hash_table_free (&abfd->section_htab);
218
219 abfd->tdata.any = preserve->tdata;
220 abfd->arch_info = preserve->arch_info;
5d1fefd3 221 io_reinit (abfd, preserve);
c9ba0c87
AM
222 abfd->section_htab = preserve->section_htab;
223 abfd->sections = preserve->sections;
224 abfd->section_last = preserve->section_last;
225 abfd->section_count = preserve->section_count;
7cf7fcc8 226 _bfd_section_id = preserve->section_id;
24a6c5ae
AM
227 abfd->symcount = preserve->symcount;
228 abfd->read_only = preserve->read_only;
229 abfd->start_address = preserve->start_address;
5d9bbb73 230 abfd->build_id = preserve->build_id;
c9ba0c87
AM
231
232 /* bfd_release frees all memory more recently bfd_alloc'd than
233 its arg, as well as its arg. */
234 bfd_release (abfd, preserve->marker);
235 preserve->marker = NULL;
f5714099 236 return preserve->cleanup;
c9ba0c87
AM
237}
238
239/* Called when the bfd state saved by bfd_preserve_save is no longer
240 needed. */
241
242static void
243bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
244{
f5714099
AM
245 if (preserve->cleanup)
246 {
247 /* Run the cleanup, assuming that all it will need is the
248 tdata at the time the cleanup was returned. */
249 void *tdata = abfd->tdata.any;
250 abfd->tdata.any = preserve->tdata;
251 preserve->cleanup (abfd);
252 abfd->tdata.any = tdata;
253 }
c9ba0c87
AM
254 /* It would be nice to be able to free more memory here, eg. old
255 tdata, but that's not possible since these blocks are sitting
256 inside bfd_alloc'd memory. The section hash is on a separate
257 objalloc. */
258 bfd_hash_table_free (&preserve->section_htab);
259 preserve->marker = NULL;
260}
261
5aa0f10c 262static void
499766a6
AM
263print_warnmsg (struct per_xvec_message **list)
264{
499766a6 265 for (struct per_xvec_message *warn = *list; warn; warn = warn->next)
bd8d7625 266 _bfd_error_handler ("%s", warn->message);
499766a6
AM
267}
268
269static void
270clear_warnmsg (struct per_xvec_message **list)
271{
272 struct per_xvec_message *warn = *list;
273 while (warn)
274 {
275 struct per_xvec_message *next = warn->next;
276 free (warn);
277 warn = next;
278 }
279 *list = NULL;
280}
281
bacc61fd
TT
282/* Free all the storage in LIST. Note that the first element of LIST
283 is special and is assumed to be stack-allocated. TARG is used for
284 re-issuing warning messages. If TARG is PER_XVEC_NO_TARGET, then
285 it acts like a sort of wildcard -- messages are only reissued if
286 they are all associated with a single BFD target, regardless of
287 which one it is. If TARG is anything else, then only messages
288 associated with TARG are emitted. */
289
499766a6 290static void
bacc61fd
TT
291print_and_clear_messages (struct per_xvec_messages *list,
292 const bfd_target *targ)
5aa0f10c 293{
bacc61fd
TT
294 struct per_xvec_messages *iter = list;
295
296 if (targ == PER_XVEC_NO_TARGET && list->next == NULL)
297 print_warnmsg (&list->messages);
298
299 while (iter != NULL)
300 {
301 struct per_xvec_messages *next = iter->next;
302
303 if (iter->targ == targ)
304 print_warnmsg (&iter->messages);
305 clear_warnmsg (&iter->messages);
306 if (iter != list)
307 free (iter);
308 iter = next;
309 }
5aa0f10c
AM
310}
311
bb9a951f
L
312/* This a copy of lto_section defined in GCC (lto-streamer.h). */
313
314struct lto_section
315{
316 int16_t major_version;
317 int16_t minor_version;
318 unsigned char slim_object;
319
320 /* Flags is a private field that is not defined publicly. */
321 uint16_t flags;
322};
323
324/* Set lto_type in ABFD. */
325
326static void
327bfd_set_lto_type (bfd *abfd ATTRIBUTE_UNUSED)
328{
329#if BFD_SUPPORTS_PLUGINS
330 if (abfd->format == bfd_object
331 && abfd->lto_type == lto_non_object
332 && (abfd->flags & (DYNAMIC | EXEC_P)) == 0)
333 {
334 asection *sec;
335 enum bfd_lto_object_type type = lto_non_ir_object;
336 struct lto_section lsection;
337 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
338 section. */
339 for (sec = abfd->sections; sec != NULL; sec = sec->next)
340 if (startswith (sec->name, ".gnu.lto_.lto.")
341 && bfd_get_section_contents (abfd, sec, &lsection, 0,
342 sizeof (struct lto_section)))
343 {
344 if (lsection.slim_object)
345 type = lto_slim_ir_object;
346 else
347 type = lto_fat_ir_object;
348 break;
349 }
350
351 abfd->lto_type = type;
352 }
353#endif
354}
355
252b5132
RH
356/*
357FUNCTION
358 bfd_check_format_matches
359
360SYNOPSIS
0a1b45a2 361 bool bfd_check_format_matches
c58b9523 362 (bfd *abfd, bfd_format format, char ***matching);
252b5132
RH
363
364DESCRIPTION
b34976b6 365 Like <<bfd_check_format>>, except when it returns FALSE with
252b5132
RH
366 <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
367 case, if @var{matching} is not NULL, it will be filled in with
368 a NULL-terminated list of the names of the formats that matched,
369 allocated with <<malloc>>.
370 Then the user may choose a format and try again.
371
372 When done with the list that @var{matching} points to, the caller
3fde5a36 373 should free it.
252b5132
RH
374*/
375
0a1b45a2 376bool
c58b9523 377bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
252b5132
RH
378{
379 extern const bfd_target binary_vec;
64bfc258
JM
380#if BFD_SUPPORTS_PLUGINS
381 extern const bfd_target plugin_vec;
382#endif
08f74004
AM
383 const bfd_target * const *target;
384 const bfd_target **matching_vector = NULL;
0aabe54e
AM
385 const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
386 int match_count, best_count, best_match;
3619ad04 387 int ar_match_index;
7cf7fcc8 388 unsigned int initial_section_id = _bfd_section_id;
ea933f17 389 struct bfd_preserve preserve, preserve_match;
cb001c0d 390 bfd_cleanup cleanup = NULL;
bacc61fd
TT
391 struct per_xvec_messages messages = { abfd, PER_XVEC_NO_TARGET, NULL, NULL };
392 struct per_xvec_messages *orig_messages;
20bf7711 393 bool old_in_format_matches;
252b5132 394
5a1dcb7e
AM
395 if (matching != NULL)
396 *matching = NULL;
397
3619ad04 398 if (!bfd_read_p (abfd)
cea4409c 399 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
1d713d9e
NC
400 {
401 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 402 return false;
1d713d9e 403 }
252b5132
RH
404
405 if (abfd->format != bfd_unknown)
bb9a951f
L
406 {
407 bfd_set_lto_type (abfd);
408 return abfd->format == format;
409 }
252b5132 410
5a1dcb7e 411 if (matching != NULL || *bfd_associated_vector != NULL)
252b5132 412 {
986f0783 413 size_t amt;
dc810e39 414
08f74004 415 amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
a50b1753 416 matching_vector = (const bfd_target **) bfd_malloc (amt);
252b5132 417 if (!matching_vector)
0a1b45a2 418 return false;
252b5132 419 }
3fde5a36 420
20bf7711
TT
421 /* Avoid clashes with bfd_cache_close_all running in another
422 thread. */
423 if (!bfd_cache_set_uncloseable (abfd, true, &old_in_format_matches))
424 return false;
425
1d713d9e 426 /* Presume the answer is yes. */
252b5132 427 abfd->format = format;
c9ba0c87 428 save_targ = abfd->xvec;
ea933f17 429
499766a6
AM
430 /* Don't report errors on recursive calls checking the first element
431 of an archive. */
bacc61fd 432 orig_messages = _bfd_set_error_handler_caching (&messages);
499766a6 433
ea933f17 434 preserve_match.marker = NULL;
f5714099 435 if (!bfd_preserve_save (abfd, &preserve, NULL))
ea933f17 436 goto err_ret;
252b5132
RH
437
438 /* If the target type was explicitly specified, just check that target. */
1d713d9e
NC
439 if (!abfd->target_defaulted)
440 {
226f9f4f 441 if (bfd_seek (abfd, 0, SEEK_SET) != 0) /* rewind! */
5a1dcb7e 442 goto err_ret;
3fde5a36 443
cb001c0d 444 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
3fde5a36 445
cb001c0d 446 if (cleanup)
5a1dcb7e 447 goto ok_ret;
1d713d9e
NC
448
449 /* For a long time the code has dropped through to check all
450 targets if the specified target was wrong. I don't know why,
451 and I'm reluctant to change it. However, in the case of an
452 archive, it can cause problems. If the specified target does
453 not permit archives (e.g., the binary target), then we should
454 not allow some other target to recognize it as an archive, but
455 should instead allow the specified target to recognize it as an
456 object. When I first made this change, it broke the PE target,
457 because the specified pei-i386 target did not recognize the
458 actual pe-i386 archive. Since there may be other problems of
459 this sort, I changed this test to check only for the binary
460 target. */
461 if (format == bfd_archive && save_targ == &binary_vec)
5a1dcb7e 462 goto err_unrecog;
1d713d9e
NC
463 }
464
c9ba0c87
AM
465 /* Since the target type was defaulted, check them all in the hope
466 that one will be uniquely recognized. */
467 right_targ = NULL;
468 ar_right_targ = NULL;
469 match_targ = NULL;
470 best_match = 256;
471 best_count = 0;
472 match_count = 0;
473 ar_match_index = _bfd_target_vector_entries;
474
1d713d9e
NC
475 for (target = bfd_target_vector; *target != NULL; target++)
476 {
ea933f17 477 void **high_water;
3fde5a36 478
999d6dff
AM
479 /* The binary target matches anything, so don't return it when
480 searching. Don't match the plugin target if we have another
481 alternative since we want to properly set the input format
482 before allowing a plugin to claim the file. Also, don't
483 check the default target twice. */
25bbc984 484 if (*target == &binary_vec
999d6dff
AM
485#if BFD_SUPPORTS_PLUGINS
486 || (match_count != 0 && *target == &plugin_vec)
487#endif
7cf7fcc8 488 || (!abfd->target_defaulted && *target == save_targ))
1d713d9e 489 continue;
3fde5a36 490
c9ba0c87
AM
491 /* If we already tried a match, the bfd is modified and may
492 have sections attached, which will confuse the next
493 _bfd_check_format call. */
f656f9c7 494 bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
ea933f17
AM
495 /* Free bfd_alloc memory too. If we have matched and preserved
496 a target then the high water mark is that much higher. */
497 if (preserve_match.marker)
498 high_water = &preserve_match.marker;
499 else
500 high_water = &preserve.marker;
501 bfd_release (abfd, *high_water);
502 *high_water = bfd_alloc (abfd, 1);
c9ba0c87
AM
503
504 /* Change BFD's target temporarily. */
505 abfd->xvec = *target;
3fde5a36 506
226f9f4f 507 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
5a1dcb7e 508 goto err_ret;
3fde5a36 509
cb001c0d
AM
510 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
511 if (cleanup)
c9ba0c87 512 {
cb001c0d 513 int match_priority = abfd->xvec->match_priority;
64bfc258
JM
514#if BFD_SUPPORTS_PLUGINS
515 /* If this object can be handled by a plugin, give that the
516 lowest priority; objects both handled by a plugin and
517 with an underlying object format will be claimed
518 separately by the plugin. */
519 if (*target == &plugin_vec)
520 match_priority = (*target)->match_priority;
521#endif
522
89d7b8aa
AM
523 if (abfd->format != bfd_archive
524 || (bfd_has_map (abfd)
525 && bfd_get_error () != bfd_error_wrong_object_format))
0aabe54e 526 {
89d7b8aa
AM
527 /* If this is the default target, accept it, even if
528 other targets might match. People who want those
529 other targets have to set the GNUTARGET variable. */
cb001c0d 530 if (abfd->xvec == bfd_default_vector[0])
89d7b8aa
AM
531 goto ok_ret;
532
533 if (matching_vector)
cb001c0d 534 matching_vector[match_count] = abfd->xvec;
89d7b8aa
AM
535 match_count++;
536
64bfc258 537 if (match_priority < best_match)
89d7b8aa 538 {
64bfc258 539 best_match = match_priority;
89d7b8aa
AM
540 best_count = 0;
541 }
7cf7fcc8
AM
542 if (match_priority <= best_match)
543 {
544 /* This format checks out as ok! */
cb001c0d 545 right_targ = abfd->xvec;
7cf7fcc8
AM
546 best_count++;
547 }
89d7b8aa
AM
548 }
549 else
550 {
551 /* An archive with no armap or objects of the wrong
552 type. We want this target to match if we get no
553 better matches. */
554 if (ar_right_targ != bfd_default_vector[0])
555 ar_right_targ = *target;
556 if (matching_vector)
557 matching_vector[ar_match_index] = *target;
558 ar_match_index++;
0aabe54e 559 }
c9ba0c87 560
ea933f17
AM
561 if (preserve_match.marker == NULL)
562 {
cb001c0d 563 match_targ = abfd->xvec;
f5714099 564 if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
ea933f17 565 goto err_ret;
f5714099 566 cleanup = NULL;
ea933f17 567 }
89d7b8aa 568 }
1d713d9e 569 }
3fde5a36 570
0aabe54e
AM
571 if (best_count == 1)
572 match_count = 1;
573
3619ad04
AM
574 if (match_count == 0)
575 {
576 /* Try partial matches. */
577 right_targ = ar_right_targ;
ed781d5d 578
3619ad04
AM
579 if (right_targ == bfd_default_vector[0])
580 {
581 match_count = 1;
582 }
583 else
584 {
585 match_count = ar_match_index - _bfd_target_vector_entries;
ed781d5d 586
5a1dcb7e 587 if (matching_vector && match_count > 1)
ed781d5d
NC
588 memcpy (matching_vector,
589 matching_vector + _bfd_target_vector_entries,
590 sizeof (*matching_vector) * match_count);
08f74004
AM
591 }
592 }
593
03ae2d5e
AM
594 /* We have more than one equally good match. If any of the best
595 matches is a target in config.bfd targ_defvec or targ_selvecs,
596 choose it. */
5a1dcb7e 597 if (match_count > 1)
08f74004
AM
598 {
599 const bfd_target * const *assoc = bfd_associated_vector;
600
601 while ((right_targ = *assoc++) != NULL)
602 {
603 int i = match_count;
604
605 while (--i >= 0)
03ae2d5e
AM
606 if (matching_vector[i] == right_targ
607 && right_targ->match_priority <= best_match)
08f74004
AM
608 break;
609
610 if (i >= 0)
611 {
612 match_count = 1;
613 break;
3619ad04
AM
614 }
615 }
616 }
617
03ae2d5e
AM
618 /* We still have more than one equally good match, and at least some
619 of the targets support match priority. Choose the first of the
620 best matches. */
033539e2 621 if (matching_vector && match_count > 1 && best_count != match_count)
03ae2d5e
AM
622 {
623 int i;
624
625 for (i = 0; i < match_count; i++)
626 {
627 right_targ = matching_vector[i];
628 if (right_targ->match_priority <= best_match)
629 break;
630 }
631 match_count = 1;
632 }
633
c9ba0c87
AM
634 /* There is way too much undoing of half-known state here. We
635 really shouldn't iterate on live bfd's. Note that saving the
636 whole bfd and restoring it would be even worse; the first thing
637 you notice is that the cached bfd file position gets out of sync. */
ea933f17 638 if (preserve_match.marker != NULL)
f5714099 639 cleanup = bfd_preserve_restore (abfd, &preserve_match);
c9ba0c87 640
1d713d9e
NC
641 if (match_count == 1)
642 {
0aabe54e
AM
643 abfd->xvec = right_targ;
644 /* If we come out of the loop knowing that the last target that
645 matched is the one we want, then ABFD should still be in a usable
ea933f17
AM
646 state (except possibly for XVEC). This is not just an
647 optimisation. In the case of plugins a match against the
648 plugin target can result in the bfd being changed such that
649 it no longer matches the plugin target, nor will it match
650 RIGHT_TARG again. */
0aabe54e
AM
651 if (match_targ != right_targ)
652 {
f656f9c7 653 bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
ea933f17 654 bfd_release (abfd, preserve.marker);
226f9f4f 655 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
0aabe54e 656 goto err_ret;
cb001c0d
AM
657 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
658 BFD_ASSERT (cleanup != NULL);
0aabe54e 659 }
3fde5a36 660
0aabe54e 661 ok_ret:
26ae6d5e
DJ
662 /* If the file was opened for update, then `output_has_begun'
663 some time ago when the file was created. Do not recompute
664 sections sizes or alignments in _bfd_set_section_contents.
665 We can not set this flag until after checking the format,
666 because it will interfere with creation of BFD sections. */
667 if (abfd->direction == both_direction)
0a1b45a2 668 abfd->output_has_begun = true;
26ae6d5e 669
c9594989 670 free (matching_vector);
ea933f17
AM
671 if (preserve_match.marker != NULL)
672 bfd_preserve_finish (abfd, &preserve_match);
673 bfd_preserve_finish (abfd, &preserve);
bacc61fd 674 _bfd_restore_error_handler_caching (orig_messages);
c9ba0c87 675
bacc61fd 676 print_and_clear_messages (&messages, abfd->xvec);
5aa0f10c 677
bb9a951f
L
678 bfd_set_lto_type (abfd);
679
c9ba0c87 680 /* File position has moved, BTW. */
20bf7711 681 return bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
252b5132 682 }
252b5132 683
252b5132
RH
684 if (match_count == 0)
685 {
5a1dcb7e 686 err_unrecog:
252b5132 687 bfd_set_error (bfd_error_file_not_recognized);
5a1dcb7e 688 err_ret:
1039fd9a
AM
689 if (cleanup)
690 cleanup (abfd);
5a1dcb7e
AM
691 abfd->xvec = save_targ;
692 abfd->format = bfd_unknown;
c9594989 693 free (matching_vector);
499766a6 694 goto out;
252b5132 695 }
3619ad04 696
c9ba0c87
AM
697 /* Restore original target type and format. */
698 abfd->xvec = save_targ;
699 abfd->format = bfd_unknown;
5a1dcb7e
AM
700 bfd_set_error (bfd_error_file_ambiguously_recognized);
701
702 if (matching)
703 {
704 *matching = (char **) matching_vector;
705 matching_vector[match_count] = NULL;
706 /* Return target names. This is a little nasty. Maybe we
707 should do another bfd_malloc? */
708 while (--match_count >= 0)
3619ad04 709 {
5a1dcb7e
AM
710 const char *name = matching_vector[match_count]->name;
711 *(const char **) &matching_vector[match_count] = name;
3619ad04
AM
712 }
713 }
c9594989 714 else
9d78076e 715 free (matching_vector);
1039fd9a
AM
716 if (cleanup)
717 cleanup (abfd);
499766a6 718 out:
ea933f17
AM
719 if (preserve_match.marker != NULL)
720 bfd_preserve_finish (abfd, &preserve_match);
721 bfd_preserve_restore (abfd, &preserve);
bacc61fd
TT
722 _bfd_restore_error_handler_caching (orig_messages);
723 print_and_clear_messages (&messages, PER_XVEC_NO_TARGET);
20bf7711 724 bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
0a1b45a2 725 return false;
252b5132
RH
726}
727
728/*
729FUNCTION
730 bfd_set_format
731
732SYNOPSIS
0a1b45a2 733 bool bfd_set_format (bfd *abfd, bfd_format format);
252b5132
RH
734
735DESCRIPTION
736 This function sets the file format of the BFD @var{abfd} to the
737 format @var{format}. If the target set in the BFD does not
738 support the format requested, the format is invalid, or the BFD
739 is not open for writing, then an error occurs.
252b5132
RH
740*/
741
0a1b45a2 742bool
c58b9523 743bfd_set_format (bfd *abfd, bfd_format format)
252b5132 744{
3619ad04 745 if (bfd_read_p (abfd)
cea4409c 746 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
1d713d9e
NC
747 {
748 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 749 return false;
1d713d9e 750 }
252b5132
RH
751
752 if (abfd->format != bfd_unknown)
b34976b6 753 return abfd->format == format;
252b5132 754
1d713d9e 755 /* Presume the answer is yes. */
252b5132
RH
756 abfd->format = format;
757
1d713d9e
NC
758 if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
759 {
760 abfd->format = bfd_unknown;
0a1b45a2 761 return false;
1d713d9e 762 }
252b5132 763
0a1b45a2 764 return true;
252b5132
RH
765}
766
252b5132
RH
767/*
768FUNCTION
769 bfd_format_string
770
771SYNOPSIS
ed781d5d 772 const char *bfd_format_string (bfd_format format);
252b5132
RH
773
774DESCRIPTION
775 Return a pointer to a const string
776 <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
777 depending upon the value of @var{format}.
778*/
779
3619ad04 780const char *
c58b9523 781bfd_format_string (bfd_format format)
252b5132 782{
c58b9523
AM
783 if (((int) format < (int) bfd_unknown)
784 || ((int) format >= (int) bfd_type_end))
252b5132 785 return "invalid";
3fde5a36 786
1d713d9e
NC
787 switch (format)
788 {
789 case bfd_object:
7dee875e 790 return "object"; /* Linker/assembler/compiler output. */
3fde5a36 791 case bfd_archive:
1d713d9e 792 return "archive"; /* Object archive file. */
3fde5a36 793 case bfd_core:
1d713d9e 794 return "core"; /* Core dump. */
3fde5a36 795 default:
1d713d9e
NC
796 return "unknown";
797 }
252b5132 798}