Add markers for bihnutils 2.44 branch
[binutils-gdb.git] / bfd / format.c
blob7769ad095f6191a8d51088ef75ffe6992741590b
1 /* Generic BFD support for file formats.
2 Copyright (C) 1990-2025 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
24 SECTION
25 File formats
27 A format is a BFD concept of high level file contents type. The
28 formats supported by BFD are:
30 o <<bfd_object>>
32 The BFD may contain data, symbols, relocations and debug info.
34 o <<bfd_archive>>
36 The BFD contains other BFDs and an optional index.
38 o <<bfd_core>>
40 The BFD contains the result of an executable core dump.
42 SUBSECTION
43 File format functions
46 #include "sysdep.h"
47 #include "bfd.h"
48 #include "libbfd.h"
49 #if BFD_SUPPORTS_PLUGINS
50 #include "plugin-api.h"
51 #include "plugin.h"
52 #endif
54 /* IMPORT from targets.c. */
55 extern const size_t _bfd_target_vector_entries;
58 FUNCTION
59 bfd_check_format
61 SYNOPSIS
62 bool bfd_check_format (bfd *abfd, bfd_format format);
64 DESCRIPTION
65 Verify if the file attached to the BFD @var{abfd} is compatible
66 with the format @var{format} (i.e., one of <<bfd_object>>,
67 <<bfd_archive>> or <<bfd_core>>).
69 If the BFD has been set to a specific target before the
70 call, only the named target and format combination is
71 checked. If the target has not been set, or has been set to
72 <<default>>, then all the known target backends is
73 interrogated to determine a match. If the default target
74 matches, it is used. If not, exactly one target must recognize
75 the file, or an error results.
77 The function returns <<TRUE>> on success, otherwise <<FALSE>>
78 with one of the following error codes:
80 o <<bfd_error_invalid_operation>> -
81 if <<format>> is not one of <<bfd_object>>, <<bfd_archive>> or
82 <<bfd_core>>.
84 o <<bfd_error_system_call>> -
85 if an error occured during a read - even some file mismatches
86 can cause bfd_error_system_calls.
88 o <<file_not_recognised>> -
89 none of the backends recognised the file format.
91 o <<bfd_error_file_ambiguously_recognized>> -
92 more than one backend recognised the file format.
94 When calling bfd_check_format (or bfd_check_format_matches),
95 any underlying file descriptor will be kept open for the
96 duration of the call. This is done to avoid races when
97 another thread calls bfd_cache_close_all. In this scenario,
98 the thread calling bfd_check_format must call bfd_cache_close
99 itself.
102 bool
103 bfd_check_format (bfd *abfd, bfd_format format)
105 return bfd_check_format_matches (abfd, format, NULL);
108 struct bfd_preserve
110 void *marker;
111 void *tdata;
112 flagword flags;
113 const struct bfd_iovec *iovec;
114 void *iostream;
115 const struct bfd_arch_info *arch_info;
116 const struct bfd_build_id *build_id;
117 bfd_cleanup cleanup;
118 struct bfd_section *sections;
119 struct bfd_section *section_last;
120 unsigned int section_count;
121 unsigned int section_id;
122 unsigned int symcount;
123 bool read_only;
124 bfd_vma start_address;
125 struct bfd_hash_table section_htab;
128 /* When testing an object for compatibility with a particular target
129 back-end, the back-end object_p function needs to set up certain
130 fields in the bfd on successfully recognizing the object. This
131 typically happens in a piecemeal fashion, with failures possible at
132 many points. On failure, the bfd is supposed to be restored to its
133 initial state, which is virtually impossible. However, restoring a
134 subset of the bfd state works in practice. This function stores
135 the subset. */
137 static bool
138 bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve,
139 bfd_cleanup cleanup)
141 preserve->tdata = abfd->tdata.any;
142 preserve->arch_info = abfd->arch_info;
143 preserve->flags = abfd->flags;
144 preserve->iovec = abfd->iovec;
145 preserve->iostream = abfd->iostream;
146 preserve->sections = abfd->sections;
147 preserve->section_last = abfd->section_last;
148 preserve->section_count = abfd->section_count;
149 preserve->section_id = _bfd_section_id;
150 preserve->symcount = abfd->symcount;
151 preserve->read_only = abfd->read_only;
152 preserve->start_address = abfd->start_address;
153 preserve->section_htab = abfd->section_htab;
154 preserve->marker = bfd_alloc (abfd, 1);
155 preserve->build_id = abfd->build_id;
156 preserve->cleanup = cleanup;
157 if (preserve->marker == NULL)
158 return false;
160 return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
161 sizeof (struct section_hash_entry));
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. */
171 static void
172 io_reinit (bfd *abfd, struct bfd_preserve *preserve)
174 if (abfd->iovec != preserve->iovec)
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 bfd_cache_close (abfd);
184 abfd->iovec = preserve->iovec;
185 abfd->iostream = preserve->iostream;
187 /* Handle in-memory to file backed transition. */
188 if ((abfd->flags & BFD_CLOSED_BY_CACHE) != 0
189 && (abfd->flags & BFD_IN_MEMORY) != 0
190 && (preserve->flags & BFD_CLOSED_BY_CACHE) == 0
191 && (preserve->flags & BFD_IN_MEMORY) == 0)
192 bfd_open_file (abfd);
194 abfd->flags = preserve->flags;
197 /* Clear out a subset of BFD state. */
199 static void
200 bfd_reinit (bfd *abfd, unsigned int section_id,
201 struct bfd_preserve *preserve, bfd_cleanup cleanup)
203 _bfd_section_id = section_id;
204 if (cleanup)
205 cleanup (abfd);
206 abfd->tdata.any = NULL;
207 abfd->arch_info = &bfd_default_arch_struct;
208 io_reinit (abfd, preserve);
209 abfd->symcount = 0;
210 abfd->read_only = 0;
211 abfd->start_address = 0;
212 abfd->build_id = NULL;
213 bfd_section_list_clear (abfd);
216 /* Restores bfd state saved by bfd_preserve_save. */
218 static bfd_cleanup
219 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
221 bfd_hash_table_free (&abfd->section_htab);
223 abfd->tdata.any = preserve->tdata;
224 abfd->arch_info = preserve->arch_info;
225 io_reinit (abfd, preserve);
226 abfd->section_htab = preserve->section_htab;
227 abfd->sections = preserve->sections;
228 abfd->section_last = preserve->section_last;
229 abfd->section_count = preserve->section_count;
230 _bfd_section_id = preserve->section_id;
231 abfd->symcount = preserve->symcount;
232 abfd->read_only = preserve->read_only;
233 abfd->start_address = preserve->start_address;
234 abfd->build_id = preserve->build_id;
236 /* bfd_release frees all memory more recently bfd_alloc'd than
237 its arg, as well as its arg. */
238 bfd_release (abfd, preserve->marker);
239 preserve->marker = NULL;
240 return preserve->cleanup;
243 /* Called when the bfd state saved by bfd_preserve_save is no longer
244 needed. */
246 static void
247 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
249 if (preserve->cleanup)
251 /* Run the cleanup, assuming that all it will need is the
252 tdata at the time the cleanup was returned. */
253 void *tdata = abfd->tdata.any;
254 abfd->tdata.any = preserve->tdata;
255 preserve->cleanup (abfd);
256 abfd->tdata.any = tdata;
258 /* It would be nice to be able to free more memory here, eg. old
259 tdata, but that's not possible since these blocks are sitting
260 inside bfd_alloc'd memory. The section hash is on a separate
261 objalloc. */
262 bfd_hash_table_free (&preserve->section_htab);
263 preserve->marker = NULL;
266 static void
267 print_warnmsg (struct per_xvec_message **list)
269 for (struct per_xvec_message *warn = *list; warn; warn = warn->next)
270 _bfd_error_handler ("%s", warn->message);
273 static void
274 clear_warnmsg (struct per_xvec_message **list)
276 struct per_xvec_message *warn = *list;
277 while (warn)
279 struct per_xvec_message *next = warn->next;
280 free (warn);
281 warn = next;
283 *list = NULL;
286 /* Free all the storage in LIST. Note that the first element of LIST
287 is special and is assumed to be stack-allocated. TARG is used for
288 re-issuing warning messages. If TARG is PER_XVEC_NO_TARGET, then
289 it acts like a sort of wildcard -- messages are reissued if all
290 targets with messages have identical messages. One copy of the
291 messages are then reissued. If TARG is anything else, then only
292 messages associated with TARG are emitted. */
294 static void
295 print_and_clear_messages (struct per_xvec_messages *list,
296 const bfd_target *targ)
298 struct per_xvec_messages *iter;
300 if (targ == PER_XVEC_NO_TARGET)
302 iter = list->next;
303 while (iter != NULL)
305 struct per_xvec_message *msg1 = list->messages;
306 struct per_xvec_message *msg2 = iter->messages;
309 if (strcmp (msg1->message, msg2->message))
310 break;
311 msg1 = msg1->next;
312 msg2 = msg2->next;
313 } while (msg1 && msg2);
314 if (msg1 || msg2)
315 break;
316 iter = iter->next;
318 if (iter == NULL)
319 targ = list->targ;
322 iter = list;
323 while (iter != NULL)
325 struct per_xvec_messages *next = iter->next;
327 if (iter->targ == targ)
328 print_warnmsg (&iter->messages);
329 clear_warnmsg (&iter->messages);
330 if (iter != list)
331 free (iter);
332 iter = next;
336 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
338 struct lto_section
340 int16_t major_version;
341 int16_t minor_version;
342 unsigned char slim_object;
344 /* Flags is a private field that is not defined publicly. */
345 uint16_t flags;
348 /* Set lto_type in ABFD. */
350 static void
351 bfd_set_lto_type (bfd *abfd ATTRIBUTE_UNUSED)
353 #if BFD_SUPPORTS_PLUGINS
354 if (abfd->format == bfd_object
355 && abfd->lto_type == lto_non_object
356 && (abfd->flags
357 & (DYNAMIC
358 | (bfd_get_flavour (abfd) == bfd_target_elf_flavour
359 ? EXEC_P : 0))) == 0)
361 asection *sec;
362 enum bfd_lto_object_type type = lto_non_ir_object;
363 struct lto_section lsection = { 0, 0, 0, 0 };
364 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
365 section. */
366 for (sec = abfd->sections; sec != NULL; sec = sec->next)
367 if (strcmp (sec->name, GNU_OBJECT_ONLY_SECTION_NAME) == 0)
369 type = lto_mixed_object;
370 abfd->object_only_section = sec;
371 break;
373 else if (lsection.major_version == 0
374 && startswith (sec->name, ".gnu.lto_.lto.")
375 && bfd_get_section_contents (abfd, sec, &lsection, 0,
376 sizeof (struct lto_section)))
378 if (lsection.slim_object)
379 type = lto_slim_ir_object;
380 else
381 type = lto_fat_ir_object;
384 abfd->lto_type = type;
386 #endif
390 FUNCTION
391 bfd_check_format_matches
393 SYNOPSIS
394 bool bfd_check_format_matches
395 (bfd *abfd, bfd_format format, char ***matching);
397 DESCRIPTION
398 Like <<bfd_check_format>>, except when it returns FALSE with
399 <<bfd_errno>> set to <<bfd_error_file_ambiguously_recognized>>. In that
400 case, if @var{matching} is not NULL, it will be filled in with
401 a NULL-terminated list of the names of the formats that matched,
402 allocated with <<malloc>>.
403 Then the user may choose a format and try again.
405 When done with the list that @var{matching} points to, the caller
406 should free it.
409 bool
410 bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
412 extern const bfd_target binary_vec;
413 const bfd_target * const *target;
414 const bfd_target **matching_vector = NULL;
415 const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
416 int match_count, best_count, best_match;
417 int ar_match_index;
418 unsigned int initial_section_id = _bfd_section_id;
419 struct bfd_preserve preserve, preserve_match;
420 bfd_cleanup cleanup = NULL;
421 struct per_xvec_messages messages = { abfd, PER_XVEC_NO_TARGET, NULL, NULL };
422 struct per_xvec_messages *orig_messages;
423 bool old_in_format_matches;
425 if (matching != NULL)
426 *matching = NULL;
428 if (!bfd_read_p (abfd)
429 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
431 bfd_set_error (bfd_error_invalid_operation);
432 return false;
435 if (abfd->format != bfd_unknown)
437 bfd_set_lto_type (abfd);
438 return abfd->format == format;
441 if (matching != NULL || *bfd_associated_vector != NULL)
443 size_t amt;
445 amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
446 matching_vector = (const bfd_target **) bfd_malloc (amt);
447 if (!matching_vector)
448 return false;
451 /* Avoid clashes with bfd_cache_close_all running in another
452 thread. */
453 if (!bfd_cache_set_uncloseable (abfd, true, &old_in_format_matches))
454 return false;
456 /* Presume the answer is yes. */
457 abfd->format = format;
458 save_targ = abfd->xvec;
460 /* Don't report errors on recursive calls checking the first element
461 of an archive. */
462 orig_messages = _bfd_set_error_handler_caching (&messages);
464 /* Locking is required here in order to manage _bfd_section_id. */
465 if (!bfd_lock ())
466 return false;
468 preserve_match.marker = NULL;
469 if (!bfd_preserve_save (abfd, &preserve, NULL))
470 goto err_ret;
472 /* If the target type was explicitly specified, just check that target. */
473 if (!abfd->target_defaulted)
475 if (bfd_seek (abfd, 0, SEEK_SET) != 0) /* rewind! */
476 goto err_ret;
478 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
480 if (cleanup)
481 goto ok_ret;
483 /* For a long time the code has dropped through to check all
484 targets if the specified target was wrong. I don't know why,
485 and I'm reluctant to change it. However, in the case of an
486 archive, it can cause problems. If the specified target does
487 not permit archives (e.g., the binary target), then we should
488 not allow some other target to recognize it as an archive, but
489 should instead allow the specified target to recognize it as an
490 object. When I first made this change, it broke the PE target,
491 because the specified pei-i386 target did not recognize the
492 actual pe-i386 archive. Since there may be other problems of
493 this sort, I changed this test to check only for the binary
494 target. */
495 if (format == bfd_archive && save_targ == &binary_vec)
496 goto err_unrecog;
499 /* Since the target type was defaulted, check them all in the hope
500 that one will be uniquely recognized. */
501 right_targ = NULL;
502 ar_right_targ = NULL;
503 match_targ = NULL;
504 best_match = 256;
505 best_count = 0;
506 match_count = 0;
507 ar_match_index = _bfd_target_vector_entries;
509 for (target = bfd_target_vector; *target != NULL; target++)
511 void **high_water;
513 /* The binary target matches anything, so don't return it when
514 searching. Don't match the plugin target if we have another
515 alternative since we want to properly set the input format
516 before allowing a plugin to claim the file. Also, don't
517 check the default target twice. */
518 if (*target == &binary_vec
519 #if BFD_SUPPORTS_PLUGINS
520 || (match_count != 0 && bfd_plugin_target_p (*target))
521 #endif
522 || (!abfd->target_defaulted && *target == save_targ))
523 continue;
525 #if BFD_SUPPORTS_PLUGINS
526 /* If the plugin target is explicitly specified when a BFD file
527 is opened, don't check it twice. */
528 if (bfd_plugin_specified_p () && bfd_plugin_target_p (*target))
529 continue;
530 #endif
532 /* If we already tried a match, the bfd is modified and may
533 have sections attached, which will confuse the next
534 _bfd_check_format call. */
535 bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
536 /* Free bfd_alloc memory too. If we have matched and preserved
537 a target then the high water mark is that much higher. */
538 if (preserve_match.marker)
539 high_water = &preserve_match.marker;
540 else
541 high_water = &preserve.marker;
542 bfd_release (abfd, *high_water);
543 *high_water = bfd_alloc (abfd, 1);
545 /* Change BFD's target temporarily. */
546 abfd->xvec = *target;
548 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
549 goto err_ret;
551 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
552 if (cleanup)
554 int match_priority = abfd->xvec->match_priority;
556 if (abfd->format != bfd_archive
557 || (bfd_has_map (abfd)
558 && bfd_get_error () != bfd_error_wrong_object_format))
560 /* If this is the default target, accept it, even if
561 other targets might match. People who want those
562 other targets have to set the GNUTARGET variable. */
563 if (abfd->xvec == bfd_default_vector[0])
564 goto ok_ret;
566 if (matching_vector)
567 matching_vector[match_count] = abfd->xvec;
568 match_count++;
570 if (match_priority < best_match)
572 best_match = match_priority;
573 best_count = 0;
575 if (match_priority <= best_match)
577 /* This format checks out as ok! */
578 right_targ = abfd->xvec;
579 best_count++;
582 else
584 /* An archive with no armap or objects of the wrong
585 type. We want this target to match if we get no
586 better matches. */
587 if (ar_right_targ != bfd_default_vector[0])
588 ar_right_targ = *target;
589 if (matching_vector)
590 matching_vector[ar_match_index] = *target;
591 ar_match_index++;
594 if (preserve_match.marker == NULL)
596 match_targ = abfd->xvec;
597 if (!bfd_preserve_save (abfd, &preserve_match, cleanup))
598 goto err_ret;
599 cleanup = NULL;
604 if (best_count == 1)
605 match_count = 1;
607 if (match_count == 0)
609 /* Try partial matches. */
610 right_targ = ar_right_targ;
612 if (right_targ == bfd_default_vector[0])
614 match_count = 1;
616 else
618 match_count = ar_match_index - _bfd_target_vector_entries;
620 if (matching_vector && match_count > 1)
621 memcpy (matching_vector,
622 matching_vector + _bfd_target_vector_entries,
623 sizeof (*matching_vector) * match_count);
627 /* We have more than one equally good match. If any of the best
628 matches is a target in config.bfd targ_defvec or targ_selvecs,
629 choose it. */
630 if (match_count > 1)
632 const bfd_target * const *assoc = bfd_associated_vector;
634 while ((right_targ = *assoc++) != NULL)
636 int i = match_count;
638 while (--i >= 0)
639 if (matching_vector[i] == right_targ
640 && right_targ->match_priority <= best_match)
641 break;
643 if (i >= 0)
645 match_count = 1;
646 break;
651 /* We still have more than one equally good match, and at least some
652 of the targets support match priority. Choose the first of the
653 best matches. */
654 if (matching_vector && match_count > 1 && best_count != match_count)
656 int i;
658 for (i = 0; i < match_count; i++)
660 right_targ = matching_vector[i];
661 if (right_targ->match_priority <= best_match)
662 break;
664 match_count = 1;
667 /* There is way too much undoing of half-known state here. We
668 really shouldn't iterate on live bfd's. Note that saving the
669 whole bfd and restoring it would be even worse; the first thing
670 you notice is that the cached bfd file position gets out of sync. */
671 if (preserve_match.marker != NULL)
672 cleanup = bfd_preserve_restore (abfd, &preserve_match);
674 if (match_count == 1)
676 abfd->xvec = right_targ;
677 /* If we come out of the loop knowing that the last target that
678 matched is the one we want, then ABFD should still be in a usable
679 state (except possibly for XVEC). This is not just an
680 optimisation. In the case of plugins a match against the
681 plugin target can result in the bfd being changed such that
682 it no longer matches the plugin target, nor will it match
683 RIGHT_TARG again. */
684 if (match_targ != right_targ)
686 bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
687 bfd_release (abfd, preserve.marker);
688 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
689 goto err_ret;
690 cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
691 BFD_ASSERT (cleanup != NULL);
694 ok_ret:
695 /* If the file was opened for update, then `output_has_begun'
696 some time ago when the file was created. Do not recompute
697 sections sizes or alignments in _bfd_set_section_contents.
698 We can not set this flag until after checking the format,
699 because it will interfere with creation of BFD sections. */
700 if (abfd->direction == both_direction)
701 abfd->output_has_begun = true;
703 free (matching_vector);
704 if (preserve_match.marker != NULL)
705 bfd_preserve_finish (abfd, &preserve_match);
706 bfd_preserve_finish (abfd, &preserve);
707 _bfd_restore_error_handler_caching (orig_messages);
709 print_and_clear_messages (&messages, abfd->xvec);
711 bfd_set_lto_type (abfd);
713 /* File position has moved, BTW. */
714 bool ret = bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
715 if (!bfd_unlock ())
716 return false;
717 return ret;
720 if (match_count == 0)
722 err_unrecog:
723 bfd_set_error (bfd_error_file_not_recognized);
724 err_ret:
725 if (cleanup)
726 cleanup (abfd);
727 abfd->xvec = save_targ;
728 abfd->format = bfd_unknown;
729 free (matching_vector);
730 goto out;
733 /* Restore original target type and format. */
734 abfd->xvec = save_targ;
735 abfd->format = bfd_unknown;
736 bfd_set_error (bfd_error_file_ambiguously_recognized);
738 if (matching)
740 *matching = (char **) matching_vector;
741 matching_vector[match_count] = NULL;
742 /* Return target names. This is a little nasty. Maybe we
743 should do another bfd_malloc? */
744 while (--match_count >= 0)
746 const char *name = matching_vector[match_count]->name;
747 *(const char **) &matching_vector[match_count] = name;
750 else
751 free (matching_vector);
752 if (cleanup)
753 cleanup (abfd);
754 out:
755 if (preserve_match.marker != NULL)
756 bfd_preserve_finish (abfd, &preserve_match);
757 bfd_preserve_restore (abfd, &preserve);
758 _bfd_restore_error_handler_caching (orig_messages);
759 print_and_clear_messages (&messages, PER_XVEC_NO_TARGET);
760 bfd_cache_set_uncloseable (abfd, old_in_format_matches, NULL);
761 bfd_unlock ();
762 return false;
766 FUNCTION
767 bfd_set_format
769 SYNOPSIS
770 bool bfd_set_format (bfd *abfd, bfd_format format);
772 DESCRIPTION
773 This function sets the file format of the BFD @var{abfd} to the
774 format @var{format}. If the target set in the BFD does not
775 support the format requested, the format is invalid, or the BFD
776 is not open for writing, then an error occurs.
779 bool
780 bfd_set_format (bfd *abfd, bfd_format format)
782 if (bfd_read_p (abfd)
783 || (unsigned int) abfd->format >= (unsigned int) bfd_type_end)
785 bfd_set_error (bfd_error_invalid_operation);
786 return false;
789 if (abfd->format != bfd_unknown)
790 return abfd->format == format;
792 /* Presume the answer is yes. */
793 abfd->format = format;
795 if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd)))
797 abfd->format = bfd_unknown;
798 return false;
801 return true;
805 FUNCTION
806 bfd_format_string
808 SYNOPSIS
809 const char *bfd_format_string (bfd_format format);
811 DESCRIPTION
812 Return a pointer to a const string
813 <<invalid>>, <<object>>, <<archive>>, <<core>>, or <<unknown>>,
814 depending upon the value of @var{format}.
817 const char *
818 bfd_format_string (bfd_format format)
820 if (((int) format < (int) bfd_unknown)
821 || ((int) format >= (int) bfd_type_end))
822 return "invalid";
824 switch (format)
826 case bfd_object:
827 return "object"; /* Linker/assembler/compiler output. */
828 case bfd_archive:
829 return "archive"; /* Object archive file. */
830 case bfd_core:
831 return "core"; /* Core dump. */
832 default:
833 return "unknown";